evoc-rs 0.1.3

Rust port of the EVoC clustering algorithm for high dimensional data
Documentation
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
//! Condensed tree construction and cluster extraction for HDBSCAN.
//!
//! Converts a single-linkage dendrogram into a condensed tree by collapsing
//! splits where either child is smaller than `min_cluster_size`. The result
//! tracks how clusters persist across the lambda (1 / dist) scale. Leaf
//! clusters in the condensed tree are the final cluster candidates; points
//! not belonging to any selected cluster are labelled noise (-1).

use crate::clustering::linkage::LinkageRow;
use crate::prelude::*;
use crate::utils::disjoint_set::DisjointSet;

////////////////
// Structures //
////////////////

/// A single node in the condensed cluster tree.
#[derive(Clone, Debug)]
pub struct CondensedNode<T> {
    /// Cluster this node belongs to (internal node index >= n_samples)
    pub parent: usize,
    /// Either a point index (< n_samples) or a child cluster index
    pub child: usize,
    /// Lambda (1 / distance) at which this child falls out of the parent
    pub lambda_val: T,
    /// Number of original points under this child (1 for leaf points)
    pub child_size: usize,
}

/// The condensed cluster hierarchy produced by [`condense_tree`].
#[derive(Clone, Debug)]
pub struct CondensedTree<T> {
    /// All nodes in the condensed tree, in traversal order
    pub nodes: Vec<CondensedNode<T>>,
    /// Number of original data points (leaf node indices are 0..n_samples)
    pub n_samples: usize,
}

/// Collect all descendants of `root` in the linkage hierarchy (DFS order).
///
/// Returns both internal nodes (index >= n_samples) and leaf points
/// (index < n_samples).
///
/// ### Params
///
/// * `linkage` - Linkage matrix. Slice of `LinkageRow<T>`
/// * `root` - Node index to start traversal from
/// * `n_samples` - Number of original data points; used to distinguish leaves
///   from internal nodes
///
/// ### Returns
///
/// All descendant node indices in DFS order, including `root` itself
fn collect_descendants<T>(linkage: &[LinkageRow<T>], root: usize, n_samples: usize) -> Vec<usize>
where
    T: EvocFloat,
{
    let mut result = Vec::new();
    let mut queue = vec![root];

    while let Some(node) = queue.pop() {
        result.push(node);
        if node >= n_samples {
            let row = &linkage[node - n_samples];
            queue.push(row.left);
            queue.push(row.right);
        }
    }
    result
}

/// Flatten all leaf points under `node` into the condensed tree as direct
/// children of `parent_cluster` at `lambda_val`.
///
/// Used when a subtree is too small to form its own cluster; its points are
/// attributed to the surviving parent cluster instead.
///
/// ### Params
///
/// * `linkage` - Linkage matrix. Slice of `LinkageRow<T>`
/// * `node` - Root of the subtree being eliminated
/// * `parent_cluster` - Cluster index the eliminated points are assigned to
/// * `lambda_val` - Lambda at which the elimination occurs
/// * `n_samples` - Number of original data points
/// * `output` - Condensed tree node buffer, appended to in place
fn eliminate_branch<T>(
    linkage: &[LinkageRow<T>],
    node: usize,
    parent_cluster: usize,
    lambda_val: T,
    n_samples: usize,
    output: &mut Vec<CondensedNode<T>>,
) where
    T: EvocFloat,
{
    let descendants = collect_descendants(linkage, node, n_samples);
    for d in descendants {
        if d < n_samples {
            output.push(CondensedNode {
                parent: parent_cluster,
                child: d,
                lambda_val,
                child_size: 1,
            });
        }
    }
}

