Skip to content

Commit f97285c

Browse files
authored
feat!: change peak shape structure (#68)
* feat!: change peak shape structure * use Peak1D interface and Shape1D in the peak structure * remove properties from output soft, noiseLevel, fromTo * check the peak list to homogenize fwhm n width in optimizePeaks * generate the soft mask in joinBroadPeaks * change input of joinBroadPeaks * use types from cheminfo-types * update ml-peak-shape-generator to 3.0.1 * fix: avoid unnecessary assertion * fix: update ml-peak-shape-generator to 3.0.2
1 parent e9e8522 commit f97285c

18 files changed

+359
-296
lines changed

examples/example.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { writeFileSync } from 'fs';
99
import { join } from 'path';
1010

1111
import { generateSpectrum } from 'spectrum-generator';
12-
13-
import { DataType, gsd, optimizePeaks } from '../src';
12+
import { gsd, optimizePeaks } from '../src';
1413

1514
const peaks = [
1615
{ x: -0.1, y: 0.2, width: 0.3 },
@@ -31,7 +30,7 @@ let peakList = gsd(original, {
3130
realTopDetection: false,
3231
smoothY: false,
3332
heightFactor: 1,
34-
shape: { kind: 'gaussian', width: 0 },
33+
shape: { kind: 'gaussian' },
3534
});
3635

3736
let optimizedPeaks = optimizePeaks(original, peakList);

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"cheminfo-build": "^1.1.11",
6464
"eslint": "^8.1.0",
6565
"eslint-config-cheminfo-typescript": "^10.2.2",
66+
"eslint-plugin-jest": "^25.2.4",
6667
"esm": "^3.2.25",
6768
"jest": "^27.3.1",
6869
"jest-matcher-deep-close-to": "^3.0.2",
@@ -71,16 +72,16 @@
7172
"nodemon": "^2.0.14",
7273
"prettier": "^2.4.1",
7374
"rimraf": "^3.0.2",
74-
"spectrum-generator": "^5.4.1",
75+
"spectrum-generator": "^6.0.0",
7576
"ts-jest": "^27.0.7",
7677
"typescript": "^4.4.4",
7778
"xy-parser": "^3.2.0"
7879
},
7980
"dependencies": {
80-
"cheminfo-types": "^0.7.0",
81-
"ml-peak-shape-generator": "^2.0.2",
81+
"cheminfo-types": "^0.8.0",
82+
"ml-peak-shape-generator": "^3.0.2",
8283
"ml-savitzky-golay-generalized": "2.0.3",
83-
"ml-spectra-fitting": "^2.0.0",
84+
"ml-spectra-fitting": "^3.0.0",
8485
"ml-spectra-processing": "^6.8.0"
8586
}
8687
}

src/__tests__/bigLowS2NSpectrum.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* Created by acastillo on 2/2/16.
3-
*/
4-
51
import { readFileSync } from 'fs';
62
import { join } from 'path';
73

@@ -18,11 +14,14 @@ describe('Global spectra deconvolution NMR spectra', () => {
1814
{
1915
noiseLevel: 57000.21889405926, // 1049200.537996172/2,
2016
minMaxRatio: 0.01,
21-
broadRatio: 0.0025,
2217
sgOptions: { windowSize: 13, polynomial: 3 },
2318
},
2419
);
25-
pp = joinBroadPeaks(pp, { width: 0.25 });
20+
21+
pp = joinBroadPeaks({ x: spectrum[0], y: spectrum[1] }, pp, {
22+
broadRatio: 0.0025,
23+
sgOptions: { windowSize: 13, polynomial: 3 },
24+
});
2625

2726
expect(pp).toHaveLength(91);
2827
});

