Skip to content

Commit

Permalink
exception management system
Browse files Browse the repository at this point in the history
  • Loading branch information
gottacatchenall committed Apr 26, 2024
1 parent 07ed5be commit af722e8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
9 changes: 5 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SliceMap = "82cb661a-3f19-5665-9e27-df437c7e54c8"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"

[weakdeps]
SpeciesDistributionToolkit = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
Expand All @@ -26,11 +27,11 @@ SDMToolkitExt = ["SpeciesDistributionToolkit"]
[compat]
Distributions = "0.25"
HaltonSequences = "0.2"
HiGHS = "1.5"
JuMP = "1.11"
ProgressMeter = "1.7.2"
Requires = "1.3"
SliceMap = "0.2.7"
SpecialFunctions = "2.1"
StatsBase = "0.34"
ProgressMeter = "1.7.2"
JuMP = "1.11"
HiGHS = "1.5"
julia = "1.9"
julia = "1.9"
4 changes: 4 additions & 0 deletions src/BiodiversityObservationNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ using SliceMap
using JuMP
using HiGHS
using LinearAlgebra
using Term

include("types.jl")
export BONSeeder, BONRefiner, BONSampler

include("exceptions.jl")
export BONException, SeederException, TooFewSites, TooManySites

include("simplerandom.jl")
export SimpleRandom

Expand Down
19 changes: 7 additions & 12 deletions src/balancedacceptance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ Base.@kwdef mutable struct BalancedAcceptance{I <: Integer, F <: AbstractFloat}
α::F = 1.0
function BalancedAcceptance(numpoints, α)
if numpoints < one(numpoints)
throw(
ArgumentError(
"You cannot have a BalancedAcceptance with fewer than one point",
),
)
throw(TooFewSites(numpoints))
end
if α < zero(α)
throw(
Expand All @@ -39,27 +35,26 @@ function _generate!(
np, α = sampler.numpoints, sampler.α
x, y = size(uncertainty)


nonnan_indices = findall(!isnan, uncertainty)
stduncert = similar(uncertainty)

uncert_values = uncertainty[nonnan_indices]
stduncert_values = similar(uncert_values)
zfit = nothing
zfit = nothing
if var(uncert_values) > 0
zfit = StatsBase.fit(ZScoreTransform, uncert_values)
stduncert_values = StatsBase.transform(zfit, uncert_values)
end

nonnan_counter = 1
for i in eachindex(uncertainty)
if isnan(uncertainty[i])
if isnan(uncertainty[i])
stduncert[i] = NaN
elseif !isnothing(zfit)
stduncert[i] = stduncert_values[nonnan_counter]
nonnan_counter += 1
else
stduncert[i] = 1.
else
stduncert[i] = 1.0
end
end

Expand Down
27 changes: 27 additions & 0 deletions src/exceptions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
abstract type BONException <: Base.Exception end
abstract type SeederException <: BONException end

Base.showerror(io::IO, e::E) where {E <: BONException} =
tprint(
io,
"{bold red}$(supertype(E)){/bold red} | {bold yellow}$E{/bold yellow}\n" *
_error_message(e),
)

function _check_arguments(sampler::S) where {S <: BONSeeder}
sampler.numpoints > 1 || _throw(TooFewSites)
return sampler.numpoints <= n || _throw(TooManySites)
end

struct TooFewSites{I} <: BONException where {I <: Integer}
provided_sites::I
end
_error_message(tfs::TooFewSites) =
"Number of sampling sites provided was $(tfs.provided_sites), but the number of sites must be {bold}greater than {cyan}1{/cyan}{/bold}.\n"

struct TooManySites{I} <: BONException where {I <: Integer}
provided_sites::I
maximum_sites::I
end
_error_message(tms::TooManySites) =
"Number of sampling sites provided was $(tms.provided_sites), which {bold}exceeds the maximum possible{/bold} number of sites, $(tms.maximum_sites).\n"
10 changes: 6 additions & 4 deletions src/seed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ function seed!(
sampler::ST,
uncertainty::Matrix{T},
) where {ST <: BONSeeder, T <: AbstractFloat}
if length(coords) != sampler.numpoints
length(coords) != sampler.numpoints &&
throw(
DimensionMismatch(
"The length of the coordinate vector must match the `numpoints` fields of the sampler",
"The length of the coordinate vector must match the `numpoints` fiel s of the sampler",
),
)
end

max_sites = prod(size(uncertainty))
max_sites < sampler.numpoints && throw(TooManySites(sampler.numpoints, max_sites))
return _generate!(coords, sampler, uncertainty)
end

Expand All @@ -42,7 +44,7 @@ end
seed(sampler::ST, uncertainty::Matrix{T})
Produces a set of candidate sampling locations in a vector `coords` of length numpoints
from a raster `uncertainty` using `sampler`, where `sampler` is a [`BONSeeder`](@ref).
from a raster `uncertainty` using `sampler`, where `sampler` is a [`BONSeeder`](@ref).
"""
function seed(
sampler::ST,
Expand Down

0 comments on commit af722e8

Please sign in to comment.