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

Specify meta in dask.array.map_blocks #5989

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

bouweandela
Copy link
Member

@bouweandela bouweandela commented Jun 6, 2024

🚀 Pull Request

Description

Specify meta in dask.array.map_blocks to avoid having the wrong chunktype in the output array.

Closes #5988


Consult Iris pull request check list


Add any of the below labels to trigger actions on this PR:

  • benchmark_this Request that this pull request be benchmarked to check if it introduces performance shifts

Copy link

codecov bot commented Jul 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.82%. Comparing base (1a9bdaa) to head (a165f61).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5989   +/-   ##
=======================================
  Coverage   89.82%   89.82%           
=======================================
  Files          88       88           
  Lines       23146    23155    +9     
  Branches     5043     5045    +2     
=======================================
+ Hits        20790    20799    +9     
  Misses       1624     1624           
  Partials      732      732           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bouweandela bouweandela marked this pull request as ready for review July 3, 2024 09:10
@pp-mo
Copy link
Member

pp-mo commented Jul 3, 2024

Hmm linkcheck fail is this one...
( whatsnew/1.4: line 131) broken https://www.opendap.org/ - 500 Server Error: Internal Server Error for url: https://www.opendap.org/

The website really is down right now :-(

@pp-mo pp-mo self-requested a review July 3, 2024 09:57
@pp-mo pp-mo self-assigned this Jul 3, 2024
Copy link
Member

@pp-mo pp-mo left a comment

Choose a reason for hiding this comment

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

This does seem basically sound, but I have a couple of worries to raise.
We can go with this if you don't like the suggestions.

Comment on lines 602 to 610
# Assume operation does not change dtype and meta if not specified.
if "meta" not in kwargs:
kwargs["meta"] = da.utils.meta_from_array(data)
if "dtype" in kwargs:
kwargs["meta"] = kwargs["meta"].astype(kwargs["dtype"])
else:
kwargs["dtype"] = kwargs["meta"].dtype

result = data.map_blocks(func, *args, chunks=out_chunks, **kwargs)
Copy link
Member

@pp-mo pp-mo Jul 9, 2024

Choose a reason for hiding this comment

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

I'm finding this special handling of "dtype" and/or "meta" rather confusing.
And I note that it is currently only exercised in tests, and nowhere in the actual code.

So I'm wondering if we can do without ??

If not, maybe they need documenting as a special case, or even listing as explicit keys : in the current code, it is rather confusing that these keywords, which specifically control map_blocks, are part of kwargs but will not be passed down to func like the docstring says they are (!).

Copy link
Member Author

@bouweandela bouweandela Jul 10, 2024

Choose a reason for hiding this comment

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

I was concerned about the assumption that the output dtype is always the same as the input dtype, that's why I thought it might be good to be able to specify this. I checked the dtypes coming out of the functions that use map_complete_blocks and tried to specify those in c1e5e1a. Does it look better now?

@@ -536,9 +536,12 @@ def lazy_elementwise(lazy_array, elementwise_op):
# This makes good practical sense for unit conversions, as a Unit.convert
# call may cast to float, or not, depending on unit equality : Thus, it's
# much safer to get udunits to decide that for us.
dtype = elementwise_op(np.zeros(1, lazy_array.dtype)).dtype
meta = da.utils.meta_from_array(lazy_array)
new_meta = elementwise_op(meta)
Copy link
Member

Choose a reason for hiding this comment

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

This now requires elementwise_op to work on an empty-shaped array, which was not the case before, and may not work for all array operations.
So I think that passing a minimal array of the correct dtype, as before, would be "safer".

Copy link
Member Author

Choose a reason for hiding this comment

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

I restored the previous behaviour in c1e5e1a

@bouweandela
Copy link
Member Author

@pp-mo This is ready for another review. The readthedocs build appears to be failing because it tries to download shapefiles from a server that is offline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Regridding produces wrong chunktype for lazy masked arrays
2 participants