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 1 commit
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
5 changes: 3 additions & 2 deletions src/TableTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module TableTransforms
using Tables
using ScientificTypes
using Distributions: Normal
using Transducers: tcollect
using Transducers: tcollect, push!!
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
using LinearAlgebra
using Statistics

Expand All @@ -29,9 +29,10 @@ export
colapply, colrevert,

# built-in
Identity,
Select,
Reject,
Identity,
Rename3,
juliohm marked this conversation as resolved.
Show resolved Hide resolved
Center,
Scale,
MinMax,
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
43 changes: 43 additions & 0 deletions src/transforms/rename.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

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

Tha transform that renames `col₁` to `newcol₁`, `col₂` to `newcol₂`, ...
"""
struct Rename3 <: Stateless
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
names::Dict{Symbol,Symbol}
end

function apply(transform::Rename3, table)
new_table = (;)
juliohm marked this conversation as resolved.
Show resolved Hide resolved

for col_name in Tables.columnnames(table)
col_value = Tables.getcolumn(table, col_name)
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved

# if the current name is to be changed, retrive the new name
#and push a col with it, else push the col with the old name
if col_name in keys(transform.names)
new_name = transform.names[col_name]
new_table = push!!(new_table, new_name => col_value)
else
new_table = push!!(new_table, col_name => col_value)
end
end

𝒯 = table |> Tables.materializer(table)
𝒯, nothing
end

function revert(transform::Rename3, table)
# reversing the key-value pairs
new_names = Dict()
for (new, old) in transform.names
new_names[old] = new
end
# normal apply operation, but on a revered Dict
reversed_transform = Rename3(new_names)
apply(reversed_transform, table) |> first
end