Skip to content

Commit

Permalink
vitepress, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
gottacatchenall committed Dec 13, 2024
1 parent 21aa91e commit 9817f11
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 47 deletions.
5 changes: 3 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[deps]
BiodiversityObservationNetworks = "a5b868d3-191d-4bba-a38a-ad28190da010"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
DocumenterMarkdown = "997ab1e6-3595-5248-9280-8efb232c3433"
DocumenterTools = "35a29f4d-8980-5a13-9543-d66fff28ecb8"
DocumenterVitepress = "4710194d-e776-4893-9690-8d956a29c365"
HaltonSequences = "13907d55-377f-55d6-a9d6-25ac19e11b95"
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NeutralLandscapes = "71847384-8354-4223-ac08-659a5128069f"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SimpleSDMLayers = "2c645270-77db-11e9-22c3-0f302a89c64c"
SliceMap = "82cb661a-3f19-5665-9e27-df437c7e54c8"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
SpeciesDistributionToolkit = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
17 changes: 8 additions & 9 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ push!(LOAD_PATH, "../src/")

using Documenter
using DocumenterCitations
using DocumenterMarkdown
using DocumenterVitepress
using BiodiversityObservationNetworks

bibliography = CitationBibliography(joinpath(@__DIR__, "BONs.bib"))
bib = CitationBibliography(joinpath(@__DIR__, "BONs.bib"))

makedocs(
bibliography;
sitename = "BiodiversityObservationNetwork.jl",
authors = "Michael D. Catchen, Timothée Poisot, Kari Norman, Hana Mayall, Tom Malpas",
modules = [BiodiversityObservationNetworks],
format = Markdown(),

format = DocumenterVitepress.MarkdownVitepress(
repo="https://github.com/gottacatchenall/BiodiversityObservationNetworks.jl",
devurl="dev",
),
warnonly = true,
plugins = [bib]
)


deploydocs(;
deps = Deps.pip("mkdocs", "pygments", "python-markdown-math", "mkdocs-material"),
repo = "github.com/PoisotLab/BiodiversityObservationNetworks.jl.git",
devbranch = "main",
make = () -> run(`mkdocs build`),
target = "site",
push_preview = true,
)
15 changes: 5 additions & 10 deletions src/adaptivehotspot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,26 @@
- **pool**: the sites that could potentially be picked
- **uncertainty**: a `Layer` specifying the current uncertainty at each site
"""
Base.@kwdef mutable struct AdaptiveHotspot{T <: Integer, F <: AbstractFloat} <: BONSampler
Base.@kwdef mutable struct AdaptiveHotspot{T <: Integer} <: BONSampler
numsites::T = 30
uncertainty::Layer = Layer(rand(50, 50))
function AdaptiveHotspot(numsites, uncertainty)
as = new{typeof(numsites), typeof(uncertainty[begin])}(numsites, uncertainty)
function AdaptiveHotspot(numsites)
as = new{typeof(numsites)}(numsites)
check_arguments(as)
return as
end
end

AdaptiveHotspot(uncertainty::Matrix{T}; numsites = 30) where T = AdaptiveHotspot(numsites, uncertainty)

maxsites(as::AdaptiveHotspot) = prod(size(as.uncertainty))
function check_arguments(as::AdaptiveHotspot)
check(TooFewSites, as)
check(TooManySites, as)
return nothing
end

function _generate!(
coords::Vector{CartesianIndex},
pool::Vector{CartesianIndex},
sampler::AdaptiveHotspot,
)
uncertainty = sampler.uncertainty
uncertainty::L
) where L<:Layer
# Distance matrix (inlined)
d = zeros(Float64, Int((sampler.numsites * (sampler.numsites - 1)) / 2))

Expand Down
2 changes: 1 addition & 1 deletion src/balancedacceptance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://doi.org/10.1111/2041-210X.13003)
Base.@kwdef struct BalancedAcceptance{I<:Integer} <: BONSampler
numsites::I = 30
dims::Tuple{I,I} = (50,50)
function BalancedAcceptance(numsites::I, dims::Tuple{I,I}) where I<:Integer
function BalancedAcceptance(numsites::I, dims::Tuple{J,J}) where {I<:Integer, J<:Integer}
bas = new{I}(numsites, dims)
check_arguments(bas)
return bas
Expand Down
14 changes: 10 additions & 4 deletions src/fractaltriad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Base.@kwdef struct FractalTriad{I <: Integer, F <: AbstractFloat} <: BONSampler
return ft
end
end
_default_pool(ft::FractalTriad) = pool(ft.dims)


maxsites(ft::FractalTriad) = maximum(ft.dims) * 10 # gets numerically unstable for large values because float coords belong to the the same cell in the raster, and arctan breaks
function check_arguments(ft::FractalTriad)
Expand Down Expand Up @@ -121,12 +123,16 @@ end
Fills `coords` with a set of points generated using the `FractalTriad` generator `ft`.
"""
function _generate!(
coords::Vector{CartesianIndex},
function _sample!(
coords::S,
candidates::C,
ft::FractalTriad,
)
) where {S<:Sites,C<:Sites}
base_triangle = _outer_triangle(ft)
coords[1:3] .= base_triangle

coords.coordinates .= CartesianIndex(1,1)

