Skip to content

Commit

Permalink
Raise DimensionMismatch if lengths don't match (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Mar 11, 2024
1 parent 5a3aaa6 commit 4c9e559
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ run lengths.
"""
function inverse_rle(vals::AbstractVector{T}, lens::AbstractVector{<:Integer}) where T
m = length(vals)
length(lens) == m || raise_dimerror()
mlens = length(lens)
mlens == m || throw(DimensionMismatch(
"number of vals ($m) does not match the number of lens ($mlens)"))
n = sum(lens)
n >= 0 || throw(ArgumentError("lengths must be non-negative"))

Expand Down
5 changes: 4 additions & 1 deletion src/ranking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

function _check_randparams(rks, x, p)
n = length(rks)
length(x) == length(p) == n || raise_dimerror()
nx = length(x)
np = length(p)
nx == np == n || throw(
DimensionMismatch("lengths of x $nx and p $np do not match that of ranks $n"))
return n
end

Expand Down
1 change: 1 addition & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ z = [1, 1, 2, 2, 2, 3, 1, 2, 2, 3, 3, 3, 3]
@test lens == [2, 3, 1, 1, 2, 4]
@test inverse_rle(vals, lens) == z
@test_throws ArgumentError inverse_rle(vals, fill(-1, length(lens)))
@test_throws DimensionMismatch inverse_rle(vals, [1])

z = [true, true, false, false, true, false, true, true, true]
vals, lens = rle(z)
Expand Down
3 changes: 3 additions & 0 deletions test/ranking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ s = ["c", "a", "b", "d", "d", "b", "e", "d"] # s is a vector of strings ordered
@test tiedrank(s) == tiedrank(x)
@test tiedrank(x, rev = true) == tiedrank(-x)
@test tiedrank(x, lt = (x, y) -> isless(y, x)) == tiedrank(-x)


@test_throws DimensionMismatch StatsBase._check_randparams([1,2], [1,2], [1])

0 comments on commit 4c9e559

Please sign in to comment.