Skip to content

Commit 2ae6131

Browse files
committed
sty: run black
1 parent f4c0ca6 commit 2ae6131

File tree

2 files changed

+95
-51
lines changed

2 files changed

+95
-51
lines changed

sdcflows/fieldmaps.py

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def _type_setter(obj, attribute, value):
4545
return value
4646

4747
if obj.method != EstimatorType.UNKNOWN and obj.method != value:
48-
raise TypeError(
49-
f"Cannot change determined method {obj.method} to {value}."
50-
)
48+
raise TypeError(f"Cannot change determined method {obj.method} to {value}.")
5149

5250
if value not in (
5351
EstimatorType.PEPOLAR,
@@ -184,14 +182,12 @@ def __attrs_post_init__(self):
184182
"""Validate metadata and additional checks."""
185183
self.entities = parse_file_entities(str(self.path))
186184
self.suffix = self.entities.pop("suffix")
187-
extension = self.entities.pop('extension').lstrip('.')
185+
extension = self.entities.pop("extension").lstrip(".")
188186

189187
# Automatically fill metadata in when possible
190188
# TODO: implement BIDS hierarchy of metadata
191189
if self.find_meta:
192-
sidecar = Path(
193-
str(self.path).replace(extension, "json")
194-
)
190+
sidecar = Path(str(self.path).replace(extension, "json"))
195191
if sidecar.is_file():
196192
_meta = self.metadata or {}
197193
self.metadata = loads(sidecar.read_text())
@@ -200,26 +196,32 @@ def __attrs_post_init__(self):
200196
# Attempt to infer a bids_root folder
201197
relative_path = relative_to_root(self.path)
202198
if str(relative_path) != str(self.path):
203-
self.bids_root = Path(str(self.path)[:-len(str(relative_path))])
199+
self.bids_root = Path(str(self.path)[: -len(str(relative_path))])
204200

205201
# Check for REQUIRED metadata (depends on suffix.)
206202
if self.suffix in ("bold", "dwi", "epi", "sbref"):
207203
if "PhaseEncodingDirection" not in self.metadata:
208-
raise MetadataError(f"Missing 'PhaseEncodingDirection' for <{self.path}>.")
204+
raise MetadataError(
205+
f"Missing 'PhaseEncodingDirection' for <{self.path}>."
206+
)
209207
if not (
210208
set(("TotalReadoutTime", "EffectiveEchoSpacing")).intersection(
211-
self.metadata.keys())
209+
self.metadata.keys()
210+
)
212211
):
213-
raise MetadataError(f"Missing readout timing information for <{self.path}>.")
212+
raise MetadataError(
213+
f"Missing readout timing information for <{self.path}>."
214+
)
214215

215216
if self.suffix == "fieldmap" and "Units" not in self.metadata:
216217
raise MetadataError(f"Missing 'Units' for <{self.path}>.")
217218

