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

FIX: Repair search for precomputed transforms #3369

Merged
merged 15 commits into from
Oct 2, 2024
2 changes: 1 addition & 1 deletion fmriprep/data/io_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"boldref2fmap": {
"datatype": "func",
"from": "orig",
"from": "boldref",
psadil marked this conversation as resolved.
Show resolved Hide resolved
"mode": "image",
"suffix": "xfm",
"extension": ".txt"
Expand Down
20 changes: 15 additions & 5 deletions fmriprep/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,24 @@ 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 = {
**q,
**{k: v for k, v in entities.items() if k not in ['suffix', 'extension']},
}
psadil marked this conversation as resolved.
Show resolved Hide resolved
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
41 changes: 41 additions & 0 deletions fmriprep/utils/tests/test_derivative_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
from pathlib import Path

import pytest

from fmriprep.data import load as load_data
from fmriprep.utils import bids


@pytest.mark.parametrize('xfm', ['boldref2fmap', 'boldref2anat', 'hmc'])
def test_transforms_found_as_str(tmp_path: Path, xfm: str):
spec = (
json.loads(load_data.readable('io_spec.json').read_text())
.get('queries')
.get('transforms')
.get(xfm)
)
psadil marked this conversation as resolved.
Show resolved Hide resolved
entities = {
'subject': '0',
'task': 'rest',
'suffix': 'bold',
'extension': '.nii.gz',
}
if xfm == 'boldref2fmap':
to_find = f'sub-{entities["subject"]}_task-{entities["task"]}_from-{spec["from"]}_to-auto00000_mode-image_xfm.txt' # noqa: E501
else:
to_find = f'sub-{entities["subject"]}_task-{entities["task"]}_from-{spec["from"]}_to-{spec["to"]}_mode-image_xfm.txt' # noqa: E501

funcd = tmp_path / f'sub-{entities["subject"]}' / 'func'
funcd.mkdir(parents=True)
(funcd / to_find).touch()
psadil marked this conversation as resolved.
Show resolved Hide resolved

derivs = bids.collect_derivatives(
derivatives_dir=tmp_path,
entities=entities,
fieldmap_id='auto_00000',
)
transforms_in_derivs = 'transforms' in derivs
xfm_in_transforms = xfm in derivs.get('transforms')
transform_is_str = isinstance(derivs.get('transforms').get(xfm), str)
assert all((transforms_in_derivs, xfm_in_transforms, transform_is_str))
psadil marked this conversation as resolved.
Show resolved Hide resolved