Skip to content

Commit 82e7593

Browse files
bpinsardeffigies
andauthored
ENH: Find B0FieldIdentifiers when one image contributes to multiple (#298)
* fix/hacks for pepolar scheme with B0FieldIdentifier as lists * fix if no B0FieldIdentifier, empty list, (testing) * Update sdcflows/fieldmaps.py Co-authored-by: Chris Markiewicz <[email protected]> * Update sdcflows/fieldmaps.py Co-authored-by: Chris Markiewicz <[email protected]> * Update sdcflows/fieldmaps.py Co-authored-by: Chris Markiewicz <[email protected]> * FIX: Update logic for finding b0 fields * FIX: Break layout.get calls into separate lines, use repr for match instead of custom wrapping * FIX: A more perfect union * FIX: JSON, not repr Co-authored-by: Chris Markiewicz <[email protected]>
1 parent 7898fac commit 82e7593

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

sdcflows/fieldmaps.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -404,27 +404,31 @@ def __attrs_post_init__(self):
404404
# Register this estimation method
405405
if not self.bids_id:
406406
# If not manually set, try to get it from BIDS metadata
407-
bids_ids = set(
408-
[
409-
f.metadata.get("B0FieldIdentifier")
410-
for f in self.sources
411-
if f.metadata.get("B0FieldIdentifier")
412-
]
413-
)
414-
if len(bids_ids) > 1:
415-
raise ValueError(
416-
f"Multiple ``B0FieldIdentifier`` set: <{', '.join(bids_ids)}>"
417-
)
418-
elif bids_ids:
407+
b0_ids = [
408+
listify(f.metadata.get("B0FieldIdentifier"))
409+
for f in self.sources
410+
if f.metadata.get("B0FieldIdentifier")
411+
]
412+
413+
if b0_ids:
414+
# Find common IDs
415+
bids_ids = set(b0_ids[0]).intersection(*b0_ids[1:])
416+
if not bids_ids:
417+
raise ValueError(
418+
f"No common ``B0FieldIdentifier`` found: <{', '.join(map(str, b0_ids))}>"
419+
)
420+
elif len(bids_ids) > 1:
421+
raise ValueError(
422+
f"Multiple common ``B0FieldIdentifier``s found: <{', '.join(bids_ids)}>"
423+
)
419424
object.__setattr__(self, "bids_id", bids_ids.pop())
420-
else:
421-
bids_id = _estimators.add(self.paths())
422-
object.__setattr__(self, "bids_id", bids_id)
423-
for intent_file in intents_meta:
424-
_intents[intent_file].add(bids_id)
425-
return
426-
427-
_estimators[self.bids_id] = self.paths()
425+
426+
if self.bids_id:
427+
_estimators[self.bids_id] = self.paths()
428+
else:
429+
bids_id = _estimators.add(self.paths())
430+
object.__setattr__(self, "bids_id", bids_id)
431+
428432
for intent_file in intents_meta:
429433
_intents[intent_file].add(self.bids_id)
430434

sdcflows/utils/wrangler.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#
2323
"""Find fieldmaps on the BIDS inputs for :abbr:`SDC (susceptibility distortion correction)`."""
2424
import logging
25+
from functools import reduce
2526
from itertools import product
2627
from contextlib import suppress
2728
from pathlib import Path
@@ -293,7 +294,12 @@ def find_estimators(
293294
# Step 1. Use B0FieldIdentifier metadata
294295
b0_ids = tuple()
295296
with suppress(BIDSEntityError):
296-
b0_ids = layout.get_B0FieldIdentifiers(**base_entities)
297+
# flatten lists from json (tupled in pybids for hashing), then unique
298+
b0_ids = reduce(
299+
set.union,
300+
(listify(ids) for ids in layout.get_B0FieldIdentifiers(**base_entities)),
301+
set()
302+
)
297303

298304
if b0_ids:
299305
logger.debug(
@@ -305,9 +311,15 @@ def find_estimators(
305311
b0_entities = base_entities.copy()
306312
b0_entities["B0FieldIdentifier"] = b0_id
307313

314+
bare_ids = layout.get(**base_entities, B0FieldIdentifier=b0_id)
315+
listed_ids = layout.get(
316+
**base_entities,
317+
B0FieldIdentifier=f'"{b0_id}"', # Double quotes to match JSON, not Python repr
318+
regex_search=True,
319+
)
308320
e = fm.FieldmapEstimation([
309321
fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata())
310-
for fmap in layout.get(**b0_entities)
322+
for fmap in bare_ids + listed_ids
311323
])
312324
_log_debug_estimation(logger, e, layout.root)
313325
estimators.append(e)

0 commit comments

Comments
 (0)