Skip to content

Commit

Permalink
rip torn
Browse files Browse the repository at this point in the history
  • Loading branch information
gottacatchenall committed Sep 14, 2024
1 parent 3da8b50 commit b155035
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 212 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SimpleSDMLayers = "2c645270-77db-11e9-22c3-0f302a89c64c"
SliceMap = "82cb661a-3f19-5665-9e27-df437c7e54c8"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Expand All @@ -20,11 +21,9 @@ Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"

[weakdeps]
SpeciesDistributionToolkit = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
NeutralLandscapes = "71847384-8354-4223-ac08-659a5128069f"

[extensions]
SDTExt = ["SpeciesDistributionToolkit"]

[compat]
Distributions = "0.25"
Expand Down
21 changes: 1 addition & 20 deletions ext/SDTExt.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
module SDTExt

using BiodiversityObservationNetworks
using SpeciesDistributionToolkit

@info "Loading BONs.jl support for SimpleSDMLayers.jl ..."

function stack(
layers::Vector{<:SpeciesDistributionToolkit.SimpleSDMLayers.SimpleSDMLayer},
)
# assert all layers are the same size if we add this function to BONs.jl
mat = zeros(size(first(layers))..., length(layers))
for (l, layer) in enumerate(layers)
thismin, thismax = extrema(layer)
mat[:, :, l] .= broadcast(
x -> isnothing(x) ? NaN : (x - thismin) / (thismax - thismin),
layer.grid,
)
end
return mat
end

end
end
22 changes: 14 additions & 8 deletions src/BiodiversityObservationNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ using Term
using TestItems

include("types.jl")
export BONSeeder, BONRefiner, BONSampler
export BONSampler
export Sites, numsites, pool
export LayerType, DataLayer, InclusionProbability
export Layer, Stack

include("sample.jl")
export sample

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

include("simplerandom.jl")
export SimpleRandom
Expand All @@ -31,8 +37,8 @@ export BalancedAcceptance
include("weightedbas.jl")
export WeightedBalancedAcceptance

include("adaptivespatial.jl")
export AdaptiveSpatial
include("adaptivehotspot.jl")
export AdaptiveHotspotDetection

include("cubesampling.jl")
export CubeSampling
Expand All @@ -46,11 +52,11 @@ export GeneralizedRandomTessellatedStratified
include("uniqueness.jl")
export Uniqueness

include("seed.jl")
export seed, seed!
#include("seed.jl")
#export seed, seed!

include("refine.jl")
export refine, refine!
#include("refine.jl")
#export refine, refine!

include("entropize.jl")
export entropize, entropize!
Expand Down
101 changes: 0 additions & 101 deletions src/adaptivespatial.jl

This file was deleted.

40 changes: 29 additions & 11 deletions src/balancedacceptance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,52 @@
A `BONSeeder` that uses Balanced-Acceptance Sampling (Van-dem-Bates et al. 2017
https://doi.org/10.1111/2041-210X.13003)
"""
Base.@kwdef struct BalancedAcceptance{I <: Integer} <: BONSeeder
Base.@kwdef struct BalancedAcceptance{I <: Integer} <: BONSampler
numsites::I = 30
dims::Tuple{I, I} = (50, 50)
function BalancedAcceptance(numsites, dims)
bas = new{typeof(numsites)}(numsites, dims)
check_arguments(bas)
mask::BitMatrix = rand(50, 50) .< 0.5
function BalancedAcceptance(numsites::Integer, mask::BitMatrix)
bas = new{typeof(numsites)}(numsites, mask)
check_arguments(bas)
return bas
end
end

maxsites(bas::BalancedAcceptance) = prod(bas.dims)
BalancedAcceptance(M::Matrix{T}; numsites = 30) where T = BalancedAcceptance(numsites, size(M))
BalancedAcceptance(l::Layer; numsites = 30) = BalancedAcceptance(numsites, l.layer.indices)

maxsites(bas::BalancedAcceptance) = prod(size(bas.mask))

function check_arguments(bas::BalancedAcceptance)
check(TooFewSites, bas)
check(TooManySites, bas)
return nothing
end

function _generate!(
coords::Vector{CartesianIndex},
function _sample!(
coords::Sites,
ba::BalancedAcceptance,
)
seed = rand(Int32.(1e0:1e7), 2)
x, y = ba.dims
for (idx, ptct) in enumerate(eachindex(coords))
n = numsites(ba)
x,y = size(ba.mask)

# This is sequentially adding points, needs to check if that value is masked
# at each step and skip if so
exp_needed = 10 * Int(ceil(sum(ba.mask) / prod(size(ba.mask)) .* n))

ct = 1
for ptct in 1:exp_needed
i, j = haltonvalue(seed[1] + ptct, 2), haltonvalue(seed[2] + ptct, 3)
coords[idx] = CartesianIndex(convert.(Int32, [ceil(x * i), ceil(y * j)])...)
proposal = CartesianIndex(convert.(Int32, [ceil(x * i), ceil(y * j)])...)
if ct > n
break
end
if ba.mask[proposal]
coords[ct] = proposal
ct += 1
end
end
coords.coordinates = coords.coordinates[1:ct-1]
return coords
end

Expand Down
2 changes: 1 addition & 1 deletion src/cubesampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A `BONRefiner` that uses Cube Sampling (Tillé 2011)
**πₖ**, a Float Vector indicating the probabilities of inclusion for each candidate point; should sum to numsites value.
"""

Base.@kwdef struct CubeSampling{I <: Integer, M <: Matrix, V <: Vector} <: BONRefiner
Base.@kwdef struct CubeSampling{I <: Integer, M <: Matrix, V <: Vector} <: BONSampler
numsites::I = 50
fast::Bool = true
x::M = rand(0:4, 3, 50)
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Base.showerror(io::IO, e::E) where {E <: BONException} =
"{bold red}$(supertype(E)){/bold red} | {bold yellow}$(e.message){/bold yellow}\n",
)

function _check_arguments(sampler::S) where {S <: Union{BONSeeder, BONRefiner}}
function _check_arguments(sampler::BONSampler)
sampler.numsites > 1 || throw(TooFewSites())
return nothing
end
Expand Down
4 changes: 2 additions & 2 deletions src/fractaltriad.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
FractalTriad
A `BONSeeder` that generates `FractalTriad` designs
A `BONSampler` that generates `FractalTriad` designs
"""
Base.@kwdef struct FractalTriad{I <: Integer, F <: AbstractFloat} <: BONSeeder
Base.@kwdef struct FractalTriad{I <: Integer, F <: AbstractFloat} <: BONSampler
numsites::I = 81
horizontal_padding::F = 0.1
vertical_padding::F = 0.1
Expand Down
9 changes: 6 additions & 3 deletions src/grts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
@Olsen
"""
@kwdef struct GeneralizedRandomTessellatedStratified <: BONSeeder
@kwdef struct GeneralizedRandomTessellatedStratified <: BONSampler
numsites = 50
dims = (100, 100)
end

maxsites(grts::GeneralizedRandomTessellatedStratified) = prod(dims)
maxsites(grts::GeneralizedRandomTessellatedStratified) = prod(grts.dims)

function check_arguments(grts::GeneralizedRandomTessellatedStratified)
check(TooManySites, grts)
Expand Down Expand Up @@ -65,5 +65,8 @@ function _generate!(
code_numbers = sum([10^(i - 1) .* ag for (i, ag) in enumerate(address_grids)])
sort_idx = sortperm([code_numbers[cidx] for cidx in eachindex(code_numbers)])

return coords .= CartesianIndices(code_numbers)[sort_idx][1:(grts.numsites)]
return filter(
idx -> idx[1] <= grts.dims[1] && idx[2] <= grts.dims[2],
CartesianIndices(code_numbers)[sort_idx],
)[1:(grts.numsites)]
end
32 changes: 32 additions & 0 deletions src/refine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ function refine(sampler::ST) where {ST <: BONRefiner}
_inner(p) = refine!(coords, first(p), sampler)
return _inner
end

"""
seed!(coords::Vector{CartesianIndex}, sampler::ST, uncertainty::Matrix{T})
Puts a set of candidate sampling locations in the preallocated vector `coords`
from a raster `uncertainty` using `sampler`, where `sampler` is a [`BONSeeder`](@ref).
- Seeder's work on rasters, refiners work on set of coordinates.
"""
function seed!(
coords::Vector{CartesianIndex},
sampler::ST,
) where {ST <: BONSeeder}
length(coords) != sampler.numsites &&
throw(
DimensionMismatch(
"The length of the coordinate vector must match the `numsites` fiel s of the sampler",
),
)
return _generate!(coords, sampler)
end

"""
seed(sampler::ST)
Produces a set of candidate sampling locations in a vector `coords` of length numsites
from a raster using `sampler`, where `sampler` is a [`BONSeeder`](@ref).
"""
function seed(sampler::ST) where {ST <: BONSeeder}
coords = Vector{CartesianIndex}(undef, sampler.numsites)
return seed!(coords, sampler)
end
31 changes: 0 additions & 31 deletions src/seed.jl

This file was deleted.

Loading

0 comments on commit b155035

Please sign in to comment.