Skip to content

Commit

Permalink
Alright maybe this'll work?
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Oct 23, 2024
1 parent 670ebc8 commit 9c5fdd8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 40 deletions.
20 changes: 16 additions & 4 deletions sdcflows/fieldmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class EstimatorType(Enum):
"sbref": EstimatorType.PEPOLAR,
"T1w": EstimatorType.ANAT,
"T2w": EstimatorType.ANAT,
"medic": EstimatorType.MEDIC,
}


Expand All @@ -84,6 +83,7 @@ def _type_setter(obj, attribute, value):
EstimatorType.PHASEDIFF,
EstimatorType.MAPPED,
EstimatorType.ANAT,
EstimatorType.MEDIC,
):
raise ValueError(f"Invalid estimation method type {value}.")

Expand Down Expand Up @@ -315,14 +315,25 @@ def __attrs_post_init__(self):

# Fieldmap option 1: actual field-mapping sequences
fmap_types = suffix_set.intersection(
("fieldmap", "phasediff", "phase1", "phase2", "bold")
("fieldmap", "phasediff", "phase1", "phase2")
)
if len(fmap_types) > 1 and fmap_types - set(("phase1", "phase2")):
raise TypeError(f"Incompatible suffixes found: <{','.join(fmap_types)}>.")

# Check for MEDIC
if ("part-mag" in self.sources[0].path.name) and ("echo-" in self.sources[0].path.name):
fmap_types = set(list(fmap_types) + ["medic"])
mag_bold, phase_bold, me_bold = 0, 0, 0
for f in self.sources:
if f.suffix == "bold" and ("part-mag" in f.path.name):
mag_bold += 1

if f.suffix == "bold" and ("part-phase" in f.path.name):
phase_bold += 1

if f.suffix == "bold" and ("echo-" in f.path.name):
me_bold += 1

if (mag_bold > 1) and (phase_bold > 1) and (me_bold > 2) and (mag_bold == phase_bold):
self.method = EstimatorType.MEDIC

if fmap_types:
sources = sorted(
Expand Down Expand Up @@ -388,6 +399,7 @@ def __attrs_post_init__(self):
[f for f in suffix_list if f in ("bold", "dwi", "epi", "sbref", "asl", "m0scan")]
) > 1
)
_pepolar_estimation = _pepolar_estimation and (self.method == EstimatorType.UNKNOWN)

if _pepolar_estimation and not anat_types:
self.method = MODALITIES[pepolar_types.pop()]
Expand Down
2 changes: 1 addition & 1 deletion sdcflows/utils/tests/test_wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def test_wrangler_filter(tmpdir, name, skeleton, estimations):
[
('pepolar', pepolar, 5),
('phasediff', phasediff, 3),
('medic', medic, 1),
('medic', medic, 3),
],
)
@pytest.mark.parametrize(
Expand Down
82 changes: 47 additions & 35 deletions sdcflows/utils/wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,53 @@ def find_estimators(
_log_debug_estimation(logger, e, layout.root)
estimators.append(e)

# Look for MEDIC field maps
# These need to be complex-valued multi-echo BOLD runs with ``IntendedFor``
has_intended = tuple()
with suppress(ValueError):
has_intended = layout.get(
**{
**base_entities,
**{
'session': sessions,
'echo': Query.REQUIRED,
'part': 'phase',
'suffix': 'bold',
'IntendedFor': Query.REQUIRED,
},
},
)

for bold_fmap in has_intended:
complex_imgs = layout.get(
**{
**bold_fmap.get_entities(),
**{'part': ['phase', 'mag'], 'echo': Query.ANY},
}
)

current_sources = [est.sources for est in estimators]
current_sources = [
str(item.path) for sublist in current_sources for item in sublist
]
if complex_imgs[0].path in current_sources:
print("Skipping fieldmap %s (already in use)" % complex_imgs[0].relpath)
continue

if current_sources:
raise Exception(complex_imgs[0].path, current_sources)

Check warning on line 490 in sdcflows/utils/wrangler.py

View check run for this annotation

Codecov / codecov/patch

sdcflows/utils/wrangler.py#L490

Added line #L490 was not covered by tests

e = fm.FieldmapEstimation(
[
fm.FieldmapFile(img.path, metadata=img.get_metadata())
for img in complex_imgs
]
)

_log_debug_estimation(logger, e, layout.root)
estimators.append(e)
continue

# At this point, only single-PE _epi files WITH ``IntendedFor`` can
# be automatically processed.
has_intended = tuple()
Expand Down Expand Up @@ -531,41 +578,6 @@ def find_estimators(
_log_debug_estimation(logger, e, layout.root)
estimators.append(e)

medic_entities = {**base_entities, **{'part': 'phase', 'echo': Query.ANY}}
has_phase = tuple()
with suppress(ValueError):
has_phase = layout.get(
suffix='bold',
**medic_entities,
)

for phase_img in has_phase:
complex_imgs = layout.get(
**{**phase_img.get_entities(), **{'part': ['phase', 'mag']}}
)

if complex_imgs[0].path in fm._estimators.sources:
continue

try:
e = fm.FieldmapEstimation(
[
fm.FieldmapFile(img.path, metadata=img.get_metadata())
for img in complex_imgs
]
)
except (ValueError, TypeError) as err:
_log_debug_estimator_fail(
logger,
"potential MEDIC fieldmap",
complex_imgs,
layout.root,
str(err),
)
else:
_log_debug_estimation(logger, e, layout.root)
estimators.append(e)

if estimators and not force_fmapless:
fmapless = False

Expand Down

0 comments on commit 9c5fdd8

Please sign in to comment.