-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateTree.js
81 lines (72 loc) · 1.88 KB
/
createTree.js
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
import binarySearch from 'binary-search';
/**
* @typedef {import("../tree-similarity").Tree} Tree
* @typedef {import("../tree-similarity").CreateTreeOptions} CreateTreeOptions
* @typedef {import("cheminfo-types").DataXY} DataXY
* @typedef {import("cheminfo-types").DoubleArray} DoubleArray
*/
/**
* Function that creates the tree
* @param {DataXY} dataXY
* @param {CreateTreeOptions} [options]
* @return { Tree | null }
*/
export function createTree(dataXY, options = {}) {
const { x, y } = dataXY;
const {
minWindow = 0.16,
threshold = 0.01,
from = x[0],
to = x[x.length - 1],
} = options;
return mainCreateTree(x, y, from, to, minWindow, threshold);
}
/**
* recursive function to generate all the nodes in the tree
* @param {DoubleArray} x
* @param {DoubleArray} y
* @param {number} from
* @param {number} to
* @param {number} minWindow
* @param {number} threshold
* @returns {Tree | null}
*/
function mainCreateTree(x, y, from, to, minWindow, threshold) {
if (to - from < minWindow) {
return null;
}
// search first point
let start = binarySearch(x, from, (a, b) => a - b);
if (start < 0) {
start = ~start;
}
// stop at last point
let sum = 0;
let center = 0;
for (let i = start; i < x.length; i++) {
if (x[i] >= to) {
break;
}
sum += y[i];
center += x[i] * y[i];
}
if (sum < threshold) {
return null;
}
center /= sum;
if (center - from < 1e-6 || to - center < 1e-6) {
return null;
}
if (center - from < minWindow / 4) {
return mainCreateTree(x, y, center, to, minWindow, threshold);
} else if (to - center < minWindow / 4) {
return mainCreateTree(x, y, from, center, minWindow, threshold);
} else {
return {
sum,
center,
left: mainCreateTree(x, y, from, center, minWindow, threshold),
right: mainCreateTree(x, y, center, to, minWindow, threshold),
};
}
}