@@ -1108,31 +1108,27 @@ cdef class ArccosDistance(DistanceMetric):
1108
1108
#
1109
1109
cdef class PyFuncDistance(DistanceMetric):
1110
1110
""" PyFunc Distance
1111
-
1112
1111
A user-defined distance
1113
-
1114
1112
Parameters
1115
1113
----------
1116
1114
func : function
1117
1115
func should take two numpy arrays as input, and return a distance.
1118
1116
"""
1119
1117
def __init__ (self , func , **kwargs ):
1120
1118
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
-
1132
1119
self .kwargs = kwargs
1133
1120
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.
1134
1126
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:
1136
1132
cdef np.ndarray x1arr
1137
1133
cdef np.ndarray x2arr
1138
1134
x1arr = _buffer_to_ndarray(x1, size)
0 commit comments