-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import range from "./range.js"; | ||
import sort from "./sort.js"; | ||
|
||
export default function rank(values, ties = "low", valueof) { | ||
if (typeof ties === "function") { | ||
valueof = ties; ties = "low"; | ||
} | ||
values = Array.from(values, valueof); | ||
const n = values.length; | ||
const r = new Float64Array(n); | ||
|
||
let last, l; | ||
const order = sort(range(n), (i) => values[i]); | ||
order.forEach((j, i) => { | ||
const value = values[j]; | ||
if (value == null || !(value <= value)) { | ||
r[j] = NaN; | ||
return; | ||
} | ||
if (last === undefined || !(value <= last)) { | ||
last = value; | ||
l = i; | ||
} | ||
r[j] = l; | ||
}); | ||
|
||
// backtrack to handle ties: low, mean, round, high, order | ||
if (ties === "order") { | ||
order.forEach((i,j) => r[i] = isNaN(r[i]) ? NaN : j); | ||
} else if (ties !== "low") { | ||
let find, replace; | ||
for (let i = n - 1; i >= 0; i--) { | ||
const j = r[order[i]]; | ||
if (i !== j && find !== j) { | ||
find = j; | ||
switch (ties) { | ||
case "mean": replace = (i + j) / 2; break; | ||
case "round": replace = (i + j) >> 1; break; | ||
case "high": replace = i; break; | ||
} | ||
} | ||
if (j === find) r[order[i]] = replace; | ||
} | ||
} | ||
|
||
return r; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import assert from "assert"; | ||
import rank from "../src/rank.js"; | ||
|
||
it("rank(numbers) returns the rank of numbers", () => { | ||
assert.deepStrictEqual(rank([1000, 10, 0]), new Float64Array([2, 1, 0])); | ||
assert.deepStrictEqual(rank([1.2, 1.1, 1.2, 1.0, 1.5, 1.2]), new Float64Array([2, 1, 2, 0, 5, 2])); | ||
}); | ||
|
||
it("rank(strings) returns the rank of letters", () => { | ||
assert.deepStrictEqual(rank([..."EDGFCBA"]), new Float64Array([4, 3, 6, 5, 2, 1, 0])); | ||
}); | ||
|
||
it("rank(dates) returns the rank of Dates", () => { | ||
assert.deepStrictEqual(rank([new Date(2000, 0, 1), new Date(2000, 0, 1), new Date(1999, 0, 1), new Date(2001, 0, 1)]), new Float64Array([1, 1, 0, 3])); | ||
}); | ||
|
||
it("rank(iterator) accepts an iterator", () => { | ||
assert.deepStrictEqual(rank(new Set(["B", "C", "A"])), new Float64Array([1, 2, 0])); | ||
assert.deepStrictEqual(rank(new Set(["B", "C", "A"]), "high"), new Float64Array([1, 2, 0])); | ||
assert.deepStrictEqual(rank({length: 3}, (_, i) => i), new Float64Array([0, 1, 2])); | ||
}); | ||
|
||
it("rank(undefineds) ranks undefined as NaN", () => { | ||
assert.deepStrictEqual(rank([1.2, 1.1, undefined, 1.0, undefined, 1.5]), new Float64Array([2, 1, NaN, 0, NaN, 3])); | ||
assert.deepStrictEqual(rank([, null, , 1.2, 1.1, undefined, 1.0, NaN, 1.5]), new Float64Array([NaN, NaN, NaN, 2, 1, NaN, 0, NaN, 3])); | ||
}); | ||
|
||
it("rank(values, valueof) accepts an accessor", () => { | ||
assert.deepStrictEqual(rank([{x: 3}, {x: 1}, {x: 2}, {x: 4}, {}], d => d.x), new Float64Array([2, 0, 1, 3, NaN])); | ||
}); | ||
|
||
it("rank(values, ties) computes the ties as expected", () => { | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c"], "low"), new Float64Array([0, 1, 1, 1, 4])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c"], "mean"), new Float64Array([0, 2, 2, 2, 4])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c"], "round"), new Float64Array([0, 2, 2, 2, 4])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c"], "high"), new Float64Array([0, 3, 3, 3, 4])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c"], "order"), new Float64Array([0, 1, 2, 3, 4])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c"], "low"), new Float64Array([0, 1, 1, 1, 1, 5])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c"], "mean"), new Float64Array([0, 2.5, 2.5, 2.5, 2.5, 5])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c"], "round"), new Float64Array([0, 2, 2, 2, 2, 5])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c"], "high"), new Float64Array([0, 4, 4, 4, 4, 5])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c"], "order"), new Float64Array([0, 1, 2, 3, 4, 5])); | ||
}); | ||
|
||
it("rank(values, ties) handles NaNs as expected", () => { | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c", null], "low"), new Float64Array([0, 1, 1, 1, 4, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c", null], "mean"), new Float64Array([0, 2, 2, 2, 4, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c", null], "round"), new Float64Array([0, 2, 2, 2, 4, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c", null], "high"), new Float64Array([0, 3, 3, 3, 4, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "c", null], "order"), new Float64Array([0, 1, 2, 3, 4, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c", null], "low"), new Float64Array([0, 1, 1, 1, 1, 5, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c", null], "mean"), new Float64Array([0, 2.5, 2.5, 2.5, 2.5, 5, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c", null], "round"), new Float64Array([0, 2, 2, 2, 2, 5, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c", null], "high"), new Float64Array([0, 4, 4, 4, 4, 5, NaN])); | ||
assert.deepStrictEqual(rank(["a", "b", "b", "b", "b", "c", null], "order"), new Float64Array([0, 1, 2, 3, 4, 5, NaN])); | ||
}); |