Skip to content

Commit

Permalink
Minor style nitpicks.
Browse files Browse the repository at this point in the history
- Vertical whitespace.
- Multiline strings.
- `; arg = arg` elisions.
- ..
  • Loading branch information
iago-lito committed Apr 18, 2023
1 parent 664e4fe commit 3eec5e7
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 169 deletions.
36 changes: 13 additions & 23 deletions src/measures/functioning.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#=
Quantifying functions
Adapted from BioenergeticFoodWeb.jl
Quantifying functions.
=#

"""
Expand Down Expand Up @@ -64,7 +63,7 @@ julia> richness([1, 1])
richness(n::AbstractVector; threshold = 0) = sum(n .> threshold)

"""
species_persistence(solution; threshold, kwargs...)
species_persistence(solution; kwargs...)
Returns the average proportion of species having a biomass superior or equal to threshold
over the `last` timesteps.
Expand Down Expand Up @@ -151,7 +150,7 @@ biomass(vec::AbstractVector;) = (total = mean(vec), species = mean(vec))


"""
shannon_diversity(solution; threshold, kwargs...)
shannon_diversity(solution; threshold = 0, kwargs...)
Computes the average Shannon entropy index, i.e. the first Hill number,
over the `last` timesteps.
Expand All @@ -167,14 +166,12 @@ https://en.wikipedia.org/wiki/Diversity_index#Shannon_index
"""
function shannon_diversity(solution; threshold = 0, kwargs...)
measure_on = extract_last_timesteps(solution; kwargs...)

shan = shannon_diversity.(eachcol(measure_on); threshold)
mean(shan)
end

function shannon_diversity(n::AbstractVector; threshold = 0)
x = filter(>(threshold), n)

if length(x) >= 1
p = x ./ sum(x)
p_ln_p = p .* log.(p)
Expand All @@ -187,7 +184,7 @@ end


"""
simpson(solution; threshold, kwargs...)
simpson(solution; threshold = 0, kwargs...)
Computes the average Simpson diversity index, i.e. the second Hill number,
over the `last` timesteps.
Expand All @@ -203,14 +200,12 @@ https://en.wikipedia.org/wiki/Diversity_index#Simpson_index
"""
function simpson(solution; threshold = 0, kwargs...)
measure_on = extract_last_timesteps(solution; kwargs...)

simp = simpson.(eachcol(measure_on); threshold)
mean(simp)
end

function simpson(n::AbstractVector; threshold = 0)
x = filter(>(threshold), n)

if length(x) >= 1
p = x ./ sum(x)
p2 = 2 .^ p
Expand All @@ -222,7 +217,7 @@ function simpson(n::AbstractVector; threshold = 0)
end

"""
evenness(solution; threshold, kwargs...)
evenness(solution; threshold = 0, kwargs...)
Computes the average Pielou evenness, over the `last` timesteps.
Expand All @@ -237,7 +232,6 @@ https://en.wikipedia.org/wiki/Species_evenness
"""
function evenness(solution; threshold = 0, kwargs...)
measure_on = extract_last_timesteps(solution; kwargs...)

piel = evenness.(eachcol(measure_on); threshold)
mean(piel)
end
Expand Down Expand Up @@ -335,7 +329,7 @@ See also [`simulate`](@ref), [`get_extinct_species`](@ref).
get_parameters(sol) = sol.prob.p.params

"""
trophic_structure(solution; threshold, kwargs...)
trophic_structure(solution; threshold = 0, idxs = nothing, kwargs...)
Returns the maximum, mean and weighted mean trophic level averaged over the `last`
timesteps. It also returns the adjacency matrix containing only the living species and the
Expand Down Expand Up @@ -371,10 +365,10 @@ julia> foodweb = FoodWeb([0 0 0; 0 1 0; 1 1 0]; quiet = true);
"""
function trophic_structure(solution; threshold = 0, idxs = nothing, kwargs...)

isnothing(idxs) || throw(ArgumentError("`trophic_structure()` operates at the whole \
network level, so it makes no sense to ask for \
particular species with anything other than \
`idxs = nothing`."))
isnothing(idxs) ||
throw(ArgumentError("`trophic_structure()` operates at the whole network level, \
so it makes no sense to ask for particular species \
with anything other than `idxs = nothing`."))


