Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail early on generating branch data if no MST #650

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions hdbscan/hdbscan_.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,12 @@ def generate_branch_detection_data(self):
branches within clusters. This data is only useful if you are
intending to use functions from ``hdbscan.branches``.
"""
if self._min_spanning_tree is None:
raise ValueError("Branch prediction requires a minimum spanning tree; please re-run "
"with `branch_repdiction_data=True` or at least `gen_min_spanning_tree=True` "
"and this this function to generate the required information for branch "
"branch detection."
)
if self.metric in FAST_METRICS:
min_samples = self.min_samples or self.min_cluster_size
if self.metric in KDTREE_VALID_METRICS:
Expand Down
7 changes: 1 addition & 6 deletions hdbscan/tests/test_branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ def test_branch_detection_data_with_unsupported_input():
def test_generate_branch_detection_data():
"""Generate branch detection data function does not re-generate MST."""
c = HDBSCAN(min_cluster_size=5).fit(X)
c.generate_branch_detection_data()
assert c.branch_detection_data_ is not None
assert_raises(AttributeError, lambda: c.minimum_spanning_tree_)
assert_raises(ValueError, c.generate_branch_detection_data)


# --- Detecting Branches
Expand Down Expand Up @@ -287,15 +285,12 @@ def test_badargs():
c = HDBSCAN(min_cluster_size=5, branch_detection_data=True).fit(X)
c_nofit = HDBSCAN(min_cluster_size=5, branch_detection_data=True)
c_nobranch = HDBSCAN(min_cluster_size=5, gen_min_span_tree=True).fit(X)
c_nomst = HDBSCAN(min_cluster_size=5).fit(X)
c_nomst.generate_branch_detection_data()

assert_raises(AttributeError, detect_branches_in_clusters, "fail")
assert_raises(AttributeError, detect_branches_in_clusters, None)
assert_raises(AttributeError, detect_branches_in_clusters, "fail")
assert_raises(ValueError, detect_branches_in_clusters, c_nofit)
assert_raises(AttributeError, detect_branches_in_clusters, c_nobranch)
assert_raises(ValueError, detect_branches_in_clusters, c_nomst)
assert_raises(ValueError, detect_branches_in_clusters, c, min_branch_size=-1)
assert_raises(ValueError, detect_branches_in_clusters, c, min_branch_size=0)
assert_raises(ValueError, detect_branches_in_clusters, c, min_branch_size=1)
Expand Down