218-
if (
219-
self.suffix == "phasediff"
220-
and ("EchoTime1" not in self.metadata or "EchoTime2" not in self.metadata)
219+
if self.suffix == "phasediff" and (
220+
"EchoTime1" not in self.metadata or "EchoTime2" not in self.metadata
221221
):
222-
raise MetadataError(f"Missing 'EchoTime1' and/or 'EchoTime2' for <{self.path}>.")
222+
raise MetadataError(
223+
f"Missing 'EchoTime1' and/or 'EchoTime2' for <{self.path}>."
224+
)
223225

224226
if self.suffix in ("phase1", "phase2") and ("EchoTime" not in self.metadata):
225227
raise MetadataError(f"Missing 'EchoTime' for <{self.path}>.")
@@ -256,13 +258,16 @@ def __attrs_post_init__(self):
256258
suffix_set = set(suffix_list)
257259

258260
# Fieldmap option 1: actual field-mapping sequences
259-
fmap_types = suffix_set.intersection(("fieldmap", "phasediff", "phase1", "phase2"))
261+
fmap_types = suffix_set.intersection(
262+
("fieldmap", "phasediff", "phase1", "phase2")
263+
)
260264
if len(fmap_types) > 1 and fmap_types - set(("phase1", "phase2")):
261265
raise TypeError(f"Incompatible suffices found: <{','.join(fmap_types)}>.")
262266

263267
if fmap_types:
264268
sources = sorted(
265-
str(f.path) for f in self.sources
269+
str(f.path)
270+
for f in self.sources
266271
if f.suffix in ("fieldmap", "phasediff", "phase1", "phase2")
267272
)
268273

@@ -282,22 +287,33 @@ def __attrs_post_init__(self):
282287
magnitude = f"magnitude{'' if self.method == EstimatorType.MAPPED else '1'}"
283288
if magnitude not in suffix_set:
284289
try:
285-
self.sources.append(FieldmapFile(
286-
sources[0].replace("fieldmap", "magnitude").replace(
287-
"diff", "1").replace("phase", "magnitude")
288-
))
290+
self.sources.append(
291+
FieldmapFile(
292+
sources[0]
293+
.replace("fieldmap", "magnitude")
294+
.replace("diff", "1")
295+
.replace("phase", "magnitude")
296+
)
297+
)
289298
except Exception:
290299
raise ValueError(
291300
"A fieldmap or phase-difference estimation type was found, "
292301
f"but an anatomical reference ({magnitude} file) is missing."
293302
)
294303

295304
# Check presence and try to find (if necessary) the second magnitude file
296-
if self.method == EstimatorType.PHASEDIFF and "magnitude2" not in suffix_set:
305+
if (
306+
self.method == EstimatorType.PHASEDIFF
307+
and "magnitude2" not in suffix_set
308+
):
297309
try:
298-
self.sources.append(FieldmapFile(
299-
sources[-1].replace("diff", "2").replace("phase", "magnitude")
300-
))
310+
self.sources.append(
311+
FieldmapFile(
312+
sources[-1]
313+
.replace("diff", "2")
314+
.replace("phase", "magnitude")
315+
)
316+
)
301317
except Exception:
302318
if "phase2" in suffix_set:
303319
raise ValueError(
@@ -308,9 +324,9 @@ def __attrs_post_init__(self):
308324
# Fieldmap option 2: PEPOLAR (and fieldmap-less or ANAT)
309325
# IMPORTANT NOTE: fieldmap-less approaches can be considered PEPOLAR with RO = 0.0s
310326
pepolar_types = suffix_set.intersection(("bold", "dwi", "epi", "sbref"))
311-
_pepolar_estimation = len([
312-
f for f in suffix_list if f in ("bold", "dwi", "epi", "sbref")
313-
]) > 1
327+
_pepolar_estimation = (
328+
len([f for f in suffix_list if f in ("bold", "dwi", "epi", "sbref")]) > 1
329+
)
314330

315331
if _pepolar_estimation:
316332
self.method = MODALITIES[pepolar_types.pop()]

sdcflows/tests/test_fieldmaps.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,48 @@ def test_FieldmapFile(testdata_dir):
88
FieldmapFile(testdata_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz")
99

1010

11-
@pytest.mark.parametrize("inputfiles,method,nsources", [
12-
(("fmap/sub-01_fieldmap.nii.gz", "fmap/sub-01_magnitude.nii.gz"),
13-
EstimatorType.MAPPED, 2),
14-
(("fmap/sub-01_phase1.nii.gz", "fmap/sub-01_phase2.nii.gz",
15-
"fmap/sub-01_magnitude1.nii.gz", "fmap/sub-01_magnitude2.nii.gz"),
16-
EstimatorType.PHASEDIFF, 4),
17-
(("fmap/sub-01_phase1.nii.gz", "fmap/sub-01_phase2.nii.gz"),
18-
EstimatorType.PHASEDIFF, 4),
19-
(("fmap/sub-01_phase2.nii.gz",), EstimatorType.PHASEDIFF, 4),
20-
(("fmap/sub-01_phase1.nii.gz",), EstimatorType.PHASEDIFF, 4),
21-
(("fmap/sub-01_dir-LR_epi.nii.gz", "fmap/sub-01_dir-RL_epi.nii.gz"),
22-
EstimatorType.PEPOLAR, 2),
23-
(("fmap/sub-01_dir-LR_epi.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
24-
EstimatorType.PEPOLAR, 2),
25-
(("anat/sub-01_T1w.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
26-
EstimatorType.ANAT, 2),
27-
])
11+
@pytest.mark.parametrize(
12+
"inputfiles,method,nsources",
13+
[
14+
(
15+
("fmap/sub-01_fieldmap.nii.gz", "fmap/sub-01_magnitude.nii.gz"),
16+
EstimatorType.MAPPED,
17+
2,
18+
),
19+
(
20+
(
21+
"fmap/sub-01_phase1.nii.gz",
22+
"fmap/sub-01_phase2.nii.gz",
23+
"fmap/sub-01_magnitude1.nii.gz",
24+
"fmap/sub-01_magnitude2.nii.gz",
25+
),
26+
EstimatorType.PHASEDIFF,
27+
4,
28+
),
29+
(
30+
("fmap/sub-01_phase1.nii.gz", "fmap/sub-01_phase2.nii.gz"),
31+
EstimatorType.PHASEDIFF,
32+
4,
33+
),
34+
(("fmap/sub-01_phase2.nii.gz",), EstimatorType.PHASEDIFF, 4),
35+
(("fmap/sub-01_phase1.nii.gz",), EstimatorType.PHASEDIFF, 4),
36+
(
37+
("fmap/sub-01_dir-LR_epi.nii.gz", "fmap/sub-01_dir-RL_epi.nii.gz"),
38+
EstimatorType.PEPOLAR,
39+
2,
40+
),
41+
(
42+
("fmap/sub-01_dir-LR_epi.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
43+
EstimatorType.PEPOLAR,
44+
2,
45+
),
46+
(
47+
("anat/sub-01_T1w.nii.gz", "dwi/sub-01_dir-RL_sbref.nii.gz"),
48+
EstimatorType.ANAT,
49+
2,
50+
),
51+
],
52+
)
2853
def test_FieldmapEstimation(testdata_dir, inputfiles, method, nsources):
2954
"""Test errors."""
3055
sub_dir = testdata_dir / "sub-01"
@@ -34,12 +59,15 @@ def test_FieldmapEstimation(testdata_dir, inputfiles, method, nsources):
3459
assert len(fe.sources) == nsources
3560

3661

37-
@pytest.mark.parametrize("inputfiles,errortype", [
38-
(("fmap/sub-01_fieldmap.nii.gz", "fmap/sub-01_phasediff.nii.gz"), TypeError),
39-
(("fmap/sub-01_dir-RL_epi.nii.gz",), ValueError),
40-
(("anat/sub-01_T1w.nii.gz",), ValueError),
41-
(("anat/sub-01_T1w.nii.gz", "fmap/sub-01_phase2.nii.gz"), TypeError),
42-
])
62+
@pytest.mark.parametrize(
63+
"inputfiles,errortype",
64+
[
65+
(("fmap/sub-01_fieldmap.nii.gz", "fmap/sub-01_phasediff.nii.gz"), TypeError),
66+
(("fmap/sub-01_dir-RL_epi.nii.gz",), ValueError),
67+
(("anat/sub-01_T1w.nii.gz",), ValueError),
68+
(("anat/sub-01_T1w.nii.gz", "fmap/sub-01_phase2.nii.gz"), TypeError),
69+
],
70+
)
4371
def test_FieldmapEstimationError(testdata_dir, inputfiles, errortype):
4472
"""Test errors."""
4573
sub_dir = testdata_dir / "sub-01"

0 commit comments

Comments
 (0)