Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Gower #49

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/doc-cleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages branch
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Delete preview and history + push changes
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up julia
uses: julia-actions/setup-julia@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up julia
uses: julia-actions/setup-julia@v1
with:
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up julia
uses: julia-actions/setup-julia@v1
with:
Expand All @@ -41,17 +41,20 @@ jobs:
uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.R-version }}
- name: Install ape and rdiversity
- name: Install ape, vegan and rdiversity
if: matrix.os == 'macOS-latest'
run: |
install.packages("ape", repos="http://cran.r-project.org")
install.packages("vegan", repos="http://cran.r-project.org")
install.packages("rdiversity", repos="http://cran.r-project.org")
shell: R --vanilla --file={0}
- name: Build package
uses: julia-actions/julia-buildpkg@v1
- name: Running
uses: julia-actions/julia-runtest@v1
- name: Process coverage
if: matrix.os == 'macOS-latest'
uses: julia-actions/julia-processcoverage@v1
- name: Codecov
if: matrix.os == 'macOS-latest'
uses: codecov/codecov-action@v3
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Diversity"
uuid = "d3d5718d-52de-57ab-b67a-eca7fd6175a4"
author = ["Richard Reeve <Richard.Reeve@glasgow.ac.uk>"]
version = "0.5.8"
version = "0.5.9"

[deps]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
Expand Down
3 changes: 2 additions & 1 deletion src/Diversity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ export generalisedrichness, richness
export generalisedshannon, shannon
export generalisedsimpson, simpson
export generalisedjaccard, jaccard
export generalisedpielou, pielou
export pielou
export gower
end # sub-module Ecology

"""
Expand Down
90 changes: 63 additions & 27 deletions src/Ecology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ end
"""
generalisedjaccard(proportions::AbstractArray, qs, Z::AbstractMatrix)
generalisedjaccard(proportions::AbstractArray, qs, sim::AbstractTypes)
generalisedjaccard(meta::AbstractAssemblage, qs)

Calculates a generalisation of the Jaccard index of two columns
representing the counts of two subcommunities. This evaluates to raw
Expand All @@ -223,8 +224,7 @@ better properties.
#### Arguments:

- `proportions`: population proportions

- `qs`: single number or vector of values of parameter q
- `meta`: metacommunity / assemblage

- `Z`: similarity matrix or
- `sim`: instance of AbstractTypes
Expand All @@ -235,33 +235,30 @@ better properties.
"""
function generalisedjaccard end

generalisedjaccard(proportions::AbstractMatrix, qs,
Z::AbstractMatrix = Matrix(1.0I, size(proportions, 1),
size(proportions, 1))) =
generalisedjaccard(proportions, qs, GeneralTypes(Z))
generalisedjaccard(proportions::AbstractMatrix, Z::AbstractMatrix) =
generalisedjaccard(proportions, GeneralTypes(Z))

function generalisedjaccard(proportions::AbstractMatrix, qs,
sim::AbstractTypes)
meta = Metacommunity(proportions, sim)
generalisedjaccard(proportions::AbstractMatrix, sim::AbstractTypes) =
generalisedjaccard(Metacommunity(proportions, sim))

function generalisedjaccard(meta::AbstractAssemblage)
countsubcommunities(meta) == 2 ||
error("Can only calculate Jaccard index for 2 subcommunities")
ab = metadiv(α(meta), qs)
g = metadiv(Γ(meta), qs)
j = innerjoin(ab, g, on=[:q, :type_level, :type_name,
:partition_level, :partition_name, :div_type],
makeunique=true)
j[!,:diversity] .= j[!,:diversity] ./ j[!,:diversity_1] .- 1
j[!,:measure] .= "Jaccard"
select!(j, Not([:diversity_1, :measure_1]))
return j
num = sum(abs.(diff(getordinariness!(meta), dims = 2)))
denom = sum(maximum(getordinariness!(meta), dims = 2))
jac = metadiv(Gamma(meta), 0)
jac[!,:diversity] .= num / denom
jac[!,:measure] .= "Jaccard"
select!(jac, Not([:q]))
return jac
end

"""
jaccard(proportions::AbstractMatrix)
jaccard(asm::AbstractAssemblage)

Calculates Jaccard index (Jaccard similarity coefficient) of two
columns representing independent subcommunity counts, which is
normmetaalpha(proportions, 0) / metagamma(proportions, 0) - 1
columns representing independent subcommunity counts

#### Arguments:

Expand All @@ -271,16 +268,15 @@ normmetaalpha(proportions, 0) / metagamma(proportions, 0) - 1

- the Jaccard index
"""
jaccard(proportions::AbstractMatrix) =
generalisedjaccard(proportions, 0,
UniqueTypes(size(proportions, 1)))
jaccard(proportions::AbstractMatrix) = jaccard(Metacommunity(proportions))

function jaccard(asm::AbstractAssemblage)
hassimilarity(asm) &&
error("function cannot run with $(typeof(gettypes(asm))) types as contains similarity")
return jaccard(occurrences(asm))
return generalisedjaccard(asm)
end

#=
"""
generalisedpielou(level::DiversityLevel,
proportions::AbstractArray,
Expand All @@ -295,7 +291,7 @@ Calculates a generalisation of Pielou's evenness for columns
representing the counts or proportions of subcommunities. Values range from
zero to one, with one representing complete evenness within the
community. Since this is calculated as H / Hmax, this uses Shannon entropy
and q is effectively 1.
and q is effectively 1.

#### Arguments:
- `level`: DiversityLevel to calculate at (e.g. subcommunityDiversity)
Expand All @@ -308,6 +304,7 @@ and q is effectively 1.
#### Returns:
- Pielou's evenness metric (at metacommunity level) or metrics (of subcommunities)
"""
=#
function generalisedpielou end
generalisedpielou(level::DiversityLevel,
proportions::AbstractArray,
Expand All @@ -322,7 +319,7 @@ generalisedpielou(level::DiversityLevel,
function generalisedpielou(level::DiversityLevel,
mc::AbstractAssemblage)
hassimilarity(mc) &&
error("Can't calculate Pielou function for $(typeof(gettypes(mc))) type as ill-defined maximum entropy")
error("Can't calculate Pielou evenness for $(typeof(gettypes(mc))) type as ill-defined maximum entropy")

if (level == subcommunityDiversity)
dm = ᾱ
Expand All @@ -345,7 +342,7 @@ end

"""
pielou(proportions::AbstractMatrix)
pielou(proportions::AbstractAssemblage)
pielou(asm::AbstractAssemblage)

Calculates Pielou's evenness of a series of
columns representing independent subcommunity counts.
Expand Down Expand Up @@ -378,3 +375,42 @@ function pielou(asm::AbstractAssemblage)
error("function cannot run with $(typeof(gettypes(asm))) types as contains similarity")
return generalisedpielou(subcommunityDiversity, asm)
end

"""
gower(proportions::AbstractMatrix; countzeros::Bool = false, logscale::Bool = true)
gower(asm::AbstractAssemblage; countzeros::Bool = false, logscale::Bool = true)

Calculates Gower's dissimarity of up to two columns representing independent subcommunity counts.

#### Arguments:

- `proportions`: population proportions; or
- `count`: population counts; or
- `asm`: Abstract Assemblage
- ``

#### Returns:

- Gower dissimilarity of the subcommunities
"""
function gower end

gower(proportions::AbstractArray; countzeros::Bool = false, logscale::Bool = false, normalise::Bool = countzeros) =
gower(Metacommunity(proportions), countzeros = countzeros, logscale = logscale, normalise = normalise)

function gower(asm::AbstractAssemblage; countzeros::Bool = false, logscale::Bool = false, normalise::Bool = countzeros)
countsubcommunities(asm) == 2 ||
error("Can only calculate Gower distances for 2 subcommunities")

g = meta_gamma(asm, 0)
nz = countzeros ? nthings(asm) : noccurring(asm)
occ = logscale ? [x == 0 ? 0 : log10(x) for x in getabundance(asm, true)] : getabundance(asm, true)
diff = normalise ? sum(x > 0 ? 1 : 0 for x in abs.(occ[:, 1] .- occ[:, 2])) : sum(abs.(occ[:, 1] .- occ[:, 2]))
g[!, :diversity] .= diff / nz
g[!, :measure] .= "Gower"
g[!, :countzeros] .= countzeros
g[!, :logscale] .= logscale
g[!, :normalise] .= normalise
select!(g, Not(:q))
return g
end
Loading