Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update colormap handling to avoid deprecation issue #41

Open
jluethi opened this issue Feb 27, 2024 · 2 comments
Open

Update colormap handling to avoid deprecation issue #41

jluethi opened this issue Feb 27, 2024 · 2 comments

Comments

@jluethi
Copy link
Collaborator

jluethi commented Feb 27, 2024

With napari 0.4.19, we run into the following deprecation warnings:

/Users/joel/mambaforge/envs/20240227_napari-classifier_0419/lib/python3.10/site-packages/napari/utils/_proxies.py:144: FutureWarning: Labels.color is deprecated since 0.4.19 and will be removed in 0.5.0, please set Labels.colormap directly with an instance of napari.utils.colormaps.DirectLabelColormap instead.!
/Users/joel/mambaforge/envs/20240227_napari-classifier_0419/lib/python3.10/site-packages/napari/utils/_proxies.py:144: FutureWarning: Labels.color_mode is deprecated since 0.4.19 and will be removed in 0.5.0. Please set Labels.colormap instead, to aninstance of napari.utils.colormaps.CyclicLabelColormap for "auto" mode, or napari.utils.colormaps.DirectLabelColormap for "direct" mode.!

We're addressing this in #40.

@jluethi
Copy link
Collaborator Author

jluethi commented Feb 27, 2024

Resetting the whole colormap works well by using this:

    colors = cmap(label_layer.features[feature_col].astype(float) / len(cmap.colors))
    colordict = dict(zip(label_layer.features[label_column], colors))
    colordict[None] = [0, 0, 0, 0]
    display_layer.colormap = DirectLabelColormap(color_dict=colordict)

=> provide a DirectLabelColormap object with a given colormap instead of just setting the colormap dict directly & changing the mode
It needs to include a color for None, otherwise all works as expected.

What doesn't work as simple as before is updating a single item only. If a user clicks on a label, we used to update the just that value in the dictionary. We can still do this by calling:

self._annotations_layer.colormap.color_dict[label] = np.array(color, dtype=np.float32)

But that does not show up in the interface afterwards. We see that the DirectLabelColormap object of the layer gets changed, but the viewer does not display this.

What does work is providing a full new DirectLabelColormap:

        colordict = self._annotations_layer.colormap.color_dict
        colordict[label] = color
        self._annotations_layer.colormap = DirectLabelColormap(color_dict=colordict)

This then updates the colors of the annotation layer. But the performance already looks somewhat slow on our tiny test dataset

@jluethi
Copy link
Collaborator Author

jluethi commented Feb 27, 2024

What we tried that did not work:

  1. Calling self._annotations_layer.refresh()
  2. Trying to reset the cache of the label layer (it just emits a new deprecation warning): self._annotations_layer._cached_labels = None
  3. Triggering a colormap event: self._annotations_layer.events.colormap()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant