Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7ea86a4
adding new file that isn't connected to anything
gottacatchenall Oct 25, 2021
cf031f2
:features: adding multivariatestats
gottacatchenall Oct 26, 2021
b0bc301
:pencil: adding MIT license for GeoData.jl to header of resample file
gottacatchenall Oct 26, 2021
baf395e
:construction: using @require and basic functionality
gottacatchenall Oct 26, 2021
e7af41f
:construction_worker: mostly there
gottacatchenall Oct 26, 2021
5930730
:pencil: docs and blue style
gottacatchenall Oct 28, 2021
77b3510
:adhesive_bandage: including _layers_are_compatible call
gottacatchenall Oct 28, 2021
f09e364
removing type assert
gottacatchenall Nov 4, 2021
d6d7b6b
:pencil:
gottacatchenall Nov 8, 2021
53badf5
:arrow_up: add mvstats to docs folder req
gottacatchenall Nov 8, 2021
708ea8d
:pencil: doc deps update
gottacatchenall Nov 8, 2021
97bdfb7
:bug: bug fix and hopefully doc fix?
gottacatchenall Nov 8, 2021
1eeb822
:pencil: docs should make a plot?
gottacatchenall Nov 8, 2021
45309e6
:pencil: remove extra unitful include
gottacatchenall Nov 8, 2021
a0e548d
:bug: missing common_keys call in fit
gottacatchenall Nov 9, 2021
e1deed1
:pencil: extra @show call
gottacatchenall Nov 9, 2021
932ef25
:zap: whitening is back
gottacatchenall Nov 9, 2021
0ce730e
:bulb: mv fix
gottacatchenall Nov 9, 2021
138786a
:bug: namespace issues, same as it ever was
gottacatchenall Nov 9, 2021
949f205
🥳 v0.9.0 in preparation for multivariate
tpoisot Nov 10, 2021
2afab99
🧹 no need to call SSDML.transform
tpoisot Nov 10, 2021
f2b8627
🧑‍🏫 fix transform
tpoisot Nov 10, 2021
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 Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SimpleSDMLayers"
uuid = "2c645270-77db-11e9-22c3-0f302a89c64c"
authors = ["Timothée Poisot <[email protected]>", "Gabriel Dansereau <[email protected]>"]
version = "0.8.3"
version = "0.9.0"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Expand Down
3 changes: 2 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ makedocs(
"Geometry for clipping" => "examples/geometry.md",
"Sliding window analysis" => "examples/slidingwindow.md",
"Landcover data" => "examples/landcover.md",
"Multivariate mapping" => "examples/multivariate.md"
"Multivariate mapping" => "examples/multivariate.md",
"Multivariate statistics" => "examples/pca.md"
],
"SDM case studies" => [
"GBIF integration" => "sdm/gbif.md",
Expand Down
54 changes: 54 additions & 0 deletions docs/src/examples/pca.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# # MultivariateStats.jl integration

# In this example we explore how you can use
# `MultivariateStats.jl` with `SimpleSDMLayers`
# to perform [principal-component-analysis (PCA)](wikiling todo) to
# reduce the dimensionality of a set of `SDMLayers`, or to remove the
# covariance between layers via data [whitening](linktodo).

# ## Principal-component analysis of many `SDMLayers`

using SimpleSDMLayers
using MultivariateStats
using Plots

# The `SimpleSDMLayers` enables integration with `MultivariateStats.jl`
# In this example, we will show how this can work.

boundaries =(left = -164.022167, right = -55.250858, bottom = 23.547132, top = 72.105833)
layers = convert.(
Float32,
SimpleSDMPredictor(WorldClim, BioClim, 1:19; boundaries...)
)

# fit pca and project to a new set of layers

pca = fit(PCA, layers)
newlayers = transform(pca, layers)

# plot them

pcaplots = plot.(newlayers)
plot(pcaplots...)


# ## Remove correlation between layers (`Whitening`)

# Have to start with two layers that are reasonably correlated.

wlayers = convert.(
Float32,
SimpleSDMPredictor(WorldClim, BioClim, 1:2; boundaries...)
)
plot(plot.(wlayers)...)


# Now we call methods just as in `MultivariateStats`

w = fit(Whitening, wlayers)
newlayers = transform(w, wlayers)


# and plot the layers without covar

plot(plot.(newlayers)...)
9 changes: 7 additions & 2 deletions src/SimpleSDMLayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ for s in instances(CMIP6)
end
for s in instances(RepresentativeConcentrationPathway)
@eval export $(Symbol(s))
end
end
for s in instances(SharedSocioeconomicPathway)
@eval export $(Symbol(s))
end
Expand Down Expand Up @@ -99,7 +99,7 @@ isdir(_layers_assets_path) || mkpath(_layers_assets_path)
export clip

function __init__()
@require GBIF="ee291a33-5a6c-5552-a3c8-0f29a1181037" begin
@require GBIF = "ee291a33-5a6c-5552-a3c8-0f29a1181037" begin
@info "Loading GBIF support for SimpleSDMLayers.jl"
include("integrations/GBIF.jl")
end
Expand All @@ -108,6 +108,11 @@ function __init__()
include("integrations/DataFrames.jl")
end

@require MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411" begin
@info "Loading MultivariateStats support for SimpleSDMLayers.jl"
include("integrations/MultivariateStats.jl")
end

end

end # module
44 changes: 44 additions & 0 deletions src/integrations/MultivariateStats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# WARNING this file is only loaded if MultivariateStats.jl is also active
# This all happens thanks to the Requires.jl package

_allowed_transforms = (MultivariateStats.PCA, MultivariateStats.PPCA, MultivariateStats.KernelPCA, MultivariateStats.Whitening)
AllowedMultivariateTransforms = Union{_allowed_transforms...}

"""
MultivariateStats.fit(a, layers::Vector{T}, kwargs...) where T <: SimpleSDMLayer

Overloads the `fit` function from `MultivariateStats.jl`.
"""
function MultivariateStats.fit(proj::Type{K}, layers::Vector{T}; kwargs...) where {T<:SimpleSDMLayer, K <: AllowedMultivariateTransforms}
_layers_are_compatible(layers) || return ArgumentError("layers are not compatible")
common_keys = reduce(∩, keys.(layers))
input = hcat([vcat([layer[key] for layer in layers]...) for key in common_keys]...)
proj = MultivariateStats.fit(proj, input; kwargs...)
return proj
end

"""
transform(proj, layers::Vector{V},
kwargs...) where {PT<:Union{MultivariateStats.PCA, MultivariateStats.PPCA},V<:SimpleSDMLayer}

Overload of the `transform` function from `MultivariateStats.jl`. Here `proj` is an output object from `MultivariateStats.fit` (see above).
"""
function MultivariateStats.transform(
proj, layers::AbstractVecOrMat{U}; kwargs...
) where {U<:SimpleSDMLayer}
_layers_are_compatible(layers) || return ArgumentError("layers are not compatible")

newlayers = [similar(first(layers)) for i in 1:MultivariateStats.outdim(proj)]
common_keys = reduce(∩, keys.(layers))

input = hcat([vcat([layer[key] for layer in layers]...) for key in common_keys]...)

for (ct, key) in enumerate(common_keys)
pcaproj = MultivariateStats.transform(proj, input[:, ct])
for i in 1:length(newlayers)
newlayers[i][key] = pcaproj[i]
end
end

return newlayers
end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[deps]
GBIF = "ee291a33-5a6c-5552-a3c8-0f29a1181037"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
NeutralLandscapes = "71847384-8354-4223-ac08-659a5128069f"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Expand Down
22 changes: 22 additions & 0 deletions test/extensions/multivariatestats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SSLTestMVStats
using SimpleSDMLayers
using MultivariateStats
using NeutralLandscapes
using Test

TEST_DIMS = (150, 150)
TEST_AUTOCORRELATION = 0.9
TEST_NUM_LAYERS = 10

layers = [
SimpleSDMResponse(rand(MidpointDisplacement(TEST_AUTOCORRELATION), TEST_DIMS...)) for
i in 1:TEST_NUM_LAYERS
]

@test typeof(transform(fit(PCA, layers), layers)) <: Vector{T} where {T<:SimpleSDMLayer}
#@test typeof(transform(fit(PPCA, layers),layers)) <: Vector{T} where T<:SimpleSDMLayer

#@assert typeof(transform(fit(KernelPCA, layers),layers)) <: Vector{T} where T<:SimpleSDMLayer
#@assert typeof(transform(fit(Whitening, layers),layers)) <: Vector{T} where T<:SimpleSDMLayer

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Test
global anyerrors = false

tests = [
"MultivariateStats" => "extensions/multivariatestats.jl",
"construction" => "core/construction.jl",
"lat./lon." => "core/latlon.jl",
"lat./lon. conversion" => "core/coordconvert.jl",
Expand Down