Skip to content

Commit cf06b0c

Browse files
committed
Include Resampler
1 parent 9626426 commit cf06b0c

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

ext/PlottingExt/PlotIntensities.jl

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ function colorrange_from_data(; data, saturation, sensitivity, allpositive)
3737
end
3838
end
3939

40+
# Makie.heatmap can fail on large grids due to an OpenGL error. A workaround is
41+
# to wrap the data in a Makie.Resampler if any of the array dimensions is
42+
# "large" https://github.com/MakieOrg/Makie.jl/issues/4950. The empirical cutoff
43+
# 2000 should (hopefully) be conservative for most systems.
44+
function maybe_resample(data)
45+
all(<(2000), size(data)) ? data : Makie.Resampler(data)
46+
end
47+
4048
"""
4149
plot_intensities!(panel, res; opts...)
4250
@@ -61,11 +69,8 @@ function Sunny.plot_intensities!(panel, res::Sunny.BandIntensities{Float64}; col
6169

6270
if res.qpts isa Sunny.QPath
6371
mindisp, maxdisp = extrema(res.disp)
64-
ebounds = if isnothing(ylims)
65-
(min(0, mindisp), 1.1*maxdisp)
66-
else
67-
ylims .* unit_energy
68-
end
72+
ylims = @something ylims (min(0, mindisp), 1.1*maxdisp) ./ unit_energy
73+
ebounds = ylims .* unit_energy
6974
energies = range(ebounds[1], ebounds[2], 512)
7075
fwhm = @something fwhm 0.02*(ebounds[2]-ebounds[1])
7176
σ = fwhm/2(2log(2))
@@ -77,8 +82,8 @@ function Sunny.plot_intensities!(panel, res::Sunny.BandIntensities{Float64}; col
7782
colorrange = @something colorrange colorrange_suggest
7883

7984
xticklabelrotation = maximum(length.(res.qpts.xticks[2])) > 3 ? π/6 : 0.0
80-
ax = Makie.Axis(panel; xlabel="Momentum (r.l.u.)", ylabel, res.qpts.xticks, xticklabelrotation, axisopts...)
81-
Makie.heatmap!(ax, axes(data, 2), collect(energies/unit_energy), data'; colorrange, colormap, lowclip=:white)
85+
ax = Makie.Axis(panel; xlabel="Momentum (r.l.u.)", ylabel, res.qpts.xticks, xticklabelrotation, limits=(nothing, ylims), axisopts...)
86+
Makie.heatmap!(ax, (1, size(data, 2)), ylims, maybe_resample(collect(data')); colorrange, colormap, lowclip=:white)
8287
for i in axes(res.disp, 1)
8388
Makie.lines!(ax, res.disp[i,:]/unit_energy; color=:lightskyblue3)
8489
end
@@ -138,18 +143,17 @@ function Sunny.plot_intensities!(panel, res::Sunny.Intensities{Float64}; colorma
138143
if qpts isa Sunny.QPath
139144
unit_energy, ylabel = get_unit_energy(units, into)
140145
xticklabelrotation = maximum(length.(qpts.xticks[2])) > 3 ? π/6 : 0.0
141-
ax = Makie.Axis(panel[1, 1]; xlabel="Momentum (r.l.u.)", ylabel, qpts.xticks, xticklabelrotation, axisopts...)
142-
isnothing(ylims) || Makie.ylims!(ax, ylims)
143-
hm = Makie.heatmap!(ax, axes(data, 2), collect(energies/unit_energy), data'; colormap, colorrange)
146+
ax = Makie.Axis(panel[1, 1]; xlabel="Momentum (r.l.u.)", ylabel, qpts.xticks, xticklabelrotation, limits=(nothing, ylims), axisopts...)
147+
hm = Makie.heatmap!(ax, (1, size(data, 2)), extrema(energies)./unit_energy, maybe_resample(data'); colormap, colorrange)
144148
Makie.Colorbar(panel[1, 2], hm)
145149
return ax
146150
elseif qpts isa Sunny.QGrid{2}
147151
if isone(length(energies))
148152
aspect = grid_aspect_ratio(crystal, qpts)
149153
xlabel, ylabel = suggest_labels_for_grid(qpts)
150-
(xs, ys) = map(range, qpts.coefs_lo, qpts.coefs_hi, size(qpts.qs))
154+
(xbounds, ybounds) = zip(qpts.coefs_lo, qpts.coefs_hi)
151155
ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, aspect, axisopts...)
152-
hm = Makie.heatmap!(ax, xs, ys, dropdims(data; dims=1); colormap, colorrange)
156+
hm = Makie.heatmap!(ax, xbounds, ybounds, maybe_resample(dropdims(data; dims=1)); colormap, colorrange)
153157
Makie.Colorbar(panel[1, 2], hm)
154158
return ax
155159
else
@@ -179,9 +183,9 @@ function Sunny.plot_intensities!(panel, res::Sunny.StaticIntensities{Float64}; c
179183
colorrange = @something colorrange colorrange_suggest
180184
aspect = grid_aspect_ratio(crystal, qpts)
181185
xlabel, ylabel = suggest_labels_for_grid(qpts)
182-
(xs, ys) = map(range, qpts.coefs_lo, qpts.coefs_hi, size(qpts.qs))
186+
(xbounds, ybounds) = zip(qpts.coefs_lo, qpts.coefs_hi)
183187
ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, aspect, axisopts...)
184-
hm = Makie.heatmap!(ax, xs, ys, data; colormap, colorrange)
188+
hm = Makie.heatmap!(ax, xbounds, ybounds, maybe_resample(data); colormap, colorrange)
185189
Makie.Colorbar(panel[1, 2], hm)
186190
return ax
187191
else
@@ -200,9 +204,8 @@ function Sunny.plot_intensities!(panel, res::Sunny.PowderIntensities{Float64}; c
200204
colormap = @something colormap (allpositive ? :gnuplot2 : :bwr)
201205
colorrange = @something colorrange colorrange_suggest
202206

203-
ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, axisopts...)
204-
isnothing(ylims) || Makie.ylims!(ylims)
205-
hm = Makie.heatmap!(ax, res.radii, collect(res.energies/unit_energy), res.data'; colormap, colorrange)
207+
ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, limits=(nothing, ylims), axisopts...)
208+
hm = Makie.heatmap!(ax, extrema(res.radii), extrema(res.energies)./unit_energy, maybe_resample(res.data'); colormap, colorrange)
206209
Makie.Colorbar(panel[1, 2], hm)
207210
return ax
208211
end

0 commit comments

Comments
 (0)