Skip to content

Commit

Permalink
REF: extract_entities to base wf
Browse files Browse the repository at this point in the history
  • Loading branch information
eilidhmacnicol committed Oct 29, 2021
1 parent 24493a8 commit b113753
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
37 changes: 37 additions & 0 deletions fprodents/patch/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
7 changes: 6 additions & 1 deletion fprodents/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
38 changes: 2 additions & 36 deletions fprodents/workflows/bold/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()}

0 comments on commit b113753

Please sign in to comment.