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

RF: update objects with niworkflows #6

Merged
merged 5 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions docs/spaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ onto other output spaces.
That is achieved using the ``--output-spaces`` argument, where standard and
nonstandard spaces can be inserted.

.. important::
*fMRIPrep* will reduce the amount of output spaces to just spaces listed in ``--output-spaces``,
even if other options require resampling the preprocessed data into intermediary spaces.

Standard spaces
"""""""""""""""
When using *fMRIPrep* in a workflow that will investigate effects that span across
Expand Down Expand Up @@ -151,18 +155,18 @@ To do so, follow the next steps.
created in ``$HOME/.cache/templateflow``.
You can modify such a configuration with the ``TEMPLATEFLOW_HOME``
environment variable, e.g.::

$ export TEMPLATEFLOW_HOME=$HOME/.templateflow

2. Install the client within your favorite Python 3 environment (this can
be done in your login-node, or in a host with Internet access,
be done in your login-node, or in a host with Internet access,
without need for Docker/Singularity)::

$ python -m pip install -U templateflow

3. Use the ``get()`` utility of the client to pull down all the templates you'll
want to use. For example::

$ python -c "from templateflow.api import get; get(['MNI152NLin2009cAsym', 'MNI152NLin6Asym', 'OASIS30ANTs', 'MNIPediatricAsym', 'MNIInfant'])"

After pulling down the resources you'll need, you will just need to make sure your
Expand Down
53 changes: 22 additions & 31 deletions docs/workflows.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ is presented below:
:simple_form: yes

from collections import namedtuple
from niworkflows.utils.spaces import Space
from fmriprep.utils.spaces import SpacesManager
from niworkflows.utils.spaces import Reference, SpatialReferences
from fmriprep.workflows.base import init_single_subject_wf
BIDSLayout = namedtuple('BIDSLayout', ['root'])
spaces = SpacesManager([
('MNI152Lin', {}),
('fsaverage', {'density': '10k'}),
('T1w', {}),
('fsnative', {})
])
spaces.snap()
BIDSLayout = namedtuple('BIDSLayout', ('root'))
wf = init_single_subject_wf(
anat_only=False,
aroma_melodic_dim=-200,
Expand Down Expand Up @@ -57,8 +49,13 @@ is presented below:
regressors_dvars_th=1.5,
regressors_fd_th=0.5,
skull_strip_fixed_seed=False,
skull_strip_template=Space('OASIS30ANTs', {}),
spaces=spaces,
skull_strip_template=Reference('OASIS30ANTs'),
spaces=SpatialReferences(
spaces=[('MNI152Lin', {}),
('fsaverage', {'density': '10k'}),
('T1w', {}),
('fsnative', {})],
checkpoint=True),
subject_id='test',
t2s_coreg=False,
task_id='',
Expand All @@ -75,7 +72,7 @@ T1w/T2w preprocessing
:graph2use: orig
:simple_form: yes

from niworkflows.utils.spaces import Space, SpatialReferences
from niworkflows.utils.spaces import Reference, SpatialReferences
from fmriprep.workflows.anatomical import init_anat_preproc_wf
wf = init_anat_preproc_wf(
bids_root='.',
Expand All @@ -85,7 +82,7 @@ T1w/T2w preprocessing
num_t1w=1,
omp_nthreads=1,
output_dir='.',
skull_strip_template=Space('MNI152NLin2009cAsym', {}),
skull_strip_template=Reference('MNI152NLin2009cAsym'),
spaces=SpatialReferences([
('MNI152Lin', {}),
('fsaverage', {'density': '10k'}),
Expand Down Expand Up @@ -291,16 +288,8 @@ BOLD preprocessing
:simple_form: yes

from collections import namedtuple
from niworkflows.utils.spaces import Space
from fmriprep.utils.spaces import SpacesManager
from niworkflows.utils.spaces import SpatialReferences
from fmriprep.workflows.bold.base import init_func_preproc_wf
spaces = SpacesManager([
('MNI152Lin', {}),
('fsaverage', {'density': '10k'}),
('T1w', {}),
('fsnative', {})
])
spaces.snap()
BIDSLayout = namedtuple('BIDSLayout', ['root'])
wf = init_func_preproc_wf(
aroma_melodic_dim=-200,
Expand All @@ -324,7 +313,12 @@ BOLD preprocessing
regressors_dvars_th=1.5,
reportlets_dir='.',
t2s_coreg=False,
spaces=spaces,
spaces=SpatialReferences(
spaces=[('MNI152Lin', {}),
('fsaverage', {'density': '10k'}),
('T1w', {}),
('fsnative', {})],
checkpoint=True),
use_aroma=False,
use_bbr=True,
use_syn=True,
Expand Down Expand Up @@ -506,18 +500,15 @@ Resampling BOLD runs onto standard spaces
:graph2use: colored
:simple_form: yes

from fmriprep.utils.spaces import SpacesManager
from niworkflows.utils.spaces import SpatialReferences
from fmriprep.workflows.bold import init_bold_std_trans_wf
spaces = SpacesManager([
('MNI152Lin', {}),
('MNIPediatricAsym', {'cohort': '6'}),
])
spaces.snap()
wf = init_bold_std_trans_wf(
freesurfer=True,
mem_gb=3,
omp_nthreads=1,
spaces=spaces,
spaces=SpatialReferences(
spaces=[('MNI152Lin', {}), ('MNIPediatricAsym', {'cohort': '6'})],
checkpoint=True),
)

This sub-workflow concatenates the transforms calculated upstream (see
Expand Down
12 changes: 6 additions & 6 deletions fmriprep/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_parser():
from packaging.version import Version
from ..__about__ import __version__
from .version import check_latest, is_flagged
from ..utils.spaces import Space, SpacesManager, SpacesManagerAction
from niworkflows.utils.spaces import Reference, SpatialReferences, OutputReferencesAction

verstr = 'fMRIPrep v{}'.format(__version__)
currentv = Version(__version__)
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_parser():
'option is not enabled, standard EPI-T1 coregistration is performed '
'using the middle echo.')
g_conf.add_argument(
'--output-spaces', nargs='+', action=SpacesManagerAction, default=SpacesManager(),
'--output-spaces', nargs='+', action=OutputReferencesAction, default=SpatialReferences(),
help="""\
Standard and non-standard spaces to resample anatomical and functional images to. \
Standard spaces may be specified by the form \
Expand Down Expand Up @@ -182,7 +182,7 @@ def get_parser():
# ANTs options
g_ants = parser.add_argument_group('Specific options for ANTs registrations')
g_ants.add_argument(
'--skull-strip-template', action='store', default='OASIS30ANTs', type=Space.from_string,
'--skull-strip-template', default='OASIS30ANTs', type=Reference.from_string,
help='select a template for skull-stripping with antsBrainExtraction')
g_ants.add_argument('--skull-strip-fixed-seed', action='store_true',
help='do not use a random seed for skull-stripping - will ensure '
Expand Down Expand Up @@ -693,7 +693,7 @@ def parse_spaces(opts):

"""
spaces = opts.output_spaces
spaces.snap()
spaces.checkpoint()

if opts.use_aroma:
# Make sure there's a normalization to FSL for AROMA to use.
Expand All @@ -706,15 +706,15 @@ def parse_spaces(opts):
spaces.add(('MNI152NLin6Asym', {'res': vol_res}))

# These arguments implicitly signal expected output
if not spaces.spaces:
if not spaces.references:
warnings.warn(
"fMRIPrep will not generate preprocessed derivatives because "
"none of `--output-spaces`, `--use-aroma`, or `--cifti-output` "
"were set."
)

# Add the default standard space (required by several sub-workflows)
if "MNI152NLin2009cAsym" not in spaces.get_std_spaces(dim=(3,)):
if "MNI152NLin2009cAsym" not in spaces.get_spaces(nonstandard=False, dim=(3,)):
spaces.add("MNI152NLin2009cAsym")
return spaces

Expand Down
59 changes: 0 additions & 59 deletions fmriprep/utils/spaces.py

This file was deleted.

81 changes: 37 additions & 44 deletions fmriprep/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,9 @@ def init_fmriprep_wf(
from collections import namedtuple, OrderedDict
BIDSLayout = namedtuple('BIDSLayout', ['root'])
from fmriprep.workflows.base import init_fmriprep_wf
from niworkflows.utils.spaces import Space
from fmriprep.utils.spaces import SpacesManager
from niworkflows.utils.spaces import Reference, SpatialReferences

os.environ['FREESURFER_HOME'] = os.getcwd()
spaces = SpacesManager([
('MNI152Lin',),
('fsaverage', {'density': '10k'}),
('T1w',),
('fsnative',)
])
spaces.snap()
wf = init_fmriprep_wf(
anat_only=False,
aroma_melodic_dim=-200,
Expand Down Expand Up @@ -123,8 +115,13 @@ def init_fmriprep_wf(
regressors_fd_th=0.5,
run_uuid='X',
skull_strip_fixed_seed=False,
skull_strip_template=Space('OASIS30ANTs', {}),
spaces=spaces,
skull_strip_template=Reference('OASIS30ANTs'),
spaces=SpatialReferences(
spaces=['MNI152Lin',
('fsaverage', {'density': '10k'}),
'T1w',
'fsnative'],
checkpoint=True),
subject_list=['fmripreptest'],
t2s_coreg=False,
task_id='',
Expand Down Expand Up @@ -191,15 +188,15 @@ def init_fmriprep_wf(
skull_strip_fixed_seed : bool
Do not use a random seed for skull-stripping - will ensure
run-to-run replicability when used with --omp-nthreads 1
spaces : :py:class:`~fmriprep.utils.spaces.SpacesManager`
Organize and filter spatial normalizations. Composed of internal and output lists
of spaces in the form of (Template, Specs). `Template` is a string of either
TemplateFlow IDs (e.g., ``MNI152Lin``, ``MNI152NLin6Asym``, ``MNI152NLin2009cAsym``, or
``fsLR``), nonstandard references (e.g., ``T1w`` or ``anat``, ``sbref``, ``run``, etc.),
or paths pointing to custom templates organized in a TemplateFlow-like structure.
Specs is a dictionary with template specifications (e.g., the specs for the template
``MNI152Lin`` could be ``{'resolution': 2}`` if one wants the resampling to be done on
the 2mm resolution version of the selected template).
spaces : :py:class:`~niworkflows.utils.spaces.SpatialReferences`
A container for storing, organizing, and parsing spatial normalizations. Composed of
:py:class:`~niworkflows.utils.spaces.Reference` objects representing spatial references.
Each ``Reference`` contains a space, which is a string of either TemplateFlow template IDs
(e.g., ``MNI152Lin``, ``MNI152NLin6Asym``, ``MNIPediatricAsym``), nonstandard references
(e.g., ``T1w`` or ``anat``, ``sbref``, ``run``, etc.), or a custom template located in
the TemplateFlow root directory. Each ``Reference`` may also contain a spec, which is a
dictionary with template specifications (e.g., a specification of ``{'resolution': 2}``
would lead to resampling on a 2mm resolution of the space).
subject_list : list
List of subject labels
t2s_coreg : bool
Expand Down Expand Up @@ -336,18 +333,10 @@ def init_single_subject_wf(
:simple_form: yes

from collections import namedtuple
from niworkflows.utils.spaces import Space
from fmriprep.utils.spaces import SpacesManager
from niworkflows.utils.spaces import Reference, SpatialReferences
from fmriprep.workflows.base import init_single_subject_wf

BIDSLayout = namedtuple('BIDSLayout', ['root'])
spaces = SpacesManager([
('MNI152Lin',),
('fsaverage', {'density': '10k'}),
('T1w',),
('fsnative',)
])
spaces.snap()
wf = init_single_subject_wf(
anat_only=False,
aroma_melodic_dim=-200,
Expand Down Expand Up @@ -375,8 +364,13 @@ def init_single_subject_wf(
regressors_dvars_th=1.5,
regressors_fd_th=0.5,
skull_strip_fixed_seed=False,
skull_strip_template=Space('OASIS30ANTs', {}),
spaces=spaces,
skull_strip_template=Reference('OASIS30ANTs'),
spaces=SpatialReferences(
spaces=['MNI152Lin',
('fsaverage', {'density': '10k'}),
'T1w',
'fsnative'],
checkpoint=True),
subject_id='test',
t2s_coreg=False,
task_id='',
Expand Down Expand Up @@ -450,15 +444,15 @@ def init_single_subject_wf(
List of subject labels
t2s_coreg : bool
For multi-echo EPI, use the calculated T2*-map for T2*-driven coregistration
spaces : :py:class:`~fmriprep.utils.spaces.SpacesManager`
Organize and filter spatial normalizations. Composed of internal and output lists
of spaces in the form of (Template, Specs). `Template` is a string of either
TemplateFlow IDs (e.g., ``MNI152Lin``, ``MNI152NLin6Asym``, ``MNI152NLin2009cAsym``, or
``fsLR``), nonstandard references (e.g., ``T1w`` or ``anat``, ``sbref``, ``run``, etc.),
or paths pointing to custom templates organized in a TemplateFlow-like structure.
Specs is a dictionary with template specifications (e.g., the specs for the template
``MNI152Lin`` could be ``{'resolution': 2}`` if one wants the resampling to be done on
the 2mm resolution version of the selected template).
spaces : :py:class:`~niworkflows.utils.spaces.SpatialReferences`
A container for storing, organizing, and parsing spatial normalizations. Composed of
:py:class:`~niworkflows.utils.spaces.Reference` objects representing spatial references.
Each ``Reference`` contains a space, which is a string of either TemplateFlow template IDs
(e.g., ``MNI152Lin``, ``MNI152NLin6Asym``, ``MNIPediatricAsym``), nonstandard references
(e.g., ``T1w`` or ``anat``, ``sbref``, ``run``, etc.), or a custom template located in
the TemplateFlow root directory. Each ``Reference`` may also contain a spec, which is a
dictionary with template specifications (e.g., a specification of ``{'resolution': 2}``
would lead to resampling on a 2mm resolution of the space).
task_id : str or None
Task ID of BOLD series to preprocess, or ``None`` to preprocess all
use_aroma : bool
Expand Down Expand Up @@ -536,10 +530,9 @@ def init_single_subject_wf(
bids_info = pe.Node(BIDSInfo(
bids_dir=layout.root, bids_validate=False), name='bids_info')

summary = pe.Node(SubjectSummary(
std_spaces=spaces.get_std_spaces(),
nstd_spaces=spaces.get_nonstd_spaces()),
name='summary', run_without_submitting=True)
summary = pe.Node(SubjectSummary(std_spaces=spaces.get_spaces(nonstandard=False),
nstd_spaces=spaces.get_spaces(standard=False)),
name='summary', run_without_submitting=True)

about = pe.Node(AboutSummary(version=__version__,
command=' '.join(sys.argv)),
Expand Down
Loading