Skip to content

Commit

Permalink
Repair search for precomputed transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
psadil committed Oct 2, 2024
1 parent f991740 commit dd198ad
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
22 changes: 18 additions & 4 deletions fmriprep/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,28 @@ 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}
# 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']
},
}
if xfm == 'boldref2fmap':
query['to'] = fieldmap_id
item = layout.get(return_type='filename', **q)
# 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
44 changes: 44 additions & 0 deletions fmriprep/utils/tests/test_derivative_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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)
)
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'
else:
to_find = f'sub-{entities['subject']}_task-{entities['task']} \
_from-{spec['from']}_to-{spec['to']}_mode-image_xfm.txt'

funcd = tmp_path / f'sub-{entities['subject']}' / 'func'
funcd.mkdir(parents=True)
(funcd / to_find).touch()

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))

0 comments on commit dd198ad

Please sign in to comment.