From 2b4d409968eedb9c503cbb8f1c88ef4db823e842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 11:40:49 +0100 Subject: [PATCH 01/14] CI: use latest Cython sources in scipy-dev build --- build_tools/azure/install.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build_tools/azure/install.sh b/build_tools/azure/install.sh index 26b184d6f7d1d..267d826c787a5 100755 --- a/build_tools/azure/install.sh +++ b/build_tools/azure/install.sh @@ -83,11 +83,11 @@ python_environment_install_and_activate() { echo "Installing development dependency wheels" dev_anaconda_url=https://pypi.anaconda.org/scipy-wheels-nightly/simple pip install --pre --upgrade --timeout=60 --extra-index $dev_anaconda_url numpy pandas scipy - echo "Installing Cython from PyPI enabling pre-releases" - pip install --pre cython - echo "Installing joblib master" + echo "Installing Cython from latest sources" + pip install https://github.com/cython/cython/archive/refs/heads/master.zip + echo "Installing joblib from latest sources" pip install https://github.com/joblib/joblib/archive/master.zip - echo "Installing pillow master" + echo "Installing pillow from latest sources" pip install https://github.com/python-pillow/Pillow/archive/main.zip fi } From 9e7549ddc7e2eab5df1ca3f3298be9189562521b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 11:46:48 +0100 Subject: [PATCH 02/14] [scipy-dev] trigger CI From 345368e6f937a4ab7ab1ca3afeabb4e1b3cc2be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 17:01:08 +0100 Subject: [PATCH 03/14] Use legacy flag + some fix and some warnings removal --- build_tools/azure/install.sh | 2 +- sklearn/_build_utils/__init__.py | 1 + sklearn/metrics/_dist_metrics.pxd.tp | 24 ++--- sklearn/metrics/_dist_metrics.pyx.tp | 144 +++++++++++++-------------- sklearn/neighbors/_ball_tree.pyx | 6 +- sklearn/neighbors/_binary_tree.pxi | 12 +-- sklearn/neighbors/_kd_tree.pyx | 4 +- sklearn/neighbors/_quad_tree.pxd | 10 +- sklearn/neighbors/_quad_tree.pyx | 12 +-- sklearn/tree/_criterion.pxd | 8 +- sklearn/tree/_criterion.pyx | 32 +++--- sklearn/tree/_splitter.pxd | 4 +- sklearn/tree/_splitter.pyx | 12 +-- sklearn/tree/_tree.pxd | 6 +- sklearn/tree/_tree.pyx | 10 +- sklearn/tree/_utils.pxd | 10 +- sklearn/tree/_utils.pyx | 10 +- 17 files changed, 154 insertions(+), 153 deletions(-) diff --git a/build_tools/azure/install.sh b/build_tools/azure/install.sh index 267d826c787a5..1c76a7a4aa5d3 100755 --- a/build_tools/azure/install.sh +++ b/build_tools/azure/install.sh @@ -84,7 +84,7 @@ python_environment_install_and_activate() { dev_anaconda_url=https://pypi.anaconda.org/scipy-wheels-nightly/simple pip install --pre --upgrade --timeout=60 --extra-index $dev_anaconda_url numpy pandas scipy echo "Installing Cython from latest sources" - pip install https://github.com/cython/cython/archive/refs/heads/master.zip + pip install https://github.com/cython/cython/archive/master.zip echo "Installing joblib from latest sources" pip install https://github.com/joblib/joblib/archive/master.zip echo "Installing pillow from latest sources" diff --git a/sklearn/_build_utils/__init__.py b/sklearn/_build_utils/__init__.py index 6828192aaf4a5..2bfe48af37650 100644 --- a/sklearn/_build_utils/__init__.py +++ b/sklearn/_build_utils/__init__.py @@ -83,6 +83,7 @@ def cythonize_extensions(extension): "initializedcheck": False, "nonecheck": False, "cdivision": True, + "legacy_implicit_noexcept": True, }, ) diff --git a/sklearn/metrics/_dist_metrics.pxd.tp b/sklearn/metrics/_dist_metrics.pxd.tp index e0e67758f5023..7d64ca55cc515 100644 --- a/sklearn/metrics/_dist_metrics.pxd.tp +++ b/sklearn/metrics/_dist_metrics.pxd.tp @@ -41,7 +41,7 @@ cdef inline DTYPE_t euclidean_dist{{name_suffix}}( const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, -) nogil except -1: +) except -1 nogil: cdef DTYPE_t tmp, d=0 cdef cnp.intp_t j for j in range(size): @@ -54,7 +54,7 @@ cdef inline DTYPE_t euclidean_rdist{{name_suffix}}( const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, -) nogil except -1: +) except -1 nogil: cdef DTYPE_t tmp, d=0 cdef cnp.intp_t j for j in range(size): @@ -63,11 +63,11 @@ cdef inline DTYPE_t euclidean_rdist{{name_suffix}}( return d -cdef inline DTYPE_t euclidean_dist_to_rdist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) nogil except -1: +cdef inline DTYPE_t euclidean_dist_to_rdist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil: return dist * dist -cdef inline DTYPE_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) nogil except -1: +cdef inline DTYPE_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil: return sqrt(dist) @@ -90,14 +90,14 @@ cdef class DistanceMetric{{name_suffix}}: const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1 + ) except -1 nogil cdef DTYPE_t rdist( self, const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1 + ) except -1 nogil cdef DTYPE_t dist_csr( self, @@ -110,7 +110,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1 + ) except -1 nogil cdef DTYPE_t rdist_csr( self, @@ -123,7 +123,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1 + ) except -1 nogil cdef int pdist( self, @@ -145,7 +145,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x1_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) nogil except -1 + ) except -1 nogil cdef int cdist_csr( self, @@ -157,10 +157,10 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x2_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) nogil except -1 + ) except -1 nogil - cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1 + cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil - cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1 + cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil {{endfor}} diff --git a/sklearn/metrics/_dist_metrics.pyx.tp b/sklearn/metrics/_dist_metrics.pyx.tp index 1e4a9429af03f..0306a41d27107 100644 --- a/sklearn/metrics/_dist_metrics.pyx.tp +++ b/sklearn/metrics/_dist_metrics.pyx.tp @@ -333,7 +333,7 @@ cdef class DistanceMetric{{name_suffix}}: const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: """Compute the distance between vectors x1 and x2 This should be overridden in a base class. @@ -345,7 +345,7 @@ cdef class DistanceMetric{{name_suffix}}: const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: """Compute the rank-preserving surrogate distance between vectors x1 and x2. This can optionally be overridden in a base class. @@ -397,7 +397,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: """Compute the distance between vectors x1 and x2 represented under the CSR format. @@ -422,7 +422,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x1_indices, const {{INPUT_DTYPE_t}}* x2_data, const SPARSE_INDEX_TYPE_t[:] x2_indices, - ) nogil except -1: + ) except -1 nogil: Where callers would use slicing on the original CSR data and indices memoryviews: @@ -463,7 +463,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: """Distance between rows of CSR matrices x1 and x2. This can optionally be overridden in a subclass. @@ -500,7 +500,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x1_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) nogil except -1: + ) except -1 nogil: """Pairwise distances between rows in CSR matrix X. Note that this implementation is twice faster than cdist_csr(X, X) @@ -540,7 +540,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x2_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) nogil except -1: + ) except -1 nogil: """Compute the cross-pairwise distances between arrays X and Y represented in the CSR format.""" cdef: @@ -569,11 +569,11 @@ cdef class DistanceMetric{{name_suffix}}: ) return 0 - cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: """Convert the rank-preserving surrogate distance to the distance""" return rdist - cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: """Convert the distance to the rank-preserving surrogate distance""" return dist @@ -867,20 +867,20 @@ cdef class EuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return euclidean_dist{{name_suffix}}(x1, x2, size) cdef inline DTYPE_t rdist(self, const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return euclidean_rdist{{name_suffix}}(x1, x2, size) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: return sqrt(rdist) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: return dist * dist def rdist_to_dist(self, rdist): @@ -900,7 +900,7 @@ cdef class EuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -952,7 +952,7 @@ cdef class EuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return sqrt( self.rdist_csr( x1_data, @@ -989,7 +989,7 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t tmp, d=0 cdef cnp.intp_t j for j in range(size): @@ -1002,13 +1002,13 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return sqrt(self.rdist(x1, x2, size)) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: return sqrt(rdist) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: return dist * dist def rdist_to_dist(self, rdist): @@ -1028,7 +1028,7 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1081,7 +1081,7 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return sqrt( self.rdist_csr( x1_data, @@ -1112,7 +1112,7 @@ cdef class ManhattanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t d = 0 cdef cnp.intp_t j for j in range(size): @@ -1130,7 +1130,7 @@ cdef class ManhattanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1195,7 +1195,7 @@ cdef class ChebyshevDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t d = 0 cdef cnp.intp_t j for j in range(size): @@ -1214,7 +1214,7 @@ cdef class ChebyshevDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1308,7 +1308,7 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t d=0 cdef cnp.intp_t j cdef bint has_w = self.size > 0 @@ -1325,13 +1325,13 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return pow(self.rdist(x1, x2, size), 1. / self.p) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: return pow(rdist, 1. / self.p) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: return pow(dist, self.p) def rdist_to_dist(self, rdist): @@ -1351,7 +1351,7 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1431,7 +1431,7 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return pow( self.rdist_csr( x1_data, @@ -1496,7 +1496,7 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t d = 0 cdef cnp.intp_t j @@ -1509,13 +1509,13 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return pow(self.rdist(x1, x2, size), 1. / self.p) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: return pow(rdist, 1. / self.p) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: return pow(dist, self.p) def rdist_to_dist(self, rdist): @@ -1535,7 +1535,7 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1585,7 +1585,7 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return pow( self.rdist_csr( x1_data, @@ -1644,7 +1644,7 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t tmp, d = 0 cdef cnp.intp_t i, j @@ -1664,13 +1664,13 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return sqrt(self.rdist(x1, x2, size)) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: return sqrt(rdist) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: return dist * dist def rdist_to_dist(self, rdist): @@ -1690,7 +1690,7 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1744,7 +1744,7 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return sqrt( self.rdist_csr( x1_data, @@ -1775,7 +1775,7 @@ cdef class HammingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int n_unequal = 0 cdef cnp.intp_t j for j in range(size): @@ -1795,7 +1795,7 @@ cdef class HammingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1850,7 +1850,7 @@ cdef class CanberraDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t denom, d = 0 cdef cnp.intp_t j for j in range(size): @@ -1870,7 +1870,7 @@ cdef class CanberraDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -1925,7 +1925,7 @@ cdef class BrayCurtisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t num = 0, denom = 0 cdef cnp.intp_t j for j in range(size): @@ -1947,7 +1947,7 @@ cdef class BrayCurtisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2005,7 +2005,7 @@ cdef class JaccardDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_eq = 0, nnz = 0 cdef cnp.intp_t j for j in range(size): @@ -2031,7 +2031,7 @@ cdef class JaccardDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2094,7 +2094,7 @@ cdef class MatchingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2114,7 +2114,7 @@ cdef class MatchingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2169,7 +2169,7 @@ cdef class DiceDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_neq = 0, n_tt = 0 cdef cnp.intp_t j for j in range(size): @@ -2190,7 +2190,7 @@ cdef class DiceDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2250,7 +2250,7 @@ cdef class KulsinskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_tt = 0, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2271,7 +2271,7 @@ cdef class KulsinskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2329,7 +2329,7 @@ cdef class RogersTanimotoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2349,7 +2349,7 @@ cdef class RogersTanimotoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2406,7 +2406,7 @@ cdef class RussellRaoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_tt = 0 cdef cnp.intp_t j for j in range(size): @@ -2426,7 +2426,7 @@ cdef class RussellRaoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2476,7 +2476,7 @@ cdef class SokalMichenerDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2496,7 +2496,7 @@ cdef class SokalMichenerDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2553,7 +2553,7 @@ cdef class SokalSneathDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef int tf1, tf2, n_tt = 0, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2574,7 +2574,7 @@ cdef class SokalSneathDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2641,7 +2641,7 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef DTYPE_t sin_0 = sin(0.5 * ((x1[0]) - (x2[0]))) cdef DTYPE_t sin_1 = sin(0.5 * ((x1[1]) - (x2[1]))) return (sin_0 * sin_0 + cos(x1[0]) * cos(x2[0]) * sin_1 * sin_1) @@ -2650,13 +2650,13 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return 2 * asin(sqrt(self.rdist(x1, x2, size))) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: return 2 * asin(sqrt(rdist)) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: cdef DTYPE_t tmp = sin(0.5 * dist) return tmp * tmp @@ -2678,7 +2678,7 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return 2 * asin(sqrt(self.rdist_csr( x1_data, x1_indices, @@ -2702,7 +2702,7 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: cdef: cnp.npy_intp ix1, ix2 @@ -2788,7 +2788,7 @@ cdef class PyFuncDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) nogil except -1: + ) except -1 nogil: return self._dist(x1, x2, size) cdef inline DTYPE_t _dist( diff --git a/sklearn/neighbors/_ball_tree.pyx b/sklearn/neighbors/_ball_tree.pyx index 094a8826acfb9..70135c0a40fc6 100644 --- a/sklearn/neighbors/_ball_tree.pyx +++ b/sklearn/neighbors/_ball_tree.pyx @@ -100,7 +100,7 @@ cdef int init_node(BinaryTree tree, NodeData_t[::1] node_data, ITYPE_t i_node, cdef inline DTYPE_t min_dist(BinaryTree tree, ITYPE_t i_node, - DTYPE_t* pt) nogil except -1: + DTYPE_t* pt) except -1 nogil: """Compute the minimum distance between a point and a node""" cdef DTYPE_t dist_pt = tree.dist(pt, &tree.node_bounds[0, i_node, 0], tree.data.shape[1]) @@ -116,7 +116,7 @@ cdef inline DTYPE_t max_dist(BinaryTree tree, ITYPE_t i_node, cdef inline int min_max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt, - DTYPE_t* min_dist, DTYPE_t* max_dist) nogil except -1: + DTYPE_t* min_dist, DTYPE_t* max_dist) except -1 nogil: """Compute the minimum and maximum distance between a point and a node""" cdef DTYPE_t dist_pt = tree.dist(pt, &tree.node_bounds[0, i_node, 0], tree.data.shape[1]) @@ -127,7 +127,7 @@ cdef inline int min_max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt, cdef inline DTYPE_t min_rdist(BinaryTree tree, ITYPE_t i_node, - DTYPE_t* pt) nogil except -1: + DTYPE_t* pt) except -1 nogil: """Compute the minimum reduced-distance between a point and a node""" if tree.euclidean: return euclidean_dist_to_rdist(min_dist(tree, i_node, pt)) diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 415fa0292fcda..3cafadc475463 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -538,7 +538,7 @@ cdef class NeighborsHeap: self._sort() return self.distances.base, self.indices.base - cdef inline DTYPE_t largest(self, ITYPE_t row) nogil except -1: + cdef inline DTYPE_t largest(self, ITYPE_t row) except -1 nogil: """Return the largest distance in the given row""" return self.distances[row, 0] @@ -546,7 +546,7 @@ cdef class NeighborsHeap: return self._push(row, val, i_val) cdef int _push(self, ITYPE_t row, DTYPE_t val, - ITYPE_t i_val) nogil except -1: + ITYPE_t i_val) except -1 nogil: """push (val, i_val) into the given row""" return heap_push( values=&self.distances[row, 0], @@ -846,7 +846,7 @@ cdef class BinaryTree: # with numbers of points between leaf_size and 2 * leaf_size self.n_levels = int( np.log2(fmax(1, (n_samples - 1) / self.leaf_size)) + 1) - self.n_nodes = (2 ** self.n_levels) - 1 + self.n_nodes = (2 ** int(self.n_levels)) - 1 # allocate arrays for storage self.idx_array = np.arange(n_samples, dtype=ITYPE) @@ -980,7 +980,7 @@ cdef class BinaryTree: ) cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, - ITYPE_t size) nogil except -1: + ITYPE_t size) except -1 nogil: """Compute the distance between arrays x1 and x2""" self.n_calls += 1 if self.euclidean: @@ -989,7 +989,7 @@ cdef class BinaryTree: return self.dist_metric.dist(x1, x2, size) cdef inline DTYPE_t rdist(self, DTYPE_t* x1, DTYPE_t* x2, - ITYPE_t size) nogil except -1: + ITYPE_t size) except -1 nogil: """Compute the reduced distance between arrays x1 and x2. The reduced distance, defined for some metrics, is a quantity which @@ -1574,7 +1574,7 @@ cdef class BinaryTree: cdef int _query_single_depthfirst(self, ITYPE_t i_node, DTYPE_t* pt, ITYPE_t i_pt, NeighborsHeap heap, - DTYPE_t reduced_dist_LB) nogil except -1: + DTYPE_t reduced_dist_LB) except -1 nogil: """Recursive Single-tree k-neighbors query, depth-first approach""" cdef NodeData_t node_info = self.node_data[i_node] diff --git a/sklearn/neighbors/_kd_tree.pyx b/sklearn/neighbors/_kd_tree.pyx index f8351afcd98be..a5db18b4ad772 100644 --- a/sklearn/neighbors/_kd_tree.pyx +++ b/sklearn/neighbors/_kd_tree.pyx @@ -82,7 +82,7 @@ cdef int init_node(BinaryTree tree, NodeData_t[::1] node_data, ITYPE_t i_node, cdef DTYPE_t min_rdist(BinaryTree tree, ITYPE_t i_node, - DTYPE_t* pt) nogil except -1: + DTYPE_t* pt) except -1 nogil: """Compute the minimum reduced-distance between a point and a node""" cdef ITYPE_t n_features = tree.data.shape[1] cdef DTYPE_t d, d_lo, d_hi, rdist=0.0 @@ -143,7 +143,7 @@ cdef DTYPE_t max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt) except -1: cdef inline int min_max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt, - DTYPE_t* min_dist, DTYPE_t* max_dist) nogil except -1: + DTYPE_t* min_dist, DTYPE_t* max_dist) except -1 nogil: """Compute the minimum and maximum distance between a point and a node""" cdef ITYPE_t n_features = tree.data.shape[1] diff --git a/sklearn/neighbors/_quad_tree.pxd b/sklearn/neighbors/_quad_tree.pxd index 4a6fb7fb5ef2c..5bcbbf436b598 100644 --- a/sklearn/neighbors/_quad_tree.pxd +++ b/sklearn/neighbors/_quad_tree.pxd @@ -68,7 +68,7 @@ cdef class _QuadTree: # Point insertion methods cdef int insert_point(self, DTYPE_t[3] point, SIZE_t point_index, - SIZE_t cell_id=*) nogil except -1 + SIZE_t cell_id=*) except -1 nogil cdef SIZE_t _insert_point_in_new_child(self, DTYPE_t[3] point, Cell* cell, SIZE_t point_index, SIZE_t size=* ) nogil @@ -87,10 +87,10 @@ cdef class _QuadTree: # Private methods cdef int _check_point_in_cell(self, DTYPE_t[3] point, Cell* cell - ) nogil except -1 + ) except -1 nogil # Private array manipulation to manage the ``cells`` array - cdef int _resize(self, SIZE_t capacity) nogil except -1 - cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1 - cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=*) nogil except -1 + cdef int _resize(self, SIZE_t capacity) except -1 nogil + cdef int _resize_c(self, SIZE_t capacity=*) except -1 nogil + cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=*) except -1 nogil cdef cnp.ndarray _get_cell_ndarray(self) diff --git a/sklearn/neighbors/_quad_tree.pyx b/sklearn/neighbors/_quad_tree.pyx index f5ad256119cae..a600a0f57c6d9 100644 --- a/sklearn/neighbors/_quad_tree.pyx +++ b/sklearn/neighbors/_quad_tree.pyx @@ -52,7 +52,7 @@ cdef class _QuadTree: # Parameters of the tree self.n_dimensions = n_dimensions self.verbose = verbose - self.n_cells_per_cell = 2 ** self.n_dimensions + self.n_cells_per_cell = 2 ** int(self.n_dimensions) # Inner structures self.max_depth = 0 @@ -112,7 +112,7 @@ cdef class _QuadTree: self._resize(capacity=self.cell_count) cdef int insert_point(self, DTYPE_t[3] point, SIZE_t point_index, - SIZE_t cell_id=0) nogil except -1: + SIZE_t cell_id=0) except -1 nogil: """Insert a point in the QuadTree.""" cdef int ax cdef SIZE_t selected_child @@ -300,7 +300,7 @@ cdef class _QuadTree: self.cell_count += 1 cdef int _check_point_in_cell(self, DTYPE_t[3] point, Cell* cell - ) nogil except -1: + ) except -1 nogil: """Check that the given point is in the cell boundaries.""" if self.verbose >= 50: @@ -459,7 +459,7 @@ cdef class _QuadTree: return self._get_cell(query_pt, 0) cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=0 - ) nogil except -1: + ) except -1 nogil: """guts of get_cell. Return the id of the cell containing the query point or raise ValueError @@ -554,7 +554,7 @@ cdef class _QuadTree: raise ValueError("Can't initialize array!") return arr - cdef int _resize(self, SIZE_t capacity) nogil except -1: + cdef int _resize(self, SIZE_t capacity) except -1 nogil: """Resize all inner arrays to `capacity`, if `capacity` == -1, then double the size of the inner arrays. @@ -566,7 +566,7 @@ cdef class _QuadTree: with gil: raise MemoryError() - cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1: + cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) except -1 nogil: """Guts of _resize Returns -1 in case of failure to allocate memory (and raise MemoryError) diff --git a/sklearn/tree/_criterion.pxd b/sklearn/tree/_criterion.pxd index 3c6217a8c272c..0fc178d1c5d99 100644 --- a/sklearn/tree/_criterion.pxd +++ b/sklearn/tree/_criterion.pxd @@ -49,10 +49,10 @@ cdef class Criterion: const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end - ) nogil except -1 - cdef int reset(self) nogil except -1 - cdef int reverse_reset(self) nogil except -1 - cdef int update(self, SIZE_t new_pos) nogil except -1 + ) except -1 nogil + cdef int reset(self) except -1 nogil + cdef int reverse_reset(self) except -1 nogil + cdef int update(self, SIZE_t new_pos) except -1 nogil cdef double node_impurity(self) nogil cdef void children_impurity( self, diff --git a/sklearn/tree/_criterion.pyx b/sklearn/tree/_criterion.pyx index e9f32b6e06ef9..83df937a126ba 100644 --- a/sklearn/tree/_criterion.pyx +++ b/sklearn/tree/_criterion.pyx @@ -49,7 +49,7 @@ cdef class Criterion: const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end, - ) nogil except -1: + ) except -1 nogil: """Placeholder for a method which will initialize the criterion. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -75,21 +75,21 @@ cdef class Criterion: """ pass - cdef int reset(self) nogil except -1: + cdef int reset(self) except -1 nogil: """Reset the criterion at pos=start. This method must be implemented by the subclass. """ pass - cdef int reverse_reset(self) nogil except -1: + cdef int reverse_reset(self) except -1 nogil: """Reset the criterion at pos=end. This method must be implemented by the subclass. """ pass - cdef int update(self, SIZE_t new_pos) nogil except -1: + cdef int update(self, SIZE_t new_pos) except -1 nogil: """Updated statistics by moving sample_indices[pos:new_pos] to the left child. This updates the collected statistics by moving sample_indices[pos:new_pos] @@ -257,7 +257,7 @@ cdef class ClassificationCriterion(Criterion): const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end - ) nogil except -1: + ) except -1 nogil: """Initialize the criterion. This initializes the criterion at node sample_indices[start:end] and children @@ -319,7 +319,7 @@ cdef class ClassificationCriterion(Criterion): self.reset() return 0 - cdef int reset(self) nogil except -1: + cdef int reset(self) except -1 nogil: """Reset the criterion at pos=start. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -336,7 +336,7 @@ cdef class ClassificationCriterion(Criterion): memcpy(&self.sum_right[k, 0], &self.sum_total[k, 0], self.n_classes[k] * sizeof(double)) return 0 - cdef int reverse_reset(self) nogil except -1: + cdef int reverse_reset(self) except -1 nogil: """Reset the criterion at pos=end. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -353,7 +353,7 @@ cdef class ClassificationCriterion(Criterion): memcpy(&self.sum_left[k, 0], &self.sum_total[k, 0], self.n_classes[k] * sizeof(double)) return 0 - cdef int update(self, SIZE_t new_pos) nogil except -1: + cdef int update(self, SIZE_t new_pos) except -1 nogil: """Updated statistics by moving sample_indices[pos:new_pos] to the left child. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -651,7 +651,7 @@ cdef class RegressionCriterion(Criterion): const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end, - ) nogil except -1: + ) except -1 nogil: """Initialize the criterion. This initializes the criterion at node sample_indices[start:end] and children @@ -694,7 +694,7 @@ cdef class RegressionCriterion(Criterion): self.reset() return 0 - cdef int reset(self) nogil except -1: + cdef int reset(self) except -1 nogil: """Reset the criterion at pos=start.""" cdef SIZE_t n_bytes = self.n_outputs * sizeof(double) memset(&self.sum_left[0], 0, n_bytes) @@ -705,7 +705,7 @@ cdef class RegressionCriterion(Criterion): self.pos = self.start return 0 - cdef int reverse_reset(self) nogil except -1: + cdef int reverse_reset(self) except -1 nogil: """Reset the criterion at pos=end.""" cdef SIZE_t n_bytes = self.n_outputs * sizeof(double) memset(&self.sum_right[0], 0, n_bytes) @@ -716,7 +716,7 @@ cdef class RegressionCriterion(Criterion): self.pos = self.end return 0 - cdef int update(self, SIZE_t new_pos) nogil except -1: + cdef int update(self, SIZE_t new_pos) except -1 nogil: """Updated statistics by moving sample_indices[pos:new_pos] to the left.""" cdef const DOUBLE_t[:] sample_weight = self.sample_weight cdef const SIZE_t[:] sample_indices = self.sample_indices @@ -931,7 +931,7 @@ cdef class MAE(RegressionCriterion): const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end, - ) nogil except -1: + ) except -1 nogil: """Initialize the criterion. This initializes the criterion at node sample_indices[start:end] and children @@ -981,7 +981,7 @@ cdef class MAE(RegressionCriterion): self.reset() return 0 - cdef int reset(self) nogil except -1: + cdef int reset(self) except -1 nogil: """Reset the criterion at pos=start. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1012,7 +1012,7 @@ cdef class MAE(RegressionCriterion): weight) return 0 - cdef int reverse_reset(self) nogil except -1: + cdef int reverse_reset(self) except -1 nogil: """Reset the criterion at pos=end. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1040,7 +1040,7 @@ cdef class MAE(RegressionCriterion): weight) return 0 - cdef int update(self, SIZE_t new_pos) nogil except -1: + cdef int update(self, SIZE_t new_pos) except -1 nogil: """Updated statistics by moving sample_indices[pos:new_pos] to the left. Returns -1 in case of failure to allocate memory (and raise MemoryError) diff --git a/sklearn/tree/_splitter.pxd b/sklearn/tree/_splitter.pxd index f97761879922a..fa1327512e9e9 100644 --- a/sklearn/tree/_splitter.pxd +++ b/sklearn/tree/_splitter.pxd @@ -86,14 +86,14 @@ cdef class Splitter: SIZE_t start, SIZE_t end, double* weighted_n_node_samples - ) nogil except -1 + ) except -1 nogil cdef int node_split( self, double impurity, # Impurity of the node SplitRecord* split, SIZE_t* n_constant_features - ) nogil except -1 + ) except -1 nogil cdef void node_value(self, double* dest) nogil diff --git a/sklearn/tree/_splitter.pyx b/sklearn/tree/_splitter.pyx index 181fc682e3ef0..0990963ea61d3 100644 --- a/sklearn/tree/_splitter.pyx +++ b/sklearn/tree/_splitter.pyx @@ -167,7 +167,7 @@ cdef class Splitter: return 0 cdef int node_reset(self, SIZE_t start, SIZE_t end, - double* weighted_n_node_samples) nogil except -1: + double* weighted_n_node_samples) except -1 nogil: """Reset splitter on node samples[start:end]. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -199,7 +199,7 @@ cdef class Splitter: return 0 cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) nogil except -1: + SIZE_t* n_constant_features) except -1 nogil: """Find the best split on node samples[start:end]. This is a placeholder method. The majority of computation will be done @@ -248,7 +248,7 @@ cdef class BaseDenseSplitter(Splitter): cdef class BestSplitter(BaseDenseSplitter): """Splitter for finding the best split.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) nogil except -1: + SIZE_t* n_constant_features) except -1 nogil: """Find the best split on node samples[start:end] Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -554,7 +554,7 @@ cdef void heapsort(DTYPE_t* Xf, SIZE_t* samples, SIZE_t n) nogil: cdef class RandomSplitter(BaseDenseSplitter): """Splitter for finding the best random split.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) nogil except -1: + SIZE_t* n_constant_features) except -1 nogil: """Find the best random split on node samples[start:end] Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1051,7 +1051,7 @@ cdef inline void sparse_swap(SIZE_t[::1] index_to_samples, SIZE_t[::1] samples, cdef class BestSparseSplitter(BaseSparseSplitter): """Splitter for finding the best split, using the sparse data.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) nogil except -1: + SIZE_t* n_constant_features) except -1 nogil: """Find the best split on node samples[start:end], using sparse features Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1269,7 +1269,7 @@ cdef class BestSparseSplitter(BaseSparseSplitter): cdef class RandomSparseSplitter(BaseSparseSplitter): """Splitter for finding a random split, using the sparse data.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) nogil except -1: + SIZE_t* n_constant_features) except -1 nogil: """Find a random split on node samples[start:end], using sparse features Returns -1 in case of failure to allocate memory (and raise MemoryError) diff --git a/sklearn/tree/_tree.pxd b/sklearn/tree/_tree.pxd index 55895a8279828..f2c5169f00534 100644 --- a/sklearn/tree/_tree.pxd +++ b/sklearn/tree/_tree.pxd @@ -58,9 +58,9 @@ cdef class Tree: cdef SIZE_t _add_node(self, SIZE_t parent, bint is_left, bint is_leaf, SIZE_t feature, double threshold, double impurity, SIZE_t n_node_samples, - double weighted_n_node_samples) nogil except -1 - cdef int _resize(self, SIZE_t capacity) nogil except -1 - cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1 + double weighted_n_node_samples) except -1 nogil + cdef int _resize(self, SIZE_t capacity) except -1 nogil + cdef int _resize_c(self, SIZE_t capacity=*) except -1 nogil cdef cnp.ndarray _get_value_ndarray(self) cdef cnp.ndarray _get_node_ndarray(self) diff --git a/sklearn/tree/_tree.pyx b/sklearn/tree/_tree.pyx index 68cb2475d3868..eda53d5661e45 100644 --- a/sklearn/tree/_tree.pyx +++ b/sklearn/tree/_tree.pyx @@ -155,7 +155,7 @@ cdef class DepthFirstTreeBuilder(TreeBuilder): cdef int init_capacity if tree.max_depth <= 10: - init_capacity = (2 ** (tree.max_depth + 1)) - 1 + init_capacity = (2 ** int(tree.max_depth + 1)) - 1 else: init_capacity = 2047 @@ -437,7 +437,7 @@ cdef class BestFirstTreeBuilder(TreeBuilder): SIZE_t start, SIZE_t end, double impurity, bint is_first, bint is_left, Node* parent, SIZE_t depth, - FrontierRecord* res) nogil except -1: + FrontierRecord* res) except -1 nogil: """Adds node w/ partition ``[start, end)`` to the frontier. """ cdef SplitRecord split cdef SIZE_t node_id @@ -688,7 +688,7 @@ cdef class Tree: value = memcpy(self.value, ( value_ndarray).data, self.capacity * self.value_stride * sizeof(double)) - cdef int _resize(self, SIZE_t capacity) nogil except -1: + cdef int _resize(self, SIZE_t capacity) except -1 nogil: """Resize all inner arrays to `capacity`, if `capacity` == -1, then double the size of the inner arrays. @@ -700,7 +700,7 @@ cdef class Tree: with gil: raise MemoryError() - cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1: + cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) except -1 nogil: """Guts of _resize Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -734,7 +734,7 @@ cdef class Tree: cdef SIZE_t _add_node(self, SIZE_t parent, bint is_left, bint is_leaf, SIZE_t feature, double threshold, double impurity, SIZE_t n_node_samples, - double weighted_n_node_samples) nogil except -1: + double weighted_n_node_samples) except -1 nogil: """Add a node to the tree. The new node registers itself as the child of its parent. diff --git a/sklearn/tree/_utils.pxd b/sklearn/tree/_utils.pxd index 9d41b757d85dc..5e9768b3b9afd 100644 --- a/sklearn/tree/_utils.pxd +++ b/sklearn/tree/_utils.pxd @@ -43,7 +43,7 @@ ctypedef fused realloc_ptr: (Cell*) (Node**) -cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) nogil except * +cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil cdef cnp.ndarray sizet_ptr_to_ndarray(SIZE_t* data, SIZE_t size) @@ -74,9 +74,9 @@ cdef class WeightedPQueue: cdef WeightedPQueueRecord* array_ cdef bint is_empty(self) nogil - cdef int reset(self) nogil except -1 + cdef int reset(self) except -1 nogil cdef SIZE_t size(self) nogil - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1 + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil cdef int remove(self, DOUBLE_t data, DOUBLE_t weight) nogil cdef int pop(self, DOUBLE_t* data, DOUBLE_t* weight) nogil cdef int peek(self, DOUBLE_t* data, DOUBLE_t* weight) nogil @@ -97,8 +97,8 @@ cdef class WeightedMedianCalculator: # = w[0] + w[1] + ... + w[k-1] cdef SIZE_t size(self) nogil - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1 - cdef int reset(self) nogil except -1 + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil + cdef int reset(self) except -1 nogil cdef int update_median_parameters_post_push( self, DOUBLE_t data, DOUBLE_t weight, DOUBLE_t original_median) nogil diff --git a/sklearn/tree/_utils.pyx b/sklearn/tree/_utils.pyx index cf4d6aebf78c3..a1471d550b48f 100644 --- a/sklearn/tree/_utils.pyx +++ b/sklearn/tree/_utils.pyx @@ -20,7 +20,7 @@ from ..utils._random cimport our_rand_r # Helper functions # ============================================================================= -cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) nogil except *: +cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil: # sizeof(realloc_ptr[0]) would be more like idiomatic C, but causes Cython # 0.20.1 to crash. cdef size_t nbytes = nelems * sizeof(p[0][0]) @@ -102,7 +102,7 @@ cdef class WeightedPQueue: def __dealloc__(self): free(self.array_) - cdef int reset(self) nogil except -1: + cdef int reset(self) except -1 nogil: """Reset the WeightedPQueue to its state at construction Return -1 in case of failure to allocate memory (and raise MemoryError) @@ -119,7 +119,7 @@ cdef class WeightedPQueue: cdef SIZE_t size(self) nogil: return self.array_ptr - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1: + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil: """Push record on the array. Return -1 in case of failure to allocate memory (and raise MemoryError) @@ -277,7 +277,7 @@ cdef class WeightedMedianCalculator: WeightedMedianCalculator""" return self.samples.size() - cdef int reset(self) nogil except -1: + cdef int reset(self) except -1 nogil: """Reset the WeightedMedianCalculator to its state at construction Return -1 in case of failure to allocate memory (and raise MemoryError) @@ -291,7 +291,7 @@ cdef class WeightedMedianCalculator: self.sum_w_0_k = 0 return 0 - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1: + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil: """Push a value and its associated weight to the WeightedMedianCalculator Return -1 in case of failure to allocate memory (and raise MemoryError) From 98485452a9633889fd652607d740ba7afb3e0d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 17:06:02 +0100 Subject: [PATCH 04/14] [scipy-dev] trigger CI From 5901476a9725c0bc2994f90435d0c85366ff938d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 17:49:18 +0100 Subject: [PATCH 05/14] Use directive only for Cython 3 --- sklearn/_build_utils/__init__.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sklearn/_build_utils/__init__.py b/sklearn/_build_utils/__init__.py index 2bfe48af37650..d7f3959ca4ac6 100644 --- a/sklearn/_build_utils/__init__.py +++ b/sklearn/_build_utils/__init__.py @@ -40,6 +40,7 @@ def cythonize_extensions(extension): """Check that a recent Cython is available and cythonize extensions""" _check_cython_version() from Cython.Build import cythonize + import Cython # Fast fail before cythonization if compiler fails compiling basic test # code even without OpenMP @@ -70,21 +71,25 @@ def cythonize_extensions(extension): os.environ.get("SKLEARN_ENABLE_DEBUG_CYTHON_DIRECTIVES", "0") != "0" ) + compiler_directives = { + "language_level": 3, + "boundscheck": cython_enable_debug_directives, + "wraparound": False, + "initializedcheck": False, + "nonecheck": False, + "cdivision": True, + } + + if not Cython.__version__.startswith("0."): + compiler_directives["legacy_implicit_noexcept"] = True + return cythonize( extension, nthreads=n_jobs, compile_time_env={ "SKLEARN_OPENMP_PARALLELISM_ENABLED": sklearn._OPENMP_SUPPORTED }, - compiler_directives={ - "language_level": 3, - "boundscheck": cython_enable_debug_directives, - "wraparound": False, - "initializedcheck": False, - "nonecheck": False, - "cdivision": True, - "legacy_implicit_noexcept": True, - }, + compiler_directives=compiler_directives, ) From ff5a2abd27ff85d6af67edd950b7909a1e1b586d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 17:50:05 +0100 Subject: [PATCH 06/14] Revert except nogil reordering, it only seems to compile for Cython 3 --- sklearn/metrics/_dist_metrics.pxd.tp | 24 ++--- sklearn/metrics/_dist_metrics.pyx.tp | 144 +++++++++++++-------------- sklearn/neighbors/_ball_tree.pyx | 6 +- sklearn/neighbors/_binary_tree.pxi | 10 +- sklearn/neighbors/_kd_tree.pyx | 4 +- sklearn/neighbors/_quad_tree.pxd | 10 +- sklearn/neighbors/_quad_tree.pyx | 10 +- sklearn/tree/_criterion.pxd | 8 +- sklearn/tree/_criterion.pyx | 32 +++--- sklearn/tree/_splitter.pxd | 4 +- sklearn/tree/_splitter.pyx | 12 +-- sklearn/tree/_tree.pxd | 6 +- sklearn/tree/_tree.pyx | 8 +- sklearn/tree/_utils.pxd | 10 +- sklearn/tree/_utils.pyx | 10 +- 15 files changed, 149 insertions(+), 149 deletions(-) diff --git a/sklearn/metrics/_dist_metrics.pxd.tp b/sklearn/metrics/_dist_metrics.pxd.tp index 7d64ca55cc515..e0e67758f5023 100644 --- a/sklearn/metrics/_dist_metrics.pxd.tp +++ b/sklearn/metrics/_dist_metrics.pxd.tp @@ -41,7 +41,7 @@ cdef inline DTYPE_t euclidean_dist{{name_suffix}}( const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, -) except -1 nogil: +) nogil except -1: cdef DTYPE_t tmp, d=0 cdef cnp.intp_t j for j in range(size): @@ -54,7 +54,7 @@ cdef inline DTYPE_t euclidean_rdist{{name_suffix}}( const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, -) except -1 nogil: +) nogil except -1: cdef DTYPE_t tmp, d=0 cdef cnp.intp_t j for j in range(size): @@ -63,11 +63,11 @@ cdef inline DTYPE_t euclidean_rdist{{name_suffix}}( return d -cdef inline DTYPE_t euclidean_dist_to_rdist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil: +cdef inline DTYPE_t euclidean_dist_to_rdist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) nogil except -1: return dist * dist -cdef inline DTYPE_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil: +cdef inline DTYPE_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) nogil except -1: return sqrt(dist) @@ -90,14 +90,14 @@ cdef class DistanceMetric{{name_suffix}}: const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil + ) nogil except -1 cdef DTYPE_t rdist( self, const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil + ) nogil except -1 cdef DTYPE_t dist_csr( self, @@ -110,7 +110,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil + ) nogil except -1 cdef DTYPE_t rdist_csr( self, @@ -123,7 +123,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil + ) nogil except -1 cdef int pdist( self, @@ -145,7 +145,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x1_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) except -1 nogil + ) nogil except -1 cdef int cdist_csr( self, @@ -157,10 +157,10 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x2_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) except -1 nogil + ) nogil except -1 - cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil + cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1 - cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil + cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1 {{endfor}} diff --git a/sklearn/metrics/_dist_metrics.pyx.tp b/sklearn/metrics/_dist_metrics.pyx.tp index 0306a41d27107..1e4a9429af03f 100644 --- a/sklearn/metrics/_dist_metrics.pyx.tp +++ b/sklearn/metrics/_dist_metrics.pyx.tp @@ -333,7 +333,7 @@ cdef class DistanceMetric{{name_suffix}}: const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: """Compute the distance between vectors x1 and x2 This should be overridden in a base class. @@ -345,7 +345,7 @@ cdef class DistanceMetric{{name_suffix}}: const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: """Compute the rank-preserving surrogate distance between vectors x1 and x2. This can optionally be overridden in a base class. @@ -397,7 +397,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: """Compute the distance between vectors x1 and x2 represented under the CSR format. @@ -422,7 +422,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x1_indices, const {{INPUT_DTYPE_t}}* x2_data, const SPARSE_INDEX_TYPE_t[:] x2_indices, - ) except -1 nogil: + ) nogil except -1: Where callers would use slicing on the original CSR data and indices memoryviews: @@ -463,7 +463,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: """Distance between rows of CSR matrices x1 and x2. This can optionally be overridden in a subclass. @@ -500,7 +500,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x1_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) except -1 nogil: + ) nogil except -1: """Pairwise distances between rows in CSR matrix X. Note that this implementation is twice faster than cdist_csr(X, X) @@ -540,7 +540,7 @@ cdef class DistanceMetric{{name_suffix}}: const SPARSE_INDEX_TYPE_t[:] x2_indptr, const ITYPE_t size, DTYPE_t[:, ::1] D, - ) except -1 nogil: + ) nogil except -1: """Compute the cross-pairwise distances between arrays X and Y represented in the CSR format.""" cdef: @@ -569,11 +569,11 @@ cdef class DistanceMetric{{name_suffix}}: ) return 0 - cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: """Convert the rank-preserving surrogate distance to the distance""" return rdist - cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: """Convert the distance to the rank-preserving surrogate distance""" return dist @@ -867,20 +867,20 @@ cdef class EuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return euclidean_dist{{name_suffix}}(x1, x2, size) cdef inline DTYPE_t rdist(self, const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return euclidean_rdist{{name_suffix}}(x1, x2, size) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: return sqrt(rdist) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: return dist * dist def rdist_to_dist(self, rdist): @@ -900,7 +900,7 @@ cdef class EuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -952,7 +952,7 @@ cdef class EuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return sqrt( self.rdist_csr( x1_data, @@ -989,7 +989,7 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t tmp, d=0 cdef cnp.intp_t j for j in range(size): @@ -1002,13 +1002,13 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return sqrt(self.rdist(x1, x2, size)) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: return sqrt(rdist) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: return dist * dist def rdist_to_dist(self, rdist): @@ -1028,7 +1028,7 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1081,7 +1081,7 @@ cdef class SEuclideanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return sqrt( self.rdist_csr( x1_data, @@ -1112,7 +1112,7 @@ cdef class ManhattanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t d = 0 cdef cnp.intp_t j for j in range(size): @@ -1130,7 +1130,7 @@ cdef class ManhattanDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1195,7 +1195,7 @@ cdef class ChebyshevDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t d = 0 cdef cnp.intp_t j for j in range(size): @@ -1214,7 +1214,7 @@ cdef class ChebyshevDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1308,7 +1308,7 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t d=0 cdef cnp.intp_t j cdef bint has_w = self.size > 0 @@ -1325,13 +1325,13 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return pow(self.rdist(x1, x2, size), 1. / self.p) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: return pow(rdist, 1. / self.p) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: return pow(dist, self.p) def rdist_to_dist(self, rdist): @@ -1351,7 +1351,7 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1431,7 +1431,7 @@ cdef class MinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return pow( self.rdist_csr( x1_data, @@ -1496,7 +1496,7 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t d = 0 cdef cnp.intp_t j @@ -1509,13 +1509,13 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return pow(self.rdist(x1, x2, size), 1. / self.p) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: return pow(rdist, 1. / self.p) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: return pow(dist, self.p) def rdist_to_dist(self, rdist): @@ -1535,7 +1535,7 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1585,7 +1585,7 @@ cdef class WMinkowskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return pow( self.rdist_csr( x1_data, @@ -1644,7 +1644,7 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t tmp, d = 0 cdef cnp.intp_t i, j @@ -1664,13 +1664,13 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return sqrt(self.rdist(x1, x2, size)) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: return sqrt(rdist) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: return dist * dist def rdist_to_dist(self, rdist): @@ -1690,7 +1690,7 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1744,7 +1744,7 @@ cdef class MahalanobisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return sqrt( self.rdist_csr( x1_data, @@ -1775,7 +1775,7 @@ cdef class HammingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int n_unequal = 0 cdef cnp.intp_t j for j in range(size): @@ -1795,7 +1795,7 @@ cdef class HammingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1850,7 +1850,7 @@ cdef class CanberraDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t denom, d = 0 cdef cnp.intp_t j for j in range(size): @@ -1870,7 +1870,7 @@ cdef class CanberraDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -1925,7 +1925,7 @@ cdef class BrayCurtisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t num = 0, denom = 0 cdef cnp.intp_t j for j in range(size): @@ -1947,7 +1947,7 @@ cdef class BrayCurtisDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2005,7 +2005,7 @@ cdef class JaccardDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_eq = 0, nnz = 0 cdef cnp.intp_t j for j in range(size): @@ -2031,7 +2031,7 @@ cdef class JaccardDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2094,7 +2094,7 @@ cdef class MatchingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2114,7 +2114,7 @@ cdef class MatchingDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2169,7 +2169,7 @@ cdef class DiceDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_neq = 0, n_tt = 0 cdef cnp.intp_t j for j in range(size): @@ -2190,7 +2190,7 @@ cdef class DiceDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2250,7 +2250,7 @@ cdef class KulsinskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_tt = 0, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2271,7 +2271,7 @@ cdef class KulsinskiDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2329,7 +2329,7 @@ cdef class RogersTanimotoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2349,7 +2349,7 @@ cdef class RogersTanimotoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2406,7 +2406,7 @@ cdef class RussellRaoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_tt = 0 cdef cnp.intp_t j for j in range(size): @@ -2426,7 +2426,7 @@ cdef class RussellRaoDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2476,7 +2476,7 @@ cdef class SokalMichenerDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2496,7 +2496,7 @@ cdef class SokalMichenerDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2553,7 +2553,7 @@ cdef class SokalSneathDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef int tf1, tf2, n_tt = 0, n_neq = 0 cdef cnp.intp_t j for j in range(size): @@ -2574,7 +2574,7 @@ cdef class SokalSneathDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2641,7 +2641,7 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef DTYPE_t sin_0 = sin(0.5 * ((x1[0]) - (x2[0]))) cdef DTYPE_t sin_1 = sin(0.5 * ((x1[1]) - (x2[1]))) return (sin_0 * sin_0 + cos(x1[0]) * cos(x2[0]) * sin_1 * sin_1) @@ -2650,13 +2650,13 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return 2 * asin(sqrt(self.rdist(x1, x2, size))) - cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil: + cdef inline DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) nogil except -1: return 2 * asin(sqrt(rdist)) - cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil: + cdef inline DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) nogil except -1: cdef DTYPE_t tmp = sin(0.5 * dist) return tmp * tmp @@ -2678,7 +2678,7 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return 2 * asin(sqrt(self.rdist_csr( x1_data, x1_indices, @@ -2702,7 +2702,7 @@ cdef class HaversineDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const SPARSE_INDEX_TYPE_t x2_start, const SPARSE_INDEX_TYPE_t x2_end, const ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: cdef: cnp.npy_intp ix1, ix2 @@ -2788,7 +2788,7 @@ cdef class PyFuncDistance{{name_suffix}}(DistanceMetric{{name_suffix}}): const {{INPUT_DTYPE_t}}* x1, const {{INPUT_DTYPE_t}}* x2, ITYPE_t size, - ) except -1 nogil: + ) nogil except -1: return self._dist(x1, x2, size) cdef inline DTYPE_t _dist( diff --git a/sklearn/neighbors/_ball_tree.pyx b/sklearn/neighbors/_ball_tree.pyx index 70135c0a40fc6..094a8826acfb9 100644 --- a/sklearn/neighbors/_ball_tree.pyx +++ b/sklearn/neighbors/_ball_tree.pyx @@ -100,7 +100,7 @@ cdef int init_node(BinaryTree tree, NodeData_t[::1] node_data, ITYPE_t i_node, cdef inline DTYPE_t min_dist(BinaryTree tree, ITYPE_t i_node, - DTYPE_t* pt) except -1 nogil: + DTYPE_t* pt) nogil except -1: """Compute the minimum distance between a point and a node""" cdef DTYPE_t dist_pt = tree.dist(pt, &tree.node_bounds[0, i_node, 0], tree.data.shape[1]) @@ -116,7 +116,7 @@ cdef inline DTYPE_t max_dist(BinaryTree tree, ITYPE_t i_node, cdef inline int min_max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt, - DTYPE_t* min_dist, DTYPE_t* max_dist) except -1 nogil: + DTYPE_t* min_dist, DTYPE_t* max_dist) nogil except -1: """Compute the minimum and maximum distance between a point and a node""" cdef DTYPE_t dist_pt = tree.dist(pt, &tree.node_bounds[0, i_node, 0], tree.data.shape[1]) @@ -127,7 +127,7 @@ cdef inline int min_max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt, cdef inline DTYPE_t min_rdist(BinaryTree tree, ITYPE_t i_node, - DTYPE_t* pt) except -1 nogil: + DTYPE_t* pt) nogil except -1: """Compute the minimum reduced-distance between a point and a node""" if tree.euclidean: return euclidean_dist_to_rdist(min_dist(tree, i_node, pt)) diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 3cafadc475463..cca4d8311de1c 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -538,7 +538,7 @@ cdef class NeighborsHeap: self._sort() return self.distances.base, self.indices.base - cdef inline DTYPE_t largest(self, ITYPE_t row) except -1 nogil: + cdef inline DTYPE_t largest(self, ITYPE_t row) nogil except -1: """Return the largest distance in the given row""" return self.distances[row, 0] @@ -546,7 +546,7 @@ cdef class NeighborsHeap: return self._push(row, val, i_val) cdef int _push(self, ITYPE_t row, DTYPE_t val, - ITYPE_t i_val) except -1 nogil: + ITYPE_t i_val) nogil except -1: """push (val, i_val) into the given row""" return heap_push( values=&self.distances[row, 0], @@ -980,7 +980,7 @@ cdef class BinaryTree: ) cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, - ITYPE_t size) except -1 nogil: + ITYPE_t size) nogil except -1: """Compute the distance between arrays x1 and x2""" self.n_calls += 1 if self.euclidean: @@ -989,7 +989,7 @@ cdef class BinaryTree: return self.dist_metric.dist(x1, x2, size) cdef inline DTYPE_t rdist(self, DTYPE_t* x1, DTYPE_t* x2, - ITYPE_t size) except -1 nogil: + ITYPE_t size) nogil except -1: """Compute the reduced distance between arrays x1 and x2. The reduced distance, defined for some metrics, is a quantity which @@ -1574,7 +1574,7 @@ cdef class BinaryTree: cdef int _query_single_depthfirst(self, ITYPE_t i_node, DTYPE_t* pt, ITYPE_t i_pt, NeighborsHeap heap, - DTYPE_t reduced_dist_LB) except -1 nogil: + DTYPE_t reduced_dist_LB) nogil except -1: """Recursive Single-tree k-neighbors query, depth-first approach""" cdef NodeData_t node_info = self.node_data[i_node] diff --git a/sklearn/neighbors/_kd_tree.pyx b/sklearn/neighbors/_kd_tree.pyx index a5db18b4ad772..f8351afcd98be 100644 --- a/sklearn/neighbors/_kd_tree.pyx +++ b/sklearn/neighbors/_kd_tree.pyx @@ -82,7 +82,7 @@ cdef int init_node(BinaryTree tree, NodeData_t[::1] node_data, ITYPE_t i_node, cdef DTYPE_t min_rdist(BinaryTree tree, ITYPE_t i_node, - DTYPE_t* pt) except -1 nogil: + DTYPE_t* pt) nogil except -1: """Compute the minimum reduced-distance between a point and a node""" cdef ITYPE_t n_features = tree.data.shape[1] cdef DTYPE_t d, d_lo, d_hi, rdist=0.0 @@ -143,7 +143,7 @@ cdef DTYPE_t max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt) except -1: cdef inline int min_max_dist(BinaryTree tree, ITYPE_t i_node, DTYPE_t* pt, - DTYPE_t* min_dist, DTYPE_t* max_dist) except -1 nogil: + DTYPE_t* min_dist, DTYPE_t* max_dist) nogil except -1: """Compute the minimum and maximum distance between a point and a node""" cdef ITYPE_t n_features = tree.data.shape[1] diff --git a/sklearn/neighbors/_quad_tree.pxd b/sklearn/neighbors/_quad_tree.pxd index 13346ed9e25e1..57f4ed453a633 100644 --- a/sklearn/neighbors/_quad_tree.pxd +++ b/sklearn/neighbors/_quad_tree.pxd @@ -68,7 +68,7 @@ cdef class _QuadTree: # Point insertion methods cdef int insert_point(self, DTYPE_t[3] point, SIZE_t point_index, - SIZE_t cell_id=*) except -1 nogil + SIZE_t cell_id=*) nogil except -1 cdef SIZE_t _insert_point_in_new_child(self, DTYPE_t[3] point, Cell* cell, SIZE_t point_index, SIZE_t size=* ) nogil @@ -87,10 +87,10 @@ cdef class _QuadTree: # Private methods cdef int _check_point_in_cell(self, DTYPE_t[3] point, Cell* cell - ) except -1 nogil + ) nogil except -1 # Private array manipulation to manage the ``cells`` array - cdef int _resize(self, SIZE_t capacity) except -1 nogil - cdef int _resize_c(self, SIZE_t capacity=*) except -1 nogil - cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=*) except -1 nogil + cdef int _resize(self, SIZE_t capacity) nogil except -1 + cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1 + cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=*) nogil except -1 cdef Cell[:] _get_cell_ndarray(self) diff --git a/sklearn/neighbors/_quad_tree.pyx b/sklearn/neighbors/_quad_tree.pyx index 353693c9f001f..6c142e4ce1031 100644 --- a/sklearn/neighbors/_quad_tree.pyx +++ b/sklearn/neighbors/_quad_tree.pyx @@ -114,7 +114,7 @@ cdef class _QuadTree: self._resize(capacity=self.cell_count) cdef int insert_point(self, DTYPE_t[3] point, SIZE_t point_index, - SIZE_t cell_id=0) except -1 nogil: + SIZE_t cell_id=0) nogil except -1: """Insert a point in the QuadTree.""" cdef int ax cdef SIZE_t selected_child @@ -302,7 +302,7 @@ cdef class _QuadTree: self.cell_count += 1 cdef int _check_point_in_cell(self, DTYPE_t[3] point, Cell* cell - ) except -1 nogil: + ) nogil except -1: """Check that the given point is in the cell boundaries.""" if self.verbose >= 50: @@ -461,7 +461,7 @@ cdef class _QuadTree: return self._get_cell(query_pt, 0) cdef int _get_cell(self, DTYPE_t[3] point, SIZE_t cell_id=0 - ) except -1 nogil: + ) nogil except -1: """guts of get_cell. Return the id of the cell containing the query point or raise ValueError @@ -566,7 +566,7 @@ cdef class _QuadTree: raise ValueError("Can't initialize array!") return arr - cdef int _resize(self, SIZE_t capacity) except -1 nogil: + cdef int _resize(self, SIZE_t capacity) nogil except -1: """Resize all inner arrays to `capacity`, if `capacity` == -1, then double the size of the inner arrays. @@ -578,7 +578,7 @@ cdef class _QuadTree: with gil: raise MemoryError() - cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) except -1 nogil: + cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1: """Guts of _resize Returns -1 in case of failure to allocate memory (and raise MemoryError) diff --git a/sklearn/tree/_criterion.pxd b/sklearn/tree/_criterion.pxd index 0fc178d1c5d99..3c6217a8c272c 100644 --- a/sklearn/tree/_criterion.pxd +++ b/sklearn/tree/_criterion.pxd @@ -49,10 +49,10 @@ cdef class Criterion: const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end - ) except -1 nogil - cdef int reset(self) except -1 nogil - cdef int reverse_reset(self) except -1 nogil - cdef int update(self, SIZE_t new_pos) except -1 nogil + ) nogil except -1 + cdef int reset(self) nogil except -1 + cdef int reverse_reset(self) nogil except -1 + cdef int update(self, SIZE_t new_pos) nogil except -1 cdef double node_impurity(self) nogil cdef void children_impurity( self, diff --git a/sklearn/tree/_criterion.pyx b/sklearn/tree/_criterion.pyx index 83df937a126ba..e9f32b6e06ef9 100644 --- a/sklearn/tree/_criterion.pyx +++ b/sklearn/tree/_criterion.pyx @@ -49,7 +49,7 @@ cdef class Criterion: const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end, - ) except -1 nogil: + ) nogil except -1: """Placeholder for a method which will initialize the criterion. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -75,21 +75,21 @@ cdef class Criterion: """ pass - cdef int reset(self) except -1 nogil: + cdef int reset(self) nogil except -1: """Reset the criterion at pos=start. This method must be implemented by the subclass. """ pass - cdef int reverse_reset(self) except -1 nogil: + cdef int reverse_reset(self) nogil except -1: """Reset the criterion at pos=end. This method must be implemented by the subclass. """ pass - cdef int update(self, SIZE_t new_pos) except -1 nogil: + cdef int update(self, SIZE_t new_pos) nogil except -1: """Updated statistics by moving sample_indices[pos:new_pos] to the left child. This updates the collected statistics by moving sample_indices[pos:new_pos] @@ -257,7 +257,7 @@ cdef class ClassificationCriterion(Criterion): const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end - ) except -1 nogil: + ) nogil except -1: """Initialize the criterion. This initializes the criterion at node sample_indices[start:end] and children @@ -319,7 +319,7 @@ cdef class ClassificationCriterion(Criterion): self.reset() return 0 - cdef int reset(self) except -1 nogil: + cdef int reset(self) nogil except -1: """Reset the criterion at pos=start. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -336,7 +336,7 @@ cdef class ClassificationCriterion(Criterion): memcpy(&self.sum_right[k, 0], &self.sum_total[k, 0], self.n_classes[k] * sizeof(double)) return 0 - cdef int reverse_reset(self) except -1 nogil: + cdef int reverse_reset(self) nogil except -1: """Reset the criterion at pos=end. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -353,7 +353,7 @@ cdef class ClassificationCriterion(Criterion): memcpy(&self.sum_left[k, 0], &self.sum_total[k, 0], self.n_classes[k] * sizeof(double)) return 0 - cdef int update(self, SIZE_t new_pos) except -1 nogil: + cdef int update(self, SIZE_t new_pos) nogil except -1: """Updated statistics by moving sample_indices[pos:new_pos] to the left child. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -651,7 +651,7 @@ cdef class RegressionCriterion(Criterion): const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end, - ) except -1 nogil: + ) nogil except -1: """Initialize the criterion. This initializes the criterion at node sample_indices[start:end] and children @@ -694,7 +694,7 @@ cdef class RegressionCriterion(Criterion): self.reset() return 0 - cdef int reset(self) except -1 nogil: + cdef int reset(self) nogil except -1: """Reset the criterion at pos=start.""" cdef SIZE_t n_bytes = self.n_outputs * sizeof(double) memset(&self.sum_left[0], 0, n_bytes) @@ -705,7 +705,7 @@ cdef class RegressionCriterion(Criterion): self.pos = self.start return 0 - cdef int reverse_reset(self) except -1 nogil: + cdef int reverse_reset(self) nogil except -1: """Reset the criterion at pos=end.""" cdef SIZE_t n_bytes = self.n_outputs * sizeof(double) memset(&self.sum_right[0], 0, n_bytes) @@ -716,7 +716,7 @@ cdef class RegressionCriterion(Criterion): self.pos = self.end return 0 - cdef int update(self, SIZE_t new_pos) except -1 nogil: + cdef int update(self, SIZE_t new_pos) nogil except -1: """Updated statistics by moving sample_indices[pos:new_pos] to the left.""" cdef const DOUBLE_t[:] sample_weight = self.sample_weight cdef const SIZE_t[:] sample_indices = self.sample_indices @@ -931,7 +931,7 @@ cdef class MAE(RegressionCriterion): const SIZE_t[:] sample_indices, SIZE_t start, SIZE_t end, - ) except -1 nogil: + ) nogil except -1: """Initialize the criterion. This initializes the criterion at node sample_indices[start:end] and children @@ -981,7 +981,7 @@ cdef class MAE(RegressionCriterion): self.reset() return 0 - cdef int reset(self) except -1 nogil: + cdef int reset(self) nogil except -1: """Reset the criterion at pos=start. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1012,7 +1012,7 @@ cdef class MAE(RegressionCriterion): weight) return 0 - cdef int reverse_reset(self) except -1 nogil: + cdef int reverse_reset(self) nogil except -1: """Reset the criterion at pos=end. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1040,7 +1040,7 @@ cdef class MAE(RegressionCriterion): weight) return 0 - cdef int update(self, SIZE_t new_pos) except -1 nogil: + cdef int update(self, SIZE_t new_pos) nogil except -1: """Updated statistics by moving sample_indices[pos:new_pos] to the left. Returns -1 in case of failure to allocate memory (and raise MemoryError) diff --git a/sklearn/tree/_splitter.pxd b/sklearn/tree/_splitter.pxd index fa1327512e9e9..f97761879922a 100644 --- a/sklearn/tree/_splitter.pxd +++ b/sklearn/tree/_splitter.pxd @@ -86,14 +86,14 @@ cdef class Splitter: SIZE_t start, SIZE_t end, double* weighted_n_node_samples - ) except -1 nogil + ) nogil except -1 cdef int node_split( self, double impurity, # Impurity of the node SplitRecord* split, SIZE_t* n_constant_features - ) except -1 nogil + ) nogil except -1 cdef void node_value(self, double* dest) nogil diff --git a/sklearn/tree/_splitter.pyx b/sklearn/tree/_splitter.pyx index 0990963ea61d3..181fc682e3ef0 100644 --- a/sklearn/tree/_splitter.pyx +++ b/sklearn/tree/_splitter.pyx @@ -167,7 +167,7 @@ cdef class Splitter: return 0 cdef int node_reset(self, SIZE_t start, SIZE_t end, - double* weighted_n_node_samples) except -1 nogil: + double* weighted_n_node_samples) nogil except -1: """Reset splitter on node samples[start:end]. Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -199,7 +199,7 @@ cdef class Splitter: return 0 cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) except -1 nogil: + SIZE_t* n_constant_features) nogil except -1: """Find the best split on node samples[start:end]. This is a placeholder method. The majority of computation will be done @@ -248,7 +248,7 @@ cdef class BaseDenseSplitter(Splitter): cdef class BestSplitter(BaseDenseSplitter): """Splitter for finding the best split.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) except -1 nogil: + SIZE_t* n_constant_features) nogil except -1: """Find the best split on node samples[start:end] Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -554,7 +554,7 @@ cdef void heapsort(DTYPE_t* Xf, SIZE_t* samples, SIZE_t n) nogil: cdef class RandomSplitter(BaseDenseSplitter): """Splitter for finding the best random split.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) except -1 nogil: + SIZE_t* n_constant_features) nogil except -1: """Find the best random split on node samples[start:end] Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1051,7 +1051,7 @@ cdef inline void sparse_swap(SIZE_t[::1] index_to_samples, SIZE_t[::1] samples, cdef class BestSparseSplitter(BaseSparseSplitter): """Splitter for finding the best split, using the sparse data.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) except -1 nogil: + SIZE_t* n_constant_features) nogil except -1: """Find the best split on node samples[start:end], using sparse features Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -1269,7 +1269,7 @@ cdef class BestSparseSplitter(BaseSparseSplitter): cdef class RandomSparseSplitter(BaseSparseSplitter): """Splitter for finding a random split, using the sparse data.""" cdef int node_split(self, double impurity, SplitRecord* split, - SIZE_t* n_constant_features) except -1 nogil: + SIZE_t* n_constant_features) nogil except -1: """Find a random split on node samples[start:end], using sparse features Returns -1 in case of failure to allocate memory (and raise MemoryError) diff --git a/sklearn/tree/_tree.pxd b/sklearn/tree/_tree.pxd index f2c5169f00534..55895a8279828 100644 --- a/sklearn/tree/_tree.pxd +++ b/sklearn/tree/_tree.pxd @@ -58,9 +58,9 @@ cdef class Tree: cdef SIZE_t _add_node(self, SIZE_t parent, bint is_left, bint is_leaf, SIZE_t feature, double threshold, double impurity, SIZE_t n_node_samples, - double weighted_n_node_samples) except -1 nogil - cdef int _resize(self, SIZE_t capacity) except -1 nogil - cdef int _resize_c(self, SIZE_t capacity=*) except -1 nogil + double weighted_n_node_samples) nogil except -1 + cdef int _resize(self, SIZE_t capacity) nogil except -1 + cdef int _resize_c(self, SIZE_t capacity=*) nogil except -1 cdef cnp.ndarray _get_value_ndarray(self) cdef cnp.ndarray _get_node_ndarray(self) diff --git a/sklearn/tree/_tree.pyx b/sklearn/tree/_tree.pyx index eda53d5661e45..9738968f8e687 100644 --- a/sklearn/tree/_tree.pyx +++ b/sklearn/tree/_tree.pyx @@ -437,7 +437,7 @@ cdef class BestFirstTreeBuilder(TreeBuilder): SIZE_t start, SIZE_t end, double impurity, bint is_first, bint is_left, Node* parent, SIZE_t depth, - FrontierRecord* res) except -1 nogil: + FrontierRecord* res) nogil except -1: """Adds node w/ partition ``[start, end)`` to the frontier. """ cdef SplitRecord split cdef SIZE_t node_id @@ -688,7 +688,7 @@ cdef class Tree: value = memcpy(self.value, ( value_ndarray).data, self.capacity * self.value_stride * sizeof(double)) - cdef int _resize(self, SIZE_t capacity) except -1 nogil: + cdef int _resize(self, SIZE_t capacity) nogil except -1: """Resize all inner arrays to `capacity`, if `capacity` == -1, then double the size of the inner arrays. @@ -700,7 +700,7 @@ cdef class Tree: with gil: raise MemoryError() - cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) except -1 nogil: + cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1: """Guts of _resize Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -734,7 +734,7 @@ cdef class Tree: cdef SIZE_t _add_node(self, SIZE_t parent, bint is_left, bint is_leaf, SIZE_t feature, double threshold, double impurity, SIZE_t n_node_samples, - double weighted_n_node_samples) except -1 nogil: + double weighted_n_node_samples) nogil except -1: """Add a node to the tree. The new node registers itself as the child of its parent. diff --git a/sklearn/tree/_utils.pxd b/sklearn/tree/_utils.pxd index 5e9768b3b9afd..9d41b757d85dc 100644 --- a/sklearn/tree/_utils.pxd +++ b/sklearn/tree/_utils.pxd @@ -43,7 +43,7 @@ ctypedef fused realloc_ptr: (Cell*) (Node**) -cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil +cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) nogil except * cdef cnp.ndarray sizet_ptr_to_ndarray(SIZE_t* data, SIZE_t size) @@ -74,9 +74,9 @@ cdef class WeightedPQueue: cdef WeightedPQueueRecord* array_ cdef bint is_empty(self) nogil - cdef int reset(self) except -1 nogil + cdef int reset(self) nogil except -1 cdef SIZE_t size(self) nogil - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1 cdef int remove(self, DOUBLE_t data, DOUBLE_t weight) nogil cdef int pop(self, DOUBLE_t* data, DOUBLE_t* weight) nogil cdef int peek(self, DOUBLE_t* data, DOUBLE_t* weight) nogil @@ -97,8 +97,8 @@ cdef class WeightedMedianCalculator: # = w[0] + w[1] + ... + w[k-1] cdef SIZE_t size(self) nogil - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil - cdef int reset(self) except -1 nogil + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1 + cdef int reset(self) nogil except -1 cdef int update_median_parameters_post_push( self, DOUBLE_t data, DOUBLE_t weight, DOUBLE_t original_median) nogil diff --git a/sklearn/tree/_utils.pyx b/sklearn/tree/_utils.pyx index a1471d550b48f..cf4d6aebf78c3 100644 --- a/sklearn/tree/_utils.pyx +++ b/sklearn/tree/_utils.pyx @@ -20,7 +20,7 @@ from ..utils._random cimport our_rand_r # Helper functions # ============================================================================= -cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil: +cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) nogil except *: # sizeof(realloc_ptr[0]) would be more like idiomatic C, but causes Cython # 0.20.1 to crash. cdef size_t nbytes = nelems * sizeof(p[0][0]) @@ -102,7 +102,7 @@ cdef class WeightedPQueue: def __dealloc__(self): free(self.array_) - cdef int reset(self) except -1 nogil: + cdef int reset(self) nogil except -1: """Reset the WeightedPQueue to its state at construction Return -1 in case of failure to allocate memory (and raise MemoryError) @@ -119,7 +119,7 @@ cdef class WeightedPQueue: cdef SIZE_t size(self) nogil: return self.array_ptr - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil: + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1: """Push record on the array. Return -1 in case of failure to allocate memory (and raise MemoryError) @@ -277,7 +277,7 @@ cdef class WeightedMedianCalculator: WeightedMedianCalculator""" return self.samples.size() - cdef int reset(self) except -1 nogil: + cdef int reset(self) nogil except -1: """Reset the WeightedMedianCalculator to its state at construction Return -1 in case of failure to allocate memory (and raise MemoryError) @@ -291,7 +291,7 @@ cdef class WeightedMedianCalculator: self.sum_w_0_k = 0 return 0 - cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil: + cdef int push(self, DOUBLE_t data, DOUBLE_t weight) nogil except -1: """Push a value and its associated weight to the WeightedMedianCalculator Return -1 in case of failure to allocate memory (and raise MemoryError) From f95dd9bd58d24b2a55f0c79a77a62a66b9c3f66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Thu, 5 Jan 2023 17:50:11 +0100 Subject: [PATCH 07/14] [scipy-dev] trigger CI From 9e660cdf3b197a12c111836420a959507c6e48d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Fri, 6 Jan 2023 13:55:30 +0100 Subject: [PATCH 08/14] [scipy-dev] Use C-like cast --- sklearn/neighbors/_binary_tree.pxi | 2 +- sklearn/neighbors/_quad_tree.pyx | 2 +- sklearn/tree/_tree.pyx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index cca4d8311de1c..00b5b3c2758d3 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -846,7 +846,7 @@ cdef class BinaryTree: # with numbers of points between leaf_size and 2 * leaf_size self.n_levels = int( np.log2(fmax(1, (n_samples - 1) / self.leaf_size)) + 1) - self.n_nodes = (2 ** int(self.n_levels)) - 1 + self.n_nodes = (2 ** self.n_levels) - 1 # allocate arrays for storage self.idx_array = np.arange(n_samples, dtype=ITYPE) diff --git a/sklearn/neighbors/_quad_tree.pyx b/sklearn/neighbors/_quad_tree.pyx index 6c142e4ce1031..a7607cb56e0eb 100644 --- a/sklearn/neighbors/_quad_tree.pyx +++ b/sklearn/neighbors/_quad_tree.pyx @@ -52,7 +52,7 @@ cdef class _QuadTree: # Parameters of the tree self.n_dimensions = n_dimensions self.verbose = verbose - self.n_cells_per_cell = 2 ** int(self.n_dimensions) + self.n_cells_per_cell = (2 ** self.n_dimensions) # Inner structures self.max_depth = 0 diff --git a/sklearn/tree/_tree.pyx b/sklearn/tree/_tree.pyx index 9738968f8e687..e5d983b1344bf 100644 --- a/sklearn/tree/_tree.pyx +++ b/sklearn/tree/_tree.pyx @@ -155,7 +155,7 @@ cdef class DepthFirstTreeBuilder(TreeBuilder): cdef int init_capacity if tree.max_depth <= 10: - init_capacity = (2 ** int(tree.max_depth + 1)) - 1 + init_capacity = (2 ** (tree.max_depth + 1)) - 1 else: init_capacity = 2047 From 0555d277377c685513e3940205c97a15b033a2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Mon, 9 Jan 2023 16:52:48 +0100 Subject: [PATCH 09/14] [scipy-dev] trigger CI From ed3dc8c1de2a1f9c7fce6632cc31e24f373544c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 10 Jan 2023 11:03:40 +0100 Subject: [PATCH 10/14] Add TODO note + change version detection --- sklearn/_build_utils/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sklearn/_build_utils/__init__.py b/sklearn/_build_utils/__init__.py index d7f3959ca4ac6..f81f116e733fd 100644 --- a/sklearn/_build_utils/__init__.py +++ b/sklearn/_build_utils/__init__.py @@ -80,7 +80,12 @@ def cythonize_extensions(extension): "cdivision": True, } - if not Cython.__version__.startswith("0."): + # TODO: once Cython 3 is released and we require Cython>=3 we should get + # rid of the legacy_implicit_noexcept directive. This should mostly consist + # in ensuring nogil is at the end of function signature, e.g. replace + # "nogil except -1" by "except -1 nogil". See + # https://github.com/cython/cython/issues/5088 for more details + if parse(Cython.__version__) > parse("3.0.0a11"): compiler_directives["legacy_implicit_noexcept"] = True return cythonize( From f7efc4f4d89255201f17c504887c2d74694abc48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 10 Jan 2023 11:11:49 +0100 Subject: [PATCH 11/14] [scipy-dev] trigger CI From 8b27be07a49f2653c529d1b805b54f02dc850790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 10 Jan 2023 14:52:08 +0100 Subject: [PATCH 12/14] [scipy-dev] trigger CI From 6ee39534c4259fb0444d81025eb47a4ce39030c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Wed, 11 Jan 2023 16:45:21 +0100 Subject: [PATCH 13/14] Update sklearn/_build_utils/__init__.py Co-authored-by: Julien Jerphanion --- sklearn/_build_utils/__init__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sklearn/_build_utils/__init__.py b/sklearn/_build_utils/__init__.py index f81f116e733fd..9da232217befb 100644 --- a/sklearn/_build_utils/__init__.py +++ b/sklearn/_build_utils/__init__.py @@ -81,10 +81,17 @@ def cythonize_extensions(extension): } # TODO: once Cython 3 is released and we require Cython>=3 we should get - # rid of the legacy_implicit_noexcept directive. This should mostly consist - # in ensuring nogil is at the end of function signature, e.g. replace - # "nogil except -1" by "except -1 nogil". See - # https://github.com/cython/cython/issues/5088 for more details + # rid of the `legacy_implicit_noexcept` directive. + # This should mostly consist in: + # + # - ensuring nogil is at the end of function signature, + # e.g. replace "nogil except -1" by "except -1 nogil". + # + # - "noexcept"-qualifying Cython and externalized C interfaces + # which aren't raising nor propagating exceptions. + # See: https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#error-return-values # noqa + # + # See: https://github.com/cython/cython/issues/5088 for more details if parse(Cython.__version__) > parse("3.0.0a11"): compiler_directives["legacy_implicit_noexcept"] = True From 5d3cb84fa8059132245714fa015d4f4f6638b3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Wed, 11 Jan 2023 17:19:29 +0100 Subject: [PATCH 14/14] [scipy-dev] Black --- sklearn/_build_utils/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sklearn/_build_utils/__init__.py b/sklearn/_build_utils/__init__.py index 9da232217befb..755171ee770c4 100644 --- a/sklearn/_build_utils/__init__.py +++ b/sklearn/_build_utils/__init__.py @@ -83,14 +83,14 @@ def cythonize_extensions(extension): # TODO: once Cython 3 is released and we require Cython>=3 we should get # rid of the `legacy_implicit_noexcept` directive. # This should mostly consist in: - # + # # - ensuring nogil is at the end of function signature, # e.g. replace "nogil except -1" by "except -1 nogil". - # + # # - "noexcept"-qualifying Cython and externalized C interfaces - # which aren't raising nor propagating exceptions. + # which aren't raising nor propagating exceptions. # See: https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#error-return-values # noqa - # + # # See: https://github.com/cython/cython/issues/5088 for more details if parse(Cython.__version__) > parse("3.0.0a11"): compiler_directives["legacy_implicit_noexcept"] = True