lawsynth/apps/studio/tests/data-prep.test.ts
caelum0x 945f20fae9 feat(product): data prep, ensemble uncertainty, model monitoring across surfaces
Wave 9 — data quality, trust & monitoring:
CLI — lawsynth prep (smooth/resample/detrend/trim/drop-constant via preprocess)
moves a noisy model WEAK(R2=0.39)->GOOD(R2=0.95); lawsynth monitor (model-based
anomaly detection, robust MAD control limits) flags an injected shock as DRIFT at
the right time; forecast --confidence emits deterministic residual-bootstrap bands.

SDK — Study.prepare (cleaned Study), discover_ensemble -> Ensemble (per-term
selection-frequency + coefficient mean/std = robust vs unstable terms) with
forecast bands, Study.monitor -> MonitorReport. Three runnable examples.

Studio — Data Prep + Monitor screens (now 11 screens): interactive prep with
raw-vs-prepared overlay that promotes to the working dataset, and observed-vs-
predicted residual monitoring with a threshold + in-control/watch/drift verdict.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-17 23:33:03 +03:00

43 lines
2.3 KiB
TypeScript

import { dataPrepModel, defaultDataPrepConfig, prepareDataset } from "../src/screens/index.js";
import type { TrajectoryInput } from "@lawsynth/chart-core";
import { equal, world } from "./support.js";
function ramp(): TrajectoryInput {
const times = [0, 1, 2, 3, 4, 5];
// Two columns: a noisy ramp and a spike, so smoothing + detrend have an effect.
const values = times.map((t) => [t + (t % 2 === 0 ? 0.5 : -0.5), Math.sin(t)]);
return { variables: ["prey", "predator"], times, values };
}
export async function dataPrepTests(): Promise<void> {
// prepareDataset applies the requested stages in order and reports them.
const result = prepareDataset(ramp(), { smoothingWindow: 3, resampleDt: 0, detrend: true, trim: 1 });
equal(result.rowsIn, 6);
equal(result.rowsOut, 4, "trim drops one row from each end"); // 6 - 2 = 4
equal(result.opsApplied.length, 3, "trim + smooth + detrend ran, resample skipped");
// Detrend removes the linear trend => prepared 'prey' column sums to ~0.
const preyIndex = result.prepared.variables.indexOf("prey");
const sum = result.prepared.values.reduce((acc, row) => acc + (row[preyIndex] ?? 0), 0);
equal(Math.abs(sum) < 1e-6, true, "detrended column is mean-centered");
// Identity config (window 1, no resample/detrend/trim) leaves rows unchanged.
const identity = prepareDataset(ramp(), { smoothingWindow: 1, resampleDt: 0, detrend: false, trim: 0 });
equal(identity.opsApplied.length, 0);
equal(identity.rowsOut, identity.rowsIn);
// The screen model overlays raw vs prepared per column (2 series + legend each).
const model = dataPrepModel({ world, initialState: { prey: 1, predator: 1 }, config: defaultDataPrepConfig(), trajectory: ramp() });
equal(model.id, "data-prep");
const charts = model.sections.filter((section) => section.kind === "chart");
equal(charts.length, 2, "one overlay chart per column");
const first = charts[0];
if (first?.kind === "chart") {
equal(first.chart.series.length, 2);
equal(first.legend?.length, 2);
}
// Apply/reset actions are present so the loop can be closed from the UI.
const actions = model.sections.find((section) => section.kind === "actions");
equal(actions?.kind, "actions");
if (actions?.kind === "actions") equal(actions.buttons.some((button) => button.id === "prep:apply"), true);
}