Skip to content

Commit 7eaad8e

Browse files
committed
Add ability to read/write freesurfer surface files in scanner ras
1 parent b5ecf48 commit 7eaad8e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

nibabel/freesurfer/io.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _read_volume_info(fobj):
5757
head = np.fromfile(fobj, '>i4', 1)
5858
if not np.array_equal(head, [20]): # Read two bytes more
5959
head = np.concatenate([head, np.fromfile(fobj, '>i4', 2)])
60-
if not np.array_equal(head, [2, 0, 20]):
60+
if not (np.array_equal(head, [2, 0, 20]) or np.array_equal(head, [2, 1, 20])):
6161
warnings.warn('Unknown extension code.')
6262
return volume_info
6363

@@ -604,6 +604,7 @@ def _serialize_volume_info(volume_info):
604604
if not (
605605
np.array_equal(volume_info[key], [20])
606606
or np.array_equal(volume_info[key], [2, 0, 20])
607+
or np.array_equal(volume_info[key], [2, 1, 20])
607608
):
608609
warnings.warn('Unknown extension code.')
609610
strings.append(np.array(volume_info[key], dtype='>i4').tobytes())

nibabel/freesurfer/tests/test_io.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from os.path import isdir
88
from os.path import join as pjoin
99
from pathlib import Path
10+
import subprocess
1011

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

94+
# Test reading/writing a surface file in scanner RAS
95+
cmd = f"mris_convert --to-scanner {pjoin(data_path, 'surf', 'lh.inflated')} {surf_path}"
96+
_ = subprocess.run(cmd.split(), capture_output=True)
97+
_ ,_, metadata = read_geometry(surf_path, read_metadata=True)
98+
np.testing.assert_array_equal(metadata["head"], [2, 1, 20])
99+
write_geometry(surf_path, coords, faces, create_stamp, metadata)
100+
93101
volume_info['head'] = [1, 2]
94102
with pytest.warns(UserWarning, match='Unknown extension'):
95103
write_geometry(surf_path, coords, faces, create_stamp, volume_info)

0 commit comments

Comments
 (0)