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

Label layers do not support uint64 in combination colormaps anymore with 0.4.18 #6077

Closed
thorstenwagner opened this issue Jul 21, 2023 · 3 comments
Labels
bug Something isn't working priority-high High priority issue

Comments

@thorstenwagner
Copy link

thorstenwagner commented Jul 21, 2023

🐛 Bug

This code just runs fine in napari 0.4.17:

import napari
import numpy as np
from vispy.color import Color

colors = [
    "#ff7f0e",
    "#1f77b4",
    "#2ca02c",
    "#d62728",
    "#9467bd",
    "#8c564b",
    "#e377c2",
    "#7f7f7f",
    "#bcbd22",
    "#17becf",
    "#ccebc5",
    "#ffed6f",
]
 
viewer = napari.Viewer()
img = np.arange(0,100).reshape(10,10).astype(np.uint64)

cmap = [Color(hex_name).RGBA.astype("float") / 255 for hex_name in colors]
cmap_dict = { 
                int(p): cmap[int(p) % len(cmap)]
                for p in np.unique(img)
            }
viewer.add_labels(
                        img,  
                        color=cmap_dict,  
                        name="whatever",
                    )
napari.run()

However, it crashes with napari 0.4.18

Traceback (most recent call last):
  File "/mnt/data/twagner/Projects/TomoTwin/results/202205_GeneralizationTomo/out_loss/napari_crash.py", line 28, in <module>
    viewer.add_labels(
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/components/viewer_model.py", line 4, in add_labels
    import itertools
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/labels/labels.py", line 340, in __init__
    self.color = color
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/labels/labels.py", line 556, in color
    self.color_mode = color_mode
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/labels/labels.py", line 696, in color_mode
    self.refresh()
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/base/base.py", line 1264, in refresh
    self.set_view_slice()
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/base/base.py", line 1029, in set_view_slice
    self._set_view_slice()
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/image.py", line 834, in _set_view_slice
    self._load_slice(data)
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/image.py", line 862, in _load_slice
    self._on_data_loaded(data, sync=True)
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/image.py", line 886, in _on_data_loaded
    if not self._slice.on_loaded(data):
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/_image_slice.py", line 144, in on_loaded
    self._set_raw_images(data.image, data.thumbnail_source)
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/_image_slice.py", line 102, in _set_raw_images
    self.image.raw = image
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/_image_view.py", line 78, in raw
    self._view = self.image_converter(raw_image)
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/image/image.py", line 1120, in _raw_to_displayed
    return self.obj()._raw_to_displayed(*args, **kwarg)
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/labels/labels.py", line 999, in _raw_to_displayed
    mapped_labels = self._map_labels_to_colors(labels_to_map)
  File "/opt/user_software/miniconda3_envs/napari-tomotwin/lib/python3.10/site-packages/napari/layers/labels/labels.py", line 1038, in _map_labels_to_colors
    for label_id in range(min_label_id, max_label_id + 1)
TypeError: 'numpy.float64' object cannot be interpreted as an integer

Was there a breaking change in 0.4.18?

Best,
Thorsten

@jni
Copy link
Member

jni commented Jul 21, 2023

This was definitely not intentional @thorstenwagner, thank you for reporting. The good news is the code runs fine in #3308 which we are making a big push on, so hopefully we can get a 2-for-1 fix there (or n-for-1). Sorry about that and we hope to have an update soon! 🙏

@jni jni closed this as completed in e0c0c8e Jul 22, 2023
@thorstenwagner
Copy link
Author

Thanks for fixing that :-)

@jni
Copy link
Member

jni commented Jul 31, 2023

No worries! Do watch out for #6084, although we intend to fix that before 0.5.0. 🤞

Czaki pushed a commit that referenced this issue Oct 17, 2023
The pull request will move the current label color calculation onto the
GPU.

This is the result of dozens of hours of hacking between @perlman,
@jni, and @brisvag. It moves the mapping from label space to 0-1
(which can then be colormapped) to the shader.

It also adds hashing in OpenGL for direct color mode, which avoids
many of the collisions we were seeing in that mode when mapping
labels to 0-1 and then using a custom colormap.

Closes #204 (count those digits!!!!)
Closes #4384 (which you'll see has been hit by many people � it's a big one)
Closes #4864
Closes #4942
Closes #6077
Closes #6010

This converts labels to np.float32, so labels above `2**23` can be aliased
together. But this limitation seems preferable to existing issues.

- [X] Implement shader for 3D label layers (ISO categorical)
- [X] Implement shader for 2D label layers
- [X] Fix the CPU-based color computation (for paint color preview &
layer preview)
- [X] 3D iso categorical view
- [x] Test function of directly mapped colors

- [ ] Address uint64 labels. Labels are currently converted to float32
- [ ] Factor `seed` into a `uniform`

- [X] This change requires a documentation update

- [X] Additional tests added to confirm consistent coloring between GPU
& CPU
- [X] Manual testing of the various examples (`examples/**labels*.py`)

---------

Co-authored-by: Juan Nunez-Iglesias <juan.nunez-iglesias@monash.edu>
Co-authored-by: Lorenzo Gaifas <brisvag@gmail.com>
Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority-high High priority issue
Projects
None yet
Development

No branches or pull requests

3 participants