Skip to content

Commit

Permalink
Merge pull request #458 from effigies/fix/bids-uris-nonepi
Browse files Browse the repository at this point in the history
FIX: Normalize BIDS-URIs to subject-relative
  • Loading branch information
effigies authored Oct 4, 2024
2 parents 3f4bbf7 + f9ac03c commit 656a51d
Show file tree
Hide file tree
Showing 2 changed files with 46 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
37 changes: 35 additions & 2 deletions sdcflows/utils/wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
from .. import fieldmaps as fm


def _normalize_intent(
intent: str,
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 +63,17 @@ def _resolve_intent(
return intent


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


def find_estimators(
*,
layout: BIDSLayout,
Expand Down Expand Up @@ -377,7 +404,10 @@ 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(), subject),
)
)
except (ValueError, TypeError) as err:
_log_debug_estimator_fail(
Expand Down Expand Up @@ -405,7 +435,10 @@ 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(), subject),
)
by_intent.setdefault(
tuple(fmapfile.metadata.get('IntendedFor', ())), []
).append(fmapfile)
Expand Down

0 comments on commit 656a51d

Please sign in to comment.