/// Condense a linkage tree with a given `min_cluster_size`.
///
/// Walks the hierarchy top-down. When a split produces a child smaller
/// than `min_cluster_size`, that branch is eliminated and its points are
/// assigned to the parent cluster. Only splits where both children meet
/// the minimum size produce new cluster nodes.
///
/// ### Params
///
/// * `linkage` - Linkage matrix. Slice of `LinkageRow<T>`
/// * `n_samples` - Number of original data points
/// * `min_cluster_size` - Minimum number of points for a split to produce a new
///   cluster
///
/// ### Returns
///
/// A [`CondensedTree`] whose leaf clusters are candidates for final cluster
/// selection
pub fn condense_tree<T>(
    linkage: &[LinkageRow<T>],
    n_samples: usize,
    min_cluster_size: usize,
) -> CondensedTree<T>
where
    T: EvocFloat,
{
    if linkage.is_empty() {
        return CondensedTree {
            nodes: Vec::new(),
            n_samples,
        };
    }

    // 2*n_samples - 2, the root node index
    let root = 2 * linkage.len();

    // BFS traversal order from root
    let traversal = collect_descendants(linkage, root, n_samples);

    let mut relabel = vec![0usize; root + 1];
    relabel[root] = n_samples; // root cluster gets label n_samples

    let mut next_label = n_samples + 1;
    let mut ignore = vec![false; root + 1];
    let mut output = Vec::new();

    for &node in &traversal {
        if node < n_samples || ignore[node] {
            continue;
        }

        let parent_label = relabel[node];
        let row = &linkage[node - n_samples];
        let left = row.left;
        let right = row.right;
        let distance = row.distance;

        let lambda = if distance > T::zero() {
            T::one() / distance
        } else {
            T::infinity()
        };

        let left_size = if left >= n_samples {
            linkage[left - n_samples].size
        } else {
            1
        };
        let right_size = if right >= n_samples {
            linkage[right - n_samples].size
        } else {
            1
        };

        let left_big = left_size >= min_cluster_size;
        let right_big = right_size >= min_cluster_size;

        match (left_big, right_big) {
            (true, true) => {
                // genuine split: both children become new clusters
                relabel[left] = next_label;
                output.push(CondensedNode {
                    parent: parent_label,
                    child: next_label,
                    lambda_val: lambda,
                    child_size: left_size,
                });
                next_label += 1;

                relabel[right] = next_label;
                output.push(CondensedNode {
                    parent: parent_label,
                    child: next_label,
                    lambda_val: lambda,
                    child_size: right_size,
                });
                next_label += 1;
            }
            (true, false) => {
                // right is too small: eliminate right, continue left
                relabel[left] = parent_label;
                eliminate_branch(linkage, right, parent_label, lambda, n_samples, &mut output);
                // mark internal nodes in right branch as ignored
                for d in collect_descendants(linkage, right, n_samples) {
                    if d >= n_samples {
                        ignore[d] = true;
                    }
                }
            }
            (false, true) => {
                // left is too small: eliminate left, continue right
                relabel[right] = parent_label;
                eliminate_branch(linkage, left, parent_label, lambda, n_samples, &mut output);
                for d in collect_descendants(linkage, left, n_samples) {
                    if d >= n_samples {
                        ignore[d] = true;
                    }
                }
            }
            (false, false) => {
                // both too small: eliminate both
                eliminate_branch(linkage, left, parent_label, lambda, n_samples, &mut output);
                eliminate_branch(linkage, right, parent_label, lambda, n_samples, &mut output);
                for d in collect_descendants(linkage, left, n_samples)
                    .into_iter()
                    .chain(collect_descendants(linkage, right, n_samples))
                {
                    if d >= n_samples {
                        ignore[d] = true;
                    }
                }
            }
        }
    }

    CondensedTree {
        nodes: output,
        n_samples,
    }
}

/// Extract leaf cluster IDs from the condensed tree.
///
/// A leaf cluster is one that never appears as a parent of another cluster,
/// only of individual points. These are the candidates for final cluster
/// assignment.
///
/// ### Params
///
/// * `tree` - Condensed tree produced by `condense_tree()`
///
/// ### Returns
///
/// Sorted list of cluster node indices that are leaves in the condensed
/// hierarchy.
pub fn extract_leaves(tree: &CondensedTree<impl EvocFloat>) -> Vec<usize> {
    if tree.nodes.is_empty() {
        return Vec::new();
    }

    let max_id = tree
        .nodes
        .iter()
        .map(|n| n.parent.max(n.child))
        .reduce(usize::max)
        .unwrap_or(0);

    let mut is_parent = vec![false; max_id + 1];
    for node in &tree.nodes {
        if node.child_size > 1 {
            is_parent[node.parent] = true;
        }
    }

    let mut leaves = Vec::new();
    for i in tree.n_samples..=max_id {
        if !is_parent[i] {
            // check it actually appears as a parent of at least one point
            let exists = tree.nodes.iter().any(|n| n.parent == i);
            if exists {
                leaves.push(i);
            }
        }
    }
    leaves
}

