|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + Coerce(pairs, tight=false, verbosity=1) |
| 7 | +
|
| 8 | +Return a copy of the table, ensuring that the scientific types of the columns match the new specification. |
| 9 | +
|
| 10 | +This transform wraps the ScientificTypes.coerce function. Please see their docstring for more details. |
| 11 | +
|
| 12 | +```julia |
| 13 | +Coerce(:col1 => Continuous, :col2 => Count) |
| 14 | +``` |
| 15 | +""" |
| 16 | +struct Coerce{P} <: Transform |
| 17 | + pairs::P |
| 18 | + tight::Bool |
| 19 | + verbosity::Int |
| 20 | +end |
| 21 | + |
| 22 | +Coerce(pair::Pair{Symbol,<:Type}...; tight=false, verbosity=1) = |
| 23 | + Coerce(pair, tight, verbosity) |
| 24 | + |
| 25 | +isrevertible(::Type{<:Coerce}) = true |
| 26 | + |
| 27 | +function apply(transform::Coerce, table) |
| 28 | + newtable = coerce(table, transform.pairs...; |
| 29 | + tight=transform.tight, |
| 30 | + verbosity=transform.verbosity) |
| 31 | + |
| 32 | + types = Tables.schema(table).types |
| 33 | + |
| 34 | + newtable, types |
| 35 | +end |
| 36 | + |
| 37 | +function revert(transform::Coerce, newtable, cache) |
| 38 | + names = Tables.columnnames(newtable) |
| 39 | + cols = Tables.columns(newtable) |
| 40 | + oldcols = map(zip(cache, names)) do (T, n) |
| 41 | + x = Tables.getcolumn(cols, n) |
| 42 | + collect(T, x) |
| 43 | + end |
| 44 | + |
| 45 | + 𝒯 = (; zip(names, oldcols)...) |
| 46 | + 𝒯 |> Tables.materializer(newtable) |
| 47 | +end |
| 48 | + |
0 commit comments