Diversity.API
The Diversity.API submodule provides the API that must be extended for new AbstractTypes, AbstractPartition and AbstractMetacommunity subtypes. It is what lets a package that knows nothing about this one — EcoSISTEM, for instance — have its own types measured by every diversity measure here.
Usage
Extending the system means adding methods to the underscore-prefixed functions in Diversity.API. You add methods; you never replace them.
A new AbstractTypes subtype needs only two: _gettypenames and _calcsimilarity. Everything else has a working default — _counttypes, for example, falls back to the length of the type name vector. Here is a complete one, in which every distinct pair of types has the same similarity:
julia> using Diversityjulia> using Diversity.APIjulia> struct UniformSimilarity <: Diversity.API.AbstractTypes names::Vector{String} similarity::Float64 endjulia> Diversity.API._gettypenames(us::UniformSimilarity, ::Bool) = us.namesjulia> function Diversity.API._calcsimilarity(us::UniformSimilarity, ::Real) n = length(us.names) Z = fill(us.similarity, n, n) for i in 1:n Z[i, i] = 1.0 end return Z end
That is enough to use it everywhere a built-in type would go:
julia> types = UniformSimilarity(["a", "b", "c"], 0.5)Main.UniformSimilarity(["a", "b", "c"], 0.5)julia> counttypes(types)3julia> meta = Metacommunity([0.5, 0.3, 0.2], types)Metacommunity{Float64, Vector{Float64}, Matrix{Float64}, Main.UniformSimilarity, Onecommunity} with 3 species in 1 subcommunity measuring unknown diversity. Species names: a, b, c Subcommunity names: 1julia> meta_gamma(meta, 0)1×8 DataFrame Row │ div_type measure q type_level type_name partition_level parti ⋯ │ String String Int64 String String String Strin ⋯ ─────┼────────────────────────────────────────────────────────────────────────── 1 │ unknown Gamma 0 types metacommunity ⋯ 2 columns omitted
The similarity parameter shows what the measures are doing. At 0.0 no type resembles any other, so the metacommunity holds three types' worth of diversity; at 1.0 every type is interchangeable and it holds one:
julia> meta_gamma(Metacommunity([0.5, 0.3, 0.2], UniformSimilarity(["a", "b", "c"], 0.0)), 0)1×8 DataFrame Row │ div_type measure q type_level type_name partition_level parti ⋯ │ String String Int64 String String String Strin ⋯ ─────┼────────────────────────────────────────────────────────────────────────── 1 │ unknown Gamma 0 types metacommunity ⋯ 2 columns omittedjulia> meta_gamma(Metacommunity([0.5, 0.3, 0.2], UniformSimilarity(["a", "b", "c"], 1.0)), 0)1×8 DataFrame Row │ div_type measure q type_level type_name partition_level parti ⋯ │ String String Int64 String String String Strin ⋯ ─────┼────────────────────────────────────────────────────────────────────────── 1 │ unknown Gamma 0 types metacommunity ⋯ 2 columns omitted
The contract
| for a new... | must implement | may implement — and what you get if you do not |
|---|---|---|
AbstractTypes | _gettypenames, _calcsimilarity (unless _hassimilarity is false) | _counttypes (counts the type names), _calcabundance (the abundances unchanged, with scale 1), _calcordinariness (_calcsimilarity(t, scale) * abundances), _getdiversityname ("unknown"), _addedoutputcols (no extra columns), _getaddedoutput (nothing), floattypes (every AbstractFloat), _hassimilarity (true), _subsettypes (materialises the similarity into a GeneralTypes) |
AbstractPartition | _getsubcommunitynames | _countsubcommunities (counts the subcommunity names), _subsetpartition (builds a Subcommunities from the names kept) |
AbstractMetacommunity | _gettypes, _getpartition, _getabundance | _getmetaabundance (abundances summed across subcommunities), _getweight (abundances summed across types), _getordinariness! (_calcordinariness of the types, abundances and scale), _getmetaordinariness! (ordinariness summed across subcommunities), _getscale (1) |
Two of the optional ones are worth knowing about even if you do not implement them. _calcabundance returns both the processed abundances and a scale, which is then passed to _calcsimilarity — that is what lets a phylogeny measure diversity over branches rather than over species. And the raw::Bool argument carried through the API distinguishes the types the user supplied from the types diversity is actually computed over, which differ for exactly that reason.
Leave a required method out and you get an error naming it. That is worth saying because these abstract types are subtypes of EcoBase's, so the fallbacks that let a plain EcoBase assemblage be measured directly would otherwise call back into the method you had not written, and the symptom would be a stack overflow rather than a missing method.
_calcsimilarity is the one required method with a way out, and it is the pair to _hassimilarity above. Declare _hassimilarity(::YourTypes) = false and you need not write it: you get an identity matrix, every type like itself and nothing else, which is what UniqueTypes means. Claim similarity and omit the matrix and you get the error instead — otherwise that same identity matrix would be used for you, and the diversities would come back quietly wrong rather than not at all.
- Diversity.API
- Diversity.Ecology
- The framework
- Genetic diversity
- Diversity.Hill
- Diversity.jl
- Diversity.Jost
- Building a metacommunity
- Phylogenetic diversity
- Coming from vegan
Diversity.API — Module
Diversity.API submoduleThe Diversity.API submodule should be imported if you want to create a new type, partition or metacommunity subtype. Otherwise it can be ignored.
Diversity.API.AbstractMetacommunity — Type
AbstractMetacommunity{FP <: AbstractFloat,
ARaw <: AbstractArray,
AProcessed <: AbstractMatrix{FP},
Sim <: AbstractTypes,
Part <: AbstractPartition}AbstractMetacommunity is the abstract supertype of all metacommunity types. AbstractMetacommunity subtypes allow you to define how to partition your total metacommunity (e.g. an ecosystem) into smaller components (e.g. subcommunities), and how to assess similarity between individuals within it.
Diversity.API.AbstractPartition — Type
AbstractPartitionAbstract supertype for all partitioning types. AbstractPartition subtypes allow you to define how to partition your total metacommunity (e.g. an ecosystem) into smaller components (e.g. subcommunities).
Diversity.API.AbstractTypes — Type
AbstractTypesAbstract supertype for all similarity types. Its subtypes allow you to define how similarity is measured between individuals.
Diversity.API._addedoutputcols — Function
_addedoutputcols(::AbstractTypes)Returns the name of any additional columns needed to be added to outputs.
Diversity.API._calcabundance — Function
_calcabundance(t::AbstractTypes, a::AbstractArray)Calculates the abundance a for AbstractTypes, t (if necessary). May be implemented by each AbstractTypes subtype.
Diversity.API._calcordinariness — Function
_calcordinariness(t::AbstractTypes, a::AbstractArray, scale::Real)Calculates the ordinariness of abundance a from AbstractTypes, t. May be implemented by each AbstractTypes subtype.
Diversity.API._calcsimilarity — Function
_calcsimilarity(t::AbstractTypes, scale::Real)Retrieves (and possibly calculates) a similarity matrix from t. Must be implemented by each AbstractTypes subtype.
Diversity.API._countsubcommunities — Function
_countsubcommunities(::AbstractPartition)Returns number of subcommunities in a partition, p. May be implemented by each AbstractPartition subtype. Default is to count length of subcommunity name vector.
Diversity.API._counttypes — Function
_counttypes(::AbstractTypes, raw::Bool)Returns number of types in an AbstractTypes object, t. May be implemented by each AbstractTypes subtype. raw determines whether to count the number of raw or processed types, which varies, for instance, when the types are determined by a phylogeny. Default is to count length of corresponding types name vector.
Diversity.API._getabundance — Function
_getabundance(m::AbstractMetacommunity, raw::Bool)Returns the abundances array of the metacommunity. Must be implemented by each AbstractMetacommunity subtype.
Diversity.API._getaddedoutput — Function
_getaddedoutput(::AbstractTypes)Returns the name of any additional columns needed to be added to outputs.
Diversity.API._getdiversityname — Function
_getdiversityname(::AbstractTypes)Returns the name of the diversity type used to calculate.
Diversity.API._getmetaabundance — Function
_getmetaabundance(m::AbstractMetacommunity, raw::Bool)Returns the metacommunity abundances of the metacommunity. May be implemented by each AbstractMetacommunity subtype.
Diversity.API._getmetaordinariness! — Function
_getmetaordinariness!(m::AbstractMetacommunity)Returns (and possibly calculates) the ordinariness of the metacommunity as a whole. May be implemented by each AbstractMetacommunity subtype.
Diversity.API._getordinariness! — Function
_getordinariness!(m::AbstractMetacommunity)Returns (and possibly calculates) the ordinariness array of the subcommunities. May be implemented by each AbstractMetacommunity subtype.
Diversity.API._getpartition — Function
_getpartition(::AbstractMetacommunity)Returns the AbstractPartition component of the metacommunity. Must be implemented by each AbstractMetacommunity subtype.
Diversity.API._getscale — Function
_getscale(m::AbstractMetacommunity)Returns a scaling factor for the metacommunity (needed for phylogenetics). Normally ignored. Must be implemented by each AbstractMetacommunity subtype.
Diversity.API._getsubcommunitynames — Function
_getsubcommunitynames(p::AbstractPartition)Returns the names of the subcommunities in the partition object. Must be implemented by each AbstractPartition subtype.
Diversity.API._gettypenames — Function
_gettypenames(t::AbstractTypes, raw::Bool)Returns the names of the types in an AbstractTypes object. Must be implemented by each AbstractTypes subtype. raw determines whether to count the number of raw or processed types, which varies, for instance, when the types are determined by a phylogeny.
Diversity.API._gettypes — Function
_gettypes(::AbstractMetacommunity)Returns the AbstractTypes component of the metacommunity. Must be implemented by each AbstractMetacommunity subtype.
Diversity.API._getweight — Function
_getweight(m::AbstractMetacommunity)Returns the subcommunity weights of the metacommunity. May be implemented by each AbstractMetacommunity subtype.
Diversity.API._subsetpartition — Function
_subsetpartition(p::AbstractPartition, idx)Returns an AbstractPartition containing only the subcommunities at idx. May be implemented by each AbstractPartition subtype; the default builds a Subcommunities from the corresponding names. This is what view() uses to restrict a metacommunity's partition.
Diversity.API._subsettypes — Function
_subsettypes(t::AbstractTypes, idx, scale::Real)Returns an AbstractTypes containing only the types at idx. May be implemented by each AbstractTypes subtype; the default materialises the similarity matrix at scale and returns a GeneralTypes holding the corresponding submatrix. This is what view() uses to restrict a metacommunity's types.
Diversity.API.floattypes — Function
floattypes(t)This function returns a set containing the floating point types that are compatible with the Diversity-related object, t.
Diversity.API.mcmatch — Function
mcmatch(procm::AbstractArray, sim::AbstractTypes, part::AbstractPartition)Checks for type and size compatibility for elements contributing to a Metacommunity
Diversity.API.typematch — Method
typematch(args...)Checks whether the types of a variety of Diversity-related objects have compatible types (using floattypes()).
Diversity.APIDiversity.DiversityDiversity.EcologyDiversity.HillDiversity.JostDiversity.ShortNamesDiversity.individualDiversityDiversity.metacommunityDiversityDiversity.subcommunityDiversityDiversity.API.AbstractMetacommunityDiversity.API.AbstractPartitionDiversity.API.AbstractTypesDiversity.AbstractGeneticDiversity.AbstractPhyloTypesDiversity.DiversityLevelDiversity.DiversityMeasureDiversity.GammaDiversity.GeneralTypesDiversity.GeneralTypesDiversity.MetacommunityDiversity.NormalisedAlphaDiversity.NormalisedBetaDiversity.NormalisedRhoDiversity.OnecommunityDiversity.PhyloBranchesDiversity.PowerMeanMeasureDiversity.RawAlphaDiversity.RawBetaDiversity.RawRhoDiversity.RelativeEntropyMeasureDiversity.SpeciesDiversity.SubAssemblageDiversity.SubcommunitiesDiversity.TaxonomyDiversity.UniqueTypesDiversity.API._addedoutputcolsDiversity.API._calcabundanceDiversity.API._calcordinarinessDiversity.API._calcsimilarityDiversity.API._countsubcommunitiesDiversity.API._counttypesDiversity.API._getabundanceDiversity.API._getaddedoutputDiversity.API._getdiversitynameDiversity.API._getmetaabundanceDiversity.API._getmetaordinariness!Diversity.API._getordinariness!Diversity.API._getpartitionDiversity.API._getscaleDiversity.API._getsubcommunitynamesDiversity.API._gettypenamesDiversity.API._gettypesDiversity.API._getweightDiversity.API._subsetpartitionDiversity.API._subsettypesDiversity.API.floattypesDiversity.API.mcmatchDiversity.API.typematchDiversity.Ecology.faith_pdDiversity.Ecology.generalisedfaith_pdDiversity.Ecology.generalisedjaccardDiversity.Ecology.generalisedrichnessDiversity.Ecology.generalisedshannonDiversity.Ecology.generalisedsimpsonDiversity.Ecology.gowerDiversity.Ecology.jaccardDiversity.Ecology.pielouDiversity.Ecology.richnessDiversity.Ecology.shannonDiversity.Ecology.simpsonDiversity.GeneticTypeDiversity.Hill.hillnumberDiversity.Jost.jostalphaDiversity.Jost.jostbetaDiversity._getmetaDiversity.addedoutputcolsDiversity.calcsimilarityDiversity.countsubcommunitiesDiversity.counttypesDiversity.diversityDiversity.getASCIINameDiversity.getFullNameDiversity.getNameDiversity.getabundanceDiversity.getaddedoutputDiversity.getdiversitynameDiversity.getmetaabundanceDiversity.getmetaordinariness!Diversity.getordinariness!Diversity.getpartitionDiversity.getsubcommunitynamesDiversity.gettypenamesDiversity.gettypesDiversity.getweightDiversity.hassimilarityDiversity.inddivDiversity.meta_gammaDiversity.metadivDiversity.norm_meta_alphaDiversity.norm_meta_betaDiversity.norm_meta_rhoDiversity.norm_sub_alphaDiversity.norm_sub_betaDiversity.norm_sub_rhoDiversity.powermeanDiversity.qDDiversity.qDZDiversity.raw_meta_alphaDiversity.raw_meta_betaDiversity.raw_meta_rhoDiversity.raw_sub_alphaDiversity.raw_sub_betaDiversity.raw_sub_rhoDiversity.sub_gammaDiversity.subdivDiversity.vcf_dataframe