/// Assign each point to a cluster given a set of selected cluster IDs.
///
/// Points not falling under any selected cluster are labelled `-1` (noise).
///
/// ### Params
///
/// * `tree` - Condensed tree produced by `condense_tree()`.
/// * `clusters` - Selected cluster IDs, typically the output of
///   `extract_leaves()`
/// * `n_samples` - Number of original data points
///
/// ### Returns
///
/// Label vector of length `n_samples`; entry `i` is the cluster index of point
/// `i`, or `-1` if the point is noise
pub fn get_cluster_label_vector<T>(
    tree: &CondensedTree<T>,
    clusters: &[usize],
    n_samples: usize,
) -> Vec<i64>
where
    T: EvocFloat,
{
    if clusters.is_empty() {
        return vec![-1i64; n_samples];
    }

    let max_id = tree
        .nodes
        .iter()
        .map(|n| n.parent.max(n.child))
        .reduce(usize::max)
        .unwrap_or(0);

    let root = tree
        .nodes
        .iter()
        .map(|n| n.parent)
        .reduce(usize::min)
        .unwrap_or(n_samples);

    let mut ds = DisjointSet::new(max_id + 1);
    let cluster_set: std::collections::HashSet<usize> = clusters.iter().copied().collect();

    // merge everything that isn't a selected cluster boundary
    for node in &tree.nodes {
        if !cluster_set.contains(&node.child) {
            ds.union(node.parent, node.child);
        }
    }

    // build label map: sorted cluster ID -> label index
    let mut sorted_clusters = clusters.to_vec();
    sorted_clusters.sort();
    let label_map: std::collections::HashMap<usize, i64> = sorted_clusters
        .iter()
        .enumerate()
        .map(|(i, &c)| (c, i as i64))
        .collect();

    let mut labels = vec![-1i64; n_samples];
    for i in 0..n_samples {
        let cluster = ds.find(i);
        if cluster <= root {
            labels[i] = -1;
        } else if let Some(&label) = label_map.get(&cluster) {
            labels[i] = label;
        } else {
            labels[i] = -1;
        }
    }

    labels
}

/// Compute the maximum lambda (death lambda) for each selected cluster.
///
/// The death lambda is the largest lambda at which any point falls out of
/// the cluster, i.e. the lambda at which the cluster effectively dissolves.
///
/// ### Params
///
/// * `tree` - Condensed tree produced by `condense_tree()`
/// * `clusters` - Selected cluster IDs
///
/// ### Returns
///
/// Map from cluster ID to its maximum lambda value
fn max_lambdas<T>(
    tree: &CondensedTree<T>,
    clusters: &[usize],
) -> std::collections::HashMap<usize, T>
where
    T: EvocFloat,
{
    let cluster_set: std::collections::HashSet<usize> = clusters.iter().copied().collect();
    let mut result: std::collections::HashMap<usize, T> =
        clusters.iter().map(|&c| (c, T::zero())).collect();

    for node in &tree.nodes {
        if node.child_size == 1 && cluster_set.contains(&node.parent) {
            let entry = result.get_mut(&node.parent).unwrap();
            if node.lambda_val > *entry {
                *entry = node.lambda_val;
            }
        }
    }
    result
}

