From 833100e9263766f16d473aab36466fe2d57fd45a Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Wed, 2 Nov 2022 19:24:26 +0100 Subject: [PATCH 1/6] fix: plot mass with labels --- src/app/components/MeasurementMassPlot.tsx | 83 +++++++++++++--------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/src/app/components/MeasurementMassPlot.tsx b/src/app/components/MeasurementMassPlot.tsx index be10cfaf..a69cb49f 100644 --- a/src/app/components/MeasurementMassPlot.tsx +++ b/src/app/components/MeasurementMassPlot.tsx @@ -1,5 +1,5 @@ import { xyToXYObject } from 'ml-spectra-processing'; -import { getBestPeaks, getPeaks } from 'ms-spectrum'; +import { getBestPeaks, Spectrum } from 'ms-spectrum'; import { useMemo } from 'react'; import { Annotation, @@ -21,6 +21,25 @@ interface Peak { } export function MeasurementMassPlot(props: MeasurementPlotProps) { + const { measurement } = props; + if (!measurement.data) { + throw new Error( + 'This is weird, the data property is not available on measurement', + ); + } + if (measurement.data.length === 0) { + throw new Error('Data property is empty'); + } + if (measurement.data.length > 1) { + throw new Error('Length of data property is larger than 1'); + } + if (!measurement.data[0].variables.x) { + throw new Error('x variable in undefined'); + } + if (!measurement.data[0].variables.y) { + throw new Error('y variable in undefined'); + } + return ( @@ -29,48 +48,41 @@ export function MeasurementMassPlot(props: MeasurementPlotProps) { } function MassComponent(props: MeasurementPlotProps) { - const { - measurement, - dataIndex = 0, - xVariableName = 'x', - yVariableName = 'y', - } = props; + const { measurement } = props; + const { data } = measurement; - const xAxis = `${xVariableName}-x`; - const yAxis = `${yVariableName}-y`; - const { x, y } = useMemo(() => { - const { variables } = data[dataIndex]; - const { [xVariableName]: x, [yVariableName]: y } = variables; - if (x === undefined || y === undefined) { - throw new Error( - `Variable "${ - x === undefined ? xVariableName : yVariableName - }" is not available in data. Only ${Object.keys( - data[dataIndex].variables, - ).join(', ')} are available`, - ); - } + const { variables } = data[0]; - return { x, y }; - }, [data, dataIndex, xVariableName, yVariableName]); + const xAxis = `x-x`; // why such a weird name ??? + const yAxis = `y-y`; + const { x, y } = useMemo(() => { + return { x: variables.x, y: variables.y }; + }, [variables]); const { x: xDomain } = usePlotControllerAxes(); const { profile, peaks } = useMemo(() => { - const profile = xyToXYObject({ + const spectrum = new Spectrum({ x: x.data, y: y.data, }); + const isContinuous = spectrum.isContinuous(); + const profile = + isContinuous && + xyToXYObject({ + x: x.data, + y: y.data, + }); return { profile, - peaks: getPeaks(profile), + peaks: spectrum.getPeaks(profile), }; }, [x.data, y.data]); const bestPeaks = useMemo( () => getBestPeaks(peaks, { - from: xDomain?.min ?? Number.NEGATIVE_INFINITY, - to: xDomain?.max ?? Number.POSITIVE_INFINITY, - limit: 5, + from: xDomain?.min, + to: xDomain?.max, + limit: 10, numberSlots: 10, threshold: 0.01, }), @@ -78,13 +90,14 @@ function MassComponent(props: MeasurementPlotProps) { ); return ( - - + {profile && ( + + )} Date: Thu, 3 Nov 2022 15:05:33 +0100 Subject: [PATCH 2/6] chore: fix mass label position --- src/app/components/MeasurementMassPlot.tsx | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/app/components/MeasurementMassPlot.tsx b/src/app/components/MeasurementMassPlot.tsx index a69cb49f..011e6f2d 100644 --- a/src/app/components/MeasurementMassPlot.tsx +++ b/src/app/components/MeasurementMassPlot.tsx @@ -106,13 +106,7 @@ function MassComponent(props: MeasurementPlotProps) { /> {bestPeaks.map(({ x, y, shortLabel }: Peak) => ( - + Date: Fri, 4 Nov 2022 11:25:18 +0100 Subject: [PATCH 3/6] refactor: remove useless axisIDs --- src/app/components/MeasurementMassPlot.tsx | 12 +++++------- src/app/components/MeasurementPlot.tsx | 6 ++---- src/app/components/utils.tsx | 22 ++++++++++------------ 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/app/components/MeasurementMassPlot.tsx b/src/app/components/MeasurementMassPlot.tsx index 011e6f2d..14b58136 100644 --- a/src/app/components/MeasurementMassPlot.tsx +++ b/src/app/components/MeasurementMassPlot.tsx @@ -53,8 +53,6 @@ function MassComponent(props: MeasurementPlotProps) { const { data } = measurement; const { variables } = data[0]; - const xAxis = `x-x`; // why such a weird name ??? - const yAxis = `y-y`; const { x, y } = useMemo(() => { return { x: variables.x, y: variables.y }; }, [variables]); @@ -94,19 +92,19 @@ function MassComponent(props: MeasurementPlotProps) { )} {bestPeaks.map(({ x, y, shortLabel }: Peak) => ( - + { const { variables } = data[dataIndex]; const { [xVariableName]: x, [yVariableName]: y } = variables; @@ -66,8 +64,8 @@ function MeasurementComponent(props: MeasurementPlotProps) { x: x.data, y: y.data, })} - xAxis={xAxis} - yAxis={yAxis} + xAxis="x" + yAxis="y" /> ); diff --git a/src/app/components/utils.tsx b/src/app/components/utils.tsx index 670ed6c6..db2709e2 100644 --- a/src/app/components/utils.tsx +++ b/src/app/components/utils.tsx @@ -40,8 +40,6 @@ export function BasicComponent( } = props; const { title = '', data } = measurement; - const xAxis = `${xVariableName}-x`; - const yAxis = `${yVariableName}-y`; const { x, y } = useMemo(() => { const { variables } = data[dataIndex]; const { [xVariableName]: x, [yVariableName]: y } = variables; @@ -59,27 +57,27 @@ export function BasicComponent( }, [data, dataIndex, xVariableName, yVariableName]); const direction = new Set(['vertical', 'horizontal']); const rectZoom = useRectangularZoom({ - horizontalAxisId: xAxis, - verticalAxisId: yAxis, + horizontalAxisId: 'x', + verticalAxisId: 'y', disabled: zoom !== 'rectangular', }); const axisZoom = useAxisZoom({ direction: zoom === 'vertical' ? 'vertical' : 'horizontal', - horizontalAxisId: xAxis, - verticalAxisId: yAxis, + horizontalAxisId: 'x', + verticalAxisId: 'y', disabled: !direction.has(zoom), }); useAxisWheelZoom({ direction: wheelZoom === 'vertical' ? 'vertical' : 'horizontal', - axisId: wheelZoom === 'vertical' ? yAxis : xAxis, + axisId: wheelZoom === 'vertical' ? 'y' : 'x', disabled: !direction.has(wheelZoom), }); const crossHairAnnot = useCrossHair({ - horizontalAxisId: xAxis, - verticalAxisId: yAxis, + horizontalAxisId: 'x', + verticalAxisId: 'y', disabled: !crossHair, }); - usePan({ horizontalAxisId: xAxis, verticalAxisId: yAxis }); + usePan({ horizontalAxisId: 'x', verticalAxisId: 'y' }); return (