Skip to content
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

Merged
merged 5 commits into from
Sep 17, 2024
Merged

LazyArray examples #257

merged 5 commits into from
Sep 17, 2024

Conversation

omaech
Copy link
Collaborator

@omaech omaech commented Sep 16, 2024

No description provided.

@omaech omaech changed the title Lazyexpr examples LazyArray examples Sep 16, 2024
>>> 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]}
Copy link
Member

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.

>>> 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
Copy link
Member

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.

>>> # Perform the mathematical operation
>>> expr = a1 + b1
>>> output = expr.eval()
>>> print("Array A: ", a[:])
Copy link
Member

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.

[[0. 0.625 1.25 ]
[1.875 2.5 3.125]
[3.75 4.375 5. ]]
>>> print("Result of A + B (Lazy evaluation):", output[:])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'A + B' -> 'a + b'

>>> b1 = blosc2.asarray(b)
>>> # Perform the mathematical operation
>>> expr = a1 + b1 # LazyExpr expression
>>> output = expr.eval(cparams={"typesize": 4})
Copy link
Member

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.

>>> # Perform the mathematical operation
>>> expr = a1 + b1 # LazyExpr expression
>>> output = expr.eval(cparams={"typesize": 4})
>>> output[3]
Copy link
Member

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.

>>> output = expr.eval(cparams={"typesize": 4})
>>> output[3]
[2.01680672 2.18487395 2.35294118 2.5210084 ]
>>> output[2:4]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

>>> operands = { 'a': a1, 'b': b1 }
>>> lazy_expr = blosc2.lazyexpr(expr, operands=operands)
>>> f"Lazy Expression Created: {lazy_expr}"
Lazy Expression Created: a1 * b1 + 2
Copy link
Member

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

Lazy Expression Created: a1 * b1 + 2
>>> expr_ = a1 * b1 + 2
>>> result = expr_.eval()
>>> result[:]
Copy link
Member

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.

>>> 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')
Copy link
Member

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.

>>> expr = a1 + b1
>>> output = expr.eval()
>>> f"Result of a + b (Lazy evaluation): {output[:]}"
Result of a + b (Lazy evaluation):
Copy link
Member

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".

Copy link
Member

@FrancescAlted FrancescAlted left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@martaiborra martaiborra left a 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]
Copy link
Collaborator

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)

Comment on lines 2043 to 2047
>>> expr_ = a1 * b1 + 2
>>> expr_[:]
[[ 2. 2.390625 3.5625 ]
[ 5.515625 8.25 11.765625]
[16.0625 21.140625 27. ]]
Copy link
Collaborator

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_)

@FrancescAlted FrancescAlted merged commit 52c651a into Blosc:main Sep 17, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants