11"""Read/write linear transforms."""
2+
23import numpy as np
34from nibabel .volumeutils import Recoder
45from nibabel .affines import voxel_sizes , from_matvec
@@ -29,12 +30,12 @@ class VolumeGeometry(StringBasedStruct):
2930 template_dtype = np .dtype (
3031 [
3132 ("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
3435 ("xras" , "f8" , (3 , 1 )), # x_r, x_a, x_s
3536 ("yras" , "f8" , (3 , 1 )), # y_r, y_a, y_s
3637 ("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
3839 ("filename" , "U1024" ),
3940 ]
4041 ) # Not conformant (may be >1024 bytes)
@@ -109,14 +110,19 @@ def from_string(cls, string):
109110 label , valstring = lines .pop (0 ).split (" =" )
110111 assert label .strip () == key
111112
112- val = ""
113- if valstring .strip ():
114- parsed = np .genfromtxt (
113+ parsed = (
114+ np .genfromtxt (
115115 [valstring .encode ()], autostrip = True , dtype = cls .dtype [key ]
116116 )
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+
120126 return volgeom
121127
122128
@@ -218,11 +224,15 @@ def to_ras(self, moving=None, reference=None):
218224 def to_string (self , partial = False ):
219225 """Convert this transform to text."""
220226 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+ )
226236
227237 # Standard preamble
228238 lines += [
@@ -232,10 +242,7 @@ def to_string(self, partial=False):
232242 ]
233243
234244 # 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 )]
239246
240247 lines += [
241248 "src volume info" ,
@@ -324,10 +331,7 @@ def __getitem__(self, idx):
324331 def to_ras (self , moving = None , reference = None ):
325332 """Set type to RAS2RAS and return the new matrix."""
326333 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 ]
331335
332336 def to_string (self ):
333337 """Convert this LTA into text format."""
@@ -396,9 +400,11 @@ def from_ras(cls, ras, moving=None, reference=None):
396400 sa ["type" ] = 1
397401 sa ["nxforms" ] = ras .shape [0 ]
398402 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+ )
402408
403409 sa ["subject" ] = "unset"
404410 sa ["fscale" ] = 0.0
@@ -407,8 +413,10 @@ def from_ras(cls, ras, moving=None, reference=None):
407413
408414def _drop_comments (string ):
409415 """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