jni / zarpaint

Paint segmentations directly to on-disk/remote zarr arrays
BSD 3-Clause "New" or "Revised" License
14 stars 8 forks source link

tensorstore may not be necessary #17

Open ianhi opened 2 years ago

ianhi commented 2 years ago

Wanted to share this here as it may be useful and was inspired by your PR (https://github.com/napari/napari/pull/3155)

I found that a user of napari can get a zarr.Array to work in a labels layer by wrapping it like so:

import napari

import zarr
import wrapt
import numpy as np

class zarrWrapper(wrapt.ObjectProxy):
    """Add fancy indexing to zarr."""

    def __getitem__(self, key):
        """Get some data inplace maybe using numpy indexing"""
        try:
            return self.__class__.__getitem__(self, key)
        except IndexError as e:
            try:
                return self.vindex[key]
            except Exception:
                raise e

    def __setitem__(self, key, value):
        """Edit data in-place allowing for fancy indexing"""
        try:
            self.__class__.__setitem__(self, key, value)
        except IndexError as e:
            try:
                self.vindex[key] = value
            except Exception:
                raise e

z_arr = zarrWrapper(zarr.zeros((50, 512, 512), dtype=np.uint16))

viewer = napari.Viewer()
viewer.add_labels(z_arr)
napari.run()
jni commented 2 years ago

Thanks @ianhi, this is very nice! I will say that (a) try/except is actually quite expensive in Python, so best to avoid it for something interactive like painting, but (b) I actually added fancy indexing to zarr arrays by default recently and this will be in release 2.11, so I'm just waiting for that to happen then we'll drop the tensorstore dependency. 😊

ianhi commented 2 years ago

I actually added fancy indexing to zarr arrays by default recently and this will be in release 2.11, so I'm just waiting for that to happen then we'll drop the tensorstore dependency. blush

Amazing!