Skip to content

MAINT Use newest NumPy C API in metrics._dist_metrics #25702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"sklearn.manifold._barnes_hut_tsne",
"sklearn.manifold._utils",
"sklearn.metrics.cluster._expected_mutual_info_fast",
"sklearn.metrics._dist_metrics",
"sklearn.metrics._pairwise_distances_reduction._datasets_pair",
"sklearn.metrics._pairwise_distances_reduction._middle_term_computer",
"sklearn.metrics._pairwise_distances_reduction._base",
Expand Down
15 changes: 7 additions & 8 deletions sklearn/metrics/_dist_metrics.pyx.tp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ METRIC_MAPPING{{name_suffix}} = {
'pyfunc': PyFuncDistance{{name_suffix}},
}

cdef inline cnp.ndarray _buffer_to_ndarray{{name_suffix}}(const {{INPUT_DTYPE_t}}* x, cnp.npy_intp n):
cdef inline object _buffer_to_ndarray{{name_suffix}}(const {{INPUT_DTYPE_t}}* x, cnp.npy_intp n):
# Wrap a memory buffer with an ndarray. Warning: this is not robust.
# In particular, if x is deallocated before the returned array goes
# out of scope, this could cause memory errors. Since there is not
Expand Down Expand Up @@ -620,9 +620,9 @@ cdef class DistanceMetric{{name_suffix}}:
return dist

def _pairwise_dense_dense(self, X, Y):
cdef cnp.ndarray[{{INPUT_DTYPE_t}}, ndim=2, mode='c'] Xarr
cdef cnp.ndarray[{{INPUT_DTYPE_t}}, ndim=2, mode='c'] Yarr
cdef cnp.ndarray[DTYPE_t, ndim=2, mode='c'] Darr
cdef const {{INPUT_DTYPE_t}}[:, ::1] Xarr
cdef const {{INPUT_DTYPE_t}}[:, ::1] Yarr
cdef DTYPE_t[:, ::1] Darr

Xarr = np.asarray(X, dtype={{INPUT_DTYPE}}, order='C')
self._validate_data(Xarr)
Expand Down Expand Up @@ -2806,10 +2806,9 @@ cdef class PyFuncDistance{{name_suffix}}(DistanceMetric{{name_suffix}}):
const {{INPUT_DTYPE_t}}* x2,
ITYPE_t size,
) except -1 with gil:
cdef cnp.ndarray x1arr
cdef cnp.ndarray x2arr
x1arr = _buffer_to_ndarray{{name_suffix}}(x1, size)
x2arr = _buffer_to_ndarray{{name_suffix}}(x2, size)
cdef:
object x1arr = _buffer_to_ndarray{{name_suffix}}(x1, size)
object x2arr = _buffer_to_ndarray{{name_suffix}}(x2, size)
d = self.func(x1arr, x2arr, **self.kwargs)
try:
# Cython generates code here that results in a TypeError
Expand Down