Skip to content

Commit 33e8f01

Browse files
authored
Merge pull request #226 from JuliaML/assert
Replace the `assert` macro with the new `_assert` utility function
2 parents beb0c2a + 97b0228 commit 33e8f01

12 files changed

+22
-18
lines changed

src/assertions.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function (assertion::SciTypeAssertion{T})(table) where {T}
2222

2323
for nm in snames
2424
x = Tables.getcolumn(cols, nm)
25-
@assert elscitype(x) <: T "the elements of the column '$nm' are not of scientific type $T"
25+
_assert(elscitype(x) <: T, "the elements of the column '$nm' are not of scientific type $T")
2626
end
2727
end
28+
29+
#-------
30+
# UTILS
31+
#-------
32+
33+
_assert(cond, msg) = cond || throw(AssertionError(msg))

src/distributions.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct EmpiricalDistribution{T<:Real} <: ContinuousUnivariateDistribution
1111
values::Vector{T}
1212

1313
function EmpiricalDistribution{T}(values) where {T<:Real}
14-
@assert !isempty(values) "values must be provided"
14+
_assert(!isempty(values), "values must be provided")
1515
new(sort(values))
1616
end
1717
end

src/tableselection.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct TableSelection{T,C}
1212

1313
function TableSelection(table::T, names, onames) where {T}
1414
cols = Tables.columns(table)
15-
@assert onames Tables.columnnames(cols)
15+
_assert(onames Tables.columnnames(cols), "all selected columns must exist in the table")
1616
ncols = length(names)
1717
mapnames = Dict(zip(names, onames))
1818
new{T,typeof(cols)}(table, cols, ncols, names, onames, mapnames)

src/transforms.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function apply(transform::FeatureTransform, table)
139139
end
140140

141141
function revert(transform::FeatureTransform, newtable, cache)
142-
@assert isrevertible(transform) "Transform is not revertible"
142+
_assert(isrevertible(transform), "Transform is not revertible")
143143

144144
newfeat, newmeta = divide(newtable)
145145
fcache, mcache = cache
@@ -242,7 +242,7 @@ function reapplyfeat(transform::ColwiseFeatureTransform, feat, fcache)
242242
caches, snames = fcache
243243

244244
# check that cache is valid
245-
@assert length(names) == length(caches) "invalid caches for feat"
245+
_assert(length(names) == length(caches), "invalid caches for feat")
246246

247247
# function to transform a single column
248248
function colfunc(i)

src/transforms/dropextrema.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct DropExtrema{S<:SingleColumnSelector,T} <: StatelessFeatureTransform
2222
high::T
2323

2424
function DropExtrema(selector::S, low::T, high::T) where {S<:SingleColumnSelector,T}
25-
@assert 0 low high 1 "invalid quantiles"
25+
_assert(0 low high 1, "invalid quantiles")
2626
new{S,T}(selector, low, high)
2727
end
2828
end

src/transforms/eigenanalysis.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ struct EigenAnalysis <: FeatureTransform
4444
pratio::Float64
4545

4646
function EigenAnalysis(proj, maxdim, pratio)
47-
@assert proj (:V, :VD, :VDV) "Invalid projection."
48-
@assert 0 pratio 1 "Invalid pratio."
47+
_assert(proj (:V, :VD, :VDV), "invalid projection")
48+
_assert(0 pratio 1, "invalid pratio")
4949
new(proj, maxdim, pratio)
5050
end
5151
end

src/transforms/functional.jl

-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ function applyfeat(transform::Functional, feat, prep)
7878
end
7979

8080
function revertfeat(transform::Functional, newfeat, fcache)
81-
@assert isrevertible(transform) "TableTransform is not revertible."
82-
8381
cols = Tables.columns(newfeat)
8482
names = Tables.columnnames(cols)
8583

src/transforms/logratio.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function applyfeat(transform::LogRatio, feat, prep)
2828

2929
# reference variable
3030
rvar = refvar(transform, names)
31-
@assert rvar names "invalid reference variable"
31+
_assert(rvar names, "invalid reference variable")
3232
rind = findfirst(==(rvar), names)
3333

3434
# permute columns if necessary

src/transforms/parallel.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function apply(p::ParallelTableTransform, table)
5454
metas = last.(splits)
5555

5656
# check the number of rows of generated tables
57-
@assert allequal(_nrows(f) for f in feats) "parallel branches must produce the same number of rows"
57+
_assert(allequal(_nrows(f) for f in feats), "parallel branches must produce the same number of rows")
5858

5959
# table with concatenated features
6060
newfeat = tablehcat(feats)
@@ -90,7 +90,7 @@ function revert(p::ParallelTableTransform, newtable, cache)
9090
caches = cache[1]
9191
rinfo = cache[2]
9292

93-
@assert !isnothing(rinfo) "transform is not revertible"
93+
_assert(!isnothing(rinfo), "transform is not revertible")
9494

9595
# features and metadata
9696
newfeat, newmeta = divide(newtable)
@@ -132,7 +132,7 @@ function reapply(p::ParallelTableTransform, table, cache)
132132
metas = last.(splits)
133133

134134
# check the number of rows of generated tables
135-
@assert allequal(_nrows(f) for f in feats) "parallel branches must produce the same number of rows"
135+
_assert(allequal(_nrows(f) for f in feats), "parallel branches must produce the same number of rows")
136136

137137
# table with concatenated features
138138
newfeat = tablehcat(feats)

src/transforms/remainder.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function applyfeat(transform::Remainder, feat, prep)
3838
else
3939
t = convert(eltype(X), transform.total)
4040
# make sure that the total is valid
41-
@assert all((t), S) "the sum for each row must be less than total"
41+
_assert(all((t), S), "the sum for each row must be less than total")
4242
t
4343
end
4444

src/transforms/rename.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Rename{S<:ColumnSelector} <: StatelessFeatureTransform
2020
selector::S
2121
newnames::Vector{Symbol}
2222
function Rename(selector::S, newnames) where {S<:ColumnSelector}
23-
@assert allunique(newnames) "new names must be unique"
23+
_assert(allunique(newnames), "new names must be unique")
2424
new{S}(selector, newnames)
2525
end
2626
end
@@ -36,7 +36,7 @@ function applyfeat(transform::Rename, feat, prep)
3636
cols = Tables.columns(feat)
3737
names = Tables.columnnames(cols)
3838
snames = transform.selector(names)
39-
@assert transform.newnames setdiff(names, snames) "duplicate names"
39+
_assert(transform.newnames setdiff(names, snames), "duplicate names")
4040

4141
mapnames = Dict(zip(snames, transform.newnames))
4242
newnames = [get(mapnames, nm, nm) for nm in names]

src/transforms/scale.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct Scale{S<:ColumnSelector,T} <: ColwiseFeatureTransform
3737
high::T
3838

3939
function Scale(selector::S, low::T, high::T) where {S<:ColumnSelector,T}
40-
@assert 0 low high 1 "invalid quantiles"
40+
_assert(0 low high 1, "invalid quantiles")
4141
new{S,T}(selector, low, high)
4242
end
4343
end

0 commit comments

Comments
 (0)