sofroniewn / tutorial-scipy2021-bioimage-analysis-fundamentals

Tutorial for SciPy 2021 Bioimage Analysis Fundamentals in Python
BSD 3-Clause "New" or "Revised" License
30 stars 8 forks source link

Dask issue lesson 3 #1

Closed sofroniewn closed 3 years ago

sofroniewn commented 3 years ago

@jni I'm running into a dask issue in lesson 3 - at the viewer = napari.view_image(embryo) call. Any idea what's going wrong? Could be a dask-image bug?

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/var/folders/30/g_dcv_xs2l315nvmwq0_v52r0000gr/T/ipykernel_16513/2174131371.py in <module>
----> 1 viewer = napari.view_image(embryo)

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/view_layers.py in view_image(*args, **kwargs)
    132 @_merge_layer_viewer_sigs_docs
    133 def view_image(*args, **kwargs):
--> 134     return _make_viewer_then('add_image', args, kwargs)
    135 
    136 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/view_layers.py in _make_viewer_then(add_method, args, kwargs)
    117         kwargs.update(kwargs.pop("kwargs"))
    118     method = getattr(viewer, add_method)
--> 119     method(*args, **kwargs)
    120     return viewer
    121 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/components/viewer_model.py in add_image(self, data, channel_axis, rgb, colormap, contrast_limits, gamma, interpolation, rendering, iso_threshold, attenuation, name, metadata, scale, translate, rotate, shear, affine, opacity, blending, visible, multiscale)
    692                         )
    693                     )
--> 694             layer = Image(data, **kwargs)
    695             self.layers.append(layer)
    696 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/image/image.py in __init__(self, data, rgb, colormap, contrast_limits, gamma, interpolation, rendering, iso_threshold, attenuation, name, metadata, scale, translate, rotate, shear, affine, opacity, blending, visible, multiscale)
    288 
    289         # Trigger generation of view slice and thumbnail
--> 290         self._update_dims()
    291 
    292     def _new_empty_slice(self):

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/base/base.py in _update_dims(self, event)
    542         self._ndim = ndim
    543 
--> 544         self.refresh()
    545 
    546     @property

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/base/base.py in refresh(self, event)
    944         """Refresh all layer data based on current view slice."""
    945         if self.visible:
--> 946             self.set_view_slice()
    947             self.events.set_data()
    948             self._update_thumbnail()

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/base/base.py in set_view_slice(self)
    808     def set_view_slice(self):
    809         with self.dask_optimized_slicing():
--> 810             self._set_view_slice()
    811 
    812     @abstractmethod

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/image/image.py in _set_view_slice(self)
    609             self, image_indices, image, thumbnail_source
    610         )
--> 611         self._load_slice(data)
    612 
    613     @property

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/image/image.py in _load_slice(self, data)
    627         data : Slice
    628         """
--> 629         if self._slice.load(data):
    630             # The load was synchronous.
    631             self._on_data_loaded(data, sync=True)

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/image/_image_slice.py in load(self, data)
    121         """
    122         self.loaded = False  # False until self._on_loaded is calls
--> 123         return self.loader.load(data)
    124 
    125     def on_loaded(self, data: ImageSliceData) -> bool:

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/image/_image_loader.py in load(self, data)
     20             True if load happened synchronously.
     21         """
---> 22         data.load_sync()
     23         return True
     24 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/napari/layers/image/_image_slice_data.py in load_sync(self)
     42     def load_sync(self) -> None:
     43         """Call asarray on our images to load them."""
---> 44         self.image = np.asarray(self.image)
     45 
     46         if self.thumbnail_source is not None:

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/array/core.py in __array__(self, dtype, **kwargs)
   1506 
   1507     def __array__(self, dtype=None, **kwargs):
-> 1508         x = self.compute()
   1509         if dtype and x.dtype != dtype:
   1510             x = x.astype(dtype)

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/base.py in compute(self, **kwargs)
    284         dask.base.compute
    285         """
