mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 09:03:37 +01:00
fix(tables): keep text placeholders as values, reset per file, bound scatter labels
- '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>
This commit is contained in:
1 parent
37a01257b3
commit
68239afbbb
6 files changed
+44
-14
No files matched your search
@@ -68,7 +68,7 @@
|
||||
<gist-file v-if="gistKind(index) === 'markdown' && gistView(index) === 'rendered'" :file="file" :reader="true" :id-prefix="gistFileId(index) + '-'" @outline="setFileOutline(index, $event)"></gist-file>
|
||||
<gist-file v-else-if="gistKind(index) === 'code' || (gistHasRendered(index) && gistView(index) === 'raw')" :file="gistSourceFile(index)" :reader="true"></gist-file>
|
||||
<div class="notebook-body" v-else-if="gistKind(index) === 'notebook'"><notebook class="markdown-body" :content="file.content"></notebook></div>
|
||||
<data-table v-else-if="gistKind(index) === 'csv'" :rows="gistCsvRows(file)" :name="file?.filename" :state-key="'table-' + (index + 1)" :view="gistView(index) === 'chart' ? 'chart' : 'table'"></data-table>
|
||||
<data-table v-else-if="gistKind(index) === 'csv'" :key="file?.filename" :rows="gistCsvRows(file)" :name="file?.filename" :state-key="'table-' + (index + 1)" :view="gistView(index) === 'chart' ? 'chart' : 'table'"></data-table>
|
||||
<div class="media-stage" v-else-if="gistKind(index) === 'image' || gistKind(index) === 'svg'"><img class="image-content" :src="safeUrl(gistMediaUrl(file))" :alt="file?.filename"></div>
|
||||
<div class="media-stage" v-else-if="gistKind(index) === 'video'"><video class="video-content" controls preload="metadata" playsinline :src="safeUrl(gistMediaUrl(file))"></video></div>
|
||||
<div class="media-stage is-audio" v-else-if="gistKind(index) === 'audio'"><audio controls preload="metadata" :src="safeUrl(gistMediaUrl(file))"></audio></div>
|
||||
|
||||
@@ -94,7 +94,7 @@
|
||||
<span class="goto-hint" :class="{ 'is-error': gotoError }">{{ gotoError || 'Line, or line:column' }}</span>
|
||||
</form>
|
||||
</header>
|
||||
<data-table v-if="isCsv() && csvView !== 'raw'" :rows="csvRows()" :name="paths?.[paths.length - 1]" :state-key="preview ? null : 'table'" :view="csvView"></data-table>
|
||||
<data-table v-if="isCsv() && csvView !== 'raw'" :key="paths?.join('/')" :rows="csvRows()" :name="paths?.[paths.length - 1]" :state-key="preview ? null : 'table'" :view="csvView"></data-table>
|
||||
<div class="file-card-body" v-if="type != 'html' && !(isCsv() && csvView !== 'raw')" v-code-editor="{ content: content, options: aceOption }"></div>
|
||||
<div class="md-find-bar" v-if="type == 'html' && mdFindOpen" role="search">
|
||||
<input type="search" class="md-find-input" placeholder="Find on this page" aria-label="Find on this page" :value="mdFindQuery" @input="mdFindQuery = $event.target.value; runMdFind()" @keydown.enter.prevent="$event.shiftKey ? stepMdFind(-1) : stepMdFind(1)" @keydown.esc.prevent="toggleMdFind(false)">
|
||||
|
||||
@@ -113,11 +113,17 @@ export function setupGistPage(state) {
|
||||
state.gistMediaUrl = (file) => blobUrl(file, false);
|
||||
state.gistRawUrl = (file) => blobUrl(file, true);
|
||||
// Parsed once per file: the table viewer re-analyzes only when its rows change.
|
||||
const csvCache = new Map();
|
||||
// Keyed by the file object: no 1 MB key string per render, and a reloaded
|
||||
// gist's old files are released with it.
|
||||
const csvCache = new WeakMap();
|
||||
state.gistCsvRows = (file) => {
|
||||
const key = (file?.filename || "") + "\0" + (file?.content || "");
|
||||
if (!csvCache.has(key)) csvCache.set(key, parseDelimited(file?.content || "", extension(file?.filename) === "tsv" ? "\t" : ","));
|
||||
return csvCache.get(key);
|
||||
if (!file) return [];
|
||||
let entry = csvCache.get(file);
|
||||
if (!entry || entry.content !== file.content || entry.filename !== file.filename) {
|
||||
entry = { content: file.content, filename: file.filename, rows: parseDelimited(file.content || "", extension(file.filename) === "tsv" ? "\t" : ",") };
|
||||
csvCache.set(file, entry);
|
||||
}
|
||||
return entry.rows;
|
||||
};
|
||||
state.gistCopied = null;
|
||||
state.copyGistFile = async (index) => {
|
||||
|
||||
@@ -11,9 +11,12 @@ export const AGGREGATE_LABEL = Object.fromEntries(AGGREGATES);
|
||||
const AGGREGATE_SHORT = { mean: "Avg", sum: "Sum", median: "Median", min: "Min", max: "Max", count: "Count", std: "Std dev" };
|
||||
export const aggregateShort = (fn) => AGGREGATE_SHORT[fn] || fn;
|
||||
|
||||
// Cells that mean "no value" in exported data (pandas, R, spreadsheets).
|
||||
const MISSING = /^(?:|na|n\/a|nan|null|none|-|—|–)$/i;
|
||||
export const isMissing = (raw) => raw == null || MISSING.test(String(raw).trim());
|
||||
// Cells that mean "no value" in exported numbers and dates (pandas, R,
|
||||
// spreadsheets). In a text column they are real categories ("none" as an
|
||||
// augmentation, a model called "None"), so there only a blank cell is empty.
|
||||
const PLACEHOLDER = /^(?:na|n\/a|nan|null|none|-|—|–)$/i;
|
||||
export const isBlank = (raw) => raw == null || String(raw).trim() === "";
|
||||
export const isMissing = (raw) => isBlank(raw) || PLACEHOLDER.test(String(raw).trim());
|
||||
|
||||
// 1,234 · -0.5 · +3 · 12% · 1e-3 · −2 (Unicode minus) · .5 — one match gives
|
||||
// the value, the decimals as written and the percent sign.
|
||||
@@ -140,7 +143,11 @@ export function makeColumn(name, index, values, forced) {
|
||||
}
|
||||
if (!type || type === "text") {
|
||||
type = "text";
|
||||
for (let i = 0; i < n; i++) keys[i] = isMissing(values[i]) ? null : String(values[i]).trim();
|
||||
empty = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
keys[i] = isBlank(values[i]) ? null : String(values[i]).trim();
|
||||
if (keys[i] === null) empty++;
|
||||
}
|
||||
}
|
||||
return {
|
||||
name, index, type, keys, empty,
|
||||
|
||||
@@ -9,7 +9,8 @@ export const SERIES_LIGHT = ["#2a78d6", "#eb6834", "#1baf7a", "#eda100"];
|
||||
export const SERIES_DARK = ["#3987e5", "#d95926", "#199e70", "#c98500"];
|
||||
export const MAX_SERIES = { bar: 4, line: 4, scatter: 3 };
|
||||
export const MAX_BARS = 40;
|
||||
export const MAX_POINTS = 2000;
|
||||
export const MAX_CATEGORIES = 500;
|
||||
const MAX_POINTS = 2000;
|
||||
|
||||
const isIdLike = (column) => column.type === "number" && !!column.identifier;
|
||||
|
||||
@@ -192,6 +193,9 @@ function scatterChart(table, rows, x, ys, base) {
|
||||
// Text on the x axis: one position per value in first-seen order.
|
||||
const order = new Map();
|
||||
points.forEach((p) => { if (!order.has(p.label)) order.set(p.label, order.size); p.xv = order.get(p.label); });
|
||||
// An ID-like column has a label per row: no readable axis, and measuring
|
||||
// every label would stall the tab.
|
||||
if (order.size > MAX_CATEGORIES) return { ...base, empty: `The x column has ${order.size.toLocaleString("en-US")} different values; choose a number or date column, or group the rows first.` };
|
||||
base.categoryLabels = [...order.keys()];
|
||||
}
|
||||
points = thin(points, base);
|
||||
@@ -210,7 +214,7 @@ function xyChart(points, x, ys, base, continuous, kind) {
|
||||
const values = points.flatMap((p) => p.values).filter((v) => v != null);
|
||||
if (!values.length) return { ...base, empty: "The selected columns have no values in these rows." };
|
||||
const narrow = base.width < 480;
|
||||
const yScale = niceTicks(Math.min(...values), Math.max(...values), narrow ? 4 : 5);
|
||||
const yScale = niceTicks(values.reduce((a, v) => Math.min(a, v), Infinity), values.reduce((a, v) => Math.max(a, v), -Infinity), narrow ? 4 : 5);
|
||||
const unit = ys.length === 1 ? ys[0].unit : "";
|
||||
const yLabels = yScale.ticks.map((v) => tickLabel(v, yScale.step, unit));
|
||||
const left = Math.max(...yLabels.map((l) => textWidth(l))) + 14;
|
||||
@@ -230,7 +234,8 @@ function xyChart(points, x, ys, base, continuous, kind) {
|
||||
sxv = (i) => plot.x + inset + i * stepW;
|
||||
if (!cats) points.forEach((p, i) => { p.xv = i; });
|
||||
const labels = cats || points.map((p) => p.label);
|
||||
const maxLabels = Math.max(2, Math.floor(plot.w / (Math.max(...labels.map((l) => textWidth(l))) + 12)));
|
||||
const widest = labels.reduce((w, l) => Math.max(w, textWidth(l)), 0);
|
||||
const maxLabels = Math.max(2, Math.floor(plot.w / (widest + 12)));
|
||||
const every = Math.ceil(labels.length / maxLabels);
|
||||
xTicks = labels.map((label, i) => ({ x: sxv(i), label, show: i % every === 0 })).filter((t) => t.show);
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ describe("Table analysis", function () {
|
||||
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", (c) => true).header).to.deep.equal(["model", "Rows", "seed", "accuracy", "plausible"]);
|
||||
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);
|
||||
});
|
||||
@@ -160,6 +160,14 @@ describe("Table analysis", function () {
|
||||
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", () => {
|
||||
@@ -216,6 +224,10 @@ describe("Table analysis", function () {
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user