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

SpecFit updates for improvements #34

Merged
merged 63 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
62895f8
changed optimization
theHenks Oct 26, 2023
deaf556
removed plots, added typedtables
theHenks Oct 26, 2023
8a336c0
changed auto calibration to more stables
theHenks Oct 26, 2023
0a22d64
new cut truncated fits
theHenks Oct 26, 2023
1eca08c
updated aoe calibration for dep cut gen
theHenks Oct 26, 2023
a345684
updated package
theHenks Oct 26, 2023
b50f5f5
qc for energy calibration and aoe tuning
theHenks Oct 26, 2023
0782d4a
new routines for simple_calibration
theHenks Oct 26, 2023
1228730
more flexible peak fitting
theHenks Oct 26, 2023
7734bb3
removed types for keywords
theHenks Nov 28, 2023
8ac3d07
new auto calib for peak splits
theHenks Nov 28, 2023
9fef9ed
new ctc routines
theHenks Nov 28, 2023
e776f2e
updated filter optimization routine
theHenks Nov 28, 2023
a7490b4
updated qc cuts for sg and energy
theHenks Nov 28, 2023
d79d84c
renamed filter optimization
theHenks Nov 28, 2023
851ff17
updated specfits
theHenks Nov 28, 2023
c6df787
updated aoe calibration
theHenks Nov 28, 2023
dc598e2
Update BAT version bound
oschulz Nov 18, 2023
98ffd3e
STASH
theHenks Dec 5, 2023
128f0bd
New recipes
theHenks Dec 10, 2023
668c4a6
AoE fit update
theHenks Dec 10, 2023
b53cec8
Add all e filter cuts
theHenks Dec 10, 2023
cde537a
Added calbrate energy
theHenks Dec 10, 2023
fbcc6b5
STASH
theHenks Dec 10, 2023
dd1cf0e
New qc
theHenks Dec 10, 2023
bd1e204
STASH
theHenks Dec 11, 2023
733c65d
Drop remnants of Julia <v1.9 support
oschulz Dec 11, 2023
7b21c35
Require LegendDataManagement
oschulz Dec 11, 2023
56a44a9
Adjust bin ranges of A/E histograms
fhagemann Dec 10, 2023
49d82e5
Set PDFs for A/E to zero outside of the histogram ranges
fhagemann Dec 10, 2023
1ef0bc5
Change function to fit sigma of A/E peaks from exp to sqrt
fhagemann Dec 10, 2023
33c33b1
Fixed calibration and high side cut
theHenks Dec 12, 2023
bd06f46
Update ValueShapes version bound
oschulz Dec 14, 2023
0b00f70
Reformat Project.toml
oschulz Dec 14, 2023
2a96714
Merge branch 'main' into dev
oschulz Dec 14, 2023
05acef5
add p-value for fit_single_peak_th228
LisaSchlueter Jan 29, 2024
416c299
add 2 more p-value calculation alternatives
LisaSchlueter Jan 30, 2024
5815b73
implement iterative fit for cali peaks based on covmat
LisaSchlueter Jan 30, 2024
56ee493
add doc to gof
LisaSchlueter Jan 30, 2024
226d511
iterative_fit: add flag, default is false -> no iterative fit
LisaSchlueter Jan 31, 2024
f4fef01
fwhm uncertainty with covmat
LisaSchlueter Feb 2, 2024
4203bde
function to sample test data from model, run test doesnt need to read…
LisaSchlueter Feb 3, 2024
574f4b2
add distribution package
LisaSchlueter Feb 3, 2024
7c85c10
fix pull request issue, forgot a plot
LisaSchlueter Feb 3, 2024
041fd42
add fast flatten
LisaSchlueter Feb 5, 2024
2acd7a2
add norm. residuals to fit results + cosmetic improvements
LisaSchlueter Feb 5, 2024
c8daa2c
Fixed compat
theHenks Jan 15, 2024
91d559e
Fix docstring
theHenks Feb 6, 2024
4a38bc5
Increased LegendDataManagement package version
theHenks Feb 6, 2024
39249e6
Cleaned dosctrings
theHenks Feb 6, 2024
309605b
Changed some formatting
theHenks Feb 6, 2024
8b70401
Apply suggestions from code review
LisaSchlueter Feb 6, 2024
b4976e1
Apply suggestions from code review
LisaSchlueter Feb 6, 2024
3ae2b5c
Fixed compat
theHenks Jan 15, 2024
72630f0
Fix docstring
theHenks Feb 6, 2024
d937058
Increased LegendDataManagement package version
theHenks Feb 6, 2024
44413c4
add fast flatten
LisaSchlueter Feb 5, 2024
b4fba2a
add norm. residuals to fit results + cosmetic improvements
LisaSchlueter Feb 5, 2024
fc1f130
Fixed deps
theHenks Feb 6, 2024
42e2138
Fixed specfit test
theHenks Feb 6, 2024
cf7bcfa
Merge branch 'dev' into dev
theHenks Feb 6, 2024
7fe0237
Merge pull request #33 from LisaSchlueter/dev
theHenks Feb 6, 2024
62958db
Bug Fix Project.toml
theHenks Feb 6, 2024
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
9 changes: 5 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ BAT = "c0cd4b16-88b7-57fa-983b-ab80aecada7e"
ChangesOfVariables = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
LegendDataManagement = "9feedd95-f0e0-423f-a8dc-de0970eae6b3"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LinearRegression = "92481ed7-9fb7-40fd-80f2-46fd0f076581"
LsqFit = "2fda8390-95c7-5789-9bda-21331edee243"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PropDicts = "4dc08600-4268-439e-8673-d706fafbb426"
RadiationSpectra = "4f207c7e-01da-51d7-a1a0-c8c06dd1d883"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Expand Down Expand Up @@ -55,16 +56,16 @@ ForwardDiff = "0.10"
IntervalSets = "0.7"
InverseFunctions = "0.1"
IrrationalConstants = "0.1, 0.2"
LegendDataManagement = "0.2.7"
LinearAlgebra = "1"
LinearRegression = "0.2"
LsqFit = "0.14, 0.15"
Measurements = "2"
Optim = "1"
Plots = "1"
PropDicts = "0.2"
RadiationSpectra = "0.5"
Random = "1"
RecipesBase = "1"
Requires = "1"
Roots = "2"
SnoopPrecompile = "1"
SpecialFunctions = "0.10, 1, 2"
Expand Down
162 changes: 129 additions & 33 deletions ext/LegendSpecFitsRecipesBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ using StatsBase, LinearAlgebra
legend := :bottomright
@series begin
seriestype := :histogram
# y := report.f_fit.(x)
bins --> 2000
bins --> :fd
# bins --> 2000
normalize --> :pdf
label := "Data"
# label := @sprintf("μ = %s ± %s\nσ = %s ± %s\nn = %d", report.μ, report.μ_err, report.σ, report.σ_err, report.n)
x[x .> cuts.low .&& x .< cuts.high]
# x
end
@series begin
color := :red
label := format("Normal Fit (μ = ({:.2f} ± {:.2f})µs, σ = ({:.2f} ± {:.2f})µs", ustrip.([report.μ, report.μ_err, report.σ, report.σ_err])...)
label := format("Normal Fit (μ = ({:.2f} ± {:.2f}), σ = ({:.2f} ± {:.2f})", ustrip.([report.μ, report.μ_err, report.σ, report.σ_err])...)
lw := 3
ustrip(cuts.low):0.00001:ustrip(cuts.high), t -> report.f_fit(t)
end
Expand Down Expand Up @@ -66,8 +65,8 @@ end
gridlinewidth := 0.5
# xscale := :log10
# yscale := :log10
ylims := (1, 5)
xlims := (1, 5)
ylims := (1, 8)
xlims := (0.5, 5)
@series begin
seriestype := :scatter
u"µs", NoUnits
Expand All @@ -86,41 +85,68 @@ end
end
end

