Skip to content

Commit cb998f6

Browse files
authored
Add Only transform (#230)
* Add Only transform * Apply suggestions * Rename variable
1 parent 307e953 commit cb998f6

File tree

7 files changed

+88
-0
lines changed

7 files changed

+88
-0
lines changed

docs/src/transforms.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Select
1414
Reject
1515
```
1616

17+
## Only
18+
19+
```@docs
20+
Only
21+
```
22+
1723
## Rename
1824

1925
```@docs

src/TableTransforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export
5151
# built-in
5252
Select,
5353
Reject,
54+
Only,
5455
Rename,
5556
StdNames,
5657
Sort,

src/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ end
265265
# ----------------
266266

267267
include("transforms/select.jl")
268+
include("transforms/only.jl")
268269
include("transforms/rename.jl")
269270
include("transforms/stdnames.jl")
270271
include("transforms/sort.jl")

src/transforms/only.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
"""
6+
Only(S)
7+
8+
Selects only columns that have scientific type `S`.
9+
10+
# Examples
11+
12+
```julia
13+
import DataScienceTraits as DST
14+
Only(DST.Continuous)
15+
```
16+
"""
17+
struct Only <: StatelessFeatureTransform
18+
scitype::DataType
19+
Only(S::Type{<:SciType}) = new(S)
20+
end
21+
22+
isrevertible(::Type{Only}) = true
23+
24+
function applyfeat(transform::Only, feat, prep)
25+
S = transform.scitype
26+
cols = Tables.columns(feat)
27+
names = Tables.columnnames(cols)
28+
snames = filter(names) do name
29+
column = Tables.getcolumn(cols, name)
30+
elscitype(column) <: S
31+
end
32+
strans = Select(snames)
33+
newfeat, sfcache = applyfeat(strans, feat, prep)
34+
newfeat, (strans, sfcache)
35+
end
36+
37+
function revertfeat(::Only, newfeat, fcache)
38+
strans, sfcache = fcache
39+
revertfeat(strans, newfeat, sfcache)
40+
end

test/shows.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@
4242
└─ selector = [:a, :b, :c]"""
4343
end
4444

45+
@testset "Only" begin
46+
T = Only(DST.Continuous)
47+
48+
# compact mode
49+
iostr = sprint(show, T)
50+
@test iostr == "Only(DataScienceTraits.Continuous)"
51+
52+
# full mode
53+
iostr = sprint(show, MIME("text/plain"), T)
54+
@test iostr == """
55+
Only transform
56+
└─ scitype = DataScienceTraits.Continuous"""
57+
end
58+
4559
@testset "Rename" begin
4660
T = Rename(:a => :x, :c => :y)
4761

test/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
transformfiles = [
22
"select.jl",
33
"rename.jl",
4+
"only.jl",
45
"stdnames.jl",
56
"sort.jl",
67
"sample.jl",

test/transforms/only.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@testset "Only" begin
2+
@test isrevertible(Only(DST.Continuous))
3+
4+
a = rand(10)
5+
b = rand(Float32, 10)
6+
c = rand(1:9, 10)
7+
d = rand('a':'z', 10)
8+
t = Table(; a, b, c, d)
9+
10+
T = Only(DST.Continuous)
11+
n, c = apply(T, t)
12+
@test Tables.schema(n).names == (:a, :b)
13+
@test Tables.getcolumn(n, :a) == t.a
14+
@test Tables.getcolumn(n, :b) == t.b
15+
tₒ = revert(T, n, c)
16+
@test t == tₒ
17+
18+
T = Only(DST.Categorical)
19+
n, c = apply(T, t)
20+
@test Tables.schema(n).names == (:c, :d)
21+
@test Tables.getcolumn(n, :c) == t.c
22+
@test Tables.getcolumn(n, :d) == t.d
23+
tₒ = revert(T, n, c)
24+
@test t == tₒ
25+
end

0 commit comments

Comments
 (0)