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

Generic simple_calibration #108

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
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
22 changes: 12 additions & 10 deletions ext/LegendSpecFitsRecipesBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -486,24 +486,25 @@
end
end

@recipe function f(report::NamedTuple{(:h_calsimple, :h_uncal, :c, :fep_guess, :peakhists, :peakstats)}; cal=true)
@recipe function f(report::NamedTuple{(:h_calsimple, :h_uncal, :c, :peak_guess, :peakhists, :peakstats)}; cal=true)

Check warning on line 489 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L489

Added line #L489 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is a new recipe. Was this not here already before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost a duplicate of the existing recipe. However, the old one has some stuff hardcoded, e.g. "FEP" in the plot label. I didn't want to mess with the existing one, since we're int he middle of the processing.

ylabel := "Counts"
legend := :topright
yscale := :log10
fill := false

Check warning on line 493 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L493

Added line #L493 was not covered by tests
if cal
h = LinearAlgebra.normalize(report.h_calsimple, mode = :density)
xlabel := "Energy (keV)"
xlims := (0, 3000)
xticks := (0:200:3000, ["$i" for i in 0:200:3000])
xticks := (0:500:3000, ["$i" for i in 0:500:3000])

Check warning on line 498 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L498

Added line #L498 was not covered by tests
ylims := (0.2, maximum(h.weights)*1.1)
fep_guess = 2614.5
peak_guess = ustrip(report.c * report.peak_guess)

Check warning on line 500 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L500

Added line #L500 was not covered by tests
else
h = LinearAlgebra.normalize(report.h_uncal, mode = :density)
xlabel := "Energy (ADC)"
xlims := (0, 1.2*report.fep_guess)
xticks := (0:3000:1.2*report.fep_guess, ["$i" for i in 0:3000:1.2*report.fep_guess])
xlims := (0, 1.2*report.peak_guess)

Check warning on line 504 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L504

Added line #L504 was not covered by tests
# xticks := (0:3000:1.2*report.peak_guess, ["$i" for i in 0:3000:1.2*report.peak_guess])
ylims := (0.2, maximum(h.weights)*1.1)
fep_guess = report.fep_guess
peak_guess = report.peak_guess

Check warning on line 507 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L507

Added line #L507 was not covered by tests
end
@series begin
seriestype := :stepbins
Expand All @@ -513,10 +514,10 @@
y_vline = 0.2:1:maximum(h.weights)*1.1
@series begin
seriestype := :line
label := "FEP Guess"
color := :red
label := "Peak estimate"#: $(round(peak_guess, digits = 1))"
color := :red2

Check warning on line 518 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L517-L518

Added lines #L517 - L518 were not covered by tests
linewidth := 1.5
fill(fep_guess, length(y_vline)), y_vline
fill(peak_guess, length(y_vline)), y_vline

Check warning on line 520 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L520

Added line #L520 was not covered by tests
end
end

Expand Down Expand Up @@ -850,7 +851,8 @@
if plot_ribbon
ribbon := uncertainty.(report.f_fit.(0:1:1.2*value(maximum(report.x))))
end
0:1:1.2*value(maximum(report.x)), value.(report.f_fit.(0:1:1.2*value(maximum(report.x))))
xstep = value(maximum(report.x))/100
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also work for the 228-Th data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it today on 228-Th data and it worked well, same result.

0:xstep:1.2*value(maximum(report.x)), value.(report.f_fit.(0:xstep:1.2*value(maximum(report.x))))

Check warning on line 855 in ext/LegendSpecFitsRecipesBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LegendSpecFitsRecipesBaseExt.jl#L854-L855

Added lines #L854 - L855 were not covered by tests
end
@series begin
seriestype := :scatter
Expand Down
8 changes: 7 additions & 1 deletion src/chi2fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@
# gof
chi2min = res.objective
dof = length(x) - length(v_chi2)
pvalue = ccdf(Chisq(dof), chi2min)
pvalue = if iszero(dof)
@warn "The number of fit parameters is equivalent to number of data points --> dof = 0 ; p-value not meaningful, set to NaN"
NaN

Check warning on line 99 in src/chi2fit.jl

View check run for this annotation

Codecov / codecov/patch

src/chi2fit.jl#L98-L99

Added lines #L98 - L99 were not covered by tests
else
ccdf(Chisq(dof), chi2min)
end

function get_y_pred_err(f_fit, x_val, x_err, pars) # get final uncertainties for normalized residuals
dual_x = ForwardDiff.Dual{UncertTag}(x_val, x_err)
dual_y = f_fit(dual_x, pars...)
Expand Down
7 changes: 6 additions & 1 deletion src/filter_optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,16 @@
modes = Vector{Float64}(undef, length(e_grid_ft))
fts_success = Bool.(zeros(length(e_grid_ft)))

Threads.@threads for f in eachindex(e_grid_ft)
# Threads.@threads
for f in eachindex(e_grid_ft)

Check warning on line 136 in src/filter_optimization.jl

View check run for this annotation

Codecov / codecov/patch

src/filter_optimization.jl#L136

Added line #L136 was not covered by tests
# get ft
ft = e_grid_ft[f]

# get e values for this ft
if all(ismissing.(e_grid[f,:]))
@debug "All values are missing, skipping"
continue

Check warning on line 143 in src/filter_optimization.jl

View check run for this annotation

Codecov / codecov/patch

src/filter_optimization.jl#L141-L143

Added lines #L141 - L143 were not covered by tests
end
e_ft = Array{Float64}(flatview(e_grid)[f, :])
e_ft = e_ft[isfinite.(e_ft)]

Expand Down
4 changes: 2 additions & 2 deletions src/fit_calibration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
result_fit = merge(result_fit, (par = par_unit,))

# built function in string
func = join(["$(mvalue(par[i]))$e_unit .* ($(e_expression)).^$(i-1)" for i in eachindex(par)], " .+ ")
func_err = join(["($(par[i]))$e_unit .* ($(e_expression)).^$(i-1)" for i in eachindex(par)], " .+ ")
func = join(["$(mvalue(par[i]))$e_unit * ($(e_expression))^$(i-1)" for i in eachindex(par)], " + ")
func_err = join(["($(par[i]))$e_unit * ($(e_expression))^$(i-1)" for i in eachindex(par)], " + ")

Check warning on line 55 in src/fit_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/fit_calibration.jl#L54-L55

Added lines #L54 - L55 were not covered by tests

result = merge(result_fit, (func = func, func_err = func_err, µ = μ, peaks = peaks))
report = merge(report_fit, (e_unit = e_unit, par = result.par, type = :cal))
Expand Down
2 changes: 2 additions & 0 deletions src/pseudo_prior.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
NamedTupleDist(; μ, σ, n, skew_fraction, skew_width, background, step_amplitude, background_slope)
elseif fit_func == :gamma_bckExp
NamedTupleDist(; μ, σ, n, skew_fraction, skew_width, background, step_amplitude, background_exp)
elseif fit_func == :gamma_minimal
NamedTupleDist(; μ, σ, n, background)

Check warning on line 37 in src/pseudo_prior.jl

View check run for this annotation

Codecov / codecov/patch

src/pseudo_prior.jl#L36-L37

Added lines #L36 - L37 were not covered by tests
else
throw(ArgumentError("Unknown fit function: $fit_func"))
end
Expand Down
Loading
Loading