Skip to content

Commit

Permalink
Merge pull request #51 from EcoJulia/rr/phylo
Browse files Browse the repository at this point in the history
Fix type issues in Phylo extension
  • Loading branch information
richardreeve authored Apr 15, 2024
2 parents 8f8009c + 68db0a7 commit cf827ee
Show file tree
Hide file tree
Showing 39 changed files with 896 additions and 732 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up julia
uses: julia-actions/setup-julia@v1
uses: julia-actions/setup-julia@v2
with:
version: '1'
arch: x64
Expand All @@ -29,10 +29,10 @@ jobs:
Pkg.instantiate()
Pkg.add("Phylo")
Pkg.add("Documenter")
shell: julia --project=docs/ --color=yes {0}
shell: julia --project=docs --color=yes {0}
- name: Build and deploy
run: |
julia --project=docs/ --color=yes docs/make.jl
julia --project=docs --color=yes docs/make.jl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/nightly.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: JuliaNightly
# Nightly Scheduled Julia Nightly Run

on:
push:
branches:
- dev
tags:
- 'v*'
schedule:
- cron: '0 2 * * 0' # Weekly at 2 AM UTC Sunday
workflow_dispatch:

jobs:
test:
Expand All @@ -17,11 +14,13 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up julia
uses: julia-actions/setup-julia@v1
uses: julia-actions/setup-julia@v2
with:
version: nightly
arch: x64
- name: Build package
uses: julia-actions/julia-buildpkg@v1
with:
ignore-no-cache: true
- name: Run tests
uses: julia-actions/julia-runtest@v1
15 changes: 12 additions & 3 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ on:
- 'v*'
pull_request:

permissions:
actions: write
contents: read

jobs:
diversity-tests:
Diversity-tests:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
strategy:
Expand All @@ -32,7 +36,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up julia
uses: julia-actions/setup-julia@v1
uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.julia-version }}
arch: ${{ matrix.arch }}
Expand All @@ -48,11 +52,16 @@ jobs:
install.packages("vegan", repos="http://cran.r-project.org")
install.packages("rdiversity", repos="http://cran.r-project.org")
shell: R --vanilla --file={0}
- name: Cache
uses: julia-actions/cache@v1
- name: Build package
uses: julia-actions/julia-buildpkg@v1
- name: Running
uses: julia-actions/julia-runtest@v1
- name: Process coverage
uses: julia-actions/julia-processcoverage@v1
- name: Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# NEWS

- v0.5.12
- Move Phylo structs into extension
- v0.5.11
- Update Phylo compat
- v0.5.10
- Use extensions for Julia 1.9+
- Allow AxisArrays to name types
- Allow AxisArrays to name types through extension
- Improve testing
- v0.5.9
- Introduce Gower
Expand Down
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>", "Claire Harris", "Isaac Peetom Heida"]
version = "0.5.11"
version = "0.5.12"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
10 changes: 6 additions & 4 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ using Diversity
using Phylo

makedocs(modules = [Diversity,
Diversity.Ecology, Diversity.Jost,
Diversity.Hill,
Diversity.ShortNames, Diversity.API],
Diversity.Ecology, Diversity.Jost,
Diversity.Hill,
Diversity.ShortNames, Diversity.API],
sitename = "Diversity.jl")

deploydocs(repo = "github.com/EcoJulia/Diversity.jl.git",
devbranch = "dev")
push_preview = true,
devbranch = "dev",
devurl = "dev")
19 changes: 9 additions & 10 deletions ext/DiversityAxisArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
module DiversityAxisArraysExt

isdefined(Base, :get_extension) ? (using AxisArrays) : (using ..AxisArrays)
using Diversity

import Diversity: GeneralTypes
function GeneralTypes(zmatrix::AM) where
{FP <: AbstractFloat, M <: AbstractMatrix{FP}, LR, LC, NAMES,
T <: Tuple{Axis{LR, NAMES}, Axis{LC, NAMES}},
AM <: AxisMatrix{FP, M, T}}
import Diversity

function Diversity.GeneralTypes(zmatrix::AM) where
{FP <: AbstractFloat, M <: AbstractMatrix{FP}, LR, LC, NAMES,
T <: Tuple{Axis{LR, NAMES}, Axis{LC, NAMES}},
AM <: AxisMatrix{FP, M, T}}
size(zmatrix, 1) == size(zmatrix, 2) ||
throw(DimensionMismatch("Similarity matrix is not square"))
throw(DimensionMismatch("Similarity matrix is not square"))

