Skip to content

Commit 19bc353

Browse files
committed
sty: run ruff
1 parent dd399a7 commit 19bc353

File tree

6 files changed

+130
-103
lines changed

6 files changed

+130
-103
lines changed

nitransforms/base.py

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Common interface for transforms."""
10+
1011
from pathlib import Path
1112
import numpy as np
1213
import 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):
248249
class 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

nitransforms/cli.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import pprint
1212

13+
1314
def cli_apply(pargs):
1415
"""
1516
Apply a transformation to an image, resampling on the reference.
@@ -34,8 +35,8 @@ def cli_apply(pargs):
3435

3536
xfm = (
3637
nlinload(pargs.transform, fmt=fmt)
37-
if pargs.nonlinear else
38-
linload(pargs.transform, fmt=fmt)
38+
if pargs.nonlinear
39+
else linload(pargs.transform, fmt=fmt)
3940
)
4041

4142
# ensure a reference is set
@@ -49,12 +50,11 @@ def cli_apply(pargs):
4950
cval=pargs.cval,
5051
prefilter=pargs.prefilter,
5152
)
52-
#moved.to_filename(pargs.out or f"nt_{os.path.basename(pargs.moving)}")
53+
# moved.to_filename(pargs.out or f"nt_{os.path.basename(pargs.moving)}")
5354

5455

5556
def cli_xfm_util(pargs):
56-
"""
57-
"""
57+
""" """
5858

5959
xfm_data = xfm_loader(pargs.transform)
6060
xfm_x5 = TransformBase(**xfm_data)
@@ -68,7 +68,7 @@ def cli_xfm_util(pargs):
6868
filename = f"{os.path.basename(pargs.transform).split('.')[0]}.x5"
6969
xfm_x5.to_filename(filename)
7070
print(f"Writing out {filename}")
71-
71+
7272

7373
def get_parser():
7474
desc = dedent(
@@ -147,12 +147,12 @@ def _add_subparser(name, description):
147147
xfm_util = _add_subparser("xfm_util", cli_xfm_util.__doc__)
148148
xfm_util.set_defaults(func=cli_xfm_util)
149149
xfm_util.add_argument("transform", help="The transform file")
150-
xfm_util.add_argument("--info",
151-
action="store_true",
152-
help="Get information about the transform")
153-
xfm_util.add_argument("--x5",
154-
action="store_true",
155-
help="Convert transform to .x5 file format.")
150+
xfm_util.add_argument(
151+
"--info", action="store_true", help="Get information about the transform"
152+
)
153+
xfm_util.add_argument(
154+
"--x5", action="store_true", help="Convert transform to .x5 file format."
155+
)
156156

157157
return parser, subparsers
158158

nitransforms/io/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
"""Read and write transforms."""
4+
45
from nitransforms.io import afni, fsl, itk, lta, x5
56
from nitransforms.io.base import TransformIOError, TransformFileError
67

@@ -22,7 +23,7 @@
2223
"fs": (lta, "FSLinearTransform"),
2324
"fsl": (fsl, "FSLLinearTransform"),
2425
"afni": (afni, "AFNILinearTransform"),
25-
"x5": (x5, "X5Transform")
26+
"x5": (x5, "X5Transform"),
2627
}
2728

2829

0 commit comments

Comments
 (0)