Skip to content
Open
84 changes: 83 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"lodash": "^4.18.1",
"mf-parser": "^3.9.3",
"ml-array-mean": "^2.0.0",
"ml-array-median": "^2.0.0",
"ml-conrec": "^6.0.0",
"ml-gsd": "^14.3.0",
"ml-peak-shape-generator": "^5.6.0",
Expand Down Expand Up @@ -155,6 +154,7 @@
"react-router-dom": "^7.18.5",
"rimraf": "^6.1.4",
"serve": "^14.2.8",
"spectrum-generator": "^8.2.1",
"stylelint": "^18.0.0",
"stylelint-config-standard": "^41.0.0",
"typescript": "~6.0.3",
Expand Down
5 changes: 3 additions & 2 deletions src/component/reducer/actions/RangesActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
updateRangesRelativeValues,
} from '../../../data/data1d/Spectrum1D/index.js';
import type { ChangeRangeRelativeValueProps } from '../../../data/data1d/Spectrum1D/ranges/changeRangeRelativeValue.js';
import { RANGE_PEAK_NOISE_FACTOR } from '../../../data/data1d/Spectrum1D/ranges/constants.js';
import type { DetectRangesOptions } from '../../../data/data1d/Spectrum1D/ranges/detectRanges.js';
import { unlink } from '../../../data/utilities/RangeUtilities.js';
import { isProton } from '../../../data/utilities/isProton.js';
Expand Down Expand Up @@ -252,7 +253,7 @@ function handleAutoRangesDetection(
smoothY: false,
sensitivity: 100,
broadWidth: 0.05,
thresholdFactor: 8,
thresholdFactor: RANGE_PEAK_NOISE_FACTOR,
minMaxRatio,
maxAbsoluteRatio: minMaxRatio,
direction: lookNegative ? 'both' : 'positive',
Expand All @@ -273,7 +274,7 @@ function handleAutoRangesDetection(
function handleAutoSpectraRangesDetection(draft: Draft<State>) {
const peakPicking: DetectRangesOptions['peakPicking'] = {
sensitivity: 100,
thresholdFactor: 8,
thresholdFactor: RANGE_PEAK_NOISE_FACTOR,
minMaxRatio: 0.05,
};
const { data, molecules } = draft;
Expand Down
4 changes: 2 additions & 2 deletions src/data/data1d/Spectrum1D/peaks/autoPeakPicking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Peak1D } from '@zakodium/nmr-types';
import type { Spectrum1D } from '@zakodium/nmrium-core';
import median from 'ml-array-median';
import { xAbsoluteMedian } from 'ml-spectra-processing';
import type { OptionsXYAutoPeaksPicking } from 'nmr-processing';
import { mapPeaks, xyAutoPeaksPicking } from 'nmr-processing';

Expand Down Expand Up @@ -30,7 +30,7 @@ export function autoPeakPicking(
defaultPeakShape = DEFAULT_PEAK_SHAPE,
} = options;
// we calculate the noise but this could be improved
const noise = median(spectrum.data.re.map((y) => Math.abs(y)));
const noise = xAbsoluteMedian(spectrum.data.re);

let { re, x } = spectrum.data;
const { originFrequency: frequency } = spectrum.info;
Expand Down
95 changes: 95 additions & 0 deletions src/data/data1d/Spectrum1D/ranges/__tests__/detectSignal.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { DataXY } from 'cheminfo-types';
import { rangesToXY } from 'nmr-processing';
import { generateSpectrum } from 'spectrum-generator';
import { describe, expect, it } from 'vitest';

import detectSignals from '../detectSignals.js';
Expand Down Expand Up @@ -33,3 +35,96 @@ describe('lineBroadening', () => {
expect(result?.js[0].coupling).toBeCloseTo(7.2, 2);
});
});

const FREQUENCY = 400;
const FROM = -0.5;
const TO = 10.5;
const NB_POINTS = 64 * 1024;
function buildSpectrum(peaks: Array<{ x: number; y: number; width: number }>) {
return generateSpectrum(peaks, {
generator: { from: FROM, to: TO, nbPoints: NB_POINTS },
noise: { distribution: 'uniform', percent: 0.2, seed: 123_456_789 },
});
}

function getSignals(spectrum: DataXY<Float64Array>, from: number, to: number) {
return detectSignals(spectrum, {
from,
to,
frequency: FREQUENCY,
nucleus: '1H',
}) as Array<{ delta: number; multiplicity?: string; peaks?: unknown[] }>;
}

