diff --git a/PUMI/pipelines/func/deconfound.py b/PUMI/pipelines/func/deconfound.py index e8d82d0..43f9d7d 100644 --- a/PUMI/pipelines/func/deconfound.py +++ b/PUMI/pipelines/func/deconfound.py @@ -44,6 +44,7 @@ def create_montage(vol_1, vol_2, vol_corrected): path = str(Path(os.getcwd() + '/fieldmap_correction_comparison.png')) plt.savefig(path) + plt.close(fig) return path vol_1 = pick_volume('vol_1', volume=volume) diff --git a/PUMI/pipelines/func/timeseries_extractor.py b/PUMI/pipelines/func/timeseries_extractor.py index ca9f7c3..58fb863 100644 --- a/PUMI/pipelines/func/timeseries_extractor.py +++ b/PUMI/pipelines/func/timeseries_extractor.py @@ -23,7 +23,7 @@ def extract_timeseries_nativespace_qc(wf, **kwargs): wf.connect('inputspec', 'timeseries', qc_timeseries, 'timeseries') wf.connect('inputspec', 'atlas', qc_timeseries, 'atlas') wf.connect('inputspec', 'modules', qc_timeseries, 'modules') - wf.connect(qc_timeseries, 'plotfile', 'sinker', 'regTimeseriesQC') + wf.connect(qc_timeseries, 'plotfile', 'sinker', 'qc_timeseries') @FuncPipeline(inputspec_fields=['atlas', 'labels', 'modules', 'anat', 'inv_linear_reg_mtrx', 'inv_nonlinear_reg_mtrx', diff --git a/PUMI/utils.py b/PUMI/utils.py index 1344406..3d1b455 100644 --- a/PUMI/utils.py +++ b/PUMI/utils.py @@ -191,6 +191,9 @@ def plot_roi(roi_img, bg_img=None, cut_coords=5, output_file=None, display_mode= Here a mosaic plot with 5 columns is generated by default, the background of every plot is black and the default cmap has been changed. + ATTENTION: If save_img is set to False, then you must close the plot yourself! + Otherwise this could lead to memory leaks! + Returns: plot (nilearn.plotting.displays.OrthoSlicer): Plot object. output_file (str): Path to the saved plot (if save_img is False, None is returned). @@ -204,14 +207,23 @@ def plot_roi(roi_img, bg_img=None, cut_coords=5, output_file=None, display_mode= import os if save_img: + # User wants to save the image directly and doesn't want to only get the plot object to modify it further + # before saving. if output_file is None: - roi_img_name = roi_img.split('/')[-1].split('.')[0] + roi_img_name = roi_img.split('/')[-1].split('.')[0] # Get filename without extension. + # Plot will be saved in current working directory with the filename (without extension) of ROI image. + # but with the suffix '_plot.png' output_file = os.path.join(os.getcwd(), roi_img_name + '_plot.png') else: + # If the user has specified an absolute path, then we can directly use it. + # If we have only received a relative path, then we append this relative path to the current working + # directory. if not output_file.startswith('/'): output_file = os.path.join(os.getcwd(), output_file) else: - output_file = None # make sure that no file is created when save_img=False + output_file = None + # If output_file is None, then nilearn won't save the image. + # In this case, the user must close the plot themself! Otherwise this could lead to memory leaks! plot = plotting.plot_roi(roi_img, bg_img=bg_img, cut_coords=cut_coords, output_file=output_file, display_mode=display_mode, figure=figure, axes=axes, title=title, annotate=annotate, @@ -286,7 +298,9 @@ def create_coregistration_qc(registered_brain, template, output_file=None, level else: if not output_file.startswith('/'): output_file = os.path.join(os.getcwd(), output_file) + plot.savefig(output_file) + plot.close() return output_file