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

Add lplot! for A/E ctc plot #7

Merged
merged 2 commits into from
Feb 25, 2025
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
KernelDensity = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LegendDataManagement = "9feedd95-f0e0-423f-a8dc-de0970eae6b3"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Expand All @@ -21,7 +22,7 @@ TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
LegendMakieExt = ["Makie", "Dates", "FileIO", "Format", "LaTeXStrings", "MathTeXEngine", "StatsBase", "Unitful"]
LegendMakieExt = ["Makie", "Dates", "FileIO", "Format", "KernelDensity", "LaTeXStrings", "MathTeXEngine", "StatsBase", "Unitful"]
LegendMakieLegendDataManagementExt = ["Makie", "LegendDataManagement", "Format", "Measurements", "PropDicts", "TypedTables", "Unitful"]
LegendMakieMeasurementsExt = ["Makie", "LaTeXStrings", "Measurements"]
LegendMakieRadiationDetectorSignalsExt = "RadiationDetectorSignals"
Expand All @@ -30,6 +31,7 @@ LegendMakieRadiationDetectorSignalsExt = "RadiationDetectorSignals"
Dates = "1"
FileIO = "1"
Format = "1"
KernelDensity = "0.6"
LaTeXStrings = "1.2"
LegendDataManagement = "0.4"
Makie = "0.22"
Expand Down
1 change: 1 addition & 0 deletions ext/LegendMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module LegendMakieExt
import Dates
import FileIO
import Format
import KernelDensity
import LaTeXStrings
import Makie
import MathTeXEngine
Expand Down
52 changes: 52 additions & 0 deletions ext/recipes/lplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,58 @@ function LegendMakie.lplot!(
fig
end

function LegendMakie.lplot!(
report::NamedTuple{(:peak, :window, :fct, :bin_width, :bin_width_qdrift, :aoe_peak, :aoe_ctc, :qdrift_peak, :h_before, :h_after, :σ_before, :σ_after, :report_before, :report_after)},
label_before = "Before correction", label_after = "After correction", watermark::Bool = true, kwargs...
)

# Best results for figsize (600,600)
fig = Makie.current_figure()

g = Makie.GridLayout(fig[1,1])

ax = Makie.Axis(g[1,1], limits = (-9,5,0,nothing), ylabel = "Counts / $(round(step(first(report.h_before.edges)), digits = 2))")
Makie.plot!(ax, report.h_before, color = :darkgrey, label = label_before)
Makie.plot!(ax, report.h_after, color = (:purple, 0.5), label = label_after)
Makie.axislegend(position = :lt)

ax2 = Makie.Axis(g[2,1], limits = (-9,5,0,11.5), xticks = -8:2:5, yticks = 0:2:10, xlabel = "A/E classifier", ylabel = "Qdrift / E")
k_before = KernelDensity.kde((report.aoe_peak, report.qdrift_peak))
k_after = KernelDensity.kde((report.aoe_ctc, report.qdrift_peak))
Makie.contourf!(ax2, k_before.x, k_before.y, k_before.density, levels = 15, colormap = :binary)
Makie.contour!(ax2, k_before.x, k_before.y, k_before.density, levels = 15 - 1, color = :white)
Makie.contour!(ax2, k_after.x, k_after.y, k_after.density, levels = 15 - 1, colormap = :plasma)
Makie.lines!(ax2, [0], [0], label = label_before, color = :darkgrey)
Makie.lines!(ax2, [0], [0], label = label_after, color = (:purple, 0.5))
Makie.axislegend(position = :lb)

ax3 = Makie.Axis(g[2,2], limits = (0,nothing,0,11.5), xlabel = "Counts / 0.1")
Makie.plot!(ax3, StatsBase.fit(StatsBase.Histogram, report.qdrift_peak, 0:0.1:11.5), color = :darkgrey, label = "Before correction", direction = :x)
ax3.xticks = Makie.WilkinsonTicks(3, k_min = 3, k_max=4)

# Formatting
Makie.linkxaxes!(ax,ax2)
Makie.hidexdecorations!(ax)
Makie.rowgap!(g, 0)
Makie.rowsize!(g, 1, Makie.Auto(0.5))
Makie.linkyaxes!(ax2,ax3)
Makie.hideydecorations!(ax3)
Makie.colgap!(g, 0)
Makie.colsize!(g, 2, Makie.Auto(0.5))
xspace = maximum(Makie.tight_xticklabel_spacing!, (ax2, ax3))
ax2.xticklabelspace = xspace
ax3.xticklabelspace = xspace
yspace = maximum(Makie.tight_yticklabel_spacing!, (ax, ax2))
ax.yticklabelspace = yspace
ax2.yticklabelspace = yspace

# add watermarks
Makie.current_axis!(ax3)
watermark && LegendMakie.add_watermarks!(; kwargs...)

fig
end


function LegendMakie.lplot!(args...; watermark::Bool = false, kwargs...)

Expand Down
10 changes: 10 additions & 0 deletions test/test_lplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ using Test
@test_nowarn lplot(report_cut, figsize = (750,400), title = "Test")
end

@testset "A/E ctc correlation plot" begin
# generate fake A/E and Qdrift/E distribution
E0 = 550u"keV"
e_cal = fill(E0, 10_000)
aoe_corr = vcat(-rand(Distributions.Exponential(5.0), 2_000), zeros(8_000)) .+ randn(10_000)
qdrift_e = max.(0, randn(10_000) .+ 5)
result_aoe_ctc, report_aoe_ctc = LegendSpecFits.ctc_aoe(aoe_corr, e_cal, qdrift_e, [E0])
lplot(report_aoe_ctc, figsize = (600,600))
end

@testset "Parameter plots" begin
l200 = LegendDataManagement.LegendData(:l200)
filekey = LegendDataManagement.start_filekey(l200, :p02, :r000, :cal)
Expand Down