-
Notifications
You must be signed in to change notification settings - Fork 12
/
xyFilterTopYValues.ts
46 lines (40 loc) · 1.04 KB
/
xyFilterTopYValues.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
import { DataXY } from 'cheminfo-types';
import { xSortDescending } from '../x';
/**
* Filter an array x/y based on various criteria x points are expected to be sorted
*
* @param data - object containing 2 properties x and y
* @param nbPeaks
* @return filtered data
*/
export function xyFilterTopYValues(data: DataXY, nbPeaks?: number) {
if (typeof nbPeaks === 'undefined') return data;
if (nbPeaks > data.x.length) return data;
const { x, y } = data;
const newX: number[] = [];
const newY: number[] = [];
const descending = xSortDescending(y.slice());
const threshold = descending[nbPeaks - 1];
let nbThreshold = 0;
for (let i = 0; i < nbPeaks; i++) {
if (descending[i] === threshold) {
nbThreshold++;
}
}
for (let i = 0; i < x.length; i++) {
if (y[i] > threshold) {
newX.push(x[i]);
newY.push(y[i]);
} else if (y[i] === threshold) {
nbThreshold--;
if (nbThreshold >= 0) {
newX.push(x[i]);
newY.push(y[i]);
}
}
}
return {
x: newX,
y: newY,
};
}