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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#[cfg(feature = "parallel")]
use crate::core_distances::parallel::CoreDistanceCalculatorPar;
#[cfg(feature = "serial")]
use crate::core_distances::serial::CoreDistanceCalculator;
use crate::data_wrappers::{CondensedNode, MSTEdge, SLTNode};
#[cfg(feature = "parallel")]
use crate::min_spanning_tree::parallel::PrimsMinSpanningTreePar;
#[cfg(feature = "serial")]
use crate::min_spanning_tree::serial::PrimsMinSpanningTree;
use crate::min_spanning_tree::MinSpanningTree;
use crate::union_find::UnionFind;
use crate::validation::DataValidator;
use crate::{distance, Center, DistanceMetric, HdbscanError, HdbscanHyperParams};
use num_traits::Float;
use std::collections::{HashMap, VecDeque};
use std::ops::Range;
type CondensedTree<T> = Vec<CondensedNode<T>>;
/// The HDBSCAN clustering algorithm in Rust. Generic over floating point numeric types.
#[derive(Debug, Clone, PartialEq)]
pub struct Hdbscan<'a, T> {
data: &'a [Vec<T>],
n_samples: usize,
hp: HdbscanHyperParams,
}
#[cfg(feature = "serial")]
impl<T: Float> Hdbscan<'_, T> {
/// Performs clustering on the list of vectors passed to the constructor.
///
/// # Returns
/// * A result that, if successful, contains a list of cluster labels, with a length equal to
/// the number of samples passed to the constructor. Positive integers mean a data point
/// belongs to a cluster of that label. -1 labels mean that a data point is noise and does
/// not belong to any cluster. An Error will be returned if the dimensionality of the input
/// vectors are mismatched, if any vector contains non-finite coordinates, or if the passed
/// data set is empty.
///
/// # Examples
/// ```
///use std::collections::HashSet;
///use hdbscan::Hdbscan;
///
///let data: Vec<Vec<f32>> = vec![
/// vec![1.5, 2.2],
/// vec![1.0, 1.1],
/// vec![1.2, 1.4],
/// vec![0.8, 1.0],
/// vec![1.1, 1.0],
/// vec![3.7, 4.0],
/// vec![3.9, 3.9],
/// vec![3.6, 4.1],
/// vec![3.8, 3.9],
/// vec![4.0, 4.1],
/// vec![10.0, 10.0],
///];
///let clusterer = Hdbscan::default_hyper_params(&data);
///let labels = clusterer.cluster().unwrap();
/// //First five points form one cluster
///assert_eq!(1, labels[..5].iter().collect::<HashSet<_>>().len());
/// // Next five points are a second cluster
///assert_eq!(1, labels[5..10].iter().collect::<HashSet<_>>().len());
/// // The final point is noise
///assert_eq!(-1, labels[10]);
/// ```
pub fn cluster(&self) -> Result<Vec<i32>, HdbscanError> {
DataValidator::new(self.data, &self.hp).validate_input_data()?;
let core_dist_calculator = CoreDistanceCalculator::new(self.data, &self.hp);
let core_distances = core_dist_calculator.calc_core_distances();
let mst_calculator =
PrimsMinSpanningTree::new(self.data, self.hp.dist_metric, &core_distances);
let min_spanning_tree = mst_calculator.compute();
let single_linkage_tree = self.make_single_linkage_tree(&min_spanning_tree);
let condensed_tree = self.condense_tree(&single_linkage_tree);
let winning_clusters = self.extract_winning_clusters(&condensed_tree);
let labelled_data = self.label_data(&winning_clusters, &condensed_tree);
Ok(labelled_data)
}
}
#[cfg(feature = "parallel")]
impl<T: Float + Send + Sync> Hdbscan<'_, T> {
/// Performs clustering on the list of vectors passed to the constructor, with some parallelism.
/// Not recommended for small or low dimension datasets.
///
/// # Returns
/// * A result that, if successful, contains a list of cluster labels, with a length equal to
/// the number of samples passed to the constructor. Positive integers mean a data point
/// belongs to a cluster of that label. -1 labels mean that a data point is noise and does
/// not belong to any cluster. An Error will be returned if the dimensionality of the input
/// vectors are mismatched, if any vector contains non-finite coordinates, or if the passed
/// data set is empty.
///
/// # Examples
/// ```
///use std::collections::HashSet;
///use hdbscan::Hdbscan;
///
///let data: Vec<Vec<f32>> = vec![
/// vec![1.5, 2.2],
/// vec![1.0, 1.1],
/// vec![1.2, 1.4],
/// vec![0.8, 1.0],
/// vec![1.1, 1.0],
/// vec![3.7, 4.0],
/// vec![3.9, 3.9],
/// vec![3.6, 4.1],
/// vec![3.8, 3.9],
/// vec![4.0, 4.1],
/// vec![10.0, 10.0],
///];
///let clusterer = Hdbscan::default_hyper_params(&data);
///let labels = clusterer.cluster_par().unwrap();
/// //First five points form one cluster
///assert_eq!(1, labels[..5].iter().collect::<HashSet<_>>().len());
/// // Next five points are a second cluster
///assert_eq!(1, labels[5..10].iter().collect::<HashSet<_>>().len());
/// // The final point is noise
///assert_eq!(-1, labels[10]);
/// ```
pub fn cluster_par(&self) -> Result<Vec<i32>, HdbscanError> {
DataValidator::new(self.data, &self.hp).validate_input_data()?;
let core_dist_calculator = CoreDistanceCalculatorPar::new(self.data, &self.hp);
let core_distances = core_dist_calculator.calc_core_distances();
let mst_calculator =
PrimsMinSpanningTreePar::new(self.data, self.hp.dist_metric, &core_distances);
let min_spanning_tree = mst_calculator.compute();
let single_linkage_tree = self.make_single_linkage_tree(&min_spanning_tree);
let condensed_tree = self.condense_tree(&single_linkage_tree);
let winning_clusters = self.extract_winning_clusters(&condensed_tree);
let labelled_data = self.label_data(&winning_clusters, &condensed_tree);
Ok(labelled_data)
}
}
impl<'a, T: Float> Hdbscan<'a, T> {
/// Creates an instance of HDBSCAN clustering model using a custom hyper parameter
/// configuration.
///
/// # Parameters
/// * `data` - a reference to the data to cluster, a collection of vectors of floating points
/// numbers. The vectors must all be of the same dimensionality and contain no
/// infinite values.
/// * `config` - the hyper parameter configuration.
///
/// # Returns
/// * The HDBSCAN model instance.
///
/// # Examples
/// ```
///use hdbscan::{DistanceMetric, Hdbscan, HdbscanHyperParams, NnAlgorithm};
///
///let data: Vec<Vec<f32>> = vec![
/// vec![1.3, 1.1],
/// vec![1.3, 1.2],
/// vec![1.0, 1.1],
/// vec![1.2, 1.2],
/// vec![0.9, 1.0],
/// vec![0.9, 1.0],
/// vec![3.7, 4.0],
/// vec![3.9, 3.9],
///];
///let config = HdbscanHyperParams::builder()
/// .min_cluster_size(3)
/// .min_samples(2)
/// .dist_metric(DistanceMetric::Manhattan)
/// .nn_algorithm(NnAlgorithm::BruteForce)
/// .build();
///let clusterer = Hdbscan::new(&data, config);
/// ```
pub fn new(data: &'a [Vec<T>], hyper_params: HdbscanHyperParams) -> Self {
let n_samples = data.len();
Hdbscan {
data,
n_samples,
hp: hyper_params,
}
}
#[deprecated(
since = "0.8.1",
note = "Please use `default_hyper_params` constructor instead"
)]
pub fn default(data: &'a [Vec<T>]) -> Hdbscan<'a, T> {
let hyper_params = HdbscanHyperParams::default();
Hdbscan::new(data, hyper_params)
}
/// Creates an instance of HDBSCAN clustering model using the default hyper parameters.
///
/// # Parameters
/// * `data` - a reference to the data to cluster, a collection of vectors of floating points
/// numbers. The vectors must all be of the same dimensionality and contain no
/// infinite values.
///
/// # Returns
/// * The HDBSCAN model instance.
///
/// # Examples
/// ```
///use hdbscan::Hdbscan;
///
///let data: Vec<Vec<f32>> = vec![
/// vec![1.3, 1.1],
/// vec![1.3, 1.2],
/// vec![1.0, 1.1],
/// vec![1.2, 1.2],
/// vec![0.9, 1.0],
/// vec![0.9, 1.0],
/// vec![3.7, 4.0],
/// vec![3.9, 3.9],
///];
///let clusterer = Hdbscan::default_hyper_params(&data);
/// ```
pub fn default_hyper_params(data: &'a [Vec<T>]) -> Hdbscan<'a, T> {
let hyper_params = HdbscanHyperParams::default();
Hdbscan::new(data, hyper_params)
}
/// Calculates the centers of the clusters just calculate.
///
/// # Parameters
/// * `center` - the type of center to calculate.
/// * `labels` - a reference to the labels calculated by a call to `Hdbscan::cluster`.
///
/// # Returns
/// * A vector of the cluster centers, of shape num clusters by num dimensions/features. The
/// index of the centroid is the cluster label. For example, the centroid cluster of label 0
/// will be the first centroid in the vector of centroids.
///
/// # Examples
/// ```
///use hdbscan::{Center, Hdbscan};
///
/// let data: Vec<Vec<f32>> = vec![
/// vec![1.5, 2.2],
/// vec![1.0, 1.1],
/// vec![1.2, 1.4],
/// vec![0.8, 1.0],
/// vec![1.1, 1.0],
/// vec![3.7, 4.0],
/// vec![3.9, 3.9],
/// vec![3.6, 4.1],
/// vec![3.8, 3.9],
/// vec![4.0, 4.1],
/// vec![10.0, 10.0],
///];
///let clusterer = Hdbscan::default_hyper_params(&data);
///let labels = clusterer.cluster().unwrap();
///let centroids = clusterer.calc_centers(Center::Centroid, &labels).unwrap();
///assert_eq!(2, centroids.len());
///assert!(centroids.contains(&vec![3.8, 4.0]) && centroids.contains(&vec![1.12, 1.34]));
/// ```
pub fn calc_centers(
&self,
center: Center,
labels: &[i32],
) -> Result<Vec<Vec<T>>, HdbscanError> {
if labels.len() != self.data.len() {
return Err(HdbscanError::WrongDimension(String::from(
"The length of the labels must equal the length of the original clustering data.",
)));
}
if self.hp.dist_metric != DistanceMetric::Haversine && center == Center::GeoCentroid {
// TODO: Implement a more appropriate error variant when doing a major version bump
return Err(HdbscanError::WrongDimension(String::from(
"Geographical centroids can only be used with geographical coordinates.",
)));
}
if self.hp.dist_metric == DistanceMetric::Precalculated {
// TODO: Implement a more appropriate error variant when doing a major version bump
return Err(HdbscanError::WrongDimension(String::from(
"Centroids cannot be calculated when using precalculated distances.",
)));
}
Ok(center.calc_centers(
self.data,
labels,
distance::get_dist_func(&self.hp.dist_metric),
))
}
fn make_single_linkage_tree(&self, min_spanning_tree: &[MSTEdge<T>]) -> Vec<SLTNode<T>> {
let mut single_linkage_tree: Vec<SLTNode<T>> = Vec::with_capacity(self.n_samples - 1);
let mut union_find = UnionFind::new(self.n_samples);
for mst_edge in min_spanning_tree.iter().take(self.n_samples - 1) {
let left_node = mst_edge.left_node_id;
let right_node = mst_edge.right_node_id;
let distance = mst_edge.distance;
let left_child = union_find.find(left_node);
let right_child = union_find.find(right_node);
let size = union_find.size_of(left_child) + union_find.size_of(right_child);
single_linkage_tree.push(SLTNode {
left_child,
right_child,
distance,
size,
});
union_find.union(left_child, right_child);
}
single_linkage_tree
}
fn condense_tree(&self, single_linkage_tree: &[SLTNode<T>]) -> CondensedTree<T> {
let top_node = (self.n_samples - 1) * 2;
let node_ids = self.find_single_linkage_children(single_linkage_tree, top_node);
let mut new_node_ids = vec![0_usize; top_node + 1];
new_node_ids[top_node] = self.n_samples;
let mut next_parent_id = self.n_samples + 1;
let mut visited = vec![false; node_ids.len()];
let mut condensed_tree = Vec::new();
for node_id in node_ids {
let has_been_visited = visited[node_id];
if has_been_visited || self.is_individual_sample(&node_id) {
continue;
}
let node = &single_linkage_tree[node_id - self.n_samples];
let left_child_id = node.left_child;
let right_child_id = node.right_child;
let lambda_birth = self.calc_lambda(node.distance);
let left_child_size = self.extract_cluster_size(left_child_id, single_linkage_tree);
let right_child_size = self.extract_cluster_size(right_child_id, single_linkage_tree);
let is_left_a_cluster = self.is_cluster_big_enough(left_child_size);
let is_right_a_cluster = self.is_cluster_big_enough(right_child_size);
match (is_left_a_cluster, is_right_a_cluster) {
(true, true) => {
for (child_id, child_size) in [left_child_id, right_child_id]
.iter()
.zip([left_child_size, right_child_size])
{
new_node_ids[*child_id] = next_parent_id;
next_parent_id += 1;
condensed_tree.push(CondensedNode {
node_id: new_node_ids[*child_id],
parent_node_id: new_node_ids[node_id],
lambda_birth,
size: child_size,
});
}
}
(false, false) => {
let new_node_id = new_node_ids[node_id];
self.add_children_to_tree(
left_child_id,
new_node_id,
single_linkage_tree,
&mut condensed_tree,
&mut visited,
lambda_birth,
);
self.add_children_to_tree(
right_child_id,
new_node_id,
single_linkage_tree,
&mut condensed_tree,
&mut visited,
lambda_birth,
);
}
(false, true) => {
new_node_ids[right_child_id] = new_node_ids[node_id];
self.add_children_to_tree(
left_child_id,
new_node_ids[node_id],
single_linkage_tree,
&mut condensed_tree,
&mut visited,
lambda_birth,
);
}
(true, false) => {
new_node_ids[left_child_id] = new_node_ids[node_id];
self.add_children_to_tree(
right_child_id,
new_node_ids[node_id],
single_linkage_tree,
&mut condensed_tree,
&mut visited,
lambda_birth,
);
}
}
}
condensed_tree
}
fn find_single_linkage_children(
&self,
single_linkage_tree: &[SLTNode<T>],
root: usize,
) -> Vec<usize> {
let mut process_queue = VecDeque::from([root]);
let mut child_nodes = Vec::new();
while !process_queue.is_empty() {
let mut current_node_id = match process_queue.pop_front() {
Some(node_id) => node_id,
None => break,
};
child_nodes.push(current_node_id);
if self.is_individual_sample(¤t_node_id) {
continue;
}
current_node_id -= self.n_samples;
let current_node = &single_linkage_tree[current_node_id];
process_queue.push_back(current_node.left_child);
process_queue.push_back(current_node.right_child);
}
child_nodes
}
fn is_individual_sample(&self, node_id: &usize) -> bool {
node_id < &self.n_samples
}
fn is_cluster(&self, node_id: &usize) -> bool {
!self.is_individual_sample(node_id)
}
fn calc_lambda(&self, dist: T) -> T {
if dist > T::zero() {
T::one() / dist
} else {
T::infinity()
}
}
fn extract_cluster_size(&self, node_id: usize, single_linkage_tree: &[SLTNode<T>]) -> usize {
if self.is_individual_sample(&node_id) {
1
} else {
single_linkage_tree[node_id - self.n_samples].size
}
}
fn is_cluster_big_enough(&self, cluster_size: usize) -> bool {
cluster_size >= self.hp.min_cluster_size
}
fn add_children_to_tree(
&self,
node_id: usize,
new_node_id: usize,
single_linkage_tree: &[SLTNode<T>],
condensed_tree: &mut CondensedTree<T>,
visited: &mut [bool],
lambda_birth: T,
) {
for child_id in self.find_single_linkage_children(single_linkage_tree, node_id) {
if self.is_individual_sample(&child_id) {
condensed_tree.push(CondensedNode {
node_id: child_id,
parent_node_id: new_node_id,
lambda_birth,
size: 1,
})
}
visited[child_id] = true
}
}
fn extract_winning_clusters(&self, condensed_tree: &CondensedTree<T>) -> Vec<usize> {
let (lower, upper) = self.get_cluster_id_bounds(condensed_tree);
let n_clusters = upper - lower;
let mut stabilities = self.calc_all_stabilities(lower..upper, condensed_tree);
let mut clusters: HashMap<usize, bool> =
stabilities.keys().map(|id| (*id, false)).collect();
for cluster_id in (lower..upper).rev() {
let stability = stabilities
.get(&cluster_id)
.expect("Couldn't retrieve stability");
let combined_child_stability = self
.get_immediate_child_clusters(cluster_id, condensed_tree)
.iter()
.map(|node| *stabilities.get(&node.node_id).unwrap_or(&T::zero()))
.fold(T::zero(), std::ops::Add::add);
if stability > &combined_child_stability
&& !self.is_cluster_too_big(&cluster_id, condensed_tree)
{
clusters.insert(cluster_id, true);
// If child clusters were already marked as winning clusters reverse
self.find_child_clusters(&cluster_id, condensed_tree)
.iter()
.for_each(|node_id| {
let is_child_selected = clusters.get(node_id);
if let Some(true) = is_child_selected {
clusters.insert(*node_id, false);
}
});
} else {
stabilities.insert(cluster_id, combined_child_stability);
}
}
let mut selected_cluster_ids = clusters
.into_iter()
.filter(|(_id, should_keep)| *should_keep)
.map(|(id, _should_keep)| id)
.collect();
if self.hp.epsilon != 0.0 && n_clusters > 0 {
selected_cluster_ids =
self.check_cluster_epsilons(selected_cluster_ids, condensed_tree);
}
selected_cluster_ids.sort();
selected_cluster_ids
}
fn get_cluster_id_bounds(&self, condensed_tree: &CondensedTree<T>) -> (usize, usize) {
if self.hp.allow_single_cluster {
let n_clusters = condensed_tree.len() - self.n_samples + 1;
(self.n_samples, self.n_samples + n_clusters)
} else {
let lower = self.n_samples + 1;
let n_clusters = condensed_tree.len() - self.n_samples;
(lower, lower + n_clusters)
}
}
fn calc_all_stabilities(
&self,
cluster_id_range: Range<usize>,
condensed_tree: &CondensedTree<T>,
) -> HashMap<usize, T> {
cluster_id_range
.map(|cluster_id| (cluster_id, self.calc_stability(cluster_id, condensed_tree)))
.collect()
}
fn calc_stability(&self, cluster_id: usize, condensed_tree: &CondensedTree<T>) -> T {
let lambda_birth = self.extract_lambda_birth(cluster_id, condensed_tree);
condensed_tree
.iter()
.filter(|node| node.parent_node_id == cluster_id)
.map(|node| (node.lambda_birth - lambda_birth) * T::from(node.size).unwrap_or(T::one()))
.fold(T::zero(), std::ops::Add::add)
}
fn extract_lambda_birth(&self, cluster_id: usize, condensed_tree: &CondensedTree<T>) -> T {
if self.is_top_cluster(&cluster_id) {
T::zero()
} else {
condensed_tree
.iter()
.find(|node| node.node_id == cluster_id)
.map(|node| node.lambda_birth)
.unwrap_or(T::zero())
}
}
fn is_top_cluster(&self, cluster_id: &usize) -> bool {
cluster_id == &self.n_samples
}
fn get_immediate_child_clusters<'b>(
&'b self,
cluster_id: usize,
condensed_tree: &'b CondensedTree<T>,
) -> Vec<&'b CondensedNode<T>> {
condensed_tree
.iter()
.filter(|node| node.parent_node_id == cluster_id)
.filter(|node| self.is_cluster(&node.node_id))
.collect()
}
fn is_cluster_too_big(&self, cluster_id: &usize, condensed_tree: &CondensedTree<T>) -> bool {
self.get_cluster_size(cluster_id, condensed_tree) > self.hp.max_cluster_size
}
fn get_cluster_size(&self, cluster_id: &usize, condensed_tree: &CondensedTree<T>) -> usize {
if self.hp.allow_single_cluster && self.is_top_cluster(cluster_id) {
condensed_tree
.iter()
.filter(|node| self.is_cluster(&node.node_id))
.filter(|node| &node.parent_node_id == cluster_id)
.map(|node| node.size)
.sum()
} else {
// All other clusters are in the tree with sizes
condensed_tree
.iter()
.find(|node| &node.node_id == cluster_id)
.map(|node| node.size)
.unwrap_or(1usize) // The cluster has to be in the tree
}
}
fn find_child_clusters(
&self,
root_node_id: &usize,
condensed_tree: &CondensedTree<T>,
) -> Vec<usize> {
let mut process_queue = VecDeque::from([root_node_id]);
let mut child_clusters = Vec::new();
while !process_queue.is_empty() {
let current_node_id = match process_queue.pop_front() {
Some(node_id) => node_id,
None => break,
};
for node in condensed_tree {
if self.is_individual_sample(&node.node_id) {
continue;
}
if node.parent_node_id == *current_node_id {
child_clusters.push(node.node_id);
process_queue.push_back(&node.node_id);
}
}
}
child_clusters
}
fn check_cluster_epsilons(
&self,
winning_clusters: Vec<usize>,
condensed_tree: &CondensedTree<T>,
) -> Vec<usize> {
let epsilon = T::from(self.hp.epsilon).expect("Couldn't convert f64 epsilon to T");
let mut processed: Vec<usize> = Vec::new();
let mut winning_epsilon_clusters = Vec::new();
for cluster_id in winning_clusters.iter() {
let cluster_epsilon = self.calc_cluster_epsilon(*cluster_id, condensed_tree, epsilon);
if cluster_epsilon < epsilon {
if processed.contains(cluster_id) {
continue;
}
let winning_cluster_id =
self.find_higher_node_sufficient_epsilon(*cluster_id, condensed_tree, epsilon);
winning_epsilon_clusters.push(winning_cluster_id);
for sub_node in self.find_child_clusters(&winning_cluster_id, condensed_tree) {
if sub_node != winning_cluster_id {
processed.push(sub_node)
}
}
} else {
winning_epsilon_clusters.push(*cluster_id);
}
}
winning_epsilon_clusters
}
fn find_higher_node_sufficient_epsilon(
&self,
starting_cluster_id: usize,
condensed_tree: &CondensedTree<T>,
epsilon: T,
) -> usize {
let mut current_id = starting_cluster_id;
let winning_cluster_id;
loop {
let parent_id = condensed_tree
.iter()
.find(|node| node.node_id == current_id)
.map(|node| node.parent_node_id)
// If the node isn't in the tree there must be only a single root cluster as
// this isn't stored explicitly in the tree. Its id is always max node id + 1
.unwrap_or(self.n_samples);
if self.is_top_cluster(&parent_id) {
if self.hp.allow_single_cluster {
winning_cluster_id = parent_id;
} else {
winning_cluster_id = current_id;
}
break;
}
let parent_epsilon = self.calc_cluster_epsilon(parent_id, condensed_tree, epsilon);
if parent_epsilon > epsilon {
winning_cluster_id = parent_id;
break;
}
current_id = parent_id;
}
winning_cluster_id
}
fn calc_cluster_epsilon(
&self,
cluster_id: usize,
condensed_tree: &CondensedTree<T>,
epsilon: T,
) -> T {
let cluster_lambda = condensed_tree
.iter()
.find(|node| node.node_id == cluster_id)
.map(|node| node.lambda_birth);
match cluster_lambda {
Some(lambda) => T::one() / lambda,
// Should be unreachable, but set to a value that will skip the cluster
None => epsilon - T::one(),
}
}
fn label_data(
&self,
winning_clusters: &[usize],
condensed_tree: &CondensedTree<T>,
) -> Vec<i32> {
// Assume all data points are noise by default then label the ones in clusters
let mut labels = vec![-1; self.n_samples];
let n_clusters = winning_clusters.len();
for (current_cluster_id, cluster_id) in winning_clusters.iter().enumerate() {
let node_size = self.get_cluster_size(cluster_id, condensed_tree);
self.find_child_samples(*cluster_id, node_size, n_clusters, condensed_tree)
.into_iter()
.for_each(|id| labels[id] = current_cluster_id as i32);
}
labels
}
fn find_child_samples(
&self,
root_node_id: usize,
node_size: usize,
n_clusters: usize,
condensed_tree: &CondensedTree<T>,
) -> Vec<usize> {
let mut process_queue = VecDeque::from([root_node_id]);
let mut child_nodes = Vec::with_capacity(node_size);
while !process_queue.is_empty() {
let current_node_id = match process_queue.pop_front() {
Some(node_id) => node_id,
None => break,
};
for node in condensed_tree {
// Skip nodes that aren't the child of this one
if node.parent_node_id != current_node_id {
continue;
}
// If node is a cluster, then its children need processing
if self.is_cluster(&node.node_id) {
process_queue.push_back(node.node_id);
continue;
}
// Finally, handle individual data points
if n_clusters == 1 && self.hp.allow_single_cluster {
let lambda_threshold = self.get_lambda_threshold(root_node_id, condensed_tree);
let node_lambda = self.extract_lambda_birth(node.node_id, condensed_tree);
if node_lambda >= lambda_threshold {
child_nodes.push(node.node_id)
}
} else if self.hp.allow_single_cluster && self.is_top_cluster(¤t_node_id) {
continue;
} else {
child_nodes.push(node.node_id);
}
}
}
child_nodes
}
fn get_lambda_threshold(&self, root_node_id: usize, condensed_tree: &CondensedTree<T>) -> T {
if self.hp.epsilon == 0.0 {
condensed_tree
.iter()
.filter(|node| node.parent_node_id == root_node_id)
.map(|node| node.lambda_birth)
.fold(None, |max, lambda| match max {
None => Some(lambda),
Some(max_lambda) => Some(if lambda > max_lambda {
lambda
} else {
max_lambda
}),
})
.unwrap_or(T::zero())
} else {
T::from(1.0 / self.hp.epsilon).unwrap()
}
}
}