Skip to content

Commit

Permalink
style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhaedonius committed Apr 19, 2020
1 parent fb31c6b commit 0b259af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
22 changes: 10 additions & 12 deletions hdbscan/prediction.py
Expand Up @@ -88,8 +88,7 @@ def _clusters_below(self, cluster):
return result

def _recurse_leaf_dfs(self, current_node):
children = self.cluster_tree[self.cluster_tree['parent'] ==
current_node]['child']
children = self.cluster_tree[self.cluster_tree['parent'] == current_node]['child']
if len(children) == 0:
return [current_node, ]
else:
Expand All @@ -111,8 +110,7 @@ def __init__(self, data, condensed_tree, min_samples,
self.cluster_map = {c: n for n, c in enumerate(sorted(list(selected_clusters)))}
self.reverse_cluster_map = {n: c for c, n in self.cluster_map.items()}

self.cluster_tree = raw_condensed_tree[raw_condensed_tree['child_size']
> 1]
self.cluster_tree = raw_condensed_tree[raw_condensed_tree['child_size'] > 1]
self.max_lambdas = {}
self.leaf_max_lambdas = {}
self.exemplars = []
Expand All @@ -126,8 +124,7 @@ def __init__(self, data, condensed_tree, min_samples,

for cluster in selected_clusters:
self.max_lambdas[cluster] = \
raw_condensed_tree['lambda_val'][raw_condensed_tree['parent']
== cluster].max()
raw_condensed_tree['lambda_val'][raw_condensed_tree['parent'] == cluster].max()

for sub_cluster in self._clusters_below(cluster):
self.cluster_map[sub_cluster] = self.cluster_map[cluster]
Expand All @@ -138,8 +135,9 @@ def __init__(self, data, condensed_tree, min_samples,
leaf_max_lambda = raw_condensed_tree['lambda_val'][
raw_condensed_tree['parent'] == leaf].max()
points = raw_condensed_tree['child'][
(raw_condensed_tree['parent'] == leaf) &
(raw_condensed_tree['lambda_val'] == leaf_max_lambda)]
(raw_condensed_tree['parent'] == leaf)
& (raw_condensed_tree['lambda_val'] == leaf_max_lambda)
]
cluster_exemplars = np.hstack([cluster_exemplars, points])

self.exemplars.append(self.raw_data[cluster_exemplars])
Expand Down Expand Up @@ -245,10 +243,9 @@ def _extend_condensed_tree(tree, neighbor_indices, neighbor_distances,
else:
# Find appropriate cluster based on lambda of new point
while potential_cluster > tree_root and \
tree[tree['child'] ==
potential_cluster]['lambda_val'] >= lambda_:
potential_cluster = tree['parent'][tree['child']
== potential_cluster][0]
tree[tree['child']
== potential_cluster]['lambda_val'] >= lambda_:
potential_cluster = tree['parent'][tree['child'] == potential_cluster][0]

new_tree_row = (potential_cluster, -1, 1, lambda_)

Expand Down Expand Up @@ -412,6 +409,7 @@ def approximate_predict(clusterer, points_to_predict):

return labels, probabilities


def approximate_predict_scores(clusterer, points_to_predict):
"""Predict the outlier score of new points. The returned scores
will be based on the original clustering found by ``clusterer``,
Expand Down
7 changes: 4 additions & 3 deletions hdbscan/tests/test_hdbscan.py
Expand Up @@ -278,17 +278,17 @@ def test_hdbscan_best_balltree_metric():

def test_hdbscan_no_clusters():
labels, p, persist, ctree, ltree, mtree = hdbscan(
X, min_cluster_size=len(X)+1)
X, min_cluster_size=len(X) + 1)
n_clusters_1 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_1, 0)

labels = HDBSCAN(min_cluster_size=len(X)+1).fit(X).labels_
labels = HDBSCAN(min_cluster_size=len(X) + 1).fit(X).labels_
n_clusters_2 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_2, 0)


def test_hdbscan_min_cluster_size():
for min_cluster_size in range(2, len(X)+1, 1):
for min_cluster_size in range(2, len(X) + 1, 1):
labels, p, persist, ctree, ltree, mtree = hdbscan(
X, min_cluster_size=min_cluster_size)
true_labels = [label for label in labels if label != -1]
Expand Down Expand Up @@ -475,6 +475,7 @@ def test_hdbscan_approximate_predict():
cluster, prob = approximate_predict(clusterer, np.array([[0.0, 0.0]]))
assert_equal(cluster, -1)


def test_hdbscan_approximate_predict_score():
clusterer = HDBSCAN(min_cluster_size=200).fit(X)
# no prediction data error
Expand Down

0 comments on commit 0b259af

Please sign in to comment.