|
5 | 5 | # This source code is licensed under a BSD-style license (found in the |
6 | 6 | # LICENSE file in the root directory of this source tree) |
7 | 7 | ####################################################################### |
| 8 | +import math |
8 | 9 |
|
9 | 10 | import numexpr as ne |
10 | 11 | import numpy as np |
@@ -50,12 +51,15 @@ def array_fixture(dtype_fixture, shape_fixture): |
50 | 51 | return a1, a2, a3, a4, na1, na2, na3, na4 |
51 | 52 |
|
52 | 53 |
|
53 | | -@pytest.mark.parametrize("reduce_op", ["sum", "prod", "min", "max", "any", "all"]) |
| 54 | +# @pytest.mark.parametrize("reduce_op", ["sum", "prod", "min", "max", "any", "all"]) |
| 55 | +@pytest.mark.parametrize("reduce_op", ["sum"]) |
54 | 56 | def test_reduce_bool(array_fixture, reduce_op): |
55 | 57 | a1, a2, a3, a4, na1, na2, na3, na4 = array_fixture |
56 | 58 | expr = a1 + a2 > a3 * a4 |
57 | 59 | nres = ne.evaluate("na1 + na2 > na3 * na4") |
58 | | - res = getattr(expr, reduce_op)() |
| 60 | + # res = getattr(expr, reduce_op)() |
| 61 | + res = expr.sum() |
| 62 | + print("res:", res) |
59 | 63 | nres = getattr(nres, reduce_op)() |
60 | 64 | tol = 1e-15 if a1.dtype == "float64" else 1e-6 |
61 | 65 | np.testing.assert_allclose(res, nres, atol=tol, rtol=tol) |
@@ -353,3 +357,62 @@ def test_save_version4(disk, fill_value, reduce_op, axis): |
353 | 357 | blosc2.remove_urlpath("a1.b2nd") |
354 | 358 | blosc2.remove_urlpath("b.b2nd") |
355 | 359 | blosc2.remove_urlpath("out.b2nd") |
| 360 | + |
| 361 | + |
| 362 | +@pytest.mark.parametrize("shape", [(10,), (10, 10), (10, 10, 10)]) |
| 363 | +@pytest.mark.parametrize("disk", [True, False]) |
| 364 | +@pytest.mark.parametrize("compute", [True, False]) |
| 365 | +def test_save_constructor_reduce(shape, disk, compute): |
| 366 | + lshape = math.prod(shape) |
| 367 | + urlpath_a = "a.b2nd" if disk else None |
| 368 | + urlpath_b = "b.b2nd" if disk else None |
| 369 | + a = blosc2.arange(lshape, shape=shape, urlpath=urlpath_a, mode="w") |
| 370 | + b = blosc2.ones(shape, urlpath=urlpath_b, mode="w") |
| 371 | + expr = f"arange({lshape}).sum() + a + ones({shape}).sum() + b + 1" |
| 372 | + lexpr = blosc2.lazyexpr(expr) |
| 373 | + if disk: |
| 374 | + lexpr.save("out.b2nd") |
| 375 | + lexpr = blosc2.open("out.b2nd") |
| 376 | + if compute: |
| 377 | + res = lexpr.compute() |
| 378 | + res = res[()] # for later comparison with nres |
| 379 | + else: |
| 380 | + res = lexpr[()] |
| 381 | + na = np.arange(lshape).reshape(shape).sum() |
| 382 | + nb = np.ones(shape).sum() |
| 383 | + nres = na + a[:] + nb + b[:] + 1 |
| 384 | + assert np.allclose(res[()], nres) |
| 385 | + if disk: |
| 386 | + blosc2.remove_urlpath(urlpath_a) |
| 387 | + blosc2.remove_urlpath(urlpath_b) |
| 388 | + blosc2.remove_urlpath("out.b2nd") |
| 389 | + |
| 390 | + |
| 391 | +@pytest.mark.parametrize("shape", [(10,), (10, 10), (10, 10, 10)]) |
| 392 | +@pytest.mark.parametrize("disk", [True, False]) |
| 393 | +@pytest.mark.parametrize("compute", [True, False]) |
| 394 | +def test_save_constructor_reduce2(shape, disk, compute): |
| 395 | + lshape = math.prod(shape) |
| 396 | + urlpath_a = "a.b2nd" if disk else None |
| 397 | + urlpath_b = "b.b2nd" if disk else None |
| 398 | + a = blosc2.arange(lshape, shape=shape, urlpath=urlpath_a, mode="w") |
| 399 | + b = blosc2.ones(shape, urlpath=urlpath_b, mode="w") |
| 400 | + expr = "sum(a + 1) + (b + 2).sum() + 3" |
| 401 | + lexpr = blosc2.lazyexpr(expr) |
| 402 | + if disk: |
| 403 | + lexpr.save("out.b2nd") |
| 404 | + lexpr = blosc2.open("out.b2nd") |
| 405 | + if compute: |
| 406 | + res = lexpr.compute() |
| 407 | + res = res[()] # for later comparison with nres |
| 408 | + else: |
| 409 | + res = lexpr[()] |
| 410 | + na = np.arange(lshape).reshape(shape) |
| 411 | + nb = np.ones(shape) |
| 412 | + nres = np.sum(na + 1) + (nb + 2).sum() + 3 |
| 413 | + assert np.allclose(res, nres) |
| 414 | + assert res.dtype == nres.dtype |
| 415 | + if disk: |
| 416 | + blosc2.remove_urlpath(urlpath_a) |
| 417 | + blosc2.remove_urlpath(urlpath_b) |
| 418 | + blosc2.remove_urlpath("out.b2nd") |
0 commit comments