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

Test field map collection #927

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
106 changes: 106 additions & 0 deletions qsiprep/tests/test_utils_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,74 @@
],
}

dset_fmap_intendedfor_relpath = {
'01': [
{
'fmap': [
{
'dir': 'PA',
'suffix': 'epi',
'metadata': {
'IntendedFor': ['dwi/sub-01_dir-AP_dwi.nii.gz'],
},
},
],
'dwi': [
{
'dir': 'AP',
'suffix': 'dwi',
},
],
},
],
}
dset_fmap_intendedfor_bidsuri = {
'01': [
{
'fmap': [
{
'dir': 'PA',
'suffix': 'epi',
'metadata': {
'IntendedFor': ['bids::sub-01/dwi/sub-01_dir-AP_dwi.nii.gz'],
},
},
],
'dwi': [
{
'dir': 'AP',
'suffix': 'dwi',
},
],
},
],
}
dset_fmap_b0fields = {
'01': [
{
'fmap': [
{
'dir': 'PA',
'suffix': 'epi',
'metadata': {
'B0FieldIdentifier': 'pepolar',
},
},
],
'dwi': [
{
'dir': 'AP',
'suffix': 'dwi',
'metadata': {
'B0FieldIdentifier': 'pepolar',
'B0FieldSource': 'pepolar',
},
},
],
},
],
}


def test_get_entity_groups_with_multipartid(tmpdir):
"""Test the get_entity_groups function."""
Expand Down Expand Up @@ -117,6 +185,44 @@ def test_get_entity_groups_without_multipartid(tmpdir):
check_expected(entity_groups, expected)


def test_get_fieldmaps(tmp_path_factory):
"""Test the get_fieldmaps function."""
base_dir = tmp_path_factory.mktemp('test_get_fieldmaps')

# Check that relative paths are correctly handled
bids_dir = base_dir / 'dset_fmap_intendedfor_relpath'
generate_bids_skeleton(str(bids_dir), dset_fmap_intendedfor_relpath)
layout = BIDSLayout(str(bids_dir))
dwi_file = layout.get(suffix='dwi', extension='nii.gz', return_type='filename')[0]
fieldmaps = layout.get_fieldmap(dwi_file, return_list=True)
assert len(fieldmaps) == 1
assert fieldmaps[0]['suffix'] == 'epi'
assert layout.get_file(fieldmaps[0]['epi']).get_metadata()['IntendedFor'] == [
'dwi/sub-01_dir-AP_dwi.nii.gz'
]

# Check that BIDS URI paths are correctly handled
bids_dir = base_dir / 'dset_fmap_intendedfor_bidsuri'
generate_bids_skeleton(str(bids_dir), dset_fmap_intendedfor_bidsuri)
layout = BIDSLayout(str(bids_dir))
dwi_file = layout.get(suffix='dwi', extension='nii.gz', return_type='filename')[0]
fieldmaps = layout.get_fieldmap(dwi_file, return_list=True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BIDSLayout.get_fieldmap doesn't work on BIDS URIs, so we need to either work around it like SDCFlows does or contribute to PyBIDS to make it work.

assert len(fieldmaps) == 1
assert fieldmaps[0]['suffix'] == 'epi'
assert layout.get_file(fieldmaps[0]['epi']).get_metadata()['IntendedFor'] == [
'bids::sub-01/dwi/sub-01_dir-AP_dwi.nii.gz'
]

# Check that B0FieldIdentifier and B0FieldSource are correctly handled
bids_dir = base_dir / 'dset_fmap_b0fields'
generate_bids_skeleton(str(bids_dir), dset_fmap_b0fields)
layout = BIDSLayout(str(bids_dir))
dwi_file = layout.get(suffix='dwi', extension='nii.gz', return_type='filename')[0]
fieldmaps = layout.get_fieldmap(dwi_file, return_list=True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't expect this to work. We probably need custom code like SDCFlows uses.

assert len(fieldmaps) == 1
assert fieldmaps[0]['suffix'] == 'epi'


def check_expected(subject_data, expected):
"""Check expected values."""
if isinstance(expected, str):
Expand Down