Skip to content

Commit 82f46d9

Browse files
jobo322lpatiny
andauthored
Update spectra fitting (#25)
* chore: use cheminfo-build instead of cheminfo-tools * feat !: homogenized input data Default behavior: the x and y are inside an object {x, y} * feat: adapt test cases sd -> fwhm and input data * feat: update ml-spectra-fitting * feat: add joinPeaks options in optimizePeaks * feat: update documentation of joinBroadPeaks * chore: improve coverage * release-as: 5.0.1 Co-authored-by: Luc Patiny <[email protected]>
1 parent c643a17 commit 82f46d9

File tree

14 files changed

+178
-132
lines changed

14 files changed

+178
-132
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"jest": true
1313
},
1414
"scripts": {
15-
"build": "cheminfo-build --entry src/index.js --root GSD",
15+
"build": "rollup -c && cheminfo-build --entry src/index.js --root GSD",
1616
"eslint": "eslint src --cache",
1717
"eslint-fix": "npm run eslint -- --fix",
1818
"prepublishOnly": "rollup -c",
@@ -53,7 +53,7 @@
5353
"devDependencies": {
5454
"@babel/plugin-transform-modules-commonjs": "^7.12.1",
5555
"chemcalc": "^3.4.1",
56-
"cheminfo-tools": "^1.23.3",
56+
"cheminfo-build": "^1.1.8",
5757
"eslint": "^7.12.1",
5858
"eslint-config-cheminfo": "^5.2.2",
5959
"eslint-plugin-import": "^2.22.1",
@@ -68,6 +68,6 @@
6868
},
6969
"dependencies": {
7070
"ml-savitzky-golay-generalized": "2.0.2",
71-
"ml-spectra-fitting": "^0.3.2"
71+
"ml-spectra-fitting": "^0.5.0"
7272
}
7373
}

