2026-11-20 08:28:19 +01:00
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
2026-11-20 08:32:18 +01:00
// 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;
2026-11-20 08:41:06 +01:00
// one target at a time, its rx, ry, error value and image quality stacked on the same time; a click on a point of any plot
// switches it, a box sets every point inside it (mode).
2026-11-20 08:28:19 +01:00
// Each change is saved at once.
2026-11-20 08:37:56 +01:00
const VALID = "#2f9e44" , INVALID = "#e03131" ;
2026-11-20 08:28:19 +01:00
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 >
2026-11-20 08:32:18 +01:00
< Selector selection = { selection } choose = { choose } start = "emulator" />
2026-11-20 08:28:19 +01:00
{ 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 >
2026-11-20 08:31:18 +01:00
< Button onClick = { create } disabled = { ! start || ! end }> Create the dataset ( every point valid )</ Button >
2026-11-20 08:28:19 +01:00
</ 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:41:06 +01:00
// The plots, top to bottom, those the sensor has (validity.COLUMNS): [column, title, unit of the tooltip]; the threshold
// of the error drawn dashed on its plot
const PANELS = [[ "relative_x" , "rx [px]" , " px" ], [ "relative_y" , "ry [px]" , " px" ], [ "error_value" , "Error value" , "" ],
[ "peak" , "Peak" , "" ], [ "flux" , "Flux" , "" ], [ "sharpness" , "Sharpness" , "" ], [ "fwhm" , "FWHM [px]" , " px" ],
[ "dominance" , "Dominance" , "" ], [ "snr" , "SNR" , "" ]];
2026-11-20 08:48:32 +01:00
const ROW = 340 , GAP = 0.006 ; // px per plot; space between plots (fraction of the height)
2026-11-20 08:41:06 +01:00
// the points green valid, red invalid, the same point at the same index on every plot
2026-11-20 08:28:19 +01:00
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" ;
2026-11-20 08:41:06 +01:00
const panels = PANELS . filter (([ c ]) => o . columns [ c ]), n = panels . length , h = ( 1 - GAP * ( n - 1 )) / n ;
const axis = { gridcolor : grid , zeroline : false }, layout = {}, traces = [];
panels . forEach (([ c , title , unit ], k ) => {
const y = k ? `y ${ k + 1 } ` : "y" , top = 1 - k * ( h + GAP );
layout [ k ? `yaxis ${ k + 1 } ` : "yaxis" ] = { ... axis , domain : [ Math . max ( 0 , top - h ), top ], title : { text : title , font : { size : 11 } } };
traces . push ({ type : "scattergl" , mode : "lines+markers" , x : o . t , y : o . columns [ c ], yaxis : y , meta : "point" ,
hovertemplate : `%{x}<br> ${ title } : %{y:.4f} ${ unit } <extra></extra>` , line : { width : 1 , color : dark ? "#555" : "#ccc" },
marker : { size : 5 , color }, unselected : { marker : { opacity : 1 } } });
if ( c === "error_value" && o . columns . error_threshold )
traces . push ({ type : "scattergl" , mode : "lines" , x : o . t , y : o . columns . error_threshold , yaxis : y , meta : "threshold" , hoverinfo : "skip" ,
line : { width : 1 , dash : "dash" , color : INVALID } });
});
Plotly . react ( g . current , traces , {
... layout , height : ROW * n + 40 , margin : { l : 70 , r : 20 , t : 10 , b : 30 }, showlegend : false , dragmode : "select" , selectdirection : "any" ,
2026-11-20 08:28:19 +01:00
uirevision : sensor , paper_bgcolor : "rgba(0,0,0,0)" , plot_bgcolor : "rgba(0,0,0,0)" , font : { family : FONT , size : 11 , color : text },
2026-11-20 08:41:06 +01:00
xaxis : { ... axis , anchor : n > 1 ? `y ${ n } ` : "y" },
2026-11-20 08:28:19 +01:00
}, { 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 ;
2026-11-20 08:41:06 +01:00
const ofPoints = ps => ps . filter ( p => p . data . meta === "point" ). map ( p => p . pointIndex );
g . current . on ( "plotly_click" , ev => { const [ i ] = ofPoints ( ev . points ); if ( i !== undefined ) latest . current . set ([ i ], ! latest . current . o . valid [ i ]); });
2026-11-20 08:28:19 +01:00
g . current . on ( "plotly_selected" , ev => {
2026-11-20 08:41:06 +01:00
const is = [... new Set ( ofPoints ( ev ? . points || []))];
if ( ! is . length ) return ;
latest . current . set ( is , latest . current . mode === "valid" );
2026-11-20 08:28:19 +01:00
Plotly . relayout ( g . current , { selections : [] });
});
}
}, [ Plotly , o , sensor , dark ]);
return < div ref = { g } />;
}