Skip to content

Commit

Permalink
First draft of report returned by fit_aoe_compton_combined
Browse files Browse the repository at this point in the history
  • Loading branch information
fhagemann committed Aug 26, 2024
1 parent 8dd6e84 commit ceadbf2
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/aoefit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function fit_aoe_compton_combined(peakhists::Vector{<:Histogram}, peakstats::Str

# fit peak
try
A, B = fit_single_aoe_compton_with_fixed_μ_and_σ(h, μ, σ, ps; uncertainty=uncertainty)
A, _ = fit_single_aoe_compton_with_fixed_μ_and_σ(h, μ, σ, ps; uncertainty=uncertainty)
neg_log_likelihoods[i] = A

Check warning on line 355 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L353-L355

Added lines #L353 - L355 were not covered by tests
catch e
@warn "Error fitting band $(compton_bands[i]): $e"
Expand All @@ -362,13 +362,39 @@ function fit_aoe_compton_combined(peakhists::Vector{<:Histogram}, peakstats::Str
end

# MLE
opt_r = optimize(f_loglike inverse(f_trafo), f_trafo(v_init), NelderMead(), Optim.Options(time_limit = 60, show_trace=false, iterations = 1000))
opt_r = optimize(f_loglike inverse(f_trafo), f_trafo(v_init), NelderMead(), Optim.Options(time_limit = 120, show_trace=false, iterations = 1000))

Check warning on line 365 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L365

Added line #L365 was not covered by tests

converged = Optim.converged(opt_r)
!converged && @warn "Fit did not converge"

Check warning on line 368 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L367-L368

Added lines #L367 - L368 were not covered by tests

v_ml = inverse(f_trafo)(Optim.minimizer(opt_r))

Check warning on line 370 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L370

Added line #L370 was not covered by tests


reports = Vector{NamedTuple{(:v, :h, :f_fit, :f_sig, :f_bck)}}(undef, length(compton_bands))

Check warning on line 373 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L373

Added line #L373 was not covered by tests

let pars = v_ml

Check warning on line 375 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L375

Added line #L375 was not covered by tests

# iterate throuh all peaks (multithreaded)
Threads.@threads for i in eachindex(compton_bands)

Check warning on line 378 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L378

Added line #L378 was not covered by tests

# get histogram and peakstats
h = peakhists[i]
ps = peakstats[i]
e = ustrip(compton_bands[i])
μ = f_aoe_mu(e, (pars.μA, pars.μB))
σ = f_aoe_sigma(e, (pars.σA, pars.σB))

Check warning on line 385 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L381-L385

Added lines #L381 - L385 were not covered by tests

# fit peak
try
_, B = fit_single_aoe_compton_with_fixed_μ_and_σ(h, μ, σ, ps; uncertainty=uncertainty)
reports[i] = B

Check warning on line 390 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L388-L390

Added lines #L388 - L390 were not covered by tests
catch e
@warn "Error fitting band $(compton_bands[i]): $e"
continue

Check warning on line 393 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L392-L393

Added lines #L392 - L393 were not covered by tests
end
end

Check warning on line 395 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L395

Added line #L395 was not covered by tests
end

if uncertainty && converged

Check warning on line 398 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L398

Added line #L398 was not covered by tests

f_loglike_array = p -> f_loglike((μA = p[1], μB = p[2], σA = p[3], σB = p[4]))

Check warning on line 400 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L400

Added line #L400 was not covered by tests
Expand Down Expand Up @@ -416,14 +442,12 @@ function fit_aoe_compton_combined(peakhists::Vector{<:Histogram}, peakstats::Str
result = merge(v_ml, )

Check warning on line 442 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L442

Added line #L442 was not covered by tests
end

# report = (
# v = v_ml,
# h = h,
# f_fit = x -> Base.Fix2(f_aoe_compton, v_ml)(x),
# f_sig = x -> Base.Fix2(f_aoe_sig, v_ml)(x),
# f_bck = x -> Base.Fix2(f_aoe_bkg, v_ml)(x)
# )
report = (

Check warning on line 445 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L445

Added line #L445 was not covered by tests
v = v_ml,
compton_bands = compton_bands,
band_reports = reports
)

return result #, report
return result, report

Check warning on line 451 in src/aoefit.jl

View check run for this annotation

Codecov / codecov/patch

src/aoefit.jl#L451

Added line #L451 was not covered by tests
end
export fit_aoe_compton_combined

0 comments on commit ceadbf2

Please sign in to comment.