Skip to content

Add ability to read/write freesurfer surface files in scanner ras #1420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nibabel/freesurfer/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _read_volume_info(fobj):
head = np.fromfile(fobj, '>i4', 1)
if not np.array_equal(head, [20]): # Read two bytes more
head = np.concatenate([head, np.fromfile(fobj, '>i4', 2)])
if not np.array_equal(head, [2, 0, 20]):
if not (np.array_equal(head, [2, 0, 20]) or np.array_equal(head, [2, 1, 20])):
warnings.warn('Unknown extension code.')
return volume_info

Expand Down Expand Up @@ -604,6 +604,7 @@ def _serialize_volume_info(volume_info):
if not (
np.array_equal(volume_info[key], [20])
or np.array_equal(volume_info[key], [2, 0, 20])
or np.array_equal(volume_info[key], [2, 1, 20])
):
warnings.warn('Unknown extension code.')
strings.append(np.array(volume_info[key], dtype='>i4').tobytes())
Expand Down
8 changes: 8 additions & 0 deletions nibabel/freesurfer/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from os.path import isdir
from os.path import join as pjoin
from pathlib import Path
import subprocess

import numpy as np
import pytest
Expand Down Expand Up @@ -90,6 +91,13 @@ def test_geometry():
assert any('volume information contained' in str(ww.message) for ww in w)
assert any('extension code' in str(ww.message) for ww in w)

# Test reading/writing a surface file in scanner RAS
cmd = f"mris_convert --to-scanner {pjoin(data_path, 'surf', 'lh.inflated')} {surf_path}"
_ = subprocess.run(cmd.split(), capture_output=True)
_ ,_, metadata = read_geometry(surf_path, read_metadata=True)
np.testing.assert_array_equal(metadata["head"], [2, 1, 20])
write_geometry(surf_path, coords, faces, create_stamp, metadata)

volume_info['head'] = [1, 2]
with pytest.warns(UserWarning, match='Unknown extension'):
write_geometry(surf_path, coords, faces, create_stamp, volume_info)
Expand Down