Files
solon/scripts/statistics-quality.ts
CatoandClaude 80113fc0cd statistics: every figure is checked against its own series before it is shown (#632)
src/lib/datakit estimates how likely a published figure is a publisher's
mistake (a zero in a series that never nears zero, a spike that returns, an
unconfirmed break) and is written without app imports so it can move to the
shared kits. The statistics screening holds back what is almost surely a
slip and lists it with the reason; what is only unusual is shown and listed.


Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-28 03:56:40 +01:00

59 lines
2.2 KiB
TypeScript

/**
* Is every statistic's series fit to be read? Prints, per statistic and place,
* what each series covers and anything irregular in it (src/lib/statistics/quality.ts).
*
* pnpm run statistics:quality report; always exits 0
* pnpm run statistics:quality --strict exit 1 when any series has a gap or is stale
*/
import { placesConfig } from "@/lib/config/places";
import { db } from "@/lib/db/client";
import { statisticMetrics } from "@/lib/statistics/present";
import { assessSeries } from "@/lib/statistics/quality";
import { loadSeries } from "@/lib/statistics/read";
import { checkSeries, screenSeries } from "@/lib/statistics/sanity";
const strict = process.argv.includes("--strict");
async function main(): Promise<number> {
const thisYear = new Date().getUTCFullYear();
let bad = 0;
for (const metric of statisticMetrics(placesConfig)) {
const screened = screenSeries(
metric.plausible,
await loadSeries(db, placesConfig, metric.key, "en"),
{ fixedByLaw: metric.statistic?.fixedByLaw === true },
);
const all = screened.series;
console.log(`\n${metric.key} (${all.length} place${all.length === 1 ? "" : "s"})`);
if (all.length === 0) {
console.log(" no figures loaded");
continue;
}
for (const series of all) {
const q = assessSeries(series, thisYear);
const notes = q.notes.map((n) => `${n.kind} ${n.years.join(",")}`).join("; ");
console.log(
` ${series.code}: ${q.count} figures ${q.from}-${q.to}${notes ? ` — ${notes}` : ""}`,
);
if (q.notes.some((n) => n.kind === "gap" || n.kind === "stale")) bad += 1;
}
// What the chart of this statistic would carry a warning about, or leave out.
for (const f of [
...screened.findings,
...checkSeries(all, { fixedByLaw: metric.statistic?.fixedByLaw === true }),
]) {
console.log(` ! ${f.severity} ${f.kind}: ${f.places.join(", ")} ${f.years.join(",")}`);
if (f.severity === "withheld") bad += 1;
}
}
console.log(`\n${bad} series with a gap, stale, or a figure left out`);
return strict && bad > 0 ? 1 : 0;
}
main()
.then((code) => process.exit(code))
.catch((error: unknown) => {
console.error(error);
process.exit(2);
});