Skip to content

Commit

Permalink
🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
gottacatchenall committed May 6, 2024
1 parent 1994eb3 commit 9961cc0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/adaptivespatial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
Base.@kwdef mutable struct AdaptiveSpatial{T <: Integer, F<: AbstractFloat} <: BONRefiner
numpoints::T = 30
uncertainty::Array{F,2} = rand(50,50)
function AdaptiveSpatial(numpoints)
function AdaptiveSpatial(numpoints, uncertainty)
if numpoints < one(numpoints)
throw(
ArgumentError(
"You cannot have an AdaptiveSpatial with fewer than one point",
),
)
end
return new{typeof(numpoints)}(numpoints)
return new{typeof(numpoints), typeof(uncertainty[begin])}(numpoints, uncertainty)
end
end

Expand Down
65 changes: 48 additions & 17 deletions src/uniqueness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
A `BONRefiner`
"""
Base.@kwdef mutable struct Uniqueness{I <: Integer, T <: Number} <: BONRefiner
Base.@kwdef struct Uniqueness{I <: Integer, T <: AbstractFloat} <: BONRefiner
numpoints::I = 30
layers::Array{T, 3}
layers::Array{T, 3} = rand(50, 50, 3)
function Uniqueness(numpoints, layers::Array{T, N}) where {T, N}
if numpoints < one(numpoints)
throw(
ArgumentError(
"You cannot have a Uniqueness sampler with less than one point",
),
)
end
if N != 3
throw(
ArgumentError(
"You cannot have a Uniqueness sampler without layers passed as a cube.",
),
)
end
return new{typeof(numpoints), T}(numpoints, layers)
uniq = new{typeof(numpoints), T}(numpoints, layers)
check_arguments(uniq)
return uniq
end
end

function check_arguments(uniq::Uniqueness)
check(TooFewSites, uniq)

length(size(uniq.layers)) == 3 ||
throw(
ArgumentError(
"You cannot have a Uniqueness sampler without layers passed as a 3-dimenisional array, where the first two axes are latitude and longitude.",
),
)

max_num_points = prod(size(uniq.layers)[1:2])
max_num_points >= uniq.numpoints || throw(TooManySites("Number of requested sites $(uniq.numpoints) is more than the number of candidates $max_num_points."))
end

function _generate!(
coords::Vector{CartesianIndex},
pool::Vector{CartesianIndex},
Expand Down Expand Up @@ -54,3 +56,32 @@ function _generate!(
coords[:] .= pool[sortedvals[1:np]]
return (coords, uncertainty)
end


# ====================================================
#
# Tests
#
# =====================================================

@testitem "Uniqueness default constructor works" begin
@test typeof(Uniqueness()) <: Uniqueness
end


@testitem "Uniqueness requires more than one point" begin
@test_throws TooFewSites Uniqueness(numpoints=-1)
@test_throws TooFewSites Uniqueness(numpoints=0)
@test_throws TooFewSites Uniqueness(numpoints=1)
end

@testitem "Uniqueness throws error if more points are requested than are possible" begin
@test_throws TooManySites Uniqueness(numpoints=26, layers=rand(5,5,2))
end

@testitem "Uniqueness has correct subtypes" begin
@test typeof(Uniqueness(2, zeros(5, 5, 5))) <: BONRefiner
@test typeof(Uniqueness(2, zeros(5, 5, 5))) <: BONSampler
end


0 comments on commit 9961cc0

Please sign in to comment.