diff --git a/fprodents/patch/utils/__init__.py b/fprodents/patch/utils/__init__.py index 1f4990c60..d215260b2 100644 --- a/fprodents/patch/utils/__init__.py +++ b/fprodents/patch/utils/__init__.py @@ -61,3 +61,40 @@ def get_template_specs(in_template, template_spec=None, default_resolution=None) ) return str(tpl_target_path), common_spec + + +def extract_entities(file_list): + """ + Return a dictionary of common entities given a list of files. + + Examples + -------- + >>> extract_entities('sub-01/anat/sub-01_T1w.nii.gz') + {'subject': '01', 'suffix': 'T1w', 'datatype': 'anat', 'extension': 'nii.gz'} + >>> extract_entities(['sub-01/anat/sub-01_T1w.nii.gz'] * 2) + {'subject': '01', 'suffix': 'T1w', 'datatype': 'anat', 'extension': 'nii.gz'} + >>> extract_entities(['sub-01/anat/sub-01_run-1_T1w.nii.gz', + ... 'sub-01/anat/sub-01_run-2_T1w.nii.gz']) + {'subject': '01', 'run': [1, 2], 'suffix': 'T1w', 'datatype': 'anat', + 'extension': 'nii.gz'} + + """ + from collections import defaultdict + from bids.layout import parse_file_entities + from niworkflows.utils.connections import listify + + entities = defaultdict(list) + for e, v in [ + ev_pair + for f in listify(file_list) + for ev_pair in parse_file_entities(f).items() + ]: + entities[e].append(v) + + def _unique(inlist): + inlist = sorted(set(inlist)) + if len(inlist) == 1: + return inlist[0] + return inlist + + return {k: _unique(v) for k, v in entities.items()} diff --git a/fprodents/workflows/base.py b/fprodents/workflows/base.py index 7d1d56d09..97f731dc1 100644 --- a/fprodents/workflows/base.py +++ b/fprodents/workflows/base.py @@ -105,11 +105,12 @@ def init_single_subject_wf(subject_id): from niworkflows.interfaces.bids import BIDSInfo from niworkflows.interfaces.nilearn import NILEARN_VERSION from niworkflows.utils.bids import collect_data + from niworkflows.utils.connections import listify from niworkflows.utils.spaces import Reference from niworkflows.workflows.epi.refmap import init_epi_reference_wf from nirodents.workflows.brainextraction import init_rodent_brain_extraction_wf from ..patch.interfaces import BIDSDataGrabber - from ..patch.utils import fix_multi_source_name + from ..patch.utils import extract_entities, fix_multi_source_name from ..patch.workflows.anatomical import init_anat_preproc_wf subject_data = collect_data( @@ -299,6 +300,10 @@ def init_single_subject_wf(subject_id): ) for bold_file in subject_data["bold"]: + echoes = extract_entities(bold_file).get("echo", []) + echo_idxs = listify(echoes) + multiecho = len(echo_idxs) > 2 + bold_ref_wf = init_epi_reference_wf( auto_bold_nss=True, omp_nthreads=config.nipype.omp_nthreads diff --git a/fprodents/workflows/bold/base.py b/fprodents/workflows/bold/base.py index 9c5ba3c11..10215b797 100644 --- a/fprodents/workflows/bold/base.py +++ b/fprodents/workflows/bold/base.py @@ -125,6 +125,8 @@ def init_func_preproc_wf(bold_file): from niworkflows.interfaces.utility import KeySelect, DictMerge from nipype.interfaces.freesurfer.utils import LTAConvert + from ...patch.utils import extract_entities + mem_gb = {"filesize": 1, "resampled": 1, "largemem": 1} bold_tlen = 10 @@ -769,39 +771,3 @@ def _to_join(in_file, join_file): return in_file res = JoinTSVColumns(in_file=in_file, join_file=join_file).run() return res.outputs.out_file - - -def extract_entities(file_list): - """ - Return a dictionary of common entities given a list of files. - - Examples - -------- - >>> extract_entities('sub-01/anat/sub-01_T1w.nii.gz') - {'subject': '01', 'suffix': 'T1w', 'datatype': 'anat', 'extension': 'nii.gz'} - >>> extract_entities(['sub-01/anat/sub-01_T1w.nii.gz'] * 2) - {'subject': '01', 'suffix': 'T1w', 'datatype': 'anat', 'extension': 'nii.gz'} - >>> extract_entities(['sub-01/anat/sub-01_run-1_T1w.nii.gz', - ... 'sub-01/anat/sub-01_run-2_T1w.nii.gz']) - {'subject': '01', 'run': [1, 2], 'suffix': 'T1w', 'datatype': 'anat', - 'extension': 'nii.gz'} - - """ - from collections import defaultdict - from bids.layout import parse_file_entities - - entities = defaultdict(list) - for e, v in [ - ev_pair - for f in listify(file_list) - for ev_pair in parse_file_entities(f).items() - ]: - entities[e].append(v) - - def _unique(inlist): - inlist = sorted(set(inlist)) - if len(inlist) == 1: - return inlist[0] - return inlist - - return {k: _unique(v) for k, v in entities.items()}