AxisArrays.axes(zmatrix, 1).val == AxisArrays.axes(zmatrix, 2).val ||
throw(DimensionMismatch("Similarity matrix does not have matching row and column labels"))
throw(DimensionMismatch("Similarity matrix does not have matching row and column labels"))

return GeneralTypes(zmatrix, collect(AxisArrays.axes(zmatrix, 1).val))
return Diversity.GeneralTypes(zmatrix,
collect(AxisArrays.axes(zmatrix, 1).val))
end

end
144 changes: 79 additions & 65 deletions ext/DiversityPhyloExt.jl
Original file line number Diff line number Diff line change
@@ -1,83 +1,97 @@
module DiversityPhyloExt

isdefined(Base, :get_extension) ? (using Phylo) : (using ..Phylo)
import Diversity: AbstractPhyloTypes
import Diversity: PhyloBranches
using Diversity
import Diversity
using Diversity.API
using Statistics
using Diversity: AbstractPhyloTypes

import Diversity.API: _addedoutputcols
function _addedoutputcols(::AbstractPhyloTypes{TS}) where
{LABEL, RT, NL, N, B, TS <: TreeSet{LABEL, RT, NL, N, B, <: AbstractTree}}
return Dict{Symbol, Type}(:treename => LABEL)
end
struct PhyloBranches{Tree <: AbstractTree} <: Diversity.PhyloBranches{Tree}
tree::Tree
nleaf::Int64
nancestral::Int64
leafnames::Vector{String}
ancestralnames::Vector{String}
ancestralmatrix::Matrix{Float64}
Zmatrix::Matrix{Float64}

import Diversity.API: _getaddedoutput
function _getaddedoutput(pt::AbstractPhyloTypes{TS}) where
{LABEL, RT, NL, N, B, TS <: TreeSet{LABEL, RT, NL, N, B, <: AbstractTree}}
return Dict{Symbol, LABEL}(:treename => first(gettreenames(pt.tree)))
end
function PhyloBranches(tree::Tree) where {Tree <: AbstractTree}
leafnames = [getnodename(tree, node)
for node in traversal(tree, preorder)
if isleaf(tree, node)]
nleaf = length(leafnames)
nleaf > 0 || error("Too few species")
leafinfo = Dict{String, Float64}()
speciesinfo = Dict{String, Tuple{String, String, Float64}}()
ancestralnames = String[]

function PhyloBranches(tree::Tree) where Tree <: AbstractTree
leafnames = [getnodename(tree, node)
for node in traversal(tree, preorder) if isleaf(tree, node)]
nleaf = length(leafnames)
nleaf > 0 || error("Too few species")
leafinfo = Dict{String, Float64}()
speciesinfo = Dict{String, Tuple{String, String, Float64}}()
ancestralnames = String[]

for leaf in leafnames
branches = branchhistory(tree, leaf)
leafinfo["$leaf"] = heighttoroot(tree, leaf)
for branch in branches
branchname = getbranchname(tree, branch)
name = "$leaf : $branchname"
push!(ancestralnames, name)
speciesinfo[name] =
tuple("$leaf", "$branchname",
getlength(tree, branch) / leafinfo["$leaf"])
for leaf in leafnames
branches = branchhistory(tree, leaf)
leafinfo["$leaf"] = heighttoroot(tree, leaf)
for branch in branches
branchname = getbranchname(tree, branch)
name = "$leaf : $branchname"
push!(ancestralnames, name)
speciesinfo[name] = tuple("$leaf", "$branchname",
getlength(tree, branch) /
leafinfo["$leaf"])
end
end
end

