import { useEffect, useState } from "react"; import { Stack, Group, Text, Title, Divider, Table, Badge, Loader, Center, SimpleGrid, } from "@mantine/core"; import { StudyReport as Report, CorrelationEntry, HistoryObjective, ImportanceObjective, SliceObjective, getStudyReport, } from "../api"; interface Props { studyId: string; /** Bumps when trials count grows so we re-fetch. */ refreshTick: number; /** Hide the analysis panel until at least this many trials have completed. */ minTrials?: number; nCompleted: number; } export function StudyReport({ studyId, refreshTick, minTrials = 3, nCompleted }: Props) { const [report, setReport] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { if (nCompleted < minTrials) { setReport(null); return; } setLoading(true); getStudyReport(studyId) .then(setReport) .catch(() => setReport(null)) .finally(() => setLoading(false)); }, [studyId, refreshTick, nCompleted, minTrials]); if (nCompleted < minTrials) { return ( Analysis (parameter importance, history, slice plots, correlations) will appear after {minTrials} trials complete. Currently: {nCompleted}. ); } return ( {loading && !report && (
)} {report && ( <> {report.importances.length > 0 && ( )} {report.history.length > 0 && ( )} {report.correlations.length > 0 && ( )} {Object.keys(report.slices).length > 0 && ( )} )}
); } // ── Parameter importance ─────────────────────────────────────────────── function ImportanceSection({ importances, }: { importances: ImportanceObjective[]; }) { return ( Parameter importance (fANOVA) Fraction of variance in each objective explained by each parameter. Higher = the optimization mostly turns on that knob. {importances.map((imp, i) => ( ))} ); } function ImportanceBars({ imp }: { imp: ImportanceObjective }) { if (imp.scores.length === 0) { return ( {imp.objective} ({imp.direction}) {imp.error ?? "Not enough data to compute importance yet."} ); } const max = Math.max(...imp.scores.map((s) => s.score), 0.001); return ( {imp.objective} ({imp.direction}) {imp.scores.map((s) => { const w = (s.score / max) * 100; return ( {s.param}
{(s.score * 100).toFixed(1)}% ); })} ); } // ── Best-so-far history ───────────────────────────────────────────────── function HistorySection({ histories }: { histories: HistoryObjective[] }) { return ( Optimization history Best-so-far value per trial. A flat line = the search has converged (or got stuck). {histories.map((h, i) => ( ))} ); } function HistoryChart({ h }: { h: HistoryObjective }) { const W = 360; const H = 140; const M = { l: 50, r: 8, t: 6, b: 22 }; if (h.points.length === 0) return null; const xs = h.points.map((p) => p.trial); const ysAll = h.points.map((p) => p.value); const ysBest = h.points.map((p) => p.best); const xMin = Math.min(...xs); const xMax = Math.max(...xs); const yMin = Math.min(...ysAll, ...ysBest); const yMax = Math.max(...ysAll, ...ysBest); const xR = Math.max(1, xMax - xMin); const yR = Math.max(1e-12, yMax - yMin); const px = (x: number) => M.l + ((x - xMin) / xR) * (W - M.l - M.r); const py = (y: number) => M.t + (1 - (y - yMin) / yR) * (H - M.t - M.b); const allPoly = h.points.map((p) => `${px(p.trial)},${py(p.value)}`).join(" "); const bestPoly = h.points.map((p) => `${px(p.trial)},${py(p.best)}`).join(" "); return ( {h.objective} ({h.direction}) {/* Axes */} {/* All trial values (light) */} {h.points.map((p, i) => ( ))} {/* Best so far */} {/* Y range labels */} {fmt(yMax)} {fmt(yMin)} {xMin} trial {xMax} ); } // ── Correlations ──────────────────────────────────────────────────────── function CorrelationSection({ correlations, }: { correlations: CorrelationEntry[]; }) { // Group by objective for a compact wide layout const byObj: Record = {}; for (const c of correlations) { (byObj[c.objective] ??= []).push(c); } return ( Linear correlations (Pearson r) Sign and magnitude of the linear relationship between each parameter and each objective. Complements (and sometimes contradicts) the importance scores: importance is non-linear, correlation is linear. Parameter {Object.keys(byObj).map((o) => ( {o} ))} {paramOrder(correlations).map((p) => ( {p} {Object.keys(byObj).map((o) => { const c = byObj[o].find((x) => x.param === p); if (!c) return —; return ( ); })} ))}
); } function CorrCell({ r, n }: { r: number; n: number }) { const abs = Math.abs(r); const color = r >= 0 ? "blue" : "red"; const variant = abs >= 0.7 ? "filled" : abs >= 0.3 ? "light" : "outline"; return ( {(r >= 0 ? "+" : "") + r.toFixed(3)} n={n} ); } function paramOrder(correlations: CorrelationEntry[]): string[] { const seen: string[] = []; for (const c of correlations) if (!seen.includes(c.param)) seen.push(c.param); return seen; } // ── Slice plots ───────────────────────────────────────────────────────── function SliceSection({ slices, }: { slices: Record; }) { const params = Object.keys(slices); return ( Slice plots How each objective varies with each individual parameter, holding the others at whatever values the sampler chose. A clear trend = strong sensitivity; scatter = the parameter does not matter on its own. {params.map((p) => ( {p} {slices[p].map((slice, i) => ( ))} ))} ); } function SliceChart({ param, slice, }: { param: string; slice: SliceObjective; }) { const W = 360; const H = 130; const M = { l: 50, r: 8, t: 6, b: 22 }; if (slice.points.length === 0) return null; const xs = slice.points.map((p) => p.x); const ys = slice.points.map((p) => p.y); const xMin = Math.min(...xs); const xMax = Math.max(...xs); const yMin = Math.min(...ys); const yMax = Math.max(...ys); const xR = Math.max(1e-12, xMax - xMin); const yR = Math.max(1e-12, yMax - yMin); const px = (x: number) => M.l + ((x - xMin) / xR) * (W - M.l - M.r); const py = (y: number) => M.t + (1 - (y - yMin) / yR) * (H - M.t - M.b); return ( {slice.objective} ({slice.direction}) {slice.points.map((p, i) => ( ))} {fmt(yMax)} {fmt(yMin)} {fmt(xMin)} {fmt(xMax)} ({param}) ); } // ── helpers ───────────────────────────────────────────────────────────── function fmt(v: number): string { if (!isFinite(v)) return String(v); const a = Math.abs(v); if (a !== 0 && (a < 0.01 || a >= 10000)) return v.toExponential(2); return Number(v.toPrecision(4)).toString(); }