Skip to content

Commit e0e344e

Browse files
authored
Remove reversibility from transforms: 'Map', 'Replace', 'Sample' and 'Sort' (#258)
1 parent 41d588b commit e0e344e

File tree

8 files changed

+24
-177
lines changed

8 files changed

+24
-177
lines changed

src/transforms/map.jl

+2-25
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function Map(pairs::MapPair...)
5757
Map(selectors, funs, targets)
5858
end
5959

60-
isrevertible(::Type{Map}) = true
60+
isrevertible(::Type{Map}) = false
6161

6262
_makename(snames, fun) = Symbol(join([snames; nameof(fun)], "_"))
6363

@@ -73,10 +73,6 @@ function applyfeat(transform::Map, feat, prep)
7373
names = collect(onames)
7474
columns = Any[Tables.getcolumn(cols, nm) for nm in onames]
7575

76-
# replaced names and columns
77-
rnames = empty(names)
78-
rcolumns = empty(columns)
79-
8076
# mapped columns
8177
mapped = map(selectors, funs, targets) do selector, fun, target
8278
snames = selector(names)
@@ -88,9 +84,7 @@ function applyfeat(transform::Map, feat, prep)
8884

8985
for (name, column) in mapped
9086
if name onames
91-
push!(rnames, name)
9287
i = findfirst(==(name), onames)
93-
push!(rcolumns, columns[i])
9488
columns[i] = column
9589
else
9690
push!(names, name)
@@ -100,22 +94,5 @@ function applyfeat(transform::Map, feat, prep)
10094

10195
𝒯 = (; zip(names, columns)...)
10296
newfeat = 𝒯 |> Tables.materializer(feat)
103-
newfeat, (onames, rnames, rcolumns)
104-
end
105-
106-
function revertfeat(::Map, newfeat, fcache)
107-
cols = Tables.columns(newfeat)
108-
109-
onames, rnames, rcolumns = fcache
110-
ocolumns = map(onames) do name
111-
if name rnames
112-
i = findfirst(==(name), rnames)
113-
rcolumns[i]
114-
else
115-
Tables.getcolumn(cols, name)
116-
end
117-
end
118-
119-
𝒯 = (; zip(onames, ocolumns)...)
120-
𝒯 |> Tables.materializer(newfeat)
97+
newfeat, nothing
12198
end

src/transforms/replace.jl

+5-25
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function Replace(pairs::Pair...)
5858
Replace(selectors, preds, news)
5959
end
6060

61-
isrevertible(::Type{<:Replace}) = true
61+
isrevertible(::Type{<:Replace}) = false
6262

6363
function applyfeat(transform::Replace, feat, prep)
6464
cols = Tables.columns(feat)
@@ -81,43 +81,23 @@ function applyfeat(transform::Replace, feat, prep)
8181
name => reps
8282
end
8383

84-
tuples = map(colreps) do (name, reps)
84+
columns = map(colreps) do (name, reps)
8585
x = Tables.getcolumn(cols, name)
8686
if isnothing(reps)
87-
x, nothing
87+
x
8888
else
89-
# reversal dict
90-
rev = Dict{Int,eltype(x)}()
91-
y = map(enumerate(x)) do (i, v)
89+
map(x) do v
9290
for (pred, new) in reps
9391
if pred(v)
94-
rev[i] = v
9592
return new
9693
end
9794
end
9895
v
9996
end
100-
y, rev
10197
end
10298
end
10399

104-
columns = first.(tuples)
105-
fcache = last.(tuples)
106-
107100
𝒯 = (; zip(names, columns)...)
108101
newfeat = 𝒯 |> Tables.materializer(feat)
109-
newfeat, fcache
110-
end
111-
112-
function revertfeat(::Replace, newfeat, fcache)
113-
cols = Tables.columns(newfeat)
114-
names = Tables.columnnames(cols)
115-
116-
columns = map(names, fcache) do name, rev
117-
y = Tables.getcolumn(cols, name)
118-
isnothing(rev) ? y : [get(rev, i, y[i]) for i in 1:length(y)]
119-
end
120-
121-
𝒯 = (; zip(names, columns)...)
122-
𝒯 |> Tables.materializer(newfeat)
102+
newfeat, nothing
123103
end

src/transforms/sample.jl

+5-35
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Sample(size::Int, weights::AbstractWeights; replace=false, ordered=false, rng=Ra
4040

4141
Sample(size::Int, weights; kwargs...) = Sample(size, Weights(collect(weights)); kwargs...)
4242

43-
isrevertible(::Type{<:Sample}) = true
43+
isrevertible(::Type{<:Sample}) = false
4444

4545
function preprocess(transform::Sample, feat)
4646
# retrieve valid indices
@@ -59,46 +59,16 @@ function preprocess(transform::Sample, feat)
5959
sample(rng, inds, weights, size; replace, ordered)
6060
end
6161

62-
# rejected indices
63-
rinds = setdiff(inds, sinds)
64-
65-
sinds, rinds
62+
sinds
6663
end
6764

6865
function applyfeat(::Sample, feat, prep)
6966
# preprocessed indices
70-
sinds, rinds = prep
67+
sinds = prep
7168

72-
# selected/rejected rows
69+
# selected rows
7370
srows = Tables.subset(feat, sinds, viewhint=true)
74-
rrows = Tables.subset(feat, rinds, viewhint=true)
7571

7672
newfeat = srows |> Tables.materializer(feat)
77-
newfeat, (sinds, rinds, rrows)
78-
end
79-
80-
function revertfeat(::Sample, newfeat, fcache)
81-
cols = Tables.columns(newfeat)
82-
names = Tables.columnnames(cols)
83-
84-
sinds, rinds, rrows = fcache
85-
86-
# columns with selected rows in original order
87-
uinds = indexin(sort(unique(sinds)), sinds)
88-
columns = map(names) do name
89-
y = Tables.getcolumn(cols, name)
90-
[y[i] for i in uinds]
91-
end
92-
93-
# insert rejected rows into columns
94-
rrcols = Tables.columns(rrows)
95-
for (name, x) in zip(names, columns)
96-
r = Tables.getcolumn(rrcols, name)
97-
for (i, v) in zip(rinds, r)
98-
insert!(x, i, v)
99-
end
100-
end
101-
102-
𝒯 = (; zip(names, columns)...)
103-
𝒯 |> Tables.materializer(newfeat)
73+
newfeat, nothing
10474
end

src/transforms/sort.jl

+2-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Sort(cols::C...; kwargs...) where {C<:Column} = Sort(selector(cols), values(kwar
3434

3535
Sort(; kwargs...) = throw(ArgumentError("cannot create Sort transform without arguments"))
3636

37-
isrevertible(::Type{<:Sort}) = true
37+
isrevertible(::Type{<:Sort}) = false
3838

3939
function preprocess(transform::Sort, feat)
4040
cols = Tables.columns(feat)
@@ -59,18 +59,5 @@ function applyfeat(::Sort, feat, prep)
5959

6060
newfeat = srows |> Tables.materializer(feat)
6161

62-
newfeat, sinds
63-
end
64-
65-
function revertfeat(::Sort, newfeat, fcache)
66-
# collect all rows
67-
rows = Tables.rowtable(newfeat)
68-
69-
# reverting indices
70-
sinds = fcache
71-
rinds = sortperm(sinds)
72-
73-
rrows = view(rows, rinds)
74-
75-
rrows |> Tables.materializer(newfeat)
62+
newfeat, nothing
7663
end

test/transforms/map.jl

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@testset "Map" begin
2+
@test !isrevertible(Map(:a => sin))
3+
24
a = [4, 7, 8, 5, 8, 1]
35
b = [1, 9, 1, 7, 9, 4]
46
c = [2, 8, 6, 3, 2, 2]
@@ -9,65 +11,47 @@
911
n, c = apply(T, t)
1012
@test Tables.schema(n).names == (:a, :b, :c, :d, :a_sin)
1113
@test n.a_sin == sin.(t.a)
12-
tₒ = revert(T, n, c)
13-
@test t == tₒ
1414

1515
T = Map(:b => cos)
1616
n, c = apply(T, t)
1717
@test Tables.schema(n).names == (:a, :b, :c, :d, :b_cos)
1818
@test n.b_cos == cos.(t.b)
19-
tₒ = revert(T, n, c)
20-
@test t == tₒ
2119

2220
T = Map("c" => tan)
2321
n, c = apply(T, t)
2422
@test Tables.schema(n).names == (:a, :b, :c, :d, :c_tan)
2523
@test n.c_tan == tan.(t.c)
26-
tₒ = revert(T, n, c)
27-
@test t == tₒ
2824

2925
T = Map(:a => sin => :a)
3026
n, c = apply(T, t)
3127
@test Tables.schema(n).names == (:a, :b, :c, :d)
3228
@test n.a == sin.(t.a)
33-
tₒ = revert(T, n, c)
34-
@test t == tₒ
3529

3630
T = Map(:a => sin => "a")
3731
n, c = apply(T, t)
3832
@test Tables.schema(n).names == (:a, :b, :c, :d)
3933
@test n.a == sin.(t.a)
40-
tₒ = revert(T, n, c)
41-
@test t == tₒ
4234

4335
T = Map([2, 3] => ((b, c) -> 2b + c) => :op1)
4436
n, c = apply(T, t)
4537
@test Tables.schema(n).names == (:a, :b, :c, :d, :op1)
4638
@test n.op1 == @. 2 * t.b + t.c
47-
tₒ = revert(T, n, c)
48-
@test t == tₒ
4939

5040
T = Map([:a, :c] => ((a, c) -> 2a * 3c) => :op1)
5141
n, c = apply(T, t)
5242
@test Tables.schema(n).names == (:a, :b, :c, :d, :op1)
5343
@test n.op1 == @. 2 * t.a * 3 * t.c
54-
tₒ = revert(T, n, c)
55-
@test t == tₒ
5644

5745
T = Map(["c", "a"] => ((c, a) -> 3c / a) => :op1, "c" => tan)
5846
n, c = apply(T, t)
5947
@test Tables.schema(n).names == (:a, :b, :c, :d, :op1, :c_tan)
6048
@test n.op1 == @. 3 * t.c / t.a
6149
@test n.c_tan == tan.(t.c)
62-
tₒ = revert(T, n, c)
63-
@test t == tₒ
6450

6551
T = Map(r"[abc]" => ((a, b, c) -> a^2 - 2b + c) => "op1")
6652
n, c = apply(T, t)
6753
@test Tables.schema(n).names == (:a, :b, :c, :d, :op1)
6854
@test n.op1 == @. t.a^2 - 2 * t.b + t.c
69-
tₒ = revert(T, n, c)
70-
@test t == tₒ
7155

7256
# throws
7357
@test_throws ArgumentError Map()

0 commit comments

Comments
 (0)