Skip to content

Commit c41faad

Browse files
committed
address comments
1 parent 995dc75 commit c41faad

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

nibabel/freesurfer/io.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,38 @@ def _fread3_many(fobj, n):
4545
return (b1 << 16) + (b2 << 8) + b3
4646

4747

48+
def _read_volume_info(extra):
49+
volume_info = OrderedDict()
50+
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.")
56+
else:
57+
try:
58+
for line in extra[4:].split(b'\n'):
59+
if len(line) == 0:
60+
continue
61+
key, val = map(bytes.strip, line.split(b'=', 1))
62+
key = key.decode('utf-8')
63+
if key in ('voxelsize', 'xras', 'yras', 'zras', 'cras'):
64+
val = np.fromstring(val, sep=' ')
65+
val = val.astype(np.float)
66+
elif key == 'volume':
67+
val = np.fromstring(val, sep=' ', dtype=np.uint)
68+
val = val.astype(np.int)
69+
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+
77+
return volume_info
78+
79+
4880
def read_geometry(filepath, read_metadata=False, read_stamp=False):
4981
"""Read a triangular format Freesurfer surface mesh.
5082
@@ -63,7 +95,7 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
6395
nvtx x 3 array of vertex (x, y, z) coordinates
6496
faces : numpy array
6597
nfaces x 3 array of defining mesh triangles
66-
volume_info : dict-like
98+
volume_info : OrderedDict
6799
If read_metadata is true, key-value pairs found in the geometry file
68100
create_stamp : str
69101
If read_stamp is true, the comment added by the program that saved
@@ -106,33 +138,7 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
106138
faces = np.fromfile(fobj, ">i4", fnum * 3).reshape(fnum, 3)
107139

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

@@ -175,11 +181,11 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
175181
postlude = [b'\x00\x00\x00\x14']
176182
for key, val in volume_info.items():
177183
if key in ('voxelsize', 'xras', 'yras', 'zras', 'cras'):
178-
val = '{:.4f} {:.4f} {:.4f}'.format(*val)
184+
val = '{0:.4f} {1:.4f} {2:.4f}'.format(*val)
179185
elif key == 'volume':
180-
val = '{:d} {:d} {:d}'.format(*val)
186+
val = '{0:d} {1:d} {2:d}'.format(*val)
181187
key = key.ljust(6)
182-
postlude.append('{} = {}'.format(key, val).encode('utf-8'))
188+
postlude.append('{0} = {1}'.format(key, val).encode('utf-8'))
183189
postlude = b'\n'.join(postlude)
184190

185191
with open(filepath, 'wb') as fobj:

0 commit comments

Comments
 (0)