Skip to content

Commit

Permalink
FIX: Normalize BIDS-URIs to subject-relative
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Sep 24, 2024
1 parent 08e198f commit 7c7de16
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
13 changes: 11 additions & 2 deletions sdcflows/utils/tests/test_wrangler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import pytest
import re
from pathlib import Path
from shutil import rmtree

import pytest

from niworkflows.utils.testing import generate_bids_skeleton

from sdcflows.utils.wrangler import find_estimators
from sdcflows.fieldmaps import clear_registry
from sdcflows.fieldmaps import clear_registry, get_identifier


def gen_layout(bids_dir, database_dir=None):
Expand Down Expand Up @@ -339,6 +342,12 @@ def test_wrangler_URIs(tmpdir, name, skeleton, session, estimations, total_estim
sessions=[session] if session else None,
)
assert len(est) == estimations or total_estimations
if session:
bold = layout.get(session=session, suffix="bold", extension=".nii.gz")[0]
intended_rel = re.sub(r'^sub-[a-zA-Z0-9]*/', '', str(Path(bold).relative_to(layout.root)))
b0_id = get_identifier(intended_rel)
assert b0_id == ('auto_00000',)

clear_registry()


Expand Down
33 changes: 31 additions & 2 deletions sdcflows/utils/wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@
from .. import fieldmaps as fm


def _normalize_intent(
intent: str,
layout: BIDSLayout,
subject: str
) -> str | None:
"""Convert BIDS-URI intent to subject-relative intent
SDCFlows currently makes strong assumptions about old-style intents,
and a change to that needs to be carefully considered and tested.
"""
if intent.startswith("bids::"):
# bids::sub-<subject>/
# ^- 10 ^- 11
return intent[11 + len(subject):]
return intent


def _resolve_intent(
intent: str,
layout: BIDSLayout,
Expand All @@ -47,6 +64,18 @@ def _resolve_intent(
return intent


def _filter_metadata(
metadata: Dict[str, Any],
layout: BIDSLayout,
subject: str
) -> Dict[str, Any]:
intents = metadata.get("IntendedFor")
if intents:
updated = [_normalize_intent(intent, layout, subject) for intent in listify(intents)]
return {**metadata, "IntendedFor": updated}
return metadata


def find_estimators(
*,
layout: BIDSLayout,
Expand Down Expand Up @@ -377,7 +406,7 @@ def find_estimators(
):
try:
e = fm.FieldmapEstimation(
fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata())
fm.FieldmapFile(fmap.path, metadata=_filter_metadata(fmap.get_metadata(), layout, subject))
)
except (ValueError, TypeError) as err:
_log_debug_estimator_fail(
Expand Down Expand Up @@ -405,7 +434,7 @@ def find_estimators(
if len(dirs) > 1:
by_intent = {}
for fmap in layout.get(**{**entities, **{'direction': dirs}}):
fmapfile = fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata())
fmapfile = fm.FieldmapFile(fmap.path, metadata=_filter_metadata(fmap.get_metadata(), layout, subject))
by_intent.setdefault(
tuple(fmapfile.metadata.get('IntendedFor', ())), []
).append(fmapfile)
Expand Down

0 comments on commit 7c7de16

Please sign in to comment.