Skip to content

Commit 939a88d

Browse files
committed
Make tests better (skip if pandas or networkx is unavailable)
1 parent 2796ecb commit 939a88d

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

hdbscan/_hdbscan_reachability.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def mutual_reachability(distance_matrix, min_points=5, alpha=1.0):
5555
core_distances.T, stage1.T).T
5656
return result
5757

58-
cpdef sparse_mutual_reachability(lil_matrix, min_points=5, alpha=1.0):
58+
cpdef sparse_mutual_reachability(object lil_matrix, np.intp_t min_points=5, float alpha=1.0):
5959

6060
cdef np.intp_t i
6161
cdef np.intp_t j

hdbscan/tests/test_hdbscan.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,39 @@
2121
from scipy.stats import mode
2222

2323
from tempfile import mkdtemp
24+
from functools import wraps
25+
from nose import SkipTest
2426

2527
from sklearn import datasets
2628

2729
n_clusters = 3
2830
X = generate_clustered_data(n_clusters=n_clusters, n_samples_per_cluster=50)
2931

32+
def if_pandas(func):
33+
"""Test decorator that skips test if pandas not installed."""
34+
@wraps(func)
35+
def run_test(*args, **kwargs):
36+
try:
37+
import pandas
38+
except ImportError:
39+
raise SkipTest('Pandas not available.')
40+
else:
41+
return func(*args, **kwargs)
42+
return run_test
43+
44+
def if_networkx(func):
45+
"""Test decorator that skips test if networkx not installed."""
46+
@wraps(func)
47+
def run_test(*args, **kwargs):
48+
try:
49+
import networkx
50+
except ImportError:
51+
raise SkipTest('NetworkX not available.')
52+
else:
53+
return func(*args, **kwargs)
54+
return run_test
55+
56+
3057
def relabel(labels):
3158
result = np.zeros(labels.shape[0])
3259
labels_to_go = set(labels)
@@ -173,13 +200,13 @@ def test_condensed_tree_plot():
173200
def test_tree_output_formats():
174201

175202
clusterer = HDBSCAN(gen_min_span_tree=True).fit(X)
176-
clusterer.condensed_tree_.to_pandas()
177-
clusterer.condensed_tree_.to_networkx()
178-
clusterer.single_linkage_tree_.to_pandas()
179-
clusterer.single_linkage_tree_.to_networkx()
203+
if_pandas(clusterer.condensed_tree_.to_pandas)()
204+
if_networkx(clusterer.condensed_tree_.to_networkx)()
205+
if_pandas(clusterer.single_linkage_tree_.to_pandas)()
206+
if_networkx(clusterer.single_linkage_tree_.to_networkx)()
180207
clusterer.single_linkage_tree_.to_numpy()
181-
clusterer.minimum_spanning_tree_.to_pandas()
182-
clusterer.minimum_spanning_tree_.to_networkx()
208+
if_pandas(clusterer.minimum_spanning_tree_.to_pandas)()
209+
if_networkx(clusterer.minimum_spanning_tree_.to_networkx)()
183210

184211
def test_hdbscan_badargs():
185212
assert_raises(ValueError,

0 commit comments

Comments
 (0)