Skip to content

Commit 16b02de

Browse files
authored
More changes than I first noticed (issue #222)
1 parent e25e53d commit 16b02de

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

hdbscan/dist_metrics.pyx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,31 +1108,27 @@ cdef class ArccosDistance(DistanceMetric):
11081108
#
11091109
cdef class PyFuncDistance(DistanceMetric):
11101110
"""PyFunc Distance
1111-
11121111
A user-defined distance
1113-
11141112
Parameters
11151113
----------
11161114
func : function
11171115
func should take two numpy arrays as input, and return a distance.
11181116
"""
11191117
def __init__(self, func, **kwargs):
11201118
self.func = func
1121-
x = np.random.random(10)
1122-
try:
1123-
d = self.func(x, x, **kwargs)
1124-
except TypeError:
1125-
raise ValueError("func must be a callable taking two arrays")
1126-
1127-
try:
1128-
d = float(d)
1129-
except TypeError:
1130-
raise ValueError("func must return a float")
1131-
11321119
self.kwargs = kwargs
11331120

1121+
# in cython < 0.26, GIL was required to be acquired during definition of
1122+
# the function and inside the body of the function. This behaviour is not
1123+
# allowed in cython >= 0.26 since it is a redundant GIL acquisition. The
1124+
# only way to be back compatible is to inherit `dist` from the base class
1125+
# without GIL and called an inline `_dist` which acquire GIL.
11341126
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2,
1135-
ITYPE_t size) except -1 with gil:
1127+
ITYPE_t size) nogil except -1:
1128+
return self._dist(x1, x2, size)
1129+
1130+
cdef inline DTYPE_t _dist(self, DTYPE_t* x1, DTYPE_t* x2,
1131+
ITYPE_t size) except -1 with gil:
11361132
cdef np.ndarray x1arr
11371133
cdef np.ndarray x2arr
11381134
x1arr = _buffer_to_ndarray(x1, size)

0 commit comments

Comments
 (0)