Skip to content

Commit 70776d3

Browse files
authored
feat: update ml-peak-shape-generator (#44)
2 parents 026a377 + 9d446a9 commit 70776d3

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
],
3636
"author": "Andres Castillo",
3737
"contributors": [
38-
"Miguel Asencio <[email protected]> (https://github.com/maasencioh)"
38+
"Miguel Asencio <[email protected]> (https://github.com/maasencioh)",
39+
"Alejandro Bolaños <[email protected]> (https://github.com/jobo322)"
3940
],
4041
"license": "MIT",
4142
"bugs": {
@@ -74,7 +75,7 @@
7475
"xy-parser": "^3.0.0"
7576
},
7677
"dependencies": {
77-
"ml-peak-shape-generator": "^0.10.2",
78+
"ml-peak-shape-generator": "^0.12.0",
7879
"ml-savitzky-golay-generalized": "2.0.3",
7980
"ml-spectra-fitting": "0.9.0",
8081
"ml-spectra-processing": "^4.9.4"

src/__tests__/simulated.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ const { generateSpectrum } = require('spectrum-generator');
55
describe('Global spectra deconvolution with simulated spectra', () => {
66
it('Overlapping peaks', () => {
77
const peaks = [
8-
{ x: -0.1, y: 0.2, width: 0.3 },
9-
{ x: 0.1, y: 0.2, width: 0.1 },
8+
{ x: -0.1, y: 0.2, width: 0.03 },
9+
{ x: 0.1, y: 0.2, width: 0.01 },
1010
];
1111

12-
const data = generateSpectrum(peaks, { from: -1, to: 1, nbPoints: 101 });
12+
const data = generateSpectrum(peaks, {
13+
from: -1,
14+
to: 1,
15+
nbPoints: 1001,
16+
factor: 6,
17+
});
1318

1419
let peakList = gsd(data, {
1520
minMaxRatio: 0,
@@ -23,18 +28,18 @@ describe('Global spectra deconvolution with simulated spectra', () => {
2328

2429
expect(peakList[0].x).toBeCloseTo(-0.1, 2);
2530
expect(peakList[0].y).toBeCloseTo(0.2, 2);
26-
expect(peakList[0].width).toBeCloseTo(0.3, 2);
31+
expect(peakList[0].width).toBeCloseTo(0.03, 2);
2732
expect(peakList[1].x).toBeCloseTo(0.1, 2);
2833
expect(peakList[1].y).toBeCloseTo(0.2, 2);
29-
expect(peakList[1].width).toBeCloseTo(0.1, 2);
34+
expect(peakList[1].width).toBeCloseTo(0.01, 2);
3035

3136
expect(optimizedPeaks[0].x).toBeCloseTo(-0.1, 2);
3237
expect(optimizedPeaks[0].y).toBeCloseTo(0.2, 2);
33-
expect(optimizedPeaks[0].width).toBeCloseTo(0.3, 2);
38+
expect(optimizedPeaks[0].width).toBeCloseTo(0.03, 2);
3439
expect(optimizedPeaks[0].group).toBe(0);
3540
expect(optimizedPeaks[1].x).toBeCloseTo(0.1, 2);
3641
expect(optimizedPeaks[1].y).toBeCloseTo(0.2, 2);
37-
expect(optimizedPeaks[1].width).toBeCloseTo(0.1, 2);
42+
expect(optimizedPeaks[1].width).toBeCloseTo(0.01, 2);
3843
expect(optimizedPeaks[1].group).toBe(1);
3944
});
4045

@@ -116,9 +121,6 @@ describe('Global spectra deconvolution with simulated spectra', () => {
116121
nbPoints: 10001,
117122
shape: {
118123
kind: 'gaussian',
119-
options: {
120-
fwhm: 10000,
121-
},
122124
},
123125
});
124126

src/gsd.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inflectionPointsWidthToFWHM } from 'ml-peak-shape-generator';
1+
import { getShapeGenerator } from 'ml-peak-shape-generator';
22
import SG from 'ml-savitzky-golay-generalized';
33

44
/**
@@ -167,12 +167,15 @@ export function gsd(data, options = {}) {
167167

168168
// Minimum in second derivative
169169
if (ddY[i] < ddY[i - 1] && ddY[i] < ddY[i + 1]) {
170-
// TODO should we change this to have 3 arrays ? Huge overhead creating arrays
171-
minddY.push(i); // ( [xData[i], yData[i], i] );
170+
minddY.push(i);
172171
broadMask.push(Math.abs(ddY[i]) <= broadRatio * maxDdy);
173172
}
174173
}
175174

175+
let widthProcessor = shape.kind
176+
? getShapeGenerator(shape.kind, shape.options).widthToFWHM
177+
: (x) => x;
178+
176179
let signals = [];
177180
let lastK = -1;
178181
let possible, frequency, distanceJ, minDistance, gettingCloser;
@@ -206,9 +209,7 @@ export function gsd(data, options = {}) {
206209
index: minddY[j],
207210
x: frequency,
208211
y: (yData[minddY[j]] + yCorrection.b) / yCorrection.m,
209-
width: shape.kind
210-
? inflectionPointsWidthToFWHM(width, shape.kind, shape.options)
211-
: width, // widthCorrection
212+
width: widthProcessor(width),
212213
soft: broadMask[j],
213214
});
214215

src/post/__tests__/groupPeaks.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('groupPeaks', () => {
77
{ x: 10, y: 10, width: 5 },
88
{ x: 30, y: 10, width: 5 },
99
]);
10-
expect(result).toEqual([
10+
expect(result).toStrictEqual([
1111
[
1212
{ x: 5, y: 10, width: 5, group: 0 },
1313
{ x: 10, y: 10, width: 5, group: 0 },
@@ -25,7 +25,7 @@ describe('groupPeaks', () => {
2525
],
2626
0.1,
2727
);
28-
expect(result).toEqual([
28+
expect(result).toStrictEqual([
2929
[{ x: 5, y: 10, width: 5, group: 0 }],
3030
[{ x: 10, y: 10, width: 5, group: 1 }],
3131
[{ x: 30, y: 10, width: 5, group: 2 }],
@@ -41,7 +41,7 @@ describe('groupPeaks', () => {
4141
],
4242
3,
4343
);
44-
expect(result).toEqual([
44+
expect(result).toStrictEqual([
4545
[
4646
{ x: 5, y: 10, width: 5, group: 0 },
4747
{ x: 10, y: 10, width: 5, group: 0 },
@@ -59,7 +59,7 @@ describe('groupPeaks', () => {
5959
],
6060
5,
6161
);
62-
expect(result).toEqual([
62+
expect(result).toStrictEqual([
6363
[
6464
{ x: 5, y: 10, width: 5, group: 0 },
6565
{ x: 10, y: 10, width: 5, group: 0 },

0 commit comments

Comments
 (0)