1
1
"""Read/write ITK transforms."""
2
+
2
3
import warnings
3
4
import numpy as np
4
5
from scipy .io import loadmat as _read_mat , savemat as _save_mat
@@ -138,8 +139,7 @@ def from_matlab_dict(cls, mdict, index=0):
138
139
sa = tf .structarr
139
140
140
141
affine = mdict .get (
141
- "AffineTransform_double_3_3" ,
142
- mdict .get ("AffineTransform_float_3_3" )
142
+ "AffineTransform_double_3_3" , mdict .get ("AffineTransform_float_3_3" )
143
143
)
144
144
145
145
if affine is None :
@@ -337,7 +337,7 @@ def from_image(cls, imgobj):
337
337
hdr = imgobj .header .copy ()
338
338
shape = hdr .get_data_shape ()
339
339
340
- if len (shape ) != 5 or shape [- 2 ] != 1 or not shape [- 1 ] in (2 , 3 ):
340
+ if len (shape ) != 5 or shape [- 2 ] != 1 or shape [- 1 ] not in (2 , 3 ):
341
341
raise TransformFileError (
342
342
'Displacements field "%s" does not come from ITK.'
343
343
% imgobj .file_map ["image" ].filename
@@ -348,9 +348,7 @@ def from_image(cls, imgobj):
348
348
hdr .set_intent ("vector" )
349
349
350
350
field = np .squeeze (np .asanyarray (imgobj .dataobj ))
351
- field [..., (0 , 1 )] *= - 1.0
352
-
353
- return imgobj .__class__ (field , imgobj .affine , hdr )
351
+ return imgobj .__class__ (field , LPS @ imgobj .affine , hdr )
354
352
355
353
@classmethod
356
354
def to_image (cls , imgobj ):
@@ -360,9 +358,7 @@ def to_image(cls, imgobj):
360
358
hdr .set_intent ("vector" )
361
359
362
360
warp_data = imgobj .get_fdata ().reshape (imgobj .shape [:3 ] + (1 , imgobj .shape [- 1 ]))
363
- warp_data [..., (0 , 1 )] *= - 1
364
-
365
- return imgobj .__class__ (warp_data , imgobj .affine , hdr )
361
+ return imgobj .__class__ (warp_data , LPS @ imgobj .affine , hdr )
366
362
367
363
368
364
class ITKCompositeH5 :
@@ -410,21 +406,16 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False):
410
406
directions = np .reshape (_fixed [9 :], (3 , 3 ))
411
407
affine = from_matvec (directions * zooms , offset )
412
408
# ITK uses Fortran ordering, like NIfTI, but with the vector dimension first
413
- field = np .moveaxis (
414
- np .reshape (
415
- xfm [f"{ typo_fallback } Parameters" ], (3 , * shape .astype (int )), order = 'F'
416
- ),
417
- 0 ,
418
- - 1 ,
419
- )
420
- field [..., (0 , 1 )] *= - 1.0
409
+ # In practice, this seems to work (see issue #171)
410
+ field = np .reshape (
411
+ xfm [f"{ typo_fallback } Parameters" ], (* shape .astype (int ), 3 )
412
+ ).transpose (2 , 1 , 0 , 3 )
413
+
421
414
hdr = Nifti1Header ()
422
415
hdr .set_intent ("vector" )
423
416
hdr .set_data_dtype ("float" )
424
417
425
- xfm_list .append (
426
- Nifti1Image (field .astype ("float" ), LPS @ affine , hdr )
427
- )
418
+ xfm_list .append (Nifti1Image (field .astype ("float" ), affine , hdr ))
428
419
continue
429
420
430
421
raise TransformIOError (
0 commit comments