Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Rename transform #15

Merged
merged 16 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/TableTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export
# built-in
Select,
Reject,
Rename,
Identity,
Center,
Scale,
Expand Down
1 change: 1 addition & 0 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ end

include("transforms/identity.jl")
include("transforms/select.jl")
include("transforms/rename.jl")
include("transforms/center.jl")
include("transforms/scale.jl")
include("transforms/zscore.jl")
Expand Down
46 changes: 46 additions & 0 deletions src/transforms/rename.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

"""
Rename(:col₁ => :newcol₁, :col₂ => :newcol₂, ..., :col₁ => :newcolₙ)
juliohm marked this conversation as resolved.
Show resolved Hide resolved

The transform that renames `col₁` to `newcol₁`, `col₂` to `newcol₂`, ...
"""
struct Rename <: Stateless
names::Dict{Symbol,Symbol}
end

pairsyms(x::Pair) = Symbol(first(x)) => Symbol(last(x))

Rename(names::Pair) = pairsyms(names) |> Dict |> Rename
Rename(names...) = pairsyms.(names) |> Dict |> Rename

function apply(transform::Rename, table)
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
_rename(transform.names, table)
end

function revert(transform::Rename, table, cache)
# reversing the key-value pairs of the Dict
newnames = Dict(new => old for (old, new) in transform.names)
_rename(newnames, table) |> first
end


function _rename(names, table)
oldnames = Tables.columnnames(table)

# check if requested renames exist in the table
@assert keys(names) ⊆ oldnames "invalid column names"

# use new names if necessary
newnames = map(oldnames) do oldname
juliohm marked this conversation as resolved.
Show resolved Hide resolved
oldname in keys(names) ? names[oldname] : oldname
end

cols = Tables.columns(table)
vals = [Tables.getcolumn(cols, name) for name in oldnames]
𝒯 = (; zip(newnames, vals)...) |> Tables.materializer(table)

𝒯, nothing
end
53 changes: 53 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,59 @@
@test n1 == n2
end

@testset "Rename" begin
a = rand(4000)
b = rand(4000)
c = rand(4000)
d = rand(4000)
t = Table(; a, b, c, d)

T = Rename(Dict(:a => :x))
n, c = apply(T, t)
@test Tables.columnnames(n) == (:x, :b, :c, :d)
tₒ = revert(T, n, c)
@test t == tₒ

T = Rename(Dict(:a => :x, :c => :y))
n, c = apply(T, t)
@test Tables.columnnames(n) == (:x, :b, :y, :d)
tₒ = revert(T, n, c)
@test t == tₒ

# rename with string pairs
T = Rename("a" => "x", "c" => "y")
n, c = apply(T, t)
@test Tables.columnnames(n) == (:x, :b, :y, :d)
tₒ = revert(T, n, c)
@test t == tₒ

# rename with symbol pairs
T = Rename(:a => :x, :c => :y)
n, c = apply(T, t)
@test Tables.columnnames(n) == (:x, :b, :y, :d)
tₒ = revert(T, n, c)
@test t == tₒ

# rename with mixed pairs
T = Rename("a" => :x)
n, c = apply(T, t)
@test Tables.columnnames(n) == (:x, :b, :c, :d)
tₒ = revert(T, n, c)
@test t == tₒ

T = Rename("a" => :x, :c => "y")
n, c = apply(T, t)
@test Tables.columnnames(n) == (:x, :b, :y, :d)
tₒ = revert(T, n, c)
@test t == tₒ

# reapply test
T = Rename(:b => :x, :d => :y)
n1, c1 = apply(T, t)
n2 = reapply(T, t, c1)
@test n1 == n2
end

@testset "Identity" begin
x = rand(4000)
y = rand(4000)
Expand Down