|
| 1 | +import type { Spectrum2D } from '@zakodium/nmrium-core'; |
| 2 | +import type { NmrData2DContent, NmrData2DFt } from 'cheminfo-types'; |
| 3 | +import { xyEquallySpaced } from 'ml-spectra-processing'; |
| 4 | + |
| 5 | +import { useChartData } from '../context/ChartContext.tsx'; |
| 6 | + |
| 7 | +export interface SpectrumData extends Pick<Spectrum2D, 'display' | 'id'> { |
| 8 | + data: NmrData2DContent; |
| 9 | +} |
| 10 | +export function use2DReducer(spectra: Spectrum2D[]) { |
| 11 | + const { xDomain, yDomain } = useChartData(); |
| 12 | + const [fromX, toX] = xDomain; |
| 13 | + const [fromY, toY] = yDomain; |
| 14 | + const outputSpectra: SpectrumData[] = []; |
| 15 | + for (const spectrum of spectra) { |
| 16 | + const { id, display, data } = spectrum; |
| 17 | + const { rr } = data as NmrData2DFt; |
| 18 | + const reducedData = reduce2DSpectrum(rr, { |
| 19 | + fromX, |
| 20 | + toX, |
| 21 | + fromY, |
| 22 | + toY, |
| 23 | + }); |
| 24 | + outputSpectra.push({ |
| 25 | + data: reducedData, |
| 26 | + id, |
| 27 | + display, |
| 28 | + }); |
| 29 | + } |
| 30 | + return outputSpectra; |
| 31 | +} |
| 32 | + |
| 33 | +interface Reduce2DSpectrumOptions { |
| 34 | + numberOfPoints?: number; |
| 35 | + fromX?: number; |
| 36 | + toX?: number; |
| 37 | + fromY?: number; |
| 38 | + toY?: number; |
| 39 | +} |
| 40 | + |
| 41 | +export function reduce2DSpectrum( |
| 42 | + data: NmrData2DContent, |
| 43 | + options: Reduce2DSpectrumOptions, |
| 44 | +) { |
| 45 | + const { |
| 46 | + minY: originalMinY, |
| 47 | + minX: originalMinX, |
| 48 | + maxY: originalMaxY, |
| 49 | + maxX: originalMaxX, |
| 50 | + z, |
| 51 | + } = data; |
| 52 | + const { numberOfPoints = 1024, fromX, fromY, toX, toY } = options; |
| 53 | + const nbPointsY = z.length; |
| 54 | + const nbPointsX = z[0]?.length || 0; |
| 55 | + |
| 56 | + if (nbPointsX <= numberOfPoints && nbPointsY <= numberOfPoints) { |
| 57 | + return data; |
| 58 | + } |
| 59 | + |
| 60 | + const minX = fromX ?? originalMinX; |
| 61 | + const maxX = toX ?? originalMaxX; |
| 62 | + const minY = fromY ?? originalMinY; |
| 63 | + const maxY = toY ?? originalMaxY; |
| 64 | + |
| 65 | + const shouldReduceX = nbPointsX > numberOfPoints; |
| 66 | + const shouldReduceY = nbPointsY > numberOfPoints; |
| 67 | + |
| 68 | + const targetPointX = shouldReduceX ? numberOfPoints : nbPointsX; |
| 69 | + const targetPointY = shouldReduceY ? numberOfPoints : nbPointsY; |
| 70 | + |
| 71 | + let reducedX: Float64Array[] = []; |
| 72 | + // Reduce over x dimension |
| 73 | + if (shouldReduceX) { |
| 74 | + const xAXis = generate1DArray({ |
| 75 | + min: originalMinX, |
| 76 | + max: originalMaxX, |
| 77 | + numberOfPoints: nbPointsX, |
| 78 | + }); |
| 79 | + for (let row = 0; row < nbPointsY; row++) { |
| 80 | + const output = xyEquallySpaced( |
| 81 | + { x: xAXis, y: z[row] }, |
| 82 | + { |
| 83 | + numberOfPoints: targetPointX, |
| 84 | + from: minX, |
| 85 | + to: maxX, |
| 86 | + }, |
| 87 | + ); |
| 88 | + reducedX[row] = Float64Array.from(output.y); |
| 89 | + } |
| 90 | + } else { |
| 91 | + reducedX = structuredClone(z); |
| 92 | + } |
| 93 | + |
| 94 | + if (!shouldReduceY) { |
| 95 | + return { |
| 96 | + ...data, |
| 97 | + minX, |
| 98 | + maxX, |
| 99 | + minY, |
| 100 | + maxY, |
| 101 | + z: reducedX, |
| 102 | + }; |
| 103 | + } |
| 104 | + const yAXis = generate1DArray({ |
| 105 | + min: originalMinY, |
| 106 | + max: originalMaxY, |
| 107 | + numberOfPoints: nbPointsY, |
| 108 | + }); |
| 109 | + |
| 110 | + const reducedMatrix: Float64Array[] = []; |
| 111 | + |
| 112 | + for (let i = 0; i < targetPointY; i++) { |
| 113 | + reducedMatrix[i] = new Float64Array(targetPointX); |
| 114 | + } |
| 115 | + |
| 116 | + const colBuffer = new Float64Array(nbPointsY); |
| 117 | + |
| 118 | + //Reduce over y dimension |
| 119 | + for (let col = 0; col < targetPointX; col++) { |
| 120 | + for (let row = 0; row < nbPointsY; row++) { |
| 121 | + colBuffer[row] = reducedX[row][col]; |
| 122 | + } |
| 123 | + const output = xyEquallySpaced( |
| 124 | + { x: yAXis, y: colBuffer }, |
| 125 | + { |
| 126 | + numberOfPoints: targetPointY, |
| 127 | + from: minY, |
| 128 | + to: maxY, |
| 129 | + }, |
| 130 | + ); |
| 131 | + |
| 132 | + //resample column into the matrix |
| 133 | + for (let row = 0; row < targetPointY; row++) { |
| 134 | + reducedMatrix[row][col] = output.y[row]; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return { |
| 139 | + ...data, |
| 140 | + minX, |
| 141 | + maxX, |
| 142 | + minY, |
| 143 | + maxY, |
| 144 | + z: reducedMatrix, |
| 145 | + }; |
| 146 | +} |
| 147 | + |
| 148 | +interface Generate1DArray { |
| 149 | + min: number; |
| 150 | + max: number; |
| 151 | + numberOfPoints: number; |
| 152 | +} |
| 153 | + |
| 154 | +function generate1DArray(options: Generate1DArray) { |
| 155 | + const { min, max, numberOfPoints } = options; |
| 156 | + const array = new Float64Array(numberOfPoints); |
| 157 | + const step = (max - min) / (numberOfPoints - 1); |
| 158 | + for (let i = 0; i < numberOfPoints; i++) { |
| 159 | + array[i] = min + i * step; |
| 160 | + } |
| 161 | + |
| 162 | + return array; |
| 163 | +} |
0 commit comments