Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/Plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ end

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...)
stats = output["stats"]

pstats = [] # pulse statistics
Expand Down Expand Up @@ -160,13 +160,6 @@ function stats(output; kwargs...)
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)"))


z = stats["z"]*1e2

Expand All @@ -187,6 +180,11 @@ function stats(output; kwargs...)
end
pfig.tight_layout()
end
if savename !== nothing
savename1 = savename*"_1.png"
plt.savefig(joinpath(savepath, savename1))
plt.close(pfig)
end
Comment on lines +183 to +187
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.

Npl = length(fstats)
if Npl > 0
Expand All @@ -203,7 +201,12 @@ function stats(output; kwargs...)
end
ffig.tight_layout()
end
[pfig, ffig]

if savename !== nothing
savename2 = savename*"_2.png"
plt.savefig(joinpath(savepath, savename2))
plt.close(ffig)
end
Comment on lines +205 to +209
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.
end
Comment on lines 203 to 210
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.

"""
Expand Down Expand Up @@ -240,7 +243,7 @@ the sum of all modes.
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(),
kwargs...)
z = output["z"]*1e2
if specaxis == :λ
Expand All @@ -267,6 +270,9 @@ function prop_2D(output, specaxis=:f;
kwargs...)
end
fig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
end
Comment on lines 272 to +275
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.
end

modeidcs(m::Int, ml) = [m]
Expand Down Expand Up @@ -379,7 +385,7 @@ Other `kwargs` are passed onto `plt.plot`.
function time_1D(output, zslice=maximum(output["z"]);
y=:Pt, modes=nothing,
oversampling=4, trange=(-50e-15, 50e-15), bandpass=nothing,
FTL=false, propagate=nothing,
FTL=false, propagate=nothing, savename=nothing, savepath=pwd(),
kwargs...)
t, Et, zactual = getEt(output, zslice,
trange=trange, oversampling=oversampling, bandpass=bandpass,
Expand Down Expand Up @@ -430,6 +436,10 @@ function time_1D(output, zslice=maximum(output["z"]);
sfig.set_size_inches(8.5, 5)
sfig.tight_layout()
sfig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
Comment on lines 438 to +442
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.
end

# Automatically find power unit depending on scale of electric field.
Expand Down Expand Up @@ -461,7 +471,7 @@ Other `kwargs` are passed onto `plt.plot`.
"""
function spec_1D(output, zslice=maximum(output["z"]), specaxis=:λ;
modes=nothing, λrange=(150e-9, 1200e-9),
log10=true, log10min=1e-6, resolution=nothing,
log10=true, log10min=1e-6, resolution=nothing, savename=nothing, savepath=pwd(),
kwargs...)
if specaxis == :λ
specx, Iω, zactual = getIω(output, specaxis, zslice, specrange=λrange, resolution=resolution)
Expand Down Expand Up @@ -504,6 +514,10 @@ function spec_1D(output, zslice=maximum(output["z"]), specaxis=:λ;
sfig.set_size_inches(8.5, 5)
sfig.tight_layout()
sfig
if savename !== nothing
plt.savefig(joinpath(savepath, savename))
plt.close(sfig)
end
Comment on lines 516 to +520
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.
end

dashes = [(0, (10, 1)),
Expand Down Expand Up @@ -541,7 +555,7 @@ function spectrogram(grid::Grid.AbstractGrid, output, zslice, specaxis=:λ;
end

function spectrogram(t::AbstractArray, Et::AbstractArray, specaxis=:λ;
trange, N, fw, λrange=(150e-9, 2000e-9), log=false, dBmin=-40,
trange, N, fw, λrange=(150e-9, 2000e-9), log=false, dBmin=-40, savename=nothing, savepath=pwd(),
kwargs...)
ω = Maths.rfftfreq(t)[2:end]
tmin, tmax = extrema(trange)
Expand All @@ -562,6 +576,10 @@ function spectrogram(t::AbstractArray, Et::AbstractArray, specaxis=:λ;
log && plt.clim(dBmin, 0)
plt.colorbar()
fig
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.
end

function energy(output; modes=nothing, bandpass=nothing, figsize=(7, 5))
Expand Down
Loading