legend-exp / LegendSpecFits.jl

LEGEND tools to fit spectra
Other
3 stars 8 forks source link

Add combined A/E fit routine #65

Closed fhagemann closed 2 weeks ago

fhagemann commented 1 month ago

Right now, this routine still uses the result of the old routine as starting parameters for the combined fit.

How to use it:

# ...

compton_bands  = aoe_config_ch.compton_bands
compton_window = aoe_config_ch.compton_window

result_fit, report_fit, compton_band_peakhists = nothing, nothing, nothing
try
    # get compton band peak histograms with generated peakstats
    compton_band_peakhists = generate_aoe_compton_bands(aoe, e_cal, compton_bands, compton_window)

    result_fit, report_fit = fit_aoe_compton(compton_band_peakhists.peakhists, compton_band_peakhists.peakstats, compton_bands,; uncertainty=true)
catch e
    @error "AoE compton bands cannot be fitted: $e"
    throw(ErrorException("AoE compton bands cannot be fitted"))
end

_compton_bands = [band for band in keys(result_fit) if result_fit[band].gof.pvalue >= p_value_cut] # end here 
μ = [result_fit[band].μ for band in _compton_bands]
σ = [result_fit[band].σ for band in _compton_bands]

# fit μ and σ with correction functions
result_corrections, report_corrections = nothing, nothing
try
    @time result_corrections, report_corrections = fit_aoe_corrections(_compton_bands, μ, σ,; e_expression = e_type_aoe_cal)
catch e
    @error "AoE corrections cannot be fitted: $e"
    throw(ErrorException("AoE corrections cannot be fitted"))
end

# use the result_corrections as input to the combined fit
combined_result = fit_aoe_compton_combined(compton_band_peakhists.peakhists, compton_band_peakhists.peakstats, compton_bands, result_corrections)
codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 0.89286% with 222 lines in your changes missing coverage. Please review.

Project coverage is 15.46%. Comparing base (392f2cc) to head (79f52b7). Report is 74 commits behind head on dev.

Files with missing lines Patch % Lines
src/aoefit_combined.jl 0.00% 150 Missing :warning:
ext/LegendSpecFitsRecipesBaseExt.jl 0.00% 70 Missing :warning:
src/aoefit.jl 0.00% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #65 +/- ## ========================================== + Coverage 15.38% 15.46% +0.08% ========================================== Files 24 32 +8 Lines 2035 2489 +454 ========================================== + Hits 313 385 +72 - Misses 1722 2104 +382 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

fhagemann commented 1 month ago

I was assigned to this PR? Does that mean I should merge it?

theHenks commented 1 month ago

Check binning in generate_aoe_compton_bands and adapt if necessary

fhagemann commented 1 month ago

fit_aoe_compton_combined now returns a report (format/naming can still be discussed/changed), which includes the fits and fit parameters to the individual Compton bands. CC @theHenks @verenaaur

fhagemann commented 1 month ago

So, to incorporate this code into the legend-julia-dataflow, one would need to insert

result_fit_combined, report_fit_combined = nothing, nothing
try
    result_fit_combined, report_fit_combined = fit_aoe_compton_combined(compton_band_peakhists.peakhists, compton_band_peakhists.peakstats, compton_bands, result_correction; e_expression = e_type_aoe_cal, uncertainty = true);
catch e
    @error "AoE compton bands cannot be fitted using a combined fit: $e"
    throw(ErrorException("AoE compton bands cannot be fitted using a combined fit"))
end

after the block where fit_aoe_corrections is called.

The plots afterwards can be adjusted to also include the combined fit result by adding an extra argument:

# instead of: p = plot(report_correction.report_µ)
p = plot(report_correction.report_μ, report_fit_combined.report_μ)

One thing to note: The combined fits took way longer yesterday than they used to do before (which is surprising to me because I didn't touch any of the maximum likelihood fit code in the last commits I added (?) --> https://github.com/fhagemann/LegendSpecFits.jl/compare/e728750..7707a3e). Not sure why this is and I hope this does not affect data production..

fhagemann commented 1 month ago

Never mind, false alarm: I reused a variable name and caused some type instabilities. This should be fixed with the latest force push.

fhagemann commented 1 month ago

There might still be changes on some internal functions (but just code style, no change of functionality), so @theHenks you can safely merge this PR and use fit_aoe_compton_combined and the new plot recipe taking two arguments to start data production ;)

(Maybe check for a small run that I didn't mess up the func strings returned in result.. )

fhagemann commented 3 weeks ago

My last commit is just a clean-up, it does not change any functionality. It just increases type stability.