Skip to content

Commit c36c53f

Browse files
authored
Fix test fail with numpy 1.25 (#3354)
1 parent 3418861 commit c36c53f

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

mars/learn/cluster/_k_means_init.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import inspect
16+
1517
import numpy as np
1618

1719
from ... import opcodes
@@ -212,12 +214,18 @@ def _kmeans_plusplus(*args, **kwargs):
212214
)
213215

214216
with device(device_id):
217+
arg_spec = inspect.getfullargspec(_kmeans_plusplus)
218+
kw = {}
219+
if "sample_weight" in arg_spec.args:
220+
kw["sample_weight"] = np.ones(x.shape[0])
221+
215222
ctx[op.outputs[0].key] = _kmeans_plusplus(
216223
x,
217224
op.n_clusters,
218225
x_squared_norms=x_squared_norms,
219226
random_state=op.state,
220227
n_local_trials=op.n_local_trials,
228+
**kw
221229
)[0]
222230

223231

mars/learn/neighbors/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@
3131
from ._kneighbors_graph import KNeighborsGraph
3232

3333

34+
def _call_if_callable(metrics):
35+
if callable(metrics):
36+
return metrics()
37+
return metrics
38+
39+
3440
VALID_METRICS = dict(
35-
ball_tree=SklearnBallTree.valid_metrics,
36-
kd_tree=SklearnKDTree.valid_metrics,
41+
ball_tree=_call_if_callable(SklearnBallTree.valid_metrics),
42+
kd_tree=_call_if_callable(SklearnKDTree.valid_metrics),
3743
# The following list comes from the
3844
# sklearn.metrics.pairwise doc string
3945
brute=(

mars/lib/sparse/tests/test_sparse.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,21 @@ def test_sparse_divide():
150150
s1 = SparseNDArray(s1_data)
151151
s2 = SparseNDArray(s2_data)
152152

153-
assert_array_equal(s1 / s2, s1 / s2)
154-
assert_array_equal(s1 / d1, s1 / d1)
155-
assert_array_equal(d1 / s1, d1 / s1.toarray())
156-
assert_array_equal(s1 / 2, s1 / 2)
157-
assert_array_equal(2 / s1, 2 / s1.toarray())
153+
assert_array_equal(s1 / s2, s1 / s2, almost=True)
154+
assert_array_equal(s1 / d1, s1 / d1, almost=True)
155+
assert_array_equal(d1 / s1, d1 / s1.toarray(), almost=True)
156+
assert_array_equal(s1 / 2, s1 / 2, almost=True)
157+
assert_array_equal(2 / s1, 2 / s1.toarray(), almost=True)
158158

159159
# test sparse vector
160160
v = SparseNDArray(v1, shape=(3,))
161-
assert_array_equal(v / v, v1_data / v1_data)
162-
assert_array_equal(v / d1, v1_data / d1)
163-
assert_array_equal(d1 / v, d1 / v1_data)
161+
assert_array_equal(v / v, v1_data / v1_data, almost=True)
162+
assert_array_equal(v / d1, v1_data / d1, almost=True)
163+
assert_array_equal(d1 / v, d1 / v1_data, almost=True)
164164
r = sps.csr_matrix(((v1.data / 1), v1.indices, v1.indptr), v1.shape)
165-
assert_array_equal(v / 1, r.toarray().reshape(3))
165+
assert_array_equal(v / 1, r.toarray().reshape(3), almost=True)
166166
r = sps.csr_matrix(((1 / v1.data), v1.indices, v1.indptr), v1.shape)
167-
assert_array_equal(1 / v, r.toarray().reshape(3))
167+
assert_array_equal(1 / v, r.toarray().reshape(3), almost=True)
168168

169169

170170
def test_sparse_floor_divide():

mars/tensor/reduction/core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,17 @@ def execute_agg(cls, ctx, op):
328328
func_name = getattr(cls, "_func_name", None)
329329
reduce_func = getattr(xp, func_name)
330330
out = op.outputs[0]
331+
332+
def get_reduce_args(func):
333+
if hasattr(func, "_implementation"):
334+
func = func._implementation
335+
return inspect.getfullargspec(func).args
336+
331337
with device(device_id):
332338
if input_chunk.size == 0 and op.keepdims:
333339
# input chunk is empty, when keepdims is True, return itself
334340
ret = input_chunk
335-
elif "dtype" in inspect.getfullargspec(reduce_func).args:
341+
elif "dtype" in get_reduce_args(reduce_func):
336342
ret = reduce_func(
337343
input_chunk, axis=axis, dtype=op.dtype, keepdims=bool(op.keepdims)
338344
)

mars/tensor/statistics/tests/test_statistics.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
import pytest
1717

1818
from ....core import tile
19+
from ....lib.version import Version
1920
from ...datasource import tensor, array
2021
from .. import digitize, histogram_bin_edges, quantile, percentile
2122
from ..quantile import INTERPOLATION_TYPES
2223

24+
np_version = Version(np.__version__)
25+
2326

2427
def test_digitize():
2528
x = tensor(np.array([0.2, 6.4, 3.0, 1.6]), chunk_size=2)
@@ -70,7 +73,11 @@ def test_quantile():
7073
raw = np.random.rand(100)
7174
q = np.random.rand(10)
7275

73-
for dtype in [np.float32, np.int64, np.complex128]:
76+
quantile_dtypes = [np.float32, np.int64]
77+
if np_version < Version("1.25"):
78+
quantile_dtypes.extend([np.complex128])
79+
80+
for dtype in quantile_dtypes:
7481
raw2 = raw.astype(dtype)
7582
a = tensor(raw2, chunk_size=100)
7683

@@ -84,7 +91,7 @@ def test_quantile():
8491
raw = np.random.rand(20, 10)
8592
q = np.random.rand(10)
8693

87-
for dtype in [np.float32, np.int64, np.complex128]:
94+
for dtype in quantile_dtypes:
8895
for axis in (None, 0, 1, [0, 1]):
8996
for interpolation in INTERPOLATION_TYPES:
9097
for keepdims in [True, False]:

0 commit comments

Comments
 (0)