@@ -47,18 +47,26 @@ def _fread3_many(fobj, n):
4747
4848def _read_volume_info (fobj ):
4949 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
6270 return volume_info
6371
6472
@@ -122,14 +130,17 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
122130 coords = np .fromfile (fobj , ">f4" , vnum * 3 ).reshape (vnum , 3 )
123131 faces = np .fromfile (fobj , ">i4" , fnum * 3 ).reshape (fnum , 3 )
124132
125- volume_info = _read_volume_info (fobj )
133+ if read_metadata :
134+ volume_info = _read_volume_info (fobj )
126135 else :
127136 raise ValueError ("File does not appear to be a Freesurfer surface" )
128137
129138 coords = coords .astype (np .float ) # XXX: due to mayavi bug on mac 32bits
130139
131140 ret = (coords , faces )
132141 if read_metadata :
142+ if len (volume_info ) == 0 :
143+ warnings .warn ('No volume information contained in the file' )
133144 ret += (volume_info ,)
134145 if read_stamp :
135146 ret += (create_stamp ,)
@@ -174,9 +185,15 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
174185 if volume_info is not None and len (volume_info ) > 0 :
175186 for key , val in volume_info .items ():
176187 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' ))
180197
181198
182199def read_morph_data (filepath ):
0 commit comments