Skip to content

Commit

Permalink
Use context manager for fMRIPrep workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Nov 13, 2023
1 parent c5a489b commit 14a6b78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
29 changes: 22 additions & 7 deletions aslprep/workflows/asl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""Preprocessing workflows for ASL data."""
import nibabel as nb
import numpy as np
from fmriprep.workflows.bold import confounds
from fmriprep.workflows.bold.base import get_estimator
from fmriprep.workflows.bold.registration import init_bold_reg_wf, init_bold_t1_trans_wf
from fmriprep.workflows.bold.resampling import (
Expand All @@ -25,7 +26,7 @@
from aslprep.utils.bids import collect_run_data
from aslprep.utils.misc import _create_mem_gb, _get_wf_name
from aslprep.workflows.asl.cbf import init_compute_cbf_wf, init_parcellate_cbf_wf
from aslprep.workflows.asl.confounds import init_asl_confounds_wf, init_carpetplot_wf
from aslprep.workflows.asl.confounds import init_asl_confounds_wf
from aslprep.workflows.asl.hmc import init_asl_hmc_wf
from aslprep.workflows.asl.outputs import init_asl_derivatives_wf
from aslprep.workflows.asl.plotting import init_plot_cbf_wf
Expand All @@ -34,6 +35,19 @@
from aslprep.workflows.asl.util import init_asl_reference_wf, init_validate_asl_wf


class OverrideConfoundsDerivativesDataSink:
def __enter__(self):
# Save the original class
self.original_class = confounds.DerivativesDataSink
# Replace SomeClass with YourOwnClass
confounds.DerivativesDataSink = DerivativesDataSink
return self

def __exit__(self, exc_type, exc_value, traceback):
# Restore the original class
confounds.DerivativesDataSink = self.original_class


def init_asl_preproc_wf(asl_file, has_fieldmap=False):
"""Perform the functional preprocessing stages of ASLPrep.
Expand Down Expand Up @@ -1010,12 +1024,13 @@ def init_asl_preproc_wf(asl_file, has_fieldmap=False):
# Standard-space outputs requested.
# Since ASLPrep automatically includes MNI152NLin2009cAsym, this should always be reached.
if spaces.get_spaces(nonstandard=False, dim=(3,)):
carpetplot_wf = init_carpetplot_wf(
mem_gb=mem_gb["resampled"],
metadata=metadata,
cifti_output=config.workflow.cifti_output,
name="carpetplot_wf",
)
with OverrideConfoundsDerivativesDataSink:
carpetplot_wf = confounds.init_carpetplot_wf(
mem_gb=mem_gb["resampled"],
metadata=metadata,
cifti_output=config.workflow.cifti_output,
name="carpetplot_wf",
)

# Xform to "MNI152NLin2009cAsym" is always computed.
carpetplot_select_std = pe.Node(
Expand Down
1 change: 1 addition & 0 deletions aslprep/workflows/asl/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def init_carpetplot_wf(
"""Build a workflow to generate *carpet* plots.
XXX: Copied from fMRIPrep. Needs to be replaced with some version that works for ASLPrep.
TODO: Find a solution that directly uses fMRIPrep's.
Resamples the MNI parcellation (ad-hoc parcellation derived from the
Harvard-Oxford template and others).
Expand Down

0 comments on commit 14a6b78

Please sign in to comment.