Skip to content

Commit 45055cd

Browse files
committed
FIX: Minor fixes
1 parent 89203bb commit 45055cd

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

nibabel/freesurfer/io.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ def _fread3_many(fobj, n):
4848
def _read_volume_info(extra):
4949
volume_info = OrderedDict()
5050

51-
if extra is None:
52-
return volume_info
53-
54-
if extra[:4] != b'\x00\x00\x00\x14':
55-
warnings.warn("Unknown extension code.")
51+
if extra is None: # we pass b'' when user does not request info
52+
pass
53+
elif len(extra) == 0:
54+
warnings.warn('No volume information contained in the file')
55+
elif extra[:4] != b'\x00\x00\x00\x14':
56+
warnings.warn("Unknown extension code for volume info: %r" % extra[:4])
5657
else:
5758
try:
5859
for line in extra[4:].split(b'\n'):
@@ -67,13 +68,11 @@ def _read_volume_info(extra):
6768
val = np.fromstring(val, sep=' ', dtype=np.uint)
6869
val = val.astype(np.int)
6970
volume_info[key] = val
70-
except ValueError:
71-
raise ValueError("Error parsing volume info")
72-
73-
if len(volume_info) == 0:
74-
warnings.warn("Volume geometry info is either "
75-
"not contained or not valid.")
76-
71+
except Exception as exp:
72+
raise ValueError("Error parsing volume info: %s" % str(exp))
73+
if len(volume_info) == 0:
74+
warnings.warn("Volume geometry info is either "
75+
"not contained or not valid.")
7776
return volume_info
7877

7978

@@ -136,8 +135,7 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
136135
fnum = np.fromfile(fobj, ">i4", 1)[0]
137136
coords = np.fromfile(fobj, ">f4", vnum * 3).reshape(vnum, 3)
138137
faces = np.fromfile(fobj, ">i4", fnum * 3).reshape(fnum, 3)
139-
140-
extra = fobj.read() if read_metadata else b''
138+
extra = fobj.read() if read_metadata else None
141139
volume_info = _read_volume_info(extra)
142140
else:
143141
raise ValueError("File does not appear to be a Freesurfer surface")

nibabel/freesurfer/tests/test_io.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ def test_geometry():
5656
assert_equal(coords.shape[0], faces.max() + 1)
5757

5858
# Test quad with sphere
59-
with clear_and_catch_warnings():
59+
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "sphere"))
60+
with clear_and_catch_warnings() as w:
6061
warnings.filterwarnings('always', category=DeprecationWarning)
61-
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "sphere"))
6262
coords, faces, volume_info, create_stamp = \
6363
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)
64+
assert_true(any('extension code' in str(ww.message) for ww in w))
65+
assert_equal(0, faces.min())
66+
assert_equal(coords.shape[0], faces.max() + 1)
67+
assert_equal(0, len(volume_info))
68+
assert_equal(u'created by greve on Thu Jun 8 19:17:51 2006',
69+
create_stamp)
6970

7071
# Test equivalence of freesurfer- and nibabel-generated triangular files
7172
# with respect to read_geometry()
@@ -84,6 +85,14 @@ def test_geometry():
8485
np.fromfile(fobj, ">u1", 3)
8586
read_create_stamp = fobj.readline().decode().rstrip('\n')
8687

88+
# now write an incomplete file
89+
write_geometry(surf_path, coords, faces)
90+
with clear_and_catch_warnings() as w:
91+
warnings.filterwarnings('always', category=DeprecationWarning)
92+
read_geometry(surf_path, read_metadata=True)
93+
assert_true(any('volume information contained' in str(ww.message)
94+
for ww in w))
95+
8796
assert_equal(create_stamp, read_create_stamp)
8897

8998
np.testing.assert_array_equal(coords, coords2)

0 commit comments

Comments
 (0)