Skip to content

Commit 53a2d65

Browse files
committed
Add 'Unit' transform
1 parent 0dea773 commit 53a2d65

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

docs/src/transforms.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ AbsoluteUnits
116116
Unitify
117117
```
118118

119+
## Unit
120+
121+
```@docs
122+
Unit
123+
```
124+
119125
## Map
120126

121127
```@docs

src/TableTransforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export
6767
DropUnits,
6868
AbsoluteUnits,
6969
Unitify,
70+
Unit,
7071
Map,
7172
Replace,
7273
Coalesce,

src/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ include("transforms/dropunits.jl")
281281
include("transforms/dropconstant.jl")
282282
include("transforms/absoluteunits.jl")
283283
include("transforms/unitify.jl")
284+
include("transforms/unit.jl")
284285
include("transforms/map.jl")
285286
include("transforms/replace.jl")
286287
include("transforms/coalesce.jl")

src/transforms/unit.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
"""
6+
Unit(unit)
7+
8+
TODO
9+
10+
Unit(cols₁ => unit₁, cols₂ => unit₂, ..., colsₙ => unitₙ)
11+
12+
TODO
13+
14+
# Examples
15+
16+
```julia
17+
```
18+
"""
19+
struct Unit <: StatelessFeatureTransform
20+
selectors::Vector{ColumnSelector}
21+
units::Vector{Units}
22+
end
23+
24+
Unit() = throw(ArgumentError("cannot create Unit transform without arguments"))
25+
26+
Unit(unit::Units) = Unit([AllSelector()], [unit])
27+
28+
Unit(pairs::Pair...) = Unit(collect(selector.(first.(pairs))), collect(last.(pairs)))
29+
30+
isrevertible(::Type{<:Unit}) = true
31+
32+
function applyfeat(transform::Unit, feat, prep)
33+
cols = Tables.columns(feat)
34+
names = Tables.columnnames(cols)
35+
36+
selectors = transform.selectors
37+
units = transform.units
38+
pairs = mapreduce(vcat, selectors, units) do selector, u
39+
snames = selector(names)
40+
snames .=> u
41+
end
42+
unitdict = Dict(pairs)
43+
44+
tuples = map(names) do name
45+
x = Tables.getcolumn(cols, name)
46+
if haskey(unitdict, name)
47+
u = unitdict[name]
48+
y = uconvert.(u, x)
49+
(y, unit(eltype(x)))
50+
else
51+
(x, nothing)
52+
end
53+
end
54+
55+
columns = first.(tuples)
56+
ounits = last.(tuples)
57+
58+
𝒯 = (; zip(names, columns)...)
59+
newfeat = 𝒯 |> Tables.materializer(feat)
60+
newfeat, ounits
61+
end
62+
63+
function revertfeat(::Unit, newfeat, fcache)
64+
cols = Tables.columns(newfeat)
65+
names = Tables.columnnames(cols)
66+
67+
ounits = fcache
68+
columns = map(names, ounits) do name, u
69+
x = Tables.getcolumn(cols, name)
70+
isnothing(u) ? x : uconvert.(u, x)
71+
end
72+
73+
𝒯 = (; zip(names, columns)...)
74+
𝒯 |> Tables.materializer(newfeat)
75+
end

0 commit comments

Comments
 (0)