src/__tests__/broadNMR.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,30 @@ describe('Global spectra deconvolution NMR spectra', () => {
1414
{
1515
noiseLevel: 1049200.537996172 / 2,
1616
minMaxRatio: 0.01,
17+
sgOptions: {
18+
windowSize: 9,
19+
polynomial: 3,
20+
},
21+
},
22+
);
23+
24+
const newResult = joinBroadPeaks(
25+
{ x: spectrum[0], y: spectrum[1] },
26+
result,
27+
{
28+
width: 0.25,
1729
broadRatio: 0.0025,
30+
shape: { kind: 'lorentzian' },
1831
sgOptions: {
1932
windowSize: 9,
2033
polynomial: 3,
2134
},
2235
},
2336
);
24-
const newResult = joinBroadPeaks(result, {
25-
width: 0.25,
26-
shape: { kind: 'lorentzian', width: 0 },
27-
});
2837
expect(newResult).toHaveLength(14);
2938
newResult.forEach((peak) => {
3039
if (Math.abs(peak.x - 4.31) < 0.01) {
31-
expect(peak.shape.width).toBeCloseTo(0.39, 2);
40+
expect(peak.width).toBeCloseTo(0.39, 2);
3241
}
3342
});
3443
});

src/__tests__/infraRed.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { readFileSync } from 'fs';
22

3-
import { DataType, gsd } from '../gsd';
3+
import type { DataXY } from 'cheminfo-types';
4+
5+
import { gsd } from '../gsd';
46

