Skip to content

Commit

Permalink
Rename Scale --> LowHigh
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Jan 24, 2024
1 parent b54d69a commit f6a72df
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ are concatenated horizontally in the final table:
```@example usage
# create a transform pipeline
f1 = ZScore()
f2 = Scale()
f2 = LowHigh()
f3 = Quantile()
f4 = Functional(cos)
f5 = Interquartile()
Expand Down
4 changes: 2 additions & 2 deletions docs/src/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ Identity
Center
```

## Scale
## LowHigh

```@docs
Scale
LowHigh
```

## MinMax
Expand Down
2 changes: 1 addition & 1 deletion src/TableTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export
OneHot,
Identity,
Center,
Scale,
LowHigh,
MinMax,
Interquartile,
ZScore,
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ revertible transform in the list.
# Examples
```julia
Scale(low=0.3, high=0.6) ⊔ EigenAnalysis(:VDV)
LowHigh(low=0.3, high=0.6) ⊔ EigenAnalysis(:VDV)
ZScore() ⊔ EigenAnalysis(:V)
```
Expand Down
68 changes: 34 additions & 34 deletions src/transforms/scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,74 @@
# ------------------------------------------------------------------

"""
Scale(; low=0.25, high=0.75)
LowHigh(; low=0.25, high=0.75)
Applies the Scale transform to all columns of the table.
The scale transform of the column `x` is defined by `(x .- xl) ./ (xh - xl)`,
Applies the LowHigh transform to all columns of the table.
The LowHigh transform of the column `x` is defined by `(x .- xl) ./ (xh - xl)`,
where `xl = quantile(x, low)` and `xh = quantile(x, high)`.
Scale(col₁, col₂, ..., colₙ; low=0.25, high=0.75)
Scale([col₁, col₂, ..., colₙ]; low=0.25, high=0.75)
Scale((col₁, col₂, ..., colₙ); low=0.25, high=0.75)
LowHigh(col₁, col₂, ..., colₙ; low=0.25, high=0.75)
LowHigh([col₁, col₂, ..., colₙ]; low=0.25, high=0.75)
LowHigh((col₁, col₂, ..., colₙ); low=0.25, high=0.75)
Applies the Scale transform on columns `col₁`, `col₂`, ..., `colₙ`.
Applies the LowHigh transform on columns `col₁`, `col₂`, ..., `colₙ`.
Scale(regex; low=0.25, high=0.75)
LowHigh(regex; low=0.25, high=0.75)
Applies the Scale transform on columns that match with `regex`.
Applies the LowHigh transform on columns that match with `regex`.
# Examples
```julia
Scale()
Scale(low=0, high=1)
Scale(low=0.3, high=0.7)
Scale(1, 3, 5, low=0, high=1)
Scale([:a, :c, :e], low=0.3, high=0.7)
Scale(("a", "c", "e"), low=0.25, high=0.75)
Scale(r"[ace]", low=0.3, high=0.7)
LowHigh()
LowHigh(low=0, high=1)
LowHigh(low=0.3, high=0.7)
LowHigh(1, 3, 5, low=0, high=1)
LowHigh([:a, :c, :e], low=0.3, high=0.7)
LowHigh(("a", "c", "e"), low=0.25, high=0.75)
LowHigh(r"[ace]", low=0.3, high=0.7)
```
"""
struct Scale{S<:ColumnSelector,T} <: ColwiseFeatureTransform
struct LowHigh{S<:ColumnSelector,T} <: ColwiseFeatureTransform
selector::S
low::T
high::T

function Scale(selector::S, low::T, high::T) where {S<:ColumnSelector,T}
function LowHigh(selector::S, low::T, high::T) where {S<:ColumnSelector,T}
_assert(0 low high 1, "invalid quantiles")
new{S,T}(selector, low, high)
end
end

