-
Notifications
You must be signed in to change notification settings - Fork 12
/
xyMaxMerge.ts
52 lines (46 loc) · 1.27 KB
/
xyMaxMerge.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
import { DataXY } from 'cheminfo-types';
interface XYMaxMergeOptions {
/**
* Window for abscissas to merge
* @default 0.001
*/
groupWidth?: number;
}
/**
* Merge abscissas values on similar ordinates and weight the group of abscissas
*
* @param data - object containing 2 properties x and y
* @param options - options
* @return array of merged and weighted points
*/
export function xyMaxMerge(
data: DataXY,
options: XYMaxMergeOptions = {},
): DataXY<number[]> {
const { x, y } = data;
const { groupWidth = 0.001 } = options;
const merged: { x: number[]; y: number[] } = { x: [], y: [] };
const maxAbscissa: { x: number[]; y: number[] } = { x: [], y: [] };
let size = 0;
let index = 0;
while (index < x.length) {
if (size === 0 || x[index] - merged.x[size - 1] > groupWidth) {
maxAbscissa.x.push(x[index]);
maxAbscissa.y.push(y[index]);
merged.x.push(x[index]);
merged.y.push(y[index]);
index++;
size++;
} else {
if (y[index] > maxAbscissa.y[size - 1]) {
maxAbscissa.x[size - 1] = x[index];
maxAbscissa.y[size - 1] = y[index];
}
merged.x[size - 1] = x[index];
merged.y[size - 1] += y[index];
index++;
}
}
merged.x = maxAbscissa.x.slice();
return merged;
}