77#
88### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99"""Common interface for transforms."""
10+
1011from pathlib import Path
1112import numpy as np
1213import h5py
@@ -146,13 +147,13 @@ def from_arrays(cls, coordinates, triangles):
146147 darrays = [
147148 nb .gifti .GiftiDataArray (
148149 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" ],
151152 ),
152153 nb .gifti .GiftiDataArray (
153154 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" ],
156157 ),
157158 ]
158159 gii = nb .gifti .GiftiImage (darrays = darrays )
@@ -248,27 +249,40 @@ def __ne__(self, other):
248249class TransformBase :
249250 """Abstract image class to represent transforms."""
250251
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+ )
253263
254264 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 ,
265270 },
266- 'TransformGroup/1' : {},
267- 'TransformChain' : {}
271+ "TransformGroup/0/Domain" : {"Grid" : None , "Size" : None , "Mapping" : None },
272+ "TransformGroup/1" : {},
273+ "TransformChain" : {},
268274 }
269275
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+ ):
272286 """Instantiate a transform."""
273287
274288 self ._reference = None
@@ -281,7 +295,7 @@ def __init__(self, x5=None, hdf5=None, nifti=None, shape=None, affine=None,
281295 self .update_x5_structure (hdf5 )
282296 elif x5 :
283297 self .update_x5_structure (x5 )
284-
298+
285299 self ._shape = shape
286300 self ._affine = affine
287301 self ._header = header
@@ -327,8 +341,8 @@ def ndim(self):
327341 raise TypeError ("TransformBase has no dimensions" )
328342
329343 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+
332346 def update_x5_structure (self , hdf5_struct = None ):
333347 self .x5_struct .update (hdf5_struct )
334348
@@ -358,9 +372,7 @@ def apply(self, *args, **kwargs):
358372
359373 Deprecated. Please use ``nitransforms.resampling.apply`` instead.
360374 """
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."
364376 warnings .warn (message , DeprecationWarning , stacklevel = 2 )
365377 from .resampling import apply
366378
@@ -372,7 +384,7 @@ def _to_hdf5(self, x5_root):
372384
373385 """Group '0' containing Affine transform"""
374386 transform_0 = transform_group .create_group ("0" )
375-
387+
376388 transform_0 .attrs ["Type" ] = "Affine"
377389 transform_0 .create_dataset ("Transform" , data = self ._matrix )
378390 transform_0 .create_dataset ("Inverse" , data = np .linalg .inv (self ._matrix ))
@@ -385,34 +397,36 @@ def _to_hdf5(self, x5_root):
385397 domain_group .attrs ["Grid" ] = self .grid
386398 domain_group .create_dataset ("Size" , data = _as_homogeneous (self ._reference .shape ))
387399 domain_group .create_dataset ("Mapping" , data = self .map )
388-
400+
389401 raise NotImplementedError
390-
402+
391403 def read_x5 (self , x5_root ):
392404 variables = {}
393405 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+ )
395409
396410 _transform = variables ["TransformGroup/0/Transform" ]
397411 _inverse = variables ["TransformGroup/0/Inverse" ]
398412 _size = variables ["TransformGroup/0/Domain/Size" ]
399413 _map = variables ["TransformGroup/0/Domain/Mapping" ]
400414
401415 return _transform , _inverse , _size , _map
402-
416+
403417 def _from_hdf5 (self , name , x5_root , storage ):
404418 if isinstance (x5_root , h5py .Dataset ):
405419 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+ }
411425 elif isinstance (x5_root , h5py .Group ):
412426 storage [name ] = {
413- ' type' : ' group' ,
414- ' attrs' : dict (x5_root .attrs ),
415- ' members' : {}
427+ " type" : " group" ,
428+ " attrs" : dict (x5_root .attrs ),
429+ " members" : {},
416430 }
417431
418432
0 commit comments