@@ -47,18 +47,26 @@ def _fread3_many(fobj, n):
47
47
48
48
def _read_volume_info (fobj ):
49
49
volume_info = OrderedDict ()
50
- head = np .fromfile (fobj , '>i4' , 3 )
51
- if any (head != [2 , 0 , 20 ]):
52
- warnings .warn ("Unknown extension code." )
53
- else :
54
- volume_info ['head' ] = head
55
- for key in ['valid' , 'filename' , 'volume' , 'voxelsize' , 'xras' , 'yras' ,
56
- 'zras' , 'cras' ]:
57
- pair = fobj .readline ().decode ('utf-8' ).split ('=' )
58
- if pair [0 ].strip () != key or len (pair ) != 2 :
59
- raise IOError ('Error parsing volume info.' )
60
- volume_info [pair [0 ]] = pair [1 ]
61
- # Ignore the rest
50
+ head = np .fromfile (fobj , '>i4' , 1 )
51
+ if not np .array_equal (head , [20 ]): # Read two bytes more
52
+ head = np .concatenate ([head , np .fromfile (fobj , '>i4' , 2 )])
53
+ if not np .array_equal (head , [2 , 0 , 20 ]):
54
+ warnings .warn ("Unknown extension code." )
55
+ return volume_info
56
+
57
+ volume_info ['head' ] = head
58
+ for key in ['valid' , 'filename' , 'volume' , 'voxelsize' , 'xras' , 'yras' ,
59
+ 'zras' , 'cras' ]:
60
+ pair = fobj .readline ().decode ('utf-8' ).split ('=' )
61
+ if pair [0 ].strip () != key or len (pair ) != 2 :
62
+ raise IOError ('Error parsing volume info.' )
63
+ if key in ('valid' , 'filename' ):
64
+ volume_info [key ] = pair [1 ].strip ()
65
+ elif key == 'volume' :
66
+ volume_info [key ] = np .array (pair [1 ].split ()).astype (int )
67
+ else :
68
+ volume_info [key ] = np .array (pair [1 ].split ()).astype (float )
69
+ # Ignore the rest
62
70
return volume_info
63
71
64
72
@@ -122,14 +130,17 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
122
130
coords = np .fromfile (fobj , ">f4" , vnum * 3 ).reshape (vnum , 3 )
123
131
faces = np .fromfile (fobj , ">i4" , fnum * 3 ).reshape (fnum , 3 )
124
132
125
- volume_info = _read_volume_info (fobj )
133
+ if read_metadata :
134
+ volume_info = _read_volume_info (fobj )
126
135
else :
127
136
raise ValueError ("File does not appear to be a Freesurfer surface" )
128
137
129
138
coords = coords .astype (np .float ) # XXX: due to mayavi bug on mac 32bits
130
139
131
140
ret = (coords , faces )
132
141
if read_metadata :
142
+ if len (volume_info ) == 0 :
143
+ warnings .warn ('No volume information contained in the file' )
133
144
ret += (volume_info ,)
134
145
if read_stamp :
135
146
ret += (create_stamp ,)
@@ -174,9 +185,15 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
174
185
if volume_info is not None and len (volume_info ) > 0 :
175
186
for key , val in volume_info .items ():
176
187
if key == 'head' :
177
- val .tofile (fobj )
178
- continue
179
- fobj .write ('{0}={1}' .format (key , val ).encode ('utf-8' ))
188
+ np .array (val , dtype = '>i4' ).tofile (fobj )
189
+ elif key in ('valid' , 'filename' ):
190
+ fobj .write ('{0} = {1}\n ' .format (key , val ).encode ('utf-8' ))
191
+ elif key == 'volume' :
192
+ fobj .write ('{0} = {1} {2} {3}\n ' .format (
193
+ key , val [0 ], val [1 ], val [2 ]).encode ('utf-8' ))
194
+ else :
195
+ fobj .write ('{0} = {1:.4f} {2:.4f} {3:.4f}\n ' .format (
196
+ key .ljust (6 ), val [0 ], val [1 ], val [2 ]).encode ('utf-8' ))
180
197
181
198
182
199
def read_morph_data (filepath ):
0 commit comments