This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from EcoJulia/tp/rescale
Add a rescale function
- Loading branch information
Showing
10 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.4.0" | ||
version = "0.4.1" | ||
|
||
[deps] | ||
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,6 @@ latitudes | |
longitudes | ||
clip | ||
mask | ||
rescale! | ||
rescale | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,5 @@ broadcast | |
Statistics.mean | ||
Statistics.median | ||
Statistics.std | ||
Statistics.quantile | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
_rescale(x, m, M) = (x .- minimum(x))./(maximum(x)-minimum(x)).*(M-m).+m | ||
|
||
""" | ||
rescale!(layer::TI, template::TJ) where {TI <: SimpleSDMLayer, TJ <: SimpleSDMLayer} | ||
Changes the values of the layer given as its first argument, so that it has the | ||
same *range* as the values of the layer given as its second argument. | ||
Modification is done in-place. | ||
""" | ||
function rescale!(layer::TI, template::TJ) where {TI <: SimpleSDMLayer, TJ <: SimpleSDMLayer} | ||
return rescale!(layer, extrema(template)) | ||
end | ||
|
||
""" | ||
rescale!(layer::TI, t::Tuple{T,T}) where {TI <: SimpleSDMLayer, T <: Number} | ||
Changes the values of the layer given as its first argument, so that it has the | ||
same *range* as the values given as a tuple of values. Modification is done | ||
in-place. | ||
""" | ||
function rescale!(layer::TI, t::Tuple{T,T}) where {TI <: SimpleSDMLayer, T <: Number} | ||
occ = findall(!isnothing, layer.grid) | ||
layer.grid[occ] .= _rescale(layer.grid[occ], t...) | ||
end | ||
|
||
""" | ||
rescale(layer::TI, template::TJ) where {TI <: SimpleSDMLayer, TJ <: SimpleSDMLayer} | ||
Copying version of `rescale!`. | ||
""" | ||
function rescale(layer::TI, template::TJ) where {TI <: SimpleSDMLayer, TJ <: SimpleSDMLayer} | ||
l = copy(layer) | ||
return rescale!(l, extrema(template)) | ||
end | ||
|
||
""" | ||
rescale(layer::TI, t::Tuple{T,T}) where {TI <: SimpleSDMLayer, T <: Number} | ||
Copying version of `rescale!`. | ||
""" | ||
function rescale(layer::TI, t::Tuple{T,T}) where {TI <: SimpleSDMLayer, T <: Number} | ||
l = copy(layer) | ||
return rescale!(l, t) | ||
end | ||
|
||
|
||
""" | ||
rescale!(layer::T, p::Vector{Real}) where {T <: SimpleSDMLayer} | ||
Rescale the values of a `layer` so that they match with the quantiles given in | ||
`p`. Internally, this uses the `Statistics.quantile` function. | ||
""" | ||
function rescale!(layer::T, p::Vector{TI}) where {T <: SimpleSDMLayer, TI <: AbstractFloat} | ||
q = reverse!(quantile(layer, p)) | ||
occupied = findall(!isnothing, layer.grid) | ||
v = collect(layer) | ||
for i in 1:length(q) | ||
layer.grid[occupied[findall(x -> x <= q[i], v)]] .= reverse(p)[i] | ||
end | ||
return layer | ||
end | ||
|
||
""" | ||
rescale(layer::T, p::Vector{Real}) where {T <: SimpleSDMLayer} | ||
Copying version of `rescale!`. | ||
""" | ||
function rescale(layer::T, p::Vector{TI}) where {T <: SimpleSDMLayer, TI <: AbstractFloat} | ||
l = copy(layer) | ||
return rescale!(l, p) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module SSLTestRescale | ||
using SimpleSDMLayers | ||
using Test | ||
|
||
M1 = rand(Float64, (5, 10)).*4 | ||
S1 = SimpleSDMPredictor(M1, 0.0, 1.0, 0.0, 1.0) | ||
|
||
M2 = (rand(Float64, (5, 10)).+1).*5 | ||
S2 = SimpleSDMPredictor(M2, 0.0, 1.0, 0.0, 1.0) | ||
|
||
@test extrema(rescale(S1, S2)) == extrema(S2) | ||
@test extrema(rescale(S2, S1)) == extrema(S1) | ||
|
||
rescale!(S1, [0.0, 0.5, 1.0]) | ||
@test sort(unique(collect(S1))) == [0.0, 0.5, 1.0] | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5ebb11f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
5ebb11f
There was a problem hiding this comment.
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/30536
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: