Skip to content

FIX: Repair search for precomputed transforms #3369

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

Merged
merged 15 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fmriprep/data/io_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
},
"boldref2anat": {
"datatype": "func",
"from": "orig",
"from": "boldref",
"to": "anat",
"mode": "image",
"suffix": "xfm",
"extension": ".txt"
},
"boldref2fmap": {
"datatype": "func",
"from": "orig",
"from": "boldref",
"mode": "image",
"suffix": "xfm",
"extension": ".txt"
Expand Down
17 changes: 12 additions & 5 deletions fmriprep/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,21 @@ def collect_derivatives(
continue
derivs_cache[f'{k}_boldref'] = item[0] if len(item) == 1 else item

transforms_cache = {}
for xfm, q in spec['transforms'].items():
query = {**q, **entities}
if xfm == 'boldref2fmap':
query['to'] = fieldmap_id
item = layout.get(return_type='filename', **q)
# Transform extension will often not match provided entities
# (e.g., ".nii.gz" vs ".txt").
# And transform suffixes will be "xfm",
# whereas relevant src file will be "bold".
query = {**entities, **q}
if xfm == 'boldref2fmap' and fieldmap_id:
# fieldmaps have ids like auto_00000
query['to'] = fieldmap_id.replace('_', '')
item = layout.get(return_type='filename', **query)
if not item:
continue
derivs_cache[xfm] = item[0] if len(item) == 1 else item
transforms_cache[xfm] = item[0] if len(item) == 1 else item
derivs_cache['transforms'] = transforms_cache
return derivs_cache


Expand Down
36 changes: 36 additions & 0 deletions fmriprep/utils/tests/test_derivative_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pathlib import Path

import pytest

from fmriprep.utils import bids


@pytest.mark.parametrize('xfm', ['boldref2fmap', 'boldref2anat', 'hmc'])
def test_transforms_found_as_str(tmp_path: Path, xfm: str):
subject = '0'
task = 'rest'
fromto = {
'hmc': 'from-orig_to-boldref',
'boldref2fmap': 'from-boldref_to-auto00000',
'boldref2anat': 'from-boldref_to-anat',
}[xfm]

to_find = tmp_path.joinpath(
f'sub-{subject}', 'func', f'sub-{subject}_task-{task}_{fromto}_mode-image_xfm.txt'
)
to_find.parent.mkdir(parents=True)
to_find.touch()

entities = {
'subject': subject,
'task': task,
'suffix': 'bold',
'extension': '.nii.gz',
}

derivs = bids.collect_derivatives(
derivatives_dir=tmp_path,
entities=entities,
fieldmap_id='auto_00000',
)
assert derivs == {'transforms': {xfm: str(to_find)}}
Loading