Skip to content

Commit

Permalink
SpecFit updates for improvements (#34)
Browse files Browse the repository at this point in the history
* changed optimization

* removed plots, added typedtables

* changed auto calibration to more stables

* new cut truncated fits

* updated aoe calibration for dep cut gen

* updated package

* qc for energy calibration and aoe tuning

* new routines for simple_calibration

* more flexible peak fitting

* removed types for keywords

* new auto calib for peak splits

* new ctc routines

* updated filter optimization routine

* updated qc cuts for sg and energy

* renamed filter optimization

* updated specfits

* updated aoe calibration

* Update BAT version bound

* STASH

* New recipes

* AoE fit update

* Add all e filter cuts

* Added calbrate energy

* STASH

* New qc

* STASH

* Drop remnants of Julia <v1.9 support

* Require LegendDataManagement

* Adjust bin ranges of A/E histograms

* Set PDFs for A/E to zero outside of the histogram ranges

* Change function to fit sigma of A/E peaks from exp to sqrt

* Fixed calibration and high side cut

* Update ValueShapes version bound

* Reformat Project.toml

* add p-value for fit_single_peak_th228

* add 2 more p-value calculation alternatives

* implement iterative fit for cali peaks based on covmat

* add doc to gof

* iterative_fit: add flag, default is false -> no iterative fit

* fwhm uncertainty with covmat

* function to sample test data from model, run test doesnt need to read external data anymore

* add distribution package

* fix pull request issue, forgot a plot

* add fast flatten

* add norm. residuals to fit results + cosmetic improvements

* Fixed compat

* Fix docstring

* Increased LegendDataManagement package version

* Cleaned dosctrings

* Changed some formatting

* Apply suggestions from code review

remove unnecessary packages from Project.toml

Co-authored-by: Florian <[email protected]>

* Apply suggestions from code review

remove unnecessary exports

Co-authored-by: Florian <[email protected]>

* Fixed compat

* Fix docstring

* Increased LegendDataManagement package version

* add fast flatten

* add norm. residuals to fit results + cosmetic improvements

* Fixed deps

* Fixed specfit test

* Bug Fix Project.toml

---------

Co-authored-by: Oliver Schulz <[email protected]>
Co-authored-by: Felix Hagemann <[email protected]>
Co-authored-by: LisaSchlueter <[email protected]>
Co-authored-by: Lisa Schlueter <[email protected]>
  • Loading branch information
5 people authored Feb 6, 2024
1 parent 64fd7f1 commit 3a1c5b5
Show file tree
Hide file tree
Showing 19 changed files with 1,333 additions and 479 deletions.
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

0 comments on commit 3a1c5b5

Please sign in to comment.