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 Diversity
julia> using Diversity.API
julia> struct UniformSimilarity <: Diversity.API.AbstractTypes names::Vector{String} similarity::Float64 end
julia> Diversity.API._gettypenames(us::UniformSimilarity, ::Bool) = us.names
julia> 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)3
julia> 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: 1
julia> 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 omitted
julia> 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 implementmay 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.APIModule
Diversity.API submodule

The Diversity.API submodule should be imported if you want to create a new type, partition or metacommunity subtype. Otherwise it can be ignored.

source
Diversity.API.AbstractMetacommunityType
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.

source
Diversity.API.AbstractPartitionType
AbstractPartition

Abstract 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).

source
Diversity.API.AbstractTypesType
AbstractTypes

Abstract supertype for all similarity types. Its subtypes allow you to define how similarity is measured between individuals.

source
Diversity.API._calcabundanceFunction
_calcabundance(t::AbstractTypes, a::AbstractArray)

Calculates the abundance a for AbstractTypes, t (if necessary). May be implemented by each AbstractTypes subtype.

source
Diversity.API._calcordinarinessFunction
_calcordinariness(t::AbstractTypes, a::AbstractArray, scale::Real)

Calculates the ordinariness of abundance a from AbstractTypes, t. May be implemented by each AbstractTypes subtype.

source
Diversity.API._calcsimilarityFunction
_calcsimilarity(t::AbstractTypes, scale::Real)

Retrieves (and possibly calculates) a similarity matrix from t. Must be implemented by each AbstractTypes subtype.

source
Diversity.API._countsubcommunitiesFunction
_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.

source
Diversity.API._counttypesFunction
_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.

source
Diversity.API._getabundanceFunction
_getabundance(m::AbstractMetacommunity, raw::Bool)

Returns the abundances array of the metacommunity. Must be implemented by each AbstractMetacommunity subtype.

source
Diversity.API._getmetaabundanceFunction
_getmetaabundance(m::AbstractMetacommunity, raw::Bool)

Returns the metacommunity abundances of the metacommunity. May be implemented by each AbstractMetacommunity subtype.

source
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.

source
Diversity.API._getordinariness!Function
_getordinariness!(m::AbstractMetacommunity)

Returns (and possibly calculates) the ordinariness array of the subcommunities. May be implemented by each AbstractMetacommunity subtype.

source
Diversity.API._getpartitionFunction
_getpartition(::AbstractMetacommunity)

Returns the AbstractPartition component of the metacommunity. Must be implemented by each AbstractMetacommunity subtype.

source
Diversity.API._getscaleFunction
_getscale(m::AbstractMetacommunity)

Returns a scaling factor for the metacommunity (needed for phylogenetics). Normally ignored. Must be implemented by each AbstractMetacommunity subtype.

source
Diversity.API._getsubcommunitynamesFunction
_getsubcommunitynames(p::AbstractPartition)

Returns the names of the subcommunities in the partition object. Must be implemented by each AbstractPartition subtype.

source
Diversity.API._gettypenamesFunction
_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.

source
Diversity.API._gettypesFunction
_gettypes(::AbstractMetacommunity)

Returns the AbstractTypes component of the metacommunity. Must be implemented by each AbstractMetacommunity subtype.

source
Diversity.API._getweightFunction
_getweight(m::AbstractMetacommunity)

Returns the subcommunity weights of the metacommunity. May be implemented by each AbstractMetacommunity subtype.

source
Diversity.API._subsetpartitionFunction
_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.

source
Diversity.API._subsettypesFunction
_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.

source
Diversity.API.floattypesFunction
floattypes(t)

This function returns a set containing the floating point types that are compatible with the Diversity-related object, t.

source
Diversity.API.mcmatchFunction
mcmatch(procm::AbstractArray, sim::AbstractTypes, part::AbstractPartition)

Checks for type and size compatibility for elements contributing to a Metacommunity

source
Diversity.API.typematchMethod
typematch(args...)

Checks whether the types of a variety of Diversity-related objects have compatible types (using floattypes()).

source