|
| 1 | +""" |
| 2 | +Test for the branches module. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import numpy as np |
| 7 | +from sklearn.exceptions import NotFittedError |
| 8 | +from fast_hdbscan import HDBSCAN, BranchDetector, find_branch_sub_clusters |
| 9 | + |
| 10 | + |
| 11 | +def make_branches(points_per_branch=30): |
| 12 | + # Control points for line segments that merge three clusters |
| 13 | + p0 = (0.13, -0.26) |
| 14 | + p1 = (0.24, -0.12) |
| 15 | + p2 = (0.32, 0.1) |
| 16 | + p3 = (0.13, 0.1) |
| 17 | + |
| 18 | + # Noisy points along lines between three clusters |
| 19 | + return np.concatenate( |
| 20 | + [ |
| 21 | + np.column_stack( |
| 22 | + ( |
| 23 | + np.linspace(p_start[0], p_end[0], points_per_branch), |
| 24 | + np.linspace(p_start[1], p_end[1], points_per_branch), |
| 25 | + ) |
| 26 | + ) |
| 27 | + + np.random.normal(size=(points_per_branch, 2), scale=0.01) |
| 28 | + for p_start, p_end in [(p0, p1), (p1, p2), (p1, p3)] |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +np.random.seed(0) |
| 34 | +X = np.concatenate( |
| 35 | + ( |
| 36 | + make_branches(), |
| 37 | + make_branches()[:60] + np.array([0.3, 0]), |
| 38 | + ) |
| 39 | +) |
| 40 | +c = HDBSCAN(min_samples=5, min_cluster_size=10).fit(X) |
| 41 | + |
| 42 | + |
| 43 | +def check_detected_groups(c, n_clusters=3, n_branches=6, overridden=False): |
| 44 | + """Checks branch_detector output for main invariants.""" |
| 45 | + noise_mask = c.labels_ == -1 |
| 46 | + assert np.all(np.unique(c.labels_[~noise_mask]) == np.arange(n_branches)) |
| 47 | + assert np.all(np.unique(c.cluster_labels_[~noise_mask]) == np.arange(n_clusters)) |
| 48 | + assert (c.branch_labels_[noise_mask] == 0).all() |
| 49 | + assert (c.branch_probabilities_[noise_mask] == 1.0).all() |
| 50 | + assert (c.probabilities_[noise_mask] == 0.0).all() |
| 51 | + assert (c.cluster_probabilities_[noise_mask] == 0.0).all() |
| 52 | + if not overridden: |
| 53 | + assert len(c.cluster_points_) == n_clusters |
| 54 | + for condensed_tree, linkage_tree in zip(c._condensed_trees, c._linkage_trees): |
| 55 | + assert linkage_tree is not None |
| 56 | + assert condensed_tree is not None |
| 57 | + |
| 58 | + |
| 59 | +# --- Detecting Branches |
| 60 | + |
| 61 | + |
| 62 | +def test_attributes(): |
| 63 | + def check_attributes(): |
| 64 | + b = BranchDetector().fit(c) |
| 65 | + check_detected_groups(b, n_clusters=2, n_branches=5) |
| 66 | + assert len(b.linkage_trees_) == 2 |
| 67 | + assert len(b.condensed_trees_) == 2 |
| 68 | + assert isinstance(b.condensed_trees_[0], CondensedTree) |
| 69 | + assert isinstance(b.linkage_trees_[0], SingleLinkageTree) |
| 70 | + assert isinstance(b.approximation_graph_, ApproximationGraph) |
| 71 | + |
| 72 | + try: |
| 73 | + from hdbscan.plots import ApproximationGraph, CondensedTree, SingleLinkageTree |
| 74 | + |
| 75 | + check_attributes() |
| 76 | + except ImportError: |
| 77 | + pass |
| 78 | + |
| 79 | + |
| 80 | +def test_selection_method(): |
| 81 | + b = BranchDetector(branch_selection_method="eom").fit(c) |
| 82 | + check_detected_groups(b, n_clusters=2, n_branches=5) |
| 83 | + |
| 84 | + b = BranchDetector(branch_selection_method="leaf").fit(c) |
| 85 | + check_detected_groups(b, n_clusters=2, n_branches=5) |
| 86 | + |
| 87 | + |
| 88 | +def test_min_branch_size(): |
| 89 | + b = BranchDetector(min_branch_size=7).fit(c) |
| 90 | + labels, counts = np.unique(b.labels_[b.branch_probabilities_ > 0], return_counts=True) |
| 91 | + assert (counts[labels >= 0] >= 7).all() |
| 92 | + check_detected_groups(b, n_clusters=2, n_branches=5) |
| 93 | + |
| 94 | + |
| 95 | +def test_label_sides_as_branches(): |
| 96 | + b = BranchDetector(label_sides_as_branches=True).fit(c) |
| 97 | + check_detected_groups(b, n_clusters=2, n_branches=6) |
| 98 | + |
| 99 | + |
| 100 | +def test_max_branch_size(): |
| 101 | + b = BranchDetector(label_sides_as_branches=True, max_branch_size=25).fit(c) |
| 102 | + check_detected_groups(b, n_clusters=2, n_branches=4) |
| 103 | + |
| 104 | + |
| 105 | +def test_override_cluster_labels(): |
| 106 | + X_missing = X.copy() |
| 107 | + X_missing[60:80] = np.nan |
| 108 | + c = HDBSCAN(min_cluster_size=5).fit(X_missing) |
| 109 | + split_y = c.labels_.copy() |
| 110 | + split_y[split_y == 1] = 0 |
| 111 | + split_y[split_y == 2] = 1 |
| 112 | + b = BranchDetector(label_sides_as_branches=True).fit(c, split_y) |
| 113 | + check_detected_groups(b, n_clusters=2, n_branches=5, overridden=True) |
| 114 | + assert b._condensed_trees[0] is None |
| 115 | + assert b._linkage_trees[0] is None |
| 116 | + |
| 117 | + |
| 118 | +def test_allow_single_branch_with_filters(): |
| 119 | + # Without persistence, find 6 branches |
| 120 | + b = BranchDetector( |
| 121 | + min_branch_size=5, |
| 122 | + branch_selection_method="leaf", |
| 123 | + ).fit(c) |
| 124 | + unique_labels = np.unique(b.labels_) |
| 125 | + assert len(unique_labels) == 5 |
| 126 | + |
| 127 | + # Adding persistence removes the branches |
| 128 | + b = BranchDetector( |
| 129 | + min_branch_size=5, |
| 130 | + branch_selection_method="leaf", |
| 131 | + branch_selection_persistence=0.15, |
| 132 | + ).fit(c) |
| 133 | + unique_labels = np.unique(b.labels_) |
| 134 | + assert len(unique_labels) == 2 |
| 135 | + |
| 136 | + # Adding epsilon removes some branches |
| 137 | + b = BranchDetector( |
| 138 | + min_branch_size=5, |
| 139 | + branch_selection_method="leaf", |
| 140 | + branch_selection_epsilon=1 / 0.002, |
| 141 | + ).fit(c) |
| 142 | + unique_labels = np.unique(b.labels_) |
| 143 | + assert len(unique_labels) == 2 |
| 144 | + |
| 145 | + |
| 146 | +def test_badargs(): |
| 147 | + c_nofit = HDBSCAN(min_cluster_size=5) |
| 148 | + |
| 149 | + with pytest.raises(TypeError): |
| 150 | + find_branch_sub_clusters("fail") |
| 151 | + with pytest.raises(TypeError): |
| 152 | + find_branch_sub_clusters(None) |
| 153 | + with pytest.raises(NotFittedError): |
| 154 | + find_branch_sub_clusters(c_nofit) |
| 155 | + with pytest.raises(ValueError): |
| 156 | + find_branch_sub_clusters(c, min_branch_size=-1) |
| 157 | + with pytest.raises(ValueError): |
| 158 | + find_branch_sub_clusters(c, min_branch_size=0) |
| 159 | + with pytest.raises(ValueError): |
| 160 | + find_branch_sub_clusters(c, min_branch_size=1) |
| 161 | + with pytest.raises(ValueError): |
| 162 | + find_branch_sub_clusters(c, min_branch_size=2.0) |
| 163 | + with pytest.raises(ValueError): |
| 164 | + find_branch_sub_clusters(c, min_branch_size="fail") |
| 165 | + with pytest.raises(ValueError): |
| 166 | + find_branch_sub_clusters(c, branch_selection_persistence=-0.1) |
| 167 | + with pytest.raises(ValueError): |
| 168 | + find_branch_sub_clusters(c, branch_selection_epsilon=-0.1) |
| 169 | + with pytest.raises(ValueError): |
| 170 | + find_branch_sub_clusters( |
| 171 | + c, |
| 172 | + branch_selection_method="something_else", |
| 173 | + ) |
0 commit comments