|
| 1 | +""" |
| 2 | +Orchestrating the dMRI-preprocessing workflow. |
| 3 | +
|
| 4 | +.. autofunction:: init_dwi_preproc_wf |
| 5 | +.. autofunction:: init_dwi_derivatives_wf |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from nipype import logging |
| 10 | + |
| 11 | +from nipype.pipeline import engine as pe |
| 12 | +from nipype.interfaces import utility as niu |
| 13 | + |
| 14 | +from niworkflows.engine.workflows import LiterateWorkflow as Workflow |
| 15 | + |
| 16 | +from ...config import DEFAULT_MEMORY_MIN_GB |
| 17 | + |
| 18 | +from ...interfaces import DerivativesDataSink |
| 19 | +from ...interfaces.reports import DiffusionSummary |
| 20 | +from ...interfaces.vectors import CheckGradientTable |
| 21 | + |
| 22 | +# dwi workflows |
| 23 | +from .util import init_dwi_reference_wf |
| 24 | + |
| 25 | + |
| 26 | +LOGGING = logging.getLogger('nipype.workflow') |
| 27 | + |
| 28 | + |
| 29 | +def init_dwi_preproc_wf( |
| 30 | + dwi_file, |
| 31 | + debug, |
| 32 | + force_syn, |
| 33 | + ignore, |
| 34 | + low_mem, |
| 35 | + omp_nthreads, |
| 36 | + output_dir, |
| 37 | + reportlets_dir, |
| 38 | + use_syn, |
| 39 | + layout=None, |
| 40 | + num_dwi=1, |
| 41 | +): |
| 42 | + """ |
| 43 | + This workflow controls the diffusion preprocessing stages of *dMRIPrep*. |
| 44 | +
|
| 45 | + Workflow Graph: |
| 46 | + .. workflow:: |
| 47 | + :graph2use: orig |
| 48 | + :simple_form: yes |
| 49 | +
|
| 50 | + from dmriprep.workflows.dwi import init_dwi_preproc_wf |
| 51 | + from collections import namedtuple |
| 52 | + BIDSLayout = namedtuple('BIDSLayout', ['root']) |
| 53 | + wf = init_dwi_preproc_wf( |
| 54 | + dwi_file='/completely/made/up/path/sub-01_dwi.nii.gz', |
| 55 | + debug=False, |
| 56 | + force_syn=True, |
| 57 | + ignore=[], |
| 58 | + low_mem=False, |
| 59 | + omp_nthreads=1, |
| 60 | + output_dir='.', |
| 61 | + reportlets_dir='.', |
| 62 | + layout=BIDSLayout('.'), |
| 63 | + num_dwi=1, |
| 64 | + ) |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + dwi_file : str |
| 69 | + dwi NIfTI file |
| 70 | + debug : bool |
| 71 | + Enable debugging outputs |
| 72 | + force_syn : bool |
| 73 | + **Temporary**: Always run SyN-based SDC |
| 74 | + ignore : list |
| 75 | + Preprocessing steps to skip (may include "sdc") |
| 76 | + low_mem : bool |
| 77 | + Write uncompressed .nii files in some cases to reduce memory usage |
| 78 | + omp_nthreads : int |
| 79 | + Maximum number of threads an individual process may use |
| 80 | + output_dir : str |
| 81 | + Directory in which to save derivatives |
| 82 | + reportlets_dir : str |
| 83 | + Absolute path of a directory in which reportlets will be temporarily stored |
| 84 | + use_syn : bool |
| 85 | + **Experimental**: Enable ANTs SyN-based susceptibility distortion correction (SDC). |
| 86 | + If fieldmaps are present and enabled, this is not run, by default. |
| 87 | + layout : BIDSLayout |
| 88 | + BIDSLayout structure to enable metadata retrieval |
| 89 | + num_dwi : int |
| 90 | + Total number of dwi files that have been set for preprocessing |
| 91 | + (default is 1) |
| 92 | +
|
| 93 | + Inputs |
| 94 | + ------ |
| 95 | + dwi_file |
| 96 | + dwi NIfTI file |
| 97 | + bvec_file |
| 98 | + File path of the b-values |
| 99 | + bval_file |
| 100 | + File path of the b-vectors |
| 101 | +
|
| 102 | + Outputs |
| 103 | + ------- |
| 104 | + dwi_file |
| 105 | + dwi NIfTI file |
| 106 | + dwi_mask |
| 107 | + dwi mask |
| 108 | +
|
| 109 | + See also |
| 110 | + -------- |
| 111 | + * :py_func:`~dmriprep.workflows.dwi.util.init_dwi_reference_wf |
| 112 | +
|
| 113 | + """ |
| 114 | + |
| 115 | + wf_name = _get_wf_name(dwi_file) |
| 116 | + |
| 117 | + # Build workflow |
| 118 | + workflow = Workflow(name=wf_name) |
| 119 | + workflow.__desc__ = """ |
| 120 | +
|
| 121 | +Diffusion data preprocessing |
| 122 | +
|
| 123 | +: For each of the {num_dwi} dwi scans found per subject (across all sessions), |
| 124 | + the following preprocessing was performed. |
| 125 | +""".format(num_dwi=num_dwi) |
| 126 | + |
| 127 | + workflow.__postdesc__ = """\ |
| 128 | + """ |
| 129 | + |
| 130 | + metadata = layout.get_metadata(dwi_file) |
| 131 | + |
| 132 | + inputnode = pe.Node(niu.IdentityInterface( |
| 133 | + fields=['dwi_file']), |
| 134 | + name='inputnode') |
| 135 | + inputnode.inputs.dwi_file = dwi_file |
| 136 | + inputnode.inputs.bvec_file = layout.get_bvec(dwi_file) |
| 137 | + inputnode.inputs.bval_file = layout.get_bval(dwi_file) |
| 138 | + |
| 139 | + outputnode = pe.Node(niu.IdentityInterface( |
| 140 | + fields=['dwi_file', 'bvec_file', 'bval_file', 'rasb_file', |
| 141 | + 'dwi_mask']), |
| 142 | + name='outputnode') |
| 143 | + |
| 144 | + summary = pe.Node( |
| 145 | + DiffusionSummary( |
| 146 | + pe_direction=metadata.get("PhaseEncodingDirection")), |
| 147 | + name='summary', mem_gb=DEFAULT_MEMORY_MIN_GB, run_without_submitting=True) |
| 148 | + |
| 149 | + gradient_table = pe.Node(CheckGradientTable(), name='gradient_table') |
| 150 | + |
| 151 | + dwi_reference_wf = init_dwi_reference_wf(omp_nthreads=1, gen_report=True) |
| 152 | + |
| 153 | + # dwi_derivatives_wf = init_dwi_derivatives_wf() |
| 154 | + |
| 155 | + # MAIN WORKFLOW STRUCTURE |
| 156 | + workflow.connect([ |
| 157 | + (inputnode, gradient_table, [ |
| 158 | + ('dwi_file', 'dwi_file'), |
| 159 | + ('bvec_file', 'in_bvec'), |
| 160 | + ('bval_file', 'in_bval')]), |
| 161 | + (inputnode, dwi_reference_wf, [('dwi_file', 'inputnode.dwi_file')]), |
| 162 | + (gradient_table, dwi_reference_wf, [('b0_ixs', 'inputnode.b0_ixs')]), |
| 163 | + ]) |
| 164 | + |
| 165 | + # REPORTING |
| 166 | + ds_report_summary = pe.Node( |
| 167 | + DerivativesDataSink(desc='summary', keep_dtype=True), |
| 168 | + name='ds_report_summary', run_without_submitting=True, |
| 169 | + mem_gb=DEFAULT_MEMORY_MIN_GB |
| 170 | + ) |
| 171 | + |
| 172 | + return workflow |
| 173 | + |
| 174 | + |
| 175 | +def _get_wf_name(dwi_fname): |
| 176 | + """ |
| 177 | + Derive the workflow name for supplied dwi file. |
| 178 | + >>> _get_wf_name('/completely/made/up/path/sub-01_dwi.nii.gz') |
| 179 | + 'dwi_preproc_wf' |
| 180 | + >>> _get_wf_name('/completely/made/up/path/sub-01_run-1_dwi.nii.gz') |
| 181 | + 'dwi_preproc_run_1_wf' |
| 182 | + """ |
| 183 | + from nipype.utils.filemanip import split_filename |
| 184 | + fname = split_filename(dwi_fname)[1] |
| 185 | + fname_nosub = '_'.join(fname.split("_")[1:]) |
| 186 | + name = "dwi_preproc_" + fname_nosub.replace( |
| 187 | + ".", "_").replace(" ", "").replace("-", "_").replace("_dwi", "_wf") |
| 188 | + |
| 189 | + return name |
0 commit comments