-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathscatter.py
250 lines (204 loc) · 7.25 KB
/
scatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from typing import Any, Dict, List, Optional, Tuple, Union
import napari
import numpy.typing as npt
from qtpy.QtWidgets import QComboBox, QLabel, QVBoxLayout, QWidget
from .base import NapariMPLWidget
from .util import Interval
__all__ = ["ScatterBaseWidget", "ScatterWidget",
"FeaturesScatterWidget", "FeaturesHistogramWidget"]
class ScatterBaseWidget(NapariMPLWidget):
"""
Base class for widgets that scatter two datasets against each other.
"""
# if the number of points is greater than this value,
# the scatter is plotted as a 2D histogram
_threshold_to_switch_to_histogram = 500
def __init__(
self,
napari_viewer: napari.viewer.Viewer,
parent: Optional[QWidget] = None,
):
super().__init__(napari_viewer, parent=parent)
self.add_single_axes()
def clear(self) -> None:
"""
Clear the axes.
"""
self.axes.clear()
def draw(self) -> None:
"""
Scatter the currently selected layers.
"""
x, y, x_axis_name, y_axis_name = self._get_data()
if x.size > self._threshold_to_switch_to_histogram:
self.axes.hist2d(
x.ravel(),
y.ravel(),
bins=100,
)
else:
self.axes.scatter(x, y, alpha=0.5)
self.axes.set_xlabel(x_axis_name)
self.axes.set_ylabel(y_axis_name)
def _get_data(self) -> Tuple[npt.NDArray[Any], npt.NDArray[Any], str, str]:
"""
Get the plot data.
This must be implemented on the subclass.
Returns
-------
x, y : np.ndarray
x and y values of plot data.
x_axis_name, y_axis_name : str
Label to display on the x/y axis
"""
raise NotImplementedError
class ScatterWidget(ScatterBaseWidget):
"""
Scatter data in two similarly shaped layers.
If there are more than 500 data points, a 2D histogram is displayed instead
of a scatter plot, to avoid too many scatter points.
"""
n_layers_input = Interval(2, 2)
input_layer_types = (napari.layers.Image,)
def _get_data(self) -> Tuple[npt.NDArray[Any], npt.NDArray[Any], str, str]:
"""
Get the plot data.
Returns
-------
data : List[np.ndarray]
List contains the in view slice of X and Y axis images.
x_axis_name : str
The title to display on the x axis
y_axis_name: str
The title to display on the y axis
"""
x = self.layers[0].data[self.current_z]
y = self.layers[1].data[self.current_z]
x_axis_name = self.layers[0].name
y_axis_name = self.layers[1].name
return x, y, x_axis_name, y_axis_name
class FeaturesScatterWidget(ScatterBaseWidget):
"""
Widget to scatter data stored in two layer feature attributes.
"""
n_layers_input = Interval(1, 1)
# All layers that have a .features attributes
input_layer_types = (
napari.layers.Labels,
napari.layers.Points,
napari.layers.Shapes,
napari.layers.Tracks,
napari.layers.Vectors,
)
def __init__(
self,
napari_viewer: napari.viewer.Viewer,
parent: Optional[QWidget] = None,
):
super().__init__(napari_viewer, parent=parent)
self.layout().addLayout(QVBoxLayout())
self._selectors: Dict[str, QComboBox] = {}
for dim in ["x", "y"]:
self._selectors[dim] = QComboBox()
# Re-draw when combo boxes are updated
self._selectors[dim].currentTextChanged.connect(self._draw)
self.layout().addWidget(QLabel(f"{dim}-axis:"))
self.layout().addWidget(self._selectors[dim])
self._update_layers(None)
@property
def x_axis_key(self) -> Union[str, None]:
"""
Key for the x-axis data.
"""
if self._selectors["x"].count() == 0:
return None
else:
return self._selectors["x"].currentText()
@x_axis_key.setter
def x_axis_key(self, key: str) -> None:
self._selectors["x"].setCurrentText(key)
self._draw()
@property
def y_axis_key(self) -> Union[str, None]:
"""
Key for the y-axis data.
"""
if self._selectors["y"].count() == 0:
return None
else:
return self._selectors["y"].currentText()
@y_axis_key.setter
def y_axis_key(self, key: str) -> None:
self._selectors["y"].setCurrentText(key)
self._draw()
def _get_valid_axis_keys(self) -> List[str]:
"""
Get the valid axis keys from the layer FeatureTable.
Returns
-------
axis_keys : List[str]
The valid axis keys in the FeatureTable. If the table is empty
or there isn't a table, returns an empty list.
"""
if len(self.layers) == 0 or not (hasattr(self.layers[0], "features")):
return []
else:
return self.layers[0].features.keys()
def _ready_to_scatter(self) -> bool:
"""
Return True if selected layer has a feature table we can scatter with,
and the two columns to be scatterd have been selected.
"""
if not hasattr(self.layers[0], "features"):
return False
feature_table = self.layers[0].features
valid_keys = self._get_valid_axis_keys()
return (
feature_table is not None
and len(feature_table) > 0
and self.x_axis_key in valid_keys
and self.y_axis_key in valid_keys
)
def draw(self) -> None:
"""
Scatter two features from the currently selected layer.
"""
if self._ready_to_scatter():
super().draw()
def _get_data(self) -> Tuple[npt.NDArray[Any], npt.NDArray[Any], str, str]:
"""
Get the plot data from the ``features`` attribute of the first
selected layer.
Returns
-------
data : List[np.ndarray]
List contains X and Y columns from the FeatureTable. Returns
an empty array if nothing to plot.
x_axis_name : str
The title to display on the x axis. Returns
an empty string if nothing to plot.
y_axis_name: str
The title to display on the y axis. Returns
an empty string if nothing to plot.
"""
feature_table = self.layers[0].features
x = feature_table[self.x_axis_key]
y = feature_table[self.y_axis_key]
x_axis_name = str(self.x_axis_key)
y_axis_name = str(self.y_axis_key)
return x, y, x_axis_name, y_axis_name
def on_update_layers(self) -> None:
"""
Called when the layer selection changes by ``self.update_layers()``.
"""
if hasattr(self, "_key_selection_widget"):
self._key_selection_widget.reset_choices()
# reset the axis keys
self._x_axis_key = None
self._y_axis_key = None
# Clear combobox
for dim in ["x", "y"]:
while self._selectors[dim].count() > 0:
self._selectors[dim].removeItem(0)
# Add keys for newly selected layer
self._selectors[dim].addItems(self._get_valid_axis_keys())