File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010from qtpy .QtGui import QIcon
1111from qtpy .QtWidgets import QVBoxLayout , QWidget
1212
13+ from .util import Interval
14+
1315mpl .rc ("axes" , edgecolor = "white" )
1416mpl .rc ("axes" , facecolor = "#262930" )
1517mpl .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 :
Original file line number Diff line number Diff line change 66
77import 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 )
Original file line number Diff line number Diff line change 66from magicgui import magicgui
77
88from .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 ,
Original file line number Diff line number Diff line change 44import numpy as np
55from 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments