Uh oh!
There was an error while loading. Please reload this page.
[WIP] Add map_blocks. - #3258
Conversation
mrocklin
left a comment
There was a problem hiding this comment.
This looks great! I left a bunch of tiny suggestions from a Dask Array perspective.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
crusaderky
commented
Aug 27, 2019
Hi, A few design opinions:
e.g. myarray.map(func1).chunk().map(func2).sum().compute() |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
shoyer
commented
Aug 27, 2019
I agree that I still think this particular set of functionality should be called |
crusaderky
commented
Aug 27, 2019
@shoyer let me rephrase it - apply_ufunc is extremely powerful, and when you need to cope with all possible shape transformations, I suspect its verbosity is quite necessary. The thing I have against the name map_blocks is that backends other than dask have no notion of blocks... |
shoyer
commented
Aug 27, 2019
Yes, 100% agreed! There is a real need for a simpler version of
I think the functionality in this PR is fundamentally dask specific. We shouldn't make a habit of adding backend specific features, but it makes sense in limited cases. |
I started prototyping a Dataset version. Here's what I have: importdaskimportxarrayasxrdarray=xr.DataArray(np.ones((10, 20)), dims=['x', 'y'], coords={'x': np.arange(10), 'y': np.arange(100, 120)})
dset=darray.to_dataset(name='a')
dset['b'] =dset.a+50dset['c'] = (dset.x+20)
dset=dset.chunk({'x': 4, 'y': 5})The function I'm applying takes a dataset and returns a DataArray because that's easy to test without figuring out how to assemble everything back into a dataset. importitertools# function takes dataset and returns dataarray so that I can check that things work without reconstructing a datasetdeffunction(ds):
returnds.a+10dataset_dims=list(dset.dims)
graph= {}
gname='dsnew'# map dims to list of chunk indexes# If different variables have different chunking along the same dim# the call to .chunks will raise an error.ichunk= {dim: range(len(dset.chunks[dim])) fordimindataset_dims}
# iterate over all possible chunk combinationsforvinitertools.product(*ichunk.values()):
chunk_index_dict=dict(zip(dataset_dims, v))
data_vars= {}
forname, variableindset.data_vars.items():
# why do does dask_keys have an extra level?# the [0] is not required for dataarraysvar_dask_keys=variable.__dask_keys__()[0]
# recursively index into dask_keys nested listchunk=var_dask_keysfordiminvariable.dims:
chunk=chunk[chunk_index_dict[dim]]
# I have key corresponding to chunk# this tuple is in a dictionary passed to xr.Dataset()# dask doesn't seem to replace this with a numpy array at execution time.data_vars[name] = (variable.dims, chunk)
graph[(gname, ) +v] = (function, (xr.Dataset, data_vars))
final_graph=dask.highlevelgraph.HighLevelGraph.from_collections(name, graph, dependencies=[dset])Elements of the graph look like This doesn't work because dask doesn't replace the keys by numpy arrays when the I'm not sure what I'm doing wrong here. An equivalent version for DataArrays works perfectly. |
mrocklin
commented
Aug 28, 2019
Dask doesn't traverse through tuples to find possible keys, so the keys here are hidden from view: {'a': (('x', 'y'), ('xarray-a-f178df193efafa67203f3862b3f9f0f4', 0, 0)),I recommend changing wrapping tuples with lists: - {'a': (('x', 'y'), ('xarray-a-f178df193efafa67203f3862b3f9f0f4', 0, 0)),+ {'a': [('x', 'y'), ('xarray-a-f178df193efafa67203f3862b3f9f0f4', 0, 0)], |
dcherian
commented
Aug 30, 2019
Thanks @mrocklin. Unfortunately that doesn't work with the Dataset constructor. With a list it treats it as array-like Unless @shoyer has another idea, I guess I can insert creating a DataArray into the graph and then refer to those keys in the Dataset constructor. |
Then you can construct a tuple as a task |
dcherian
commented
Sep 2, 2019
Thanks. That worked. I have a new version up in #3276 that works with both DataArrays and Datasets. |
mrocklin
commented
Sep 2, 2019
I'm glad to see progress here. FWIW, I think that many people would be quite happy with a version that just worked for DataArrays, in case that's faster to get in than the full solution with DataSets. |
dcherian
commented
Sep 8, 2019
Closing in favour of #3276 |

black . && mypy . && flake8whats-new.rstfor all changes andapi.rstfor new APIping @mrocklin@sofroniewn@shanaxel42