nancestral = length(ancestralnames)
Lbar = Statistics.mean(collect(values(leafinfo)))
ancestralmatrix = Matrix{Float64}(undef, nancestral, nleaf)
fill!(ancestralmatrix, 0.0)
for i in 1:nancestral
for j in 1:nleaf
if speciesinfo[ancestralnames[i]][1] == leafnames[j]
ancestralmatrix[i, j] =
speciesinfo[ancestralnames[i]][3] * leafinfo[leafnames[j]]
nancestral = length(ancestralnames)
Lbar = Statistics.mean(collect(values(leafinfo)))
ancestralmatrix = Matrix{Float64}(undef, nancestral, nleaf)
fill!(ancestralmatrix, 0.0)
for i in 1:nancestral
for j in 1:nleaf
if speciesinfo[ancestralnames[i]][1] == leafnames[j]
ancestralmatrix[i, j] = speciesinfo[ancestralnames[i]][3] *
leafinfo[leafnames[j]]
end
end
end
end

Zmatrix = Matrix{Float64}(undef, nancestral, nancestral)
fill!(Zmatrix, 0.0)
for i in 1:nancestral
for j in 1:nancestral
if haskey(speciesinfo,
"$(speciesinfo[ancestralnames[j]][1]) : " *
"$(speciesinfo[ancestralnames[i]][2])")
Zmatrix[i, j] = 1.0 /
leafinfo[speciesinfo[ancestralnames[j]][1]]

Zmatrix = Matrix{Float64}(undef, nancestral, nancestral)
fill!(Zmatrix, 0.0)
for i in 1:nancestral
for j in 1:nancestral
if haskey(speciesinfo,
"$(speciesinfo[ancestralnames[j]][1]) : " *
"$(speciesinfo[ancestralnames[i]][2])")
Zmatrix[i, j] = 1.0 /
leafinfo[speciesinfo[ancestralnames[j]][1]]
end
end
end

return new{Tree}(tree, nleaf, nancestral, leafnames,
ancestralnames, ancestralmatrix, Zmatrix)
end

function PhyloBranches(treeset::TS) where {TS <: TreeSet}
ntrees(treeset) == 1 ||
error("Can currently only handle one tree in a PhyloSet")
pt = PhyloBranches(first(treeset))
return new{TS}(treeset, pt.nleaf, pt.nancestral,
pt.leafnames, pt.ancestralnames,
pt.ancestralmatrix, pt.Zmatrix)
end

return PhyloBranches{Tree}(tree, nleaf, nancestral, leafnames,
ancestralnames, ancestralmatrix, Zmatrix)
end

function PhyloBranches(treeset::TS) where TS <: TreeSet
ntrees(treeset) == 1 ||
error("Can currently only handle one tree in a PhyloSet")
pt = PhyloBranches(first(treeset))
return PhyloBranches{TS}(treeset, pt.nleaf, pt.nancestral,
pt.leafnames, pt.ancestralnames,
pt.ancestralmatrix, pt.Zmatrix)
Diversity.PhyloBranches(args...) = PhyloBranches(args...)

import Diversity.API: _addedoutputcols
function _addedoutputcols(::AbstractPhyloTypes{TS}) where
{LABEL, RT, NL, N, B,
TS <: TreeSet{LABEL, RT, NL, N, B, <:AbstractTree}}
return Dict{Symbol, Type}(:treename => LABEL)
end

import Diversity.API: _getaddedoutput
function _getaddedoutput(pt::AbstractPhyloTypes{TS}) where
{LABEL, RT, NL, N, B,
TS <: TreeSet{LABEL, RT, NL, N, B, <:AbstractTree}}
return Dict{Symbol, LABEL}(:treename => first(gettreenames(pt.tree)))
end

import Diversity.API: _gettypenames
Expand All @@ -92,7 +106,7 @@ end

import Diversity.API: _calcabundance
function _calcabundance(phy::PhyloBranches,
raw::AbstractMatrix{<: AbstractFloat})
raw::AbstractMatrix{<:AbstractFloat})
processed = phy.ancestralmatrix * raw
scale = sum(processed)
processed ./= scale
Expand All @@ -106,7 +120,7 @@ end

import Diversity.API: _calcordinariness
function _calcordinariness(phy::PhyloBranches,
processed::AbstractMatrix{<: AbstractFloat},
processed::AbstractMatrix{<:AbstractFloat},
scale::Real)
return (phy.Zmatrix * processed) .* scale
end
Expand Down
Loading

2 comments on commit cf827ee

@richardreeve
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104905

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.12 -m "<description of version>" cf827ee4c303a5bd12ee6a2237818891831d5de7
git push origin v0.5.12

Please sign in to comment.