Skip to content

Plotting: add save plots option#418

Open
SabbahMohammed wants to merge 1 commit into
LupoLab:masterfrom
SabbahMohammed:save-plots-option
Open

Plotting: add save plots option#418
SabbahMohammed wants to merge 1 commit into
LupoLab:masterfrom
SabbahMohammed:save-plots-option

Conversation

@SabbahMohammed
Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings March 11, 2026 10:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to add an option to save figures produced by Plotting helpers (e.g., propagation plots, 1D slices, spectrograms) to disk via new savename/savepath keyword arguments.

Changes:

  • Add savename/savepath kwargs to multiple plotting functions to enable plt.savefig(...).
  • Close figures after saving in some plotting helpers to reduce open-figure accumulation.
  • Remove several “other stats” items from Plotting.stats output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Plotting.jl
Comment on lines 116 to +121
"""
stats(output; kwargs...)

Plot all statistics available in `output`. Additional `kwargs` are passed onto `plt.plot()`
"""
function stats(output; kwargs...)
function stats(output; savename=nothing, savepath=pwd(), kwargs...)
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring still advertises stats(output; kwargs...), but the function now takes savename and savepath. Please update the docstring signature/keyword-argument documentation so users discover the new save option from the generated docs.

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines 158 to 163
haskey(stats, "pressure") && push!(
fstats, (stats["pressure"], "Pressure (bar)"))
haskey(stats, "dz") && push!(fstats, (1e6*stats["dz"], "Stepsize (μm)"))
haskey(stats, "core_radius") && push!(fstats, (1e6*stats["core_radius"], "Core radius (μm)"))
haskey(stats, "zdw") && push!(fstats, (1e9*stats["zdw"], "ZDW (nm)"))
haskey(stats, "mode_reconstruction_error") && push!(
fstats, (stats["mode_reconstruction_error"], "Mode error"))
haskey(stats, "transverse_points") && push!(
fstats, (stats["transverse_points"], "Transverse grid points"))
haskey(stats, "transverse_integral_error_rel") && push!(
fstats, (stats["transverse_integral_error_rel"], "Transverse integral error (relative)"))


Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is titled about adding a save option, but this hunk also removes plotting of mode_reconstruction_error, transverse_points, and transverse integral error stats (which are still produced by Stats). If this removal is intentional, it should be called out in the PR description/release notes; otherwise, consider restoring these plots.

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines 438 to +442
sfig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

time_1D now always returns nothing because the last expression is the if savename !== nothing block. The preceding sfig line does not affect the return value. Please return sfig (and only then optionally save/close), so callers can still display or further modify the figure.

Suggested change
sfig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
return sfig

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines +183 to +187
if savename !== nothing
savename1 = savename*"_1.png"
plt.savefig(joinpath(savepath, savename1))
plt.close(pfig)
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pfig is only defined when length(pstats) > 0, but it is unconditionally referenced in the savename !== nothing block. If pstats is empty, calling stats(...; savename=...) will throw UndefVarError: pfig not defined. Guard the save/close with Npl > 0 (or isdefined) or initialize pfig = nothing and check before closing/saving.

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines +205 to +209
if savename !== nothing
savename2 = savename*"_2.png"
plt.savefig(joinpath(savepath, savename2))
plt.close(ffig)
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ffig is only created when length(fstats) > 0, but the save/close block runs whenever savename !== nothing. If fstats is empty this will throw UndefVarError: ffig not defined. Please only save/close when the corresponding figure was actually created.

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines 203 to 210
end
[pfig, ffig]

if savename !== nothing
savename2 = savename*"_2.png"
plt.savefig(joinpath(savepath, savename2))
plt.close(ffig)
end
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stats previously returned [pfig, ffig], but it now has no return value (the last evaluated expression is the if savename !== nothing block). This is a breaking API change and will also make REPL usage less convenient. Consider preserving the prior return value (e.g., return figures even if you saved/closed them, or return the saved filenames in addition) so existing callers keep working.

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines 272 to +275
fig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prop_2D now always returns nothing because the final expression in the function is the if savename !== nothing block; the standalone fig line is not the last expression. This breaks existing usage (e.g., REPL will no longer show the figure). Ensure the function returns fig (or the list of figures in multimode) regardless of whether saving is enabled.

Suggested change
fig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
end
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
end
return fig

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines 516 to +520
sfig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec_1D now always returns nothing because the final expression is the if savename !== nothing block. This is a regression from returning the figure object. Please restructure so sfig is returned regardless of savename.

Suggested change
sfig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
sfig

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(fig)
end
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spectrogram(t, Et, ...) now always returns nothing due to the if savename !== nothing block being the final expression. Please return fig regardless of saving, to preserve existing behavior.

Suggested change
end
end
return fig

Copilot uses AI. Check for mistakes.
Comment thread src/Plotting.jl
Comment on lines 230 to +246
"""
prop_2D(output, specaxis=:f)

Make false-colour propagation plots for `output`, using spectral x-axis `specaxis` (see
[`getIω`](@ref)). For multimode simulations, create one figure for each mode plus one for
the sum of all modes.

# Keyword arguments
- `λrange::Tuple(Float64, Float64)` : x-axis limits for spectral plot (wavelength in metres)
- `trange::Tuple(Float64, Float64)` : x-axis limits for time-domain plot (time in seconds)
- `dBmin::Float64` : lower colour-scale limit for logarithmic spectral plot
- `resolution::Real` smooth the spectral energy density as defined by [`getIω`](@ref).
"""
function prop_2D(output, specaxis=:f;
trange=(-50e-15, 50e-15), bandpass=nothing,
λrange=(150e-9, 2000e-9), dBmin=-60,
resolution=nothing, modes=nothing, oversampling=4,
resolution=nothing, modes=nothing, oversampling=4, savename=nothing, savepath=pwd(),
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prop_2D docstring's signature/keyword-argument list doesn't mention the newly added savename/savepath options. Please update the docstring so the generated docs reflect the new API.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants