Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the sample function #195

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SpeciesDistributionToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ include("io/read_write.jl")
include("pseudoabsences.jl")
export WithinRadius, SurfaceRangeEnvelope, RandomSelection
export pseudoabsencemask
export rarefy

end # module SpeciesDistributionToolkit
24 changes: 14 additions & 10 deletions src/pseudoabsences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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...)
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test/02_rarefy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module TestThatRarefyWorks

using Test
using SpeciesDistributionToolkit

@test error("Rarefy tests are not implemented yet")

end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"
5 changes: 1 addition & 4 deletions test/edgecases/03_layers_keycheck.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/rarefy.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading