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

Peakwise fit function for calibration peak fits #72

Merged
merged 7 commits into from
Oct 6, 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
27 changes: 17 additions & 10 deletions src/pseudo_prior.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@
μ = ifelse(fixed_position, ConstValueDist(ps.peak_pos), Normal(ps.peak_pos, 0.2*ps.peak_sigma)),
σ = weibull_from_mx(ps.peak_sigma, 1.5*ps.peak_sigma),
n = weibull_from_mx(ps.peak_counts, 1.5*ps.peak_counts),
step_amplitude = weibull_from_mx(ps.mean_background_step, ps.mean_background_step + 5*ps.mean_background_std),
skew_fraction = ifelse(low_e_tail, truncated(weibull_from_mx(0.002, 0.008), 0.0, 0.5), ConstValueDist(0.0)),
skew_width = ifelse(low_e_tail, weibull_from_mx(ps.peak_sigma/ps.peak_pos, 2*ps.peak_sigma/ps.peak_pos), ConstValueDist(1.0)),
background = weibull_from_mx(ps.mean_background, ps.mean_background + 5*ps.mean_background_std),
)
if fit_func == :f_fit
pprior_step = NamedTupleDist(step_amplitude = weibull_from_mx(ps.mean_background_step, ps.mean_background_step + 5*ps.mean_background_std),)
if fit_func == :gamma_def
return merge(pprior_base, pprior_step)
elseif fit_func == :gamma_bckFlat

Check warning on line 13 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L13

Added line #L13 was not covered by tests
return pprior_base
elseif fit_func == :gamma_tails || fit_func == :gamma_tails_bckFlat
pprior_tails = merge(pprior_base, NamedTupleDist(

Check warning on line 16 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L15-L16

Added lines #L15 - L16 were not covered by tests
skew_fraction_highE = ifelse(low_e_tail, truncated(weibull_from_mx(0.01, 0.05), 0.0, 0.1), ConstValueDist(0.0)),
skew_width_highE = ifelse(low_e_tail, weibull_from_mx(0.001, 1e-2), ConstValueDist(1.0)),)
)
if fit_func == :gamma_tails
return merge(pprior_tails, pprior_step)

Check warning on line 21 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L20-L21

Added lines #L20 - L21 were not covered by tests
else
return pprior_tails

Check warning on line 23 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L23

Added line #L23 was not covered by tests
end
else
window_left = ps.peak_pos - minimum(h.edges[1])
window_right = maximum(h.edges[1]) - ps.peak_pos
return merge(pprior_base,
if fit_func == :f_fit_bckSlope
return merge(pprior_base, pprior_step,

Check warning on line 28 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L28

Added line #L28 was not covered by tests
if fit_func == :gamma_bckSlope
NamedTupleDist(
background_slope = ifelse(ps.mean_background < 5, ConstValueDist(0),
truncated(Normal(0, 0.1*ps.mean_background_std / (window_left + window_right)), - ps.mean_background / window_right, 0)),
)
elseif fit_func ==:f_fit_bckExp
elseif fit_func ==:gamma_bckExp

Check warning on line 34 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L34

Added line #L34 was not covered by tests
NamedTupleDist(background_exp = weibull_from_mx(3e-2, 5e-2))
elseif fit_func == :f_fit_tails
NamedTupleDist(
skew_fraction_highE = ifelse(low_e_tail, truncated(weibull_from_mx(0.01, 0.05), 0.0, 0.1), ConstValueDist(0.0)),
skew_width_highE = ifelse(low_e_tail, weibull_from_mx(0.001, 1e-2), ConstValueDist(1.0)),
)
end
)
end
Expand Down
23 changes: 13 additions & 10 deletions src/specfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,32 @@
export fit_peaks

function fit_peaks_th228(peakhists::Array, peakstats::StructArray, th228_lines::Vector{T},; e_unit::Union{Nothing, Unitful.EnergyUnits}=nothing, uncertainty::Bool=true, low_e_tail::Bool=true, iterative_fit::Bool=false,
fit_func::Symbol= :f_fit, pseudo_prior::NamedTupleDist=NamedTupleDist(empty = true), m_cal_simple::MaybeWithEnergyUnits = 1.0) where T<:Any

fit_func::Vector{Symbol}= fill(:gamma_def, length(th228_lines)), pseudo_prior::NamedTupleDist=NamedTupleDist(empty = true), m_cal_simple::MaybeWithEnergyUnits = 1.0) where T<:Any

