Skip to content

Commit 891e81b

Browse files
authored
Merge pull request #342 from effigies/rf/split_pepolar_by_intent
RF: Split PEPolar fieldmaps by intent, if available
2 parents acfb2d6 + 78c2342 commit 891e81b

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

sdcflows/utils/wrangler.py

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ def find_estimators(
129129
[FieldmapEstimation(sources=<2 files>, method=<EstimatorType.PHASEDIFF: 3>,
130130
bids_id='auto_00006'),
131131
FieldmapEstimation(sources=<2 files>, method=<EstimatorType.PEPOLAR: 2>,
132-
bids_id='auto_00007')]
132+
bids_id='auto_00007'),
133+
FieldmapEstimation(sources=<2 files>, method=<EstimatorType.PEPOLAR: 2>,
134+
bids_id='auto_00008')]
133135
134136
Finally, *SDCFlows*' "*dataset A*" and "*dataset B*" contain BIDS structures
135137
with zero-byte NIfTI files and some corresponding metadata:
@@ -363,21 +365,22 @@ def find_estimators(
363365
)
364366
dirs = layout.get_directions(**entities)
365367
if len(dirs) > 1:
366-
fieldmaps = layout.get(**{**entities, **{"direction": dirs}})
367-
try:
368-
e = fm.FieldmapEstimation(
369-
[
370-
fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata())
371-
for fmap in fieldmaps
372-
]
373-
)
374-
except ValueError as err:
375-
_log_debug_estimator_fail(
376-
logger, "unnamed PEPOLAR", fieldmaps, layout.root, str(err)
377-
)
378-
else:
379-
_log_debug_estimation(logger, e, layout.root)
380-
estimators.append(e)
368+
by_intent = {}
369+
for fmap in layout.get(**{**entities, **{'direction': dirs}}):
370+
fmapfile = fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata())
371+
by_intent.setdefault(
372+
tuple(fmapfile.metadata.get('IntendedFor', ())), []
373+
).append(fmapfile)
374+
for collection in by_intent.values():
375+
try:
376+
e = fm.FieldmapEstimation(collection)
377+
except (ValueError, TypeError) as err:
378+
_log_debug_estimator_fail(
379+
logger, "unnamed PEPOLAR", collection, layout.root, str(err)
380+
)
381+
else:
382+
_log_debug_estimation(logger, e, layout.root)
383+
estimators.append(e)
381384

382385
# At this point, only single-PE _epi files WITH ``IntendedFor`` can
383386
# be automatically processed.
@@ -398,23 +401,43 @@ def find_estimators(
398401
logger.debug("Found single PE fieldmap %s", epi_fmap.relpath)
399402
epi_base_md = epi_fmap.get_metadata()
400403

401-
# There are two possible interpretations of an IntendedFor list:
402-
# 1) The fieldmap and each intended target are combined separately
403-
# 2) The fieldmap and all intended targets are combined at once
404-
#
405-
# (1) has been the historical interpretation of NiPreps,
406-
# so construct a separate estimator for each target.
404+
# Find existing IntendedFor targets and warn if missing
405+
all_targets = []
407406
for intent in listify(epi_base_md["IntendedFor"]):
408407
target = layout.get_file(str(subject_root / intent))
409408
if target is None:
410409
logger.debug("Single PE target %s not found", intent)
411410
continue
411+
all_targets.append(target)
412+
413+
# If sbrefs are targets, then the goal is generally to estimate with epi+sbref
414+
# and correct bold/dwi
415+
sbrefs = [
416+
target for target in all_targets if target.entities["suffix"] == "sbref"
417+
]
418+
if sbrefs:
419+
targets = sbrefs
420+
intent_map = []
421+
for sbref in sbrefs:
422+
ents = sbref.entities.copy()
423+
ents["suffix"] = ["bold", "dwi"]
424+
intent_map.append(
425+
[
426+
target
427+
for target in layout.get(**ents)
428+
if target in all_targets
429+
]
430+
)
431+
else:
432+
targets = all_targets
433+
intent_map = [[target] for target in all_targets]
412434

435+
for target, intent in zip(targets, intent_map):
413436
logger.debug("Found single PE target %s", target.relpath)
414437
# The new estimator is IntendedFor the individual targets,
415438
# even if the EPI file is IntendedFor multiple
416439
estimator_md = epi_base_md.copy()
417-
estimator_md["IntendedFor"] = [intent]
440+
estimator_md["IntendedFor"] = intent
418441
try:
419442
e = fm.FieldmapEstimation(
420443
[

0 commit comments

Comments
 (0)