Skip to content

Commit de0c952

Browse files
committed
Okay, let's support old numpy ... somehow
1 parent 9611dc9 commit de0c952

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ env:
1414
- MODULE=hdbscan
1515
matrix:
1616
- DISTRIB="conda" PYTHON_VERSION="2.7"
17-
NUMPY_VERSION="1.7.0" SCIPY_VERSION="0.11.0" CYTHON_VERSION="0.21"
17+
NUMPY_VERSION="1.6.2" SCIPY_VERSION="0.11.0" CYTHON_VERSION="0.21"
1818
- DISTRIB="conda" PYTHON_VERSION="3.5" COVERAGE="true"
1919
NUMPY_VERSION="1.10.4" SCIPY_VERSION="0.17.0" CYTHON_VERSION="0.23.4"
2020

hdbscan/hdbscan_.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,54 @@
3939

4040
FAST_METRICS = KDTree.valid_metrics + BallTree.valid_metrics
4141

42+
# Supporting numpy prior to version 1.7 is a little painful ...
43+
if hasattr(np, 'isclose'):
44+
from numpy import isclose
45+
else:
46+
def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False):
47+
48+
def within_tol(x, y, atol, rtol):
49+
with errstate(invalid='ignore'):
50+
result = less_equal(abs(x-y), atol + rtol * abs(y))
51+
if isscalar(a) and isscalar(b):
52+
result = bool(result)
53+
return result
54+
55+
x = array(a, copy=False, subok=True, ndmin=1)
56+
y = array(b, copy=False, subok=True, ndmin=1)
57+
58+
# Make sure y is an inexact type to avoid bad behavior on abs(MIN_INT).
59+
# This will cause casting of x later. Also, make sure to allow subclasses
60+
# (e.g., for numpy.ma).
61+
dt = multiarray.result_type(y, 1.)
62+
y = array(y, dtype=dt, copy=False, subok=True)
63+
64+
xfin = isfinite(x)
65+
yfin = isfinite(y)
66+
if all(xfin) and all(yfin):
67+
return within_tol(x, y, atol, rtol)
68+
else:
69+
finite = xfin & yfin
70+
cond = zeros_like(finite, subok=True)
71+
# Because we're using boolean indexing, x & y must be the same shape.
72+
# Ideally, we'd just do x, y = broadcast_arrays(x, y). It's in
73+
# lib.stride_tricks, though, so we can't import it here.
74+
x = x * ones_like(cond)
75+
y = y * ones_like(cond)
76+
# Avoid subtraction with infinite/nan values...
77+
cond[finite] = within_tol(x[finite], y[finite], atol, rtol)
78+
# Check for equality of infinite values...
79+
cond[~finite] = (x[~finite] == y[~finite])
80+
if equal_nan:
81+
# Make NaN == NaN
82+
both_nan = isnan(x) & isnan(y)
83+
cond[both_nan] = both_nan[both_nan]
84+
85+
if isscalar(a) and isscalar(b):
86+
return bool(cond)
87+
else:
88+
return cond
89+
4290

4391
def _tree_to_labels(X, single_linkage_tree, min_cluster_size=10, allow_single_cluster=False):
4492
""" Converts a pretrained tree and cluster size into a

0 commit comments

Comments
 (0)