Spaces:
Sleeping
Sleeping
Download frontend/src/components/OptimizePanel.tsx from ilyaRozhkovA/LAMPSUI: direct link, hf CLI and curl.
- Browser
- Download file 30.8 kB
-
https://huggingface.co/spaces/ilyaRozhkovA/LAMPSUI/resolve/main/frontend/src/components/OptimizePanel.tsx
- Command line
-
hf download hf://spaces/ilyaRozhkovA/LAMPSUI/frontend/src/components/OptimizePanel.tsx
-
curl -L -o OptimizePanel.tsx https://huggingface.co/spaces/ilyaRozhkovA/LAMPSUI/resolve/main/frontend/src/components/OptimizePanel.tsx
30.8 kB
| import { useEffect, useMemo, useState } from "react"; | |
| import { | |
| Stack, | |
| Group, | |
| Title, | |
| Text, | |
| Button, | |
| TextInput, | |
| NumberInput, | |
| Select, | |
| ActionIcon, | |
| Badge, | |
| Table, | |
| Alert, | |
| Code, | |
| Divider, | |
| Tooltip, | |
| } from "@mantine/core"; | |
| import { | |
| IconTrash, | |
| IconPlus, | |
| IconPlayerPlay, | |
| IconPlayerStop, | |
| IconInfoCircle, | |
| } from "@tabler/icons-react"; | |
| import { StudyReport } from "./StudyReport"; | |
| import { notifications } from "@mantine/notifications"; | |
| import { | |
| listStudies, | |
| startStudy, | |
| stopStudy, | |
| StudyState, | |
| ParamSpec, | |
| SearchSpaceEntry, | |
| Objective, | |
| Reducer, | |
| Direction, | |
| Sampler, | |
| TARGET_REDUCERS, | |
| } from "../api"; | |
| interface Props { | |
| example: string | null; | |
| schema: ParamSpec[]; | |
| } | |
| interface SpaceRow { | |
| name: string; | |
| low: number; | |
| high: number; | |
| /** Optional discretization step. Empty/zero = continuous (or default 1 for ints). */ | |
| step: number | ""; | |
| } | |
| const REDUCERS: { value: Reducer; label: string }[] = [ | |
| { value: "abs_last", label: "abs(last) — drive value → 0" }, | |
| { value: "last", label: "last — final sample" }, | |
| { value: "mean", label: "mean — average over trace" }, | |
| { value: "abs_mean", label: "abs(mean)" }, | |
| { value: "stability", label: "stability — std-dev of 2nd half" }, | |
| { value: "mae", label: "MAE — mean absolute error vs target" }, | |
| { value: "mape", label: "MAPE % — mean absolute percent error vs target" }, | |
| ]; | |
| function isTargetReducer(r: Reducer): boolean { | |
| return (TARGET_REDUCERS as Reducer[]).includes(r); | |
| } | |
| const STATUS_COLOR: Record<StudyState["status"], string> = { | |
| queued: "gray", | |
| running: "blue", | |
| done: "teal", | |
| cancelled: "yellow", | |
| failed: "red", | |
| }; | |
| const SAMPLER_LABELS: Record<Sampler, string> = { | |
| tpe: "TPE (Bayesian)", | |
| random: "Random", | |
| cmaes: "CMA-ES (single-objective only)", | |
| nsga2: "NSGA-II (multi-objective, default)", | |
| nsga3: "NSGA-III (3+ objectives)", | |
| }; | |
| const METRIC_OPTIONS = ["Step", "Temp", "Press", "Volume", "PotEng", "KinEng", "TotEng"]; | |
| function fmtTime(t: number): string { | |
| if (!t) return "—"; | |
| return new Date(t * 1000).toLocaleString(undefined, { | |
| month: "short", | |
| day: "numeric", | |
| hour: "2-digit", | |
| minute: "2-digit", | |
| }); | |
| } | |
| function fmtNum(v: number): string { | |
| if (!isFinite(v)) return String(v); | |
| const a = Math.abs(v); | |
| if (a !== 0 && (a < 0.01 || a >= 1000)) return v.toExponential(3); | |
| return Number(v.toPrecision(4)).toString(); | |
| } | |
| export function OptimizePanel({ example, schema }: Props) { | |
| // Configuration state | |
| const [name, setName] = useState("opt-study"); | |
| const [nTrials, setNTrials] = useState<number>(10); | |
| const [sampler, setSampler] = useState<Sampler>("tpe"); | |
| const [objectives, setObjectives] = useState<Objective[]>([ | |
| { metric: "Press", reduce: "abs_last", direction: "minimize" }, | |
| ]); | |
| const [rows, setRows] = useState<SpaceRow[]>([]); | |
| // Studies & active study | |
| const [studies, setStudies] = useState<StudyState[]>([]); | |
| const [activeId, setActiveId] = useState<string | null>(null); | |
| const [submitting, setSubmitting] = useState(false); | |
| const numericParams = useMemo( | |
| () => schema.filter((p) => p.type === "float" || p.type === "int"), | |
| [schema] | |
| ); | |
| function refresh() { | |
| listStudies() | |
| .then(setStudies) | |
| .catch(() => setStudies([])); | |
| } | |
| useEffect(refresh, []); | |
| // Auto-pick sampler when number of objectives changes | |
| useEffect(() => { | |
| if (objectives.length >= 2 && (sampler === "tpe" || sampler === "cmaes")) { | |
| setSampler("nsga2"); | |
| } | |
| if (objectives.length === 1 && (sampler === "nsga2" || sampler === "nsga3")) { | |
| setSampler("tpe"); | |
| } | |
| }, [objectives.length]); // eslint-disable-line react-hooks/exhaustive-deps | |
| useEffect(() => { | |
| if (activeId !== null) return; | |
| const running = studies.find((s) => s.status === "running"); | |
| if (running) setActiveId(running.id); | |
| else if (studies.length > 0) setActiveId(studies[0].id); | |
| }, [studies, activeId]); | |
| const active = studies.find((s) => s.id === activeId) ?? null; | |
| const liveStudy = active && active.status === "running" ? active : null; | |
| useEffect(() => { | |
| if (!liveStudy) return; | |
| const t = setInterval(refresh, 2000); | |
| return () => clearInterval(t); | |
| }, [liveStudy?.id]); // eslint-disable-line react-hooks/exhaustive-deps | |
| // ---- objective row helpers ---- | |
| function addObjective() { | |
| setObjectives([ | |
| ...objectives, | |
| { metric: "Temp", reduce: "stability", direction: "minimize", target: null }, | |
| ]); | |
| } | |
| function changeReducer(i: number, newReducer: Reducer) { | |
| const o = objectives[i]; | |
| const next: Objective = { ...o, reduce: newReducer }; | |
| if (isTargetReducer(newReducer) && (o.target == null || o.target === undefined)) { | |
| // give the user a sensible starting value rather than an empty box | |
| next.target = 0; | |
| } | |
| setObjectives(objectives.map((x, ix) => (ix === i ? next : x))); | |
| } | |
| function updateObjective(i: number, patch: Partial<Objective>) { | |
| setObjectives(objectives.map((o, ix) => (ix === i ? { ...o, ...patch } : o))); | |
| } | |
| function removeObjective(i: number) { | |
| if (objectives.length <= 1) return; | |
| setObjectives(objectives.filter((_, ix) => ix !== i)); | |
| } | |
| // ---- search space row helpers ---- | |
| /** Sensible default low/high/step for a freshly-picked parameter. */ | |
| function defaultsFor(spec: ParamSpec): { low: number; high: number; step: number | "" } { | |
| const isInt = spec.type === "int"; | |
| if (isInt) { | |
| return { low: spec.min ?? 1, high: (spec.min ?? 1) + 99, step: spec.step ?? 1 }; | |
| } | |
| const step = spec.step ?? 0.1; | |
| const lo = spec.min ?? 0; | |
| return { low: lo, high: lo + step * 10, step: "" }; | |
| } | |
| function addRow() { | |
| const used = new Set(rows.map((r) => r.name)); | |
| const next = numericParams.find((p) => !used.has(p.name)); | |
| if (!next) return; | |
| setRows([...rows, { name: next.name, ...defaultsFor(next) }]); | |
| } | |
| function changeRowParam(i: number, newName: string) { | |
| const oldRow = rows[i]; | |
| const oldSpec = numericParams.find((p) => p.name === oldRow.name); | |
| const newSpec = numericParams.find((p) => p.name === newName); | |
| if (!newSpec) return; | |
| // Reset bounds whenever the type changes (int <-> float). Otherwise keep | |
| // the user's bounds — they may have just renamed the parameter. | |
| if (oldSpec?.type !== newSpec.type) { | |
| setRows( | |
| rows.map((r, ix) => | |
| ix === i ? { name: newName, ...defaultsFor(newSpec) } : r | |
| ) | |
| ); | |
| } else { | |
| setRows(rows.map((r, ix) => (ix === i ? { ...r, name: newName } : r))); | |
| } | |
| } | |
| function updateRow(i: number, patch: Partial<SpaceRow>) { | |
| setRows(rows.map((r, ix) => (ix === i ? { ...r, ...patch } : r))); | |
| } | |
| function removeRow(i: number) { | |
| setRows(rows.filter((_, ix) => ix !== i)); | |
| } | |
| async function onStart() { | |
| if (!example) { | |
| notifications.show({ | |
| title: "Pick an example first", | |
| message: "Use the dropdown in the top toolbar.", | |
| color: "yellow", | |
| }); | |
| return; | |
| } | |
| if (rows.length === 0) { | |
| notifications.show({ | |
| title: "Empty search space", | |
| message: "Add at least one parameter to vary.", | |
| color: "yellow", | |
| }); | |
| return; | |
| } | |
| const space: Record<string, SearchSpaceEntry> = {}; | |
| for (const r of rows) { | |
| const spec = numericParams.find((p) => p.name === r.name); | |
| if (!spec) continue; | |
| const entry: SearchSpaceEntry = { | |
| type: spec.type === "int" ? "int" : "float", | |
| low: r.low, | |
| high: r.high, | |
| }; | |
| if (typeof r.step === "number" && r.step > 0) { | |
| entry.step = r.step; | |
| } | |
| space[r.name] = entry; | |
| } | |
| setSubmitting(true); | |
| try { | |
| const s = await startStudy({ | |
| name, | |
| example, | |
| fixed: {}, | |
| space, | |
| objectives, | |
| n_trials: nTrials, | |
| sampler, | |
| }); | |
| setActiveId(s.id); | |
| refresh(); | |
| notifications.show({ | |
| title: "Optimization started", | |
| message: `${s.n_trials} trials with ${SAMPLER_LABELS[s.sampler]}`, | |
| color: "teal", | |
| }); | |
| } catch (e) { | |
| notifications.show({ | |
| title: "Could not start study", | |
| message: String(e), | |
| color: "red", | |
| }); | |
| } finally { | |
| setSubmitting(false); | |
| } | |
| } | |
| async function onStop() { | |
| if (!active) return; | |
| try { | |
| await stopStudy(active.id); | |
| refresh(); | |
| } catch (e) { | |
| notifications.show({ | |
| title: "Stop failed", | |
| message: String(e), | |
| color: "red", | |
| }); | |
| } | |
| } | |
| return ( | |
| <Stack gap="md" h="100%" mih={0}> | |
| <Stack gap="xs"> | |
| <Title order={5}>Set up an optimization</Title> | |
| <Alert | |
| variant="light" | |
| color="blue" | |
| icon={<IconInfoCircle size={14} />} | |
| styles={{ message: { fontSize: 12 } }} | |
| > | |
| Each <b>trial</b> launches a run with parameters chosen by the sampler, | |
| then reduces the run's thermo trace to one value per <b>objective</b>. | |
| Add a second objective for trade-off optimisation (NSGA-II/III) — for | |
| example, minimise pressure deviation <i>and</i> maximise stability. | |
| Trials show up in the right-hand "Recent runs" panel — click any | |
| trial row below to inspect that run. | |
| </Alert> | |
| <Group grow> | |
| <TextInput | |
| label="Study name" | |
| value={name} | |
| onChange={(e) => setName(e.currentTarget.value)} | |
| placeholder="my-sweep" | |
| maxLength={80} | |
| /> | |
| <NumberInput | |
| label="Number of trials" | |
| value={nTrials} | |
| onChange={(v) => setNTrials(typeof v === "number" ? v : 10)} | |
| min={1} | |
| max={500} | |
| /> | |
| <Select | |
| label="Sampler" | |
| data={Object.entries(SAMPLER_LABELS).map(([value, label]) => ({ | |
| value, | |
| label, | |
| disabled: | |
| (objectives.length >= 2 && (value === "cmaes")) || | |
| (objectives.length === 1 && (value === "nsga2" || value === "nsga3")), | |
| }))} | |
| value={sampler} | |
| onChange={(v) => v && setSampler(v as Sampler)} | |
| description={ | |
| objectives.length >= 2 | |
| ? "Multi-objective: NSGA-II / NSGA-III recommended" | |
| : undefined | |
| } | |
| /> | |
| </Group> | |
| <Divider | |
| label={`Objectives (${objectives.length}${ | |
| objectives.length >= 2 ? " — Pareto" : "" | |
| })`} | |
| labelPosition="left" | |
| /> | |
| <Table withRowBorders={false} verticalSpacing={4}> | |
| <Table.Thead> | |
| <Table.Tr> | |
| <Table.Th style={{ width: 180 }}>Metric (thermo column)</Table.Th> | |
| <Table.Th>Reduce</Table.Th> | |
| <Table.Th style={{ width: 110 }}>Target</Table.Th> | |
| <Table.Th style={{ width: 130 }}>Direction</Table.Th> | |
| <Table.Th style={{ width: 40 }}></Table.Th> | |
| </Table.Tr> | |
| </Table.Thead> | |
| <Table.Tbody> | |
| {objectives.map((o, i) => { | |
| const needsTarget = isTargetReducer(o.reduce); | |
| return ( | |
| <Table.Tr key={i}> | |
| <Table.Td> | |
| <Select | |
| size="xs" | |
| data={METRIC_OPTIONS} | |
| value={o.metric} | |
| onChange={(v) => v && updateObjective(i, { metric: v })} | |
| searchable | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| <Select | |
| size="xs" | |
| data={REDUCERS} | |
| value={o.reduce} | |
| onChange={(v) => v && changeReducer(i, v as Reducer)} | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| {needsTarget ? ( | |
| <NumberInput | |
| size="xs" | |
| value={o.target ?? 0} | |
| onChange={(v) => | |
| updateObjective(i, { | |
| target: typeof v === "number" ? v : 0, | |
| }) | |
| } | |
| decimalScale={6} | |
| placeholder="reference value" | |
| /> | |
| ) : ( | |
| <Text size="11px" c="dimmed"> | |
| n/a | |
| </Text> | |
| )} | |
| </Table.Td> | |
| <Table.Td> | |
| <Select | |
| size="xs" | |
| data={[ | |
| { value: "minimize", label: "Minimize" }, | |
| { value: "maximize", label: "Maximize" }, | |
| ]} | |
| value={o.direction} | |
| onChange={(v) => | |
| v && updateObjective(i, { direction: v as Direction }) | |
| } | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| <Tooltip | |
| label={ | |
| objectives.length === 1 | |
| ? "Need at least one objective" | |
| : "Remove" | |
| } | |
| withArrow | |
| > | |
| <ActionIcon | |
| size="sm" | |
| variant="subtle" | |
| color="red" | |
| onClick={() => removeObjective(i)} | |
| disabled={objectives.length <= 1} | |
| > | |
| <IconTrash size={14} /> | |
| </ActionIcon> | |
| </Tooltip> | |
| </Table.Td> | |
| </Table.Tr> | |
| ); | |
| })} | |
| </Table.Tbody> | |
| </Table> | |
| <Group> | |
| <Button | |
| size="xs" | |
| variant="default" | |
| leftSection={<IconPlus size={14} />} | |
| onClick={addObjective} | |
| > | |
| Add objective | |
| </Button> | |
| {objectives.length >= 2 && ( | |
| <Text size="xs" c="dimmed"> | |
| Multi-objective active — Optuna will return a <b>Pareto front</b> of | |
| non-dominated trials, not a single best. | |
| </Text> | |
| )} | |
| </Group> | |
| <Divider label="Search space" labelPosition="left" /> | |
| {rows.length === 0 ? ( | |
| <Text size="sm" c="dimmed"> | |
| No parameters to vary yet. Add one with the button below. | |
| </Text> | |
| ) : ( | |
| <Table withRowBorders={false} verticalSpacing={4}> | |
| <Table.Thead> | |
| <Table.Tr> | |
| <Table.Th>Parameter</Table.Th> | |
| <Table.Th style={{ width: 120 }}>Low</Table.Th> | |
| <Table.Th style={{ width: 120 }}>High</Table.Th> | |
| <Table.Th style={{ width: 120 }}> | |
| <Group gap={4} wrap="nowrap"> | |
| <Text size="xs">Step</Text> | |
| <Tooltip | |
| label="Optional discretization step. Leave blank for continuous (floats) or unit step (integers)." | |
| withArrow | |
| multiline | |
| w={240} | |
| > | |
| <IconInfoCircle size={12} /> | |
| </Tooltip> | |
| </Group> | |
| </Table.Th> | |
| <Table.Th style={{ width: 40 }}></Table.Th> | |
| </Table.Tr> | |
| </Table.Thead> | |
| <Table.Tbody> | |
| {rows.map((r, i) => { | |
| const spec = numericParams.find((p) => p.name === r.name); | |
| const isInt = spec?.type === "int"; | |
| const decimals = isInt ? 0 : spec?.decimals ?? 6; | |
| const stepInput = spec?.step ?? (isInt ? 1 : 0.1); | |
| const usedElsewhere = new Set( | |
| rows.filter((_, ix) => ix !== i).map((x) => x.name) | |
| ); | |
| const options = numericParams | |
| .filter((p) => !usedElsewhere.has(p.name)) | |
| .map((p) => ({ | |
| value: p.name, | |
| label: `${p.label} (${p.name})`, | |
| })); | |
| return ( | |
| <Table.Tr key={i}> | |
| <Table.Td> | |
| <Select | |
| size="xs" | |
| data={options} | |
| value={r.name} | |
| onChange={(v) => v && changeRowParam(i, v)} | |
| searchable | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| <NumberInput | |
| size="xs" | |
| value={r.low} | |
| onChange={(v) => | |
| updateRow(i, { low: typeof v === "number" ? v : 0 }) | |
| } | |
| decimalScale={decimals} | |
| step={stepInput} | |
| allowDecimal={!isInt} | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| <NumberInput | |
| size="xs" | |
| value={r.high} | |
| onChange={(v) => | |
| updateRow(i, { high: typeof v === "number" ? v : 1 }) | |
| } | |
| decimalScale={decimals} | |
| step={stepInput} | |
| allowDecimal={!isInt} | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| <NumberInput | |
| size="xs" | |
| value={r.step} | |
| onChange={(v) => | |
| updateRow(i, { | |
| step: typeof v === "number" ? v : "", | |
| }) | |
| } | |
| placeholder={isInt ? "1 (default)" : "continuous"} | |
| decimalScale={decimals} | |
| step={stepInput} | |
| min={0} | |
| allowDecimal={!isInt} | |
| hideControls={!isInt} | |
| /> | |
| </Table.Td> | |
| <Table.Td> | |
| <Tooltip label="Remove" withArrow> | |
| <ActionIcon | |
| size="sm" | |
| variant="subtle" | |
| color="red" | |
| onClick={() => removeRow(i)} | |
| > | |
| <IconTrash size={14} /> | |
| </ActionIcon> | |
| </Tooltip> | |
| </Table.Td> | |
| </Table.Tr> | |
| ); | |
| })} | |
| </Table.Tbody> | |
| </Table> | |
| )} | |
| <Group> | |
| <Button | |
| size="xs" | |
| variant="default" | |
| leftSection={<IconPlus size={14} />} | |
| onClick={addRow} | |
| disabled={rows.length >= numericParams.length} | |
| > | |
| Add parameter | |
| </Button> | |
| <Button | |
| leftSection={<IconPlayerPlay size={14} />} | |
| onClick={onStart} | |
| loading={submitting} | |
| disabled={!example || rows.length === 0 || liveStudy !== null} | |
| > | |
| Start optimization | |
| </Button> | |
| {liveStudy && ( | |
| <Button | |
| leftSection={<IconPlayerStop size={14} />} | |
| variant="default" | |
| color="red" | |
| onClick={onStop} | |
| > | |
| Stop | |
| </Button> | |
| )} | |
| <Text size="xs" c="dimmed" ml="auto"> | |
| Example: <Code>{example ?? "—"}</Code> | |
| </Text> | |
| </Group> | |
| </Stack> | |
| <Divider /> | |
| <Stack gap="xs" mih={0} style={{ flex: 1 }}> | |
| <Group justify="space-between" wrap="nowrap"> | |
| <Group gap="xs"> | |
| <Title order={6}>Studies</Title> | |
| <Badge size="xs" variant="light" color="gray"> | |
| {studies.length} | |
| </Badge> | |
| </Group> | |
| {studies.length > 0 && ( | |
| <Select | |
| size="xs" | |
| data={studies.map((s) => ({ | |
| value: s.id, | |
| label: `${s.name} (${s.status}, ${s.n_completed}/${s.n_trials})`, | |
| }))} | |
| value={activeId} | |
| onChange={setActiveId} | |
| w={320} | |
| clearable={false} | |
| /> | |
| )} | |
| </Group> | |
| {!active ? ( | |
| <Text size="sm" c="dimmed"> | |
| No studies yet. Configure one above and click Start. | |
| </Text> | |
| ) : ( | |
| <ActiveStudyView study={active} /> | |
| )} | |
| </Stack> | |
| </Stack> | |
| ); | |
| } | |
| function ActiveStudyView({ study }: { study: StudyState }) { | |
| const completed = study.trials.filter((t) => t.values !== null).length; | |
| const objLabels = study.objectives.map((o) => { | |
| const base = `${o.metric}/${o.reduce}`; | |
| return o.target != null ? `${base}@${fmtNum(o.target)}` : base; | |
| }); | |
| // Per-objective formatter: MAPE shown with %, others as-is. | |
| const fmtVal = (i: number, v: number) => | |
| study.objectives[i]?.reduce === "mape" ? `${fmtNum(v)}%` : fmtNum(v); | |
| return ( | |
| <Stack gap="xs" mih={0} style={{ flex: 1 }}> | |
| <Group gap="md" wrap="wrap"> | |
| <Badge color={STATUS_COLOR[study.status]} variant="filled" size="md"> | |
| {study.status} | |
| </Badge> | |
| <Text size="sm"> | |
| <b>{completed}</b> / {study.n_trials} trials | |
| </Text> | |
| <Text size="xs" c="dimmed"> | |
| {objLabels.join(" · ")} | |
| {" — "} | |
| {study.objectives.map((o) => o.direction).join("/")} | |
| {" · "} | |
| {study.sampler.toUpperCase()} | |
| {" · started "} | |
| {fmtTime(study.started_at)} | |
| </Text> | |
| </Group> | |
| {study.error && ( | |
| <Alert color="red" variant="light" title="Study error"> | |
| {study.error} | |
| </Alert> | |
| )} | |
| {study.pareto_trials.length > 0 && ( | |
| <ParetoBlock study={study} /> | |
| )} | |
| <StudyReport | |
| studyId={study.id} | |
| refreshTick={completed} | |
| nCompleted={completed} | |
| /> | |
| <Divider label="Trials" labelPosition="left" /> | |
| <Table.ScrollContainer minWidth={400} style={{ flex: 1, minHeight: 0, overflow: "auto" }}> | |
| <Table verticalSpacing={4} highlightOnHover> | |
| <Table.Thead> | |
| <Table.Tr> | |
| <Table.Th style={{ width: 40 }}>#</Table.Th> | |
| <Table.Th>Parameters</Table.Th> | |
| {objLabels.map((l, i) => ( | |
| <Table.Th key={i} style={{ width: 110 }}> | |
| {l} | |
| </Table.Th> | |
| ))} | |
| <Table.Th style={{ width: 80 }}>Status</Table.Th> | |
| <Table.Th style={{ width: 90 }}>Run</Table.Th> | |
| </Table.Tr> | |
| </Table.Thead> | |
| <Table.Tbody> | |
| {study.trials.map((t) => ( | |
| <Table.Tr key={t.number}> | |
| <Table.Td> | |
| <Text size="xs" ff="monospace"> | |
| {t.number} | |
| </Text> | |
| </Table.Td> | |
| <Table.Td> | |
| <Text size="xs" ff="monospace" lineClamp={1}> | |
| {Object.entries(t.overrides) | |
| .map(([k, v]) => | |
| typeof v === "number" | |
| ? `${k}=${Number(v.toPrecision(4))}` | |
| : `${k}=${v}` | |
| ) | |
| .join(" ")} | |
| </Text> | |
| </Table.Td> | |
| {objLabels.map((_, i) => ( | |
| <Table.Td key={i}> | |
| <Text size="xs" ff="monospace"> | |
| {t.values && t.values[i] !== undefined | |
| ? fmtVal(i, t.values[i]) | |
| : "—"} | |
| </Text> | |
| </Table.Td> | |
| ))} | |
| <Table.Td> | |
| <Badge | |
| size="xs" | |
| color={ | |
| t.status === "done" | |
| ? "teal" | |
| : t.status === "running" | |
| ? "blue" | |
| : t.status === "cancelled" | |
| ? "yellow" | |
| : "red" | |
| } | |
| variant="light" | |
| > | |
| {t.status} | |
| </Badge> | |
| </Table.Td> | |
| <Table.Td> | |
| <Text size="10px" ff="monospace" c="dimmed"> | |
| {t.run_id || "—"} | |
| </Text> | |
| </Table.Td> | |
| </Table.Tr> | |
| ))} | |
| {study.trials.length === 0 && ( | |
| <Table.Tr> | |
| <Table.Td colSpan={4 + objLabels.length}> | |
| <Text size="xs" c="dimmed" ta="center"> | |
| No trials run yet… | |
| </Text> | |
| </Table.Td> | |
| </Table.Tr> | |
| )} | |
| </Table.Tbody> | |
| </Table> | |
| </Table.ScrollContainer> | |
| </Stack> | |
| ); | |
| } | |
| function ParetoBlock({ study }: { study: StudyState }) { | |
| const nObj = study.n_objectives; | |
| const fmtVal = (i: number, v: number) => | |
| study.objectives[i]?.reduce === "mape" ? `${fmtNum(v)}%` : fmtNum(v); | |
| if (nObj === 1) { | |
| const best = study.pareto_trials[0]; | |
| return ( | |
| <Stack gap={4}> | |
| <Text size="sm"> | |
| Best: <b>{fmtVal(0, best.values[0])}</b> · trial #{best.number} | |
| </Text> | |
| <Code block style={{ fontSize: 11 }}> | |
| {`best_params = ${JSON.stringify(best.params, null, 2)}`} | |
| </Code> | |
| </Stack> | |
| ); | |
| } | |
| return ( | |
| <Stack gap="xs"> | |
| <Text size="sm"> | |
| Pareto front: <b>{study.pareto_trials.length}</b> non-dominated trials | |
| </Text> | |
| {nObj === 2 && ( | |
| <ParetoScatter | |
| xs={study.trials | |
| .filter((t) => t.values !== null) | |
| .map((t) => t.values![0])} | |
| ys={study.trials | |
| .filter((t) => t.values !== null) | |
| .map((t) => t.values![1])} | |
| paretoXs={study.pareto_trials.map((t) => t.values[0])} | |
| paretoYs={study.pareto_trials.map((t) => t.values[1])} | |
| xLabel={`${study.objectives[0].metric}/${study.objectives[0].reduce} (${study.objectives[0].direction})`} | |
| yLabel={`${study.objectives[1].metric}/${study.objectives[1].reduce} (${study.objectives[1].direction})`} | |
| /> | |
| )} | |
| <Table verticalSpacing={4} striped> | |
| <Table.Thead> | |
| <Table.Tr> | |
| <Table.Th style={{ width: 40 }}>#</Table.Th> | |
| {study.objectives.map((o, i) => ( | |
| <Table.Th key={i}>{`${o.metric}/${o.reduce}`}</Table.Th> | |
| ))} | |
| <Table.Th>Params</Table.Th> | |
| </Table.Tr> | |
| </Table.Thead> | |
| <Table.Tbody> | |
| {study.pareto_trials.map((p) => ( | |
| <Table.Tr key={p.number}> | |
| <Table.Td> | |
| <Text size="xs" ff="monospace"> | |
| {p.number} | |
| </Text> | |
| </Table.Td> | |
| {p.values.map((v, i) => ( | |
| <Table.Td key={i}> | |
| <Text size="xs" ff="monospace"> | |
| {fmtVal(i, v)} | |
| </Text> | |
| </Table.Td> | |
| ))} | |
| <Table.Td> | |
| <Text size="xs" ff="monospace" lineClamp={1}> | |
| {Object.entries(p.params) | |
| .map(([k, v]) => | |
| typeof v === "number" | |
| ? `${k}=${Number(v.toPrecision(4))}` | |
| : `${k}=${v}` | |
| ) | |
| .join(" ")} | |
| </Text> | |
| </Table.Td> | |
| </Table.Tr> | |
| ))} | |
| </Table.Tbody> | |
| </Table> | |
| </Stack> | |
| ); | |
| } | |
| function ParetoScatter({ | |
| xs, | |
| ys, | |
| paretoXs, | |
| paretoYs, | |
| xLabel, | |
| yLabel, | |
| }: { | |
| xs: number[]; | |
| ys: number[]; | |
| paretoXs: number[]; | |
| paretoYs: number[]; | |
| xLabel: string; | |
| yLabel: string; | |
| }) { | |
| const W = 600; | |
| const H = 240; | |
| const M = { l: 60, r: 12, t: 8, b: 36 }; | |
| const innerW = W - M.l - M.r; | |
| const innerH = H - M.t - M.b; | |
| if (xs.length === 0) return null; | |
| const xMin = Math.min(...xs, ...paretoXs); | |
| const xMax = Math.max(...xs, ...paretoXs); | |
| const yMin = Math.min(...ys, ...paretoYs); | |
| const yMax = Math.max(...ys, ...paretoYs); | |
| 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) * innerW; | |
| const py = (y: number) => M.t + (1 - (y - yMin) / yR) * innerH; | |
| // Sort pareto by x for the connecting line | |
| const sortedPareto = paretoXs | |
| .map((x, i) => ({ x, y: paretoYs[i] })) | |
| .sort((a, b) => a.x - b.x); | |
| return ( | |
| <svg | |
| viewBox={`0 0 ${W} ${H}`} | |
| preserveAspectRatio="xMidYMid meet" | |
| style={{ width: "100%", height: 240, background: "#fafafa", borderRadius: 4 }} | |
| > | |
| {/* Axes */} | |
| <line x1={M.l} x2={M.l} y1={M.t} y2={H - M.b} stroke="#adb5bd" /> | |
| <line x1={M.l} x2={W - M.r} y1={H - M.b} y2={H - M.b} stroke="#adb5bd" /> | |
| {/* All trials */} | |
| {xs.map((x, i) => ( | |
| <circle key={i} cx={px(x)} cy={py(ys[i])} r={3} fill="#adb5bd" opacity={0.5} /> | |
| ))} | |
| {/* Pareto front line */} | |
| <polyline | |
| points={sortedPareto.map((p) => `${px(p.x)},${py(p.y)}`).join(" ")} | |
| fill="none" | |
| stroke="#1c7ed6" | |
| strokeWidth={1} | |
| strokeDasharray="2 2" | |
| /> | |
| {/* Pareto points */} | |
| {paretoXs.map((x, i) => ( | |
| <circle key={i} cx={px(x)} cy={py(paretoYs[i])} r={5} fill="#1c7ed6" /> | |
| ))} | |
| {/* Axis labels */} | |
| <text x={W / 2} y={H - 8} textAnchor="middle" fontSize={11} fill="#495057"> | |
| {xLabel} | |
| </text> | |
| <text | |
| x={14} | |
| y={H / 2} | |
| textAnchor="middle" | |
| fontSize={11} | |
| fill="#495057" | |
| transform={`rotate(-90 14 ${H / 2})`} | |
| > | |
| {yLabel} | |
| </text> | |
| {/* Min/max value labels */} | |
| <text x={M.l} y={H - M.b + 14} textAnchor="middle" fontSize={10} fill="#868e96"> | |
| {fmtNum(xMin)} | |
| </text> | |
| <text x={W - M.r} y={H - M.b + 14} textAnchor="middle" fontSize={10} fill="#868e96"> | |
| {fmtNum(xMax)} | |
| </text> | |
| <text x={M.l - 6} y={H - M.b} textAnchor="end" fontSize={10} fill="#868e96"> | |
| {fmtNum(yMin)} | |
| </text> | |
| <text x={M.l - 6} y={M.t + 6} textAnchor="end" fontSize={10} fill="#868e96"> | |
| {fmtNum(yMax)} | |
| </text> | |
| </svg> | |
| ); | |
| } | |