Skip to content

Commit

Permalink
samplers, last commit before network api rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gottacatchenall committed Jul 6, 2024
1 parent dec0b74 commit 1f722aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
8 changes: 7 additions & 1 deletion src/realizable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ function realizable(
E = nfl.energy / unique_localities
_scale = map(x -> _rate_matrix(x, θ, relabd_mat, E), net)

adj = sum(adjacency.(filter(!isnothing, _scale.network)))
adj = nothing
if typeof(_scale) <: Global
adj = adjacency(_scale)
else
adj = sum(adjacency.(filter(x->!isnothing(x), _scale.network)))
end

mw = SpeciesInteractionNetwork(net.metaweb.nodes, Binary(adj))


Expand Down
2 changes: 1 addition & 1 deletion src/realize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ end
function realize(net::Network{Realizable,SC}) where {SC}
_scale = map(_realize, net)

mw_adj = sum(adjacency.(filter(!isnothing, _scale.network)))
mw_adj = SC <: Global ? adjacency(_scale.network) : sum(adjacency.(filter(!isnothing, _scale.network)))

mw = SpeciesInteractionNetwork(
net.metaweb.nodes,
Expand Down
58 changes: 40 additions & 18 deletions src/samplers.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@

struct Static end

@kwdef struct SpeciesInteractionSampler
@kwdef struct SpeciesInteractionSampler{R,P}
_feasibility_model = NicheModel()
_range_model = Static
_phenology_model = Static
_range_model::R = Static()
_phenology_model::P = Static()
_abundance_model = NormalizedLogNormal()
_realization_model = NeutrallyForbiddenLinks()
_detection_model = RelativeAbundanceScaled()
end

struct Sample{L,A,D,G,T,Z,DT}
struct Sample{L,D}
λ::L
abd::A
δ::D
γ::G
θ::T
ζ::Z
detected::DT
# abd::A
# δ::D
# γ::G
# θ::T
# ζ::Z
detected::D
end
Base.show(io::IO, s::Sample) = tprint(io, "Sample with FNR = $(falsenegativerate(s))")

struct SampleSet{S<:Sample}
set::Vector{S}
end
Base.length(s::SampleSet) = length(s.set)
falsenegativerate(s::SampleSet) = mean(falsenegativerate.(s.set))
Base.show(io::IO, set::SampleSet) = tprint(io, "$(length(set)) samples with FNR = $(falsenegativerate(set))")


function sample(sampler::SpeciesInteractionSampler, n::Integer)
[sample(sampler) for _ in 1:n]
SampleSet([sample(sampler) for _ in 1:n])
end
falsenegativerate(s::S) where {S<:Sample} = 1 - (sum(adjacency(s.detected.metaweb) .> 0) / sum(adjacency(s.λ.metaweb)))

function sample(sampler::SpeciesInteractionSampler)
_get_possible(::SpeciesInteractionSampler{R,P}, λ) where {R<:Static, P<:Static} = λ
_get_possible(sampler::SpeciesInteractionSampler{R,P}, λ) where {R<:RangeGenerator, P<:Static} = begin
ranges = generate(sampler._range_model, richness(λ))
possible(λ, ranges)
end
_get_possible(sampler::SpeciesInteractionSampler{R,P}, λ) where {R<:Static, P<:PhenologyGenerator} = begin
phen = generate(sampler._phenology_model, richness(λ))
possible(λ, phen)
end
_get_possible(sampler::SpeciesInteractionSampler{R,P}, λ) where {R<:RangeGenerator, P<:PhenologyGenerator} = begin
ranges = generate(sampler._range_model, richness(λ))
phen = generate(sampler._phenology_model, richness(λ))
possible(λ, ranges, phen)
end

function sample(sampler::SpeciesInteractionSampler{R,S}) where {R,S}
λ = generate(sampler._feasibility_model)
abd = generate(sampler._abundance_model, λ)
δ = detectability(sampler._detection_model, λ, abd)
if sampler._range_model == Static && sampler._phenology_model <: Static
θ = realizable(sampler._realization_model, λ, abd)
ζ = realize(θ)
D = detect(ζ, δ)
Sample(λ, abd, δ, λ, θ, ζ, D)
end
γ = _get_possible(sampler, λ)
θ = realizable(sampler._realization_model, γ, abd)
ζ = realize(θ)
D = detect(ζ, δ)
return Sample(λ, D)
end

0 comments on commit 1f722aa

Please sign in to comment.