Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Find B0FieldIdentifiers when one image contributes to multiple #298

Merged
merged 11 commits into from
Dec 6, 2022
22 changes: 13 additions & 9 deletions sdcflows/fieldmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,20 @@ def __attrs_post_init__(self):
# Register this estimation method
if not self.bids_id:
# If not manually set, try to get it from BIDS metadata
bids_ids = set(
[
f.metadata.get("B0FieldIdentifier")
for f in self.sources
if f.metadata.get("B0FieldIdentifier")
]
)
if len(bids_ids) > 1:
b0_ids = [
listify(f.metadata.get("B0FieldIdentifier"))
for f in self.sources
if f.metadata.get("B0FieldIdentifier")
]
bids_ids = set.intersection(*b0_ids)
bpinsard marked this conversation as resolved.
Show resolved Hide resolved

if not bids_ids:
bpinsard marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
f"No common ``B0FieldIdentifier`` found: <{', '.join(b0_ids)}>"
)
elif len(bids_ids) > 1:
raise ValueError(
f"Multiple ``B0FieldIdentifier`` set: <{', '.join(bids_ids)}>"
f"Multiple common ``B0FieldIdentifier``s found: <{', '.join(bids_ids)}>"
)
elif bids_ids:
object.__setattr__(self, "bids_id", bids_ids.pop())
Expand Down
5 changes: 4 additions & 1 deletion sdcflows/utils/wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def find_estimators(
b0_ids = tuple()
with suppress(BIDSEntityError):
b0_ids = layout.get_B0FieldIdentifiers(**base_entities)
# flatten lists from json (tupled in pybids for hashing), then unique
b0_ids = set(sum([(b0_id if isinstance(b0_id, tuple) else (b0_id,)) for b0_id in b0_ids ],()))

if b0_ids:
logger.debug(
Expand All @@ -296,7 +298,8 @@ def find_estimators(

e = fm.FieldmapEstimation([
fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata())
for fmap in layout.get(**b0_entities)
for fmap in layout.get(**base_entities, B0FieldIdentifier=b0_id) +
layout.get(**base_entities, B0FieldIdentifier=f'"{b0_id}"', regex_search=True)
])
_log_debug_estimation(logger, e, layout.root)
estimators.append(e)
Expand Down