mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
- 'none', 'NA' and '-' are empty only in number and date columns; in text columns they are categories. - The table viewer is keyed per file, so a search or filter in one CSV doesn't carry over to the next. - A scatter chart with more than 500 text x values is refused instead of measuring every label (and spreading 100k values into Math.max). - Gist CSV rows are cached per file object instead of a 1 MB string key built on every render. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
15 KiB
JavaScript
260 lines
15 KiB
JavaScript
const { expect } = require("chai");
|
||
const { JSDOM } = require("jsdom");
|
||
const path = require("path");
|
||
const { pathToFileURL } = require("url");
|
||
|
||
const load = (name) => import(pathToFileURL(path.join(__dirname, "../public/script", name)).href);
|
||
|
||
describe("Table analysis", function () {
|
||
let T, chart, formatters;
|
||
before(async () => {
|
||
T = await load("table-analysis.js");
|
||
chart = await load("table-chart.js");
|
||
formatters = await load("formatters.js");
|
||
});
|
||
const table = (text, sep = ",") => T.analyzeTable(formatters.parseDelimited(text, sep));
|
||
const all = (t) => [...Array(t.size).keys()];
|
||
const RESULTS = "model,seed,accuracy,plausible\nBaseline,1,71.0,410\nBaseline,2,71.4,414\n+ Aug.,1,73.8,445\n+ Aug.,2,74.2,449\nOurs,1,78.7,501\nOurs,2,79.1,\n";
|
||
|
||
describe("numeric parsing", () => {
|
||
it("reads grouped thousands, percents, exponents, signs and the Unicode minus", () => {
|
||
expect(["1,234", "12%", "1e-3", "-0.5", "+3", "−2", ".5", " 7 ", "1,234.5"].map(T.parseNumber)).to.deep.equal([1234, 12, 0.001, -0.5, 3, -2, 0.5, 7, 1234.5]);
|
||
});
|
||
it("rejects text, misplaced separators and bare signs", () => {
|
||
expect(["1,2", "12,34", "abc", "-", "", "1.2.3", "0x10", "Infinity", "12 %"].map(T.parseNumber)).to.deep.equal([null, null, null, null, null, null, null, null, null]);
|
||
});
|
||
it("counts decimals as written, including exponents", () => {
|
||
expect(["78.90", "1e-3", "1.38e-3", "12", "2.5e2", "0.3"].map(T.decimalsOf)).to.deep.equal([2, 3, 5, 0, 0, 1]);
|
||
});
|
||
});
|
||
|
||
describe("type detection", () => {
|
||
it("detects numbers, dates and text, ignoring empty and NA cells", () => {
|
||
expect(T.detectType(["1", "2.5", "", "NA", "1,000"])).to.equal("number");
|
||
expect(T.detectType(["2026-03-01", "2026-03-02T10:00Z", "", "Mar 3, 2026"])).to.equal("date");
|
||
expect(T.detectType(["Baseline", "12"])).to.equal("text");
|
||
expect(T.detectType(["03/01/2026", "04/01/2026"])).to.equal("text");
|
||
expect(T.detectType(["", ""])).to.equal("text");
|
||
});
|
||
it("keeps a column's precision, unit and scientific notation", () => {
|
||
const t = table("a,b,c,d\n1,12%,1.38e-3,x\n2.25,8%,2.1e-4,y\n");
|
||
expect(t.columns.map((c) => [c.type, c.decimals, c.unit])).to.deep.equal([["number", 2, ""], ["number", 0, "%"], ["number", 5, ""], ["text", 0, ""]]);
|
||
expect(t.columns[2].scientific).to.equal(2);
|
||
expect(T.summarize(t.columns[2], all(t), "mean").text).to.equal("7.950e-4");
|
||
expect(T.summarize(t.columns[1], all(t), "mean").text).to.equal("10.0%");
|
||
});
|
||
it("parses unambiguous dates only", () => {
|
||
expect(T.parseDate("2026-03-01")).to.equal(Date.UTC(2026, 2, 1));
|
||
expect(T.parseDate("2026/3/1 10:30")).to.equal(Date.UTC(2026, 2, 1, 10, 30));
|
||
expect(T.parseDate("1 March 2026")).to.equal(Date.UTC(2026, 2, 1));
|
||
expect(T.parseDate("2026-13-01")).to.equal(null);
|
||
expect(T.parseDate("03/01/2026")).to.equal(null);
|
||
});
|
||
});
|
||
|
||
describe("aggregates", () => {
|
||
const keys = [3, null, 1, 4, 1, 5];
|
||
const rows = [0, 1, 2, 3, 4, 5];
|
||
it("skips empty cells", () => {
|
||
expect(T.aggregate(keys, rows, "count")).to.equal(5);
|
||
expect(T.aggregate(keys, rows, "sum")).to.equal(14);
|
||
expect(T.aggregate(keys, rows, "mean")).to.equal(2.8);
|
||
expect(T.aggregate(keys, rows, "min")).to.equal(1);
|
||
expect(T.aggregate(keys, rows, "max")).to.equal(5);
|
||
});
|
||
it("computes the median for odd and even counts", () => {
|
||
expect(T.aggregate(keys, rows, "median")).to.equal(3);
|
||
expect(T.aggregate([4, 1, 3, 2], [0, 1, 2, 3], "median")).to.equal(2.5);
|
||
});
|
||
it("uses the sample standard deviation and needs two values", () => {
|
||
expect(T.aggregate([2, 4, 4, 4, 5, 5, 7, 9], [0, 1, 2, 3, 4, 5, 6, 7], "std")).to.be.closeTo(2.138, 1e-3);
|
||
expect(T.aggregate([5, null], [0, 1], "std")).to.equal(null);
|
||
expect(T.aggregate([null], [0], "mean")).to.equal(null);
|
||
expect(T.aggregate([null], [0], "count")).to.equal(0);
|
||
});
|
||
it("formats a summary with the column's precision, over the given rows only", () => {
|
||
const t = table(RESULTS);
|
||
expect(T.summarize(t.columns[2], all(t), "mean").text).to.equal("74.7");
|
||
expect(T.summarize(t.columns[2], [0, 1], "mean").text).to.equal("71.2");
|
||
expect(T.summarize(t.columns[3], all(t), "mean").text).to.equal("443.8");
|
||
expect(T.summarize(t.columns[3], all(t), "count").text).to.equal("5");
|
||
expect(T.summarize(t.columns[3], all(t), "sum").text).to.equal("2,219");
|
||
// Text columns count distinct values; identifier columns stay empty.
|
||
expect(T.summarize(t.columns[0], all(t)).text).to.equal("3 distinct");
|
||
expect(T.summarize(t.columns[1], all(t), "mean").kind).to.equal("none");
|
||
expect(T.summarize(t.columns[1], all(t), "mean", () => true).text).to.equal("1.5");
|
||
});
|
||
});
|
||
|
||
describe("identifier columns", () => {
|
||
const ids = (text) => table(text).columns.map((c) => c.identifier);
|
||
it("recognizes seed, id, run, fold, index and year by name", () => {
|
||
expect(ids("seed,run_id,fold,index,year,Trial,accuracy\n1,10,0,5,2024,3,71.2\n2,11,1,6,2025,4,71.4\n")).to.deep.equal([true, true, true, true, true, true, false]);
|
||
expect(ids("modelId,seeds,n_runs\n4,1,7\n9,2,8\n")).to.deep.equal([true, true, true]);
|
||
});
|
||
it("recognizes small consecutive whole numbers that repeat across rows", () => {
|
||
expect(ids("k,v\n1,5\n2,9\n3,4\n1,7\n2,2\n3,1\n")).to.deep.equal([true, false]);
|
||
expect(ids("k\n0\n1\n0\n1\n")).to.deep.equal([true]);
|
||
});
|
||
it("leaves measures alone: decimals, percents, gaps, big ranges, negatives, unique values", () => {
|
||
expect(ids("a,b,c,d,e\n1.0,1%,1,410,-1\n2.0,2%,3,414,0\n1.0,1%,1,445,-1\n2.0,2%,3,449,0\n")).to.deep.equal([false, false, false, false, false]);
|
||
expect(ids("epochs,plausible\n10,410\n20,414\n")).to.deep.equal([false, false]);
|
||
expect(ids("seed,name\nx,y\n")).to.deep.equal([false, false]);
|
||
expect(ids("seed\n1.5\n2.5\n")).to.deep.equal([false]);
|
||
});
|
||
});
|
||
|
||
describe("sorting", () => {
|
||
it("sorts numbers numerically and keeps empty cells last in both directions", () => {
|
||
const t = table("k,v\na,10\nb,\nc,9\nd,-1\ne,1e1\n");
|
||
expect(T.sortIndices(t, all(t), 1, "asc")).to.deep.equal([3, 2, 0, 4, 1]);
|
||
expect(T.sortIndices(t, all(t), 1, "desc")).to.deep.equal([0, 4, 2, 3, 1]);
|
||
});
|
||
it("is stable: ties keep their current order", () => {
|
||
const t = table(RESULTS);
|
||
expect(T.sortIndices(t, all(t), 0, "asc")).to.deep.equal([2, 3, 0, 1, 4, 5]);
|
||
const bySeed = T.sortIndices(t, all(t), 1, "desc");
|
||
expect(bySeed).to.deep.equal([1, 3, 5, 0, 2, 4]);
|
||
expect(T.sortIndices(t, bySeed, 0, "asc")).to.deep.equal([3, 2, 1, 0, 5, 4]);
|
||
});
|
||
it("sorts text naturally and dates chronologically", () => {
|
||
const t = table("name,date\nrun 10,Mar 3 2026\nrun 2,2026-03-01\nRun 1,2026-02-28\n");
|
||
expect(T.sortIndices(t, all(t), 0, "asc")).to.deep.equal([2, 1, 0]);
|
||
expect(t.columns[1].type).to.equal("date");
|
||
expect(T.sortIndices(t, all(t), 1, "desc")).to.deep.equal([0, 1, 2]);
|
||
const d = table("date\n2026-03-10\n2026-02-28\n2026-03-01\n");
|
||
expect(T.sortIndices(d, all(d), 0, "asc")).to.deep.equal([1, 2, 0]);
|
||
});
|
||
});
|
||
|
||
describe("filtering", () => {
|
||
it("combines the search box, numeric ranges and value sets", () => {
|
||
const t = table(RESULTS);
|
||
expect(T.filterIndices(t, { query: "aug" })).to.deep.equal([2, 3]);
|
||
expect(T.filterIndices(t, { ranges: { 2: [74, null] } })).to.deep.equal([3, 4, 5]);
|
||
expect(T.filterIndices(t, { ranges: { 3: [null, 450] } })).to.deep.equal([0, 1, 2, 3]);
|
||
expect(T.filterIndices(t, { values: { 0: ["Ours", "Baseline"] }, ranges: { 1: [2, 2] } })).to.deep.equal([1, 5]);
|
||
});
|
||
});
|
||
|
||
describe("grouping", () => {
|
||
it("reproduces the mean per model over seeds", () => {
|
||
const t = table(RESULTS);
|
||
const g = T.groupTable(t, all(t), 0, "mean");
|
||
expect(g.header).to.deep.equal(["model", "Rows", "accuracy", "plausible"]);
|
||
expect(g.body).to.deep.equal([["Baseline", "2", "71.2", "412.0"], ["+ Aug.", "2", "74.0", "447.0"], ["Ours", "2", "78.9", "501.0"]]);
|
||
// "Treat as number" brings the seed back.
|
||
expect(T.groupTable(t, all(t), 0, "mean", () => true).header).to.deep.equal(["model", "Rows", "seed", "accuracy", "plausible"]);
|
||
expect(g.columns[2].agg).to.equal("mean");
|
||
expect(g.columns[2].keys[0]).to.be.closeTo(71.2, 1e-9);
|
||
});
|
||
it("groups only the filtered rows and can be sorted, counted and exported", () => {
|
||
const t = table(RESULTS);
|
||
const g = T.groupTable(t, T.filterIndices(t, { ranges: { 1: [1, 1] } }), 0, "max");
|
||
expect(g.body.map((r) => r[0])).to.deep.equal(["Baseline", "+ Aug.", "Ours"]);
|
||
const order = T.sortIndices(g, [0, 1, 2], 2, "desc");
|
||
expect(order.map((i) => g.body[i][0])).to.deep.equal(["Ours", "+ Aug.", "Baseline"]);
|
||
expect(T.toMarkdown(g.header, order.map((i) => g.body[i]), g.columns.map((c) => c.type === "number")).split("\n")[1]).to.equal("| --- | ---: | ---: | ---: |");
|
||
});
|
||
it("labels empty groups", () => {
|
||
const t = table("k,v\na,1\n,2\na,3\n");
|
||
expect(T.groupTable(t, all(t), 0, "sum").body).to.deep.equal([["a", "2", "4"], ["(empty)", "1", "2"]]);
|
||
});
|
||
it("keeps 'none', 'NA' and '-' as categories in text columns", () => {
|
||
const t = table("augmentation,acc\nnone,1\nflip,2\nnone,3\nNA,4\n-,5\n,6\n");
|
||
expect(t.columns[0].type).to.equal("text");
|
||
expect(t.columns[0].empty).to.equal(1);
|
||
expect(T.groupTable(t, all(t), 0, "sum").body.map((r) => r[0])).to.deep.equal(["none", "flip", "NA", "-", "(empty)"]);
|
||
// In a number column they still mean "no value".
|
||
expect(table("v\n1\nNA\n-\n3\n").columns[0]).to.include({ type: "number", empty: 2 });
|
||
});
|
||
});
|
||
|
||
describe("export", () => {
|
||
it("quotes CSV cells only when needed and escapes Markdown pipes", () => {
|
||
expect(T.toDelimited(["a", "b"], [["x,y", 'say "hi"'], ["plain", ""]])).to.equal('a,b\n"x,y","say ""hi"""\nplain,\n');
|
||
expect(T.toMarkdown(["a|b"], [["c|d"]])).to.equal("| a\\|b |\n| --- |\n| c\\|d |\n");
|
||
});
|
||
});
|
||
|
||
describe("shareable state", () => {
|
||
it("round-trips through the fragment and stays empty by default", () => {
|
||
expect(T.writeHashState("table", T.DEFAULT_STATE)).to.equal("");
|
||
const state = { ...T.DEFAULT_STATE, view: "chart", group: "model", agg: "median", sort: { column: "accuracy", dir: "desc" }, values: { model: ["Ours", "+ Aug."] }, ranges: { accuracy: [70, null] }, sums: { accuracy: "std" }, chart: "bar", y: ["accuracy", "plausible"] };
|
||
const hash = T.writeHashState("table-2", state);
|
||
expect(hash).to.match(/^#table-2:view=chart&sort=accuracy:desc&group=model&agg=median/);
|
||
const back = T.readHashState(hash, "table-2");
|
||
expect(back).to.deep.include({ view: "chart", group: "model", agg: "median", chart: "bar" });
|
||
expect(back.sort).to.deep.equal({ column: "accuracy", dir: "desc" });
|
||
expect(back.values).to.deep.equal({ model: ["Ours", "+ Aug."] });
|
||
expect(back.ranges).to.deep.equal({ accuracy: [70, null] });
|
||
expect(back.y).to.deep.equal(["accuracy", "plausible"]);
|
||
expect(T.readHashState(hash, "table")).to.equal(null);
|
||
expect(T.readHashState("#L12", "table")).to.equal(null);
|
||
expect(T.isTableHash("#L12")).to.equal(false);
|
||
});
|
||
});
|
||
|
||
describe("column stats and charts", () => {
|
||
it("summarizes a column with a histogram", () => {
|
||
const t = table(RESULTS);
|
||
const s = T.columnStats(t.columns[3], all(t), 4);
|
||
expect(s).to.include({ type: "number", count: 5, empty: 1, distinct: 5, min: 410, max: 501 });
|
||
expect(s.histogram.map((b) => b.count)).to.deep.equal([2, 2, 0, 1]);
|
||
expect(T.columnStats(t.columns[0], all(t)).top[0]).to.deep.equal(["+ Aug.", 2]);
|
||
});
|
||
it("defaults to a bar of the first measure by the first text column, skipping seeds", () => {
|
||
const t = table(RESULTS);
|
||
expect(chart.defaultChart(t, false)).to.deep.equal({ type: "bar", x: "model", y: ["accuracy"] });
|
||
const g = T.groupTable(t, all(t), 0, "mean");
|
||
expect(chart.defaultChart(g, true)).to.deep.equal({ type: "bar", x: "model", y: ["accuracy"] });
|
||
});
|
||
it("averages repeated categories in a bar chart and says so", () => {
|
||
const t = table(RESULTS);
|
||
const c = chart.buildChart(t, all(t), { type: "bar", x: "model", y: ["accuracy"], agg: "mean" }, { width: 600 });
|
||
expect(c.categories.map((x) => x.label)).to.deep.equal(["Baseline", "+ Aug.", "Ours"]);
|
||
expect(c.categories[2].tip.rows[0].value).to.equal("78.9");
|
||
expect(c.notes[0]).to.equal("Average per model over 6 rows.");
|
||
expect(c.marks.every((m) => m.h <= 24)).to.equal(true);
|
||
});
|
||
it("draws lines on a date axis in order and scatter points with their rows", () => {
|
||
const t = table("date\tv\n2026-03-03\t3\n2026-03-01\t1\n2026-03-02\t2\n", "\t");
|
||
const line = chart.buildChart(t, all(t), { type: "line", x: "date", y: ["v"] }, { width: 600 });
|
||
expect(line.points.map((p) => p.title)).to.deep.equal(["date 2026-03-01", "date 2026-03-02", "date 2026-03-03"]);
|
||
expect(line.series[0].path.startsWith("M")).to.equal(true);
|
||
const scatter = chart.buildChart(t, all(t), { type: "scatter", x: "v", y: ["v"] }, { width: 600 });
|
||
expect(scatter.series[0].dots).to.have.length(3);
|
||
// An ID-like text x column is refused instead of measuring every label.
|
||
const ids = table("id,v\n" + Array.from({ length: 200000 }, (_, i) => `row-${i},${i % 7}`).join("\n"));
|
||
const many = chart.buildChart(ids, all(ids), { type: "scatter", x: "id", y: ["v"] }, { width: 600 });
|
||
expect(many.empty).to.match(/200,000 different values/);
|
||
});
|
||
it("explains an empty chart", () => {
|
||
const t = table(RESULTS);
|
||
expect(chart.buildChart(t, [], { type: "bar", x: "model", y: ["accuracy"] }, { width: 600 }).empty).to.match(/No rows/);
|
||
expect(chart.buildChart(t, all(t), { type: "bar", x: "model", y: [] }, { width: 600 }).empty).to.match(/numeric/);
|
||
});
|
||
it("picks round ticks", () => {
|
||
expect(chart.niceTicks(0, 79.1, 5).ticks).to.deep.equal([0, 20, 40, 60, 80]);
|
||
expect(chart.tickLabel(25000, 5000)).to.equal("25K");
|
||
expect(chart.tickLabel(0.775, 0.025)).to.equal("0.775");
|
||
});
|
||
});
|
||
|
||
describe("Markdown and notebook tables", () => {
|
||
let dom;
|
||
before(() => { dom = new JSDOM("<!doctype html><body></body>"); global.window = dom.window; });
|
||
after(() => { delete global.window; dom.window.close(); });
|
||
it("reads redaction chips and slots back as XXXX-n and the pandas header rows", async () => {
|
||
const enhance = await load("table-enhance.js");
|
||
const doc = dom.window.document;
|
||
doc.body.innerHTML = '<table class="dataframe"><thead><tr><th></th><th>acc</th></tr><tr><th>model</th><th></th></tr></thead><tbody>'
|
||
+ '<tr><th>Ours</th><td>78.9</td></tr><tr><th><mark class="redact-chip" data-term="XXXX-2"><i>2</i></mark></th><td>71.2</td></tr></tbody></table>';
|
||
const data = enhance.tableData(doc.querySelector("table"));
|
||
expect(data.rows).to.deep.equal([["model", "acc"], ["Ours", "78.9"], ["XXXX-2", "71.2"]]);
|
||
doc.body.innerHTML = '<table><tr><td colspan="2">x</td></tr><tr><td>1</td><td>2</td></tr></table>';
|
||
expect(enhance.tableData(doc.querySelector("table"))).to.equal(null);
|
||
});
|
||
});
|
||
});
|