Skip to content

Commit 74200f0

Browse files
authored
Add Assert transform (#249)
* Add Assert transform * Fix typo * Rename variable
1 parent fa1ac27 commit 74200f0

23 files changed

+155
-78
lines changed

docs/src/transforms.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Below is the list of transforms that are are available in this package.
44

5+
# Assert
6+
7+
```@docs
8+
Assert
9+
```
10+
511
## Select
612

713
```@docs

src/TableTransforms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import Distributions: quantile, cdf
3030
import TransformsBase: assertions, isrevertible, isinvertible
3131
import TransformsBase: apply, revert, reapply, preprocess, inverse
3232

33-
include("assertions.jl")
3433
include("tabletraits.jl")
3534
include("distributions.jl")
3635
include("tableselection.jl")
@@ -49,6 +48,7 @@ export
4948
reapply,
5049

5150
# built-in
51+
Assert,
5252
Select,
5353
Reject,
5454
Satisfies,

src/assertions.jl

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ end
265265
# ----------------
266266

267267
include("transforms/utils.jl")
268+
include("transforms/assert.jl")
268269
include("transforms/select.jl")
269270
include("transforms/satisfies.jl")
270271
include("transforms/rename.jl")

src/transforms/assert.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
"""
6+
Assert(; cond, msg="")
7+
8+
Asserts all columns of the table by throwing a `AssertionError(msg)`
9+
if `cond(column)` returns `false`, otherwise returns the input table.
10+
11+
The `msg` argument can be a string, or a function that receives
12+
the column name and returns a string, e.g.: `nm -> "error in column \$nm"`.
13+
14+
Assert(col₁, col₂, ..., colₙ; cond, msg="")
15+
Assert([col₁, col₂, ..., colₙ]; cond, msg="")
16+
Assert((col₁, col₂, ..., colₙ); cond, msg="")
17+
18+
Asserts the selected columns `col₁`, `col₂`, ..., `colₙ`.
19+
20+
Assert(regex; cond, msg="")
21+
22+
Asserts the columns that match with `regex`.
23+
24+
# Examples
25+
26+
```julia
27+
Assert(cond=allunique, msg="assertion error")
28+
Assert([2, 3, 5], cond=x -> sum(x) > 100)
29+
Assert([:b, :c, :e], cond=x -> eltype(x) <: Integer)
30+
Assert(("b", "c", "e"), cond=allunique, msg=nm -> "error in column \$nm")
31+
Assert(r"[bce]", cond=x -> sum(x) > 100)
32+
```
33+
"""
34+
struct Assert{S<:ColumnSelector,C,M} <: StatelessFeatureTransform
35+
selector::S
36+
cond::C
37+
msg::M
38+
end
39+
40+
Assert(selector::ColumnSelector; cond, msg="") = Assert(selector, cond, msg)
41+
42+
Assert(; kwargs...) = Assert(AllSelector(); kwargs...)
43+
Assert(cols; kwargs...) = Assert(selector(cols); kwargs...)
44+
Assert(cols::C...; kwargs...) where {C<:Column} = Assert(selector(cols); kwargs...)
45+
46+
isrevertible(::Type{<:Assert}) = true
47+
48+
function applyfeat(transform::Assert, feat, prep)
49+
cols = Tables.columns(feat)
50+
names = Tables.columnnames(cols)
51+
snames = transform.selector(names)
52+
cond = transform.cond
53+
msg = transform.msg
54+
55+
msgfun = msg isa AbstractString ? _ -> msg : msg
56+
for name in snames
57+
x = Tables.getcolumn(cols, name)
58+
_assert(cond(x), msgfun(name))
59+
end
60+
61+
feat, nothing
62+
end
63+
64+
revertfeat(::Assert, newfeat, fcache) = newfeat

src/transforms/center.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Center() = Center(AllSelector())
3535
Center(cols) = Center(selector(cols))
3636
Center(cols::C...) where {C<:Column} = Center(selector(cols))
3737

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

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

src/transforms/closure.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct Closure <: StatelessFeatureTransform end
1515

1616
isrevertible(::Type{Closure}) = true
1717

18-
assertions(::Closure) = [SciTypeAssertion{Continuous}()]
18+
assertions(::Closure) = [scitypeassert(Continuous)]
1919

2020
function applyfeat(::Closure, feat, prep)
2121
cols = Tables.columns(feat)

src/transforms/eigenanalysis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252

5353
EigenAnalysis(proj; maxdim=nothing, pratio=1.0) = EigenAnalysis(proj, maxdim, pratio)
5454

55-
assertions(::EigenAnalysis) = [SciTypeAssertion{Continuous}()]
55+
assertions(::EigenAnalysis) = [scitypeassert(Continuous)]
5656

5757
isrevertible(::Type{EigenAnalysis}) = true
5858

src/transforms/indicator.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ end
4646

4747
Indicator(col::Column; k=10, scale=:quantile, categ=false) = Indicator(selector(col), k, scale, categ)
4848

49-
assertions(transform::Indicator) = [SciTypeAssertion{Continuous}(transform.selector)]
49+
assertions(transform::Indicator) = [scitypeassert(Continuous, transform.selector)]
5050

5151
isrevertible(::Type{<:Indicator}) = true
5252

src/transforms/levels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Levels(pairs::Pair{C}...; ordered=nothing) where {C<:Column} =
2727

2828
Levels(; kwargs...) = throw(ArgumentError("cannot create Levels transform without arguments"))
2929

30-
assertions(transform::Levels) = [SciTypeAssertion{Categorical}(transform.selector)]
30+
assertions(transform::Levels) = [scitypeassert(Categorical, transform.selector)]
3131

3232
isrevertible(::Type{<:Levels}) = true
3333

0 commit comments

Comments
 (0)