-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samplers, last commit before network api rework
- Loading branch information
1 parent
dec0b74
commit 1f722aa
Showing
3 changed files
with
48 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |