Files
aimsight-app/app/web/src/pages/Points.jsx
T

107 lines
6.6 KiB
React
Raw Normal View History

import { useEffect, useRef, useState } from "react";
import { Badge, Button, Card, Group, Loader, SegmentedControl, Select, Stack, Text, Title, useComputedColorScheme } from "@mantine/core";
import { call, Selector } from "../common.jsx";
// Points: the point labels (labeling/points/, labeling/scripts/points.py), one boolean per measurement of a range, valid or
// not, set by hand: the ground truth of the benchmarks. Mainly on the runs of the emulator: the page starts on that source. A dataset per station and range, created with every point valid;
// one target at a time, corr_x above corr_y; a click on a point switches it, a box sets every point inside it (mode).
// Each change is saved at once.
2026-11-20 08:37:56 +01:00
const VALID = "#2f9e44", INVALID = "#e03131";
const FONT = "Roboto, sans-serif";
export default function Points({ selection, choose }) {
const { station, start, end } = selection, name = `${station}_${start}_${end}`;
const [list, setList] = useState([]), [d, setD] = useState(null), [loading, setLoading] = useState(false);
const [sensor, setSensor] = useState(null), [mode, setMode] = useState("invalid");
const refresh = () => station ? call(`/api/points?${new URLSearchParams({ station })}`).then(l => l && setList(l)) : setList([]);
const exists = list.some(x => x.name === name);
useEffect(() => { refresh(); }, [station]);
useEffect(() => {
setD(null);
if (!exists) return;
setLoading(true);
call(`/api/points/${name}`).then(r => { setLoading(false); if (r) { setD(r); setSensor(s => (s in r.sensors ? s : Object.keys(r.sensors)[0])); } });
}, [name, exists]);
const create = () => { setLoading(true); call("/api/points", "POST", { station, start, end }).then(r => { setLoading(false); r && refresh(); }); };
// set the points (indices of the target's series) valid or not: at once on the page, then in the file; a refusal reloads
function set(indices, valid) {
const o = d.sensors[sensor], times = indices.filter(i => o.valid[i] !== valid).map(i => o.t[i]);
if (!times.length) return;
const v = o.valid.slice();
indices.forEach(i => { v[i] = valid; });
setD({ ...d, invalid: d.invalid + (valid ? -times.length : times.length), sensors: { ...d.sensors, [sensor]: { ...o, valid: v } } });
call(`/api/points/${name}`, "PUT", { sensor, times, valid }).then(r => {
if (r) setList(l => l.map(x => (x.name === name ? r : x)));
else call(`/api/points/${name}`).then(r2 => r2 && setD(r2));
});
}
const invalidOf = s => d.sensors[s].valid.filter(v => !v).length;
return (
<Stack gap="lg">
<div>
<Title order={2}>Points</Title>
<Text c="dimmed">Each measurement of a range, valid or not, set by hand: the ground truth of the benchmarks
(labeling/points/). A click on a point switches it; a box sets every point inside it.</Text>
</div>
<Selector selection={selection} choose={choose} start="emulator" />
{list.length > 0 && <Group gap="xs">
<Text size="sm" c="dimmed">Datasets of {station}:</Text>
{list.map(x => <Button key={x.name} size="compact-sm" variant={x.name === name ? "filled" : "light"}
onClick={() => choose({ station, start: x.start, end: x.end })}>
{x.start} – {x.end} · {x.invalid}/{x.points} invalid</Button>)}
</Group>}
{loading && <Loader />}
{station && !exists && !loading && <Card withBorder>
<Group justify="space-between">
<Text>No dataset for {station} from {start} to {end} (excluded).</Text>
<Button onClick={create} disabled={!start || !end}>Create the dataset (every point valid)</Button>
</Group>
</Card>}
{d && <Card withBorder>
<Stack>
<Group align="flex-end">
<Select label="Target" w={260} searchable allowDeselect={false} value={sensor} onChange={setSensor}
data={Object.keys(d.sensors).map(s => ({ value: s, label: `${s}${invalidOf(s) ? ` · ${invalidOf(s)} invalid` : ""}` }))} />
<div>
<Text size="sm" fw={500} mb={4}>A box sets the points</Text>
<SegmentedControl value={mode} onChange={setMode} data={[{ value: "invalid", label: "Invalid" }, { value: "valid", label: "Valid" }]} />
</div>
<Badge size="lg" variant="light" color="red">{d.invalid} invalid of {d.points}</Badge>
</Group>
{sensor && <Plot o={d.sensors[sensor]} sensor={sensor} set={set} mode={mode} />}
</Stack>
</Card>}
</Stack>
);
}
2026-11-20 08:37:56 +01:00
// corr_x above corr_y (px, the curves labelled), sharing the time; the points green valid, red invalid
function Plot({ o, sensor, set, mode }) {
const g = useRef(), [Plotly, setPlotly] = useState(null), dark = useComputedColorScheme("light") === "dark";
const latest = useRef();
latest.current = { o, set, mode };
useEffect(() => { import("plotly.js-dist-min").then(m => setPlotly(() => m.default)); }, []);
useEffect(() => {
if (!Plotly) return;
const color = o.valid.map(v => (v ? VALID : INVALID)), text = dark ? "#c9c9c9" : "#495057", grid = dark ? "#2e2e2e" : "#eef0f2";
const trace = (y, yaxis) => ({ type: "scattergl", mode: "lines+markers", x: o.t, y, yaxis, hovertemplate: "%{x}<br>%{y:.4f} px<extra></extra>",
line: { width: 1, color: dark ? "#555" : "#ccc" }, marker: { size: 6, color }, unselected: { marker: { opacity: 1 } } });
const axis = { gridcolor: grid, zeroline: false };
Plotly.react(g.current, [trace(o.x, "y"), trace(o.y, "y2")], {
height: 560, margin: { l: 60, r: 20, t: 10, b: 30 }, showlegend: false, dragmode: "select", selectdirection: "any",
uirevision: sensor, paper_bgcolor: "rgba(0,0,0,0)", plot_bgcolor: "rgba(0,0,0,0)", font: { family: FONT, size: 11, color: text },
grid: { rows: 2, columns: 1, shared_xaxes: true }, xaxis: { ...axis, anchor: "y2" },
yaxis: { ...axis, domain: [0.53, 1], title: { text: "corr_x [px]" } }, yaxis2: { ...axis, domain: [0, 0.47], title: { text: "corr_y [px]" } },
}, { responsive: true, displaylogo: false, doubleClick: "reset", modeBarButtonsToRemove: ["lasso2d", "autoScale2d", "toggleSpikelines"] });
if (!g.current._bound) { // once per plot: the handlers read the latest state
g.current._bound = true;
g.current.on("plotly_click", ev => { const i = ev.points[0].pointIndex; latest.current.set([i], !latest.current.o.valid[i]); });
g.current.on("plotly_selected", ev => {
if (!ev?.points?.length) return;
latest.current.set([...new Set(ev.points.map(p => p.pointIndex))], latest.current.mode === "valid");
Plotly.relayout(g.current, { selections: [] });
});
}
}, [Plotly, o, sensor, dark]);
return <div ref={g} />;
}