Skip to content

Commit a774e68

Browse files
authored
fix: improve zone detection typings (#3871)
1 parent c114785 commit a774e68

9 files changed

Lines changed: 254 additions & 251 deletions

File tree

package-lock.json

Lines changed: 177 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
"@emotion/react": "^11.14.0",
6969
"@emotion/styled": "^11.14.1",
7070
"@hookform/resolvers": "^5.2.2",
71-
"@zakodium/nmr-types": "^0.4.4",
72-
"@zakodium/nmrium-core": "^0.4.12",
73-
"@zakodium/nmrium-core-plugins": "^0.6.15",
71+
"@zakodium/nmr-types": "^0.4.5",
72+
"@zakodium/nmrium-core": "^0.4.13",
73+
"@zakodium/nmrium-core-plugins": "^0.6.17",
7474
"@zakodium/pdnd-esm": "^1.0.2",
7575
"@zip.js/zip.js": "^2.8.11",
7676
"cheminfo-font": "^1.13.1",
@@ -96,7 +96,7 @@
9696
"ml-spectra-processing": "^14.18.1",
9797
"ml-tree-similarity": "^2.2.0",
9898
"nmr-correlation": "2.3.3",
99-
"nmr-processing": "^21.2.2",
99+
"nmr-processing": "^21.2.4",
100100
"numeral": "^2.0.6",
101101
"openchemlib": "^9.18.2",
102102
"openchemlib-utils": "^8.8.1",
@@ -131,34 +131,34 @@
131131
"@types/lodash": "^4.17.21",
132132
"@types/node": "^24.10.1",
133133
"@types/numeral": "^2.0.5",
134-
"@types/papaparse": "^5.5.0",
134+
"@types/papaparse": "^5.5.1",
135135
"@types/react": "^18.3.13",
136136
"@types/react-dom": "^18.3.1",
137137
"@types/react-table": "^7.7.20",
138138
"@vitejs/plugin-react": "^5.1.1",
139139
"@vitest/coverage-v8": "^4.0.14",
140140
"@zakodium/tsconfig": "^1.0.2",
141141
"cross-env": "^10.1.0",
142-
"cspell": "^9.3.2",
142+
"cspell": "^9.4.0",
143143
"eslint": "^9.39.1",
144144
"eslint-config-cheminfo-react": "^19.0.0",
145145
"eslint-config-cheminfo-typescript": "^21.0.1",
146-
"knip": "^5.70.2",
146+
"knip": "^5.71.0",
147147
"modern-normalize": "^3.0.1",
148148
"postcss": "^8.5.6",
149149
"postcss-styled-syntax": "^0.7.1",
150-
"prettier": "^3.7.1",
150+
"prettier": "^3.7.3",
151151
"rc-menu": "^9.16.1",
152152
"react": "^18.3.1",
153153
"react-dom": "^18.3.1",
154154
"react-router-dom": "^7.9.6",
155155
"rimraf": "^6.1.2",
156156
"rollup-plugin-analyzer": "^4.0.0",
157157
"serve": "^14.2.5",
158-
"stylelint": "^16.26.0",
158+
"stylelint": "^16.26.1",
159159
"stylelint-config-standard": "^39.0.1",
160160
"typescript": "~5.9.3",
161-
"vite": "^7.2.4",
161+
"vite": "^7.2.6",
162162
"vitest": "^4.0.14"
163163
},
164164
"volta": {

src/component/reducer/actions/ZonesActions.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Filter2DEntry, SignalKind, Zone } from '@zakodium/nmr-types';
22
import type { Spectrum2D, ZonesViewState } from '@zakodium/nmrium-core';
33
import type { FromTo, NmrData2DFt } from 'cheminfo-types';
44
import type { Draft } from 'immer';
5-
import { original } from 'immer';
65
import lodashCloneDeep from 'lodash/cloneDeep.js';
76
import { setPathLength } from 'nmr-correlation';
87
import { Filters2DManager } from 'nmr-processing';
@@ -13,7 +12,6 @@ import {
1312
detectZonesManual,
1413
isSpectrum2D,
1514
} from '../../../data/data2d/Spectrum2D/index.js';
16-
import type { DetectionZonesOptions } from '../../../data/data2d/Spectrum2D/zones/getDetectionZones.js';
1715
import { unlink } from '../../../data/utilities/ZoneUtilities.js';
1816
import type { Axis } from '../../assignment/AssignmentsContext.js';
1917
import { defaultZonesViewState } from '../../hooks/useActiveSpectrumZonesViewState.js';
@@ -140,11 +138,10 @@ function handleAdd2dZone(draft: Draft<State>, action: Add2dZoneAction) {
140138

141139
const drawnZone = get2DRange(draft, action.payload);
142140

143-
const zones = detectZonesManual(original(spectrum), {
141+
const zones = detectZonesManual(spectrum, {
144142
selectedZone: drawnZone,
145143
thresholdFactor: draft.toolOptions.data.zonesNoiseFactor,
146144
maxPercentCutOff: draft.toolOptions.data.zonesMinMaxRatio,
147-
convolutionByFFT: false,
148145
});
149146

150147
spectrum.zones.values.push(...zones);
@@ -167,12 +164,11 @@ function handleAutoZonesDetection(
167164

168165
const [fromX, toX] = draft.xDomain;
169166
const [fromY, toY] = draft.yDomain;
170-
const detectionOptions: DetectionZonesOptions = {
167+
const zones = detectZones(spectrum, {
171168
selectedZone: { fromX, toX, fromY, toY },
172169
thresholdFactor,
173170
maxPercentCutOff,
174-
};
175-
const zones = detectZones(original(spectrum), detectionOptions);
171+
});
176172
spectrum.zones.values = spectrum.zones.values.concat(zones);
177173
handleUpdateCorrelations(draft);
178174
}
@@ -183,13 +179,11 @@ function handleAutoSpectraZonesDetection(draft: Draft<State>) {
183179
const { info, data } = datum;
184180
if (isSpectrum2D(datum) && info.isFt) {
185181
const { minX, maxX, minY, maxY } = (data as NmrData2DFt).rr;
186-
const detectionOptions = {
182+
const zones = detectZones(datum, {
187183
selectedZone: { fromX: minX, toX: maxX, fromY: minY, toY: maxY },
188184
thresholdFactor: 1,
189185
maxPercentCutOff: 0.03,
190-
};
191-
192-
const zones = detectZones(original(datum), detectionOptions);
186+
});
193187
datum.zones.values = datum.zones.values.concat(zones);
194188

195189
handleUpdateCorrelations(draft);

src/component/reducer/helper/get2DRange.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import type { Draft } from 'immer';
2+
13
import { get2DXScale, get2DYScale } from '../../2d/utilities/scale.js';
4+
import type { State } from '../Reducer.ts';
25

36
export interface ZoneBoundary {
47
startX: number;
@@ -7,7 +10,7 @@ export interface ZoneBoundary {
710
endY: number;
811
}
912

10-
export default function get2DRange(draft: any, options: ZoneBoundary) {
13+
export default function get2DRange(draft: Draft<State>, options: ZoneBoundary) {
1114
const { startX, startY, endX, endY } = options;
1215
const scaleX = get2DXScale(draft);
1316
const scaleY = get2DYScale(draft);

src/data/SpectraManager.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ export const DataExportOptions = {
6464
*/
6565
SELF_CONTAINED_EXTERNAL_DATASOURCE: 'SELF_CONTAINED_EXTERNAL_DATASOURCE',
6666
} as const;
67-
68-
// eslint-disable-next-line @typescript-eslint/no-redeclare
6967
export type DataExportOptions =
7068
(typeof DataExportOptions)[keyof typeof DataExportOptions];
7169

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { Zone } from '@zakodium/nmr-types';
2+
import type { Spectrum2D } from '@zakodium/nmrium-core';
23
import { mapZones } from 'nmr-processing';
34

45
import type { DetectionZonesOptions } from './getDetectionZones.js';
56
import { getDetectionZones } from './getDetectionZones.js';
67

78
export function detectZones(
8-
datum: any,
9+
datum: Spectrum2D,
910
options: DetectionZonesOptions,
1011
): Zone[] {
1112
const zones = getDetectionZones(datum, options);
1213

13-
return mapZones(zones as Zone[], datum);
14+
return mapZones(zones, datum);
1415
}
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import type { Zone } from '@zakodium/nmr-types';
2+
import type { Spectrum2D } from '@zakodium/nmrium-core';
23
import { mapZones } from 'nmr-processing';
34

45
import type { DetectionZonesOptions } from './getDetectionZones.js';
56
import { getDetectionZones } from './getDetectionZones.js';
67

8+
type DetectZonesManualOptions = Pick<
9+
DetectionZonesOptions,
10+
'selectedZone' | 'thresholdFactor' | 'maxPercentCutOff'
11+
>;
12+
713
/**
814
*
9-
* @param datum
10-
* @param {object} options
11-
* @param {object} options.selectedZone
12-
* @param {number} options.selectedZone.fromX
13-
* @param {number} options.selectedZone.fromY
14-
* @param {number} options.selectedZone.toX
15-
* @param {number} options.selectedZone.toY
16-
* @param {number} options.thresholdFactor
17-
* @param {boolean} options.convolutionByFFT
15+
* @param spectrum
16+
* @param options
1817
*/
19-
export function detectZonesManual(datum: any, options: DetectionZonesOptions) {
20-
const { spectralWidth, originFrequency } = datum.info;
21-
options.tolerances = spectralWidth.map(
22-
(sw: any, i: any) => sw * originFrequency[i],
23-
);
24-
const zones = getDetectionZones(datum, options);
18+
export function detectZonesManual(
19+
spectrum: Spectrum2D,
20+
options: DetectZonesManualOptions,
21+
): Zone[] {
22+
const { spectralWidth, originFrequency } = spectrum.info;
23+
const zones = getDetectionZones(spectrum, {
24+
...options,
25+
tolerances: spectralWidth.map((sw, i) => sw * originFrequency[i]),
26+
});
2527
let { fromX, fromY, toX, toY } = options.selectedZone;
2628
if (fromX > toX) [fromX, toX] = [toX, fromX];
2729
if (fromX > toX) [fromY, toY] = [toY, fromY];
@@ -30,5 +32,6 @@ export function detectZonesManual(datum: any, options: DetectionZonesOptions) {
3032
x: { from: fromX, to: toX },
3133
y: { from: fromY, to: toY },
3234
};
33-
return mapZones(zones as Zone[], datum);
35+
36+
return mapZones(zones, spectrum);
3437
}
Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { Info2D } from '@zakodium/nmr-types';
12
import type { Spectrum2D } from '@zakodium/nmrium-core';
3+
import type { NmrData2DContent, NmrData2DFt } from 'cheminfo-types';
24
import { xyzAutoZonesPicking } from 'nmr-processing';
35

46
export interface DetectionZonesOptions {
@@ -11,70 +13,46 @@ export interface DetectionZonesOptions {
1113
thresholdFactor: number;
1214
maxPercentCutOff: number;
1315
tolerances?: number[];
14-
convolutionByFFT?: boolean;
15-
enhanceSymmetry?: boolean;
1616
}
1717

1818
/**
1919
*
20-
* @param {object} options
21-
* @param {object} options.selectedZone
22-
* @param {number} options.selectedZone.fromX
23-
* @param {number} options.selectedZone.fromY
24-
* @param {number} options.selectedZone.toX
25-
* @param {number} options.selectedZone.toY
26-
* @param {number} options.thresholdFactor
27-
* @param {boolean} options.convolutionByFFT
20+
* @param spectrum
21+
* @param options
2822
*/
2923
export function getDetectionZones(
3024
spectrum: Spectrum2D,
3125
options: DetectionZonesOptions,
3226
) {
33-
let dataMatrix = {};
34-
const { selectedZone } = options;
35-
if (selectedZone) {
36-
options.enhanceSymmetry = false;
37-
dataMatrix = getSubMatrix(spectrum, selectedZone);
38-
} else {
39-
dataMatrix = spectrum.data;
40-
}
41-
42-
return autoZonesDetection(dataMatrix, {
43-
...options,
44-
info: spectrum.info,
45-
});
27+
const dataMatrix = getSubMatrix(spectrum, options.selectedZone);
28+
return autoZonesDetection(dataMatrix, spectrum.info, options);
4629
}
4730

48-
function autoZonesDetection(data: any, options: any) {
49-
const {
50-
clean,
51-
tolerances,
52-
thresholdFactor,
53-
maxPercentCutOff,
54-
convolutionByFFT,
55-
info: { nucleus: nuclei, originFrequency },
56-
} = options;
57-
58-
const { enhanceSymmetry = nuclei[0] === nuclei[1] } = options;
31+
function autoZonesDetection(
32+
data: NmrData2DContent,
33+
info: Info2D,
34+
options: DetectionZonesOptions,
35+
) {
36+
const { tolerances, thresholdFactor, maxPercentCutOff } = options;
37+
const { nucleus: nuclei, originFrequency } = info;
5938

60-
const zones = xyzAutoZonesPicking(data, {
39+
return xyzAutoZonesPicking(data, {
6140
nuclei,
6241
tolerances,
6342
observedFrequencies: originFrequency,
6443
thresholdFactor,
6544
realTopDetection: true,
66-
clean,
6745
maxPercentCutOff,
68-
enhanceSymmetry,
69-
convolutionByFFT,
46+
enhanceSymmetry: false,
7047
});
71-
72-
return zones;
7348
}
7449

75-
function getSubMatrix(datum: any, selectedZone: any) {
50+
function getSubMatrix(
51+
datum: Spectrum2D,
52+
selectedZone: DetectionZonesOptions['selectedZone'],
53+
): NmrData2DContent {
7654
const { fromX, toX, fromY, toY } = selectedZone;
77-
const data = datum.data.rr;
55+
const data = (datum.data as NmrData2DFt).rr;
7856
const xStep = (data.maxX - data.minX) / (data.z[0].length - 1);
7957
const yStep = (data.maxY - data.minY) / (data.z.length - 1);
8058
let xIndexFrom = Math.max(Math.floor((fromX - data.minX) / xStep), 0);
@@ -91,20 +69,14 @@ function getSubMatrix(datum: any, selectedZone: any) {
9169
if (xIndexFrom > xIndexTo) [xIndexFrom, xIndexTo] = [xIndexTo, xIndexFrom];
9270
if (yIndexFrom > yIndexTo) [yIndexFrom, yIndexTo] = [yIndexTo, yIndexFrom];
9371

94-
const dataMatrix: any = {
95-
z: [],
96-
maxX: data.minX + xIndexTo * xStep,
97-
minX: data.minX + xIndexFrom * xStep,
98-
maxY: data.minY + yIndexTo * yStep,
99-
minY: data.minY + yIndexFrom * yStep,
100-
};
101-
let maxZ = Number.MIN_SAFE_INTEGER;
102-
let minZ = Number.MAX_SAFE_INTEGER;
72+
const z: NmrData2DContent['z'] = [];
73+
let maxZ = Number.MAX_SAFE_INTEGER;
74+
let minZ = Number.MIN_SAFE_INTEGER;
10375

10476
const nbXPoints = xIndexTo - xIndexFrom + 1;
10577

10678
for (let j = yIndexFrom; j < yIndexTo; j++) {
107-
const row = new Float32Array(nbXPoints);
79+
const row = new Float64Array(nbXPoints);
10880
let xIndex = xIndexFrom;
10981
for (let i = 0; i < nbXPoints; i++) {
11082
row[i] = data.z[j][xIndex++];
@@ -113,9 +85,16 @@ function getSubMatrix(datum: any, selectedZone: any) {
11385
if (maxZ < rowValue) maxZ = rowValue;
11486
if (minZ > rowValue) minZ = rowValue;
11587
}
116-
dataMatrix.z.push(Array.from(row));
88+
z.push(row);
11789
}
118-
dataMatrix.minZ = minZ;
119-
dataMatrix.maxZ = maxZ;
120-
return dataMatrix;
90+
91+
return {
92+
z,
93+
maxX: data.minX + xIndexTo * xStep,
94+
minX: data.minX + xIndexFrom * xStep,
95+
maxY: data.minY + yIndexTo * yStep,
96+
minY: data.minY + yIndexFrom * yStep,
97+
maxZ,
98+
minZ,
99+
};
121100
}

src/data/data2d/Spectrum2D/zones/initiateZones.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function initiateZones(
1818
},
1919
options.zones,
2020
{
21-
values: mapZones(options?.zones?.values || [], spectrum),
21+
values: mapZones(options.zones?.values || [], spectrum),
2222
},
2323
);
2424
}

0 commit comments

Comments
 (0)