Skip to content

Commit 944eae2

Browse files
authored
Remove Tree dependencies (#58)
1 parent 1a1a198 commit 944eae2

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

lib/model/hdbscan.js

+19-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Tree from '../util/tree.js'
2-
31
/**
42
* Hierarchical Density-based spatial clustering of applications with noise
53
*/
@@ -82,7 +80,7 @@ export default class HDBSCAN {
8280

8381
const tree = []
8482
for (let i = 0; i < n; i++) {
85-
tree[i] = new Tree({ point: datas[i], index: [i], distance: 0 })
83+
tree[i] = { index: [i], distance: 0, children: [] }
8684
}
8785
while (tree.length > 1) {
8886
let min_i = -1
@@ -107,13 +105,11 @@ export default class HDBSCAN {
107105
}
108106
dmreach[min_i].splice(min_j, 1)
109107
dmreach.splice(min_j, 1)
110-
tree[min_i] = new Tree(
111-
{
112-
distance: min_d,
113-
index: [...tree[min_i].value.index, ...tree[min_j].value.index],
114-
},
115-
[tree[min_i], tree[min_j]]
116-
)
108+
tree[min_i] = {
109+
distance: min_d,
110+
index: [...tree[min_i].index, ...tree[min_j].index],
111+
children: [tree[min_i], tree[min_j]],
112+
}
117113
tree.splice(min_j, 1)
118114
}
119115

@@ -122,29 +118,29 @@ export default class HDBSCAN {
122118
const clusters = []
123119
while (stack.length > 0) {
124120
let [tree, ctree] = stack.pop()
125-
ctree.index = tree.value.index
126-
ctree.lbirth = 1 / tree.value.distance
121+
ctree.index = tree.index
122+
ctree.lbirth = 1 / tree.distance
127123
ctree.stability = 0
128124
ctree.children = []
129125
clusters.push(ctree)
130126
while (true) {
131-
for (let i = tree.length - 1; i >= 0; i--) {
132-
const c = tree.at(i)
133-
if (c.value.index.length < this._minClusterSize) {
134-
tree.removeAt(i)
135-
if (c.value.distance > 0) {
136-
ctree.stability += c.value.index.length * (1 / c.value.distance - ctree.lbirth)
127+
for (let i = tree.children.length - 1; i >= 0; i--) {
128+
const c = tree.children[i]
129+
if (c.index.length < this._minClusterSize) {
130+
tree.children.splice(i, 1)
131+
if (c.distance > 0) {
132+
ctree.stability += c.index.length * (1 / c.distance - ctree.lbirth)
137133
}
138134
}
139135
}
140-
if (tree.length === 0) {
136+
if (tree.children.length === 0) {
141137
break
142-
} else if (tree.length === 1) {
143-
tree = tree.at(0)
138+
} else if (tree.children.length === 1) {
139+
tree = tree.children[0]
144140
} else {
145-
for (let i = 0; i < tree.length; i++) {
141+
for (let i = 0; i < tree.children.length; i++) {
146142
const ct = {}
147-
stack.push([tree.at(i), (ctree.children[i] = ct)])
143+
stack.push([tree.children[i], (ctree.children[i] = ct)])
148144
}
149145
break
150146
}

0 commit comments

Comments
 (0)