Skip to content

Commit 0eb38ac

Browse files
committed
Tests for mass and NMR spectra
1 parent 4d1700b commit 0eb38ac

File tree

9 files changed

+90
-51
lines changed

9 files changed

+90
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Global Spectra Deconvolution + Peak optimizer
44

55
## Example
6-
``
6+
```
77
var CC = require('chemcalc');
88
var Stat = require('ml-stat');
99
var peakPicking = require("../src/index");

dist/ml-gsd.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,30 @@ return /******/ (function(modules) { // webpackBootstrap
6969
var Opt = __webpack_require__(2);
7070

7171
function sampleFunction(from, to, x, y, lastIndex){
72-
//console.log(from+" "+to);
73-
//console.log(lastIndex+" "+x[lastIndex[0]]);
7472
var nbPoints = x.length;
7573
var sampleX = [];
7674
var sampleY = [];
7775
var direction = Math.sign(x[1]-x[0]);//Direction of the derivative
78-
var delta = (to-from)/2;
76+
if(direction==-1){
77+
lastIndex[0]= x.length-1;
78+
}
79+
80+
var delta = Math.abs(to-from)/2;
7981
var mid = (from+to)/2;
8082
var stop = false;
8183
var index = lastIndex[0];
82-
while(!stop&&index<nbPoints){
84+
while(!stop&&index<nbPoints&&index>=0){
8385
if(Math.abs(x[index]-mid)<=delta){
8486
sampleX.push(x[index]);
8587
sampleY.push(y[index]);
86-
index++;
87-
88+
index+=direction;
8889
}
8990
//It is outside the range.
9091
else{
9192

92-
if(Math.sign(mid-x[index])==direction){
93+
if(Math.sign(mid-x[index])==1){
9394
//We'll reach the mid going in the current direction
94-
index++;
95+
index+=direction;
9596
}
9697
else{
9798
//There is not more peaks in the current range
@@ -104,20 +105,19 @@ return /******/ (function(modules) { // webpackBootstrap
104105
return [sampleX, sampleY];
105106
}
106107

107-
function optimizePeaks(peakList, x, y, n, fnType){
108+
function optimizePeaks(peakList,x,y,n, fnType){
108109
var i, j, lastIndex=[0];
109110
var groups = groupPeaks(peakList,n);
110111
var result = [];
111112
var factor = 1;
112113
if(fnType=="gaussian")
113114
factor = 1.17741;//From https://en.wikipedia.org/wiki/Gaussian_function#Properties
114-
//console.log(x[0]+" "+x[1]);
115115
for(i=0;i<groups.length;i++){
116-
//console.log(peakList[i]);
117116
var peaks = groups[i].group;
118117
if(peaks.length>1){
119118
//Multiple peaks
120119
//console.log("Pending group of overlaped peaks "+peaks.length);
120+
//console.log("here1");
121121
//console.log(groups[i].limits);
122122
var sampling = sampleFunction(groups[i].limits[0]-groups[i].limits[1],groups[i].limits[0]+groups[i].limits[1],x,y,lastIndex);
123123
//console.log(sampling);
@@ -133,8 +133,9 @@ return /******/ (function(modules) { // webpackBootstrap
133133
optPeaks = Opt.optimizeLorentzianSum(sampling, peaks, opts);
134134
}
135135
}
136+
//console.log(optPeak);
136137
for(j=0;j<optPeaks.length;j++){
137-
result.push({x:optPeaks[j][0],y:optPeaks[j][1],width:optPeaks[j][2]*factor});
138+
result.push({x:optPeaks[j][0][0],y:optPeaks[j][1][0],width:optPeaks[j][2][0]*factor});
138139
}
139140
}
140141
}
@@ -143,7 +144,8 @@ return /******/ (function(modules) { // webpackBootstrap
143144
peaks = peaks[0];
144145
var sampling = sampleFunction(peaks.x-n*peaks.width,
145146
peaks.x+n*peaks.width,x,y,lastIndex);
146-
//console.log(sampling);
147+
//console.log("here2");
148+
//console.log(groups[i].limits);
147149
if(sampling[0].length>5){
148150
var error = peaks.width/1000;
149151
var opts = [ 3, 100, error, error, error, error*10, error*10, 11, 9, 1 ];
@@ -157,7 +159,8 @@ return /******/ (function(modules) { // webpackBootstrap
157159
var optPeak = Opt.optimizeSingleLorentzian([sampling[0],sampling[1]], peaks, opts);
158160
}
159161
}
160-
result.push({x:optPeak[0],y:optPeak[1],width:optPeak[2]*factor}); // From https://en.wikipedia.org/wiki/Gaussian_function#Properties}
162+
//console.log(optPeak);
163+
result.push({x:optPeak[0][0],y:optPeak[1][0],width:optPeak[2][0]*factor}); // From https://en.wikipedia.org/wiki/Gaussian_function#Properties}
161164
}
162165
}
163166

dist/ml-gsd.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ml-gsd.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/ethylvinylether.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var peakPicking = require("../src/index");
5+
6+
describe('Global spectra deconvolution NMR spectra', function () {
7+
8+
// Test case obtained from Pag 443, Chap 8.
9+
it('Ethylvinylether should have 21 peaks', function () {
10+
var spectrum=JSON.parse(fs.readFileSync('./test//ethylvinylether.json', 'utf-8'));
11+
var result = peakPicking.gsd(spectrum[0],spectrum[1], {noiseLevel: 1049200.537996172, minMaxRatio:0.03});
12+
});
13+
});

test/ethylvinylether.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test/massPeakPicking.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,47 @@ var spectrum=CC.analyseMF("Cl2.Br2", {isotopomers:'arrayXXYY', fwhm:0.01, gaussi
99
var xy=spectrum.arrayXXYY;
1010
var x=xy[0];
1111
var y=xy[1];
12-
//console.log(y);
12+
var max =Stat.array.max(y);
1313
var noiseLevel=Stat.array.median(y.filter(function(a) {return (a>0)}))*3;
14+
/*
15+
69.938 100
16+
71.935 63.99155
17+
73.932 10.2373
18+
157.837 51.39931
19+
159.835 100
20+
161.833 48.63878
1421
15-
var result=peakPicking.gsd(x, y, {noiseLevel: noiseLevel, minMaxRatio:0, broadRatio:0,smoothY:false});
16-
console.log("Before optmization");
17-
console.log(result);
18-
result = peakPicking.optimize(result,x,y,1,"gaussian");
19-
console.log("After optmization");
20-
console.log(result);
22+
*/
2123

22-
describe.only('Check the peak picking of a simulated mass spectrum', function () {
24+
describe('Check the peak picking of a simulated mass spectrum', function () {
2325

2426
it('Check result', function () {
27+
var result=peakPicking.gsd(x, y, {noiseLevel: noiseLevel, minMaxRatio:0, broadRatio:0,smoothY:false});
28+
result = peakPicking.optimize(result,x,y,1,"gaussian");
2529

30+
result[0].x.should.approximately(69.938,0.02);
31+
result[0].y.should.approximately(max,0.01);
32+
result[0].width.should.approximately(0.01,5e-4);
33+
34+
result[1].x.should.approximately(71.935,0.02);
35+
result[1].y.should.approximately(63.99155*max/100,0.01);
36+
result[1].width.should.approximately(0.01,5e-4);
37+
38+
result[2].x.should.approximately(73.932,0.02);
39+
result[2].y.should.approximately(10.2373*max/100,0.01);
40+
result[2].width.should.approximately(0.01,5e-4);
41+
42+
result[3].x.should.approximately(157.837,0.02);
43+
result[3].y.should.approximately(51.39931*max/100,0.01);
44+
result[3].width.should.approximately(0.01,5e-4);
45+
46+
result[4].x.should.approximately(159.835,0.02);
47+
result[4].y.should.approximately(max,0.01);
48+
result[4].width.should.approximately(0.01,5e-4);
49+
50+
result[5].x.should.approximately(161.833,0.02);
51+
result[5].y.should.approximately(48.63878*max/100,0.01);
52+
result[5].width.should.approximately(0.01,5e-4);
2653
});
2754
});
2855

test/test.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
'use strict';
22

33
var fs = require('fs');
4-
var gsd = require("..");
5-
6-
7-
8-
9-
10-
describe('Global spectra deconvolution', function () {
11-
12-
var spectrum=JSON.parse(fs.readFileSync('./test//C2.json', 'utf-8'));
13-
14-
4+
var peakPicking = require("../src/index");
155

6+
describe('Global spectra deconvolution simple simulated spectrum', function () {
167

178
// Test case obtained from Pag 443, Chap 8.
189
it('Should provide the right result ...', function () {
10+
var spectrum=JSON.parse(fs.readFileSync('./test//C2.json', 'utf-8'));
11+
var result = peakPicking.gsd(spectrum[0],spectrum[1], {noiseLevel: 0.001, minMaxRatio:0});
1912

13+
result[0].x.should.approximately(24,0.02);
14+
result[0].y.should.approximately(0.09394372786996513,0.0005);
15+
//result[0].width.should.approximately(0.008,5e-4);
2016

21-
var result = gsd(spectrum[0],spectrum[1], {noiseLevel: 0.001, minMaxRatio:0});
22-
//console.log(result);
17+
result[1].x.should.approximately(25,0.02);
18+
result[1].y.should.approximately(0.0020321396708958394,0.0005);
19+
//result[1].width.should.approximately(0.006,5e-4);
2320

2421
});
2522
});

test/ubiquitin.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,32 @@ var parser = require('xy-parser');
99
var Stat = require('ml-stat');
1010

1111

12-
13-
14-
describe('Global spectra deconvolution2', function () {
12+
describe('Global spectra deconvolution HR mass spectra', function () {
1513

1614

1715
var spectrum=parser.parse(fs.readFileSync('./test/ubiquitin.txt', 'utf-8'), {arrayType: 'xxyy'});
1816
var d = new Date();
1917
var n = d.getTime();
2018
//var spectrum=parser.parse(fs.readFileSync('./ubiquitin.txt', 'utf-8'), {arrayType: 'xxyy'});
2119
d = new Date();
22-
console.log("Parsing time: "+(d.getTime()-n));
20+
//console.log("Parsing time: "+(d.getTime()-n));
2321
var noiseLevel=Stat.array.max(spectrum[1])*0.015;
2422

2523
var result = peakPicking.gsd(spectrum[0],spectrum[1], {noiseLevel: noiseLevel, minMaxRatio:0, broadRatio:0,smoothY:false});
2624
//console.log(result);
2725
d = new Date();
28-
console.log("Parsing + gsd time: "+(d.getTime()-n));
26+
//console.log("Parsing + gsd time: "+(d.getTime()-n));
2927

30-
result = peakPicking.optimize(result,spectrum[0],spectrum[1],1,"gaussian");
31-
d = new Date();
32-
console.log("Parsing + gsd + optimization time: "+(d.getTime()-n));
28+
//result = peakPicking.optimize(result,spectrum[0],spectrum[1],1,"gaussian");
29+
//d = new Date();
30+
//console.log("Parsing + gsd + optimization time: "+(d.getTime()-n));
3331

3432

35-
/* for(var i=0;i<result.length;i++){
33+
//for(var i=0;i<result.length;i++){
3634
//if(result[i].width<0.05)
37-
console.log(result[i].x+" "+result[i].y+" "+result[i].width);
38-
}
39-
console.log(result.length);*/
35+
// console.log(result[i].x+" "+result[i].y+" "+result[i].width);
36+
//}
37+
//console.log(result.length);*/
4038

4139
// Test case obtained from Pag 443, Chap 8.
4240
it('Should provide the right result ...', function () {

0 commit comments

Comments
 (0)