|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +# |
| 4 | +# Copyright The NiPreps Developers <[email protected]> |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# We support and encourage derived works from this project, please read |
| 19 | +# about our expectations at |
| 20 | +# |
| 21 | +# https://www.nipreps.org/community/licensing/ |
| 22 | +# |
| 23 | +"""Writing out derivative files.""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +from fmriprep.utils.bids import dismiss_echo |
| 28 | +from nipype.interfaces import utility as niu |
| 29 | +from nipype.pipeline import engine as pe |
| 30 | +from niworkflows.interfaces.fixes import FixHeaderApplyTransforms as ApplyTransforms |
| 31 | +from niworkflows.utils.images import dseg_label |
| 32 | + |
| 33 | +from fmripost_aroma.config import DEFAULT_MEMORY_MIN_GB |
| 34 | +from fmripost_aroma.interfaces.bids import DerivativesDataSink |
| 35 | + |
| 36 | + |
| 37 | +def init_func_fit_reports_wf( |
| 38 | + *, |
| 39 | + output_dir: str, |
| 40 | + name='func_fit_reports_wf', |
| 41 | +) -> pe.Workflow: |
| 42 | + """Set up a battery of datasinks to store reports in the right location. |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + freesurfer : :obj:`bool` |
| 47 | + FreeSurfer was enabled |
| 48 | + output_dir : :obj:`str` |
| 49 | + Directory in which to save derivatives |
| 50 | + name : :obj:`str` |
| 51 | + Workflow name (default: func_fit_reports_wf) |
| 52 | +
|
| 53 | + Inputs |
| 54 | + ------ |
| 55 | + source_file |
| 56 | + Input BOLD images |
| 57 | + """ |
| 58 | + from nireports.interfaces.reporting.base import ( |
| 59 | + SimpleBeforeAfterRPT as SimpleBeforeAfter, |
| 60 | + ) |
| 61 | + |
| 62 | + from fmripost_aroma.interfaces.nilearn import MeanImage |
| 63 | + |
| 64 | + workflow = pe.Workflow(name=name) |
| 65 | + |
| 66 | + inputfields = [ |
| 67 | + 'source_file', |
| 68 | + 'bold_mni6', |
| 69 | + 'anat_dseg', |
| 70 | + 'anat2std_xfm', |
| 71 | + ] |
| 72 | + inputnode = pe.Node(niu.IdentityInterface(fields=inputfields), name='inputnode') |
| 73 | + |
| 74 | + # Average the BOLD image over time |
| 75 | + calculate_mean_bold = pe.Node( |
| 76 | + MeanImage(), |
| 77 | + name='calculate_mean_bold', |
| 78 | + mem_gb=1, |
| 79 | + ) |
| 80 | + workflow.connect([(inputnode, calculate_mean_bold, [('bold_mni6', 'bold_file')])]) |
| 81 | + |
| 82 | + # Warp the tissue segmentation to MNI |
| 83 | + dseg_to_mni6 = pe.Node( |
| 84 | + ApplyTransforms(interpolation='MultiLabel'), |
| 85 | + name='dseg_to_mni6', |
| 86 | + mem_gb=1, |
| 87 | + ) |
| 88 | + workflow.connect([ |
| 89 | + (inputnode, dseg_to_mni6, [ |
| 90 | + ('anat_dseg', 'input_image'), |
| 91 | + ('anat2std_xfm', 'transforms'), |
| 92 | + ('bold_mni6', 'reference_image'), |
| 93 | + ]), |
| 94 | + ]) # fmt:skip |
| 95 | + |
| 96 | + mni6_wm = pe.Node( |
| 97 | + niu.Function(function=dseg_label), |
| 98 | + name='mni6_wm', |
| 99 | + mem_gb=DEFAULT_MEMORY_MIN_GB, |
| 100 | + ) |
| 101 | + mni6_wm.inputs.label = 2 # BIDS default is WM=2 |
| 102 | + workflow.connect([(dseg_to_mni6, mni6_wm, [('output_image', 'in_file')])]) |
| 103 | + |
| 104 | + # EPI-MNI registration |
| 105 | + epi_mni_report = pe.Node( |
| 106 | + SimpleBeforeAfter( |
| 107 | + before_label='T1w', |
| 108 | + after_label='EPI', |
| 109 | + dismiss_affine=True, |
| 110 | + ), |
| 111 | + name='epi_mni_report', |
| 112 | + mem_gb=0.1, |
| 113 | + ) |
| 114 | + workflow.connect([ |
| 115 | + (inputnode, epi_mni_report, [('coreg_boldref', 'after')]), |
| 116 | + (calculate_mean_bold, epi_mni_report, [('out_file', 'before')]), |
| 117 | + (mni6_wm, epi_mni_report, [('output_image', 'wm_seg')]), |
| 118 | + ]) # fmt:skip |
| 119 | + |
| 120 | + ds_epi_mni_report = pe.Node( |
| 121 | + DerivativesDataSink( |
| 122 | + base_directory=output_dir, |
| 123 | + desc='coreg', |
| 124 | + suffix='bold', |
| 125 | + datatype='figures', |
| 126 | + dismiss_entities=dismiss_echo(), |
| 127 | + ), |
| 128 | + name='ds_epi_mni_report', |
| 129 | + ) |
| 130 | + workflow.connect([ |
| 131 | + (inputnode, ds_epi_mni_report, [('source_file', 'source_file')]), |
| 132 | + (epi_mni_report, ds_epi_mni_report, [('out_report', 'in_file')]), |
| 133 | + ]) # fmt:skip |
| 134 | + |
| 135 | + return workflow |
0 commit comments