@recipe function f(report::NamedTuple{(:v, :h, :f_fit, :f_sig, :f_lowEtail, :f_bck)}; show_label::Bool)
@recipe function f(report:: NamedTuple{(:wl, :min_sf, :min_sf_err, :a_grid_wl_sg, :sfs, :sfs_err)})
xlabel := "Window Length (ns)"
ylabel := "SEP Surrival Fraction (%)"
grid := :true
gridcolor := :black
gridalpha := 0.2
gridlinewidth := 0.5
ylims := (0, 30)
@series begin
seriestype := :scatter
label := "SF"
yerror --> report.sfs_err
ustrip.(report.a_grid_wl_sg), report.sfs
end
@series begin
seriestype := :hline
label := "Min. SF (WT: $(report.wl))"
color := :red
linewidth := 2.5
[report.min_sf]
end
end

@recipe function f(report::NamedTuple{(:v, :h, :f_fit, :f_sig, :f_lowEtail, :f_bck)}; show_label=true, show_fit=true)
xlabel := "Energy (keV)"
ylabel := "Counts"
legend := :bottomright
yscale := :log10
ylims := (1, 1.2*report.f_sig(report.v.μ))
ylim_max = max(5*report.f_sig(report.v.μ), 5*maximum(report.h.weights))
ylim_max = ifelse(ylim_max == 0.0, 1e5, ylim_max)
ylims := (1, ylim_max)
@series begin
seriestype := :stepbins
label := ifelse(show_label, "Data", "")
bins --> :sqrt
LinearAlgebra.normalize(report.h, mode = :density)
end
@series begin
seriestype := :line
label := ifelse(show_label, "Best Fit", "")
color := :red
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_fit
end
@series begin
seriestype := :line
label := ifelse(show_label, "Signal", "")
color := :green
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_sig
end
@series begin
seriestype := :line
label := ifelse(show_label, "Low Energy Tail", "")
color := :blue
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_lowEtail
end
@series begin
seriestype := :line
label := ifelse(show_label, "Background", "")
color := :black
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_bck
if show_fit
@series begin
seriestype := :line
label := ifelse(show_label, "Best Fit", "")
color := :red
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_fit
end
@series begin
seriestype := :line
label := ifelse(show_label, "Signal", "")
color := :green
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_sig
end
@series begin
seriestype := :line
label := ifelse(show_label, "Low Energy Tail", "")
color := :blue
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_lowEtail
end
@series begin
seriestype := :line
label := ifelse(show_label, "Background", "")
color := :black
minimum(report.h.edges[1]):0.1:maximum(report.h.edges[1]), report.f_bck
end
end
end