--> 286         (result,) = compute(self, traverse=False, **kwargs)
    287         return result
    288 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/base.py in compute(*args, **kwargs)
    566         postcomputes.append(x.__dask_postcompute__())
    567 
--> 568     results = schedule(dsk, keys, **kwargs)
    569     return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
    570 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/threaded.py in get(dsk, result, cache, num_workers, pool, **kwargs)
     77             pool = MultiprocessingPoolExecutor(pool)
     78 
---> 79     results = get_async(
     80         pool.submit,
     81         pool._max_workers,

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/local.py in get_async(submit, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, chunksize, **kwargs)
    512                             _execute_task(task, data)  # Re-execute locally
    513                         else:
--> 514                             raise_exception(exc, tb)
    515                     res, worker_id = loads(res_info)
    516                     state["cache"][key] = res

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/local.py in reraise(exc, tb)
    323     if exc.__traceback__ is not tb:
    324         raise exc.with_traceback(tb)
--> 325     raise exc
    326 
    327 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
    221     try:
    222         task, data = loads(task_info)
--> 223         result = _execute_task(task, data)
    224         id = get_id()
    225         result = dumps((result, id))

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/core.py in _execute_task(arg, cache, dsk)
    119         # temporaries by their reference count and can execute certain
    120         # operations in-place.
--> 121         return func(*(_execute_task(a, cache) for a in args))
    122     elif not ishashable(arg):
    123         return arg

IndexError: too many indices for array: array is 3-dimensional, but 4 were indexed
sofroniewn commented 3 years ago

Note the bug also happens with np.asarray(embryo[0])

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/var/folders/30/g_dcv_xs2l315nvmwq0_v52r0000gr/T/ipykernel_16513/3912845265.py in <module>
----> 1 np.asarray(embryo[0])

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/array/core.py in __array__(self, dtype, **kwargs)
   1506 
   1507     def __array__(self, dtype=None, **kwargs):
-> 1508         x = self.compute()
   1509         if dtype and x.dtype != dtype:
   1510             x = x.astype(dtype)

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/base.py in compute(self, **kwargs)
    284         dask.base.compute
    285         """
--> 286         (result,) = compute(self, traverse=False, **kwargs)
    287         return result
    288 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/base.py in compute(*args, **kwargs)
    566         postcomputes.append(x.__dask_postcompute__())
    567 
--> 568     results = schedule(dsk, keys, **kwargs)
    569     return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
    570 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/threaded.py in get(dsk, result, cache, num_workers, pool, **kwargs)
     77             pool = MultiprocessingPoolExecutor(pool)
     78 
---> 79     results = get_async(
     80         pool.submit,
     81         pool._max_workers,

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/local.py in get_async(submit, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, chunksize, **kwargs)
    512                             _execute_task(task, data)  # Re-execute locally
    513                         else:
--> 514                             raise_exception(exc, tb)
    515                     res, worker_id = loads(res_info)
    516                     state["cache"][key] = res

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/local.py in reraise(exc, tb)
    323     if exc.__traceback__ is not tb:
    324         raise exc.with_traceback(tb)
--> 325     raise exc
    326 
    327 

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
    221     try:
    222         task, data = loads(task_info)
--> 223         result = _execute_task(task, data)
    224         id = get_id()
    225         result = dumps((result, id))

~/opt/anaconda3/envs/scipy2021-bioimage-analysis/lib/python3.8/site-packages/dask/core.py in _execute_task(arg, cache, dsk)
    119         # temporaries by their reference count and can execute certain
    120         # operations in-place.
--> 121         return func(*(_execute_task(a, cache) for a in args))
    122     elif not ishashable(arg):
    123         return arg

IndexError: too many indices for array: array is 3-dimensional, but 4 were indexed

so something is going wrong with dask-image or the data, it has nothing to do with napari

sofroniewn commented 3 years ago

solved by pinning dask-image==0.5.0

sofroniewn commented 3 years ago

see https://github.com/dask/dask-image/issues/220