Scale(selector::ColumnSelector, low, high) = Scale(selector, promote(low, high)...)
LowHigh(selector::ColumnSelector, low, high) = LowHigh(selector, promote(low, high)...)

Scale(; low=0.25, high=0.75) = Scale(AllSelector(), low, high)
Scale(cols; low=0.25, high=0.75) = Scale(selector(cols), low, high)
Scale(cols::C...; low=0.25, high=0.75) where {C<:Column} = Scale(selector(cols), low, high)
LowHigh(; low=0.25, high=0.75) = LowHigh(AllSelector(), low, high)
LowHigh(cols; low=0.25, high=0.75) = LowHigh(selector(cols), low, high)
LowHigh(cols::C...; low=0.25, high=0.75) where {C<:Column} = LowHigh(selector(cols), low, high)

assertions(transform::Scale) = [scitypeassert(Continuous, transform.selector)]
assertions(transform::LowHigh) = [scitypeassert(Continuous, transform.selector)]

parameters(transform::Scale) = (low=transform.low, high=transform.high)
parameters(transform::LowHigh) = (low=transform.low, high=transform.high)

isrevertible(::Type{<:Scale}) = true
isrevertible(::Type{<:LowHigh}) = true

function colcache(transform::Scale, x)
function colcache(transform::LowHigh, x)
low = convert(eltype(x), transform.low)
high = convert(eltype(x), transform.high)
xl, xh = quantile(x, (low, high))
xl == xh && ((xl, xh) = (zero(xl), one(xh)))
(; xl, xh)
end

colapply(::Scale, x, c) = @. (x - c.xl) / (c.xh - c.xl)
colapply(::LowHigh, x, c) = @. (x - c.xl) / (c.xh - c.xl)

colrevert(::Scale, y, c) = @. (c.xh - c.xl) * y + c.xl
colrevert(::LowHigh, y, c) = @. (c.xh - c.xl) * y + c.xl

"""
MinMax()
Applies the MinMax transform to all columns of the table.
The MinMax transform is equivalent to `Scale(low=0, high=1)`.
The MinMax transform is equivalent to `LowHigh(low=0, high=1)`.
MinMax(col₁, col₂, ..., colₙ)
MinMax([col₁, col₂, ..., colₙ])
Expand All @@ -91,15 +91,15 @@ MinMax(("a", "c", "e"))
MinMax(r"[ace]")
```
See also [`Scale`](@ref).
See also [`LowHigh`](@ref).
"""
MinMax(args...) = Scale(args...; low=0, high=1)
MinMax(args...) = LowHigh(args...; low=0, high=1)

"""
Interquartile()
Applies the Interquartile transform to all columns of the table.
The Interquartile transform is equivalent to `Scale(low=0.25, high=0.75)`.
The Interquartile transform is equivalent to `LowHigh(low=0.25, high=0.75)`.
Interquartile(col₁, col₂, ..., colₙ)
Interquartile([col₁, col₂, ..., colₙ])
Expand All @@ -120,6 +120,6 @@ Interquartile(("a", "c", "e"))
Interquartile(r"[ace]")
```
See also [`Scale`](@ref).
See also [`LowHigh`](@ref).
"""
Interquartile(args...) = Scale(args...; low=0.25, high=0.75)
Interquartile(args...) = LowHigh(args...; low=0.25, high=0.75)
28 changes: 14 additions & 14 deletions test/shows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,17 @@
└─ selector = all"""
end

@testset "Scale" begin
T = Scale()
@testset "LowHigh" begin
T = LowHigh()

# compact mode
iostr = sprint(show, T)
@test iostr == "Scale(all, 0.25, 0.75)"
@test iostr == "LowHigh(all, 0.25, 0.75)"

# full mode
iostr = sprint(show, MIME("text/plain"), T)
@test iostr == """
Scale transform
LowHigh transform
├─ selector = all
├─ low = 0.25
└─ high = 0.75"""
Expand Down Expand Up @@ -509,60 +509,60 @@
@testset "SequentialTransform" begin
t1 = Select(:x, :z)
t2 = ZScore()
t3 = Scale(low=0, high=1)
t3 = LowHigh(low=0, high=1)
pipeline = t1 t2 t3

# compact mode
iostr = sprint(show, pipeline)
@test iostr == "Select([:x, :z], nothing) → ZScore(all) → Scale(all, 0, 1)"
@test iostr == "Select([:x, :z], nothing) → ZScore(all) → LowHigh(all, 0, 1)"

# full mode
iostr = sprint(show, MIME("text/plain"), pipeline)
@test iostr == """
SequentialTransform
├─ Select([:x, :z], nothing)
├─ ZScore(all)
└─ Scale(all, 0, 1)"""
└─ LowHigh(all, 0, 1)"""
end

