qsiprep.utils.grouping module
Utilities to group scans based on their acquisition parameters
Download many variations of fieldmaps and dwi data
Examples
Set up tests >>> import os >>> from qsiprep.utils.testing import get_grouping_test_data >>> data_root = get_grouping_test_data() >>> os.chdir(data_root)
- qsiprep.utils.grouping.find_fieldmaps_from_other_dwis(dwi_files, dwi_file_metadatas)[source]
Find a list of files in the dwi/ directory that can be used for distortion correction.
It is common to acquire DWI scans with opposite phase encoding directions so they can be used to correct each other’s EPI distortion. There is currently no mechanism in BIDS to specify whether b=0 scans in dwi/ can be used as fieldmaps for one another.
- Parameters:
- Returns:
dwi_series_fieldmaps – A dictionary where the keys are the full paths to dwi files and the values are dictionaries describing the fieldmap. If no fieldmap is found, the dictionary will be empty.
- Return type:
Examples
A single scan with no opportunities to SDC with a DWI scan >>> from qsiprep.utils.grouping import find_fieldmaps_from_other_dwis >>> single_dwi_file = [“/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz”] >>> single_dwi_file_metadatas = [{“PhaseEncodingDirection”: “j”}] >>> find_fieldmaps_from_other_dwis(single_dwi_file, single_dwi_file_metadatas) {‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’: {}}
Two scans with the same PE direction: again no opportunities to SDC >>> repeat_dwi_files = [“/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz”] >>> repeat_dwi_file_metadatas = [{“PhaseEncodingDirection”: “j”}, … {“PhaseEncodingDirection”: “j”}] >>> find_fieldmaps_from_other_dwis(repeat_dwi_files, … repeat_dwi_file_metadatas) # doctest: +NORMALIZE_WHITESPACE {‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’: {},
‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’: {}}
Paired scans, each in opposite PE directions >>> paired_dwi_files = [ … “/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz”] >>> paired_dwi_file_metadatas = [ … {“PhaseEncodingDirection”: “j”}, … {“PhaseEncodingDirection”: “j-“}] >>> find_fieldmaps_from_other_dwis(paired_dwi_files, … paired_dwi_file_metadatas) # doctest: +NORMALIZE_WHITESPACE {‘/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’: {‘suffix’: ‘dwi’,
‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’]},
- ‘/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’: {‘suffix’: ‘dwi’,
‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’]}}
Multiple scans in multiple PE directions >>> multi_dwi_files = [ … “/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz”] >>> multi_dwi_file_metadatas = [ … {“PhaseEncodingDirection”: “j”}, … {“PhaseEncodingDirection”: “j”}, … {“PhaseEncodingDirection”: “j”}, … {“PhaseEncodingDirection”: “j-“}, … {“PhaseEncodingDirection”: “j-“}] >>> find_fieldmaps_from_other_dwis(multi_dwi_files, … multi_dwi_file_metadatas) # doctest: +NORMALIZE_WHITESPACE {‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’: {‘suffix’: ‘dwi’,
- ‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
- ‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’: {‘suffix’: ‘dwi’,
- ‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
- ‘/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz’: {‘suffix’: ‘dwi’,
- ‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
- ‘/data/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’: {‘suffix’: ‘dwi’,
- ‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’, ‘/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz’]},
- ‘/data/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’: {‘suffix’: ‘dwi’,
- ‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’, ‘/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz’]}}
No information available >>> empty_dwi_files = [ … “/data/sub-1/dwi/sub-1_run-1_dwi.nii.gz”, … “/data/sub-1/dwi/sub-1_run-2_dwi.nii.gz”] >>> empty_dwi_file_metadatas = [ … {}, … {}] >>> find_fieldmaps_from_other_dwis(empty_dwi_files, … empty_dwi_file_metadatas) # doctest: +NORMALIZE_WHITESPACE {‘/data/sub-1/dwi/sub-1_run-1_dwi.nii.gz’: {},
‘/data/sub-1/dwi/sub-1_run-2_dwi.nii.gz’: {}}
- qsiprep.utils.grouping.get_concatenated_bids_name(dwi_group)[source]
Derive the output file name for a group of dwi files.
Strip away non-shared key/values from the input list of files. This function assumes you have already split the dwi group into something meaningful and really want to combine all the inputs.
- Parameters:
dwi_group (
list
ofstr
) – A list of full paths to dwi nifti files in a BIDS tree.- Returns:
fname – The BIDS name of the concatenated dwi series.
- Return type:
Examples
>>> get_concatenated_bids_name([ ... '/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_dir-AP_run-4_dwi.nii.gz' ... ]) 'sub-1_dir-AP'
>>> get_concatenated_bids_name([ ... '/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz' ... ]) 'sub-1'
>>> get_concatenated_bids_name([ ... '/data/sub-1/dwi/sub-1_acq-HCP-dir-AP_run-1_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_acq-HCP_dir-AP_run-2_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_acq-HCP_dir-PA_run-1_dwi.nii.gz', ... '/data/sub-1/dwi/sub-1_acq-HCP_dir-PA_run-2_dwi.nii.gz' ... ]) 'sub-1_acq-HCP'
- qsiprep.utils.grouping.get_highest_priority_fieldmap(fmap_infos)[source]
Return a dictionary describing the highest priority fieldmap.
- Parameters:
fmap_infos (
list
ofdict
) – A list of dictionaries describing fieldmaps. Each dictionary must have asuffix
key and may have anepi
key.- Returns:
selected_fmap_info – The dictionary describing the highest priority fieldmap. This will be the entry from
fmap_infos
with the highest priority value. If no fieldmaps are found, the dictionary will have asuffix
key with a value ofNone
.- Return type:
Examples
Invent some potential fieldmaps >>> epi_fmap1 = {“epi”: “/data/sub-1/fmap/sub-1_dir-AP_run-1_epi.nii.gz”, “suffix”: “epi”} >>> epi_fmap2 = {“epi”: “/data/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz”, “suffix”: “epi”} >>> epi_fmap3 = {“epi”: “/data/sub-1/fmap/sub-1_dir-PA_epi.nii.gz”, “suffix”: “epi”} >>> >>> phasediff_fmap = {“phasediff”: “/data/sub-1/fmap/sub-1_phasediff.nii.gz”, … “suffix”: “phasediff”} >>> phases_fmap = {“phase1”: “/data/sub-1/fmap/sub-1_phase1.nii.gz”, … “suffix”: “phase1”} >>> >>> dwi_fmap1 = {“dwi”: “/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz”, “suffix”: “dwi”} >>> dwi_fmap2 = {‘suffix’: ‘dwi’, … ‘dwi’: [‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz’]}
When there are no fieldmaps in
fmaps/
, but a reverse PE DWI series >>> get_highest_priority_fieldmap([dwi_fmap1]) {‘dwi’: ‘/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’, ‘suffix’: ‘dwi’}There is both an epi fieldmap and a phase1/phase2 GRE fieldmap >>> get_highest_priority_fieldmap([epi_fmap1, phases_fmap]) {‘suffix’: ‘epi’, ‘epi’: [‘/data/sub-1/fmap/sub-1_dir-AP_run-1_epi.nii.gz’]}
Multiple EPI fieldmaps >>> get_highest_priority_fieldmap( … [epi_fmap1, epi_fmap2, epi_fmap3]) # doctest: +NORMALIZE_WHITESPACE {‘suffix’: ‘epi’,
- ‘epi’: [‘/data/sub-1/fmap/sub-1_dir-AP_run-1_epi.nii.gz’,
‘/data/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’, ‘/data/sub-1/fmap/sub-1_dir-PA_epi.nii.gz’]}
An EPI fieldmap from
fmap/
should be chosen over a reverse PE DWI series >>> get_highest_priority_fieldmap([epi_fmap1, dwi_fmap2]) {‘suffix’: ‘epi’, ‘epi’: [‘/data/sub-1/fmap/sub-1_dir-AP_run-1_epi.nii.gz’]}
- qsiprep.utils.grouping.get_session_groups(layout, subject_data, combine_all_dwis)[source]
Handle the grouping of multiple dwi files within a session.
- Parameters:
- Returns:
dwi_session_groups – A list of lists of dwi files. Each list of dwi files is a group of scans that can be concatenated together.
- Return type:
- qsiprep.utils.grouping.group_by_warpspace(dwi_files, layout, ignore_fieldmaps)[source]
Groups a session’s DWI files by their acquisition parameters.
DWIs are grouped by their warped space. Two DWI series that are listed in the IntendedFor field of a fieldmap are assumed to have the same susceptibility distortions and therefore be in the same warped space. The goal of this function is to combine DWI series into groups of acquisitions that are in the same warped space into a list of scans that can be combined after unwarping.
- Parameters:
- Returns:
dwi_groups – A list of dictionaries describing each group of dwi files. Each dictionary has the following keys:
dwi_series
: A list of full paths to dwi nifti files in a BIDS tree.fieldmap_info
: A dictionary describing the fieldmap.If no fieldmap is found, the dictionary will be empty.
dwi_series_pedir
: The phase encoding direction of the dwi series.If no information is available, the value will be an empty string.
concatenated_bids_name
: The BIDS name of the concatenated dwi series.If no information is available, the value will be an empty string.
- Return type:
Examples
Set up tests >>> from qsiprep.utils.bids import collect_data >>> SUBJECT_ID = “1”
No fieldmap data, a single DWI series >>> subject_data, layout = collect_data(“easy”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…sub-1_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1’}]
Two DWIs with the same PE direction, to be concatenated >>> subject_data, layout = collect_data(“concat1”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/concat1/sub-1/dwi/sub-1_run-01_dwi.nii.gz’,
‘…/concat1/sub-1/dwi/sub-1_run-02_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1’}]
Two DWI series intended to SDC each other >>> subject_data, layout = collect_data(“opposite”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
‘dwi’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’},
- {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
‘dwi’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA’}]
Multiple DWI series in two different PE directions >>> subject_data, layout = collect_data(“opposite_concat”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
- ‘dwi’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’},
- {‘dwi_series’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
- ‘dwi’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA’}]
A phasediff fieldmap defines the warped group >>> subject_data, layout = collect_data(“phasediff”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/phasediff/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/phasediff/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘phasediff’: ‘…/phasediff/sub-1/fmap/sub-1_phasediff.nii.gz’,
‘magnitude1’: ‘…/magnitude1/sub-1/fmap/sub-1_magnitude1.nii.gz’, ‘suffix’: ‘phasediff’},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’}]
Two DWI series, each with its own fieldmap/warped space >>> subject_data, layout = collect_data(“separate_fmaps”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/separate_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘epi’,
‘epi’: [’…/separate_fmaps/sub-1/fmap/sub-1_dir-PA_run-1_epi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’},
- {‘dwi_series’: [’…/separate_fmaps/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘epi’,
‘epi’: [’…/separate_fmaps/sub-1/fmap/sub-1_dir-PA_run-2_epi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-2’}]
Same as above but ignoring fieldmaps. Data gets concatenated >>> subject_data, layout = collect_data(“separate_fmaps”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, True) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/separate_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/separate_fmaps/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’}]
Two DWI series, opposite PE directions, dedicated EPI fieldmap for each >>> subject_data, layout = collect_data(“mixed_fmaps”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘epi’,
‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-PA_run-1_epi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’},
- {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘epi’,
‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’]}, ‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA_run-2’}]
Same as last one, but ignore fieldmaps. The DWI series will be used for SDC instead >>> subject_data, layout = collect_data(“mixed_fmaps”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, True) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
‘dwi’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’},
- {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
‘dwi’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA_run-2’}]
There is no metadata related to epi distortion: don’t concatenate anything >>> subject_data, layout = collect_data(“missing_info”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/missing_info/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘’, ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’},
- {‘dwi_series’: [’…/missing_info/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘’, ‘concatenated_bids_name’: ‘sub-1_dir-PA_run-2’}]
A bizarre mix of PE directions and some missing data >>> subject_data, layout = collect_data(“wtf”, SUBJECT_ID) >>> group_by_warpspace( … subject_data[‘dwi’], layout, False) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS [{‘dwi_series’: [’…/wtf/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/wtf/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
- ‘dwi’: [’…/wtf/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘…/wtf/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’},
- {‘dwi_series’: [’…/wtf/sub-1/dwi/sub-1_dir-IS_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘k-‘, ‘concatenated_bids_name’: ‘sub-1_dir-IS’},
- {‘dwi_series’: [’…/wtf/sub-1/dwi/sub-1_run-1_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘’, ‘concatenated_bids_name’: ‘sub-1_run-1’},
- {‘dwi_series’: [’…/wtf/sub-1/dwi/sub-1_run-2_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘’, ‘concatenated_bids_name’: ‘sub-1_run-2’},
- {‘dwi_series’: [’…/wtf/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘…/wtf/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘suffix’: ‘dwi’,
- ‘dwi’: [’…/wtf/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/wtf/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’]},
‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA’}]
- qsiprep.utils.grouping.group_dwi_scans(subject_data, using_fsl=False, combine_scans=True, ignore_fieldmaps=False, concatenate_distortion_groups=False)[source]
Determine which scans can be concatenated based on their acquisition parameters.
- Parameters:
- Returns:
scan_groups – A dict where the keys are the BIDS derivatives name of the output file after concatenation. The values are lists of dwi files in that group.
- Return type:
- qsiprep.utils.grouping.group_for_concatenation(all_dwi_fmap_groups)[source]
Find matched pairs of phase encoding directions that can be combined after SHORELine.
Any groups that don’t have a phase encoding direction won’t be correctable by SHORELine.
- qsiprep.utils.grouping.group_for_eddy(all_dwi_fmap_groups)[source]
Find matched pairs of phase encoding directions that can be combined for TOPUP/eddy.
Any groups that don’t have a phase encoding direction won’t be correctable by eddy/TOPUP.
- Parameters:
all_dwi_fmap_groups (
list
ofdict
) – A list of dictionaries describing each group of dwi files.- Returns:
eddy_groups – A list of dictionaries describing each group of dwi files. Each dictionary has the following keys:
dwi_series
: A list of full paths to dwi nifti files in a BIDS tree.fieldmap_info
: A dictionary describing the fieldmap.If no fieldmap is found, the dictionary will be empty.
dwi_series_pedir
: The phase encoding direction of the dwi series.If no information is available, the value will be an empty string.
concatenated_bids_name
: The BIDS name of the concatenated dwi series.If no information is available, the value will be an empty string.
- Return type:
Examples
Paired DWI series to correct each other: >>> dwi_groups = [ … {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’}, … {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j-‘, … ‘concatenated_bids_name’: ‘sub-1_dir-PA_run-2’}] >>> group_for_eddy(dwi_groups) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
‘dwi_series_pedir’: ‘j’, ‘fieldmap_info’: {‘suffix’: ‘rpe_series’,
‘rpe_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
‘concatenated_bids_name’: ‘sub-1’}]
AP/PA EPI fieldmaps >>> dwi_groups = [ … {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘epi’, … ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-PA_run-1_epi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’}, … {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘epi’, … ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j-‘, … ‘concatenated_bids_name’: ‘sub-1_dir-PA_run-2’}] >>> group_for_eddy(dwi_groups) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
‘dwi_series_pedir’: ‘j’, ‘fieldmap_info’: {‘suffix’: ‘rpe_series’,
‘rpe_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’], ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’,
‘…/mixed_fmaps/sub-1/fmap/sub-1_dir-PA_run-1_epi.nii.gz’]},
‘concatenated_bids_name’: ‘sub-1’}]
Repeated scans per PE direction >>> dwi_groups = [ … {‘dwi_series’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’, … ‘…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’, … ‘…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP’}, … {‘dwi_series’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’, … ‘…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’, … ‘…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j-‘, … ‘concatenated_bids_name’: ‘sub-1_dir-PA’}] >>> group_for_eddy(dwi_groups) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/opposite_concat/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
‘dwi_series_pedir’: ‘j’, ‘fieldmap_info’: {‘suffix’: ‘rpe_series’,
- ‘rpe_series’: [’…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-1_dwi.nii.gz’,
‘…/opposite_concat/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’]},
‘concatenated_bids_name’: ‘sub-1’}]
A phasediff fieldmap (Not used by eddy) >>> dwi_groups = [ … {‘dwi_series’: [’…/phasediff/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’, … ‘…/phasediff/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’], … ‘fieldmap_info’: {‘phasediff’: ‘…/phasediff/sub-1/fmap/sub-1_phasediff.nii.gz’, … ‘magnitude1’: ‘…/magnitude1/sub-1/fmap/sub-1_magnitude1.nii.gz’, … ‘suffix’: ‘phasediff’}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP’}] >>> group_for_eddy(dwi_groups) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [’…/phasediff/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘…/phasediff/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’],
- ‘fieldmap_info’: {‘phasediff’: ‘…/phasediff/sub-1/fmap/sub-1_phasediff.nii.gz’,
‘magnitude1’: ‘…/magnitude1/sub-1/fmap/sub-1_magnitude1.nii.gz’, ‘suffix’: ‘phasediff’},
‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’}]
- qsiprep.utils.grouping.merge_dwi_groups(dwi_groups_plus, dwi_groups_minus)[source]
Convert two dwi groups into a single group that will be concatenated for FSL.
- Parameters:
dwi_groups_plus (
list
ofdict
) – A list of dictionaries describing each group of dwi files. Each dictionary has the following keys:dwi_series
: A list of full paths to dwi nifti files in a BIDS tree.fieldmap_info
: A dictionary describing the fieldmap.If no fieldmap is found, the dictionary will be empty.
dwi_series_pedir
: The phase encoding direction of the dwi series.If no information is available, the value will be an empty string.
concatenated_bids_name
: The BIDS name of the concatenated dwi series.If no information is available, the value will be an empty string.
dwi_groups_minus (
list
ofdict
) – A list of dictionaries describing each group of dwi files. Each dictionary has the same keys asdwi_groups_plus
.
- Returns:
merged_group – A dictionary describing the merged group of dwi files. The dictionary has the same keys as
dwi_groups_plus
.- Return type:
Examples
Set up tests >>> SUBJECT_ID = “1”
AP/PA fieldmaps and paired DWI series >>> plus_groups = [ … {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘epi’, … ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-PA_run-1_epi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP_run-1’}] >>> minus_groups = [ … {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘epi’, … ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j-‘, … ‘concatenated_bids_name’: ‘sub-1_dir-PA_run-2’}] >>> merge_dwi_groups(plus_groups, minus_groups) # doctest: +NORMALIZE_WHITESPACE {‘dwi_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’],
‘dwi_series_pedir’: ‘j’, ‘fieldmap_info’: {‘suffix’: ‘rpe_series’,
‘rpe_series’: [’…/mixed_fmaps/sub-1/dwi/sub-1_dir-PA_run-2_dwi.nii.gz’], ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’,
‘…/mixed_fmaps/sub-1/fmap/sub-1_dir-PA_run-1_epi.nii.gz’]},
‘concatenated_bids_name’: ‘sub-1’}
Two series SDC each other >>> plus_groups = [ … {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP’}] >>> minus_groups = [ … {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j-‘, … ‘concatenated_bids_name’: ‘sub-1_dir-PA’}] >>> merge_dwi_groups(plus_groups, minus_groups) # doctest: +NORMALIZE_WHITESPACE {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’],
‘dwi_series_pedir’: ‘j’, ‘fieldmap_info’: {‘suffix’: ‘rpe_series’,
‘rpe_series’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’]},
‘concatenated_bids_name’: ‘sub-1’}
An odd case: one has an EPI >>> plus_groups = [ … {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘dwi’, … ‘dwi’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j’, … ‘concatenated_bids_name’: ‘sub-1_dir-AP’}] >>> minus_groups = [ … {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’], … ‘fieldmap_info’: {‘suffix’: ‘epi’, … ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’]}, … ‘dwi_series_pedir’: ‘j-‘, … ‘concatenated_bids_name’: ‘sub-1_dir-PA’}] >>> merge_dwi_groups(plus_groups, minus_groups) # doctest: +NORMALIZE_WHITESPACE {‘dwi_series’: [’…/opposite/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’],
‘dwi_series_pedir’: ‘j’, ‘fieldmap_info’: {‘suffix’: ‘rpe_series’,
‘rpe_series’: [’…/opposite/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’], ‘epi’: [’…/mixed_fmaps/sub-1/fmap/sub-1_dir-AP_run-2_epi.nii.gz’]},
‘concatenated_bids_name’: ‘sub-1’}
- qsiprep.utils.grouping.split_by_phase_encoding_direction(dwi_files, metadatas)[source]
If no fieldmaps have been found for a group of dwi files, split them by PE direction.
- Parameters:
- Returns:
dwi_groups – A list of dictionaries describing each group of dwi files. Each dictionary has the following keys:
dwi_series
: A list of full paths to dwi nifti files in a BIDS tree.fieldmap_info
: A dictionary describing the fieldmap.If no fieldmap is found, the dictionary will be empty.
dwi_series_pedir
: The phase encoding direction of the dwi series.If no information is available, the value will be an empty string.
concatenated_bids_name
: The BIDS name of the concatenated dwi series.If no information is available, the value will be an empty string.
- Return type:
Examples
One of each direction (Not likely to see in the wild) >>> dwi_files = [ … ‘/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-RL_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-LR_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-IS_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-SI_dwi.nii.gz’ … ] >>> metadatas = [ … {‘PhaseEncodingDirection’: ‘j’}, … {‘PhaseEncodingDirection’: ‘j-‘}, … {‘PhaseEncodingDirection’: ‘i’}, … {‘PhaseEncodingDirection’: ‘i-‘}, … {‘PhaseEncodingDirection’: ‘k’}, … {‘PhaseEncodingDirection’: ‘k-‘} … ] >>> split_by_phase_encoding_direction(dwi_files, metadatas) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-RL_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘i’, ‘concatenated_bids_name’: ‘sub-1_dir-RL’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-LR_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘i-‘, ‘concatenated_bids_name’: ‘sub-1_dir-LR’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-AP_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-IS_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘k’, ‘concatenated_bids_name’: ‘sub-1_dir-IS’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-SI_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘k-‘, ‘concatenated_bids_name’: ‘sub-1_dir-SI’}]
Repeats of some: >>> dwi_files = [ … ‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_dir-RL_dwi.nii.gz’ … ] >>> metadatas = [ … {‘PhaseEncodingDirection’: ‘j’}, … {‘PhaseEncodingDirection’: ‘j’}, … {‘PhaseEncodingDirection’: ‘j’}, … {‘PhaseEncodingDirection’: ‘j-‘}, … {‘PhaseEncodingDirection’: ‘i’} … ] >>> split_by_phase_encoding_direction(dwi_files, metadatas) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-RL_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘i’, ‘concatenated_bids_name’: ‘sub-1_dir-RL’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-AP_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_dir-AP_run-2_dwi.nii.gz’, ‘/data/sub-1/dwi/sub-1_dir-AP_run-3_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1_dir-AP’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_dir-PA_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_dir-PA’}]
Some missing metadata >>> dwi_files = [ … ‘/data/sub-1/dwi/sub-1_run-1_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_run-2_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_run-3_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_run-4_dwi.nii.gz’, … ‘/data/sub-1/dwi/sub-1_run-5_dwi.nii.gz’ … ] >>> metadatas = [ … {‘PhaseEncodingDirection’: ‘j’}, … {‘PhaseEncodingDirection’: ‘j’}, … {‘PhaseEncodingDirection’: ‘j-‘}, … {}, … {} … ] >>> split_by_phase_encoding_direction(dwi_files, metadatas) # doctest: +NORMALIZE_WHITESPACE [{‘dwi_series’: [‘/data/sub-1/dwi/sub-1_run-1_dwi.nii.gz’,
‘/data/sub-1/dwi/sub-1_run-2_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j’, ‘concatenated_bids_name’: ‘sub-1’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_run-3_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘j-‘, ‘concatenated_bids_name’: ‘sub-1_run-3’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_run-4_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘’, ‘concatenated_bids_name’: ‘sub-1_run-4’},
- {‘dwi_series’: [‘/data/sub-1/dwi/sub-1_run-5_dwi.nii.gz’],
‘fieldmap_info’: {‘suffix’: None}, ‘dwi_series_pedir’: ‘’, ‘concatenated_bids_name’: ‘sub-1_run-5’}]