Repository navigation
Closed
Labels
Description
Hi,
The following example causes me trouble:
import dask.array as da
import numpy as np
indices = da.arange(4, chunks=4)
data = da.arange(25, chunks=5).reshape(5, 5)
def get_samples(indices, data):
print(data, indices)
return data[:, indices]
da.map_blocks(get_samples, indices, data=data, new_axis=0, dtype=data.dtype).compute()Basically, it looks like the data variable is converted to a string, hence the [:, indices] failing with a TypeError. Using a numpy array as data works as expected.
(btw, this is obviously just a toy example).
I understand that data needs to be computed before we can run map_blocks, but I don't want to do it just there.
So this looks like a bug to me, but maybe I'm missing something ?
Traceback:
-> python test_dask.py tmp/
('finalize-4b8aee4f1226703a4df0635fe70b6c86', array([0, 1, 2, 3]))
Traceback (most recent call last):
File "test_dask.py", line 11, in <module>
da.map_blocks(get_samples, indices, data=data, new_axis=0, dtype=data.dtype).compute()
File "/home/a001673/.local/lib/python2.7/site-packages/dask/base.py", line 143, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/home/a001673/.local/lib/python2.7/site-packages/dask/base.py", line 392, in compute
results = get(dsk, keys, **kwargs)
File "/home/a001673/.local/lib/python2.7/site-packages/dask/threaded.py", line 75, in get
pack_exception=pack_exception, **kwargs)
File "/home/a001673/.local/lib/python2.7/site-packages/dask/local.py", line 521, in get_async
raise_exception(exc, tb)
File "/home/a001673/.local/lib/python2.7/site-packages/dask/local.py", line 290, in execute_task
result = _execute_task(task, data)
File "/home/a001673/.local/lib/python2.7/site-packages/dask/local.py", line 271, in _execute_task
return func(*args2)
File "test_dask.py", line 9, in get_samples
return data[:, indices]
TypeError: string indices must be integers, not tuple
Activity
mrocklin commentedon Mar 9, 2018
I believe that map_blocks currently only unpacks dask arguments from args, not from kwargs. This could be changed if someone wants to submit a fix, but short term I would recommend including your dask array arguments as normal positional arguments.
djhoese commentedon Mar 9, 2018
@mrocklin I believe he intended to compute the entire
dataarray and pass it to the function so it could be indexed. The oddness here is that the token of the dask array seems to get passed to the function and not the dask array.mraspaud commentedon Mar 9, 2018
@mrocklin yes, I understand that, and the intended purposed wasn't to unpack the
dataarray, rather to index it. The strange thing is thatmap_blockstransforms thedatadask array into a string (looks like the token as @davidh-ssec says) that is then unusable as is in theget_samplesfunction.mraspaud commentedon Mar 10, 2018
Also passing data as a positional argument exhibits the same problem:
da.map_blocks(get_samples, indices, data, new_axis=0, dtype=data.dtype).compute()->
TypeError: string indices must be integers, not tuplemrocklin commentedon Mar 10, 2018
Using positional arguments I get the following error:
mraspaud commentedon Mar 12, 2018
Yes, I get that error too with data as positional argument, sorry for that.
Anyway, the use case is when you want to create an array with the values of another (
data) using theindiceswhich has the shape of the array to create.In that case we can't really use
dataas a positional argument. Is there a way to delay the evaluation ofdatauntilget_samplesis executed, since passingdataas a dask array in the kwargs just returns its token ?mraspaud commentedon Mar 12, 2018
I looked a bit further, and it looks like this function is responsible for replacing the
dataarray with its token:dask/dask/delayed.py
Lines 32 to 37 in 69ea16e
So I see the delayed objects are replaced with their tokens, should it do the same for dask arrays ? Is there a way to retrieve the dask array from its token somehow ?
mraspaud commentedon Mar 13, 2018
Ok, new discovery this morning: if I remove
new_axis=0from themap_blockscall,datapassed toget_samplesis a numpy array, which is perfect in my case.However, the shape of the resulting dask array is wrong. Running
computemakes it right, but it means the shape of the dask array is unusable (which is problematic for us as we use xarray in conjuction with dask).So, which behaviour is the right one when passing
datatoget_samplesviamap_blocks?jakirkham commentedon Apr 5, 2018
For some problems,
map_blocksis great. However as one starts to adventure outside of the canonical usage ofmap_blocks, things can get a bit strange. Fortunatelymap_blocksis pretty good about suggesting an alternative as part of the error message for some of these common extensions.Namely it suggests
atop. Would suggest taking a look at it for this use case as well. It can be a little daunting understandingatop, but the examples help and playing with a few toy problems should clarify it further. So take a look and see if it helps at all. Would be good to hear back either way. :)djhoese commentedon Jun 20, 2018
Small update on this: I was able to use
atopto solve @mraspaud's problem. He had written some logic to get around dask arrays not supporting indexing by dask arrays and I used that to provide the necessary dimension information toatop. For reference (although I don't know how readable this is out of context):Where
iais an integer index array (dask),viiis a boolean array of "valid inputs", and the original input data is an xarray DataArray. Above this code is a big for loop to map source dimensions to output dimensions along with figuring out which dimensions are flattened to handle the boolean array indexing being used. The ugliest part of this is that I have to provide these_sliceslists so that I ignore dimensions I don't care about and slicing the ones I do care about. Not sure there is an easier way to do this.I guess it is up to you guys if this issue stays open since the original question is more generic than our use case.
GenevieveBuckley commentedon Oct 12, 2021
I can run the code snippet without error with dask 2021.09.0, so I'm going to close this issue since it looks like it's been addressed in more recent versions of Dask.
Anyone still experiencing this should try upgrading their version of dask, and if that doesn't help then please open an issue.