coords.coordinates[1:3] .= base_triangle
count = 4

triangle = coords[1:3]
Expand Down
2 changes: 1 addition & 1 deletion src/sample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end
function sample(alg::BONSampler, l::L) where L<:Layer
_sample!(
_allocate_sites(numsites(alg)),
pool(l),
l,
alg
)
end
Expand Down
11 changes: 11 additions & 0 deletions src/simplerandom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ end

_default_pool(::SimpleRandom) = pool((50,50))

function sample(::SimpleRandom, ::Layer)
end
function sample(::SimpleRandom, ::Matrix)
end
function sample(sr::SimpleRandom, T::Tuple{I,I}) where I <: Integer

end




function _sample!(
selections::S,
candidates::C,
Expand Down
3 changes: 3 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ end
_allocate_sites(n) = Sites(Array{CartesianIndex}(undef, n))
coordinates(s::Sites) = s.coordinates
Base.getindex(s::Sites, i::Integer) = getindex(coordinates(s), i)
Base.getindex(s::Sites, i::UnitRange) = getindex(coordinates(s), i)

Base.setindex!(s::Sites, v, i::Integer) = setindex!(coordinates(s), v,i)
Base.length(s::Sites) = length(coordinates(s))
Base.eachindex(s::Sites) = eachindex(s.coordinates)
Expand Down Expand Up @@ -46,6 +48,7 @@ function Layer(l::S; categorical = false) where S<:SimpleSDMLayers.SDMLayer
Layer{T,S}(l)
end
Layer(m::Matrix{I}) where I<:Integer = Layer{CategoricalData,typeof(m)}(m)
Layer(m::Matrix{R}) where R<:Real = Layer{ContinousData,typeof(m)}(m)

_indices(m::M) where M<:Matrix = findall(i->!isnothing(m[i]) && !isnan(m[i]) && !ismissing(m[i]), CartesianIndices(m))
_indices(l::SDMLayer) = findall(l.indices)
Expand Down
37 changes: 17 additions & 20 deletions src/weightedbas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,39 @@
A `BONSeeder` that uses Balanced-Acceptance Sampling [@cite] combined with rejection sampling to create a set of sampling sites that is weighted toward values with higher uncertainty as a function of the parameter α.
"""
Base.@kwdef struct WeightedBalancedAcceptance{I <: Integer,L<:Layer, F <: Real} <: BONSampler
Base.@kwdef struct WeightedBalancedAcceptance{I <: Integer, F <: Real} <: BONSampler
numsites::I = 30
uncertainty::L = Layer(rand(50, 50))
α::F = 1.0
function WeightedBalancedAcceptance(numsites, uncertainty::L, α) where L
wbas = new{typeof(numsites), L, typeof(α)}(numsites, uncertainty, α)
function WeightedBalancedAcceptance(numsites, α)
wbas = new{typeof(numsites), typeof(α)}(numsites, α)
check_arguments(wbas)
return wbas
end
end

maxsites(wbas::WeightedBalancedAcceptance) = prod(size(wbas.uncertainty))

function check_arguments(wbas::WeightedBalancedAcceptance)
check(TooFewSites, wbas)
check(TooManySites, wbas)
wbas.α > 0 ||
throw(
ArgumentError("WeightedBalancedAcceptance requires α to be greater than 0 "),
)
return nothing
end

function _generate!(
coords::Vector{CartesianIndex},
function _sample!(
coords::S,
candidates::C,
sampler::WeightedBalancedAcceptance{I, T},
) where {I <: Integer, T <: AbstractFloat}
weights::L
) where {S<:Sites,C<:Sites,I <: Integer, T <: AbstractFloat,L<:Layer}
seed = rand(I.(1e0:1e7), 2)
uncertainty = sampler.uncertainty
α = sampler.α
x, y = size(uncertainty)
x, y = size(weights)

nonnan_indices = findall(!isnan, uncertainty)
stduncert = similar(uncertainty)
nonnan_indices = findall(!isnan, weights)
stduncert = similar(weights)

uncert_values = uncertainty[nonnan_indices]
uncert_values = weights[nonnan_indices]
stduncert_values = similar(uncert_values)
zfit = nothing
if var(uncert_values) > 0
Expand All @@ -47,8 +44,8 @@ function _generate!(
end

nonnan_counter = 1
for i in eachindex(uncertainty)
if isnan(uncertainty[i])
for i in eachindex(weights)
if isnan(weights[i])
stduncert[i] = NaN
elseif !isnothing(zfit)
stduncert[i] = stduncert_values[nonnan_counter]
Expand Down Expand Up @@ -95,12 +92,12 @@ end
numpts, numcandidates = 26, 25
@test numpts > numcandidates # who watches the watchmen?
dims = Int.(floor.((sqrt(numcandidates), sqrt(numcandidates))))
uncert = rand(dims...)
uncert = Layer(rand(dims...))

@test_throws TooManySites WeightedBalancedAcceptance(
bas = WeightedBalancedAcceptance(
numsites = numpts,
uncertainty = uncert,
)
@test_throws TooManySites sample(bas, uncert)
end

@testitem "WeightedBalancedAcceptance can take bias parameter α as keyword argument" begin
Expand Down

0 comments on commit 9817f11

Please sign in to comment.