# Measure trophic structure over last timesteps
Expand All @@ -388,7 +382,6 @@ function trophic_structure(solution; threshold = 0, idxs = nothing, kwargs...)
alive = alive_trophic_network(measure_on[:, i], net; threshold)
tlvl = alive.trophic_level
bm = alive.species_biomass

push!(maxl, max_trophic_level(tlvl))
push!(avgl, mean_trophic_level(tlvl))
push!(wavg, weighted_average_trophic_level(bm, tlvl))
Expand Down Expand Up @@ -440,9 +433,9 @@ function alive_trophic_network(n::AbstractVector, A::AbstractMatrix; kwargs...)
end

docstring = """
max_trophic_level(solution; threshold, kwargs...)
mean_trophic_level(solution; threshold, kwargs...)
weighted_average_trophic_level(solution; threshold, kwargs...)
max_trophic_level(solution; threshold = 0, kwargs...)
mean_trophic_level(solution; threshold = 0, kwargs...)
weighted_average_trophic_level(solution; threshold = 0, kwargs...)
Return the aggregated trophic level over the `last` timesteps,
either with `max` or `mean` aggregation,
Expand Down Expand Up @@ -597,9 +590,7 @@ julia> B0 = [0, 0.5, 0.5];
"""
function living_species(solution::Solution; threshold = 0, idxs = nothing, kwargs...)
measure_on = extract_last_timesteps(solution; idxs, kwargs...)

alive_sp = living_species(measure_on; threshold)

sp = get_parameters(solution).network.species

tmp_idxs = process_idxs(solution; idxs)
Expand All @@ -611,7 +602,6 @@ end

living_species(mat::AbstractMatrix; threshold = 0) =
findall(>(threshold), biomass(mat).species)

living_species(n::AbstractVector; threshold = 0) = findall(>(threshold), n)

"""
Expand Down
19 changes: 7 additions & 12 deletions src/measures/stability.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=
Various measures of stability
Various measures of stability.
=#

"""
Expand All @@ -11,6 +11,7 @@ See [`coefficient_of_variation`](@ref) for the details.
"""
function species_cv(solution::Solution; threshold = 0, last = "10%", kwargs...)
measure_on = extract_last_timesteps(solution; last, kwargs...)

# Fetch species that are alive, whose avg biomass is > threshold
living_sp = living_species(measure_on; threshold)

Expand All @@ -21,7 +22,6 @@ function species_cv(solution::Solution; threshold = 0, last = "10%", kwargs...)
end

function species_cv(mat::AbstractMatrix; corrected = true)

if any(size(mat) .== 0)
average = NaN
species = NaN
Expand Down Expand Up @@ -59,19 +59,18 @@ function synchrony(
corrected = true,
kwargs...,
)

measure_on = extract_last_timesteps(solution; last, kwargs...)

# Fetch species that are alive, whose avg biomass is > threshold
living_sp = living_species(measure_on; threshold)

# Transpose to get the time x species matrix
mat = transpose(measure_on[living_sp, :])

synchrony(mat; corrected = corrected)

end
function synchrony(mat::AbstractMatrix; corrected = true)

function synchrony(mat::AbstractMatrix; corrected = true)
if any(size(mat) .== 0)
phi = NaN
else
Expand All @@ -82,9 +81,7 @@ function synchrony(mat::AbstractMatrix; corrected = true)

phi = com_var / std_sp^2
end

phi

end

"""
Expand All @@ -101,19 +98,18 @@ function community_cv(
corrected = true,
kwargs...,
)
measure_on = extract_last_timesteps(solution; last, kwargs...)

measure_on = extract_last_timesteps(solution; last = last, kwargs...)
# Fetch species that are alive, whose avg biomass is > threshold
living_sp = living_species(measure_on; threshold = threshold, kwargs...)
living_sp = living_species(measure_on; threshold, kwargs...)

# Transpose to get the time x species matrix
mat = transpose(measure_on[living_sp, :])

community_cv(mat; corrected)

end
function community_cv(mat::AbstractMatrix; corrected = true)

function community_cv(mat::AbstractMatrix; corrected = true)
if any(size(mat) .== 0)
cv_com = NaN
else
Expand Down Expand Up @@ -175,7 +171,6 @@ function coefficient_of_variation(
corrected = true,
kwargs...,
)

measure_on = extract_last_timesteps(solution; last, kwargs...)
# Fetch species that are alive, whose avg biomass is > threshold
alive_sp = living_species(measure_on; threshold)
Expand Down
Loading

0 comments on commit 3eec5e7

Please sign in to comment.