POSYDON-code/POSYDON

[BUG]: plot_popsyn_over_grid_slice(..., save_fig=False) still saves internally through plot2D

开放

#852 创建于 2026年6月17日

 (0 条评论) (0 个反应) (0 位负责人)Python (35 个派生)auto 404
P2 - medium prioritybuggood first issue

仓库指标

星标
 (42 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

Note: The below bug report text was generated by AI under my supervision

Describe the bug

Calling TransientPopulation.plot_popsyn_over_grid_slice(..., save_fig=False) still appears to trigger an internal save through the lower-level PSyGrid.plot2D / plot2D.__call__ path.

In other words, save_fig=False suppresses the outer save in plot_popsyn_over_grid_slice, but the lower-level grid plotting call still receives a non-None filename via PLOT_PROPERTIES["fname"]. This causes plot2D.__call__ to run fig.savefig(...) anyway.

This is surprising because I would expect save_fig=False to disable all saving associated with this top-level plotting call.

To Reproduce

Minimal example:

BBH_mergers.plot_popsyn_over_grid_slice(
    "HMS-HMS",
    1e-4,
    slices=[0.7],
    save_fig=False,
)

In my case, this fails because the internal fig.savefig(...) triggers a matplotlib render with LaTeX text rendering enabled, and my environment does not have a complete LaTeX installation:

RuntimeError: latex was not able to process the following string:
b'lp'

! LaTeX Error: File `type1cm.sty' not found.

The relevant part of the traceback is:

File .../posydon/popsyn/synthetic_population.py:2291, in TransientPopulation.plot_popsyn_over_grid_slice(...)
    plot_pop.plot_popsyn_over_grid_slice(...)

File .../posydon/visualization/plot_pop.py:260, in plot_popsyn_over_grid_slice(...)
    plot_grid_slice(...)

File .../posydon/visualization/plot_pop.py:499, in plot_grid_slice(...)
    grid.plot2D(..., **PLOT_PROPERTIES)

File .../posydon/grids/psygrid.py:1928, in PSyGrid.plot2D(...)
    plot()

File .../posydon/visualization/plot2D.py:476, in plot2D.__call__(...)
    fig.savefig(self.path_to_file + self.fname,
                dpi=self.dpi, bbox_inches=self.bbox_inches)

I believe the issue is that plot_popsyn_over_grid_slice sets PLOT_PROPERTIES["fname"] regardless of the value of save_fig, and plot2D.__call__ saves whenever self.fname is not None.

A workaround that fixed the issue locally was to monkeypatch plot_grid_slice so that it passes fname=None into grid.plot2D:

import posydon.visualization.plot_pop as plot_pop

_orig_plot_grid_slice = plot_pop.plot_grid_slice

def plot_grid_slice_no_internal_save(
    grid,
    slice_3D_var_str,
    slice_3D_var_range,
    termination_flag="combined_TF12",
    PLOT_PROPERTIES=None,
):
    if PLOT_PROPERTIES is None:
        PLOT_PROPERTIES = {}
    else:
        PLOT_PROPERTIES = dict(PLOT_PROPERTIES)

    PLOT_PROPERTIES["fname"] = None
    PLOT_PROPERTIES["show_fig"] = False
    PLOT_PROPERTIES["close_fig"] = False

    return _orig_plot_grid_slice(
        grid,
        slice_3D_var_str,
        slice_3D_var_range,
        termination_flag=termination_flag,
        PLOT_PROPERTIES=PLOT_PROPERTIES,
    )

plot_pop.plot_grid_slice = plot_grid_slice_no_internal_save

BBH_mergers.plot_popsyn_over_grid_slice(
    "HMS-HMS",
    1e-4,
    slices=[0.7],
    save_fig=False,
    show_fig=False,
    close_fig=False,
)

plot_pop.plot_grid_slice = _orig_plot_grid_slice

Expected behavior

With save_fig=False, I would expect plot_popsyn_over_grid_slice not to save any figures, including through lower-level calls to grid.plot2D.

Specifically, the top-level save_fig argument should either:

  1. prevent PLOT_PROPERTIES["fname"] from being set, or
  2. set PLOT_PROPERTIES["fname"] = None before calling grid.plot2D, or
  3. pass an explicit save flag down to plot2D.

Screenshots

Not applicable. The issue is triggered before a figure is successfully rendered in my environment.

POSYDON Version:

I am using POSYDON v2.2.8 installed via conda

System Configuration (please complete the following information):

  • Operating System: Linux
  • HPC scheduler: Not applicable for this plotting call / not run through a scheduler
  • Python: 3.11
  • Matplotlib: using notebook/inline rendering

Additional context

This originally surfaced as a LaTeX-related matplotlib error because POSYDON’s plotting style enables text.usetex. However, the underlying issue appears to be independent of LaTeX: even when the user passes save_fig=False, plot2D.__call__ still reaches fig.savefig(...) because self.fname is not None.

A possible fix would be to add something like this before the call to plot_grid_slice inside plot_popsyn_over_grid_slice:

if not save_fig:
    PLOT_PROPERTIES["fname"] = None

or to otherwise propagate the top-level save_fig=False setting to the lower-level plot2D call.

贡献者指南