Skip to content

Commit

Permalink
🚧 Minor fixes to basic output analyses functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
iago-lito committed Apr 19, 2024
1 parent 7745c2f commit 560cf95
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ makedocs(;
],
)

deploydocs(; repo = "github.com/BecksLab/EcologicalNetworksDynamics.jl", devbranch = "doc")
deploydocs(; repo = "github.com/BecksLab/EcologicalNetworksDynamics.jl", devbranch = "dev")
4 changes: 0 additions & 4 deletions src/EcologicalNetworksDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ include("./nontrophic_layers.jl")
#-------------------------------------------------------------------------------------------
# Analysis tools working on the output of the simulation.
include("output-analysis.jl")
export richness
export persistence
export shannon_diversity
export total_biomass

# Avoid Revise interruptions when redefining methods and properties.
Framework.REVISING = true
Expand Down
70 changes: 35 additions & 35 deletions src/output-analysis.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
richness(solution::Solution; threshold = 0)
richness(solution::Solution; threshold = 0)
Return the number of alive species at each timestep of the simulation.
`solution` is the output of [`simulate`](@ref).
Expand All @@ -14,7 +14,8 @@ Let's start with a simple example where the richness remains constant:
julia> foodweb = Foodweb([0 0; 1 0])
m = default_model(foodweb)
B0 = [0.5, 0.5]
sol = simulate(m, B0)
tmax = 100
sol = simulate(m, B0, tmax)
richness_trajectory = richness(sol)
all(richness_trajectory .== 2) # At each timestep, there are 2 alive species.
true
Expand All @@ -26,36 +27,33 @@ We expect to observe a decrease in richness from 1 to 0 over time.
```jldoctest
julia> B0 = [0, 0.5] # The producer is extinct at the beginning.
sol = simulate(m, B0)
sol = simulate(m, B0, 1_000)
richness_trajectory = richness(sol)
richness_trajectory[1] == 1 && richness_trajectory[end] == 0
true
```
"""
richness(solution::AbstractODESolution; threshold = 0) = richness.(solution.u; threshold)
richness(solution::Solution; threshold = 0) = richness.(solution.u; threshold)
export richness

"""
richness(vec::AbstractVector; threshold = 0)
richness(biomasses::AbstractVector; threshold = 0)
Return the number of alive species given a biomass vector `vec`.
Return the number of alive species given a biomass vector.
By default, species are considered extinct if their biomass is 0.
But, this `threshold` can be changed using the corresponding keyword argument.
# Examples
```jldoctest
julia> foodweb = Foodweb([0 0; 1 0])
m = default_model(foodweb)
B0 = [0.5, 0.5]
sol = simulate(m, B0)
richness(sol[end]) # Richness at the end of the simulation.
2.0
julia> richness([0.2, 0, 0.3]) # Only two species are non-extinct in this biomass vector.
2
```
"""
richness(vec::AbstractVector; threshold = 0) = count(>(threshold), vec)
richness(biomasses::AbstractVector; threshold = 0) = count(>(threshold), biomasses)

"""
persistence(solution::AbstractODESolution; threshold = 0)
persistence(solution::Solution; threshold = 0)
Fraction of alive species at each timestep of the simulation.
See [`richness`](@ref) for details.
Expand All @@ -67,25 +65,25 @@ julia> S = 20 # Initial number of species.
foodweb = Foodweb(:niche; S = 20, C = 0.1)
m = default_model(foodweb)
B0 = rand(S)
sol = simulate(m, B0)
sol = simulate(m, B0, 2000)
all(persistence(sol) .== richness(sol) / S)
true
```
"""
function persistence(solution::AbstractODESolution; threshold = 0)
persistence.(solution.u; threshold)
end
persistence(solution::Solution; threshold = 0) = persistence.(solution.u; threshold)
export persistence

"""
persistence(vec::AbstractVector; threshold = 0)
persistence(biomasses::AbstractVector; threshold = 0)
Fraction of alive species given a biomass vector `vec`.
Fraction of alive species given a biomass vector.
See [`richness`](@ref) for details.
"""
persistence(vec::AbstractVector; threshold = 0) = richness(vec; threshold) / length(vec)
persistence(biomasses::AbstractVector; threshold = 0) =
richness(biomasses; threshold) / length(biomasses)

"""
total_biomass(solution::AbstractODESolution)
total_biomass(solution::Solution)
Total biomass of a community at each timestep of the simulation.
`solution` is the output of [`simulate`](@ref).
Expand All @@ -100,18 +98,19 @@ so we can observe the consumer's biomass decrease over time.
julia> foodweb = Foodweb([0 0; 1 0])
m = default_model(foodweb)
B0 = [0, 0.5] # The producer is extinct at the beginning.
sol = simulate(m, B0)
sol = simulate(m, B0, 1_000)
biomass_trajectory = total_biomass(sol)
biomass_trajectory[1] == 0.5 && biomass_trajectory[end] == 0
true
```
"""
total_biomass(solution::AbstractODESolution) = total_biomass.(solution.u)
total_biomass(solution::Solution) = total_biomass.(solution.u)
export total_biomass

"""
total_biomass(vec::AbstractVector)
total_biomass(biomasses::AbstractVector)
Total biomass of a community given a biomass vector `vec`.
Total biomass of a community given a biomass vector.
# Examples
Expand All @@ -120,10 +119,10 @@ julia> total_biomass([0.5, 1.5]) # 0.5 + 1.5 = 2.0
2.0
```
"""
total_biomass(vec::AbstractVector) = sum(vec)
total_biomass(biomasses::AbstractVector) = sum(biomasses)

"""
shannon_diversity(solution::AbstractODESolution; threshold = 0)
shannon_diversity(solution::Solution; threshold = 0)
Shannon diversity index at each timestep of the simulation.
`solution` is the output of [`simulate`](@ref).
Expand All @@ -142,20 +141,21 @@ as the biomass of the species diverge from each other.
julia> foodweb = Foodweb([0 0; 1 0])
m = default_model(foodweb)
B0 = [0.5, 0.5] # Even biomass, maximal shannon diversity.
sol = simulate(m, B0)
sol = simulate(m, B0, 1_000)
shannon_trajectory = shannon_diversity(sol)
biomass_trajectory[1] > biomass_trajectory[end]
true
```
"""
function shannon_diversity(solution::AbstractODESolution; threshold = 0)
shannon_diversity(solution::Solution; threshold = 0) =
shannon_diversity.(solution.u; threshold)
end
export shannon_diversity

"""
shannon_diversity(vec::AbstractVector; threshold = 0)
shannon_diversity(biomasses::AbstractVector; threshold = 0)
Shannon diversity index given a biomass vector.
Shannon diversitty index given a biomass vector `vec
Shannon diversity is a measure of species diversity based on the entropy.
According to the Shannon index, for a same number of species,
the more evenly the biomass is distributed among them,
Expand All @@ -176,8 +176,8 @@ true
We observe as we decrease the biomass of the third species,
the shannon diversity tends to 2, as we tend towards an effective two-species community.
"""
function shannon_diversity(vec::AbstractVector; threshold = 0)
x = filter(>(threshold), vec)
function shannon_diversity(biomasses::AbstractVector; threshold = 0)
x = filter(>(threshold), biomasses)
p = x ./ sum(x)
exp(-sum(p .* log.(p)))
end

0 comments on commit 560cf95

Please sign in to comment.