Skip to content

Commit 9c5fdd8

Browse files
committed
Alright maybe this'll work?
1 parent 670ebc8 commit 9c5fdd8

File tree

3 files changed

+64
-40
lines changed

3 files changed

+64
-40
lines changed

sdcflows/fieldmaps.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class EstimatorType(Enum):
6868
"sbref": EstimatorType.PEPOLAR,
6969
"T1w": EstimatorType.ANAT,
7070
"T2w": EstimatorType.ANAT,
71-
"medic": EstimatorType.MEDIC,
7271
}
7372

7473

@@ -84,6 +83,7 @@ def _type_setter(obj, attribute, value):
8483
EstimatorType.PHASEDIFF,
8584
EstimatorType.MAPPED,
8685
EstimatorType.ANAT,
86+
EstimatorType.MEDIC,
8787
):
8888
raise ValueError(f"Invalid estimation method type {value}.")
8989

@@ -315,14 +315,25 @@ def __attrs_post_init__(self):
315315

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

323323
# Check for MEDIC
324-
if ("part-mag" in self.sources[0].path.name) and ("echo-" in self.sources[0].path.name):
325-
fmap_types = set(list(fmap_types) + ["medic"])
324+
mag_bold, phase_bold, me_bold = 0, 0, 0
325+
for f in self.sources:
326+
if f.suffix == "bold" and ("part-mag" in f.path.name):
327+
mag_bold += 1
328+
329+
if f.suffix == "bold" and ("part-phase" in f.path.name):
330+
phase_bold += 1
331+
332+
if f.suffix == "bold" and ("echo-" in f.path.name):
333+
me_bold += 1
334+
335+
if (mag_bold > 1) and (phase_bold > 1) and (me_bold > 2) and (mag_bold == phase_bold):
336+
self.method = EstimatorType.MEDIC
326337

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

392404
if _pepolar_estimation and not anat_types:
393405
self.method = MODALITIES[pepolar_types.pop()]

sdcflows/utils/tests/test_wrangler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def test_wrangler_filter(tmpdir, name, skeleton, estimations):
708708
[
709709
('pepolar', pepolar, 5),
710710
('phasediff', phasediff, 3),
711-
('medic', medic, 1),
711+
('medic', medic, 3),
712712
],
713713
)
714714
@pytest.mark.parametrize(

sdcflows/utils/wrangler.py

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,53 @@ def find_estimators(
453453
_log_debug_estimation(logger, e, layout.root)
454454
estimators.append(e)
455455

456+
# Look for MEDIC field maps
457+
# These need to be complex-valued multi-echo BOLD runs with ``IntendedFor``
458+
has_intended = tuple()
459+
with suppress(ValueError):
460+
has_intended = layout.get(
461+
**{
462+
**base_entities,
463+
**{
464+
'session': sessions,
465+
'echo': Query.REQUIRED,
466+
'part': 'phase',
467+
'suffix': 'bold',
468+
'IntendedFor': Query.REQUIRED,
469+
},
470+
},
471+
)
472+
473+
for bold_fmap in has_intended:
474+
complex_imgs = layout.get(
475+
**{
476+
**bold_fmap.get_entities(),
477+
**{'part': ['phase', 'mag'], 'echo': Query.ANY},
478+
}
479+
)
480+
481+
current_sources = [est.sources for est in estimators]
482+
current_sources = [
483+
str(item.path) for sublist in current_sources for item in sublist
484+
]
485+
if complex_imgs[0].path in current_sources:
486+
print("Skipping fieldmap %s (already in use)" % complex_imgs[0].relpath)
487+
continue
488+
489+
if current_sources:
490+
raise Exception(complex_imgs[0].path, current_sources)
491+
492+
e = fm.FieldmapEstimation(
493+
[
494+
fm.FieldmapFile(img.path, metadata=img.get_metadata())
495+
for img in complex_imgs
496+
]
497+
)
498+
499+
_log_debug_estimation(logger, e, layout.root)
500+
estimators.append(e)
501+
continue
502+
456503
# At this point, only single-PE _epi files WITH ``IntendedFor`` can
457504
# be automatically processed.
458505
has_intended = tuple()
@@ -531,41 +578,6 @@ def find_estimators(
531578
_log_debug_estimation(logger, e, layout.root)
532579
estimators.append(e)
533580

534-
medic_entities = {**base_entities, **{'part': 'phase', 'echo': Query.ANY}}
535-
has_phase = tuple()
536-
with suppress(ValueError):
537-
has_phase = layout.get(
538-
suffix='bold',
539-
**medic_entities,
540-
)
541-
542-
for phase_img in has_phase:
543-
complex_imgs = layout.get(
544-
**{**phase_img.get_entities(), **{'part': ['phase', 'mag']}}
545-
)
546-
547-
if complex_imgs[0].path in fm._estimators.sources:
548-
continue
549-
550-
try:
551-
e = fm.FieldmapEstimation(
552-
[
553-
fm.FieldmapFile(img.path, metadata=img.get_metadata())
554-
for img in complex_imgs
555-
]
556-
)
557-
except (ValueError, TypeError) as err:
558-
_log_debug_estimator_fail(
559-
logger,
560-
"potential MEDIC fieldmap",
561-
complex_imgs,
562-
layout.root,
563-
str(err),
564-
)
565-
else:
566-
_log_debug_estimation(logger, e, layout.root)
567-
estimators.append(e)
568-
569581
if estimators and not force_fmapless:
570582
fmapless = False
571583

0 commit comments

Comments
 (0)