describe('signal detection inside a range (issue #4435)', () => {
it('detects a signal in a narrow range', () => {
const spectrum = buildSpectrum([
{ x: 8.265, y: 1000, width: 0.002 },
{ x: 3.32, y: 200, width: 0.003 },
]);
const signals = getSignals(spectrum, 8.2625, 8.2675);
expect(signals.length).toBeGreaterThanOrEqual(1);
expect(signals[0].delta).toBeCloseTo(8.265, 2);
});

it('detects weak lines when they are the tallest point of their own range', () => {
const spectrum = buildSpectrum([
{ x: 7.968, y: 50, width: 0.002 },
{ x: 7.972, y: 50, width: 0.002 },
]);
const signals = getSignals(spectrum, 7.964, 7.975);
expect(signals.length).toBeGreaterThanOrEqual(1);
});

it('detects the signals when the strongest point of the range is at its edge', () => {
const spectrum = buildSpectrum([
{ x: 7.9595, y: 1000, width: 0.003 },
{ x: 7.968, y: 50, width: 0.002 },
{ x: 7.972, y: 50, width: 0.002 },
]);
const signals = getSignals(spectrum, 7.962, 7.975);
expect(signals.length).toBeGreaterThanOrEqual(1);
});

it.each([0.0005, 0.001, 0.002, 0.003, 0.005, 0.008, 0.01, 0.02])(
'never leaves a range without signals when it contains a peak (width %f ppm)',
(width) => {
const spectrum = buildSpectrum([{ x: 8.265, y: 1000, width: 0.002 }]);
const signals = getSignals(
spectrum,
8.265 - width / 2,
8.265 + width / 2,
);
expect(signals.length).toBeGreaterThanOrEqual(1);
},
);

it('keeps the multiplet analysis of a well resolved range', () => {
const spectrum = buildSpectrum([
{ x: 8.256, y: 1000, width: 0.0015 },
{ x: 8.262, y: 2000, width: 0.0015 },
{ x: 8.268, y: 1000, width: 0.0015 },
]);
const signals = getSignals(spectrum, 8.25, 8.27);
expect(signals).toHaveLength(1);
expect(signals[0].multiplicity).toBe('t');
expect(signals[0].delta).toBeCloseTo(8.262, 2);
});

it('keeps a signal when the range only contains the flank of a broad peak', () => {
// No local maximum inside the range: the detection must still report
// something (peak picking, multiplet analysis or the fallback).
const spectrum = buildSpectrum([{ x: 7.962, y: 1000, width: 0.02 }]);
const signals = getSignals(spectrum, 7.964, 7.978);
expect(signals.length).toBeGreaterThanOrEqual(1);
});

it('returns no signal for a range that only contains noise', () => {
const spectrum = buildSpectrum([
{ x: 8.265, y: 1000, width: 0.002 },
{ x: 3.32, y: 200, width: 0.003 },
]);
const signals = getSignals(spectrum, 5.1, 5.12);
expect(signals).toHaveLength(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Logger } from 'cheminfo-types';
import { generateSpectrum } from 'spectrum-generator';
import { describe, expect, it, vi } from 'vitest';

import detectSignals from '../detectSignals.js';

// The multiplet analysis is forced to return nothing so that the fallback of
// `detectSignals` is exercised on its own, without depending on the ability of
// the peak picking to find a local maximum in the range.
vi.mock('../detectSignalsByMultipletAnalysis.js', () => ({
detectSignalsByMultipletAnalysis: vi.fn(() => []),
}));

const FREQUENCY = 400;
const FROM = -0.5;
const TO = 10.5;
const NB_POINTS = 64 * 1024;
function buildSpectrum(peaks: Array<{ x: number; y: number; width: number }>) {
return generateSpectrum(peaks, {
generator: { from: FROM, to: TO, nbPoints: NB_POINTS },
noise: { distribution: 'uniform', percent: 0.2, seed: 123_456_789 },
});
}

describe('detectSignals fallback', () => {
it('reports the point of maximum intensity when nothing can be detected', () => {
const spectrum = buildSpectrum([{ x: 8.265, y: 1000, width: 0.002 }]);
const warn = vi.fn();
const signals = detectSignals(spectrum, {
from: 8.26,
to: 8.27,
frequency: FREQUENCY,
nucleus: '1H',
logger: { warn } as unknown as Logger,
}) as Array<{
delta: number;
multiplicity?: string;
kind?: string;
peaks?: unknown[];
}>;

expect(signals).toHaveLength(1);
expect(signals[0].delta).toBeCloseTo(8.265, 2);
expect(signals[0].multiplicity).toBe('');
expect(signals[0].kind).toBe('signal');
expect(signals[0].peaks).toHaveLength(0);
// the user is informed that the multiplicity could not be determined
expect(warn).toHaveBeenCalledOnce();
});

it('stays empty when the range only contains noise', () => {
const spectrum = buildSpectrum([{ x: 8.265, y: 1000, width: 0.002 }]);
const warn = vi.fn();
const signals = detectSignals(spectrum, {
from: 5.1,
to: 5.12,
frequency: FREQUENCY,
nucleus: '1H',
logger: { warn } as unknown as Logger,
});

expect(signals).toHaveLength(0);
expect(warn).not.toHaveBeenCalled();
});
});
4 changes: 4 additions & 0 deletions src/data/data1d/Spectrum1D/ranges/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Shared noise threshold factor for range signal and range peak picking.
*/
export const RANGE_PEAK_NOISE_FACTOR = 8;
Loading
Loading