Skip to content

Commit acd9b67

Browse files
committed
sty(black): standardize formatting a bit
1 parent 0b23506 commit acd9b67

File tree

2 files changed

+91
-70
lines changed

2 files changed

+91
-70
lines changed

niworkflows/interfaces/nibabel.py

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@
77
from nipype import logging
88
from nipype.utils.filemanip import fname_presuffix
99
from nipype.interfaces.base import (
10-
traits, TraitedSpec, BaseInterfaceInputSpec, File,
11-
SimpleInterface, OutputMultiObject, InputMultiObject
10+
traits,
11+
TraitedSpec,
12+
BaseInterfaceInputSpec,
13+
File,
14+
SimpleInterface,
15+
OutputMultiObject,
16+
InputMultiObject,
1217
)
1318

14-
IFLOGGER = logging.getLogger('nipype.interface')
19+
IFLOGGER = logging.getLogger("nipype.interface")
1520

1621

1722
class _ApplyMaskInputSpec(BaseInterfaceInputSpec):
18-
in_file = File(exists=True, mandatory=True, desc='an image')
19-
in_mask = File(exists=True, mandatory=True, desc='a mask')
20-
threshold = traits.Float(0.5, usedefault=True,
21-
desc='a threshold to the mask, if it is nonbinary')
23+
in_file = File(exists=True, mandatory=True, desc="an image")
24+
in_mask = File(exists=True, mandatory=True, desc="a mask")
25+
threshold = traits.Float(
26+
0.5, usedefault=True, desc="a threshold to the mask, if it is nonbinary"
27+
)
2228

2329

2430
class _ApplyMaskOutputSpec(TraitedSpec):
25-
out_file = File(exists=True, desc='masked file')
31+
out_file = File(exists=True, desc="masked file")
2632

2733

2834
class ApplyMask(SimpleInterface):
@@ -36,8 +42,9 @@ def _run_interface(self, runtime):
3642
msknii = nb.load(self.inputs.in_mask)
3743
msk = msknii.get_fdata() > self.inputs.threshold
3844

39-
self._results['out_file'] = fname_presuffix(
40-
self.inputs.in_file, suffix='_masked', newpath=runtime.cwd)
45+
self._results["out_file"] = fname_presuffix(
46+
self.inputs.in_file, suffix="_masked", newpath=runtime.cwd
47+
)
4148

4249
if img.dataobj.shape[:3] != msk.shape:
4350
raise ValueError("Image and mask sizes do not match.")
@@ -49,19 +56,18 @@ def _run_interface(self, runtime):
4956
msk = msk[..., np.newaxis]
5057

5158
masked = img.__class__(img.dataobj * msk, None, img.header)
52-
masked.to_filename(self._results['out_file'])
59+
masked.to_filename(self._results["out_file"])
5360
return runtime
5461

5562

5663
class _BinarizeInputSpec(BaseInterfaceInputSpec):
57-
in_file = File(exists=True, mandatory=True, desc='input image')
58-
thresh_low = traits.Float(mandatory=True,
59-
desc='non-inclusive lower threshold')
64+
in_file = File(exists=True, mandatory=True, desc="input image")
65+
thresh_low = traits.Float(mandatory=True, desc="non-inclusive lower threshold")
6066

6167

6268
class _BinarizeOutputSpec(TraitedSpec):
63-
out_file = File(exists=True, desc='masked file')
64-
out_mask = File(exists=True, desc='output mask')
69+
out_file = File(exists=True, desc="masked file")
70+
out_mask = File(exists=True, desc="output mask")
6571

6672

6773
class Binarize(SimpleInterface):
@@ -73,33 +79,35 @@ class Binarize(SimpleInterface):
7379
def _run_interface(self, runtime):
7480
img = nb.load(self.inputs.in_file)
7581

