1
1
"""Read/write linear transforms."""
2
+
2
3
import numpy as np
3
4
from nibabel .volumeutils import Recoder
4
5
from nibabel .affines import voxel_sizes , from_matvec
@@ -29,12 +30,12 @@ class VolumeGeometry(StringBasedStruct):
29
30
template_dtype = np .dtype (
30
31
[
31
32
("valid" , "i4" ), # Valid values: 0, 1
32
- ("volume" , "i4" , (3 , )), # width, height, depth
33
- ("voxelsize" , "f4" , (3 , )), # xsize, ysize, zsize
33
+ ("volume" , "i4" , (3 ,)), # width, height, depth
34
+ ("voxelsize" , "f4" , (3 ,)), # xsize, ysize, zsize
34
35
("xras" , "f8" , (3 , 1 )), # x_r, x_a, x_s
35
36
("yras" , "f8" , (3 , 1 )), # y_r, y_a, y_s
36
37
("zras" , "f8" , (3 , 1 )), # z_r, z_a, z_s
37
- ("cras" , "f8" , (3 , )), # c_r, c_a, c_s
38
+ ("cras" , "f8" , (3 ,)), # c_r, c_a, c_s
38
39
("filename" , "U1024" ),
39
40
]
40
41
) # Not conformant (may be >1024 bytes)
@@ -109,14 +110,19 @@ def from_string(cls, string):
109
110
label , valstring = lines .pop (0 ).split (" =" )
110
111
assert label .strip () == key
111
112
112
- val = ""
113
- if valstring .strip ():
114
- parsed = np .genfromtxt (
113
+ parsed = (
114
+ np .genfromtxt (
115
115
[valstring .encode ()], autostrip = True , dtype = cls .dtype [key ]
116
116
)
117
- if parsed .size :
118
- val = parsed .reshape (sa [key ].shape )
119
- sa [key ] = val
117
+ if valstring .strip ()
118
+ else None
119
+ )
120
+
121
+ if parsed is not None and parsed .size :
122
+ sa [key ] = parsed .reshape (sa [key ].shape )
123
+ else : # pragma: no coverage
124
+ """Do not set sa[key]"""
125
+
120
126
return volgeom
121
127
122
128
@@ -218,11 +224,15 @@ def to_ras(self, moving=None, reference=None):
218
224
def to_string (self , partial = False ):
219
225
"""Convert this transform to text."""
220
226
sa = self .structarr
221
- lines = [
222
- "# LTA file created by NiTransforms" ,
223
- "type = {}" .format (sa ["type" ]),
224
- "nxforms = 1" ,
225
- ] if not partial else []
227
+ lines = (
228
+ [
229
+ "# LTA file created by NiTransforms" ,
230
+ "type = {}" .format (sa ["type" ]),
231
+ "nxforms = 1" ,
232
+ ]
233
+ if not partial
234
+ else []
235
+ )
226
236
227
237
# Standard preamble
228
238
lines += [
@@ -232,10 +242,7 @@ def to_string(self, partial=False):
232
242
]
233
243
234
244
# Format parameters matrix
235
- lines += [
236
- " " .join (f"{ v :18.15e} " for v in sa ["m_L" ][i ])
237
- for i in range (4 )
238
- ]
245
+ lines += [" " .join (f"{ v :18.15e} " for v in sa ["m_L" ][i ]) for i in range (4 )]
239
246
240
247
lines += [
241
248
"src volume info" ,
@@ -324,10 +331,7 @@ def __getitem__(self, idx):
324
331
def to_ras (self , moving = None , reference = None ):
325
332
"""Set type to RAS2RAS and return the new matrix."""
326
333
self .structarr ["type" ] = 1
327
- return [
328
- xfm .to_ras (moving = moving , reference = reference )
329
- for xfm in self .xforms
330
- ]
334
+ return [xfm .to_ras (moving = moving , reference = reference ) for xfm in self .xforms ]
331
335
332
336
def to_string (self ):
333
337
"""Convert this LTA into text format."""
@@ -396,9 +400,11 @@ def from_ras(cls, ras, moving=None, reference=None):
396
400
sa ["type" ] = 1
397
401
sa ["nxforms" ] = ras .shape [0 ]
398
402
for i in range (sa ["nxforms" ]):
399
- lt ._xforms .append (cls ._inner_type .from_ras (
400
- ras [i , ...], moving = moving , reference = reference
401
- ))
403
+ lt ._xforms .append (
404
+ cls ._inner_type .from_ras (
405
+ ras [i , ...], moving = moving , reference = reference
406
+ )
407
+ )
402
408
403
409
sa ["subject" ] = "unset"
404
410
sa ["fscale" ] = 0.0
@@ -407,8 +413,10 @@ def from_ras(cls, ras, moving=None, reference=None):
407
413
408
414
def _drop_comments (string ):
409
415
"""Drop comments."""
410
- return "\n " .join ([
411
- line .split ("#" )[0 ].strip ()
412
- for line in string .splitlines ()
413
- if line .split ("#" )[0 ].strip ()
414
- ])
416
+ return "\n " .join (
417
+ [
418
+ line .split ("#" )[0 ].strip ()
419
+ for line in string .splitlines ()
420
+ if line .split ("#" )[0 ].strip ()
421
+ ]
422
+ )
0 commit comments