Skip to content

Commit

Permalink
FIX: Repair search for precomputed transforms (#3369)
Browse files Browse the repository at this point in the history
  • Loading branch information
psadil authored and effigies committed Oct 2, 2024
1 parent d7bf47b commit ce31460
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
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)}}

0 comments on commit ce31460

Please sign in to comment.