/// Compute membership strength for each point relative to its assigned cluster.
///
/// Strength is the ratio of the point's lambda to the cluster's death lambda,
/// clamped to `[0, 1]`. A strength of `1` means the point persisted until the
/// cluster dissolved; lower values indicate earlier departure.
///
/// ### Params
///
/// * `tree` - Condensed tree produced by `condense_tree()`
/// * `clusters` - Selected cluster IDs, typically the output of
///   `extract_leaves()`
/// * `labels` - Cluster label per point from `get_cluster_label_vector()`
///
/// ### Returns
///
/// Membership strength vector of length `labels.len()`; noise points (label `-1`)
/// retain a strength of `0`
pub fn get_point_membership_strengths<T>(
    tree: &CondensedTree<T>,
    clusters: &[usize],
    labels: &[i64],
) -> Vec<T>
where
    T: EvocFloat,
{
    let n = labels.len();
    let mut result = vec![T::zero(); n];
    let deaths = max_lambdas(tree, clusters);

    let mut sorted_clusters = clusters.to_vec();
    sorted_clusters.sort();

    // index -> cluster_id
    let index_to_cluster: std::collections::HashMap<i64, usize> = sorted_clusters
        .iter()
        .enumerate()
        .map(|(i, &c)| (i as i64, c))
        .collect();

    let root = tree
        .nodes
        .iter()
        .map(|n| n.parent)
        .reduce(usize::min)
        .unwrap_or(n);

    for node in &tree.nodes {
        let point = node.child;
        if point >= root || point >= n {
            continue;
        }
        if labels[point] < 0 {
            continue;
        }

        let cluster_id = match index_to_cluster.get(&labels[point]) {
            Some(&c) => c,
            None => continue,
        };

        let max_lambda = match deaths.get(&cluster_id) {
            Some(&ml) => ml,
            None => continue,
        };

        if max_lambda == T::zero() || !node.lambda_val.is_finite() {
            result[point] = T::one();
        } else {
            let lv = if node.lambda_val < max_lambda {
                node.lambda_val
            } else {
                max_lambda
            };
            result[point] = lv / max_lambda;
        }
    }

    result
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;
    use crate::clustering::linkage::{LinkageRow, mst_to_linkage_tree};
    use crate::clustering::mst::build_mst;

    fn make_two_cluster_tree() -> (Vec<LinkageRow<f64>>, usize) {
        // two tight clusters: {0,1,2} near origin, {3,4,5} far away
        let data = vec![
            vec![0.0, 0.0],
            vec![0.1, 0.0],
            vec![0.0, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.0],
            vec![10.0, 10.1],
        ];
        let mut mst = build_mst(&data, 1);
        let n = data.len();
        let linkage = mst_to_linkage_tree(&mut mst, n);
        (linkage, n)
    }

    #[test]
    fn test_condense_two_clusters() {
        let (linkage, n) = make_two_cluster_tree();
        let ct = condense_tree(&linkage, n, 2);

        let leaves = extract_leaves(&ct);
        assert_eq!(leaves.len(), 2, "expected 2 leaf clusters");
    }

    #[test]
    fn test_condense_min_cluster_size_too_large() {
        let (linkage, n) = make_two_cluster_tree();
        // min_cluster_size larger than either cluster
        let ct = condense_tree(&linkage, n, n);

        let leaves = extract_leaves(&ct);
        // everything collapses into root, no leaf clusters with children
        assert!(leaves.len() <= 1);
    }

    #[test]
    fn test_label_vector_two_clusters() {
        let (linkage, n) = make_two_cluster_tree();
        let ct = condense_tree(&linkage, n, 2);
        let leaves = extract_leaves(&ct);
        let labels = get_cluster_label_vector(&ct, &leaves, n);

        assert_eq!(labels.len(), n);

        // points 0,1,2 should share a label; 3,4,5 should share another
        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[0], labels[2]);
        assert_eq!(labels[3], labels[4]);
        assert_eq!(labels[3], labels[5]);

        // the two groups should differ
        assert_ne!(labels[0], labels[3]);

        // no noise expected
        assert!(labels.iter().all(|&l| l >= 0));
    }

    #[test]
    fn test_membership_strengths_range() {
        let (linkage, n) = make_two_cluster_tree();
        let ct = condense_tree(&linkage, n, 2);
        let leaves = extract_leaves(&ct);
        let labels = get_cluster_label_vector(&ct, &leaves, n);
        let strengths = get_point_membership_strengths(&ct, &leaves, &labels);

        assert_eq!(strengths.len(), n);
        for (i, &s) in strengths.iter().enumerate() {
            if labels[i] >= 0 {
                assert!(
                    (0.0..=1.0).contains(&s),
                    "strength {} out of range: {}",
                    i,
                    s
                );
            }
        }
    }

    #[test]
    fn test_condense_empty_linkage() {
        let ct = condense_tree::<f64>(&[], 0, 2);
        assert!(ct.nodes.is_empty());
        assert!(extract_leaves(&ct).is_empty());
    }

    #[test]
    fn test_labels_with_noise() {
        // 5 points: tight pair + 3 scattered
        let data = vec![
            vec![0.0, 0.0],
            vec![0.05, 0.0],
            vec![5.0, 5.0],
            vec![10.0, 0.0],
            vec![0.0, 10.0],
        ];
        let mut mst = build_mst(&data, 1);
        let n = data.len();
        let linkage = mst_to_linkage_tree(&mut mst, n);
        let ct = condense_tree(&linkage, n, 2);
        let leaves = extract_leaves(&ct);
        let labels = get_cluster_label_vector(&ct, &leaves, n);

        // the tight pair should be in the same cluster
        if labels[0] >= 0 && labels[1] >= 0 {
            assert_eq!(labels[0], labels[1]);
        }
    }
}