76-
self._results['out_file'] = fname_presuffix(
77-
self.inputs.in_file, suffix='_masked', newpath=runtime.cwd)
78-
self._results['out_mask'] = fname_presuffix(
79-
self.inputs.in_file, suffix='_mask', newpath=runtime.cwd)
82+
self._results["out_file"] = fname_presuffix(
83+
self.inputs.in_file, suffix="_masked", newpath=runtime.cwd
84+
)
85+
self._results["out_mask"] = fname_presuffix(
86+
self.inputs.in_file, suffix="_mask", newpath=runtime.cwd
87+
)
8088

8189
data = img.get_fdata()
8290
mask = data > self.inputs.thresh_low
8391
data[~mask] = 0.0
8492
masked = img.__class__(data, img.affine, img.header)
85-
masked.to_filename(self._results['out_file'])
93+
masked.to_filename(self._results["out_file"])
8694

87-
img.header.set_data_dtype('uint8')
88-
maskimg = img.__class__(mask.astype('uint8'), img.affine,
89-
img.header)
90-
maskimg.to_filename(self._results['out_mask'])
95+
img.header.set_data_dtype("uint8")
96+
maskimg = img.__class__(mask.astype("uint8"), img.affine, img.header)
97+
maskimg.to_filename(self._results["out_mask"])
9198

9299
return runtime
93100

94101

95102
class _FourToThreeInputSpec(BaseInterfaceInputSpec):
96-
in_file = File(exists=True, mandatory=True, desc='input 4d image')
97-
allow_3D = traits.Bool(False, usedefault=True, desc='do not fail if a 3D volume is passed in')
103+
in_file = File(exists=True, mandatory=True, desc="input 4d image")
104+
allow_3D = traits.Bool(
105+
False, usedefault=True, desc="do not fail if a 3D volume is passed in"
106+
)
98107

99108

100109
class _FourToThreeOutputSpec(TraitedSpec):
101-
out_files = OutputMultiObject(File(exists=True),
102-
desc='output list of 3d images')
110+
out_files = OutputMultiObject(File(exists=True), desc="output list of 3d images")
103111

104112

105113
class SplitSeries(SimpleInterface):
@@ -117,9 +125,11 @@ def _run_interface(self, runtime):
117125
if ndim != 4:
118126
if self.inputs.allow_3D and ndim == 3:
119127
out_file = str(
120-
Path(fname_presuffix(self.inputs.in_file, suffix=f"_idx-000")).absolute()
128+
Path(
129+
fname_presuffix(self.inputs.in_file, suffix=f"_idx-000")
130+
).absolute()
121131
)
122-
self._results['out_files'] = out_file
132+
self._results["out_files"] = out_file
123133
filenii.to_filename(out_file)
124134
return runtime
125135
raise RuntimeError(
@@ -128,27 +138,29 @@ def _run_interface(self, runtime):
128138
)
129139

130140
files_3d = nb.four_to_three(filenii)
131-
self._results['out_files'] = []
141+
self._results["out_files"] = []
132142
in_file = self.inputs.in_file
133143
for i, file_3d in enumerate(files_3d):
134144
out_file = str(
135145
Path(fname_presuffix(in_file, suffix=f"_idx-{i:03}")).absolute()
136146
)
137147
file_3d.to_filename(out_file)
138-
self._results['out_files'].append(out_file)
148+
self._results["out_files"].append(out_file)
139149

140150
return runtime
141151

142152

143153
class _MergeSeriesInputSpec(BaseInterfaceInputSpec):
144-
in_files = InputMultiObject(File(exists=True, mandatory=True,
145-
desc='input list of 3d images'))
146-
allow_4D = traits.Bool(True, usedefault=True,
147-
desc='whether 4D images are allowed to be concatenated')
154+
in_files = InputMultiObject(
155+
File(exists=True, mandatory=True, desc="input list of 3d images")
156+
)
157+
allow_4D = traits.Bool(
158+
True, usedefault=True, desc="whether 4D images are allowed to be concatenated"
159+
)
148160

149161

150162
class _MergeSeriesOutputSpec(TraitedSpec):
151-
out_file = File(exists=True, desc='output 4d image')
163+
out_file = File(exists=True, desc="output 4d image")
152164