57
describe('Global spectra deconvolution Infrared spectra', () => {
68
// Test case obtained from Pag 443, Chap 8.
79
it('Should get the correct result', () => {
8-
let spectrum: DataType = JSON.parse(
10+
let spectrum: DataXY = JSON.parse(
911
readFileSync(`${__dirname}/data/infraRed.json`, 'utf-8'),
1012
);
1113
gsd(spectrum, {

src/__tests__/massPeakPicking.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import CC from 'chemcalc';
2+
import type { DataXY } from 'cheminfo-types';
3+
import { Gaussian } from 'ml-peak-shape-generator';
24
import Stat from 'ml-stat';
35

4-
import { DataType, gsd, optimizePeaks } from '..';
6+
import { gsd, optimizePeaks } from '..';
57

68
let spectrum = CC.analyseMF('Cl2.Br2', {
79
isotopomers: 'arrayXXYY',
810
fwhm: 0.01,
911
gaussianWidth: 11,
1012
});
11-
let xy: DataType = spectrum.arrayXXYY;
13+
let xy: DataXY = spectrum.arrayXXYY;
1214
let x: number[] = xy[0];
1315
let y: number[] = xy[1];
1416
let max = Stat.array.max(y);
@@ -30,7 +32,6 @@ describe('Check the peak picking of a simulated mass spectrum', () => {
3032
{
3133
noiseLevel: noiseLevel,
3234
minMaxRatio: 0,
33-
broadRatio: 0,
3435
smoothY: false,
3536
realTopDetection: true,
3637
},
@@ -39,31 +40,31 @@ describe('Check the peak picking of a simulated mass spectrum', () => {
3940
factorWidth: 4,
4041
shape: {
4142
kind: 'gaussian',
42-
width: 0,
4343
},
4444
});
4545
expect(result[0].x).toBeCloseTo(69.938, 1);
4646
expect(result[0].y).toBeCloseTo(max, 2);
47-
expect(result[0].shape.width).toBeCloseTo(0.01, 4);
47+
expect(result[0].fwhm).toBeCloseTo(0.01, 4);
48+
expect(result[0].width).toBeCloseTo(Gaussian.fwhmToWidth(0.01), 4);
4849

4950
expect(result[1].x).toBeCloseTo(71.935, 2);
5051
expect(result[1].y).toBeCloseTo((63.99155 * max) / 100, 3);
51-
expect(result[1].shape.width).toBeCloseTo(0.01, 4);
52+
expect(result[1].fwhm).toBeCloseTo(0.01, 4);
5253

5354
expect(result[2].x).toBeCloseTo(73.932, 1);
5455
expect(result[2].y).toBeCloseTo((10.2373 * max) / 100, 2);
55-
expect(result[2].shape.width).toBeCloseTo(0.01, 4);
56+
expect(result[2].fwhm).toBeCloseTo(0.01, 4);
5657

5758
expect(result[3].x).toBeCloseTo(157.837, 1);
5859
expect(result[3].y).toBeCloseTo((51.39931 * max) / 100, 2);
59-
expect(result[3].shape.width).toBeCloseTo(0.01, 4);
60+
expect(result[3].fwhm).toBeCloseTo(0.01, 4);
6061

6162
expect(result[4].x).toBeCloseTo(159.835, 1);
6263
expect(result[4].y).toBeCloseTo(max, 2);
63-
expect(result[4].shape.width).toBeCloseTo(0.01, 4);
64+
expect(result[4].fwhm).toBeCloseTo(0.01, 4);
6465

6566
expect(result[5].x).toBeCloseTo(161.833, 1);
6667
expect(result[5].y).toBeCloseTo((48.63878 * max) / 100, 2);
67-
expect(result[5].shape.width).toBeCloseTo(0.01, 4);
68+
expect(result[5].fwhm).toBeCloseTo(0.01, 4);
6869
});
6970
});

src/__tests__/simple.test.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Simple test cases', () => {
2626
negY.push(0);
2727
}
2828

29-
let widthToFWHM = getShape1D('gaussian').widthToFWHM;
29+
let widthToFWHM = getShape1D({ kind: 'gaussian' }).widthToFWHM;
3030

3131
it('gsd not realtop', () => {
3232
let peaks = gsd(
@@ -42,7 +42,6 @@ describe('Simple test cases', () => {
4242
);
4343

4444
expect(peaks[0].y).toBeCloseTo(4.657, 3);
45-
expect(peaks[0].shape.noiseLevel).toBeCloseTo(1.1956, 3);
4645
expect(peaks[0].x).toBeCloseTo(15, 2);
4746
});
4847

@@ -60,7 +59,6 @@ describe('Simple test cases', () => {
6059
},
6160
);
6261
expect(peaks[0].y).toBeCloseTo(-4.657, 3);
63-
expect(peaks[0].shape.noiseLevel).toBeCloseTo(1.1956, 3);
6462
expect(peaks[0].x).toBeCloseTo(15, 2);
6563
});
6664

@@ -82,13 +80,11 @@ describe('Simple test cases', () => {
8280
expect(peaks).toMatchCloseTo(
8381
[
8482
{
85-
shape: {
86-
noiseLevel: 1.2434539324230613,
87-
soft: false,
88-
width: widthToFWHM(3),
89-
},
83+
width: 3,
84+
fwhm: widthToFWHM(3),
9085
x: 15,
9186
y: 5,
87+
shape: { kind: 'gaussian' },
9288
},
9389
],
9490
2,
@@ -112,13 +108,11 @@ describe('Simple test cases', () => {
112108
expect(peaks).toMatchCloseTo(
113109
[
114110
{
115-
shape: {
116-
noiseLevel: 1.2434539324230613,
117-
soft: false,
118-
width: widthToFWHM(3),
119-
},
111+
width: 3,
112+
fwhm: widthToFWHM(3),
120113
x: 14.5,
121114
y: 4.006546067576939,
115+
shape: { kind: 'gaussian' },
122116
},
123117
],
124118
2,

src/__tests__/simpleShifted.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ describe('Simple shifted baseline test cases', () => {
3838
},
3939
},
4040
);
41-
let widthToFWHM = getShape1D('gaussian').widthToFWHM;
41+
let widthToFWHM = getShape1D({ kind: 'gaussian' }).widthToFWHM;
4242
expect(peaks).toHaveLength(1);
4343
expect(peaks[0]).toMatchCloseTo({
4444
x: 15,
4545
y: 4.657142857142857,
46-
shape: {
47-
width: widthToFWHM(2),
48-
soft: false,
49-
noiseLevel: 0.6695521174585716,
50-
},
46+
width: 2,
47+
fwhm: widthToFWHM(2),
48+
shape: { kind: 'gaussian' },
5149
});
5250
});
5351

@@ -64,16 +62,14 @@ describe('Simple shifted baseline test cases', () => {
6462
},
6563
},
6664
);
67-
let widthToFWHM = getShape1D('gaussian').widthToFWHM;
65+
let widthToFWHM = getShape1D({ kind: 'gaussian' }).widthToFWHM;
6866
expect(peaks).toHaveLength(1);
6967
expect(peaks[0]).toMatchCloseTo({
7068
x: 15,
7169
y: -4.657142857142857,
72-
shape: {
73-
width: widthToFWHM(2),
74-
soft: false,
75-
noiseLevel: 0.6695521174585716,
76-
},
70+
width: 2,
71+
fwhm: widthToFWHM(2),
72+
shape: { kind: 'gaussian' },
7773
});
7874
});
7975
});

0 commit comments

Comments
 (0)