Skip to content

Commit

Permalink
Remove deprecated code from Labels layer (napari#6641)
Browse files Browse the repository at this point in the history
# References and relevant issues

This completes the deprecations started in napari#6542. All it does is remove
the things we warned we would remove. 😂

---------

Co-authored-by: Grzegorz Bokota <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 29, 2024
1 parent 35c7987 commit 6065309
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 236 deletions.
13 changes: 8 additions & 5 deletions napari/benchmarks/benchmark_labels_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import napari
from napari.components.dims import Dims
from napari.layers import Labels
from napari.utils.colormaps import DirectLabelColormap

from .utils import Skip, labeled_particles

Expand Down Expand Up @@ -95,12 +96,13 @@ def setup(self, n, brush_size, color_mode, contour):
(n, n), dtype=np.int32, n=int(np.log2(n) ** 2), seed=1
)

colors = None
self.layer = Labels(self.data)

if color_mode == 'direct':
random_label_ids = np.random.randint(64, size=50)
colors = {i + 1: np.random.random(4) for i in random_label_ids}

self.layer = Labels(self.data, color=colors)
colors[None] = np.array([0, 0, 0, 0.3])
self.layer.colormap = DirectLabelColormap(color_dict=colors)

self.layer.brush_size = brush_size
self.layer.contour = contour
Expand Down Expand Up @@ -132,9 +134,10 @@ def setup(self, n, dtype):
random_label_ids = np.random.randint(
low=max(-10000, info.min), high=min(10000, info.max), size=20
)
colors = {i + 1: np.random.random(4) for i in random_label_ids}
colors[None] = np.array([0, 0, 0, 0.3])
self.layer = Labels(
self.data,
color={i + 1: np.random.random(4) for i in random_label_ids},
self.data, colormap=DirectLabelColormap(color_dict=colors)
)
self.layer._raw_to_displayed(
self.layer._slice.image.raw, (slice(0, n), slice(0, n))
Expand Down
3 changes: 2 additions & 1 deletion napari/benchmarks/benchmark_qt_viewer_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import napari
from napari.components.viewer_model import ViewerModel
from napari.qt import QtViewer
from napari.utils.colormaps import DirectLabelColormap

from .utils import Skip

Expand Down Expand Up @@ -150,7 +151,7 @@ def setup(self, radius, dtype, label_mode):
)
colors[None] = 'yellow'
colors[0] = 'transparent'
self.layer.color = colors
self.layer.colormap = DirectLabelColormap(color_dict=colors)
self.qt_viewr.show()

@staticmethod
Expand Down
10 changes: 0 additions & 10 deletions napari/components/_tests/test_viewer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,6 @@ def test_add_labels():
assert viewer.dims.ndim == 2


def test_add_labels_warnings():
"""Test adding labels image."""
viewer = ViewerModel()
np.random.seed(0)
with pytest.warns(
FutureWarning, match='Setting Labels.num_colors is deprecated since'
):
viewer.add_labels(np.zeros((10, 15), dtype=np.uint8), num_colors=20)


def test_add_points():
"""Test adding points."""
viewer = ViewerModel()
Expand Down
80 changes: 13 additions & 67 deletions napari/layers/labels/_tests/test_labels.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import itertools
import time
from collections import defaultdict
from dataclasses import dataclass
from tempfile import TemporaryDirectory
from typing import List
Expand All @@ -12,7 +13,6 @@
import xarray as xr
import zarr
from numpy.core.numerictypes import issubdtype
from numpy.testing import assert_array_almost_equal, assert_raises
from skimage import data as sk_data

from napari._tests.utils import check_layer_world_data_extent
Expand Down Expand Up @@ -261,56 +261,6 @@ def test_blending():


@pytest.mark.filterwarnings('ignore:.*seed is deprecated.*')
def test_seed():
"""Test setting seed."""
np.random.seed(0)
data = np.random.randint(20, size=(10, 15))
layer = Labels(data)
assert layer.seed == 0.5

layer.seed = 0.9
assert layer.seed == 0.9

layer = Labels(data, seed=0.7)
assert layer.seed == 0.7