153165

154166
class MergeSeries(SimpleInterface):
@@ -169,12 +181,13 @@ def _run_interface(self, runtime):
169181
nii_list += nb.four_to_three(filenii)
170182
continue
171183
else:
172-
raise ValueError("Input image has an incorrect number of dimensions"
173-
f" ({ndim}).")
184+
raise ValueError(
185+
"Input image has an incorrect number of dimensions" f" ({ndim})."
186+
)
174187

175188
img_4d = nb.concat_images(nii_list)
176189
out_file = fname_presuffix(self.inputs.in_files[0], suffix="_merged")
177190
img_4d.to_filename(out_file)
178191

179-
self._results['out_file'] = out_file
192+
self._results["out_file"] = out_file
180193
return runtime

niworkflows/interfaces/tests/test_nibabel.py

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def test_Binarize(tmp_path):
1414
mask = np.zeros((20, 20, 20), dtype=bool)
1515
mask[5:15, 5:15, 5:15] = bool
1616

17-
data = np.zeros_like(mask, dtype='float32')
17+
data = np.zeros_like(mask, dtype="float32")
1818
data[mask] = np.random.gamma(2, size=mask.sum())
1919

20-
in_file = tmp_path / 'input.nii.gz'
20+
in_file = tmp_path / "input.nii.gz"
2121
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
2222

2323
binif = Binarize(thresh_low=0.0, in_file=str(in_file)).run()
@@ -36,28 +36,32 @@ def test_ApplyMask(tmp_path):
3636
mask[8:11, 8:11, 8:11] = 1.0
3737

3838
# Test the 3D
39-
in_file = tmp_path / 'input3D.nii.gz'
39+
in_file = tmp_path / "input3D.nii.gz"
4040
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
4141

42-
in_mask = tmp_path / 'mask.nii.gz'
42+
in_mask = tmp_path / "mask.nii.gz"
4343
nb.Nifti1Image(mask, np.eye(4), None).to_filename(str(in_mask))
4444

4545
masked1 = ApplyMask(in_file=str(in_file), in_mask=str(in_mask), threshold=0.4).run()
46-
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 5**3
46+
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 5 ** 3
4747

4848
masked1 = ApplyMask(in_file=str(in_file), in_mask=str(in_mask), threshold=0.6).run()
49-
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 3**3
49+
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 3 ** 3
5050

5151
data4d = np.stack((data, 2 * data, 3 * data), axis=-1)
5252
# Test the 4D case
53-
in_file4d = tmp_path / 'input4D.nii.gz'
53+
in_file4d = tmp_path / "input4D.nii.gz"
5454
nb.Nifti1Image(data4d, np.eye(4), None).to_filename(str(in_file4d))
5555

56-
masked1 = ApplyMask(in_file=str(in_file4d), in_mask=str(in_mask), threshold=0.4).run()
57-
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 5**3 * 6
56+
masked1 = ApplyMask(
57+
in_file=str(in_file4d), in_mask=str(in_mask), threshold=0.4
58+
).run()
59+
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 5 ** 3 * 6
5860

59-
masked1 = ApplyMask(in_file=str(in_file4d), in_mask=str(in_mask), threshold=0.6).run()
60-
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 3**3 * 6
61+
masked1 = ApplyMask(
62+
in_file=str(in_file4d), in_mask=str(in_mask), threshold=0.6
63+
).run()
64+
assert nb.load(masked1.outputs.out_file).get_fdata().sum() == 3 ** 3 * 6
6165

6266
# Test errors
6367
nb.Nifti1Image(mask, 2 * np.eye(4), None).to_filename(str(in_mask))
@@ -77,16 +81,17 @@ def test_SplitSeries(tmp_path):
7781

7882
# Test the 4D
7983
data = np.ones((20, 20, 20, 15), dtype=float)
80-
in_file = tmp_path / 'input4D.nii.gz'
84+
in_file = tmp_path / "input4D.nii.gz"
8185
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
8286

