Skip to content
Merged
27 changes: 13 additions & 14 deletions examples/ndarray/reduce_and_enlarge.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
# In particular, note how re-opening a stored expression can adapt to
# changes in the operands.

import numpy as np

import blosc2

# Create arrays with specific dimensions
a = blosc2.full((2, 3, 4), 1, urlpath="a.b2nd", mode="w") # 3D array with dimensions (2, 3, 4)
b = blosc2.full((2, 4), 2, urlpath="b.b2nd", mode="w") # 2D array with dimensions (2, 4)
c = blosc2.full(4, 3, urlpath="c.b2nd", mode="w") # 1D array with dimensions (4,)
c = blosc2.full(4, 3, dtype=np.int8, urlpath="c.b2nd", mode="w") # 1D array with dimensions (4,)

print("Array a:", a[:])
print("Array b:", b[:])
Expand All @@ -28,11 +30,18 @@
# Define an expression using the arrays above
# expression = "a.sum() + b * c"
# expression = "a.sum(axis=1) + b * c"
expression = "sum(a, axis=1) + b * c"
# expression = "sum(a, axis=1) + b * c + 0"
expression = "c + np.int8(0)"
# expression = "sum(a, axis=1) + b * sin(c)"
# Define the operands for the expression
operands = {"a": a, "b": b, "c": c}
# operands = {"a": a, "b": b, "c": c}
operands = {"c": c}
# Create a lazy expression
print("expression:", expression, type(expression), operands["c"].dtype)
lazy_expression = blosc2.lazyexpr(expression, operands)
print(lazy_expression.shape, lazy_expression.dtype) # Print the shape of the lazy expression
# lazy_expression = blosc2.lazyexpr(expression)
print(lazy_expression[:]) # Evaluate and print the result of the lazy expression (should be a 2x4 arr)

# Store and reload the expressions
url_path = "my_expr.b2nd" # Define the path for the file
Expand All @@ -41,15 +50,5 @@
url_path = "my_expr.b2nd" # Define the path for the file
lazy_expression = blosc2.open(urlpath=url_path) # Open the saved file
print(lazy_expression) # Print the lazy expression
print(lazy_expression.shape) # Print the shape of the lazy expression
print(lazy_expression.shape, lazy_expression.dtype) # Print the shape of the lazy expression
print(lazy_expression[:]) # Evaluate and print the result of the lazy expression (should be a 2x4 arr)

# Enlarge the arrays and re-evaluate the expression
a.resize((3, 3, 4))
a[2] = 3
b.resize((3, 4))
b[2] = 5
lazy_expression = blosc2.open(urlpath=url_path) # Open the saved file
print(lazy_expression.shape)
result = lazy_expression.compute()
print(result[:])
Loading