Skip to content

Commit

Permalink
new utils-fitting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofia Calgaro committed Dec 30, 2024
1 parent 5685f6e commit 138636a
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 21 deletions.
12 changes: 7 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
)
5 changes: 3 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Pages = [
"installation.md",
"config.md",
"inputs.md",
"toys.md"
"toys.md",
"tutorial.md"
]
Depth = 1
```
```
15 changes: 9 additions & 6 deletions src/fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
14 changes: 7 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion test/statistics/test_all.jl
Original file line number Diff line number Diff line change
@@ -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
102 changes: 102 additions & 0 deletions test/statistics/test_generate_disjoint_uniform_samples.jl
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions test/statistics/test_get_bkg_info.jl
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/utils/test_all.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Test

Test.@testset "likelihood" begin
include("test_get_range.jl")
end
66 changes: 66 additions & 0 deletions test/utils/test_get_range.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 138636a

Please sign in to comment.