Expand Down Expand Up @@ -182,7 +208,7 @@ end
end
end

@recipe function f(report::NamedTuple{(:h_calsimple, :h_uncal, :c, :fep_guess, :peakhists, :peakstats)}; cal::Bool)
@recipe function f(report::NamedTuple{(:h_calsimple, :h_uncal, :c, :fep_guess, :peakhists, :peakstats)}; cal=true)
ylabel := "Counts"
legend := :topright
yscale := :log10
Expand Down Expand Up @@ -216,7 +242,7 @@ end
end
end

@recipe function f(report_ctc::NamedTuple{(:peak, :window, :fct, :bin_width, :bin_width_qdrift, :e_peak, :e_ctc, :qdrift_peak, :h_before, :h_after)})
@recipe function f(report_ctc::NamedTuple{(:peak, :window, :fct, :bin_width, :bin_width_qdrift, :e_peak, :e_ctc, :qdrift_peak, :h_before, :h_after, :fwhm_before, :fwhm_after, :err, :report_before, :report_after)})
layout := (2,2)
thickness_scaling := 2.0
size := (2400, 1600)
Expand Down Expand Up @@ -252,10 +278,33 @@ end
label := "Before CTC"
xlabel := "Energy (keV)"
ylabel := "Counts"
title := "FWHM $(round(report_ctc.fwhm_before, digits=2))±$(round(report_ctc.err.fwhm_before, digits=2))keV"
yscale := :log10
subplot := 3
report_ctc.h_before
end
# @series begin
# # seriestype := :stepbins
# color := :red
# # label := "Before CTC"
# # xlabel := "Energy (keV)"
# # ylabel := "Counts"
# # yscale := :log10
# subplot := 3
# # report_ctc.h_before
# minimum(report_ctc.e_peak):0.001:maximum(report_ctc.e_peak), t -> report_ctc.report_before.f_fit(t)
# end
# @series begin
# # seriestype := :stepbins
# color := :red
# # label := "Before CTC"
# # xlabel := "Energy (keV)"
# # ylabel := "Counts"
# # yscale := :log10
# subplot := 4
# # report_ctc.h_before
# minimum(report_ctc.e_peak):0.001:maximum(report_ctc.e_peak), t -> report_ctc.report_after.f_fit(t)
# end
@series begin
seriestype := :stepbins
color := :red
Expand All @@ -272,11 +321,58 @@ end
label := "After CTC"
xlabel := "Energy (keV)"
ylabel := "Counts"
title := "FWHM $(round(report_ctc.fwhm_after, digits=2))±$(round(report_ctc.err.fwhm_after, digits=2))keV"
yscale := :log10
subplot := 4
report_ctc.h_after
end
end

