@@ -78,17 +78,17 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
7878 filepath : str
7979 Path to surface file
8080 read_metadata : bool
81- Read metadata as key-value pairs
81+ Read metadata as key-value pairs.
8282 Valid keys:
83- 'head' : array of int
84- 'valid' : str
85- 'filename' : str
86- 'volume' : array of int, shape (3,)
87- 'voxelsize' : array of float, shape (3,)
88- 'xras' : array of float, shape (3,)
89- 'yras' : array of float, shape (3,)
90- 'zras' : array of float, shape (3,)
91- 'cras' : array of float, shape (3,)
83+ * 'head' : array of int
84+ * 'valid' : str
85+ * 'filename' : str
86+ * 'volume' : array of int, shape (3,)
87+ * 'voxelsize' : array of float, shape (3,)
88+ * 'xras' : array of float, shape (3,)
89+ * 'yras' : array of float, shape (3,)
90+ * 'zras' : array of float, shape (3,)
91+ * 'cras' : array of float, shape (3,)
9292 read_stamp : bool
9393 Return the comment from the file
9494
@@ -173,17 +173,17 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
173173 create_stamp : str
174174 User/time stamp (default: "created by <user> on <ctime>")
175175 volume_info : dict-like or None
176- Key-value pairs to encode at the end of the file
176+ Key-value pairs to encode at the end of the file.
177177 Valid keys:
178- 'head' : array of int
179- 'valid' : str
180- 'filename' : str
181- 'volume' : array of int, shape (3,)
182- 'voxelsize' : array of float, shape (3,)
183- 'xras' : array of float, shape (3,)
184- 'yras' : array of float, shape (3,)
185- 'zras' : array of float, shape (3,)
186- 'cras' : array of float, shape (3,)
178+ * 'head' : array of int
179+ * 'valid' : str
180+ * 'filename' : str
181+ * 'volume' : array of int, shape (3,)
182+ * 'voxelsize' : array of float, shape (3,)
183+ * 'xras' : array of float, shape (3,)
184+ * 'yras' : array of float, shape (3,)
185+ * 'zras' : array of float, shape (3,)
186+ * 'cras' : array of float, shape (3,)
187187
188188 """
189189 magic_bytes = np .array ([255 , 255 , 254 ], dtype = np .uint8 )
@@ -203,23 +203,8 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
203203 faces .astype ('>i4' ).reshape (- 1 ).tofile (fobj )
204204
205205 # Add volume info, if given
206- if volume_info is None or len (volume_info ) == 0 :
207- return
208-
209- for key , val in volume_info .items ():
210- if key == 'head' :
211- if not (np .array_equal (val , [20 ]) or np .array_equal (
212- val , [2 , 0 , 20 ])):
213- warnings .warn ("Unknown extension code." )
214- np .array (val , dtype = '>i4' ).tofile (fobj )
215- elif key in ('valid' , 'filename' ):
216- fobj .write ('{0} = {1}\n ' .format (key , val ).encode ('utf-8' ))
217- elif key == 'volume' :
218- fobj .write ('{0} = {1} {2} {3}\n ' .format (
219- key , val [0 ], val [1 ], val [2 ]).encode ('utf-8' ))
220- else :
221- fobj .write ('{0} = {1:.4f} {2:.4f} {3:.4f}\n ' .format (
222- key .ljust (6 ), val [0 ], val [1 ], val [2 ]).encode ('utf-8' ))
206+ if volume_info is not None and len (volume_info ) > 0 :
207+ fobj .write (_serialize_volume_info (volume_info ))
223208
224209
225210def read_morph_data (filepath ):
@@ -461,3 +446,32 @@ def read_label(filepath, read_scalars=False):
461446 scalar_array = np .loadtxt (filepath , skiprows = 2 , usecols = [- 1 ])
462447 return label_array , scalar_array
463448 return label_array
449+
450+
451+ def _serialize_volume_info (volume_info ):
452+ """Helper for serializing the volume info."""
453+ keys = ['head' , 'valid' , 'filename' , 'volume' , 'voxelsize' , 'xras' , 'yras' ,
454+ 'zras' , 'cras' ]
455+ diff = set (volume_info .keys ()).difference (keys )
456+ if len (diff ) > 0 :
457+ raise ValueError ('Invalid volume info: %s.' % diff .pop ())
458+
459+ strings = list ()
460+ for key in keys :
461+ if key == 'head' :
462+ if not (np .array_equal (volume_info [key ], [20 ]) or np .array_equal (
463+ volume_info [key ], [2 , 0 , 20 ])):
464+ warnings .warn ("Unknown extension code." )
465+ strings .append (np .array (volume_info [key ], dtype = '>i4' ).tostring ())
466+ elif key in ('valid' , 'filename' ):
467+ val = volume_info [key ]
468+ strings .append ('{0} = {1}\n ' .format (key , val ).encode ('utf-8' ))
469+ elif key == 'volume' :
470+ val = volume_info [key ]
471+ strings .append ('{0} = {1} {2} {3}\n ' .format (
472+ key , val [0 ], val [1 ], val [2 ]).encode ('utf-8' ))
473+ else :
474+ val = volume_info [key ]
475+ strings .append ('{0} = {1:f} {2:f} {3:f}\n ' .format (
476+ key .ljust (6 ), val [0 ], val [1 ], val [2 ]).encode ('utf-8' ))
477+ return '' .join (strings )
0 commit comments