Skip to content

Commit

Permalink
Bumpy Cython version. Fix Numpy 2.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlk committed Oct 3, 2024
1 parent ad1166b commit 684c9b9
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion k_means_constrained/k_means_constrained_.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def k_means_constrained(X, n_clusters, size_min=None, size_max=None, init='k-mea

# Validate init array
if hasattr(init, '__array__'):
init = check_array(init, dtype=X.dtype.type, copy=True)
init = check_array(init, dtype=X.dtype.type)
_validate_center_shape(X, n_clusters, init)

if n_init != 1:
Expand Down
1 change: 1 addition & 0 deletions k_means_constrained/sklearn_import/cluster/_k_means.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# cython: profile=True
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
# Profiling is enabled by default as the overhead does not seem to be measurable
# on this specific use case.

Expand Down
2 changes: 1 addition & 1 deletion k_means_constrained/sklearn_import/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def pairwise_distances_argmin_min(X, Y, axis=1, metric="euclidean",
# Allocate output arrays
indices = np.empty(X.shape[0], dtype=np.intp)
values = np.empty(X.shape[0])
values.fill(np.infty)
values.fill(np.inf)

for chunk_x in gen_batches(X.shape[0], batch_size):
X_chunk = X[chunk_x, :]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#cython: boundscheck=False
#cython: cdivision=True
#cython: wraparound=False
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION

# Author: Andreas Mueller <[email protected]>
# Lars Buitinck
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#!python
#cython: boundscheck=False, wraparound=False, cdivision=True
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION

from libc.math cimport fabs, sqrt, pow
cimport numpy as np
Expand Down
19 changes: 7 additions & 12 deletions k_means_constrained/sklearn_import/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def check_array(array, accept_sparse=False, dtype="numeric", order=None,
copy=False, force_all_finite=True, ensure_2d=True,
force_all_finite=True, ensure_2d=True,
allow_nd=False, ensure_min_samples=1, ensure_min_features=1,
warn_on_dtype=False, estimator=None):
"""Input validation on an array, list, sparse matrix or similar.
Expand Down Expand Up @@ -46,15 +46,10 @@ def check_array(array, accept_sparse=False, dtype="numeric", order=None,
order : 'F', 'C' or None (default=None)
Whether an array will be forced to be fortran or c-style.
When order is None (default), then if copy=False, nothing is ensured
about the memory layout of the output array; otherwise (copy=True)
the memory layout of the returned array is kept as close as possible
When order is None (default), the memory layout of the
returned array is kept as close as possible
to the original array.
copy : boolean (default=False)
Whether a forced copy will be triggered. If copy=False, a copy might
be triggered by a conversion.
force_all_finite : boolean (default=True)
Whether to raise an error on np.inf and np.nan in X.
Expand Down Expand Up @@ -131,10 +126,10 @@ def check_array(array, accept_sparse=False, dtype="numeric", order=None,
context = " by %s" % estimator_name if estimator is not None else ""

if sp.issparse(array):
array = _ensure_sparse_format(array, accept_sparse, dtype, copy,
force_all_finite)
array = _ensure_sparse_format(array, accept_sparse, dtype,
force_all_finite, copy=True)
else:
array = np.array(array, dtype=dtype, order=order, copy=copy)
array = np.array(array, dtype=dtype, order=order, copy=True)

if ensure_2d:
if array.ndim == 1:
Expand All @@ -145,7 +140,7 @@ def check_array(array, accept_sparse=False, dtype="numeric", order=None,
"if it contains a single sample.".format(array))
array = np.atleast_2d(array)
# To ensure that array flags are maintained
array = np.array(array, dtype=dtype, order=order, copy=copy)
array = np.array(array, dtype=dtype, order=order, copy=True)

# make sure we actually converted to numeric:
if dtype_numeric and array.dtype.kind == "O":
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r requirements.txt
pytest>=5.1
pandas>=1.0.4
twine
Expand All @@ -7,4 +8,4 @@ numpydoc
bump2version
nose
scikit-learn>=0.24.2
cython
cython>=3.0.11
4 changes: 2 additions & 2 deletions tests/test_kmeans_constrained_from_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_labels_assignment_and_inertia():
noisy_centers = centers + rng.normal(size=centers.shape)
labels_gold = - np.ones(n_samples, dtype=int)
mindist = np.empty(n_samples)
mindist.fill(np.infty)
mindist.fill(np.inf)
for center_id in range(n_clusters):
dist = np.sum((X - noisy_centers[center_id]) ** 2, axis=1)
labels_gold[dist < mindist] = center_id
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_transform():
X_new = km.transform(km.cluster_centers_)

for c in range(n_clusters):
assert_equal(X_new[c, c], 0)
assert_array_almost_equal(X_new[c, c], 0)
for c2 in range(n_clusters):
if c != c2:
assert X_new[c, c2] > 0
Expand Down

0 comments on commit 684c9b9

Please sign in to comment.