Skip to content

Commit 97b4cb7

Browse files
committed
Merge branch '256_plot_finalize' into 'dev'
Shared finalize API for all return_fig plot methods See merge request mass-spectrometry/corems!239
2 parents e3a438f + 30320c7 commit 97b4cb7

6 files changed

Lines changed: 414 additions & 119 deletions

File tree

corems/chroma_peak/factory/chroma_peak_classes.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
GCPeakCalculation,
1111
LCMSMassFeatureCalculation,
1212
)
13+
from corems.encapsulation.plot_utils import _finalize_plot
1314
from corems.mass_spectra.factory.chromat_data import EIC_Data
1415
from corems.molecular_id.factory.EI_SQL import LowResCompoundRef
1516

@@ -594,6 +595,7 @@ def plot(
594595
self,
595596
to_plot=["EIC", "MS1", "MS2"],
596597
return_fig=True,
598+
path=None,
597599
plot_smoothed_eic=False,
598600
plot_eic_datapoints=False,
599601
molecular_metadata=None,
@@ -608,7 +610,11 @@ def plot(
608610
"EIC", "MS2", "MS2_mirror", and "MS1".
609611
Default is ["EIC", "MS1", "MS2"].
610612
return_fig : bool, optional
611-
If True, the figure is returned. Default is True.
613+
If True, return the open figure (caller owns lifecycle).
614+
Default is True.
615+
path : str or path-like, optional
616+
If set, save the figure to this path. When ``return_fig`` is False,
617+
the figure is closed after saving and ``plt.show()`` is not called.
612618
plot_smoothed_eic : bool, optional
613619
If True, the smoothed EIC is plotted. Default is False.
614620
plot_eic_datapoints : bool, optional
@@ -623,8 +629,9 @@ def plot(
623629
Returns
624630
-------
625631
matplotlib.figure.Figure or None
626-
The figure object if `return_fig` is True.
627-
Otherwise None and the figure is displayed.
632+
The figure object if `return_fig` is True (left open for the caller).
633+
Otherwise None; the figure is displayed with ``plt.show()`` when
634+
``path`` is not set.
628635
"""
629636
# Adjust to_plot list if there are not spectra added to the mass features
630637
if self.mass_spectrum is None:
@@ -681,10 +688,7 @@ def plot(
681688
# Add space between subplots
682689
plt.tight_layout()
683690

684-
if return_fig:
685-
# Close figure
686-
plt.close(fig)
687-
return fig
691+
return _finalize_plot(fig, return_fig=return_fig, path=path)
688692

689693
@property
690694
def mz(self):

corems/encapsulation/plot_utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Shared plotting helpers for CoreMS figure lifecycle management."""
2+
3+
from __future__ import annotations
4+
5+
import matplotlib.pyplot as plt
6+
7+
8+
def _finalize_plot(fig, return_fig=False, path=None, **savefig_kwargs):
9+
"""Shared exit path for CoreMS plot methods that support ``return_fig``.
10+
11+
Parameters
12+
----------
13+
fig : matplotlib.figure.Figure
14+
Figure to finalize.
15+
return_fig : bool, optional
16+
If True, leave the figure open and return it. The caller owns the
17+
figure lifecycle (e.g. further customization or ``plt.close(fig)``).
18+
Default is False.
19+
path : str or path-like, optional
20+
If set, save the figure to this path via ``fig.savefig`` before
21+
showing or returning.
22+
**savefig_kwargs
23+
Forwarded to ``fig.savefig`` when ``path`` is set.
24+
25+
Returns
26+
-------
27+
matplotlib.figure.Figure or None
28+
The open figure if ``return_fig`` is True; otherwise None.
29+
30+
Notes
31+
-----
32+
Behavior matrix:
33+
34+
- ``return_fig=False``, ``path=None``: call ``plt.show()`` (interactive /
35+
notebook default).
36+
- ``return_fig=False``, ``path`` set: save, close the figure, do **not**
37+
call ``plt.show()`` (batch / headless friendly).
38+
- ``return_fig=True``: optionally save if ``path`` is set; return the open
39+
figure without showing or closing it.
40+
"""
41+
if path is not None:
42+
fig.savefig(path, **savefig_kwargs)
43+
44+
if return_fig:
45+
return fig
46+
47+
if path is None:
48+
plt.show()
49+
else:
50+
plt.close(fig)
51+
return None

0 commit comments

Comments
 (0)