import {memo, ReactNode, useEffect, useState} from 'react'; import {Button, FormControlLabel, Grid, Slider, Stack, Switch, Typography} from "@mui/material"; import {spot_finding_settings} from "../client"; import {putConfigSpotFindingMutation} from "../client/@tanstack/react-query.gen"; import SettingsPanel from "./SettingsPanel"; import {useUpload} from "./useUpload"; import _ from "lodash"; type MyProps = { s?: spot_finding_settings, update: () => void }; type Edits = Partial; // The broker stores the thresholds as float32, so 0.02 comes back as 0.0199999995: compare numbers // with a tolerance, or an uploaded value would never be recognised as the broker's. const same = (a: unknown, b: unknown) => typeof a === "number" && typeof b === "number" ? Math.abs(a - b) <= 1e-6 * Math.max(1, Math.abs(a)) : a === b; // The edits that still differ from the broker's copy. const pendingEdits = (edits: Edits, serverS: spot_finding_settings): Edits => _.pickBy(edits, (v, k) => !same(v, serverS[k as keyof spot_finding_settings])); type SettingSliderProps = { label: ReactNode, value: number, min: number, max: number, step: number, digits?: number, disabled: boolean, onChange: (v: number) => void, onCommit: (v: number) => void }; // Firefox saves a range input whose value the browser snapped to the slider's step (the broker's float32 // ice-ring width 0.02 reads back as 0.0199999...) as if the user had edited it, and on session restore // writes it back with an input event, which MUI commits as a change the user never made. const NO_FORM_RESTORE = { input: { autoComplete: "off" } }; function SettingSlider({label, value, min, max, step, digits = 0, disabled, onChange, onCommit}: SettingSliderProps) { return <> {label} onChange(v as number)} onChangeCommitted={(_e, v) => onCommit(v as number)} valueLabelDisplay="auto" valueLabelFormat={(x) => x.toFixed(digits)}/> ; } // The panel holds the broker's copy (serverS, from the /statistics poll) and the user's edits on top // of it, and shows the two merged. Nothing is shown before the broker's copy has arrived, and what is // sent is always the broker's copy with the edits applied - never a value the frontend made up. // Edits are sent by Upload; with Live sync engaged each change is sent as it is made instead. function DataProcessingSettings({s: serverS, update}: MyProps) { const [edits, setEdits] = useState({}); const [liveSync, setLiveSync] = useState(false); // The value a limit switched back on returns to: the last one the broker or the slider had. const [highResLimit, setHighResLimit] = useState(2.5); const [highResGap, setHighResGap] = useState(1.5); const { submit, pending, snackbar } = useUpload({ ...putConfigSpotFindingMutation(), onSuccess: () => update(), }); // An edit is done once the broker reports the same value (after Upload, live sync, or another // client setting it); a rejected one stays and keeps the panel marked as unsaved. useEffect(() => { if (serverS === undefined) return; setEdits(e => pendingEdits(e, serverS)); if (serverS.high_resolution_limit !== undefined) setHighResLimit(serverS.high_resolution_limit); if (serverS.high_res_gap_Q_recipA !== undefined) setHighResGap(serverS.high_res_gap_Q_recipA); }, [serverS]); if (serverS === undefined) return ( Waiting for the broker's settings ... ); const s: spot_finding_settings = {...serverS, ...edits}; const dirty = !_.isEmpty(pendingEdits(edits, serverS)); // A slider being dragged only moves the panel; MUI fires onChange for every intermediate position. const preview = (patch: Edits) => setEdits(e => ({...e, ...patch})); // A finished change: kept as an edit, and sent at once only with live sync engaged. const commit = (patch: Edits) => { const next = {...edits, ...patch}; setEdits(next); if (liveSync) submit({...serverS, ...next}); }; const toggle = (label: string, checked: boolean, onChange: (checked: boolean) => void, disabled = !s.enable) => onChange(e.target.checked)}/>}/>; return ( submit({...serverS, ...edits})} uploadDisabled={!dirty || pending} actions={<> {/* Engaged only from a clean panel, so live sync never sends edits made before it. */} setLiveSync(e.target.checked)}/>}/> }> {liveSync ? "Live sync on: each change is sent to the broker at once" : dirty ? "Unsaved edits: press Upload to send them, or Revert" : "Showing the broker's settings"} {toggle("Spot finding", s.enable, (v) => commit({enable: v}), false)} {/* Rejected by a broker whose detector finds spots on the FPGA: the edit then stays unsaved and the snackbar says why. */} {toggle("Adaptive (self-calibrating) threshold", s.adaptive_threshold ?? false, (v) => commit({adaptive_threshold: v}))} Sets the count threshold per resolution ring from each image's own noise. Not available where spots are found on the FPGA. preview({photon_count_threshold: v})} onCommit={(v) => commit({photon_count_threshold: v})}/> preview({false_pixels_per_frame: v})} onCommit={(v) => commit({false_pixels_per_frame: v})}/> preview({signal_to_noise_threshold: v})} onCommit={(v) => commit({signal_to_noise_threshold: v})}/> preview({min_pix_per_spot: v})} onCommit={(v) => commit({min_pix_per_spot: v})}/> Low resolution limit [Å]} value={Number(s.low_resolution_limit)} disabled={!s.enable} min={10} max={100} step={0.1} digits={1} onChange={(v) => preview({low_resolution_limit: v})} onCommit={(v) => commit({low_resolution_limit: v})}/> {/* No high-resolution limit means spot finding goes as far as the detector reaches. */} {toggle("To detector edge", s.high_resolution_limit === undefined, (v) => commit({high_resolution_limit: v ? undefined : highResLimit}))} High resolution limit [Å]} value={Number(s.high_resolution_limit ?? highResLimit)} disabled={!s.enable || s.high_resolution_limit === undefined} min={1} max={5} step={0.1} digits={1} onChange={(v) => { setHighResLimit(v); preview({high_resolution_limit: v}); }} onCommit={(v) => commit({high_resolution_limit: v})}/> High resolution limit for counting low resolution spots [Å]} value={Number(s.high_resolution_limit_for_spot_count_low_res)} disabled={!s.enable} min={2} max={8} step={0.1} digits={1} onChange={(v) => preview({high_resolution_limit_for_spot_count_low_res: v})} onCommit={(v) => commit({high_resolution_limit_for_spot_count_low_res: v})}/> Ice ring width in Q-space [Å-1]} value={Number(s.ice_ring_width_q_recipA)} disabled={!s.enable} min={0.0} max={0.2} step={0.001} digits={3} onChange={(v) => preview({ice_ring_width_q_recipA: v})} onCommit={(v) => commit({ice_ring_width_q_recipA: v})}/> {toggle("High-res gap Q-space filter", s.high_res_gap_Q_recipA !== undefined, (v) => commit({high_res_gap_Q_recipA: v ? highResGap : undefined}))} High-res gap [Å-1]} value={Number(s.high_res_gap_Q_recipA ?? highResGap)} disabled={!s.enable || s.high_res_gap_Q_recipA === undefined} min={0.1} max={5.0} step={0.01} digits={2} onChange={(v) => { setHighResGap(v); preview({high_res_gap_Q_recipA: v}); }} onCommit={(v) => commit({high_res_gap_Q_recipA: v})}/> {toggle("Indexing", s.indexing, (v) => commit({indexing: v}))} ); } export default memo(DataProcessingSettings);