Skip to content

Commit 1d42c12

Browse files
committed
Improve plots coverage
1 parent 088f210 commit 1d42c12

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

hdbscan/plots.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,6 @@ def _get_leaves(condensed_tree):
4848
root = cluster_tree['parent'].min()
4949
return _recurse_leaf_dfs(cluster_tree, root)
5050

51-
def _bfs_from_linkage_tree(hierarchy, bfs_root):
52-
"""
53-
Perform a breadth first search on a tree in scipy hclust format.
54-
"""
55-
56-
dim = hierarchy.shape[0]
57-
max_node = 2 * dim
58-
num_points = max_node - dim + 1
59-
60-
to_process = [bfs_root]
61-
result = []
62-
63-
while to_process:
64-
result.extend(to_process)
65-
to_process = [x - num_points for x in
66-
to_process if x >= num_points]
67-
if to_process:
68-
to_process = hierarchy[to_process, :2].flatten().astype(np.int64).tolist()
69-
70-
return result
71-
72-
7351
class CondensedTree(object):
7452
def __init__(self, condensed_tree_array):
7553
self._raw_tree = condensed_tree_array
@@ -165,7 +143,12 @@ def get_plot_data(self, leaf_separation=1, log_size=False):
165143
bar_bottoms.append(current_lambda)
166144
bar_widths.append(current_size)
167145
if log_size:
168-
current_size = np.log(np.exp(current_size) - row['child_size'])
146+
exp_size = np.exp(current_size) - row['child_size']
147+
# Ensure we don't try to take log of zero
148+
if exp_size > 0.01:
149+
current_size = np.log(np.exp(current_size) - row['child_size'])
150+
else:
151+
current_size = 0.0
169152
else:
170153
current_size -= row['child_size']
171154
current_lambda = row['lambda_val']
@@ -327,7 +310,7 @@ def plot(self, leaf_separation=1, cmap='viridis', select_clusters=False,
327310
)
328311

329312
if selection_palette is not None and \
330-
len(selection_palette) > len(chosen_clusters):
313+
len(selection_palette) >= len(chosen_clusters):
331314
oval_color = selection_palette[i]
332315
else:
333316
oval_color = 'r'

hdbscan/tests/test_hdbscan.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,10 @@ def test_hdbscan_boruvka_balltree_matches():
273273
def test_condensed_tree_plot():
274274
clusterer = HDBSCAN(gen_min_span_tree=True).fit(X)
275275
if_matplotlib(clusterer.condensed_tree_.plot)(select_clusters=True,
276+
label_clusters=True,
276277
selection_palette=('r','g','b'),
277278
cmap='Reds')
278-
if_matplotlib(clusterer.condensed_tree_.plot)(label_clusters=True,
279+
if_matplotlib(clusterer.condensed_tree_.plot)(log_size=True,
279280
colorbar=False,
280281
cmap='none')
281282

@@ -284,7 +285,7 @@ def test_single_linkage_tree_plot():
284285
if_matplotlib(clusterer.single_linkage_tree_.plot)(cmap='Reds')
285286
if_matplotlib(clusterer.single_linkage_tree_.plot)(vary_line_width=False,
286287
truncate_mode='lastp',
287-
p=10, cmap='Reds',
288+
p=10, cmap='none',
288289
colorbar=False)
289290

290291
def test_min_span_tree_plot():

0 commit comments

Comments
 (0)