@recipe function f(report_window_cut::NamedTuple{(:h, :f_fit, :x_fit, :low_cut, :high_cut, :low_cut_fit, :high_cut_fit, :center, :σ)})
xlims := (ustrip(report_window_cut.center - 5*report_window_cut.σ), ustrip(report_window_cut.center + 5*report_window_cut.σ))
@series begin
seriestype := :barbins
alpha := 0.5
label := "Data"
report_window_cut.h
end
@series begin
seriestype := :line
label := "Best Fit"
color := :red
linewidth := 3
report_window_cut.x_fit, report_window_cut.f_fit
end
@series begin
seriestype := :vline
label := "Cut Window"
color := :green
linewidth := 3
ustrip.([report_window_cut.low_cut, report_window_cut.high_cut])
end
# @series begin
# seriestype := :vline
# label := "Center"
# color := :blue
# linewidth := 3
# ustrip.([report_window_cut.center])
# end
@series begin
seriestype := :vline
label := "Fit Window"
color := :orange
linewidth := 3
ustrip.([report_window_cut.low_cut_fit, report_window_cut.high_cut_fit])
end
@series begin
seriestype := :vspan
label := ""
color := :lightgreen
alpha := 0.2
ustrip.([report_window_cut.low_cut, report_window_cut.high_cut])
end

end


end # module LegendSpecFitsRecipesBaseExt
34 changes: 13 additions & 21 deletions src/LegendSpecFits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ using Random

using ArgCheck
using ArraysOfArrays
using BAT
using Distributions
using FillArrays
using ForwardDiff
using IntervalSets
using InverseFunctions
using IrrationalConstants
using LegendDataManagement
using LinearRegression
using LsqFit
using Optim
using PropDicts
using RadiationSpectra
using Roots
using SpecialFunctions
using StatsBase
using StructArrays
using Tables
using TypedTables
using Unitful
using ValueShapes
using IntervalSets
using Roots
using BAT
using LsqFit
using Optim
using ForwardDiff
using LinearRegression
using PropDicts

include("utils.jl")
include("peakshapes.jl")
include("likelihoods.jl")
include("priors.jl")
include("cut.jl")
include("aoefit.jl")
include("optimization.jl")
include("filter_optimization.jl")
include("singlefit.jl")
include("specfit.jl")
include("fwhm.jl")
Expand All @@ -48,18 +50,8 @@ include("auto_calibration.jl")
include("aoe_calibration.jl")
include("specfit_combined.jl")
include("ctc.jl")

# @static if !isdefined(Base, :get_extension)
# using Requires
# include("../ext/LegendSpecFitsRecipesBaseExt.jl")
# end

include("qc.jl")
include("gof.jl")
include("precompile.jl")

function __init__()
@static if !isdefined(Base, :get_extension)
@require BAT = "c0cd4b16-88b7-57fa-983b-ab80aecada7e" include("../ext/LegendSpecFitsBATExt.jl")
end
end

end # module
Loading
Loading