|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + Compose(; as=:CODA) |
| 7 | +
|
| 8 | +Converts all columns of the table into parts of a composition |
| 9 | +in a new column named `as`, using the `CoDa.compose` function. |
| 10 | +
|
| 11 | + Compose(col₁, col₂, ..., colₙ; as=:CODA) |
| 12 | + Compose([col₁, col₂, ..., colₙ]; as=:CODA) |
| 13 | + Compose((col₁, col₂, ..., colₙ); as=:CODA) |
| 14 | +
|
| 15 | +Converts the selected columns `col₁`, `col₂`, ..., `colₙ` into parts of a composition. |
| 16 | +
|
| 17 | + Compose(regex; as=:CODA) |
| 18 | +
|
| 19 | +Converts the columns that match with `regex` into parts of a composition. |
| 20 | +
|
| 21 | +# Examples |
| 22 | +
|
| 23 | +```julia |
| 24 | +Compose(as=:comp) |
| 25 | +Compose([2, 3, 5]) |
| 26 | +Compose([:b, :c, :e]) |
| 27 | +Compose(("b", "c", "e")) |
| 28 | +Compose(r"[bce]", as="COMP") |
| 29 | +``` |
| 30 | +""" |
| 31 | +struct Compose{S<:ColumnSelector} <: StatelessFeatureTransform |
| 32 | + selector::S |
| 33 | + as::Symbol |
| 34 | +end |
| 35 | + |
| 36 | +Compose(selector::ColumnSelector; as=:CODA) = Compose(selector, Symbol(as)) |
| 37 | + |
| 38 | +Compose(; kwargs...) = Compose(AllSelector(); kwargs...) |
| 39 | +Compose(cols; kwargs...) = Compose(selector(cols); kwargs...) |
| 40 | +Compose(cols::C...; kwargs...) where {C<:Column} = Compose(selector(cols); kwargs...) |
| 41 | + |
| 42 | +isrevertible(::Type{<:Compose}) = true |
| 43 | + |
| 44 | +function applyfeat(transform::Compose, feat, prep) |
| 45 | + cols = Tables.columns(feat) |
| 46 | + names = Tables.columnnames(cols) |
| 47 | + snames = transform.selector(names) |
| 48 | + as = transform.as |
| 49 | + |
| 50 | + newfeat = compose(feat, snames; as) |
| 51 | + |
| 52 | + newfeat, (names, snames, as) |
| 53 | +end |
| 54 | + |
| 55 | +function revertfeat(::Compose, newfeat, fcache) |
| 56 | + cols = Tables.columns(newfeat) |
| 57 | + names, snames, as = fcache |
| 58 | + |
| 59 | + coda = Tables.getcolumn(cols, as) |
| 60 | + columns = map(names) do name |
| 61 | + if name ∈ snames |
| 62 | + Tables.getcolumn(coda, name) |
| 63 | + else |
| 64 | + Tables.getcolumn(cols, name) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + 𝒯 = (; zip(names, columns)...) |
| 69 | + 𝒯 |> Tables.materializer(newfeat) |
| 70 | +end |
0 commit comments