7
7
#
8
8
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9
9
"""Common interface for transforms."""
10
+
10
11
from pathlib import Path
11
12
import numpy as np
12
13
import h5py
@@ -146,13 +147,13 @@ def from_arrays(cls, coordinates, triangles):
146
147
darrays = [
147
148
nb .gifti .GiftiDataArray (
148
149
coordinates .astype (np .float32 ),
149
- intent = nb .nifti1 .intent_codes [' NIFTI_INTENT_POINTSET' ],
150
- datatype = nb .nifti1 .data_type_codes [' NIFTI_TYPE_FLOAT32' ],
150
+ intent = nb .nifti1 .intent_codes [" NIFTI_INTENT_POINTSET" ],
151
+ datatype = nb .nifti1 .data_type_codes [" NIFTI_TYPE_FLOAT32" ],
151
152
),
152
153
nb .gifti .GiftiDataArray (
153
154
triangles .astype (np .int32 ),
154
- intent = nb .nifti1 .intent_codes [' NIFTI_INTENT_TRIANGLE' ],
155
- datatype = nb .nifti1 .data_type_codes [' NIFTI_TYPE_INT32' ],
155
+ intent = nb .nifti1 .intent_codes [" NIFTI_INTENT_TRIANGLE" ],
156
+ datatype = nb .nifti1 .data_type_codes [" NIFTI_TYPE_INT32" ],
156
157
),
157
158
]
158
159
gii = nb .gifti .GiftiImage (darrays = darrays )
@@ -248,27 +249,40 @@ def __ne__(self, other):
248
249
class TransformBase :
249
250
"""Abstract image class to represent transforms."""
250
251
251
- __slots__ = ("_reference" , "_ndim" , "_affine" , "_shape" , "_header" ,
252
- "_grid" , "_mapping" , "_hdf5_dct" , "_x5_dct" )
252
+ __slots__ = (
253
+ "_reference" ,
254
+ "_ndim" ,
255
+ "_affine" ,
256
+ "_shape" ,
257
+ "_header" ,
258
+ "_grid" ,
259
+ "_mapping" ,
260
+ "_hdf5_dct" ,
261
+ "_x5_dct" ,
262
+ )
253
263
254
264
x5_struct = {
255
- 'TransformGroup/0' : {
256
- 'Type' : None ,
257
- 'Transform' : None ,
258
- 'Metadata' : None ,
259
- 'Inverse' : None
260
- },
261
- 'TransformGroup/0/Domain' : {
262
- 'Grid' : None ,
263
- 'Size' : None ,
264
- 'Mapping' : None
265
+ "TransformGroup/0" : {
266
+ "Type" : None ,
267
+ "Transform" : None ,
268
+ "Metadata" : None ,
269
+ "Inverse" : None ,
265
270
},
266
- 'TransformGroup/1' : {},
267
- 'TransformChain' : {}
271
+ "TransformGroup/0/Domain" : {"Grid" : None , "Size" : None , "Mapping" : None },
272
+ "TransformGroup/1" : {},
273
+ "TransformChain" : {},
268
274
}
269
275
270
- def __init__ (self , x5 = None , hdf5 = None , nifti = None , shape = None , affine = None ,
271
- header = None , reference = None ):
276
+ def __init__ (
277
+ self ,
278
+ x5 = None ,
279
+ hdf5 = None ,
280
+ nifti = None ,
281
+ shape = None ,
282
+ affine = None ,
283
+ header = None ,
284
+ reference = None ,
285
+ ):
272
286
"""Instantiate a transform."""
273
287
274
288
self ._reference = None
@@ -281,7 +295,7 @@ def __init__(self, x5=None, hdf5=None, nifti=None, shape=None, affine=None,
281
295
self .update_x5_structure (hdf5 )
282
296
elif x5 :
283
297
self .update_x5_structure (x5 )
284
-
298
+
285
299
self ._shape = shape
286
300
self ._affine = affine
287
301
self ._header = header
@@ -327,8 +341,8 @@ def ndim(self):
327
341
raise TypeError ("TransformBase has no dimensions" )
328
342
329
343
def init_x5_structure (self , xfm_data = None ):
330
- self .x5_struct [' TransformGroup/0/Transform' ] = xfm_data
331
-
344
+ self .x5_struct [" TransformGroup/0/Transform" ] = xfm_data
345
+
332
346
def update_x5_structure (self , hdf5_struct = None ):
333
347
self .x5_struct .update (hdf5_struct )
334
348
@@ -358,9 +372,7 @@ def apply(self, *args, **kwargs):
358
372
359
373
Deprecated. Please use ``nitransforms.resampling.apply`` instead.
360
374
"""
361
- message = (
362
- "The `apply` method is deprecated. Please use `nitransforms.resampling.apply` instead."
363
- )
375
+ message = "The `apply` method is deprecated. Please use `nitransforms.resampling.apply` instead."
364
376
warnings .warn (message , DeprecationWarning , stacklevel = 2 )
365
377
from .resampling import apply
366
378
@@ -372,7 +384,7 @@ def _to_hdf5(self, x5_root):
372
384
373
385
"""Group '0' containing Affine transform"""
374
386
transform_0 = transform_group .create_group ("0" )
375
-
387
+
376
388
transform_0 .attrs ["Type" ] = "Affine"
377
389
transform_0 .create_dataset ("Transform" , data = self ._matrix )
378
390
transform_0 .create_dataset ("Inverse" , data = np .linalg .inv (self ._matrix ))
@@ -385,34 +397,36 @@ def _to_hdf5(self, x5_root):
385
397
domain_group .attrs ["Grid" ] = self .grid
386
398
domain_group .create_dataset ("Size" , data = _as_homogeneous (self ._reference .shape ))
387
399
domain_group .create_dataset ("Mapping" , data = self .map )
388
-
400
+
389
401
raise NotImplementedError
390
-
402
+
391
403
def read_x5 (self , x5_root ):
392
404
variables = {}
393
405
with h5py .File (x5_root , "r" ) as f :
394
- f .visititems (lambda filename , x5_root : self ._from_hdf5 (filename , x5_root , variables ))
406
+ f .visititems (
407
+ lambda filename , x5_root : self ._from_hdf5 (filename , x5_root , variables )
408
+ )
395
409
396
410
_transform = variables ["TransformGroup/0/Transform" ]
397
411
_inverse = variables ["TransformGroup/0/Inverse" ]
398
412
_size = variables ["TransformGroup/0/Domain/Size" ]
399
413
_map = variables ["TransformGroup/0/Domain/Mapping" ]
400
414
401
415
return _transform , _inverse , _size , _map
402
-
416
+
403
417
def _from_hdf5 (self , name , x5_root , storage ):
404
418
if isinstance (x5_root , h5py .Dataset ):
405
419
storage [name ] = {
406
- ' type' : ' dataset' ,
407
- ' attrs' : dict (x5_root .attrs ),
408
- ' shape' : x5_root .shape ,
409
- ' data' : x5_root [()] # Read the data
410
- }
420
+ " type" : " dataset" ,
421
+ " attrs" : dict (x5_root .attrs ),
422
+ " shape" : x5_root .shape ,
423
+ " data" : x5_root [()], # Read the data
424
+ }
411
425
elif isinstance (x5_root , h5py .Group ):
412
426
storage [name ] = {
413
- ' type' : ' group' ,
414
- ' attrs' : dict (x5_root .attrs ),
415
- ' members' : {}
427
+ " type" : " group" ,
428
+ " attrs" : dict (x5_root .attrs ),
429
+ " members" : {},
416
430
}
417
431
418
432
0 commit comments