-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LazyArray examples #257
LazyArray examples #257
Conversation
src/blosc2/lazyexpr.py
Outdated
>>> expr.save(urlpath='lazy_array.b2nd', mode='w') | ||
>>> # Open and load the LazyExpr from disk | ||
>>> disk_expr = blosc2.open('lazy_array.b2nd') | ||
>>> f"The first two rows of the loaded LazyExpr data: {disk_expr[:2]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too verbose. Perhaps simply write >>> disk_expr[:2]
? It should be explanatory enough IMO.
src/blosc2/lazyexpr.py
Outdated
>>> import numpy as np | ||
>>> # Define a user-defined function that will be applied to each block of data | ||
>>> def my_function(inputs_tuple, output, offset): | ||
>>> a, b = inputs_tuple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the sake of locality (which improves readability), I'd move this function definition as close as possible to their use down below.
src/blosc2/lazyexpr.py
Outdated
>>> # Perform the mathematical operation | ||
>>> expr = a1 + b1 | ||
>>> output = expr.eval() | ||
>>> print("Array A: ", a[:]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why uppercase 'A'? I don't even think printing them is necessary, as linspace()
should be easy to grasp for the reader.
src/blosc2/lazyexpr.py
Outdated
[[0. 0.625 1.25 ] | ||
[1.875 2.5 3.125] | ||
[3.75 4.375 5. ]] | ||
>>> print("Result of A + B (Lazy evaluation):", output[:]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'A + B' -> 'a + b'
src/blosc2/lazyexpr.py
Outdated
>>> b1 = blosc2.asarray(b) | ||
>>> # Perform the mathematical operation | ||
>>> expr = a1 + b1 # LazyExpr expression | ||
>>> output = expr.eval(cparams={"typesize": 4}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typesize
should be never needed when creating arrays in blosc2.
src/blosc2/lazyexpr.py
Outdated
>>> # Perform the mathematical operation | ||
>>> expr = a1 + b1 # LazyExpr expression | ||
>>> output = expr.eval(cparams={"typesize": 4}) | ||
>>> output[3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not checking getitem for blosc2.NDArray
here, but rather for np.array
.
src/blosc2/lazyexpr.py
Outdated
>>> output = expr.eval(cparams={"typesize": 4}) | ||
>>> output[3] | ||
[2.01680672 2.18487395 2.35294118 2.5210084 ] | ||
>>> output[2:4] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
src/blosc2/lazyexpr.py
Outdated
>>> operands = { 'a': a1, 'b': b1 } | ||
>>> lazy_expr = blosc2.lazyexpr(expr, operands=operands) | ||
>>> f"Lazy Expression Created: {lazy_expr}" | ||
Lazy Expression Created: a1 * b1 + 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point it should be illustrative to get some results out of the lazy expression
src/blosc2/lazyexpr.py
Outdated
Lazy Expression Created: a1 * b1 + 2 | ||
>>> expr_ = a1 * b1 + 2 | ||
>>> result = expr_.eval() | ||
>>> result[:] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above two line can be fused into one: expr_[:]
. Remember that a LazyExpr supports the __getitem__
interface.
src/blosc2/lazyexpr.py
Outdated
>>> a_path = 'a_array.b2nd' | ||
>>> b_path = 'b_array.b2nd' | ||
>>> a1 = blosc2.asarray(a, urlpath=a_path, mode='w') | ||
>>> b1 = blosc2.asarray(b, urlpath=b_path, mode='w') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a_path
and b_path
are going to be used just once, it is better to use the values directly. In this case, the above 5 lines can be reduced to:
a1 = blosc2.asarray(a, urlpath='a_array.b2nd', mode='w')
b1= blosc2.asarray(b, urlpath='b_array.b2nd', mode='w')
and it reads better IMO.
src/blosc2/lazyexpr.py
Outdated
>>> expr = a1 + b1 | ||
>>> output = expr.eval() | ||
>>> f"Result of a + b (Lazy evaluation): {output[:]}" | ||
Result of a + b (Lazy evaluation): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use upper case in the middle of sentences. For example, above "Lazy" -> "lazy".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than some comments, LGTM.
>>> import numpy as np | ||
>>> dtype = np.float64 | ||
>>> shape = [3, 3] | ||
>>> size = shape[0] * shape[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, you can change these kind of lines, for size = np.prod(shape)
src/blosc2/lazyexpr.py
Outdated
>>> expr_ = a1 * b1 + 2 | ||
>>> expr_[:] | ||
[[ 2. 2.390625 3.5625 ] | ||
[ 5.515625 8.25 11.765625] | ||
[16.0625 21.140625 27. ]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to show the results of the lazyexpr created with the blosc2.lazyexpr lazy_expr
instead of creating the same expression and showing it (expr_
)
No description provided.