fix(viewers): summary row as the table's last row, identifiers left out

The CSV summary row read as a floating card: rounded, a repeated "Avg"
label in every cell, distinct counts under text columns and an average
of the seed column.

- The footer follows the column grid: square edges, a 2px top rule and
  the header's solid fill. It stays at the bottom of the scroll area,
  which no longer has vertical padding, so no rows show past it; a
  short table ends on it.
- One "Average" select in the first column (or the row-number column)
  sets the aggregate for every numeric column. Cells show only the
  value, right-aligned, semibold. A column can override it from its
  menu, marked by a small label in its cell.
- Text, date and identifier columns are empty in the footer; their
  distinct counts stay in the column menu.
- Identifiers (seed, id, run, fold, index, year by name, or small
  consecutive whole numbers that repeat) are left out of the summary,
  group averages and default charts. "Treat as number" in the column
  menu brings one back (#table:num=seed).
- Markdown and notebook tables get the same footer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 10:22:05 +00:00
1 parent 43e92bef53
commit 4fc3a8b2df
11 files changed
+231 -148

No files matched your search

+42 -13
View File
@@ -81,9 +81,34 @@ export function analyzeTable(rows) {
const values = body.map((r) => r[ci] ?? "");
columns.push(makeColumn(String(header[ci] ?? "").trim() || "Column " + (ci + 1), ci, values));
}
for (const c of columns) c.identifier = isIdentifier(c);
return { header: columns.map((c) => c.name), body, columns, size: body.length };
}
// Columns that name a run rather than measure it: seed, id, run, fold, index,
// year (by name), or small consecutive whole numbers that repeat across rows
// (1..5 for five seeds). They are left out of summaries and group aggregates;
// the column menu can still treat one as a number.
const ID_NAME = /(?:^|[\s_.\-/(])(?:seed|id|ids|run|runs|run_id|fold|folds|index|idx|year|trial|trials|rep|reps|repeat|split|#|no)s?(?:$|[\s_.\-/)])|^(?:seed|id|run|fold|index|idx|year|trial|rep|repeat)s?$|[a-z]Id$/i;
export function isIdentifier(column) {
if (!column || column.type !== "number") return false;
if (ID_NAME.test(column.name.trim())) return column.decimals === 0;
if (column.decimals !== 0 || column.unit) return false;
let min = Infinity, max = -Infinity, filled = 0;
const seen = new Set();
for (const k of column.keys) {
if (k == null) continue;
if (k < 0 || !Number.isInteger(k)) return false;
filled++;
seen.add(k);
if (k < min) min = k;
if (k > max) max = k;
if (seen.size > 12) return false;
}
// Consecutive keys (1..5, 0..9) each used at least twice.
return seen.size >= 2 && filled >= 2 * seen.size && max - min + 1 === seen.size && min <= 1;
}
export function makeColumn(name, index, values, forced) {
const n = values.length;
const keys = new Array(n);
@@ -244,14 +269,12 @@ export function formatKey(column, key, fn) {
}
// The summary cell of a column over the given rows: the selected aggregate for
// numbers, the count of distinct values otherwise.
export function summarize(column, indices, fn = "mean") {
if (column.type === "number") {
const value = aggregate(column.keys, indices, fn);
return { kind: "number", fn, value, text: formatKey(column, value, fn) };
}
const distinct = distinctValues(column, indices).size;
return { kind: "distinct", value: distinct, text: distinct === 1 ? "1 distinct" : formatNumber(distinct) + " distinct" };
// measures; nothing for text, dates and identifiers (their distinct counts are
// in the column stats).
export function summarize(column, indices, fn = "mean", isMeasure = (c) => !c.identifier) {
if (column.type !== "number" || !isMeasure(column)) return { kind: "none", text: "" };
const value = aggregate(column.keys, indices, fn);
return { kind: "number", fn, value, text: formatKey(column, value, fn) };
}
/**
@@ -259,7 +282,7 @@ export function summarize(column, indices, fn = "mean") {
* numeric column aggregated with fn. The result is a table model like
* analyzeTable's, so it can be sorted, summarized, charted and exported.
*/
export function groupTable(table, indices, groupIndex, fn = "mean") {
export function groupTable(table, indices, groupIndex, fn = "mean", isMeasure = (c) => !c.identifier) {
const by = table.columns[groupIndex];
if (!by) return null;
const groups = new Map();
@@ -268,13 +291,14 @@ export function groupTable(table, indices, groupIndex, fn = "mean") {
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(i);
}
const numeric = table.columns.filter((c) => c.type === "number" && c.index !== groupIndex);
const numeric = table.columns.filter((c) => c.type === "number" && c.index !== groupIndex && isMeasure(c));
const entries = [...groups.entries()];
// Text groups keep the cell as written (first occurrence); numbers and dates are formatted.
const labels = entries.map(([key, rows]) => (key === "" ? "(empty)" : by.type === "text" ? table.body[rows[0]][groupIndex].trim() : formatKey(by, key)));
const groupColumn = makeColumn(by.name, 0, labels, { type: by.type, decimals: by.decimals, unit: by.unit });
groupColumn.keys = entries.map(([key]) => (key === "" ? null : key));
groupColumn.source = groupIndex;
groupColumn.identifier = by.type === "number";
const countColumn = makeColumn("Rows", 1, entries.map(([, rows]) => rows.length), { type: "number", decimals: 0, unit: "" });
const valueColumns = numeric.map((c, n) => {
const values = entries.map(([, rows]) => aggregate(c.keys, rows, fn));
@@ -337,7 +361,7 @@ export function toMarkdown(header, rows, numeric = []) {
// "grouped by model, average accuracy": #table:group=model&agg=mean (a single
// file) or #table-2:... (the second file of a gist). Columns are named, not
// numbered. Without the fragment the table opens as it always did.
export const DEFAULT_STATE = Object.freeze({ view: "table", query: "", sort: null, group: null, agg: "mean", ranges: {}, values: {}, sums: {}, chart: null, x: null, y: [] });
export const DEFAULT_STATE = Object.freeze({ view: "table", query: "", sort: null, group: null, agg: "mean", sum: "mean", ranges: {}, values: {}, sums: {}, numeric: [], chart: null, x: null, y: [] });
export function encodeState(state) {
const p = new URLSearchParams();
@@ -348,7 +372,9 @@ export function encodeState(state) {
if (state.group && state.agg && state.agg !== "mean") p.set("agg", state.agg);
for (const [name, [min, max]] of Object.entries(state.ranges || {})) if (min != null || max != null) p.set("range." + name, (min ?? "") + "~" + (max ?? ""));
for (const [name, list] of Object.entries(state.values || {})) for (const v of list || []) p.append("in." + name, v);
for (const [name, fn] of Object.entries(state.sums || {})) if (fn && fn !== "mean") p.set("sum." + name, fn);
if (state.sum && state.sum !== "mean") p.set("sum", state.sum);
for (const [name, fn] of Object.entries(state.sums || {})) if (fn && fn !== (state.sum || "mean")) p.set("sum." + name, fn);
if (state.numeric?.length) p.set("num", state.numeric.join("|"));
if (state.chart) p.set("chart", state.chart);
if (state.x) p.set("x", state.x);
if (state.y?.length) p.set("y", state.y.join("|"));
@@ -357,7 +383,7 @@ export function encodeState(state) {
export function decodeState(text) {
const p = new URLSearchParams(text || "");
const out = { ...DEFAULT_STATE, ranges: {}, values: {}, sums: {}, y: [] };
const out = { ...DEFAULT_STATE, ranges: {}, values: {}, sums: {}, numeric: [], y: [] };
const view = p.get("view");
if (["table", "chart", "raw"].includes(view)) out.view = view;
out.query = p.get("q") || "";
@@ -373,6 +399,9 @@ export function decodeState(text) {
} else if (key.startsWith("in.")) (out.values[key.slice(3)] ||= []).push(value);
else if (key.startsWith("sum.") && AGGREGATE_LABEL[value]) out.sums[key.slice(4)] = value;
}
const sum = p.get("sum");
if (sum && AGGREGATE_LABEL[sum]) out.sum = sum;
out.numeric = (p.get("num") || "").split("|").filter(Boolean);
const chart = p.get("chart");
if (["bar", "line", "scatter"].includes(chart)) out.chart = chart;
out.x = p.get("x") || null;