Skip to content

Commit 5216358

Browse files
authored
Refactor: Drop ColSpec in favor of ColumnSelector from ColumnSelectors.jl (#203)
* Refactor: Drop 'ColSpec' in favor of 'ColumnSelector' from ColumnSelectors.jl * Update 'DropUnits' and 'AbsoluteUnits' * Fix typo
1 parent e316a9b commit 5216358

29 files changed

+181
-547
lines changed

Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "1.15.3"
66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
88
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
9+
ColumnSelectors = "9cc86067-7e36-4c61-b350-1ac9833d277f"
910
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1011
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1112
NelderMead = "2f6b4ddb-b4ff-44c0-b59b-2ab99302f970"
@@ -22,6 +23,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2223
[compat]
2324
AbstractTrees = "0.4"
2425
CategoricalArrays = "0.10"
26+
ColumnSelectors = "0.1"
2527
Distributions = "0.25"
2628
NelderMead = "0.4"
2729
PrettyTables = "2"

src/TableTransforms.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ using CategoricalArrays
1919
using Random
2020
using NelderMead: optimise
2121

22+
using ColumnSelectors: ColumnSelector, SingleColumnSelector
23+
using ColumnSelectors: AllSelector, Column, selector, selectsingle
2224
using Unitful: AbstractQuantity, AffineQuantity, AffineUnits, Units
2325

2426
import Distributions: ContinuousUnivariateDistribution
@@ -28,7 +30,6 @@ import TransformsBase: Transform, Identity, →
2830
import TransformsBase: assertions, isrevertible, preprocess
2931
import TransformsBase: apply, revert, reapply
3032

31-
include("colspec.jl")
3233
include("assertions.jl")
3334
include("tabletraits.jl")
3435
include("distributions.jl")

src/assertions.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
# ------------------------------------------------------------------
44

55
"""
6-
SciTypeAssertion{T}(colspec = AllSpec())
6+
SciTypeAssertion{T}(selector = AllSelector())
77
8-
Asserts that the columns in the `colspec` have a scientific type `T`.
8+
Asserts that the columns in the `selector` have a scientific type `T`.
99
"""
10-
struct SciTypeAssertion{T,S<:ColSpec}
11-
colspec::S
10+
struct SciTypeAssertion{T,S<:ColumnSelector}
11+
selector::S
1212
end
1313

14-
SciTypeAssertion{T}(colspec::S) where {T,S<:ColSpec} = SciTypeAssertion{T,S}(colspec)
14+
SciTypeAssertion{T}(selector::S) where {T,S<:ColumnSelector} = SciTypeAssertion{T,S}(selector)
1515

16-
SciTypeAssertion{T}() where {T} = SciTypeAssertion{T}(AllSpec())
16+
SciTypeAssertion{T}() where {T} = SciTypeAssertion{T}(AllSelector())
1717

1818
function (assertion::SciTypeAssertion{T})(table) where {T}
1919
cols = Tables.columns(table)
2020
names = Tables.columnnames(cols)
21-
snames = choose(assertion.colspec, names)
21+
snames = assertion.selector(names)
2222

2323
for nm in snames
2424
x = Tables.getcolumn(cols, nm)

src/colspec.jl

-219
This file was deleted.

src/transforms.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ these functions in parallel for all columns with multiple threads.
9191
9292
## Notes
9393
94-
* `ColwiseFeatureTransform` subtypes must have a `colspec` field.
94+
* `ColwiseFeatureTransform` subtypes must have a `selector` field.
9595
"""
9696
abstract type ColwiseFeatureTransform <: FeatureTransform end
9797

@@ -182,7 +182,7 @@ function applyfeat(transform::ColwiseFeatureTransform, feat, prep)
182182
# retrieve column names and values
183183
cols = Tables.columns(feat)
184184
names = Tables.columnnames(cols)
185-
snames = choose(transform.colspec, names)
185+
snames = transform.selector(names)
186186

187187
# function to transform a single column
188188
function colfunc(n)

src/transforms/absoluteunits.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ AbsoluteUnits(("b", "c", "e"))
2828
AbsoluteUnits(r"[bce]")
2929
```
3030
"""
31-
struct AbsoluteUnits{S<:ColSpec} <: StatelessFeatureTransform
32-
colspec::S
31+
struct AbsoluteUnits{S<:ColumnSelector} <: StatelessFeatureTransform
32+
selector::S
3333
end
3434

35-
AbsoluteUnits() = AbsoluteUnits(AllSpec())
36-
AbsoluteUnits(spec) = AbsoluteUnits(colspec(spec))
37-
AbsoluteUnits(cols::T...) where {T<:Col} = AbsoluteUnits(colspec(cols))
35+
AbsoluteUnits() = AbsoluteUnits(AllSelector())
36+
AbsoluteUnits(cols) = AbsoluteUnits(selector(cols))
37+
AbsoluteUnits(cols::C...) where {C<:Column} = AbsoluteUnits(selector(cols))
3838

3939
isrevertible(::Type{<:AbsoluteUnits}) = true
4040

@@ -51,7 +51,7 @@ end
5151
function applyfeat(transform::AbsoluteUnits, feat, prep)
5252
cols = Tables.columns(feat)
5353
names = Tables.columnnames(cols)
54-
snames = choose(transform.colspec, names)
54+
snames = transform.selector(names)
5555

5656
tuples = map(names) do name
5757
x = Tables.getcolumn(cols, name)

src/transforms/center.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ Center(("a", "c", "e"))
2727
Center(r"[ace]")
2828
```
2929
"""
30-
struct Center{S<:ColSpec} <: ColwiseFeatureTransform
31-
colspec::S
30+
struct Center{S<:ColumnSelector} <: ColwiseFeatureTransform
31+
selector::S
3232
end
3333

34-
Center() = Center(AllSpec())
35-
Center(spec) = Center(colspec(spec))
36-
Center(cols::C...) where {C<:Col} = Center(colspec(cols))
34+
Center() = Center(AllSelector())
35+
Center(cols) = Center(selector(cols))
36+
Center(cols::C...) where {C<:Column} = Center(selector(cols))
3737

38-
assertions(transform::Center) = [SciTypeAssertion{Continuous}(transform.colspec)]
38+
assertions(transform::Center) = [SciTypeAssertion{Continuous}(transform.selector)]
3939

4040
isrevertible(::Type{<:Center}) = true
4141

src/transforms/coalesce.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ Coalesce(r"[ace]", value=4)
3333
3434
* The transform can alter the element type of columns from `Union{Missing,T}` to `T`.
3535
"""
36-
struct Coalesce{S<:ColSpec,T} <: ColwiseFeatureTransform
37-
colspec::S
36+
struct Coalesce{S<:ColumnSelector,T} <: ColwiseFeatureTransform
37+
selector::S
3838
value::T
3939
end
4040

41-
Coalesce(; value) = Coalesce(AllSpec(), value)
42-
Coalesce(spec; value) = Coalesce(colspec(spec), value)
43-
Coalesce(cols::C...; value) where {C<:Col} = Coalesce(colspec(cols), value)
41+
Coalesce(; value) = Coalesce(AllSelector(), value)
42+
Coalesce(cols; value) = Coalesce(selector(cols), value)
43+
Coalesce(cols::C...; value) where {C<:Column} = Coalesce(selector(cols), value)
4444

4545
isrevertible(::Type{<:Coalesce}) = true
4646

0 commit comments

Comments
 (0)