@@ -45,6 +45,38 @@ def _fread3_many(fobj, n):
45
45
return (b1 << 16 ) + (b2 << 8 ) + b3
46
46
47
47
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
+
48
80
def read_geometry (filepath , read_metadata = False , read_stamp = False ):
49
81
"""Read a triangular format Freesurfer surface mesh.
50
82
@@ -63,7 +95,7 @@ def read_geometry(filepath, read_metadata=False, read_stamp=False):
63
95
nvtx x 3 array of vertex (x, y, z) coordinates
64
96
faces : numpy array
65
97
nfaces x 3 array of defining mesh triangles
66
- volume_info : dict-like
98
+ volume_info : OrderedDict
67
99
If read_metadata is true, key-value pairs found in the geometry file
68
100
create_stamp : str
69
101
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):
106
138
faces = np .fromfile (fobj , ">i4" , fnum * 3 ).reshape (fnum , 3 )
107
139
108
140
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 )
136
142
else :
137
143
raise ValueError ("File does not appear to be a Freesurfer surface" )
138
144
@@ -175,11 +181,11 @@ def write_geometry(filepath, coords, faces, create_stamp=None,
175
181
postlude = [b'\x00 \x00 \x00 \x14 ' ]
176
182
for key , val in volume_info .items ():
177
183
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 )
179
185
elif key == 'volume' :
180
- val = '{:d} {:d} {:d}' .format (* val )
186
+ val = '{0 :d} {1 :d} {2 :d}' .format (* val )
181
187
key = key .ljust (6 )
182
- postlude .append ('{} = {}' .format (key , val ).encode ('utf-8' ))
188
+ postlude .append ('{0 } = {1 }' .format (key , val ).encode ('utf-8' ))
183
189
postlude = b'\n ' .join (postlude )
184
190
185
191
with open (filepath , 'wb' ) as fobj :
0 commit comments