@testset "ParallelTableTransform" begin
t1 = Scale(low=0.3, high=0.6)
t1 = LowHigh(low=0.3, high=0.6)
t2 = EigenAnalysis(:VDV)
t3 = Functional(exp)
pipeline = t1 t2 t3

# compact mode
iostr = sprint(show, pipeline)
@test iostr == "Scale(all, 0.3, 0.6) ⊔ EigenAnalysis(:VDV, nothing, 1.0) ⊔ Functional(all, exp)"
@test iostr == "LowHigh(all, 0.3, 0.6) ⊔ EigenAnalysis(:VDV, nothing, 1.0) ⊔ Functional(all, exp)"

# full mode
iostr = sprint(show, MIME("text/plain"), pipeline)
@test iostr == """
ParallelTableTransform
├─ Scale(all, 0.3, 0.6)
├─ LowHigh(all, 0.3, 0.6)
├─ EigenAnalysis(:VDV, nothing, 1.0)
└─ Functional(all, exp)"""

# parallel and sequential
f1 = ZScore()
f2 = Scale()
f2 = LowHigh()
f3 = Functional(exp)
f4 = Interquartile()
pipeline = (f1 f2) (f3 f4)

# compact mode
iostr = sprint(show, pipeline)
@test iostr == "ZScore(all) → Scale(all, 0.25, 0.75) ⊔ Functional(all, exp) → Scale(all, 0.25, 0.75)"
@test iostr == "ZScore(all) → LowHigh(all, 0.25, 0.75) ⊔ Functional(all, exp) → LowHigh(all, 0.25, 0.75)"

