From 7d70db155fd7b4680d758e5b5c73a27394e30667 Mon Sep 17 00:00:00 2001 From: Kevin Hoffschlag <72939508+khoffschlag@users.noreply.github.com> Date: Thu, 8 Feb 2024 15:24:33 +0100 Subject: [PATCH 1/5] close figure in fieldmap_correction_qc --- PUMI/pipelines/func/deconfound.py | 1 + 1 file changed, 1 insertion(+) 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) From bd35657eb3b8b23d78e3ece2b1c0ffd66342c8db Mon Sep 17 00:00:00 2001 From: Kevin Hoffschlag <72939508+khoffschlag@users.noreply.github.com> Date: Thu, 8 Feb 2024 15:25:53 +0100 Subject: [PATCH 2/5] Rename regTimeseriesQC folder to qc_timeseries --- PUMI/pipelines/func/timeseries_extractor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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', From 357891be7f68df1594cb9fa3084b7a54039f9e62 Mon Sep 17 00:00:00 2001 From: Kevin Hoffschlag <72939508+khoffschlag@users.noreply.github.com> Date: Thu, 8 Feb 2024 15:57:27 +0100 Subject: [PATCH 3/5] Close figure in create_coregistration_qc --- PUMI/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PUMI/utils.py b/PUMI/utils.py index 1344406..8628ca5 100644 --- a/PUMI/utils.py +++ b/PUMI/utils.py @@ -286,7 +286,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 From f63e7d7897a892d73276ff79705bf0397c3655c4 Mon Sep 17 00:00:00 2001 From: Kevin Hoffschlag <72939508+khoffschlag@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:01:25 +0100 Subject: [PATCH 4/5] Add attention statement to plot_roi method --- PUMI/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PUMI/utils.py b/PUMI/utils.py index 8628ca5..9feb18b 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). From 90573ca233698d2064d5caaee60ac8daa134c4c7 Mon Sep 17 00:00:00 2001 From: Kevin Hoffschlag <72939508+khoffschlag@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:09:05 +0100 Subject: [PATCH 5/5] Add comments to our plot_roi wrapper --- PUMI/utils.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/PUMI/utils.py b/PUMI/utils.py index 9feb18b..3d1b455 100644 --- a/PUMI/utils.py +++ b/PUMI/utils.py @@ -207,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,