Skip to content

Commit

Permalink
Store temporary files in working directory (PennLINC#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo authored Oct 31, 2023
1 parent 42f7da6 commit 66a8fe3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions xcp_d/interfaces/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ def _run_interface(self, runtime):
preprocessed_bold_figure=preprocessed_bold_figure,
denoised_bold_figure=denoised_bold_figure,
standardize=self.inputs.standardize,
temporary_file_dir=runtime.cwd,
mask=mask_file,
seg_data=segmentation_file,
run_index=run_index,
Expand Down
2 changes: 2 additions & 0 deletions xcp_d/tests/test_utils_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_plot_fmri_es(ds001419_data, tmp_path_factory):
denoised_bold_figure=denoised_bold_figure,
TR=t_r,
standardize=False,
temporary_file_dir=tmpdir,
)
assert os.path.isfile(out_file1)
assert os.path.isfile(out_file2)
Expand All @@ -43,6 +44,7 @@ def test_plot_fmri_es(ds001419_data, tmp_path_factory):
denoised_bold_figure=denoised_bold_figure,
TR=t_r,
standardize=True,
temporary_file_dir=tmpdir,
)
assert os.path.isfile(out_file1)
assert os.path.isfile(out_file2)
14 changes: 11 additions & 3 deletions xcp_d/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Plotting tools."""
import os
import tempfile

import matplotlib.cm as cm
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -489,6 +488,7 @@ def plot_fmri_es(
preprocessed_bold_figure,
denoised_bold_figure,
standardize,
temporary_file_dir,
mask=None,
seg_data=None,
run_index=None,
Expand All @@ -512,6 +512,8 @@ def plot_fmri_es(
If False, then the preferred DCAN version of the plot will be generated,
where the BOLD data are not rescaled, and the carpet plot has color limits of -600 and 600.
If True, then the BOLD data will be z-scored and the color limits will be -2 and 2.
temporary_file_dir : :obj:`str`
Path in which to store temporary files.
mask : :obj:`str`, optional
Brain mask file. Used only when the pre- and post-processed BOLD data are NIFTIs.
seg_data : :obj:`str`, optional
Expand Down Expand Up @@ -602,10 +604,11 @@ def plot_fmri_es(
).T

# Make a temporary file for niftis and ciftis
rm_temp_file = True
if preprocessed_bold.endswith(".nii.gz"):
temp_preprocessed_file = os.path.join(tempfile.mkdtemp(), "filex_raw.nii.gz")
temp_preprocessed_file = os.path.join(temporary_file_dir, "filex_raw.nii.gz")
else:
temp_preprocessed_file = os.path.join(tempfile.mkdtemp(), "filex_raw.dtseries.nii")
temp_preprocessed_file = os.path.join(temporary_file_dir, "filex_raw.dtseries.nii")

# Write out the scaled data
temp_preprocessed_file = write_ndata(
Expand All @@ -616,6 +619,7 @@ def plot_fmri_es(
TR=TR,
)
else:
rm_temp_file = False
temp_preprocessed_file = preprocessed_bold

files_for_carpet = [temp_preprocessed_file, uncensored_denoised_bold]
Expand Down Expand Up @@ -685,6 +689,10 @@ def plot_fmri_es(
# Save out the before processing file
fig.savefig(figure_name, bbox_inches="tight", pad_inches=None, dpi=300)

# Remove temporary files
if rm_temp_file:
os.remove(temp_preprocessed_file)

# Save out the after processing file
return preprocessed_bold_figure, denoised_bold_figure

Expand Down

0 comments on commit 66a8fe3

Please sign in to comment.