-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathhistogram_lut.py
More file actions
315 lines (237 loc) · 10.1 KB
/
histogram_lut.py
File metadata and controls
315 lines (237 loc) · 10.1 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import weakref
import numpy as np
from pygfx import Group
from ..graphics import LineGraphic, ImageGraphic, TextGraphic
from ..graphics._base import Graphic
from ..graphics.selectors import LinearRegionSelector
# TODO: This is a widget, we can think about a BaseWidget class later if necessary
class HistogramLUT(Graphic):
def __init__(
self,
data: np.ndarray,
image_graphic: ImageGraphic,
nbins: int = 100,
flank_divisor: float = 5.0,
**kwargs,
):
"""
Parameters
----------
data
image_graphic
nbins
flank_divisor: float, default 5.0
set `np.inf` for no flanks
kwargs
"""
super().__init__(**kwargs)
self._nbins = nbins
self._flank_divisor = flank_divisor
self._image_graphic = image_graphic
self._data = weakref.proxy(data)
self._scale_factor: float = 1.0
hist, edges, hist_scaled, edges_flanked = self._calculate_histogram(data)
line_data = np.column_stack([hist_scaled, edges_flanked])
self._histogram_line = LineGraphic(line_data)
bounds = (edges[0], edges[-1])
limits = (edges_flanked[0], edges_flanked[-1])
size = 120 # since it's scaled to 100
origin = (hist_scaled.max() / 2, 0)
self._linear_region_selector = LinearRegionSelector(
bounds=bounds,
limits=limits,
size=size,
origin=origin,
axis="y",
edge_thickness=8,
)
# there will be a small difference with the histogram edges so this makes them both line up exactly
self._linear_region_selector.selection = (
image_graphic.cmap.vmin,
image_graphic.cmap.vmax,
)
self._vmin = self.image_graphic.cmap.vmin
self._vmax = self.image_graphic.cmap.vmax
vmin_str, vmax_str = self._get_vmin_vmax_str()
self._text_vmin = TextGraphic(
text=vmin_str,
size=16,
position=(0, 0),
anchor="top-left",
outline_color="black",
outline_thickness=1,
)
self._text_vmin.world_object.material.pick_write = False
self._text_vmax = TextGraphic(
text=vmax_str,
size=16,
position=(0, 0),
anchor="bottom-left",
outline_color="black",
outline_thickness=1,
)
self._text_vmax.world_object.material.pick_write = False
widget_wo = Group()
widget_wo.add(
self._histogram_line.world_object,
self._linear_region_selector.world_object,
self._text_vmin.world_object,
self._text_vmax.world_object,
)
self._set_world_object(widget_wo)
self.world_object.local.scale_x *= -1
self._text_vmin.position_x = -120
self._text_vmin.position_y = self._linear_region_selector.selection()[0]
self._text_vmax.position_x = -120
self._text_vmax.position_y = self._linear_region_selector.selection()[1]
self._linear_region_selector.selection.add_event_handler(
self._linear_region_handler
)
self.image_graphic.cmap.add_event_handler(self._image_cmap_handler)
def _get_vmin_vmax_str(self) -> tuple[str, str]:
if self.vmin < 0.001 or self.vmin > 99_999:
vmin_str = f"{self.vmin:.2e}"
else:
vmin_str = f"{self.vmin:.2f}"
if self.vmax < 0.001 or self.vmax > 99_999:
vmax_str = f"{self.vmax:.2e}"
else:
vmax_str = f"{self.vmax:.2f}"
return vmin_str, vmax_str
def _fpl_add_plot_area_hook(self, plot_area):
self._plot_area = plot_area
self._linear_region_selector._fpl_add_plot_area_hook(plot_area)
self._histogram_line._fpl_add_plot_area_hook(plot_area)
self._plot_area.auto_scale()
def _calculate_histogram(self, data):
if data.ndim > 2:
# subsample to max of 500 x 100 x 100,
# np.histogram takes ~30ms with this size on a 8 core Ryzen laptop
# dim0 is usually time, allow max of 500 timepoints
ss0 = max(1, int(data.shape[0] / 500)) # max to prevent step = 0
# allow max of 100 for x and y if ndim > 2
ss1 = max(1, int(data.shape[1] / 100))
ss2 = max(1, int(data.shape[2] / 100))
data_ss = data[::ss0, ::ss1, ::ss2]
hist, edges = np.histogram(data_ss, bins=self._nbins)
else:
# allow max of 1000 x 1000
# this takes ~4ms on a 8 core Ryzen laptop
ss0 = max(1, int(data.shape[0] / 1_000))
ss1 = max(1, int(data.shape[1] / 1_000))
data_ss = data[::ss0, ::ss1]
hist, edges = np.histogram(data_ss, bins=self._nbins)
# used if data ptp <= 10 because event things get weird
# with tiny world objects due to floating point error
# so if ptp <= 10, scale up by a factor
self._scale_factor: int = max(1, 100 * int(10 / data_ss.ptp()))
edges = edges * self._scale_factor
bin_width = edges[1] - edges[0]
flank_nbins = int(self._nbins / self._flank_divisor)
flank_size = flank_nbins * bin_width
flank_left = np.arange(edges[0] - flank_size, edges[0], bin_width)
flank_right = np.arange(
edges[-1] + bin_width, edges[-1] + flank_size, bin_width
)
edges_flanked = np.concatenate((flank_left, edges, flank_right))
np.unique(np.diff(edges_flanked))
hist_flanked = np.concatenate(
(np.zeros(flank_nbins), hist, np.zeros(flank_nbins))
)
# scale 0-100 to make it easier to see
# float32 data can produce unnecessarily high values
hist_scaled = hist_flanked / (hist_flanked.max() / 100)
if edges_flanked.size > hist_scaled.size:
# we don't care about accuracy here so if it's off by 1-2 bins that's fine
edges_flanked = edges_flanked[: hist_scaled.size]
return hist, edges, hist_scaled, edges_flanked
def _linear_region_handler(self, ev):
# must use world coordinate values directly from selection()
# otherwise the linear region bounds jump to the closest bin edges
vmin, vmax = self._linear_region_selector.selection()
vmin, vmax = vmin / self._scale_factor, vmax / self._scale_factor
self.vmin, self.vmax = vmin, vmax
def _image_cmap_handler(self, ev):
self.vmin, self.vmax = ev.pick_info["vmin"], ev.pick_info["vmax"]
def _block_events(self, b: bool):
self.image_graphic.cmap.block_events(b)
self._linear_region_selector.selection.block_events(b)
@property
def vmin(self) -> float:
return self._vmin
@vmin.setter
def vmin(self, value: float):
self._block_events(True)
# must use world coordinate values directly from selection()
# otherwise the linear region bounds jump to the closest bin edges
self._linear_region_selector.selection = (
value * self._scale_factor,
self._linear_region_selector.selection()[1],
)
self.image_graphic.cmap.vmin = value
self._block_events(False)
self._vmin = value
vmin_str, vmax_str = self._get_vmin_vmax_str()
self._text_vmin.position_y = self._linear_region_selector.selection()[0]
self._text_vmin.text = vmin_str
@property
def vmax(self) -> float:
return self._vmax
@vmax.setter
def vmax(self, value: float):
self._block_events(True)
# must use world coordinate values directly from selection()
# otherwise the linear region bounds jump to the closest bin edges
self._linear_region_selector.selection = (
self._linear_region_selector.selection()[0],
value * self._scale_factor,
)
self.image_graphic.cmap.vmax = value
self._block_events(False)
self._vmax = value
vmin_str, vmax_str = self._get_vmin_vmax_str()
self._text_vmax.position_y = self._linear_region_selector.selection()[1]
self._text_vmax.text = vmax_str
def set_data(self, data, reset_vmin_vmax: bool = True):
hist, edges, hist_scaled, edges_flanked = self._calculate_histogram(data)
line_data = np.column_stack([hist_scaled, edges_flanked])
self._histogram_line.data = line_data
bounds = (edges[0], edges[-1])
limits = (edges_flanked[0], edges_flanked[-11])
origin = (hist_scaled.max() / 2, 0)
# self.linear_region.fill.world.position = (*origin, -2)
if reset_vmin_vmax:
# reset according to the new data
self._linear_region_selector.limits = limits
self._linear_region_selector.selection = bounds
else:
# don't change the current selection
self._block_events(True)
self._linear_region_selector.limits = limits
self._block_events(False)
self._data = weakref.proxy(data)
# reset plotarea dims
self._plot_area.auto_scale()
@property
def image_graphic(self) -> ImageGraphic:
return self._image_graphic
@image_graphic.setter
def image_graphic(self, graphic):
if not isinstance(graphic, ImageGraphic):
raise TypeError(
f"HistogramLUT can only use ImageGraphic types, you have passed: {type(graphic)}"
)
if self._image_graphic is not None:
# cleanup events from current image graphic
self._image_graphic.cmap.remove_event_handler(self._image_cmap_handler)
self._image_graphic = graphic
self.image_graphic.cmap.add_event_handler(self._image_cmap_handler)
def disconnect_image_graphic(self):
self._image_graphic.cmap.remove_event_handler(self._image_cmap_handler)
del self._image_graphic
# self._image_graphic = None
def _fpl_cleanup(self):
self._linear_region_selector._fpl_cleanup()
self._histogram_line._fpl_cleanup()
del self._histogram_line
del self._linear_region_selector