-
Notifications
You must be signed in to change notification settings - Fork 8
/
gsd.ts
246 lines (218 loc) · 6.02 KB
/
gsd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { v4 as generateID } from '@lukeed/uuid';
import type { DataXY } from 'cheminfo-types';
import { sgg, SGGOptions } from 'ml-savitzky-golay-generalized';
import {
xIsEquallySpaced,
xIsMonotonic,
xNoiseStandardDeviation,
xMinMaxValues,
} from 'ml-spectra-processing';
import { GSDPeak } from './GSDPeak';
import { MakeMandatory } from './utils/MakeMandatory';
import { optimizeTop } from './utils/optimizeTop';
export interface GSDOptions {
/**
* Options for the Savitzky-Golay generalised algorithm. This algorithm is used
* to calculate the first ans second derivative.
* It is also used when smoothY:true to smooth the spectrum for peak picking.
* The Y values in case of smoothY is true will therefore be lower.
* @default {windowSize:9,polynomial:3}
*/
sgOptions?: SGGOptions;
/**
* Select the peak intensities from a smoothed version of the independent variables
* @default false
*/
smoothY?: boolean;
/**
* Peaks are local maxima (true) or minima (false)
* @default true
*/
maxCriteria?: boolean;
/**
* Peak under the noiseLevel (or over in case of maxCriteria=false) are ignored.
*/
noiseLevel?: number;
/**
* Minimal height of small peaks based on the ratio between min and max
* @default 0.00025
*/
minMaxRatio?: number;
/**
* Use a quadratic optimizations with the peak and its 3 closest neighbors
* @default false
*/
realTopDetection?: boolean;
}
export type GSDPeakID = MakeMandatory<GSDPeak, 'id'>;
/**
* Global spectra deconvolution
* @param data - Object data with x and y arrays. Values in x has to be growing
* @param {number} [options.broadRatio = 0.00] - If `broadRatio` is higher than 0, then all the peaks which second derivative
* smaller than `broadRatio * maxAbsSecondDerivative` will be marked with the soft mask equal to true.
*/
export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeakID[] {
let { noiseLevel } = options;
const {
sgOptions = {
windowSize: 9,
polynomial: 3,
},
smoothY = false,
maxCriteria = true,
minMaxRatio = 0.00025,
realTopDetection = false,
} = options;
const { x } = data;
let { y } = data;
if (xIsMonotonic(x) !== 1) {
throw new Error('GSD only accepts monotone increasing x values');
}
//rescale;
y = y.slice();
// If the max difference between delta x is less than 5%, then,
// we can assume it to be equally spaced variable
const equallySpaced = xIsEquallySpaced(x);
if (noiseLevel === undefined) {
if (equallySpaced) {
const noiseInfo = xNoiseStandardDeviation(y);
if (maxCriteria) {
noiseLevel = noiseInfo.median + 1.5 * noiseInfo.sd;
} else {
noiseLevel = -noiseInfo.median + 1.5 * noiseInfo.sd;
}
} else {
noiseLevel = 0;
}
} else if (!maxCriteria) {
noiseLevel *= -1;
}
if (!maxCriteria) {
for (let i = 0; i < y.length; i++) {
y[i] *= -1;
}
}
if (noiseLevel !== undefined) {
for (let i = 0; i < y.length; i++) {
if (y[i] < noiseLevel) {
y[i] = noiseLevel;
}
}
}
const xValue = equallySpaced ? x[1] - x[0] : x;
const yData = smoothY
? sgg(y, xValue, {
...sgOptions,
derivative: 0,
})
: y;
const dY = sgg(y, xValue, {
...sgOptions,
derivative: 1,
});
const ddY = sgg(y, xValue, {
...sgOptions,
derivative: 2,
});
const { min: minY, max: maxY } = xMinMaxValues(yData);
if (minY > maxY || minY === maxY) return [];
const yThreshold = minY + (maxY - minY) * minMaxRatio;
const dX = x[1] - x[0];
interface XIndex {
x: number;
index: number;
}
let lastMax: XIndex | null = null;
let lastMin: XIndex | null = null;
const minddY: number[] = [];
const intervalL: XIndex[] = [];
const intervalR: XIndex[] = [];
// By the intermediate value theorem We cannot find 2 consecutive maximum or minimum
for (let i = 1; i < yData.length - 1; ++i) {
if (
(dY[i] < dY[i - 1] && dY[i] <= dY[i + 1]) ||
(dY[i] <= dY[i - 1] && dY[i] < dY[i + 1])
) {
lastMin = {
x: x[i],
index: i,
};
if (dX > 0 && lastMax !== null) {
intervalL.push(lastMax);
intervalR.push(lastMin);
}
}
// Maximum in first derivative
if (
(dY[i] >= dY[i - 1] && dY[i] > dY[i + 1]) ||
(dY[i] > dY[i - 1] && dY[i] >= dY[i + 1])
) {
lastMax = {
x: x[i],
index: i,
};
if (dX < 0 && lastMin !== null) {
intervalL.push(lastMax);
intervalR.push(lastMin);
}
}
// Minimum in second derivative
if (ddY[i] < ddY[i - 1] && ddY[i] < ddY[i + 1]) {
minddY.push(i);
}
}
let lastK = -1;
const peaks: GSDPeakID[] = [];
for (const minddYIndex of minddY) {
const deltaX = x[minddYIndex];
let possible = -1;
let k = lastK + 1;
let minDistance = Number.POSITIVE_INFINITY;
let currentDistance = 0;
while (possible === -1 && k < intervalL.length) {
currentDistance = Math.abs(
deltaX - (intervalL[k].x + intervalR[k].x) / 2,
);
if (currentDistance < (intervalR[k].x - intervalL[k].x) / 2) {
possible = k;
lastK = k;
}
++k;
// Not getting closer?
if (currentDistance >= minDistance) {
break;
}
minDistance = currentDistance;
}
if (possible !== -1) {
if (yData[minddYIndex] > yThreshold) {
const width = Math.abs(intervalR[possible].x - intervalL[possible].x);
peaks.push({
id: generateID(),
x: deltaX,
y: yData[minddYIndex],
width,
index: minddYIndex,
ddY: ddY[minddYIndex],
inflectionPoints: {
from: intervalL[possible],
to: intervalR[possible],
},
});
}
}
}
if (realTopDetection) {
optimizeTop({ x, y: yData }, peaks);
}
peaks.forEach((peak) => {
if (!maxCriteria) {
peak.y *= -1;
peak.ddY = peak.ddY * -1;
}
});
peaks.sort((a, b) => {
return a.x - b.x;
});
return peaks;
}