Skip to content

Commit

Permalink
Merge pull request #143 from bpinsard/enh/bids_filter
Browse files Browse the repository at this point in the history
ENH: Enable users to pass JSON filters to select subsets of data
  • Loading branch information
effigies authored Feb 14, 2020
2 parents f0fd2a7 + 436255d commit 07a7ba3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ install_requires =
matplotlib >= 2.2.0
nibabel >= 3.0.1
nipype >= 1.3.1
niworkflows ~= 1.1.6
niworkflows @ git+https://github.com/poldracklab/niworkflows.git@edeea81352f3f2b276f11e42e7829e1c5a03770f
numpy
packaging
pybids ~= 0.9.4
Expand Down
9 changes: 9 additions & 0 deletions smriprep/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def get_parser():
g_bids.add_argument('--participant-label', '--participant_label', action='store', nargs='+',
help='a space delimited list of participant identifiers or a single '
'identifier (the sub- prefix can be removed)')
g_bids.add_argument(
'--bids-filter-file', action='store', type=Path, metavar='PATH',
help='a JSON file describing custom BIDS input filters using pybids '
'{<suffix>:{<entity>:<filter>,...},...} '
'(https://github.com/bids-standard/pybids/blob/master/bids/layout/config/bids.json)')

g_perfm = parser.add_argument_group('Options to handle performance')
g_perfm.add_argument('--nprocs', '--ncpus', '--nthreads', '--n_cpus', '-n-cpus',
Expand Down Expand Up @@ -270,6 +275,7 @@ def build_workflow(opts, retval):
from subprocess import check_call, CalledProcessError, TimeoutExpired
from pkg_resources import resource_filename as pkgrf

import json
from bids import BIDSLayout
from nipype import logging, config as ncfg
from niworkflows.utils.bids import collect_participants
Expand All @@ -296,6 +302,8 @@ def build_workflow(opts, retval):
subject_list = collect_participants(
layout, participant_label=opts.participant_label)

bids_filters = json.loads(opts.bids_filter_file.read_text()) if opts.bids_filter_file else None

# Load base plugin_settings from file if --use-plugin
if opts.use_plugin is not None:
from yaml import load as loadyml
Expand Down Expand Up @@ -412,6 +420,7 @@ def build_workflow(opts, retval):
spaces=opts.output_spaces,
subject_list=subject_list,
work_dir=str(work_dir),
bids_filters=bids_filters,
)
retval['return_code'] = 0

Expand Down
13 changes: 12 additions & 1 deletion smriprep/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def init_smriprep_wf(
spaces,
subject_list,
work_dir,
bids_filters,
):
"""
Create the execution graph of *sMRIPrep*, with a sub-workflow for each subject.
Expand Down Expand Up @@ -72,6 +73,7 @@ def init_smriprep_wf(
spaces=SpatialReferences(spaces=['MNI152NLin2009cAsym', 'fsaverage5']),
subject_list=['smripreptest'],
work_dir='.',
bids_filters=None,
)
Parameters
Expand Down Expand Up @@ -109,6 +111,9 @@ def init_smriprep_wf(
work_dir : :obj:`str`
Directory in which to store workflow execution state and
temporary files
bids_filters : dict
Provides finer specification of the pipeline input files through pybids entities filters.
A dict with the following structure {<suffix>:{<entity>:<filter>,...},...}
"""
smriprep_wf = Workflow(name='smriprep_wf')
Expand Down Expand Up @@ -141,6 +146,7 @@ def init_smriprep_wf(
skull_strip_template=skull_strip_template,
spaces=spaces,
subject_id=subject_id,
bids_filters=bids_filters,
)

single_subject_wf.config['execution']['crashdump_dir'] = (
Expand Down Expand Up @@ -172,6 +178,7 @@ def init_single_subject_wf(
skull_strip_template,
spaces,
subject_id,
bids_filters,
):
"""
Create a single subject workflow.
Expand Down Expand Up @@ -209,6 +216,7 @@ def init_single_subject_wf(
skull_strip_template=Reference('OASIS30ANTs'),
spaces=SpatialReferences(spaces=['MNI152NLin2009cAsym', 'fsaverage5']),
subject_id='test',
bids_filters=None,
)
Parameters
Expand Down Expand Up @@ -243,6 +251,9 @@ def init_single_subject_wf(
Object containing standard and nonstandard space specifications.
subject_id : :obj:`str`
List of subject labels
bids_filters : dict
Provides finer specification of the pipeline input files through pybids entities filters.
A dict with the following structure {<suffix>:{<entity>:<filter>,...},...}
Inputs
------
Expand All @@ -257,7 +268,7 @@ def init_single_subject_wf(
't1w': ['/completely/made/up/path/sub-01_T1w.nii.gz'],
}
else:
subject_data = collect_data(layout, subject_id)[0]
subject_data = collect_data(layout, subject_id, bids_filters=bids_filters)[0]

if not subject_data['t1w']:
raise Exception("No T1w images found for participant {}. "
Expand Down

0 comments on commit 07a7ba3

Please sign in to comment.