Skip to content

Commit 67cee22

Browse files
committed
Fix #19: constant columns in Scale and Quantile
1 parent bf666bf commit 67cee22

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Diff for: src/distributions.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function cdf(d::EmpiricalDistribution{T}, x::T) where {T<:Real}
4141
elseif x > u
4242
return one(T)
4343
else
44-
if head == tail
45-
return head / n
44+
if l == u
45+
return tail / n
4646
else
4747
pl, pu = head / n, tail / n
4848
return (pu - pl) * (x - l) / (u - l) + pl

Diff for: src/transforms/scale.jl

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ isrevertible(::Type{Scale}) = true
2727
function colcache(transform::Scale, x)
2828
levels = (transform.low, transform.high)
2929
xl, xh = quantile(x, levels)
30+
xl == xh && ((xl, xh) = (zero(xl), one(xh)))
3031
(xl=xl, xh=xh)
3132
end
3233

Diff for: test/transforms.jl

+22
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@
219219
end
220220

221221
@testset "Scale" begin
222+
# constant column
223+
x = fill(3.0, 10)
224+
y = rand(10)
225+
t = Table(; x, y)
226+
T = MinMax()
227+
n, c = apply(T, t)
228+
@test n.x == x
229+
@test n.y != y
230+
tₒ = revert(T, n, c)
231+
@test tₒ == t
232+
222233
Random.seed!(42) # to reproduce the results
223234
x = rand(Normal(4,3), 4000)
224235
y = rand(Normal(7,5), 4000)
@@ -274,6 +285,17 @@
274285
r = revert(Quantile(), n, c)
275286
@test all(-4 .< extrema(n.z) .< 4)
276287
@test all(0 .≤ extrema(r.z) .≤ 1)
288+
289+
# constant column
290+
x = fill(3.0, 10)
291+
y = rand(10)
292+
t = Table(; x, y)
293+
T = Quantile()
294+
n, c = apply(T, t)
295+
@test maximum(abs, n.x - x) < 0.1
296+
@test n.y != y
297+
tₒ = revert(T, n, c)
298+
@test tₒ.x == t.x
277299
end
278300

279301
@testset "Functional" begin

0 commit comments

Comments
 (0)