-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_collection_base.py
More file actions
367 lines (254 loc) · 10.2 KB
/
_collection_base.py
File metadata and controls
367 lines (254 loc) · 10.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from contextlib import suppress
from typing import Any
import numpy as np
from ._base import Graphic
class CollectionProperties:
"""
Properties common to all Graphic Collections
Allows getting and setting the common properties of the individual graphics in the collection
"""
def _set_feature(self, feature, values):
if not len(values) == len(self):
raise IndexError
for g, v in zip(self, values):
setattr(g, feature, v)
@property
def names(self) -> np.ndarray[str | None]:
"""get or set the name of the individual graphics in the collection"""
return np.asarray([g.name for g in self])
@names.setter
def names(self, values: np.ndarray[str] | list[str]):
self._set_feature("name", values)
@property
def metadatas(self) -> np.ndarray[str | None]:
"""get or set the metadata of the individual graphics in the collection"""
return np.asarray([g.metadata for g in self])
@metadatas.setter
def metadatas(self, values: np.ndarray[str] | list[str]):
self._set_feature("metadata", values)
@property
def offsets(self) -> np.ndarray:
"""get or set the offset of the individual graphics in the collection"""
return np.stack([g.offset for g in self])
@offsets.setter
def offsets(self, values: np.ndarray | list[np.ndarray]):
self._set_feature("offset", values)
@property
def rotations(self) -> np.ndarray:
"""get or set the rotation of the individual graphics in the collection"""
return np.stack([g.rotation for g in self])
@rotations.setter
def rotations(self, values: np.ndarray | list[np.ndarray]):
self._set_feature("rotation", values)
# TODO: how to work with deleted feature in a collection
@property
def visibles(self) -> np.ndarray[bool]:
"""get or set the offsets of the individual graphics in the collection"""
return np.asarray([g.visible for g in self])
@visibles.setter
def visibles(self, values: np.ndarray[bool] | list[bool]):
self._set_feature("visible", values)
class CollectionIndexer(CollectionProperties):
"""Collection Indexer"""
def __init__(self, selection: np.ndarray[Graphic], features: set[str]):
"""
Parameters
----------
selection: np.ndarray of Graphics
array of the selected Graphics from the parent GraphicCollection based on the ``selection_indices``
"""
if isinstance(selection, Graphic):
selection = np.asarray([selection])
self._selection = selection
self.features = features
@property
def graphics(self) -> np.ndarray[Graphic]:
"""Returns an array of the selected graphics"""
return tuple(self._selection)
def add_event_handler(self, *args):
"""
Register an event handler.
Parameters
----------
callback: callable, the first argument
Event handler, must accept a single event argument
*types: list of strings
A list of event types, ex: "click", "data", "colors", "pointer_down"
For the available renderer event types, see
https://jupyter-rfb.readthedocs.io/en/stable/events.html
All feature support events, i.e. ``graphic.features`` will give a set of
all features that are evented
Can also be used as a decorator.
Example
-------
.. code-block:: py
def my_handler(event):
print(event)
graphic.add_event_handler(my_handler, "pointer_up", "pointer_down")
Decorator usage example:
.. code-block:: py
@graphic.add_event_handler("click")
def my_handler(event):
print(event)
"""
decorating = not callable(args[0])
types = args if decorating else args[1:]
if decorating:
def decorator(_callback):
for g in self:
g.add_event_handler(_callback, *types)
return _callback
return decorator
for g in self:
g.add_event_handler(*args)
def remove_event_handler(self, callback, *types):
for g in self:
g.remove_event_handler(callback, *types)
def clear_event_handlers(self):
for g in self:
g.clear_event_handlers()
def __getitem__(self, item):
return self.graphics[item]
def __len__(self):
return len(self._selection)
def __iter__(self):
self._iter = iter(range(len(self)))
return self
def __next__(self) -> Graphic:
index = next(self._iter)
return self.graphics[index]
def __repr__(self):
return (
f"{self.__class__.__name__} @ {hex(id(self))}\n"
f"Selection of <{len(self._selection)}> {self._selection[0].__class__.__name__}"
)
class GraphicCollection(Graphic, CollectionProperties):
"""Graphic Collection base class"""
_child_type: type
_indexer: type
# tooltips will come from the child graphics
_fpl_support_tooltip = False
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls._features = cls._child_type._features
def __init__(self, name: str = None, metadata: Any = None, **kwargs):
super().__init__(name=name, metadata=metadata, **kwargs)
# list of mem locations of the graphics
self._graphics: list[Graphic] = list()
self._graphics_changed: bool = True
self._iter = None
@property
def graphics(self) -> np.ndarray[Graphic]:
"""The Graphics within this collection."""
return np.asarray(self._graphics)
def add_graphic(self, graphic: Graphic):
"""
Add a graphic to the collection.
Parameters
----------
graphic: Graphic
graphic to add, must be a real ``Graphic`` not a proxy
"""
if not type(graphic) == self._child_type:
raise TypeError(
f"Can only add graphics of the same type to a collection.\n"
f"You can only add {self._child_type.__name__} to a {self.__class__.__name__}, "
f"you are trying to add a {graphic.__class__.__name__}."
)
self._graphics.append(graphic)
self.world_object.add(graphic.world_object)
self._graphics_changed = True
def remove_graphic(self, graphic: Graphic):
"""
Remove a graphic from the collection.
Note: Only removes the graphic from the collection. Does not remove
the graphic from the scene, and does not delete the graphic.
Parameters
----------
graphic: Graphic
graphic to remove
"""
self._graphics.remove(graphic)
self.world_object.remove(graphic.world_object)
self._graphics_changed = True
def add_event_handler(self, *args):
"""
Register an event handler.
Parameters
----------
callback: callable, the first argument
Event handler, must accept a single event argument
*types: list of strings
A list of event types, ex: "click", "data", "colors", "pointer_down"
For the available renderer event types, see
https://jupyter-rfb.readthedocs.io/en/stable/events.html
All feature support events, i.e. ``graphic.features`` will give a set of
all features that are evented
Can also be used as a decorator.
Example
-------
.. code-block:: py
def my_handler(event):
print(event)
graphic.add_event_handler(my_handler, "pointer_up", "pointer_down")
Decorator usage example:
.. code-block:: py
@graphic.add_event_handler("click")
def my_handler(event):
print(event)
"""
return self[:].add_event_handler(*args)
def remove_event_handler(self, callback, *types):
"""remove an event handler"""
self[:].remove_event_handler(callback, *types)
def clear_event_handlers(self):
self[:].clear_event_handlers()
def _fpl_add_plot_area_hook(self, plot_area):
super()._fpl_add_plot_area_hook(plot_area)
for g in self:
g._fpl_add_plot_area_hook(plot_area)
def _fpl_prepare_del(self):
"""
Cleans up the graphic in preparation for __del__(), such as removing event handlers from
plot renderer, feature event handlers, etc.
Optionally implemented in subclasses
"""
# clear any attached event handlers and animation functions
self.world_object._event_handlers.clear()
self.world_object.clear()
for g in self:
g._fpl_prepare_del()
def __getitem__(self, key) -> CollectionIndexer:
if np.issubdtype(type(key), np.integer):
return self.graphics[key]
return self._indexer(selection=self.graphics[key], features=self._features)
def __len__(self):
return len(self._graphics)
def __iter__(self):
self._iter = iter(range(len(self)))
return self
def __next__(self) -> Graphic:
index = next(self._iter)
return self._graphics[index]
def __repr__(self):
rval = super().__repr__()
return f"{rval}\nCollection of <{len(self._graphics)}> Graphics"
class CollectionFeature:
"""Collection Feature"""
def __init__(self, selection: np.ndarray[Graphic], feature: str):
"""
selection: list of Graphics
a list of the selected Graphics from the parent GraphicCollection based on the ``selection_indices``
feature: str
feature of Graphics in the GraphicCollection being indexed
"""
self._selection = selection
self._feature = feature
self._feature_instances = [getattr(g, feature) for g in self._selection]
def __getitem__(self, item):
return np.stack([fi[item] for fi in self._feature_instances])
def __setitem__(self, key, value):
for fi in self._feature_instances:
fi[key] = value
def __repr__(self):
return f"Collection feature for: <{self._feature}>"