Skip to content

Commit

Permalink
catch import error in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JelmerBot committed Jan 3, 2025
1 parent 2b89eff commit b2b5007
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
33 changes: 18 additions & 15 deletions fast_hbcc/tests/test_hbcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@

from fast_hbcc import HBCC, fast_hbcc

try:
from hdbscan.plots import (
ApproximationGraph,
CondensedTree,
SingleLinkageTree,
MinimumSpanningTree,
)

HAVE_HDBSCAN = True
except ModuleNotFoundError:
HAVE_HDBSCAN = False


n_clusters = 3
X, y = make_blobs(n_samples=200, random_state=10)
Expand Down Expand Up @@ -162,22 +174,13 @@ def test_hbcc_persistence_threshold():
assert np.all(model.labels_ == -1)


@pytest.mark.skipif(not HAVE_HDBSCAN, reason='requires HDBSCAN')
def test_attributes():
try:
from hdbscan.plots import (
ApproximationGraph,
CondensedTree,
SingleLinkageTree,
MinimumSpanningTree,
)

c = HBCC().fit(X)
assert isinstance(c.condensed_tree_, CondensedTree)
assert isinstance(c.single_linkage_tree_, SingleLinkageTree)
assert isinstance(c.minimum_spanning_tree_, MinimumSpanningTree)
assert isinstance(c.approximation_graph_, ApproximationGraph)
except ImportError:
pass
c = HBCC().fit(X)
assert isinstance(c.condensed_tree_, CondensedTree)
assert isinstance(c.single_linkage_tree_, SingleLinkageTree)
assert isinstance(c.minimum_spanning_tree_, MinimumSpanningTree)
assert isinstance(c.approximation_graph_, ApproximationGraph)


# Disable for now -- need to refactor to meet newer standards
Expand Down
27 changes: 15 additions & 12 deletions fast_hbcc/tests/test_sub_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

from fast_hbcc import BoundaryClusterDetector, find_boundary_sub_clusters

try:
from hdbscan.plots import ApproximationGraph, CondensedTree, SingleLinkageTree

HAVE_HDBSCAN = True
except ModuleNotFoundError:
HAVE_HDBSCAN = False


def make_branches(points_per_branch=30):
# Control points for line segments that merge three clusters
Expand Down Expand Up @@ -57,19 +64,15 @@ def check_detected_groups(c, n_clusters=3, n_subs=6, overridden=False):
# --- Detecting Sub_Clusters


@pytest.mark.skipif(not HAVE_HDBSCAN, reason='requires HDBSCAN')
def test_attributes():
try:
from hdbscan.plots import ApproximationGraph, CondensedTree, SingleLinkageTree

b = BoundaryClusterDetector().fit(c)
check_detected_groups(b, n_clusters=2, n_subs=6)
assert len(b.linkage_trees_) == 2
assert len(b.condensed_trees_) == 2
assert isinstance(b.condensed_trees_[0], CondensedTree)
assert isinstance(b.linkage_trees_[0], SingleLinkageTree)
assert isinstance(b.approximation_graph_, ApproximationGraph)
except ImportError:
pass
b = BoundaryClusterDetector().fit(c)
check_detected_groups(b, n_clusters=2, n_subs=6)
assert len(b.linkage_trees_) == 2
assert len(b.condensed_trees_) == 2
assert isinstance(b.condensed_trees_[0], CondensedTree)
assert isinstance(b.linkage_trees_[0], SingleLinkageTree)
assert isinstance(b.approximation_graph_, ApproximationGraph)


def test_selection_method():
Expand Down

0 comments on commit b2b5007

Please sign in to comment.