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

MultivariateStats + StatsAPI #210

Closed
wants to merge 7 commits into from
Closed
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
11 changes: 9 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SpeciesDistributionToolkit"
uuid = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
authors = ["Timothée Poisot <[email protected]>"]
version = "0.0.10"
version = "0.1.0"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand All @@ -17,6 +17,13 @@ SimpleSDMDatasets = "2c7d61d0-5c73-410d-85b2-d2e7fbbdcefa"
SimpleSDMLayers = "2c645270-77db-11e9-22c3-0f302a89c64c"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[weakdeps]
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
StatsAPI = "82ae8749-77ed-4fe6-ae5f-f523153014b0"

[extensions]
MultivariateExtension = ["MultivariateStats", "StatsAPI"]

[compat]
ArchGDAL = "0.9, 0.10"
Distances = "0.10"
Expand All @@ -30,4 +37,4 @@ Reexport = "1.2"
SimpleSDMDatasets = "0.1"
SimpleSDMLayers = "0.9"
StatsBase = "0.33, 0.34"
julia = "1.8"
julia = "1.9"
63 changes: 63 additions & 0 deletions ext/MultivariateExtension.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module MultivariateExtension

using SpeciesDistributionToolkit
using MultivariateStats
using StatsAPI

# These types have a fit method
types_to_fit = [:PCA, :PPCA, :KernelPCA, :Whitening, :MDS, :MetricMDS]

# These types have a transform method
types_to_transform = [:Whitening]

# These types have a predict method
types_to_predict = [:PCA, :PPCA, :KernelPCA, :MDS, :MetricMDS]

for tf in types_to_fit
eval(
quote
function StatsAPI.fit(::Type{MultivariateStats.$tf}, X::Vector{T}; kwargs...) where {T <: SimpleSDMLayers.SimpleSDMLayer}
@assert SimpleSDMLayers._layers_are_compatible(X)
return StatsAPI.fit(MultivariateStats.$tf, Array(X); kwargs...)
end
end
)
end

for tf in types_to_transform
eval(
quote
function MultivariateStats.transform(f::MultivariateStats.$tf, X::Vector{T}; kwargs...) where {T <: SimpleSDMLayers.SimpleSDMLayer}
@assert SimpleSDMLayers._layers_are_compatible(X)
D = MultivariateStats.transform(f, Array(X); kwargs...)
O = [similar(X[1], eltype(D)) for i in 1:length(X)]
for i in axes(O, 1)
for (j, k) in enumerate(keys(O[i]))
O[i][k] = D[i, j]
end
end
return O
end
end
)
end

for tf in types_to_predict
eval(
quote
function StatsAPI.predict(f::MultivariateStats.$tf, X::Vector{T}; kwargs...) where {T <: SimpleSDMLayers.SimpleSDMLayer}
@assert SimpleSDMLayers._layers_are_compatible(X)
D = StatsAPI.predict(f, Array(X); kwargs...)
O = [similar(X[1], eltype(D)) for i in 1:MultivariateStats.outdim(M)]
for i in axes(O, 1)
for (j, k) in enumerate(keys(O[i]))
O[i][k] = D[i, j]
end
end
return O
end
end
)
end

end
5 changes: 4 additions & 1 deletion src/SpeciesDistributionToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ using Reexport
@reexport using Fauxcurrences
@reexport using Phylopic

# Quality of life functions
include("quality_of_life.jl")

# SimpleSDMLayers to wrap everything together
include("integrations/datasets_layers.jl")

Expand All @@ -31,7 +34,7 @@ include("integrations/gbif_layers.jl")
# GBIF and Phylopic integration
include("integrations/gbif_phylopic.jl")

# Plotting
# Makie recipes
include("integrations/makie.jl")

# Functions for IO
Expand Down
1 change: 0 additions & 1 deletion src/integrations/makie.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Function to turn a layer into something (Geo)Makie can use
function sprinkle(layer::T) where {T <: SimpleSDMLayer}
return (
longitudes(layer),
Expand Down
12 changes: 12 additions & 0 deletions src/quality_of_life.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Base.Array(layers::Vector{T}) where {T <: SimpleSDMLayers.SimpleSDMLayer}
@assert SimpleSDMLayers._layers_are_compatible(layers)
k = reduce(intersect, [findall(!isnothing, grid(layer)) for layer in layers])
R = SimpleSDMLayers._inner_type(first(layers))
X = Matrix{R}(undef, length(layers), length(k))
for i in eachindex(k)
for j in eachindex(layers)
X[j,i] = layers[j][k[i]]
end
end
return X
end
Loading