Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Colorbar more reliable #315

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions docs/src/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
(In development)

* Fix error in `SampledCorrelations` with a coarse ``𝐪``-grid. ([PR
#314](https://github.com/SunnySuite/Sunny.jl/pull/314))
#314](https://github.com/SunnySuite/Sunny.jl/pull/314)).
* Fix colorbar in `plot_intensities!` when all data is uniform ([PR
#315](https://github.com/SunnySuite/Sunny.jl/pull/315)).

## v0.7.1
(Sep 3, 2024)

* Fix crash in `plot_intensities!` when all data is uniform.
* Correctness fix for scalar biquadratic interactions specified with option
`biquad` to [`set_exchange!`](@ref).
* Prototype implementation of entangled units.
Expand Down
30 changes: 22 additions & 8 deletions ext/PlottingExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1013,11 +1013,25 @@ function colorrange_from_data(; data, saturation, sensitivity, allpositive)
cmax = Statistics.quantile(vec(maximum(data; dims=1)), saturation)
cmin = Statistics.quantile(vec(minimum(data; dims=1)), 1 - saturation)

# The returned (lo, hi) should satisfy lo < hi in all cases to allow for a
# meaningful Makie.Colorbar.
if allpositive
return (sensitivity, 1) .* cmax
# Intensities are supposed to be non-negative in this branch, but might
# not be due to any of: User error, very small roundative around
# negative zero, or Gibbs ringing in a KPM calculation.
@assert 0 <= sensitivity < 1
if iszero(abs(cmax))
return (0, 1e-15)
else
(sensitivity, 1) .* abs(cmax)
end
else
cabsmax = max(abs(cmax), abs(cmin))
return (-cabsmax, cabsmax)
cscale = max(abs(cmax), abs(cmin))
if iszero(cscale)
return (-1e-15, 1e-15)
else
return (-cscale, cscale)
end
end
end

Expand Down Expand Up @@ -1113,7 +1127,7 @@ function Sunny.plot_intensities!(panel, res::Sunny.Intensities{Float64}; colorma
xticklabelrotation = maximum(length.(qpts.xticks[2])) > 3 ? π/6 : 0.0
ax = Makie.Axis(panel[1, 1]; xlabel="Momentum (r.l.u.)", ylabel, qpts.xticks, xticklabelrotation, axisopts...)
hm = Makie.heatmap!(ax, axes(data, 2), collect(energies/unit_energy), data'; colormap, colorrange)
allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm)
Makie.Colorbar(panel[1, 2], hm)
return ax
elseif qpts isa Sunny.QGrid{2}
if isone(length(energies))
Expand All @@ -1122,7 +1136,7 @@ function Sunny.plot_intensities!(panel, res::Sunny.Intensities{Float64}; colorma
(xs, ys) = map(range, qpts.coefs_lo, qpts.coefs_hi, size(qpts.qs))
ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, aspect, axisopts...)
hm = Makie.heatmap!(ax, xs, ys, dropdims(data; dims=1); colormap, colorrange)
allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm)
Makie.Colorbar(panel[1, 2], hm)
return ax
else
error("Cannot yet plot $(typeof(res))")
Expand All @@ -1139,14 +1153,14 @@ function Sunny.plot_intensities!(panel, res::Sunny.StaticIntensities{Float64}; c
colorrange_suggest = colorrange_from_data(; data=reshape(data, 1, size(data)...), saturation, sensitivity=0, allpositive)
colormap = @something colormap (allpositive ? :gnuplot2 : :bwr)
colorrange = @something colorrange colorrange_suggest

if qpts isa Sunny.QGrid{2}
aspect = grid_aspect_ratio(crystal, qpts)
xlabel, ylabel = suggest_labels_for_grid(qpts)
(xs, ys) = map(range, qpts.coefs_lo, qpts.coefs_hi, size(qpts.qs))
ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, aspect, axisopts...)
hm = Makie.heatmap!(ax, xs, ys, data; colormap, colorrange)
allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm)
Makie.Colorbar(panel[1, 2], hm)
return ax
elseif qpts isa Sunny.QPath
xticklabelrotation = maximum(length.(qpts.xticks[2])) > 3 ? π/6 : 0.0
Expand All @@ -1170,7 +1184,7 @@ function Sunny.plot_intensities!(panel, res::Sunny.PowderIntensities{Float64}; c

ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, axisopts...)
hm = Makie.heatmap!(ax, res.radii, collect(res.energies/unit_energy), res.data'; colormap, colorrange)
allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm)
Makie.Colorbar(panel[1, 2], hm)
return ax
end

Expand Down
Loading