From 9817f11e749bbbe46a291955b3d89eb17dd6023e Mon Sep 17 00:00:00 2001 From: "michael d. catchen" Date: Fri, 13 Dec 2024 13:34:14 -0500 Subject: [PATCH] vitepress, etc. --- docs/Project.toml | 5 +++-- docs/make.jl | 17 ++++++++--------- src/adaptivehotspot.jl | 15 +++++---------- src/balancedacceptance.jl | 2 +- src/fractaltriad.jl | 14 ++++++++++---- src/sample.jl | 2 +- src/simplerandom.jl | 11 +++++++++++ src/types.jl | 3 +++ src/weightedbas.jl | 37 +++++++++++++++++-------------------- 9 files changed, 59 insertions(+), 47 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 71daffa..3512144 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,10 +1,11 @@ [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" @@ -12,7 +13,7 @@ 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" diff --git a/docs/make.jl b/docs/make.jl index 89eee49..3190e93 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -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, ) diff --git a/src/adaptivehotspot.jl b/src/adaptivehotspot.jl index c83bb7a..c3c9afb 100644 --- a/src/adaptivehotspot.jl +++ b/src/adaptivehotspot.jl @@ -5,22 +5,17 @@ - **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 @@ -28,8 +23,8 @@ 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)) diff --git a/src/balancedacceptance.jl b/src/balancedacceptance.jl index 4f5d03d..559e741 100644 --- a/src/balancedacceptance.jl +++ b/src/balancedacceptance.jl @@ -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 diff --git a/src/fractaltriad.jl b/src/fractaltriad.jl index 0693c35..abc8172 100644 --- a/src/fractaltriad.jl +++ b/src/fractaltriad.jl @@ -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) @@ -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] diff --git a/src/sample.jl b/src/sample.jl index 3e358e9..61c046e 100644 --- a/src/sample.jl +++ b/src/sample.jl @@ -11,7 +11,7 @@ end function sample(alg::BONSampler, l::L) where L<:Layer _sample!( _allocate_sites(numsites(alg)), - pool(l), + l, alg ) end diff --git a/src/simplerandom.jl b/src/simplerandom.jl index 7f10c82..ce853db 100644 --- a/src/simplerandom.jl +++ b/src/simplerandom.jl @@ -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, diff --git a/src/types.jl b/src/types.jl index 7c3212b..358594a 100644 --- a/src/types.jl +++ b/src/types.jl @@ -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) @@ -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) diff --git a/src/weightedbas.jl b/src/weightedbas.jl index 60e4d1e..c040392 100644 --- a/src/weightedbas.jl +++ b/src/weightedbas.jl @@ -3,22 +3,18 @@ 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 "), @@ -26,19 +22,20 @@ function check_arguments(wbas::WeightedBalancedAcceptance) 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 @@ -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] @@ -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