e_unit = ifelse(isnothing(e_unit), NoUnits, e_unit)

# create return and result dicts
v_result = Vector{NamedTuple}(undef, length(th228_lines))
v_report = Vector{NamedTuple}(undef, length(th228_lines))


# iterate throuh all peaks
Threads.@threads for i in eachindex(th228_lines)
# get histogram and peakstats
peak = th228_lines[i]
h = peakhists[i]
ps = peakstats[i]
f = fit_func[i]

# fit peak
result_peak, report_peak = fit_single_peak_th228(h, ps; uncertainty=uncertainty, low_e_tail=low_e_tail, fit_func = fit_func, pseudo_prior = pseudo_prior)
result_peak, report_peak = fit_single_peak_th228(h, ps; uncertainty=uncertainty, low_e_tail=low_e_tail, fit_func = f, pseudo_prior = pseudo_prior)

# check covariance matrix for being semi positive definite (no negative uncertainties)
if uncertainty
if iterative_fit && !isposdef(result_peak.covmat)
@warn "Covariance matrix not positive definite for peak $peak - repeat fit without low energy tail"
pval_save = result_peak.pval
result_peak, report_peak = fit_single_peak_th228(h, ps, ; uncertainty=uncertainty, low_e_tail=false, fit_func = fit_func, pseudo_prior = pseudo_prior)
result_peak, report_peak = fit_single_peak_th228(h, ps, ; uncertainty=uncertainty, low_e_tail=false, fit_func = f, pseudo_prior = pseudo_prior)

Check warning on line 50 in src/specfit.jl

View check run for this annotation

Codecov / codecov/patch

src/specfit.jl#L50

Added line #L50 was not covered by tests
@info "New covariance matrix is positive definite: $(isposdef(result_peak.covmat))"
@info "p-val with low-energy tail p=$(round(pval_save,digits=5)) , without low-energy tail: p=$(round((result_peak.pval),digits=5))"
end
Expand Down Expand Up @@ -76,7 +78,7 @@
"""
function fit_single_peak_th228(h::Histogram, ps::NamedTuple{(:peak_pos, :peak_fwhm, :peak_sigma, :peak_counts, :bin_width, :mean_background, :mean_background_step, :mean_background_std), NTuple{8, T}};
uncertainty::Bool=true, low_e_tail::Bool=true, fixed_position::Bool=false, pseudo_prior::NamedTupleDist=NamedTupleDist(empty = true),
fit_func::Symbol=:f_fit, background_center::Union{Real,Nothing} = ps.peak_pos, m_cal_simple::Real = 1.0) where T<:Real
fit_func::Symbol=:gamma_def, background_center::Union{Real,Nothing} = ps.peak_pos, m_cal_simple::Real = 1.0) where T<:Real
# create standard pseudo priors
pseudo_prior = get_pseudo_prior(h, ps, fit_func; pseudo_prior = pseudo_prior, fixed_position = fixed_position, low_e_tail = low_e_tail)

Expand Down Expand Up @@ -142,7 +144,8 @@
@debug "FWHM: $(fwhm) ± $(fwhm_err)"

result = merge(NamedTuple{keys(v_ml)}([measurement(v_ml[k], v_ml_err[k]) for k in keys(v_ml)]...),
(fwhm = measurement(fwhm, fwhm_err),
(fwhm = measurement(fwhm, fwhm_err),
fit_func = fit_func,
gof = (pvalue = pval,
chi2 = chi2,
dof = dof,
Expand Down Expand Up @@ -170,7 +173,7 @@
@debug "FWHM: $(fwhm)"

result = merge(NamedTuple{keys(v_ml)}([measurement(v_ml[k], NaN) for k in keys(v_ml)]...),
(fwhm = measurement(fwhm, NaN), ), (gof = (converged = converged,),))
(fwhm = measurement(fwhm, NaN), fit_func = fit_func, ), (gof = (converged = converged,),))
report = (
v = v_ml,
h = h,
Expand Down Expand Up @@ -208,7 +211,7 @@
"""
function estimate_fwhm(v::NamedTuple)
# get FWHM
f_sigWithTail = Base.Fix2(get_th228_fit_functions().f_sigWithTail,v)
f_sigWithTail = Base.Fix2(get_th228_fit_functions().gamma_sigWithTail,v)
try
if v.skew_fraction <= 0.5
half_max_sig = maximum(f_sigWithTail.(v.μ - v.σ:0.001:v.μ + v.σ))/2
Expand Down Expand Up @@ -268,7 +271,7 @@
function fit_subpeaks_th228(
h_survived::Histogram, h_cut::Histogram, h_result;
uncertainty::Bool=false, low_e_tail::Bool=true, fix_σ::Bool = true, fix_skew_fraction::Bool = true, fix_skew_width::Bool = true,
pseudo_prior::NamedTupleDist=NamedTupleDist(empty = true), fit_func::Symbol=:f_fit, background_center::Real = h_result.μ
pseudo_prior::NamedTupleDist=NamedTupleDist(empty = true), fit_func::Symbol= :gamma_def, background_center::Real = h_result.μ
)

