Skip to content

collect_derivatives modifies queries in place #486

@tsalo

Description

@tsalo

Describe the bug
The collect_derivatives function is modifying its queries as it loops over spaces, with the first space listed being retained in later spaces' queries. For example, if I search for transforms with spaces ['a', 'b', 'c'], all three spaces' transform queries will use 'a'.

Stems from nipreps/fmriprep#3491 (comment).

Here is a script to reproduce the error. Note that the transforms in the output dictionary for fsnative and MNI152NLin6Asym spaces have 'from'/'to': 'MNI152NLin2009cAsym'.

import os
from pprint import pprint

from bids.layout import BIDSLayout
from niworkflows.utils.testing import generate_bids_skeleton
from smriprep.utils.bids import collect_derivatives

# Dictionary representation of dataset
skeleton_dict = {
    '01': [
        {
            'anat': [
                {
                    'desc': 'preproc',
                    'suffix': 'T1w'
                },
                {
                    'from': 'fsnative',
                    'to': 'T1w',
                    'mode': 'image',
                    'suffix': 'xfm',
                    'extension': '.txt'
                },
                {
                    'from': 'T1w',
                    'to': 'fsnative',
                    'mode': 'image',
                    'suffix': 'xfm',
                    'extension': '.txt'
                },
                {
                    'from': 'MNI152NLin2009cAsym',
                    'to': 'T1w',
                    'mode': 'image',
                    'suffix': 'xfm',
                    'extension': '.h5'
                },
                {
                    'from': 'T1w',
                    'to': 'MNI152NLin2009cAsym',
                    'mode': 'image',
                    'suffix': 'xfm',
                    'extension': '.h5'
                },
                {
                    'from': 'MNI152NLin6Asym',
                    'to': 'T1w',
                    'mode': 'image',
                    'suffix': 'xfm',
                    'extension': '.h5'
                },
                {
                    'from': 'T1w',
                    'to': 'MNI152NLin6Asym',
                    'mode': 'image',
                    'suffix': 'xfm',
                    'extension': '.h5'
                }
            ]
        }
    ]
}

bids_dir = os.path.abspath('smriprep_test_dset')
generate_bids_skeleton(str(bids_dir), skeleton_dict)
layout = BIDSLayout(str(bids_dir))

precomputed = collect_derivatives(
    bids_dir,
    '01',
    ['MNI152NLin2009cAsym', 'fsnative', 'MNI152NLin6Asym'],
    spec=None,
    patterns=None,
    session_id=None,
)
pprint(precomputed)

"""
{'t1w_preproc': '.../sub-01_desc-preproc_T1w.nii.gz',
 'transforms': {'MNI152NLin2009cAsym': {'forward': '.../sub-01_from-T1w_to-MNI152NLin2009cAsym_mode-image_xfm.h5',
                                        'reverse': '.../sub-01_from-MNI152NLin2009cAsym_to-T1w_mode-image_xfm.h5'},
                'MNI152NLin6Asym': {'forward': '.../sub-01_from-T1w_to-MNI152NLin2009cAsym_mode-image_xfm.h5',
                                    'reverse': '.../sub-01_from-MNI152NLin2009cAsym_to-T1w_mode-image_xfm.h5'},
                'fsnative': {'forward': '.../sub-01_from-T1w_to-MNI152NLin2009cAsym_mode-image_xfm.h5',
                             'reverse': '.../sub-01_from-MNI152NLin2009cAsym_to-T1w_mode-image_xfm.h5'}}}
"""

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions