Skip to content

Commit ce31460

Browse files
psadileffigies
authored andcommitted
FIX: Repair search for precomputed transforms (#3369)
1 parent d7bf47b commit ce31460

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

fmriprep/data/io_spec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
},
3434
"boldref2anat": {
3535
"datatype": "func",
36-
"from": "orig",
36+
"from": "boldref",
3737
"to": "anat",
3838
"mode": "image",
3939
"suffix": "xfm",
4040
"extension": ".txt"
4141
},
4242
"boldref2fmap": {
4343
"datatype": "func",
44-
"from": "orig",
44+
"from": "boldref",
4545
"mode": "image",
4646
"suffix": "xfm",
4747
"extension": ".txt"

fmriprep/utils/bids.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,21 @@ def collect_derivatives(
6868
continue
6969
derivs_cache[f'{k}_boldref'] = item[0] if len(item) == 1 else item
7070

71+
transforms_cache = {}
7172
for xfm, q in spec['transforms'].items():
72-
query = {**q, **entities}
73-
if xfm == 'boldref2fmap':
74-
query['to'] = fieldmap_id
75-
item = layout.get(return_type='filename', **q)
73+
# Transform extension will often not match provided entities
74+
# (e.g., ".nii.gz" vs ".txt").
75+
# And transform suffixes will be "xfm",
76+
# whereas relevant src file will be "bold".
77+
query = {**entities, **q}
78+
if xfm == 'boldref2fmap' and fieldmap_id:
79+
# fieldmaps have ids like auto_00000
80+
query['to'] = fieldmap_id.replace('_', '')
81+
item = layout.get(return_type='filename', **query)
7682
if not item:
7783
continue
78-
derivs_cache[xfm] = item[0] if len(item) == 1 else item
84+
transforms_cache[xfm] = item[0] if len(item) == 1 else item
85+
derivs_cache['transforms'] = transforms_cache
7986
return derivs_cache
8087

8188

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from fmriprep.utils import bids
6+
7+
8+
@pytest.mark.parametrize('xfm', ['boldref2fmap', 'boldref2anat', 'hmc'])
9+
def test_transforms_found_as_str(tmp_path: Path, xfm: str):
10+
subject = '0'
11+
task = 'rest'
12+
fromto = {
13+
'hmc': 'from-orig_to-boldref',
14+
'boldref2fmap': 'from-boldref_to-auto00000',
15+
'boldref2anat': 'from-boldref_to-anat',
16+
}[xfm]
17+
18+
to_find = tmp_path.joinpath(
19+
f'sub-{subject}', 'func', f'sub-{subject}_task-{task}_{fromto}_mode-image_xfm.txt'
20+
)
21+
to_find.parent.mkdir(parents=True)
22+
to_find.touch()
23+
24+
entities = {
25+
'subject': subject,
26+
'task': task,
27+
'suffix': 'bold',
28+
'extension': '.nii.gz',
29+
}
30+
31+
derivs = bids.collect_derivatives(
32+
derivatives_dir=tmp_path,
33+
entities=entities,
34+
fieldmap_id='auto_00000',
35+
)
36+
assert derivs == {'transforms': {xfm: str(to_find)}}

0 commit comments

Comments
 (0)