Skip to content

Commit c208966

Browse files
committed
More tests of lazy expressions with Blosc2 NDArrays
1 parent ad659b9 commit c208966

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

caterva2/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ def __init__(self, root, path):
556556
"""
557557
super().__init__(root, path)
558558

559+
def __str__(self):
560+
return self.path.as_posix()
561+
559562
def __repr__(self):
560563
# TODO: add more info about dims, types, etc.
561564
return f"<Dataset: {self.path}>"

caterva2/services/sub.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,9 @@ def open_b2(abspath, path):
293293
for key, value in operands.items():
294294
if value is None:
295295
raise ValueError(f'Missing operand "{key}"')
296+
metaval = value.schunk.meta if hasattr(value, "schunk") else {}
296297
vlmetaval = value.schunk.vlmeta
297-
if "proxy-source" in value.schunk.meta or (
298-
"_ftype" in vlmetaval and vlmetaval["_ftype"] == "hdf5"
299-
):
298+
if "proxy-source" in metaval or ("_ftype" in vlmetaval and vlmetaval["_ftype"] == "hdf5"):
300299
# Save operand as Proxy, see blosc2.open doc for more info.
301300
# Or, it can be an HDF5 dataset too (which should be handled in the next call)
302301
relpath = srv_utils.get_relpath(
@@ -325,6 +324,11 @@ def open_b2(abspath, path):
325324
elif isinstance(value, blosc2.LazyExpr):
326325
# Properly open the operands (to e.g. find proxies)
327326
for opkey, opvalue in value.operands.items():
327+
if isinstance(opvalue, blosc2.LazyExpr):
328+
continue
329+
relpath = srv_utils.get_relpath(
330+
opvalue, settings.cache, settings.personal, settings.shared, settings.public
331+
)
328332
value.operands[opkey] = open_b2(opvalue.schunk.urlpath, relpath)
329333

330334
return container

caterva2/tests/test_api.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
# See LICENSE.txt for details about copyright and rights to use.
88
###############################################################################
99
import contextlib
10+
import os
1011
import pathlib
1112

1213
import blosc2
1314
import httpx
15+
import numexpr as ne
1416
import numpy as np
1517
import pytest
1618

@@ -664,6 +666,50 @@ def test_lazyexpr(auth_client):
664666
np.testing.assert_array_equal(a[:], b[:])
665667

666668

669+
# More exercises for the expression evaluation with Blosc2 arrays
670+
@pytest.mark.parametrize(
671+
"expression",
672+
[
673+
"a + 50",
674+
"a ** 2.3 + b / 2.3",
675+
"sqrt(a) ** sin(b)",
676+
"where(a < 50, a + 50, b)",
677+
],
678+
)
679+
def test_lazyexpr2(expression, examples_dir, tmp_path, auth_client):
680+
if not auth_client:
681+
pytest.skip("authentication support needed")
682+
683+
root = pathlib.Path("@shared")
684+
remote_root = auth_client.get(root)
685+
remote_dir = "arrays"
686+
ds_a = f"{remote_dir}/3d-blosc2-a.b2nd"
687+
ds_b = f"{remote_dir}/3d-blosc2-b.b2nd"
688+
689+
with chdir_ctxt(tmp_path):
690+
os.makedirs(remote_dir, exist_ok=True)
691+
a = np.linspace(-1, 2, 1000).reshape(10, 10, 10)
692+
blosc2.asarray(a, urlpath=ds_a, chunks=(5, 10, 10))
693+
remote_a = remote_root.upload(ds_a)
694+
b = np.linspace(-1, 2, 1000).reshape(10, 10, 10)
695+
blosc2.asarray(b, urlpath=ds_b, chunks=(3, 5, 5))
696+
remote_b = remote_root.upload(ds_b)
697+
assert ds_a in remote_root
698+
assert ds_b in remote_root
699+
700+
operands = {"a": remote_a, "b": remote_b}
701+
lxpath = auth_client.lazyexpr("myexpr", expression, operands)
702+
assert lxpath == pathlib.Path("@personal/myexpr.b2nd")
703+
704+
# Compute the expression
705+
result = auth_client.get_slice(lxpath)
706+
assert isinstance(result, blosc2.NDArray)
707+
708+
# Check the data
709+
nresult = ne.evaluate(expression, {"a": a, "b": b})
710+
np.testing.assert_allclose(result[:], nresult)
711+
712+
667713
def test_lazyexpr_getchunk(auth_client):
668714
if not auth_client:
669715
pytest.skip("authentication support needed")

caterva2/tests/test_hdf5_proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def test_unfold_fetch(fetch_or_slice, examples_dir, tmp_path, auth_client):
241241
"where(a < 50, a + 50, b)",
242242
],
243243
)
244-
def test_expr_where(expression, examples_dir, tmp_path, auth_client):
244+
def test_expression(expression, examples_dir, tmp_path, auth_client):
245245
if not auth_client:
246246
pytest.skip("authentication support needed")
247247

0 commit comments

Comments
 (0)