diff --git a/src/SpeciesDistributionToolkit.jl b/src/SpeciesDistributionToolkit.jl index df048a320..a7188395f 100644 --- a/src/SpeciesDistributionToolkit.jl +++ b/src/SpeciesDistributionToolkit.jl @@ -42,5 +42,6 @@ include("io/read_write.jl") include("pseudoabsences.jl") export WithinRadius, SurfaceRangeEnvelope, RandomSelection export pseudoabsencemask +export rarefy end # module SpeciesDistributionToolkit diff --git a/src/pseudoabsences.jl b/src/pseudoabsences.jl index b538a5e0c..7ba09e9d9 100644 --- a/src/pseudoabsences.jl +++ b/src/pseudoabsences.jl @@ -36,7 +36,10 @@ end """ _random_point(ref, d; R = 6371.0) -This function is used _internally_ to create a random point located within a distance `d` of point `ref`, assuming that the radius of the Earth is `R`. All it does is to generate a random angle in degrees, and the using the `_known_point` method to generate the new point. +This function is used _internally_ to create a random point located within a +distance `d` of point `ref`, assuming that the radius of the Earth is `R`. All +it does is to generate a random angle in degrees, and the using the +`_known_point` method to generate the new point. """ function _random_point(ref, d; R = 6371.0) α = deg2rad(rand() * 360.0) @@ -46,7 +49,8 @@ end """ _known_point(ref, d, α; R = 6371.0) -This function will generate a new point set at a distance `d` and angle `α` from the `ref` point, assuming the radius of the Earth is `R`. +This function will generate a new point set at a distance `d` and angle `α` from +the `ref` point, assuming the radius of the Earth is `R`. """ function _known_point(ref, d, α; R = 6371.0) # Convert the coordinates from degrees to radians @@ -106,10 +110,10 @@ end """ pseudoabsencemask(::Type{RandomSelection}, presence::T) where {T <: SimpleSDMLayer} -Generates a mask for pseudo-absences using the random selection method. Candidate -cells for the pseudo-absence mask are (i) within the bounding box of the _layer_ -(use `SurfaceRangeEnvelope` to use the presences bounding box), and (ii) valued in the -layer. +Generates a mask for pseudo-absences using the random selection method. +Candidate cells for the pseudo-absence mask are (i) within the bounding box of +the _layer_ (use `SurfaceRangeEnvelope` to use the presences bounding box), and +(ii) valued in the layer. """ function pseudoabsencemask( ::Type{RandomSelection}, @@ -151,13 +155,13 @@ function pseudoabsencemask( end """ - sample(layer::T, n::Integer = 1; kwargs...) where {T <: SimpleSDMLayer} + rarefy(layer::T, n::Integer = 1; kwargs...) where {T <: SimpleSDMLayer} Sample a series of background points from a Boolean layer. The `kwargs` arguments are passed to `StatsBase.sample`. This method returns a Boolean layer where the values of `true` correspond to a background point. """ -function sample(layer::T, n::Integer = 1; kwargs...) where {T <: SimpleSDMLayer} +function rarefy(layer::T, n::Integer = 1; kwargs...) where {T <: SimpleSDMLayer} @assert SimpleSDMLayers._inner_type(layer) <: Bool pseudoabs = similar(layer, Bool) for k in StatsBase.sample(keys(replace(layer, false => nothing)), n; kwargs...) @@ -167,14 +171,14 @@ function sample(layer::T, n::Integer = 1; kwargs...) where {T <: SimpleSDMLayer} end """ - sample( layer::T, weights::T2, n::Integer = 1; kwargs..., ) where {T <: SimpleSDMLayer, T2 <: SimpleSDMLayer} + rarefy(layer::T, weights::T2, n::Integer = 1; kwargs..., ) where {T <: SimpleSDMLayer, T2 <: SimpleSDMLayer} Sample a series of background points from a Boolean layer, where each point has a probability of being included in the background given by the second layer. This methods works like (and is, in fact, a wrapper around) `StatsBase.sample`, where the cell values in the weight layers are transformed into weights. """ -function sample( +function rarefy( layer::T, weights::T2, n::Integer = 1; diff --git a/test/02_rarefy.jl b/test/02_rarefy.jl new file mode 100644 index 000000000..fa036020f --- /dev/null +++ b/test/02_rarefy.jl @@ -0,0 +1,8 @@ +module TestThatRarefyWorks + +using Test +using SpeciesDistributionToolkit + +@test error("Rarefy tests are not implemented yet") + +end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index e3edd5b69..1ebf3d956 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,3 +1,4 @@ [deps] GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe" diff --git a/test/edgecases/03_layers_keycheck.jl b/test/edgecases/03_layers_keycheck.jl index bea23b9de..ea89c8c0d 100644 --- a/test/edgecases/03_layers_keycheck.jl +++ b/test/edgecases/03_layers_keycheck.jl @@ -1,7 +1,4 @@ -module TestThatWrongKeywordsAreCaught - -using SpeciesDistributionToolkit -using Test +@testitem "Layers are correctly keychecked" begin provider = RasterData(WorldClim2, BioClim) @test_throws "The keyword argument layers is not" SimpleSDMPredictor(provider; layers=3) diff --git a/test/rarefy.jl b/test/rarefy.jl new file mode 100644 index 000000000..d91e8543d --- /dev/null +++ b/test/rarefy.jl @@ -0,0 +1,9 @@ +@testitem "Rarefy returns the correct number of points" begin + + random_layer = SimpleSDMResponse(rand(Bool, (20, 30))) + + rarefied_layer = rarefy(random_layer, 100) + replace!(rarefied_layer, false => nothing) + @info rarefied_layer + +end diff --git a/test/runtests.jl b/test/runtests.jl index bfb407ac3..36f662746 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,6 +5,7 @@ global anyerrors = false tests = [ "read/write layers" => "01_integration_read.jl", + "rarefy pseudo-absences" => "02_rarefy.jl", "EDGE: stitch bounding box" => "edgecases/01_stitch_wrong_bb.jl", "EDGE: clip & GDAL" => "edgecases/02_clip_gdalwarp.jl", "EDGE: keychecker" => "edgecases/03_layers_keycheck.jl",