Skip to content

API: provides method to compute unknown chunks sizes - #5312

Merged
TomAugspurger merged 26 commits into
dask:masterfrom
stsievert:unknown-chunks
Sep 7, 2019
Merged

TomAugspurger merged 26 commits into
dask:masterfrom
stsievert:unknown-chunks

Conversation

@stsievert

Copy link
Copy Markdown
Member

What does this PR implement?
It provides a method to compute unknown chunk sizes:

x = da.random.normal(size=20, chunks=5)
y = x[x < 0]
assert y.chunks == ((nan, nan, nan, nan), )
y.compute_chunksizes()
assert y.chunks  ((1, 2, 2, 1), )

References issues/PRs

  • Tests added / passed
  • Passes black dask / flake8 dask

@stsievert

Copy link
Copy Markdown
Member Author

I also added a flag to Array.persist (metadata). This is the API suggested in #3293 (comment). This proposal has raised some concerns by @mrocklin in #3293 (comment):

But I hesitate to do this a bit because the persist API is used for other collections and for the dask.persist function as well. The inconsistency might be a little surprising to users (though probably not that bad). I think that the two-liner is ok, and in many cases probably about the same speed.

I'd expect persist to compute the relevant metadata for the Dask Array. I understand this doesn't fit with Dask's data model (#4292 (comment)), which is why no computation occurs by default.

@stsievert

Copy link
Copy Markdown
Member Author

I'm getting some CI errors on importing:

from .core import concatenate3, Array, normalize_chunks
E   ImportError: cannot import name 'concatenate3'
E
E   Dask array requirements are not installed

I'm getting the same errors on my local machine.

@jrbourbeau

Copy link
Copy Markdown
Member

I think that's related to the from .rechunk import _get_chunks import added to dask/array/core.py (rechunk itself imports things from core). You may consider placing from .rechunk import _get_chunks inside the body of the method where _get_chunks is called

@stsievert

Copy link
Copy Markdown
Member Author

Thanks @jrbourbeau! That fixed the error.

I think there's some future work to do on naming.

@jrbourbeau

Copy link
Copy Markdown
Member

Looks like all tests are passing now 🎉 But a black formatting issue is causing CI failures on Travis

@stsievert stsievert changed the title ENH: provides method to compute unknown chunks sizes API: provides method to compute unknown chunks sizes Aug 22, 2019
@stsievert

Copy link
Copy Markdown
Member Author

I've done some renaming. Now, the chunk sizes can be computed with x.compute_metadata() or x.persist(compute_metadata=True).

@jrbourbeau

Copy link
Copy Markdown
Member

Thanks for your work here @stsievert, I'll give this a more thorough review tomorrow

@jrbourbeau jrbourbeau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks again for working on this @stsievert, looking forward to seeing this PR merged. Overall, these additions looks good! I've attached a few review comments.

I think the main thing left to do is to determine a name for the new method (e.g. compute_metadata vs. compute_chunksizes) and decide if we want to add a new compute_metadata (or some other name) parameter to the Array.persist method. @mrocklin previously you were hesitant to add parameters to persist that weren't general to all dask collections, is this still the case?

Also, @stsievert once we finalize a method name could you it to the array API docs?

Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/tests/test_rechunk.py Resolved
@jrbourbeau

Copy link
Copy Markdown
Member

cc @jakirkham

@mrocklin

Copy link
Copy Markdown
Member

@mrocklin previously you were hesitant to add parameters to persist that weren't general to all dask collections, is this still the case?

If it's ok I'm a little low on mental energy at the moment. I'd appreciate it if some of the other maintainers could take on responsibility for this decision.

@jakirkham is off this week. I usually ping @jcrist when it comes to the dask.base API, and matters of consistency.

@stsievert

Copy link
Copy Markdown
Member Author

Thanks for the review @jrbourbeau! This PR is on hold until after Labor Day; I’m out this week. I’ll clean up this PR after that (and respond to the other comments you left).

name in the array API docs

Can do. I think a bigger documentation note should also be included; I’ll try to add that, possibly in a separate PR.

@stsievert

stsievert commented Aug 30, 2019 •

Copy link
Copy Markdown
Member Author

I've responded to the review and cleaned a bit.

I'd expect persist to compute the relevant metadata for the Dask Array

I don't know if I'd expect that. The documentation for dask.persist says

This turns lazy Dask collections into Dask collections with the same metadata, but now with their results fully computed or actively computing in the background.

—https://docs.dask.org/en/latest/api.html#dask.persist (emphasis added)

I think I'd support adding a compute_metadata flag to dask.persist.

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @stsievert ! I left a few tiny comments, but hopefully everything is straightforward.

Comment thread dask/array/core.py Resolved
Comment thread dask/array/rechunk.py Resolved
Comment thread dask/array/rechunk.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
@stsievert

Copy link
Copy Markdown
Member Author

Thanks for the review @mrocklin! I should have caught some of those comments earlier – sorry 'bout that.

I changed the name again – compute_chunk_sizes is exactly what this computation does. Maybe in the future a compute_metadata attribute can be added to retrieve more information (e.g., fsspec properties).

Comment thread dask/array/tests/test_array_core.py Resolved
Comment thread dask/array/tests/test_array_core.py Resolved
@stsievert

stsievert commented Sep 3, 2019 •

Copy link
Copy Markdown
Member Author

I've implemented suggestions to use compute_chunk_sizes in the error messages for several functions. Here's an example:

>>> x = da.from_array([-1, 0, 1], chunks=1)
>>> y = x[x >= 0]
>>> y.rechunk("auto")
ValueError: Can not perform automatic rechunking with unknown (nan) chunk sizes.

A possible solution is with
  x.compute_chunk_sizes()
>>> y = y.compute_chunk_sizes()
>>> y.rechunk("auto")
dask.array<rechunk-merge, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>

I currently add this snippet to error messages for rechunk, concatenate, to_zarr, to_svg, reshape, reductions (e.g., argmin) and slicing. I test that these error messages work as expected.

Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
Comment thread dask/array/tests/test_array_core.py Resolved
@stsievert

Copy link
Copy Markdown
Member Author

compute_chunk_sizes modifies the Dask array in place. Is this an issue?

I think I'd expect it to modify the Dask array in place – the side effect ofcompute_chunk_sizes is positive.

Pragmatically, I'm not sure this matters because Dask arrays are immutable:

dask/dask/array/core.py

Lines 2183 to 2185 in 973c6e1

def copy(self):
"""
Copy array. This is a no-op for dask.arrays, which are immutable

@TomAugspurger TomAugspurger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good overall. Can you add a note to https://docs.dask.org/en/latest/array-chunks.html#unknown-chunks and an api entry in https://docs.dask.org/en/latest/array-api.html.

If you're interested, it'd be nice to be more consistent on the error message. Some of "Solution in", others have "A solution in". Some have colons.

Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
@quasiben

quasiben commented Sep 6, 2019

Copy link
Copy Markdown
Member

I think it's worth adding a test for a 3d or anything n > 2 array

@TomAugspurger
TomAugspurger merged commit 0ba2223 into dask:master Sep 7, 2019
@TomAugspurger

Copy link
Copy Markdown
Member

Thanks @stsievert!

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.

5 participants