# full mode
iostr = sprint(show, MIME("text/plain"), pipeline)
@test iostr == """
ParallelTableTransform
├─ SequentialTransform
│ ├─ ZScore(all)
│ └─ Scale(all, 0.25, 0.75)
│ └─ LowHigh(all, 0.25, 0.75)
└─ SequentialTransform
├─ Functional(all, exp)
└─ Scale(all, 0.25, 0.75)"""
└─ LowHigh(all, 0.25, 0.75)"""
end
end
6 changes: 3 additions & 3 deletions test/transforms/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
z = y + rand(Normal(0, 5), 1500)
t = Table(; x, y, z)

T = Scale(low=0.3, high=0.6) EigenAnalysis(:VDV)
T = LowHigh(low=0.3, high=0.6) EigenAnalysis(:VDV)
n, c = apply(T, t)
tₒ = revert(T, n, c)
@test Tables.matrix(t) Tables.matrix(tₒ)
Expand All @@ -16,7 +16,7 @@

# distributivity with respect to sequential transform
T₁ = Center()
T₂ = Scale(low=0.2, high=0.8)
T₂ = LowHigh(low=0.2, high=0.8)
T₃ = EigenAnalysis(:VD)
P₁ = T₁ (T₂ T₃)
P₂ = (T₁ T₂) (T₁ T₃)
Expand All @@ -26,7 +26,7 @@

# row table
rt = Tables.rowtable(t)
T = Scale(low=0.3, high=0.6) EigenAnalysis(:VDV)
T = LowHigh(low=0.3, high=0.6) EigenAnalysis(:VDV)
n, c = apply(T, rt)
@test Tables.isrowtable(n)
rtₒ = revert(T, n, c)
Expand Down
18 changes: 9 additions & 9 deletions test/transforms/scale.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testset "Scale" begin
@test TT.parameters(Scale(:a)) == (low=0.25, high=0.75)
@testset "LowHigh" begin
@test TT.parameters(LowHigh(:a)) == (low=0.25, high=0.75)

# constant column
x = fill(3.0, 10)
Expand All @@ -15,7 +15,7 @@
x = rand(rng, Normal(4, 3), 4000)
y = rand(rng, Normal(7, 5), 4000)
t = Table(; x, y)
T = Scale(low=0, high=1)
T = LowHigh(low=0, high=1)
n, c = apply(T, t)
@test all((1), n.x)
@test all((0), n.x)
Expand All @@ -26,7 +26,7 @@

# row table
rt = Tables.rowtable(t)
T = Scale()
T = LowHigh()
n, c = apply(T, rt)
@test Tables.isrowtable(n)
rtₒ = revert(T, n, c)
Expand All @@ -35,7 +35,7 @@
# columntype does not change
for FT in (Float16, Float32)
t = Table(; x=rand(FT, 10))
for T in (MinMax(), Interquartile(), Scale(low=0, high=0.5))
for T in (MinMax(), Interquartile(), LowHigh(low=0, high=0.5))
n, c = apply(T, t)
@test Tables.columntype(t, :x) == Tables.columntype(n, :x)
tₒ = revert(T, n, c)
Expand All @@ -47,7 +47,7 @@
z = x + y
t = Table(; x, y, z)

T = Scale(1, 2, low=0, high=1)
T = LowHigh(1, 2, low=0, high=1)
n, c = apply(T, t)
@test all((1), n.x)
@test all((0), n.x)
Expand All @@ -56,7 +56,7 @@
tₒ = revert(T, n, c)
@test Tables.matrix(t) Tables.matrix(tₒ)

T = Scale([:x, :y], low=0, high=1)
T = LowHigh([:x, :y], low=0, high=1)
n, c = apply(T, t)
@test all((1), n.x)
@test all((0), n.x)
Expand All @@ -65,7 +65,7 @@
tₒ = revert(T, n, c)
@test Tables.matrix(t) Tables.matrix(tₒ)

T = Scale(("x", "y"), low=0, high=1)
T = LowHigh(("x", "y"), low=0, high=1)
n, c = apply(T, t)
@test all((1), n.x)
@test all((0), n.x)
Expand All @@ -74,7 +74,7 @@
tₒ = revert(T, n, c)
@test Tables.matrix(t) Tables.matrix(tₒ)

T = Scale(r"[xy]", low=0, high=1)
T = LowHigh(r"[xy]", low=0, high=1)
n, c = apply(T, t)
@test all((1), n.x)
@test all((0), n.x)
Expand Down
4 changes: 2 additions & 2 deletions test/transforms/sequential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
z = y + rand(Normal(0, 5), 1500)
t = Table(; x, y, z)

T = Scale(low=0.2, high=0.8) EigenAnalysis(:VDV)
T = LowHigh(low=0.2, high=0.8) EigenAnalysis(:VDV)
n, c = apply(T, t)
tₒ = revert(T, n, c)
@test Tables.matrix(t) Tables.matrix(tₒ)
Expand All @@ -18,7 +18,7 @@

# row table
rt = Tables.rowtable(t)
T = Scale(low=0.2, high=0.8) EigenAnalysis(:VDV)
T = LowHigh(low=0.2, high=0.8) EigenAnalysis(:VDV)
n, c = apply(T, rt)
@test Tables.isrowtable(n)
rtₒ = revert(T, n, c)
Expand Down

0 comments on commit f6a72df

Please sign in to comment.