Skip to content

Commit 63fd6c9

Browse files
committed
add test + misc
1 parent ddf9db1 commit 63fd6c9

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

nibabel/freesurfer/io.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import division, print_function, absolute_import
22

3+
import warnings
34
import numpy as np
45
import getpass
56
import time
@@ -107,20 +108,31 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
107108

108109
extra = fobj.read() if read_metadata else b''
109110
if extra:
111+
volume_info = OrderedDict()
112+
110113
if extra[:4] != b'\x00\x00\x00\x14':
111114
warnings.warn("Unknown extension code.")
112-
volume_info = OrderedDict()
113-
try:
114-
for line in extra[4:].split(b'\n'):
115-
key, val = map(bytes.strip, line.split(b'=', 1))
116-
key = key.decode('utf-8')
117-
if key in ('voxelsize', 'xras', 'yras', 'zras'):
118-
val = np.fromstring(val, sep=' ')
119-
elif key == 'volume':
120-
val = np.fromstring(val, sep=' ', dtype=np.uint)
121-
volume_info[key] = val
122-
except ValueError:
123-
raise ValueError("Error parsing volume info")
115+
else:
116+
try:
117+
for line in extra[4:].split(b'\n'):
118+
if len(line) == 0:
119+
continue
120+
key, val = map(bytes.strip, line.split(b'=', 1))
121+
print(key, val)
122+
key = key.decode('utf-8')
123+
if key in ('voxelsize', 'xras', 'yras', 'zras', 'cras'):
124+
val = np.fromstring(val, sep=' ')
125+
val = val.astype(np.float)
126+
elif key == 'volume':
127+
val = np.fromstring(val, sep=' ', dtype=np.uint)
128+
val = val.astype(np.int)
129+
volume_info[key] = val
130+
except ValueError:
131+
raise ValueError("Error parsing volume info")
132+
133+
if len(volume_info) == 0:
134+
warnings.warn("Volume geometry info is either "
135+
"not contained or not valid.")
124136

125137
else:
126138
raise ValueError("File does not appear to be a Freesurfer surface")
@@ -160,10 +172,10 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
160172
time.ctime())
161173

162174
postlude = b''
163-
if volume_info is not None:
175+
if volume_info is not None and len(volume_info) > 0:
164176
postlude = [b'\x00\x00\x00\x14']
165177
for key, val in volume_info.items():
166-
if key in ('voxelsize', 'xras', 'yras', 'zras'):
178+
if key in ('voxelsize', 'xras', 'yras', 'zras', 'cras'):
167179
val = '{:.3f} {:.3f} {:.3f}'.format(*val)
168180
elif key == 'volume':
169181
val = '{:d} {:d} {:d}'.format(*val)

nibabel/freesurfer/tests/test_io.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import getpass
55
import time
66
import hashlib
7+
import warnings
78

89

910
from ...tmpdirs import InTemporaryDirectory
@@ -17,7 +18,7 @@
1718

1819
from ...tests.nibabel_data import get_nibabel_data
1920
from ...fileslice import strided_scalar
20-
21+
from ...testing import clear_and_catch_warnings
2122

2223
DATA_SDIR = 'fsaverage'
2324

@@ -55,21 +56,31 @@ def test_geometry():
5556
assert_equal(coords.shape[0], faces.max() + 1)
5657

5758
# Test quad with sphere
58-
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "sphere"))
59-
coords, faces = read_geometry(surf_path)
60-
assert_equal(0, faces.min())
61-
assert_equal(coords.shape[0], faces.max() + 1)
59+
with clear_and_catch_warnings() as w:
60+
warnings.filterwarnings('always', category=DeprecationWarning)
61+
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "sphere"))
62+
coords, faces, volume_info, create_stamp = \
63+
read_geometry(surf_path, read_metadata=True, read_stamp=True)
64+
assert_equal(0, faces.min())
65+
assert_equal(coords.shape[0], faces.max() + 1)
66+
assert_equal(0, len(volume_info))
67+
assert_equal(u'created by greve on Thu Jun 8 19:17:51 2006',
68+
create_stamp)
69+
assert_equal(len(w), 2)
6270

6371
# Test equivalence of freesurfer- and nibabel-generated triangular files
6472
# with respect to read_geometry()
6573
with InTemporaryDirectory():
6674
surf_path = 'test'
6775
create_stamp = "created by %s on %s" % (getpass.getuser(),
6876
time.ctime())
69-
write_geometry(surf_path, coords, faces, create_stamp)
77+
volume_info['cras'] = np.array([1., 2., 3.])
78+
write_geometry(surf_path, coords, faces, create_stamp, volume_info)
7079

71-
coords2, faces2 = read_geometry(surf_path)
80+
coords2, faces2, volume_info2 = \
81+
read_geometry(surf_path, read_metadata=True)
7282

83+
assert_equal(volume_info2['cras'], volume_info['cras'])
7384
with open(surf_path, 'rb') as fobj:
7485
np.fromfile(fobj, ">u1", 3)
7586
read_create_stamp = fobj.readline().decode().rstrip('\n')

0 commit comments

Comments
 (0)