# ensure setting seed updates the random colormap
mapped_07 = layer.colormap.map(layer.data)
layer.seed = 0.4
mapped_04 = layer.colormap.map(layer.data)
assert_raises(
AssertionError, assert_array_almost_equal, mapped_07, mapped_04
)


def test_num_colors():
"""Test setting number of colors in colormap with deprecated API."""
np.random.seed(0)
data = np.random.randint(20, size=(10, 15))
layer = Labels(data)

with pytest.warns(FutureWarning, match='num_colors is deprecated'):
assert layer.num_colors == 50

with pytest.warns(FutureWarning, match='num_colors is deprecated'):
layer.num_colors = 80

assert len(layer.colormap) == 80

with pytest.warns(FutureWarning, match='num_colors is deprecated'):
layer = Labels(data, num_colors=60)

assert len(layer.colormap) == 60

with pytest.raises(ValueError, match=r'.*Only up to 2\*\*16=65535 colors'):
with pytest.warns(FutureWarning, match='num_colors is deprecated'):
layer.num_colors = 2**17

with pytest.raises(ValueError, match=r'.*Only up to 2\*\*16=65535 colors'):
with pytest.warns(FutureWarning, match='num_colors is deprecated'):
Labels(data, num_colors=2**17)


def test_properties():
"""Test adding labels with properties."""
np.random.seed(0)
Expand Down Expand Up @@ -447,11 +397,13 @@ def test_custom_color_dict():
"""Test custom color dict."""
np.random.seed(0)
data = np.random.randint(20, size=(10, 15))
with pytest.warns(FutureWarning, match='Labels.color is deprecated'):
layer = Labels(
data,
color={2: 'white', 4: 'red', 8: 'blue', 16: 'red', 32: 'blue'},
cmap = DirectLabelColormap(
color_dict=defaultdict(
lambda: 'black',
{2: 'white', 4: 'red', 8: 'blue', 16: 'red', 32: 'blue'},
)
)
layer = Labels(data, colormap=cmap)

# test with custom color dict
assert isinstance(layer.get_color(2), np.ndarray)
Expand All @@ -462,8 +414,6 @@ def test_custom_color_dict():

# test disable custom color dict
# should not initialize as white since we are using random.seed
with pytest.warns(FutureWarning, match='Labels.color_mode is deprecated'):
layer.color_mode = 'auto'
assert not (layer.get_color(1) == np.array([1.0, 1.0, 1.0, 1.0])).all()


Expand Down Expand Up @@ -1511,30 +1461,26 @@ def test_is_default_color():
# setting color to default colors doesn't update color mode
layer.colormap = DirectLabelColormap(color_dict=current_color)
assert isinstance(layer.colormap, CyclicLabelColormap)
with pytest.warns(FutureWarning, match='Labels.color_mode is deprecated'):
assert layer.color_mode == 'auto'

# new colors are not default
new_color = {0: 'white', 1: 'red', 3: 'green', None: 'blue'}
assert not layer._is_default_colors(new_color)
# setting the color with non-default colors updates color mode
layer.colormap = DirectLabelColormap(color_dict=new_color)
assert isinstance(layer.colormap, DirectLabelColormap)
with pytest.warns(FutureWarning, match='Labels.color_mode is deprecated'):
assert layer.color_mode == 'direct'


def test_large_labels_direct_color():
"""Make sure direct color works with large label ranges"""
pytest.importorskip('numba')
data = np.array([[0, 1], [2**16, 2**20]], dtype=np.uint32)
colors = {1: 'white', 2**16: 'green', 2**20: 'magenta'}
layer = Labels(data)
with pytest.warns(FutureWarning, match='Labels.color is deprecated'):
layer.color = colors

with pytest.warns(FutureWarning, match='Labels.color_mode is deprecated'):
assert layer.color_mode == 'direct'
layer = Labels(
data,
colormap=DirectLabelColormap(
color_dict=defaultdict(lambda: 'black', colors)
),
)
np.testing.assert_allclose(layer.get_color(2**20), [1.0, 0.0, 1.0, 1.0])


Expand Down
Loading

0 comments on commit 6065309

Please sign in to comment.