diff --git a/docs/make.jl b/docs/make.jl index f448d70..eba3f12 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -7,14 +7,14 @@ push!(LOAD_PATH, "../src") using ZeroNuFit makedocs( + debug=true, modules = [ZeroNuFit], sitename = "ZeroNuFit.jl", authors = "S. Calgaro, T. Dixon", format = Documenter.HTML( - #mathengine = MathJax(), - # Set size thresholds for HTML generation - size_threshold = 400 * 1024, # Hard limit (e.g., 400 KiB) - size_threshold_warn = 300 * 1024 # Warning limit (e.g., 300 KiB) + # size thresholds for HTML generation + size_threshold = 400 * 1024, # hard limit + size_threshold_warn = 300 * 1024 # warning limit ), pages = [ "Home" => "index.md", @@ -23,5 +23,7 @@ makedocs( "Partitions and events" => "inputs.md", "Generating toys" => "toys.md", "Tutorial" => "tutorial.md", - ] + #"API" => "internal_api.md", + ], + doctest = ("fixdoctests" in ARGS) ? :fix : true, ) diff --git a/docs/src/index.md b/docs/src/index.md index 004b212..68122fa 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -78,7 +78,8 @@ Pages = [ "installation.md", "config.md", "inputs.md", - "toys.md" + "toys.md", + "tutorial.md" ] Depth = 1 -``` \ No newline at end of file +``` diff --git a/src/fitting.jl b/src/fitting.jl index f5612c0..e13703f 100644 --- a/src/fitting.jl +++ b/src/fitting.jl @@ -18,6 +18,12 @@ function get_bkg_info(config) return bkg_shape,bkg_shape_pars end +function get_range(fit_range) + range_l = [arr[1] for arr in fit_range] + range_h = [arr[2] for arr in fit_range] + return sort(range_l), sort(range_h) +end + """ norm_linear(x::Float64,p::NamedTuple,b_name::Symbol,fit_range) @@ -30,8 +36,7 @@ Parameters - x::Real, the x value to evaluate at """ function norm_linear(x::Float64,p::NamedTuple,b_name::Symbol,fit_range) - range_l = [arr[1] for arr in fit_range] - range_h = [arr[2] for arr in fit_range] + range_l, range_h = get_range(fit_range) center = range_l[1] sum_range = sum(range_h .- range_l) @@ -56,8 +61,7 @@ Parameters - x::Real, the x value to evaluate at """ function norm_uniform(x::Real,p::NamedTuple,b_name::Symbol,fit_range) - range_l = [arr[1] for arr in fit_range] - range_h = [arr[2] for arr in fit_range] + range_l, range_h = get_range(fit_range) center = range_l[1] norm =sum(range_h .- range_l) @@ -86,8 +90,7 @@ Parameters - x::Real, the x value to evaluate at """ function norm_exponential(x::Float64,p::NamedTuple,b_name::Symbol,fit_range) - range_l = [arr[1] for arr in fit_range] - range_h = [arr[2] for arr in fit_range] + range_l, range_h = get_range(fit_range) center = range_l[1] centers=[center,center,center] diff --git a/src/utils.jl b/src/utils.jl index 2f4b068..0dc5c6a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -281,8 +281,7 @@ end ## sampling function inverse_uniform_cdf(p, fit_range) - range_l = [arr[1] for arr in fit_range] - range_h = [arr[2] for arr in fit_range] + range_l, range_h = get_range(fit_range) delta = sum(range_h .- range_l) cumulative_prob = 0.0 @@ -314,12 +313,13 @@ function inverse_uniform_cdf(p, fit_range) end -function generate_disjoint_uniform_samples(n, fit_range) - rands=[] - for i in 1:n - append!(rands,rand()) +function generate_disjoint_uniform_samples(n, fit_range; seed=nothing) + # fix the seed (if provided) + if seed !== nothing + Random.seed!(seed) end - res = [inverse_uniform_cdf(rand,fit_range) for rand in rands] + rands = rand(n) + res = [inverse_uniform_cdf(rand, fit_range) for rand in rands] return res end diff --git a/test/statistics/test_all.jl b/test/statistics/test_all.jl index 500a66e..fa34abb 100644 --- a/test/statistics/test_all.jl +++ b/test/statistics/test_all.jl @@ -1,9 +1,11 @@ using Test -Test.@testset "likelihood" begin +Test.@testset "statistics" begin include("test_get_signal_pdf.jl") include("test_get_mu_s_b.jl") include("test_build_likelihood_zero_obs_evts.jl") include("test_build_likelihood_per_partition.jl") include("test_inverse_uniform_cdf.jl") + include("test_generate_disjoint_uniform_samples.jl") + include("test_get_bkg_info.jl") end \ No newline at end of file diff --git a/test/statistics/test_generate_disjoint_uniform_samples.jl b/test/statistics/test_generate_disjoint_uniform_samples.jl new file mode 100644 index 0000000..44b5641 --- /dev/null +++ b/test/statistics/test_generate_disjoint_uniform_samples.jl @@ -0,0 +1,102 @@ +using Pkg +Pkg.activate(".") +Pkg.instantiate() +using Random +include("../../src/ZeroNuFit.jl") +using .ZeroNuFit +include("../../main.jl") +include("../../src/utils.jl") + +@testset "test_generate_disjoint_uniform_samples" begin + + @info "Testing function for getting random 'n' energies (function 'generate_disjoint_uniform_samples' in src/utils.jl)" + + # fixed seed (1 energy, 1 range) + one_energy = nothing + fit_range = [[1920.0,1930.0]] + try + one_energy = ZeroNuFit.generate_disjoint_uniform_samples(1, fit_range; seed=123) + catch e + @error "Error in 'generate_disjoint_uniform_samples' evaluation: $e" + throw(e) + end + + @testset "Check one_energy is valid (fixed seed)" begin + @test !isnothing(one_energy) + end + + expected_value = [1925.212137955354] + @testset "Check energy accuracy (fixed seed)" begin + @test one_energy == expected_value + end + + function check_in_ranges(energies, fit_range) + return [any(e >= r[1] && e <= r[2] for r in fit_range) for e in energies] + end + + @testset "Check energy containment in fit range (fixed seed)" begin + contained = check_in_ranges(one_energy, fit_range) + @test all(contained) + end + + # random seed generator (1 energy, 1 range) + one_energy = nothing + try + one_energy = ZeroNuFit.generate_disjoint_uniform_samples(1, fit_range) + catch e + @error "Error in 'generate_disjoint_uniform_samples' evaluation: $e" + throw(e) + end + + @testset "Check one_energy is valid (random seed)" begin + @test !isnothing(one_energy) + end + + expected_value = [1925.212137955354] + @testset "Check energy accuracy (random seed, 1 energy)" begin + @test one_energy != expected_value + end + + @testset "Check energy containment in fit range (random seed, 1 energy)" begin + contained = check_in_ranges(one_energy, fit_range) + @test all(contained) + end + + # random seed generator (more energies, 1 range) + more_energies = nothing + try + more_energies = ZeroNuFit.generate_disjoint_uniform_samples(10, fit_range) + catch e + @error "Error in 'generate_disjoint_uniform_samples' evaluation: $e" + throw(e) + end + + @testset "Check more_energies is valid (random seed, more energies)" begin + @test !isnothing(more_energies) + end + + @testset "Check energy containment in fit range (random seed, more energies)" begin + contained = check_in_ranges(more_energies, fit_range) + @test all(contained) + end + + # random seed generator (more energies, more ranges) + more_energies = nothing + fit_range = [[1920.0,1930.0], [1970.0,1980.0], [2100.0,2250.0]] + try + more_energies = ZeroNuFit.generate_disjoint_uniform_samples(10, fit_range) + catch e + @error "Error in 'generate_disjoint_uniform_samples' evaluation: $e" + throw(e) + end + + @testset "Check more_energies is valid (random seed, more energies, more ranges)" begin + @test !isnothing(more_energies) + end + + @testset "Check energy containment in fit range (random seed, more energies, more ranges)" begin + contained = check_in_ranges(more_energies, fit_range) + @test all(contained) + end + +end diff --git a/test/statistics/test_get_bkg_info.jl b/test/statistics/test_get_bkg_info.jl new file mode 100644 index 0000000..5d91d84 --- /dev/null +++ b/test/statistics/test_get_bkg_info.jl @@ -0,0 +1,78 @@ +using Pkg +Pkg.activate(".") +Pkg.instantiate() +using Random +include("../../src/ZeroNuFit.jl") +using .ZeroNuFit +include("../../main.jl") + +@testset "test_get_bkg_info" begin + + @info "Testing gaussian signal for one partition and one close event @ 2039.05 keV (function 'get_bkg_info' in src/fitting.jl)" + + # default modeling, i.e. flat + config = Dict( + "bkg" => Dict() + ) + + bkg_shape = nothing + bkg_shape_pars = nothing + try + bkg_shape,bkg_shape_pars = ZeroNuFit.get_bkg_info(config) + catch e + @error "Error in 'get_bkg_info' evaluation: $e" + throw(e) + end + + expected_value = :uniform + @testset "Check bkg_shape accuracy" begin + @test bkg_shape == expected_value + end + expected_value = nothing + @testset "Check bkg_shape_pars accuracy" begin + @test bkg_shape_pars == expected_value + end + + # linear modeling + config = Dict("bkg" => Dict("shape" => Dict("name" => "linear", "pars" => Dict("slope" => [-10, 10])))) + + bkg_shape = nothing + bkg_shape_pars = nothing + try + bkg_shape,bkg_shape_pars = ZeroNuFit.get_bkg_info(config) + catch e + @error "Error in 'get_bkg_info' evaluation: $e" + throw(e) + end + + expected_value = :linear + @testset "Check bkg_shape accuracy" begin + @test bkg_shape == expected_value + end + expected_value = Dict("slope" => [-10, 10]) + @testset "Check bkg_shape_pars accuracy" begin + @test bkg_shape_pars == expected_value + end + + # exponential modeling + config = Dict("bkg" => Dict("shape" => Dict("name" => "exponential", "pars" => Dict("slope" => [-5, 5])))) + + bkg_shape = nothing + bkg_shape_pars = nothing + try + bkg_shape,bkg_shape_pars = ZeroNuFit.get_bkg_info(config) + catch e + @error "Error in 'get_bkg_info' evaluation: $e" + throw(e) + end + + expected_value = :exponential + @testset "Check bkg_shape accuracy" begin + @test bkg_shape == expected_value + end + expected_value = Dict("slope" => [-5, 5]) + @testset "Check bkg_shape_pars accuracy" begin + @test bkg_shape_pars == expected_value + end + +end diff --git a/test/utils/test_all.jl b/test/utils/test_all.jl new file mode 100644 index 0000000..1ed7459 --- /dev/null +++ b/test/utils/test_all.jl @@ -0,0 +1,5 @@ +using Test + +Test.@testset "likelihood" begin + include("test_get_range.jl") +end diff --git a/test/utils/test_get_range.jl b/test/utils/test_get_range.jl new file mode 100644 index 0000000..3087645 --- /dev/null +++ b/test/utils/test_get_range.jl @@ -0,0 +1,66 @@ +using Pkg +Pkg.activate(".") # activate the environment +Pkg.instantiate() # instantiate the environment +include("../../src/ZeroNuFit.jl") +using .ZeroNuFit +include("../../main.jl") + +@testset "test_get_range" begin + + @info "Testing function to retrieve lower and upper range edges (function 'get_range' in src/fitting.jl)" + + # 1 entry in fit ranges + fit_ranges = [[1930.0, 1950.0]] + range_l = nothing + range_h = nothing + try + range_l, range_h = ZeroNuFit.get_range(fit_ranges) + catch e + @error "Error in get_range: $e" + throw(e) + end + + @testset "Check range_l is valid" begin + @test !isnothing(range_l) + end + @testset "Check range_h is valid" begin + @test !isnothing(range_h) + end + + expected_events=[1930.0] + @testset "Check events accuracy" begin + @test range_l == expected_events + end + expected_events=[1950.0] + @testset "Check events accuracy" begin + @test range_h == expected_events + end + + # more entries in fit ranges + fit_ranges = [[1930.0, 1950.0], [2100.0,2200.0], [1450.0, 1500.0]] + range_l = nothing + range_h = nothing + try + range_l, range_h = ZeroNuFit.get_range(fit_ranges) + catch e + @error "Error in get_range: $e" + throw(e) + end + + @testset "Check range_l is valid" begin + @test !isnothing(range_l) + end + @testset "Check range_h is valid" begin + @test !isnothing(range_h) + end + + expected_events=[1450.0, 1930.0, 2100.0] + @testset "Check events accuracy" begin + @test range_l == expected_events + end + expected_events=[1500.0, 1950.0, 2200.0] + @testset "Check events accuracy" begin + @test range_h == expected_events + end +end +