import {ChangeEvent, memo, useEffect, useState} from 'react'; import {FormControlLabel, Checkbox, Stack, Radio, RadioGroup, Typography} from "@mui/material"; import NumberTextField from "./NumberTextField"; import {azim_int_settings, q_binning} from "../client"; import {putConfigAzimIntMutation} from "../client/@tanstack/react-query.gen"; import _ from "lodash"; import FormControl from "@mui/material/FormControl"; import SettingsPanel from "./SettingsPanel"; import {useUpload} from "./useUpload"; // Linear bins are set by their spacing or by their number; log10 bins only by their number. type Binning = "linear_spacing" | "linear_bins" | "log10"; function binningOf(s: azim_int_settings): Binning { if (s.q_binning === q_binning.LOG10) return "log10"; return (s.q_bins !== undefined) ? "linear_bins" : "linear_spacing"; } type MyProps = { s?: azim_int_settings }; const default_azim_int_settings : azim_int_settings = { solid_angle_corr: true, polarization_corr: true, high_q_recipA: 5, low_q_recipA: 0.1, q_spacing: 0.1, q_binning: q_binning.LINEAR, azimuthal_bins: 1, force_cpu: false } function AzIntSettings({s: serverS}: MyProps) { const [s, setS] = useState(default_azim_int_settings); const [lastDownloadedS, setLastDownloadedS] = useState(default_azim_int_settings); const [downloadCounter, setDownloadCounter] = useState(0); const [lowQError, setLowQError] = useState(false); const [highQError, setHighQError] = useState(false); const [qSpacingError, setQSpacingError] = useState(false); const [qBinsError, setQBinsError] = useState(false); const [highQ, setHighQ] = useState(5); // The spacing and bin count last entered, kept while the other way of binning is selected. const [qSpacing, setQSpacing] = useState(0.1); const [qBins, setQBins] = useState(500); const { submit, pending, snackbar } = useUpload(putConfigAzimIntMutation()); useEffect(() => { if ((serverS !== undefined) && !_.isEqual(serverS, lastDownloadedS)) { setS(serverS); setLastDownloadedS(serverS); setDownloadCounter(c => c + 1); setHighQ(serverS.high_q_recipA ?? highQ); setQSpacing(serverS.q_spacing ?? qSpacing); setQBins(serverS.q_bins ?? qBins); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [serverS]); const dirty = !_.isEqual(s, lastDownloadedS); const binning = binningOf(s); const selectBinning = (b: Binning) => { setS(prev => { const rest = _.omit(prev, ["q_spacing", "q_bins"]); if (b === "linear_spacing") return {...rest, q_binning: q_binning.LINEAR, q_spacing: qSpacing}; return {...rest, q_binning: (b === "log10") ? q_binning.LOG10 : q_binning.LINEAR, q_bins: qBins}; }); }; return ( submit(s)} uploadDisabled={pending || ((s.high_q_recipA !== undefined) && (s.high_q_recipA <= s.low_q_recipA)) || highQError || lowQError || (binning === "linear_spacing" ? qSpacingError : qBinsError)} snackbar={snackbar}> {(binning === "linear_spacing") ? { setQSpacing(val); setS(prev => ({...prev, q_spacing: val})); setQSpacingError(err); }} fullWidth/> : { setQBins(val); setS(prev => ({...prev, q_bins: val})); setQBinsError(err); }} fullWidth/> } { setS(prev => ({...prev, low_q_recipA: val})); setLowQError(err); }} fullWidth/> { setHighQ(val); setS(prev => ({...prev, high_q_recipA: val})); setHighQError(err); }} fullWidth/> ) => { // No high Q means integrating out to the highest Q the detector reaches. setS(prev => event.target.checked ? _.omit(prev, "high_q_recipA") : {...prev, high_q_recipA: highQ}); }} />} label="High Q to detector edge"/> ) => { setS(prev => ({...prev, solid_angle_corr: event.target.checked})); }} />} label="Solid angle correction"/> ) => { setS(prev => ({...prev, polarization_corr: event.target.checked})); }} />} label="Polarization correction"/> ) => { setS(prev => ({...prev, force_cpu: event.target.checked})); }} />} label="Force CPU calculation in FPGA workflow"/> Q binning ) => selectBinning(event.target.value as Binning)} > } label="Linear, by spacing"/> } label="Linear, by number of bins"/> } label="Logarithmic (log10 Q)"/> Azimuthal bins ) => { setS(prev => ({...prev, azimuthal_bins: Number(event.target.value)})); }} > {[1, 2, 4, 8, 16, 32, 64, 128].map((value) => ( } label={value.toString()} /> ))} ); } export default memo(AzIntSettings);