8387
split = SplitSeries(in_file=str(in_file)).run()
8488
assert len(split.outputs.out_files) == 15
8589

8690
# Test the 3D
87-
data = np.ones((20, 20, 20), dtype=float)
88-
in_file = tmp_path / 'input3D.nii.gz'
89-
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
91+
in_file = tmp_path / "input3D.nii.gz"
92+
nb.Nifti1Image(np.ones((20, 20, 20), dtype=float), np.eye(4), None).to_filename(
93+
str(in_file)
94+
)
9095

9196
with pytest.raises(RuntimeError):
9297
SplitSeries(in_file=str(in_file)).run()
@@ -95,9 +100,10 @@ def test_SplitSeries(tmp_path):
95100
assert isinstance(split.outputs.out_files, str)
96101

97102
# Test the 3D
98-
data = np.ones((20, 20, 20, 1), dtype=float)
99-
in_file = tmp_path / 'input3D.nii.gz'
100-
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
103+
in_file = tmp_path / "input3D.nii.gz"
104+
nb.Nifti1Image(np.ones((20, 20, 20, 1), dtype=float), np.eye(4), None).to_filename(
105+
str(in_file)
106+
)
101107

102108
with pytest.raises(RuntimeError):
103109
SplitSeries(in_file=str(in_file)).run()
@@ -106,9 +112,10 @@ def test_SplitSeries(tmp_path):
106112
assert isinstance(split.outputs.out_files, str)
107113

108114
# Test the 5D
109-
data = np.ones((20, 20, 20, 2, 2), dtype=float)
110-
in_file = tmp_path / 'input5D.nii.gz'
111-
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
115+
in_file = tmp_path / "input5D.nii.gz"
116+
nb.Nifti1Image(
117+
np.ones((20, 20, 20, 2, 2), dtype=float), np.eye(4), None
118+
).to_filename(str(in_file))
112119

113120
with pytest.raises(RuntimeError):
114121
SplitSeries(in_file=str(in_file)).run()
@@ -118,7 +125,7 @@ def test_SplitSeries(tmp_path):
118125

119126
# Test splitting ANTs warpfields
120127
data = np.ones((20, 20, 20, 1, 3), dtype=float)
121-
in_file = tmp_path / 'warpfield.nii.gz'
128+
in_file = tmp_path / "warpfield.nii.gz"
122129
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
123130

124131
split = SplitSeries(in_file=str(in_file)).run()
@@ -129,17 +136,18 @@ def test_MergeSeries(tmp_path):
129136
"""Test 3-to-4 NIfTI concatenation interface."""
130137
os.chdir(str(tmp_path))
131138

132-
data = np.ones((20, 20, 20), dtype=float)
133-
in_file = tmp_path / 'input3D.nii.gz'
134-
nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file))
139+
in_file = tmp_path / "input3D.nii.gz"
140+
nb.Nifti1Image(np.ones((20, 20, 20), dtype=float), np.eye(4), None).to_filename(
141+
str(in_file)
142+
)
135143

136144
merge = MergeSeries(in_files=[str(in_file)] * 5).run()
137145
assert nb.load(merge.outputs.out_file).dataobj.shape == (20, 20, 20, 5)
138146

139-
in_4D = tmp_path / 'input4D.nii.gz'
140-
nb.Nifti1Image(
141-
np.ones((20, 20, 20, 4), dtype=float), np.eye(4), None
142-
).to_filename(str(in_4D))
147+
in_4D = tmp_path / "input4D.nii.gz"
148+
nb.Nifti1Image(np.ones((20, 20, 20, 4), dtype=float), np.eye(4), None).to_filename(
149+
str(in_4D)
150+
)
143151

144152
merge = MergeSeries(in_files=[str(in_file)] + [str(in_4D)]).run()
145153
assert nb.load(merge.outputs.out_file).dataobj.shape == (20, 20, 20, 5)

0 commit comments

Comments
 (0)