Skip to content

Commit 92ce8a2

Browse files
authored
Add 'DropConstant' transform (#254)
1 parent 6793c43 commit 92ce8a2

File tree

7 files changed

+105
-23
lines changed

7 files changed

+105
-23
lines changed

src/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ include("transforms/filter.jl")
277277
include("transforms/dropmissing.jl")
278278
include("transforms/dropextrema.jl")
279279
include("transforms/dropunits.jl")
280+
include("transforms/dropconstant.jl")
280281
include("transforms/absoluteunits.jl")
281282
include("transforms/map.jl")
282283
include("transforms/replace.jl")

src/transforms/dropconstant.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
"""
6+
DropConstant()
7+
8+
Drops the constant columns using the `allequal` function.
9+
"""
10+
struct DropConstant <: StatelessFeatureTransform end
11+
12+
isrevertible(::Type{DropConstant}) = true
13+
14+
function applyfeat(::DropConstant, feat, prep)
15+
cols = Tables.columns(feat)
16+
names = Tables.columnnames(cols) |> collect
17+
18+
# constant columns
19+
cnames = filter(names) do name
20+
x = Tables.getcolumn(cols, name)
21+
allequal(x)
22+
end
23+
cinds = indexin(cnames, names)
24+
cvalues = [first(Tables.getcolumn(cols, nm)) for nm in cnames]
25+
26+
# selected columns
27+
snames = setdiff(names, cnames)
28+
columns = [Tables.getcolumn(cols, nm) for nm in snames]
29+
30+
𝒯 = (; zip(snames, columns)...)
31+
newfeat = 𝒯 |> Tables.materializer(feat)
32+
newfeat, (cinds, cnames, cvalues)
33+
end
34+
35+
function revertfeat(::DropConstant, newfeat, fcache)
36+
cols = Tables.columns(newfeat)
37+
names = Tables.columnnames(cols) |> collect
38+
columns = Any[Tables.getcolumn(cols, name) for name in names]
39+
40+
cinds, cnames, cvalues = fcache
41+
42+
nrows = _nrows(newfeat)
43+
for (i, cind) in enumerate(cinds)
44+
insert!(names, cind, cnames[i])
45+
insert!(columns, cind, fill(cvalues[i], nrows))
46+
end
47+
48+
𝒯 = (; zip(names, columns)...)
49+
𝒯 |> Tables.materializer(newfeat)
50+
end

src/transforms/satisfies.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,3 @@ Except(DST.Categorical)
6666
```
6767
"""
6868
Except(S::Type{<:SciType}) = Satisfies(x -> !(elscitype(x) <: S))
69-
70-
"""
71-
DropConstant()
72-
73-
Drops the constant columns using the `allequal` function.
74-
"""
75-
DropConstant() = Satisfies(!allequal)

test/shows.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,32 @@
191191
└─ high = 0.75"""
192192
end
193193

194+
@testset "DropUnits" begin
195+
T = DropUnits(:a, :b, :c)
196+
197+
# compact mode
198+
iostr = sprint(show, T)
199+
@test iostr == "DropUnits([:a, :b, :c])"
200+
201+
# full mode
202+
iostr = sprint(show, MIME("text/plain"), T)
203+
@test iostr == """
204+
DropUnits transform
205+
└─ selector = [:a, :b, :c]"""
206+
end
207+
208+
@testset "DropConstant" begin
209+
T = DropConstant()
210+
211+
# compact mode
212+
iostr = sprint(show, T)
213+
@test iostr == "DropConstant()"
214+
215+
# full mode
216+
iostr = sprint(show, MIME("text/plain"), T)
217+
@test iostr == "DropConstant transform"
218+
end
219+
194220
@testset "Map" begin
195221
fun = (a, b) -> 2a + b
196222
T = Map(:a => sin, [:a, :b] => fun => :c)

test/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ transformfiles = [
1111
"dropmissing.jl",
1212
"dropextrema.jl",
1313
"dropunits.jl",
14+
"dropconstant.jl",
1415
"absoluteunits.jl",
1516
"map.jl",
1617
"replace.jl",

test/transforms/dropconstant.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@testset "DropConstant" begin
2+
@test isrevertible(DropUnits())
3+
4+
a = [4, 6, 7, 8, 1, 2]
5+
b = fill(5, 6)
6+
c = [1.9, 7.4, 8.6, 8.9, 2.4, 7.7]
7+
d = fill(5.5, 6)
8+
t = Table(; a, b, c, d)
9+
10+
T = DropConstant()
11+
n, c = apply(T, t)
12+
@test Tables.schema(n).names == (:a, :c)
13+
@test Tables.getcolumn(n, :a) == t.a
14+
@test Tables.getcolumn(n, :c) == t.c
15+
tₒ = revert(T, n, c)
16+
@test t == tₒ
17+
18+
# revert with different number of rows
19+
T = DropConstant()
20+
_, c = apply(T, t)
21+
n = Table(; a=[t.a; [3, 4, 7, 2]], c=[t.c; [5.3, 4.9, 3.1, 6.8]])
22+
r = revert(T, n, c)
23+
@test r.a == n.a
24+
@test r.b == fill(5, 10)
25+
@test r.c == n.c
26+
@test r.d == fill(5.5, 10)
27+
end

test/transforms/satisfies.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,3 @@ end
7171
tₒ = revert(T, n, c)
7272
@test t == tₒ
7373
end
74-
75-
@testset "DropConstant" begin
76-
a = [4, 6, 7, 8, 1, 2]
77-
b = fill(5, 6)
78-
c = [1.9, 7.4, 8.6, 8.9, 2.4, 7.7]
79-
d = fill(5.5, 6)
80-
t = Table(; a, b, c, d)
81-
82-
T = DropConstant()
83-
n, c = apply(T, t)
84-
@test Tables.schema(n).names == (:a, :c)
85-
@test Tables.getcolumn(n, :a) == t.a
86-
@test Tables.getcolumn(n, :c) == t.c
87-
tₒ = revert(T, n, c)
88-
@test t == tₒ
89-
end

0 commit comments

Comments
 (0)