Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit caca5d0

Browse filesBrowse files
committed
Add layer number/type validation
1 parent 081d4b6 commit caca5d0
Copy full SHA for caca5d0

File tree

Expand file treeCollapse file tree

5 files changed

+51
-8
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+51
-8
lines changed

‎src/napari_matplotlib/base.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/base.py
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from qtpy.QtGui import QIcon
1111
from qtpy.QtWidgets import QVBoxLayout, QWidget
1212

13+
from .util import Interval
14+
1315
mpl.rc("axes", edgecolor="white")
1416
mpl.rc("axes", facecolor="#262930")
1517
mpl.rc("axes", labelcolor="white")
@@ -65,6 +67,11 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
6567

6668
self.setup_callbacks()
6769

70+
# Accept any number of input layers by default
71+
n_layers_input = Interval(None, None)
72+
# Accept any type of input layer by default
73+
input_layer_types = (napari.layers.Layer,)
74+
6875
@property
6976
def n_selected_layers(self) -> int:
7077
"""
@@ -104,10 +111,10 @@ def _draw(self) -> None:
104111
figure if so.
105112
"""
106113
self.clear()
107-
if self.n_selected_layers != self.n_layers_input:
108-
self.canvas.draw()
109-
return
110-
self.draw()
114+
if self.n_selected_layers in self.n_layers_input and all(
115+
isinstance(layer, self.input_layer_types) for layer in self.layers
116+
):
117+
self.draw()
111118
self.canvas.draw()
112119

113120
def clear(self) -> None:

‎src/napari_matplotlib/histogram.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/histogram.py
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import napari
88

9+
from .util import Interval
10+
911
_COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}
1012

1113

@@ -14,7 +16,8 @@ class HistogramWidget(NapariMPLWidget):
1416
Display a histogram of the currently selected layer.
1517
"""
1618

17-
n_layers_input = 1
19+
n_layers_input = Interval(1, 1)
20+
input_layer_types = (napari.layers.Image,)
1821

1922
def __init__(self, napari_viewer: napari.viewer.Viewer):
2023
super().__init__(napari_viewer)

‎src/napari_matplotlib/scatter.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/scatter.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from magicgui import magicgui
77

88
from .base import NapariMPLWidget
9+
from .util import Interval
910

1011
__all__ = ["ScatterWidget", "FeaturesScatterWidget"]
1112

@@ -84,7 +85,8 @@ class ScatterWidget(ScatterBaseWidget):
8485
of a scatter plot, to avoid too many scatter points.
8586
"""
8687

87-
n_layers_input = 2
88+
n_layers_input = Interval(2, 2)
89+
input_layer_types = (napari.layers.Image,)
8890

8991
def __init__(
9092
self,

‎src/napari_matplotlib/slice.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/slice.py
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import numpy as np
55
from qtpy.QtWidgets import QComboBox, QHBoxLayout, QLabel, QSpinBox
66

7-
from napari_matplotlib.base import NapariMPLWidget
7+
from .base import NapariMPLWidget
8+
from .util import Interval
89

910
__all__ = ["SliceWidget"]
1011

@@ -17,7 +18,8 @@ class SliceWidget(NapariMPLWidget):
1718
Plot a 1D slice along a given dimension.
1819
"""
1920

20-
n_layers_input = 1
21+
n_layers_input = Interval(1, 1)
22+
input_layer_types = (napari.layers.Image,)
2123

2224
def __init__(self, napari_viewer: napari.viewer.Viewer):
2325
# Setup figure/axes

‎src/napari_matplotlib/util.py

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Optional
2+
3+
4+
class Interval:
5+
def __init__(self, lower_bound: Optional[int], upper_bound: Optional[int]):
6+
"""
7+
Parameters
8+
----------
9+
lower_bound, upper_bound:
10+
Bounds. Use `None` to specify an open bound.
11+
"""
12+
if (
13+
lower_bound is not None
14+
and upper_bound is not None
15+
and lower_bound > upper_bound
16+
):
17+
raise ValueError("lower_bound must be <= upper_bound")
18+
19+
self.lower = lower_bound
20+
self.upper = upper_bound
21+
22+
def __contains__(self, val):
23+
if not isinstance(val, int):
24+
return NotImplemented
25+
if self.lower is not None and val < self.lower:
26+
return False
27+
if self.upper is not None and val > self.upper:
28+
return False
29+
return True

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.