@@ -72,8 +72,8 @@ def get_affine_trackvis_to_rasmm(header):
72
72
73
73
Parameters
74
74
----------
75
- header : dict
76
- Dict containing trackvis header.
75
+ header : dict or ndarray
76
+ Dict or numpy structured array containing trackvis header.
77
77
78
78
Returns
79
79
-------
@@ -101,9 +101,12 @@ def get_affine_trackvis_to_rasmm(header):
101
101
# If the voxel order implied by the affine does not match the voxel
102
102
# order in the TRK header, change the orientation.
103
103
# voxel (header) -> voxel (affine)
104
- header_ornt = asstr (header [Field .VOXEL_ORDER ])
104
+ vox_order = header [Field .VOXEL_ORDER ]
105
+ # Input header can be dict or structured array
106
+ if hasattr (vox_order , 'item' ): # structured array
107
+ vox_order = header [Field .VOXEL_ORDER ].item ()
105
108
affine_ornt = "" .join (aff2axcodes (header [Field .VOXEL_TO_RASMM ]))
106
- header_ornt = axcodes2ornt (header_ornt )
109
+ header_ornt = axcodes2ornt (vox_order . decode ( 'latin1' ) )
107
110
affine_ornt = axcodes2ornt (affine_ornt )
108
111
ornt = nib .orientations .ornt_transform (header_ornt , affine_ornt )
109
112
M = nib .orientations .inv_ornt_aff (ornt , header [Field .DIMENSIONS ])
@@ -265,25 +268,25 @@ def is_correct_format(cls, fileobj):
265
268
def _default_structarr (cls ):
266
269
""" Return an empty compliant TRK header as numpy structured array
267
270
"""
268
- header = np .zeros (1 , dtype = header_2_dtype )
271
+ st_arr = np .zeros (() , dtype = header_2_dtype )
269
272
270
273
# Default values
271
- header [Field .MAGIC_NUMBER ] = cls .MAGIC_NUMBER
272
- header [Field .VOXEL_SIZES ] = np .array ((1 , 1 , 1 ), dtype = "f4" )
273
- header [Field .DIMENSIONS ] = np .array ((1 , 1 , 1 ), dtype = "h" )
274
- header [Field .VOXEL_TO_RASMM ] = np .eye (4 , dtype = "f4" )
275
- header [Field .VOXEL_ORDER ] = b"RAS"
276
- header ['version' ] = 2
277
- header ['hdr_size' ] = cls .HEADER_SIZE
274
+ st_arr [Field .MAGIC_NUMBER ] = cls .MAGIC_NUMBER
275
+ st_arr [Field .VOXEL_SIZES ] = np .array ((1 , 1 , 1 ), dtype = "f4" )
276
+ st_arr [Field .DIMENSIONS ] = np .array ((1 , 1 , 1 ), dtype = "h" )
277
+ st_arr [Field .VOXEL_TO_RASMM ] = np .eye (4 , dtype = "f4" )
278
+ st_arr [Field .VOXEL_ORDER ] = b"RAS"
279
+ st_arr ['version' ] = 2
280
+ st_arr ['hdr_size' ] = cls .HEADER_SIZE
278
281
279
- return header
282
+ return st_arr
280
283
281
284
@classmethod
282
285
def create_empty_header (cls ):
283
286
""" Return an empty compliant TRK header as dict
284
287
"""
285
- header_rec = cls ._default_structarr ()
286
- return dict (zip (header_rec .dtype .names , header_rec ))
288
+ st_arr = cls ._default_structarr ()
289
+ return dict (zip (st_arr .dtype .names , st_arr . tolist () ))
287
290
288
291
@classmethod
289
292
def load (cls , fileobj , lazy_load = False ):
@@ -410,7 +413,6 @@ def save(self, fileobj):
410
413
nb_scalars = 0
411
414
nb_properties = 0
412
415
413
- header = header [0 ]
414
416
with Opener (fileobj , mode = "wb" ) as f :
415
417
# Keep track of the beginning of the header.
416
418
beginning = f .tell ()
0 commit comments