Skip to content

Commit 0b1b867

Browse files
jobo322lpatiny
andauthored
fix: joinBroadPeaks n types (#91)
* fix: remove data from joinBroadPeaks close #89 * fix: use appendShapeAndFWHM n adapt types * fix: update cheminfo-types * chore: fix prettier * feat: use PeakXYWidth * fix(gsd): avoid falsy noiseLevel close #92 * fix: remove type definition file Co-authored-by: Luc Patiny <[email protected]>
1 parent cd65dbc commit 0b1b867

File tree

8 files changed

+31
-33
lines changed

8 files changed

+31
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@
8282
"ml-peak-shape-generator": "^4.0.3",
8383
"ml-savitzky-golay-generalized": "4.0.0",
8484
"ml-spectra-fitting": "^3.0.4",
85-
"ml-spectra-processing": "^10.1.2"
85+
"ml-spectra-processing": "^10.2.0"
8686
}
8787
}

src/__tests__/gaussian.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,12 @@ describe('smooth:false option', () => {
155155

156156
expect(peakList).toBeDeepCloseTo(expected);
157157
});
158+
159+
it('negative peaks with maxCriteria true', () => {
160+
let peakList = gsd(
161+
{ x: data.x, y: data.y.map((value) => -value) },
162+
{ maxCriteria: true },
163+
);
164+
expect(peakList).toHaveLength(0);
165+
});
158166
});

src/gsd.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
7171
if (!xIsMonotoneIncreasing(x)) {
7272
throw new Error('GSD only accepts monotone increasing x values');
7373
}
74+
//rescale;
7475
y = y.slice();
7576

7677
// If the max difference between delta x is less than 5%, then,
@@ -99,7 +100,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
99100
y[i] *= -1;
100101
}
101102
}
102-
if (noiseLevel) {
103+
if (noiseLevel !== undefined) {
103104
for (let i = 0; i < y.length; i++) {
104105
if (y[i] < noiseLevel) {
105106
y[i] = noiseLevel;
@@ -151,15 +152,12 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
151152

152153
const minY = xMinValue(yData);
153154
const maxY = xMaxValue(yData);
155+
156+
if (minY > maxY || minY === maxY) return [];
157+
154158
const yThreshold = minY + (maxY - minY) * minMaxRatio;
155159

156160
const dX = x[1] - x[0];
157-
let maxAbsDdy = 0;
158-
for (let i = 0; i < yData.length; i++) {
159-
if (Math.abs(ddY[i]) > maxAbsDdy) {
160-
maxAbsDdy = Math.abs(ddY[i]);
161-
}
162-
}
163161

164162
interface XIndex {
165163
x: number;
@@ -259,7 +257,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
259257

260258
peaks.forEach((peak) => {
261259
if (!maxCriteria) {
262-
peak.y = -peak.y;
260+
peak.y *= -1;
263261
peak.ddY = peak.ddY * -1;
264262
}
265263
});

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
export * from './gsd';
22
export * from './post/optimizePeaks';
3-
// joinBroadPeaks is not tested and should not be used
4-
// export * from './post/joinBroadPeaks';
3+
export * from './post/joinBroadPeaks';
54
export * from './post/broadenPeaks';
6-
5+
export * from './utils/appendShapeAndFWHM';
76
export * from './GSDBroadenPeak';
87
export * from './GSDPeak';
98
export * from './GSDPeakOptimized';

src/post/XYWidth.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/post/__tests__/joinBroadPeaks.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('joinBroadPeaks', () => {
2222
);
2323

2424
const peaks = gsd(data);
25-
const newPeaks = joinBroadPeaks(data, peaks, {
25+
const newPeaks = joinBroadPeaks(peaks, {
2626
broadRatio: 0.03,
2727
broadWidth: 0.25,
2828
});

src/post/joinBroadPeaks.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { DataXY } from 'cheminfo-types';
21
import type { Shape1D } from 'ml-peak-shape-generator';
32
import { OptimizationOptions } from 'ml-spectra-fitting';
43

5-
import { optimizePeaks } from '..';
4+
import { GSDPeakOptimized, optimizePeaks } from '..';
65
import { GSDPeak } from '../GSDPeak';
6+
import { appendShapeAndFWHM } from '../utils/appendShapeAndFWHM';
77

88
export interface JoinBroadPeaksOptions {
99
/**
@@ -31,7 +31,6 @@ export interface JoinBroadPeaksOptions {
3131
*/
3232

3333
export function joinBroadPeaks(
34-
data: DataXY,
3534
peakList: GSDPeak[],
3635
options: JoinBroadPeaksOptions = {},
3736
): GSDPeak[] {
@@ -45,8 +44,10 @@ export function joinBroadPeaks(
4544
let max = 0;
4645
let maxI = 0;
4746
let count = 1;
48-
const broadLines: any[] = [];
49-
const peaks: any[] = JSON.parse(JSON.stringify(peakList));
47+
const broadLines: GSDPeakOptimized[] = [];
48+
const peaks = appendShapeAndFWHM(peakList, { shape });
49+
50+
if (peaks.length < 2) return peaks;
5051

5152
let maxDdy = peakList[0].ddY;
5253
for (let i = 1; i < peakList.length; i++) {
@@ -59,8 +60,8 @@ export function joinBroadPeaks(
5960
}
6061
}
6162

62-
// Push a feke peak
63-
broadLines.push({ x: Number.MAX_VALUE, y: 0, width: 0 });
63+
//@ts-expect-error Push a feke peak
64+
broadLines.push({ x: Number.MAX_VALUE, y: 0 });
6465

6566
let candidates: { x: number[]; y: number[] } = {
6667
x: [broadLines[0].x],
@@ -90,10 +91,12 @@ export function joinBroadPeaks(
9091
],
9192
{ shape, optimization },
9293
);
94+
//@ts-expect-error type is equal as expected
9395
peaks.push(fitted[0]);
9496
} else {
95-
// Put back the candidates to the signals list
97+
// Put back the candidates to the peak list
9698
indexes.forEach((index) => {
99+
// @ts-expect-error todo 2
97100
peaks.push(broadLines[index]);
98101
});
99102
}

src/post/optimizePeaks.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DataXY } from 'cheminfo-types';
2+
import { PeakXYWidth } from 'cheminfo-types';
23
import { getShape1D, Shape1D } from 'ml-peak-shape-generator';
34
import { optimize } from 'ml-spectra-fitting';
45
import type { OptimizationOptions } from 'ml-spectra-fitting';
@@ -30,12 +31,6 @@ export interface OptimizePeaksOptions {
3031
optimization?: OptimizationOptions;
3132
}
3233

33-
interface XYWidth {
34-
x: number;
35-
y: number;
36-
width: number;
37-
}
38-
3934
/**
4035
* Optimize the position (x), max intensity (y), full width at half maximum (fwhm)
4136
* and the ratio of gaussian contribution (mu) if it's required. It currently supports three kind of shapes: gaussian, lorentzian and pseudovoigt
@@ -44,7 +39,7 @@ interface XYWidth {
4439
*/
4540
export function optimizePeaks(
4641
data: DataXY,
47-
peakList: XYWidth[],
42+
peakList: PeakXYWidth[],
4843
options: OptimizePeaksOptions = {},
4944
): GSDPeakOptimized[] {
5045
const {

0 commit comments

Comments
 (0)