# create standard pseudo priors
Expand Down Expand Up @@ -437,7 +440,7 @@
@debug "σ cut : $(v_ml.σ_cut)"

result = merge(NamedTuple{keys(v_ml)}([measurement(v_ml[k], NaN) for k in keys(v_ml)]...),
(fwhm = measurement(fwhm, NaN), gof = (converged = converged, survived = NamedTuple(), cut = NamedTuple())))
(fwhm = measurement(fwhm, NaN), fit_func = fit_func, gof = (converged = converged, survived = NamedTuple(), cut = NamedTuple())))
end

report = (
Expand Down
53 changes: 32 additions & 21 deletions src/specfit_functions.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# This file is a part of LegendSpecFits.jl, licensed under the MIT License (MIT).

# helper functions for fitting peakshapes, legacy
th228_fit_functions = (
f_fit = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background),
f_fit_bckSlope = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_slope = v.background_slope),
f_fit_bckExp = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_exp = v.background_exp),
f_fit_tails = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width , v.background; skew_fraction_highE = v.skew_fraction_highE, skew_width_highE = v.skew_width_highE),
f_sigWithTail = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction) + lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
)

"""
get_th228_fit_functions(; background_center::Union{Real,Nothing} = nothing)
This function defines the gamma peakshape fit functions used in the calibration specfits.
* gamma_def: "default" gamma peakshape with gaussian signal, low-energy tail, and background (flat + step)
* gamma_tails: default gamma peakshape + high-energy tail
* gamma_bckSlope: default gamma peakshape + linear background slope
* gamma_bckExp: default gamma peakshape + exponential background
* gamma_bckFlat: default gamma peakshape - step background (only flat component!)
* gamma_tails_bckFlat: default gamma peakshape + high-energy tail - step background (only flat component!)
"""
function get_th228_fit_functions(; background_center::Union{Real,Nothing} = nothing)
merge(
(f_fit = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background),
f_fit_tails = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width , v.background; skew_fraction_highE = v.skew_fraction_highE, skew_width_highE = v.skew_width_highE),
f_sigWithTail = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction) + lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
(gamma_def = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background),
gamma_tails = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width , v.background; skew_fraction_highE = v.skew_fraction_highE, skew_width_highE = v.skew_width_highE),
gamma_sigWithTail = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction) + lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
gamma_bckFlat = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, 0.0, v.skew_fraction, v.skew_width, v.background),
gamma_tails_bckFlat = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, 0.0, v.skew_fraction, v.skew_width , v.background; skew_fraction_highE = v.skew_fraction_highE, skew_width_highE = v.skew_width_highE),
),
if isnothing(background_center)
(f_fit_bckSlope = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_slope = v.background_slope, background_center = v.μ),
f_fit_bckExp = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_exp = v.background_exp, background_center = v.μ),
(gamma_bckSlope = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_slope = v.background_slope, background_center = v.μ),
gamma_bckExp = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_exp = v.background_exp, background_center = v.μ),
)
else
(f_fit_bckSlope = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_slope = v.background_slope, background_center = background_center),
f_fit_bckExp = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_exp = v.background_exp, background_center = background_center),
(gamma_bckSlope = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_slope = v.background_slope, background_center = background_center),
gamma_bckExp = (x, v) -> gamma_peakshape(x, v.μ, v.σ, v.n, v.step_amplitude, v.skew_fraction, v.skew_width, v.background; background_exp = v.background_exp, background_center = background_center),
)
end
)
Expand All @@ -33,23 +35,32 @@
These component functions are used in the fit-report and in plot receipes
"""
function peakshape_components(fit_func::Symbol; background_center::Union{Real,Nothing} = nothing)
if fit_func == :f_fit
if fit_func == :gamma_def
funcs = (f_sig = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction),
f_lowEtail = (x, v) -> lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
f_bck = (x, v) -> background_peakshape(x, v.μ, v.σ, v.step_amplitude, v.background))
elseif fit_func == :f_fit_bckSlope
elseif fit_func == :gamma_bckSlope

Check warning on line 42 in src/specfit_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/specfit_functions.jl#L42

Added line #L42 was not covered by tests
funcs = (f_sig = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction),
f_lowEtail = (x, v) -> lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
f_bck = (x, v) -> background_peakshape(x, v.μ, v.σ, v.step_amplitude, v.background; background_slope = v.background_slope, background_center = background_center))
elseif fit_func == :f_fit_bckExp
elseif fit_func == :gamma_bckExp

Check warning on line 46 in src/specfit_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/specfit_functions.jl#L46

Added line #L46 was not covered by tests
funcs = (f_sig = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction),
f_lowEtail = (x, v) -> lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
f_bck = (x, v) -> background_peakshape(x, v.μ, v.σ, v.step_amplitude, v.background; background_exp = v.background_exp, background_center = background_center))
elseif fit_func == :f_fit_tails
elseif fit_func == :gamma_bckFlat
funcs = (f_sig = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction),

Check warning on line 51 in src/specfit_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/specfit_functions.jl#L50-L51

Added lines #L50 - L51 were not covered by tests
f_lowEtail = (x, v) -> lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
f_bck = (x, v) -> background_peakshape(x, v.μ, v.σ, 0.0, v.background))
elseif fit_func == :gamma_tails

Check warning on line 54 in src/specfit_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/specfit_functions.jl#L54

Added line #L54 was not covered by tests
funcs = (f_sig = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, (v.skew_fraction + v.skew_fraction_highE)),
f_lowEtail = (x, v) -> lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
f_highEtail = (x, v) -> highEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction_highE, v.skew_width_highE),
f_bck = (x, v) -> background_peakshape(x, v.μ, v.σ, v.step_amplitude, v.background))
elseif fit_func == :gamma_tails_bckFlat
funcs = (f_sig = (x, v) -> signal_peakshape(x, v.μ, v.σ, v.n, (v.skew_fraction + v.skew_fraction_highE)),

Check warning on line 60 in src/specfit_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/specfit_functions.jl#L59-L60

Added lines #L59 - L60 were not covered by tests
f_lowEtail = (x, v) -> lowEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction, v.skew_width),
f_highEtail = (x, v) -> highEtail_peakshape(x, v.μ, v.σ, v.n, v.skew_fraction_highE, v.skew_width_highE),
f_bck = (x, v) -> background_peakshape(x, v.μ, v.σ, 0.0, v.background))
end
labels = (f_sig = "Signal", f_lowEtail = "Low-energy tail", f_bck = "Background", f_highEtail = "High-energy tail")
colors = (f_sig = :orangered1, f_lowEtail = :orange, f_bck = :dodgerblue2, f_highEtail = :forestgreen)
Expand Down
4 changes: 2 additions & 2 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Sample Legend200 calibration data based on "Inverse Transform Sampling" method
* find the value x such that F(x) = u by solving for x . done by interpolation of the inverse cdf
* repeat for many u : energy samples
"""
function generate_mc_spectrum(n_tot::Int=200000,; f_fit::Base.Callable=LegendSpecFits.get_th228_fit_functions().f_fit)
function generate_mc_spectrum(n_tot::Int=200000,; f_fit::Base.Callable=LegendSpecFits.get_th228_fit_functions().gamma_def)

th228_lines = [583.191, 727.330, 860.564, 1592.53, 1620.50, 2103.53, 2614.51]

Expand Down Expand Up @@ -53,7 +53,7 @@ function generate_mc_spectrum(n_tot::Int=200000,; f_fit::Base.Callable=LegendSpe
for i=1:length(th228_lines)
bandwidth = maximum(model_cdf_all[i])-minimum(model_cdf_all[i])
rand_i = minimum(model_cdf_all[i]).+bandwidth.*rand(n_i[i]); # make sure sample is within model range
interp_cdf_inv = linear_interpolation(model_cdf_all[i],bin_centers_all[i]) # inverse cdf
interp_cdf_inv = linear_interpolation(model_cdf_all[i], bin_centers_all[i]) # inverse cdf
energy_mc_all[i] = interp_cdf_inv.(rand_i)
end

Expand Down
Loading