Dask_image.ndmeasure.center_of_mass gives ValueError: Chunks do not add up to shape

I’m looking to find the center of mass for labelled objects in my 3D-image. But I get an error when I use center_of_mass and I don’t know what I do wrong. I’ve tried rechunking but it didn’t help.

Minimum reproducible example:

import dask.array as da
import dask_image.ndmeasure as dmeasure

dummy_arr = da.zeros((8000, 700, 700))
dummy_arr[500:600, 100:200, 100:200] = 1
dummy_arr[2500:2600, 100:200, 100:200] = 1
labels, N = dmeasure.label(dummy_arr)
com = dmeasure.center_of_mass(dummy_arr, labels)

Error:

File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "c:\users\kahy\skogforsk\mätdoktorandssf - general\teams ct-tör\mätningen\code\test.py", line 53, in <module>
    com = dmeasure.center_of_mass(dummy_arr, labels)

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask_image\ndmeasure\__init__.py", line 137, in center_of_mass
    com_lbl = labeled_comprehension(

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask_image\ndmeasure\__init__.py", line 433, in labeled_comprehension
    positions = _utils._ravel_shape_indices(

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask_image\ndmeasure\_utils\__init__.py", line 60, in _ravel_shape_indices
    indices = [

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask_image\ndmeasure\_utils\__init__.py", line 61, in <listcomp>
    da.arange(

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask\backends.py", line 122, in wrapper
    return getattr(self, dispatch_name)(*args, **kwargs)

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask\array\creation.py", line 394, in arange
    chunks = normalize_chunks(chunks, (num,), dtype=dtype)

  File "C:\Program Files\Miniconda3\envs\ct_env2\lib\site-packages\dask\array\core.py", line 3105, in normalize_chunks
    raise ValueError(

ValueError: Chunks do not add up to shape. Got chunks=((255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 95),), shape=(0,)

Hi,

I took the right to update your post with code cells for a better readability.

However, I tried your code and didn’t end up with any error.
Except when trying to compute the result, my notebook kernel crashed, but it’s probably because the dummy_arr is too big.

Thanks for looking into it! I tried again with a dummy_arr that’s 4000 long instead of 8000, and then it worked. At length 4500 and above, I get the error. So the error does seem to be a size issue. It does surprise me a bit though since I haven’t had that problem with any other function than center_of_mass. I might raise an issue on it.

It worked with the initial shape your provided on my laptop, so not sure what the problem is on your side.