You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issue with trim_excess in dask.array.coarsen #6102
I am trying to use dask.array.coarsen on 3-d arrays and am finding that the result depends on the chunking of the data, which doesn't seem right. The following example illustrates the issue:
In [1]: importnumpyasnpIn [2]: importdask.arrayasdaIn [3]: array1=da.random.random((623, 768, 768)).rechunk((322, 256, 256))
In [4]: array2=da.random.random((623, 768, 768)).rechunk((623, 768, 768))
In [5]: importnumpyasnpIn [6]: da.coarsen(np.nanmean, array1, {1: 3}, trim_excess=True).shapeOut[6]: (623, 255, 768)
In [7]: da.coarsen(np.nanmean, array2, {1: 3}, trim_excess=True).shapeOut[7]: (623, 256, 768)
Since 768 is divisible by 3, I would expect the final array to have shape (623, 256, 768) as for array2. The result for array1 seems incorrect, so I think this is a bug?
I think the issue is that the coarsen is happening on each chunk so you are getting a different result than expected because 256 is not divisible by 3. That is why you get an error with array1 if you take out trim_excess=True. So I am not entirely sure if this is worth changing or not.
I feel like a top-level function such as this should probably return the same result independent of how the data is chunked (unlike a function like say map_blocks which explicitly operates on chunks)
da.coarsen doesn't apply any overlaps between neighboring blocks, so the output is always dependent on the chunking of the input. There may be a bug here in terms of the output shape; I'm not familiar enough with coarsen to know, perhaps @mrocklin remembers - this method is one of the first things added to dask in 2015 and hasn't been touched much since. However, the output values will still differ depending on chunking, since no windowing is used.
For windowed computations that don't depend on how the data is chunked I recommend looking at da.map_overlap.
The result for array1 seems incorrect, so I think this is a bug?
Looks like it to me
I feel like a top-level function such as this should probably return the same result independent of how the data is chunked
+1
I think that the solution here is to verify that all chunks of the coarsened dimensions align with the coarsening, and if not, to call a minimally invasive rechunk automatically within this function.
This shouldn't add too much extra cost, and should make sure that we get correct and consistent results.
If someone wanted to solve this I would recommend the following approach:
Make a function that takes in the chunks for a single dimension and an integer coarsenting value and produces a new chunking that is aligned with that value
defaligned_coarse_chunks(chunks: List[int], multiple: int) ->List[int]:
""" Returns a new chunking aligned with the coarsening multiple""">>>aligned_coarse_chunks((20, 10, 15, 23, 24), 10)
(20, 10, 20, 20, 20, 2)
Note that the sum of the two lists should be the same, all numbers should be multiples of the multiple, and any excess should be at the end.
Then I would apply that function to all of the dimensions that are being chunked with the right multiple. If the resulting chunking is different from the inputs .chunks value then I would call rechunk with that combined tuple.
Activity
jsignell commentedon Apr 16, 2020
I think the issue is that the coarsen is happening on each chunk so you are getting a different result than expected because 256 is not divisible by 3. That is why you get an error with array1 if you take out
trim_excess=True. So I am not entirely sure if this is worth changing or not.The relevant code is here:
https://github.com/dask/dask/blob/366c7998398bc778c4aa5f4b6bb22c25b584fbc1/dask/array/routines.py#L1303:L1305
astrofrog commentedon Apr 20, 2020
I feel like a top-level function such as this should probably return the same result independent of how the data is chunked (unlike a function like say
map_blockswhich explicitly operates on chunks)jcrist commentedon Apr 20, 2020
da.coarsendoesn't apply any overlaps between neighboring blocks, so the output is always dependent on the chunking of the input. There may be a bug here in terms of the output shape; I'm not familiar enough withcoarsento know, perhaps @mrocklin remembers - this method is one of the first things added to dask in 2015 and hasn't been touched much since. However, the output values will still differ depending on chunking, since no windowing is used.For windowed computations that don't depend on how the data is chunked I recommend looking at
da.map_overlap.mrocklin commentedon Apr 21, 2020
Looks like it to me
+1
I think that the solution here is to verify that all chunks of the coarsened dimensions align with the coarsening, and if not, to call a minimally invasive rechunk automatically within this function.
This shouldn't add too much extra cost, and should make sure that we get correct and consistent results.
mrocklin commentedon Apr 21, 2020
If someone wanted to solve this I would recommend the following approach:
Make a function that takes in the chunks for a single dimension and an integer coarsenting value and produces a new chunking that is aligned with that value
Note that the sum of the two lists should be the same, all numbers should be multiples of the
multiple, and any excess should be at the end.Then I would apply that function to all of the dimensions that are being chunked with the right multiple. If the resulting chunking is different from the inputs
.chunksvalue then I would call rechunk with that combined tuple.dcherian commentedon Aug 24, 2020
Here's my attempt at this function
For these tests:
I get
Does this look sensible?