Skip to content

Commit

Permalink
Merge pull request #188 from pni-lab/fix-regTimeseries
Browse files Browse the repository at this point in the history
Fix problem with timeseries qc images (memory leaking)
  • Loading branch information
spisakt authored Mar 7, 2024
2 parents fbda47b + 90573ca commit d993f82
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions PUMI/pipelines/func/deconfound.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion PUMI/pipelines/func/timeseries_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 16 additions & 2 deletions PUMI/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d993f82

Please sign in to comment.