Skip to content

Enhance ranking code #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 1, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 43 additions & 46 deletions src/ranking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,63 @@
# The implementations here follow this wikipedia page.
#


function _check_randparams(rks, x, p)
n = length(rks)
length(x) == length(p) == n || raise_dimerror()
return n
end

# ranking helper function: calls sortperm(x) and then ranking method f!
function _rank(f!, x::AbstractArray, R::Type=Int; sortkwargs...)
rks = similar(x, R)
ord = reshape(sortperm(vec(x); sortkwargs...), size(x))
return f!(rks, x, ord)
end

# ranking helper function for arrays with missing values
function _rank(f!, x::AbstractArray{>: Missing}, R::Type=Int; sortkwargs...)
inds = findall(!ismissing, vec(x))
isempty(inds) && return missings(R, size(x))
T = nonmissingtype(eltype(x))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noticed that T is not used anymore, will remove this line

xv = Vector{T}(undef, length(inds))
@inbounds for (i, ind) in enumerate(inds)
xv[i] = x[ind]
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just do xv = view(x, inds)? If making a copy is faster, then xv = disallowmissing(view(x, inds)) as in the old code or xv = convert(AbstractArray{T}, x) is both shorter and much more efficient e.g. for CategoricalArray.

rks = missings(R, size(x))
ordv = sortperm(xv; sortkwargs...)
f!(view(rks, inds), xv, ordv)
return rks
end

# Ordinal ranking ("1234 ranking") -- use the literal order resulted from sort
function ordinalrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
n = _check_randparams(rks, x, p)

if n > 0
i = 1
while i <= n
rks[p[i]] = i
i += 1
end
function _ordinalrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
_check_randparams(rks, x, p)
@inbounds for i in eachindex(p)
rks[p[i]] = i
end

return rks
end


"""
ordinalrank(x; lt = isless, rev::Bool = false)
ordinalrank(x; sortkwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeating the list of supported arguments is more user-friendly IMO, and it doesn't take a lot of space.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've expanded lt, by and rev, because they are more commonly used, and alg and order definitions are too long.
The docstring now explicitly refers to the sort(x) keywords in the description.


Return the [ordinal ranking](https://en.wikipedia.org/wiki/Ranking#Ordinal_ranking_.28.221234.22_ranking.29)
("1234" ranking) of an array. The `lt` keyword allows providing a custom "less
than" function; use `rev=true` to reverse the sorting order.
All items in `x` are given distinct, successive ranks based on their
position in `sort(x; lt = lt, rev = rev)`.
position in `sort(x; sortkwargs...)`.
Missing values are assigned rank `missing`.
"""
ordinalrank(x::AbstractArray; lt = isless, rev::Bool = false) =
ordinalrank!(Array{Int}(undef, size(x)), x, sortperm(x; lt = lt, rev = rev))
ordinalrank(x::AbstractArray; sortkwargs...) =
_rank(_ordinalrank!, x; sortkwargs...)


# Competition ranking ("1224" ranking) -- resolve tied ranks using min
function competerank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
function _competerank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
n = _check_randparams(rks, x, p)

if n > 0
@inbounds if n > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're at it, maybe replace these while loops with for i in 2:n and so on? They are hard to read currently.

p1 = p[1]
v = x[p1]
rks[p1] = k = 1
Expand All @@ -73,7 +86,7 @@ end


"""
competerank(x; lt = isless, rev::Bool = false)
competerank(x; sortkwargs...)

Return the [standard competition ranking](http://en.wikipedia.org/wiki/Ranking#Standard_competition_ranking_.28.221224.22_ranking.29)
("1224" ranking) of an array. The `lt` keyword allows providing a custom "less
Expand All @@ -82,15 +95,15 @@ Items that compare equal are given the same rank, then a gap is left
in the rankings the size of the number of tied items - 1.
Missing values are assigned rank `missing`.
"""
competerank(x::AbstractArray; lt = isless, rev::Bool = false) =
competerank!(Array{Int}(undef, size(x)), x, sortperm(x; lt = lt, rev = rev))
competerank(x::AbstractArray; sortkwargs...) =
_rank(_competerank!, x; sortkwargs...)


# Dense ranking ("1223" ranking) -- resolve tied ranks using min
function denserank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
function _denserank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
n = _check_randparams(rks, x, p)

if n > 0
@inbounds if n > 0
p1 = p[1]
v = x[p1]
rks[p1] = k = 1
Expand All @@ -114,7 +127,7 @@ end


"""
denserank(x)
denserank(x; sortkwargs...)

Return the [dense ranking](http://en.wikipedia.org/wiki/Ranking#Dense_ranking_.28.221223.22_ranking.29)
("1223" ranking) of an array. The `lt` keyword allows providing a custom "less
Expand All @@ -123,15 +136,15 @@ compare equal receive the same ranking, and the next subsequent rank is
assigned with no gap.
Missing values are assigned rank `missing`.
"""
denserank(x::AbstractArray; lt = isless, rev::Bool = false) =
denserank!(Array{Int}(undef, size(x)), x, sortperm(x; lt = lt, rev = rev))
denserank(x::AbstractArray; sortkwargs...) =
_rank(_denserank!, x; sortkwargs...)


# Tied ranking ("1 2.5 2.5 4" ranking) -- resolve tied ranks using average
function tiedrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
function _tiedrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
n = _check_randparams(rks, x, p)

if n > 0
@inbounds if n > 0
v = x[p[1]]

s = 1 # starting index of current range
Expand Down Expand Up @@ -163,7 +176,7 @@ end

# order (aka. rank), resolving ties using the mean rank
"""
tiedrank(x)
tiedrank(x; sortkwargs...)

Return the [tied ranking](http://en.wikipedia.org/wiki/Ranking#Fractional_ranking_.28.221_2.5_2.5_4.22_ranking.29),
also called fractional or "1 2.5 2.5 4" ranking,
Expand All @@ -173,21 +186,5 @@ Items that compare equal receive the mean of the
rankings they would have been assigned under ordinal ranking.
Missing values are assigned rank `missing`.
"""
tiedrank(x::AbstractArray; lt = isless, rev::Bool = false) =
tiedrank!(Array{Float64}(undef, size(x)), x, sortperm(x; lt = lt, rev = rev))

for (f, f!, S) in zip([:ordinalrank, :competerank, :denserank, :tiedrank],
[:ordinalrank!, :competerank!, :denserank!, :tiedrank!],
[Int, Int, Int, Float64])
@eval begin
function $f(x::AbstractArray{>: Missing}; lt = isless, rev::Bool = false)
inds = findall(!ismissing, x)
isempty(inds) && return missings($S, size(x))
xv = disallowmissing(view(x, inds))
sp = sortperm(xv; lt = lt, rev = rev)
rks = missings($S, length(x))
$(f!)(view(rks, inds), xv, sp)
rks
end
end
end
tiedrank(x::AbstractArray; sortkwargs...) =
_rank(_tiedrank!, x, Float64; sortkwargs...)