From 9961cc0213bd59e20db240f9fab89263940a078e Mon Sep 17 00:00:00 2001 From: "michael d. catchen" Date: Mon, 6 May 2024 15:33:28 -0400 Subject: [PATCH] :bug: --- src/adaptivespatial.jl | 4 +-- src/uniqueness.jl | 65 +++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/adaptivespatial.jl b/src/adaptivespatial.jl index e708a34..68a36f5 100644 --- a/src/adaptivespatial.jl +++ b/src/adaptivespatial.jl @@ -8,7 +8,7 @@ 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( @@ -16,7 +16,7 @@ Base.@kwdef mutable struct AdaptiveSpatial{T <: Integer, F<: AbstractFloat} <: B ), ) end - return new{typeof(numpoints)}(numpoints) + return new{typeof(numpoints), typeof(uncertainty[begin])}(numpoints, uncertainty) end end diff --git a/src/uniqueness.jl b/src/uniqueness.jl index 40d4a68..f6bb157 100644 --- a/src/uniqueness.jl +++ b/src/uniqueness.jl @@ -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}, @@ -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 + +