|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + Unitify() |
| 7 | +
|
| 8 | +Add units to columns of the table using bracket syntax. |
| 9 | +A column named `col [unit]` will be renamed to a unitful |
| 10 | +column `col` with a valid `unit` from Unitful.jl. |
| 11 | +
|
| 12 | +In the case that the `unit` is not recognized by Unitful.jl, |
| 13 | +no units are added. Empty brackets are also allowed to represent |
| 14 | +columns without units, e.g. `col []`. |
| 15 | +""" |
| 16 | +struct Unitify <: StatelessFeatureTransform end |
| 17 | + |
| 18 | +isrevertible(::Type{Unitify}) = true |
| 19 | + |
| 20 | +function _unitify(name) |
| 21 | + m = match(r"(.*)\[(.*)\]", string(name)) |
| 22 | + if !isnothing(m) |
| 23 | + namestr, unitstr = m.captures |
| 24 | + newname = Symbol(strip(namestr)) |
| 25 | + unit = if !isempty(unitstr) |
| 26 | + try |
| 27 | + uparse(unitstr) |
| 28 | + catch |
| 29 | + @warn "the unit \"$unitstr\" is not valid" |
| 30 | + NoUnits |
| 31 | + end |
| 32 | + else |
| 33 | + NoUnits |
| 34 | + end |
| 35 | + newname, unit |
| 36 | + else |
| 37 | + name, NoUnits |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +function applyfeat(::Unitify, feat, prep) |
| 42 | + cols = Tables.columns(feat) |
| 43 | + names = Tables.columnnames(cols) |
| 44 | + |
| 45 | + pairs = map(names) do name |
| 46 | + x = Tables.getcolumn(cols, name) |
| 47 | + newname, unit = _unitify(name) |
| 48 | + newname => x * unit |
| 49 | + end |
| 50 | + |
| 51 | + newfeat = (; pairs...) |> Tables.materializer(feat) |
| 52 | + newfeat, names |
| 53 | +end |
| 54 | + |
| 55 | +function revertfeat(::Unitify, newfeat, fcache) |
| 56 | + cols = Tables.columns(newfeat) |
| 57 | + names = Tables.columnnames(cols) |
| 58 | + |
| 59 | + onames = fcache |
| 60 | + ocolumns = map(names) do name |
| 61 | + x = Tables.getcolumn(cols, name) |
| 62 | + ustrip.(x) |
| 63 | + end |
| 64 | + |
| 65 | + 𝒯 = (; zip(onames, ocolumns)...) |
| 66 | + 𝒯 |> Tables.materializer(newfeat) |
| 67 | +end |
0 commit comments