#!/usr/bin/env python
"""
dMRIprep base processing workflows
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: init_dmriprep_wf
.. autofunction:: init_single_subject_wf
"""
import os
from copy import deepcopy
from nipype.pipeline import engine as pe
from ..utils.bids import collect_data
from .dwi import init_dwi_preproc_wf
[docs]def init_dmriprep_wf(
layout,
output_dir,
subject_list,
session_list,
concat_dwis,
b0_thresh,
output_resolution,
bet_dwi,
bet_mag,
acqp_file,
slspec_file,
omp_nthreads,
ignore,
use_ants,
use_brainsuite,
work_dir,
synb0_dir
):
"""
This workflow organizes the execusion of dMRIprep, with a sub-workflow for
each subject.
.. workflow::
:graph2use: orig
:simple_form: yes
from collections import namedtuple
from dmriprep.workflows.base import init_dmriprep_wf
BIDSLayout = namedtuple('BIDSLayout', ['root'])
wf = init_dmriprep_wf(
layout=BIDSLayout('.', validate=False),
output_dir='.',
subject_list=['dmripreptest'],
session_list=[],
concat_dwis=[],
b0_thresh=5,
output_resolution=(1, 1, 1),
bet_dwi=0.3,
bet_mag=0.3,
acqp_file='',
omp_nthreads=1,
ignore=[],
use_ants=False,
use_brainsuite=False,
work_dir='.',
synb0_dir=''
)
Parameters
layout: BIDSLayout object
BIDS dataset layout
output_dir: str
Directory in which to save derivatives
subject_list: list
List of subject labels
session_list: list
List of session labels
concat_dwis: list
List of dwi images to concatenate (specified with the 'acq-') tag
b0_thresh: int
Threshold for identifying bval as a b0
output_resolution: tuple
Output resolution of dwi image in x, y and z axes
bet_dwi: float
Fractional intensity threshold for BET on dwi image
bet_mag: float
Fractional intensity threshold for BET on magnitude image
omp_nthreads: int
Maximum number of threads an individual process may use
acqp_file: str
Optionally supply eddy acquisition parameters file
ignore: list
Preprocessing steps to skip (may include 'denoise', 'unring', 'fieldmaps')
work_dir: str
Directory in which to store workflow execution state and temporary files
synb0_dir: str
Direction in which synb0 derivatives are saved
"""
dmriprep_wf = pe.Workflow(name='dmriprep_wf')
dmriprep_wf.base_dir = work_dir
for subject_id in subject_list:
single_subject_wf = init_single_subject_wf(
name='single_subject_' + subject_id + '_wf',
layout=layout,
output_dir=output_dir,
subject_id=subject_id,
session_list=session_list,
concat_dwis=concat_dwis,
b0_thresh=b0_thresh,
output_resolution=output_resolution,
bet_dwi=bet_dwi,
bet_mag=bet_mag,
acqp_file=acqp_file,
slspec_file=slspec_file,
omp_nthreads=omp_nthreads,
ignore=ignore,
use_ants=use_ants,
use_brainsuite=use_brainsuite,
work_dir=work_dir,
synb0_dir=synb0_dir
)
single_subject_wf.config['execution']['crashdump_dir'] = os.path.join(
output_dir, 'dmriprep', 'sub-' + subject_id, 'log'
)
for node in single_subject_wf._get_all_nodes():
node.config = deepcopy(single_subject_wf.config)
dmriprep_wf.add_nodes([single_subject_wf])
return dmriprep_wf
[docs]def init_single_subject_wf(
name,
layout,
output_dir,
subject_id,
session_list,
concat_dwis,
b0_thresh,
output_resolution,
bet_dwi,
bet_mag,
omp_nthreads,
acqp_file,
slspec_file,
ignore,
use_ants,
use_brainsuite,
work_dir,
synb0_dir
):
"""
This workflow organizes the preprocessing pipeline for a single subject.
It collects and reports information about the subject, and prepares
sub-workflows to perform diffusion preprocessing.
Diffusion preprocessing is performed using a separate workflow for each
individual dwi series.
.. workflow::
:graph2use: orig
:simple_form: yes
from collections import namedtuple
from dmriprep.workflows.base import init_single_subject_wf
BIDSLayout = namedtuple('BIDSLayout', ['root'])
wf = init_single_subject_wf(
name='single_subject_wf',
layout=BIDSLayout,
output_dir='.',
subject_id='test',
session_list=[],
concat_dwis=[],
b0_thresh=5,
output_resolution=(1, 1, 1),
bet_dwi=0.3,
bet_mag=0.3,
acqp_file='',
omp_nthreads=1,
ignore=[],
use_ants=False,
use_brainsuite=False,
work_dir='.',
synb0_dir=''
)
Parameters
name:
layout: BIDSLayout object
BIDS dataset layout
output_dir: str
Directory in which to save derivatives
subject_id: str
Single subject label
session_list: list
List of sessions
concat_dwis: list
List of dwi images to concatenate (specified with the 'acq-') tag
b0_thresh: int
Threshold for identifying bval as a b0
output_resolution: tuple
Output resolution of dwi image in x, y and z axes
bet_dwi: float
Fractional intensity threshold for BET on dwi image
bet_mag: float
Fractional intensity threshold for BET on magnitude image
acqp_file: str
Optional acquisition parameters file
omp_nthreads: int
Maximum number of threads an individual process may use
ignore: list
Preprocessing steps to skip (may include 'denoise', 'unring', 'fieldmaps')
work_dir: str
Directory in which to store workflow execution state and temporary files
synb0_dir: str
Directory in which synb0 derivatives are saved
"""
# for documentation purposes
if name in ('single_subject_wf', 'single_subject_dmripreptest_wf'):
subject_data = {
't1w': ['/madeup/path/sub-01_T1w.nii.gz'],
'dwi': ['/madeup/path/sub-01_dwi.nii.gz']
}
else:
subject_data = collect_data(layout, subject_id, concat_dwis, session_list)
if subject_data['dwi'] == []:
raise Exception(
'No dwi images found for participant {} in session {}. '
'All workflows require dwi images'.format(
subject_id, session_list if session_list else '<all>')
)
subject_wf = pe.Workflow(name=name)
for dwi_file in subject_data['dwi']:
dwi_preproc_wf = init_dwi_preproc_wf(
layout=layout,
output_dir=output_dir,
subject_id=subject_id,
dwi_file=dwi_file,
b0_thresh=b0_thresh,
output_resolution=output_resolution,
bet_dwi=bet_dwi,
bet_mag=bet_mag,
omp_nthreads=omp_nthreads,
acqp_file=acqp_file,
slspec_file=slspec_file,
ignore=ignore,
use_ants=use_ants,
use_brainsuite=use_brainsuite,
synb0_dir=synb0_dir
)
dwi_preproc_wf.base_dir = os.path.join(
os.path.abspath(work_dir), subject_id
)
return subject_wf