Skip to content

Commit

Permalink
add BigInt support to the normalize transform
Browse files Browse the repository at this point in the history
closes #2154
  • Loading branch information
Fil committed Aug 28, 2024
1 parent 0032c11 commit 8e16072
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function maybeTypedMap(data, f, type) {
return map(data, isNumberType(type) ? (d, i) => coerceNumber(f(d, i)) : f, type); // allow conversion from BigInt
}

function maybeTypedArrayify(data, type) {
export function maybeTypedArrayify(data, type) {
return type === undefined
? arrayify(data) // preserve undefined type
: isArrowVector(data)
Expand Down
5 changes: 3 additions & 2 deletions src/transforms/normalize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {extent, deviation, max, mean, median, min, sum} from "d3";
import {defined} from "../defined.js";
import {percentile, taker} from "../options.js";
import {maybeTypedArrayify, percentile, taker} from "../options.js";
import {mapX, mapY} from "./map.js";

export function normalizeX(basis, options) {
Expand Down Expand Up @@ -43,7 +43,8 @@ export function normalize(basis) {
function normalizeBasis(basis) {
return {
mapIndex(I, S, T) {
const b = +basis(I, S);
S = maybeTypedArrayify(S, Float64Array);
const b = basis(I, S);
for (const i of I) {
T[i] = S[i] === null ? NaN : S[i] / b;
}
Expand Down
117 changes: 117 additions & 0 deletions test/output/bigintNormalize.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions test/output/bigintNormalizeMean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions test/plots/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,28 @@ export async function bigintOrdinal() {
export async function bigintStack() {
return Plot.barY(integers, {x: (d, i) => i % 5, y: "big1"}).plot();
}

async function olympiansByWeight() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return d3
.bin()(olympians.map((d) => d.height))
.map((bin) => ({weightclass: bin.x0, count: BigInt(bin.length)}));
}

export async function bigintNormalize() {
return Plot.rectY(
d3.sort(await olympiansByWeight(), (d) => -d.count),
Plot.normalizeY({x: "weightclass", y: "count"})
).plot();
}

export async function bigintNormalizeMean() {
return Plot.plot({
x: {interval: 0.05},
y: {label: "relative to mean"},
marks: [
Plot.barY(await olympiansByWeight(), Plot.normalizeY("mean", {x: "weightclass", y: "count"})),
Plot.ruleY([1], {stroke: "red"})
]
});
}
8 changes: 8 additions & 0 deletions test/transforms/normalize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ it("Plot.normalize first", () => {
testNormalize([1, 2, 4, 5], "first", [1, 2, 4, 5]);
});

it("Plot.normalize first BigInt", () => {
testNormalize([1n, 2n, 4n, 5n], "first", [1, 2, 4, 5]);
});

it("Plot.normalize last", () => {
testNormalize([1, 2, 4, 5], "last", [0.2, 0.4, 0.8, 1]);
});
Expand All @@ -17,6 +21,10 @@ it("Plot.normalize median", () => {
testNormalize([1, 2, 6, 6], "median", [0.25, 0.5, 1.5, 1.5]);
});

it("Plot.normalize median BigInt", () => {
testNormalize([1n, 2n, 6n, 6n], "median", [0.25, 0.5, 1.5, 1.5]);
});

it("Plot.normalize min", () => {
testNormalize([10, 6, 2], "min", [5, 3, 1]);
});
Expand Down

0 comments on commit 8e16072

Please sign in to comment.