src/__tests__/bigLowS2NSpectrum.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ describe('Global spectra deconvolution NMR spectra', () => {
1313
readFileSync(join(__dirname, '/data/bigLowS2NSpectrum.json'), 'utf8'),
1414
);
1515

16-
let pp = gsd(spectrum[0], spectrum[1], {
17-
noiseLevel: 57000.21889405926, // 1049200.537996172/2,
18-
minMaxRatio: 0.01,
19-
broadRatio: 0.0025,
20-
sgOptions: { windowSize: 13, polynomial: 3 },
21-
});
16+
let pp = gsd(
17+
{ x: spectrum[0], y: spectrum[1] },
18+
{
19+
noiseLevel: 57000.21889405926, // 1049200.537996172/2,
20+
minMaxRatio: 0.01,
21+
broadRatio: 0.0025,
22+
sgOptions: { windowSize: 13, polynomial: 3 },
23+
},
24+
);
2225

2326
joinBroadPeaks(pp, { width: 0.25 });
2427

src/__tests__/broadNMR.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ describe('Global spectra deconvolution NMR spectra', () => {
99
let spectrum = JSON.parse(
1010
readFileSync(join(__dirname, '/data/broadNMR.json'), 'utf-8'),
1111
);
12-
let result = gsd(spectrum[0], spectrum[1], {
13-
noiseLevel: 1049200.537996172 / 2,
14-
minMaxRatio: 0.01,
15-
broadRatio: 0.0025,
16-
sgOptions: {
17-
windowSize: 9,
18-
polynomial: 3,
12+
let result = gsd(
13+
{ x: spectrum[0], y: spectrum[1] },
14+
{
15+
noiseLevel: 1049200.537996172 / 2,
16+
minMaxRatio: 0.01,
17+
broadRatio: 0.0025,
18+
sgOptions: {
19+
windowSize: 9,
20+
polynomial: 3,
21+
},
1922
},
20-
});
23+
);
2124
joinBroadPeaks(result, { width: 0.25 });
2225
expect(result).toHaveLength(14);
2326
result.forEach((peak) => {

src/__tests__/ethylvinylether.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ describe('Global spectra deconvolution NMR spectra', () => {
88
let spectrum = JSON.parse(
99
readFileSync(`${__dirname}/data/ethylvinylether.json`, 'utf-8'),
1010
);
11-
let result = gsd(spectrum[0], spectrum[1], {
12-
// noiseLevel: 1049200.537996172,
13-
minMaxRatio: 0.03,
14-
smoothY: false,
15-
realTopDetection: true,
16-
sgOptions: { windowSize: 5, polynomial: 3 },
17-
});
18-
// console.log(spectrum[1][13223]);
19-
// console.log(result);
11+
let result = gsd(
12+
{ x: spectrum[0], y: spectrum[1] },
13+
{
14+
// noiseLevel: 1049200.537996172,
15+
minMaxRatio: 0.03,
16+
smoothY: false,
17+
realTopDetection: true,
18+
sgOptions: { windowSize: 5, polynomial: 3 },
19+
},
20+
);
2021
expect(result).toHaveLength(21);
2122
});
2223
});

src/__tests__/infraRed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('Global spectra deconvolution Infrared spectra', function () {
88
let spectrum = JSON.parse(
99
readFileSync(`${__dirname}/data/infraRed.json`, 'utf-8'),
1010
);
11-
gsd(spectrum.x, spectrum.y, {
11+
gsd(spectrum, {
1212
noiseLevel: 32,
1313
minMaxRatio: 0.03,
1414
maxCriteria: false,

src/__tests__/massPeakPicking.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,42 @@ let noiseLevel = Stat.array.median(y.filter((a) => a > 0)) * 3;
2525

2626
describe('Check the peak picking of a simulated mass spectrum', () => {
2727
it('Check result', () => {
28-
let result = gsd(x, y, {
29-
noiseLevel: noiseLevel,
30-
minMaxRatio: 0,
31-
broadRatio: 0,
32-
smoothY: false,
33-
realTopDetection: true,
34-
});
35-
result = optimizePeaks(result, x, y, {
28+
let result = gsd(
29+
{ x, y },
30+
{
31+
noiseLevel: noiseLevel,
32+
minMaxRatio: 0,
33+
broadRatio: 0,
34+
smoothY: false,
35+
realTopDetection: true,
36+
},
37+
);
38+
result = optimizePeaks({ x, y }, result, {
3639
factorWidth: 1,
3740
functionName: 'gaussian',
3841
});
3942
expect(result[0].x).toBeCloseTo(69.938, 1);
4043
expect(result[0].y).toBeCloseTo(max, 2);
41-
expect(result[0].width).toBeCloseTo(0.0118, 4);
44+
expect(result[0].width).toBeCloseTo(0.01, 4);
4245

4346
expect(result[1].x).toBeCloseTo(71.935, 2);
4447
expect(result[1].y).toBeCloseTo((63.99155 * max) / 100, 3);
45-
expect(result[1].width).toBeCloseTo(0.0118, 4);
48+
expect(result[1].width).toBeCloseTo(0.01, 4);
4649

4750
expect(result[2].x).toBeCloseTo(73.932, 1);
4851
expect(result[2].y).toBeCloseTo((10.2373 * max) / 100, 2);
49-
expect(result[2].width).toBeCloseTo(0.0118, 4);
52+
expect(result[2].width).toBeCloseTo(0.01, 4);
5053

5154
expect(result[3].x).toBeCloseTo(157.837, 1);
5255
expect(result[3].y).toBeCloseTo((51.39931 * max) / 100, 2);
53-
expect(result[3].width).toBeCloseTo(0.0118, 4);
56+
expect(result[3].width).toBeCloseTo(0.01, 4);
5457

5558
expect(result[4].x).toBeCloseTo(159.835, 1);
5659
expect(result[4].y).toBeCloseTo(max, 2);
57-
expect(result[4].width).toBeCloseTo(0.0118, 4);
60+
expect(result[4].width).toBeCloseTo(0.01, 4);
5861

5962
expect(result[5].x).toBeCloseTo(161.833, 1);
6063
expect(result[5].y).toBeCloseTo((48.63878 * max) / 100, 2);
61-
expect(result[5].width).toBeCloseTo(0.0118, 4);
64+
expect(result[5].width).toBeCloseTo(0.01, 4);
6265
});
6366
});

src/__tests__/simple.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
11
import { gsd } from '..';
22

33
describe('Simple test cases', () => {
4-
let X = [];
5-
let Y = [];
4+
let x = [];
5+
let y = [];
66
for (let i = 0; i < 10; i++) {
7-
X.push(X.length);
8-
Y.push(0);
7+
x.push(x.length);
8+
y.push(0);
99
}
1010
for (let i = 0; i <= 10; i++) {
11-
X.push(X.length);
12-
Y.push(i > 5 ? 10 - i : i);
11+
x.push(x.length);
12+
y.push(i > 5 ? 10 - i : i);
1313
}
1414
for (let i = 0; i < 10; i++) {
15-
X.push(X.length);
16-
Y.push(0);
15+
x.push(x.length);
16+
y.push(0);
1717
}
1818

1919
it('gsd not realtop', () => {
20-
let peaks = gsd(X, Y, {
21-
realTopDetection: false,
22-
smoothY: true,
23-
sgOptions: {
24-
windowSize: 5,
25-
polynomial: 3,
20+
let peaks = gsd(
21+
{ x, y },
22+
{
23+
realTopDetection: false,
24+
smoothY: true,
25+
sgOptions: {
26+
windowSize: 5,
27+
polynomial: 3,
28+
},
2629
},
27-
});
30+
);
2831

2932
expect(peaks[0].y).toBeCloseTo(4.657, 3);
3033
expect(peaks[0].base).toBeCloseTo(1.1956, 3);
3134
expect(peaks[0].x).toStrictEqual(15);
3235
});
3336

3437
it('gsd not realtop asymetric', () => {
35-
let Y2 = Y.slice(0);
38+
let Y2 = y.slice(0);
3639
Y2[14] = 5;
37-
let peaks = gsd(X, Y2, {
38-
realTopDetection: false,
39-
smoothY: true,
40-
sgOptions: {
41-
windowSize: 5,
42-
polynomial: 3,
40+
let peaks = gsd(
41+
{ x, y: Y2 },
42+
{
43+
realTopDetection: false,
44+
smoothY: true,
45+
sgOptions: {
46+
windowSize: 5,
47+
polynomial: 3,
48+
},
4349
},
44-
});
50+
);
4551

4652
expect(peaks).toStrictEqual([
4753
{
@@ -64,16 +70,19 @@ describe('Simple test cases', () => {
6470
});
6571

6672
it('gsd realtop', () => {
67-
let Y2 = Y.slice();
73+
let Y2 = y.slice();
6874
Y2[14] = 5;
69-
let peaks = gsd(X, Y2, {
70-
realTopDetection: true,
71-
smoothY: false,
72-
sgOptions: {
73-
windowSize: 5,
74-
polynomial: 3,
75+
let peaks = gsd(
76+
{ x, y: Y2 },
77+
{
78+
realTopDetection: true,
79+
smoothY: false,
80+
sgOptions: {
81+
windowSize: 5,
82+
polynomial: 3,
83+
},
7584
},
76-
});
85+
);
7786
expect(peaks).toStrictEqual([
7887
{
7988
base: 1.2434539324230613,

src/__tests__/simulated.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
import { gsd } from '..';
1+
import { gsd, optimizePeaks } from '..';
22

3-
const { SpectrumGenerator } = require('spectrum-generator');
3+
const { generateSpectrum } = require('spectrum-generator');
44

55
describe('Global spectra deconvolution with simulated spectra', () => {
66
// Test case obtained from Pag 443, Chap 8.
77
it('Should provide the right result ...', () => {
8-
const sg = new SpectrumGenerator({ start: 0, end: 100, pointsPerUnit: 10 });
8+
const peaks = [
9+
{ x: 0.1, y: 0.2, width: 0.1 },
10+
{ x: -0.1, y: 0.2, width: 0.3 },
11+
];
912

10-
sg.addPeak([20, 100], { width: 5 });
11-
sg.addPeak([50, 50], { width: 5 });
12-
sg.addPeak([70, 20], { width: 5 });
13+
const data = generateSpectrum(peaks, { from: -1, to: 1, nbPoints: 101 });
1314

14-
let spectrum = sg.getSpectrum();
15-
16-
let result = gsd(spectrum.x, spectrum.y, {
15+
let peakList = gsd(data, {
1716
minMaxRatio: 0,
1817
realTopDetection: false,
1918
smoothY: false,
2019
});
2120

22-
expect(result[0]).toMatchObject({ x: 20, y: 100 });
23-
expect(result[1]).toMatchObject({ x: 50, y: 50 });
24-
expect(result[2]).toMatchObject({ x: 70, y: 20 });
21+
let optPeaks = optimizePeaks(data, peakList, {});
22+
23+
expect(optPeaks[0].x).toBeCloseTo(-0.1, 2);
24+
expect(optPeaks[0].y).toBeCloseTo(0.2, 2);
25+
expect(optPeaks[0].width).toBeCloseTo(0.3, 2);
26+
expect(optPeaks[1].x).toBeCloseTo(0.1, 2);
27+
expect(optPeaks[1].y).toBeCloseTo(0.2, 2);
28+
expect(optPeaks[1].width).toBeCloseTo(0.1, 2);
2529
});
2630
});

src/__tests__/test.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ describe('Global spectra deconvolution simple simulated spectrum', () => {
1515
let spectrum = JSON.parse(
1616
readFileSync(join(__dirname, '/data//C2.json'), 'utf8'),
1717
);
18-
let result = gsd(spectrum[0], spectrum[1], {
19-
// noiseLevel: 0.001,
20-
minMaxRatio: 0,
21-
realTopDetection: true,
22-
smoothY: false,
23-
});
18+
let result = gsd(
19+
{ x: spectrum[0], y: spectrum[1] },
20+
{
21+
// noiseLevel: 0.001,
22+
minMaxRatio: 0,
23+
realTopDetection: true,
24+
smoothY: false,
25+
},
26+
);
2427

2528
expect(result[0].x).toBeCloseTo(24, 2);
2629
expect(result[0].y).toBeCloseTo(0.09394372786996513, 5);
@@ -49,12 +52,15 @@ describe('Global spectra deconvolution simple simulated spectrum', () => {
4952
lorentzian(i, 9 * fourth) +
5053
2 * lorentzian(i, 10 * fourth);
5154
}
52-
let ans = gsd(times, tic, {
53-
noiseLevel: 0,
54-
realTopDetection: false,
55-
smoothY: false,
56-
sgOptions: { windowSize: 5, polynomial: 3 },
57-
});
55+
let ans = gsd(
56+
{ x: times, y: tic },
57+
{
58+
noiseLevel: 0,
59+
realTopDetection: false,
60+
smoothY: false,
61+
sgOptions: { windowSize: 5, polynomial: 3 },
62+
},
63+
);
5864

5965
expect(ans).toHaveLength(10);
6066
});

src/__tests__/ubiquitin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Global spectra deconvolution ubiquitin', () => {
1818
// console.log("Parsing time: "+(d.getTime()-n));
1919
let noiseLevel = 0; // Stat.array.max(spectrum[1])*0.015;
2020

21-
let result = gsd(spectrum.x, spectrum.y, {
21+
let result = gsd(spectrum, {
2222
noiseLevel: noiseLevel,
2323
minMaxRatio: 0.0,
2424
broadRatio: 0,

0 commit comments

Comments
 (0)