3
3
from nibabel .volumeutils import Recoder
4
4
from nibabel .affines import voxel_sizes
5
5
6
- from .base import StringBasedStruct
6
+ from .base import StringBasedStruct , TransformFileError
7
7
8
8
9
9
transform_codes = Recoder ((
@@ -20,10 +20,10 @@ class VolumeGeometry(StringBasedStruct):
20
20
('valid' , 'i4' ), # Valid values: 0, 1
21
21
('volume' , 'i4' , (3 , 1 )), # width, height, depth
22
22
('voxelsize' , 'f4' , (3 , 1 )), # xsize, ysize, zsize
23
- ('xras' , 'f4 ' , (3 , 1 )), # x_r, x_a, x_s
24
- ('yras' , 'f4 ' , (3 , 1 )), # y_r, y_a, y_s
25
- ('zras' , 'f4 ' , (3 , 1 )), # z_r, z_a, z_s
26
- ('cras' , 'f4 ' , (3 , 1 )), # c_r, c_a, c_s
23
+ ('xras' , 'f8 ' , (3 , 1 )), # x_r, x_a, x_s
24
+ ('yras' , 'f8 ' , (3 , 1 )), # y_r, y_a, y_s
25
+ ('zras' , 'f8 ' , (3 , 1 )), # z_r, z_a, z_s
26
+ ('cras' , 'f8 ' , (3 , 1 )), # c_r, c_a, c_s
27
27
('filename' , 'U1024' )]) # Not conformant (may be >1024 bytes)
28
28
dtype = template_dtype
29
29
@@ -80,7 +80,7 @@ def from_string(klass, string):
80
80
lines = string .splitlines ()
81
81
for key in ('valid' , 'filename' , 'volume' , 'voxelsize' ,
82
82
'xras' , 'yras' , 'zras' , 'cras' ):
83
- label , valstring = lines .pop (0 ).split (' = ' )
83
+ label , valstring = lines .pop (0 ).split (' =' )
84
84
assert label .strip () == key
85
85
86
86
val = np .genfromtxt ([valstring .encode ()],
@@ -94,9 +94,9 @@ class LinearTransform(StringBasedStruct):
94
94
template_dtype = np .dtype ([
95
95
('mean' , 'f4' , (3 , 1 )), # x0, y0, z0
96
96
('sigma' , 'f4' ),
97
- ('m_L' , 'f4 ' , (4 , 4 )),
98
- ('m_dL' , 'f4 ' , (4 , 4 )),
99
- ('m_last_dL' , 'f4 ' , (4 , 4 )),
97
+ ('m_L' , 'f8 ' , (4 , 4 )),
98
+ ('m_dL' , 'f8 ' , (4 , 4 )),
99
+ ('m_last_dL' , 'f8 ' , (4 , 4 )),
100
100
('src' , VolumeGeometry ),
101
101
('dst' , VolumeGeometry ),
102
102
('label' , 'i4' )])
@@ -191,13 +191,10 @@ def from_string(klass, string):
191
191
lta = klass ()
192
192
sa = lta .structarr
193
193
lines = string .splitlines ()
194
- while lines :
195
- if not lines [0 ].startswith ('type' ):
196
- lines .pop (0 )
197
- continue
198
- break
199
- if not lines :
200
- raise Exception ("Invalid LTA format" )
194
+ lines = [l .strip () for l in string .splitlines ()
195
+ if l .strip () and not l .strip ().startswith ('#' )]
196
+ if not lines or not lines [0 ].startswith ('type' ):
197
+ raise TransformFileError ("Invalid LTA format" )
201
198
for key in ('type' , 'nxforms' ):
202
199
label , valstring = lines .pop (0 ).split (' = ' )
203
200
assert label .strip () == key
@@ -214,12 +211,16 @@ def from_string(klass, string):
214
211
# Optional keys
215
212
if not lines [0 ].startswith (key ):
216
213
continue
217
- label , valstring = lines .pop (0 ).split (' ' )
218
- assert label .strip () == key
214
+ try :
215
+ label , valstring = lines .pop (0 ).split (' ' )
216
+ except ValueError :
217
+ sa [key ] = ''
218
+ else :
219
+ assert label .strip () == key
219
220
220
- val = np .genfromtxt ([valstring .encode ()],
221
- dtype = klass .dtype [key ])
222
- sa [key ] = val .reshape (sa [key ].shape ) if val .size else ''
221
+ val = np .genfromtxt ([valstring .encode ()],
222
+ dtype = klass .dtype [key ])
223
+ sa [key ] = val .reshape (sa [key ].shape ) if val .size else ''
223
224
224
225
assert len (lta ._xforms ) == sa ['nxforms' ]
225
226
return lta
0 commit comments