-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_data.py
More file actions
219 lines (166 loc) · 6.77 KB
/
_data.py
File metadata and controls
219 lines (166 loc) · 6.77 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
from typing import *
import numpy as np
import pygfx
from ._base import (
GraphicFeatureIndexable,
cleanup_slice,
FeatureEvent,
to_gpu_supported_dtype,
cleanup_array_slice,
)
class PointsDataFeature(GraphicFeatureIndexable):
"""
Access to the vertex buffer data shown in the graphic.
Supports fancy indexing if the data array also supports it.
"""
def __init__(self, parent, data: Any, collection_index: int = None):
data = self._fix_data(data, parent)
super().__init__(parent, data, collection_index=collection_index)
@property
def buffer(self) -> pygfx.Buffer:
return self._parent.world_object.geometry.positions
def __getitem__(self, item):
return self.buffer.data[item]
def _fix_data(self, data, parent):
graphic_type = parent.__class__.__name__
data = to_gpu_supported_dtype(data)
if data.ndim == 1:
# for scatter if we receive just 3 points in a 1d array, treat it as just a single datapoint
# this is different from fix_data for LineGraphic since there we assume that a 1d array
# is just y-values
if graphic_type == "ScatterGraphic":
data = np.array([data])
elif graphic_type == "LineGraphic":
data = np.dstack([np.arange(data.size, dtype=data.dtype), data])[0]
if data.shape[1] != 3:
if data.shape[1] != 2:
raise ValueError(f"Must pass 1D, 2D or 3D data to {graphic_type}")
# zeros for z
zs = np.zeros(data.shape[0], dtype=data.dtype)
data = np.dstack([data[:, 0], data[:, 1], zs])[0]
return data
def __setitem__(self, key, value):
if isinstance(key, np.ndarray):
# make sure 1D array of int or boolean
key = cleanup_array_slice(key, self._upper_bound)
# put data into right shape if they're only indexing datapoints
if isinstance(key, (slice, int, np.ndarray, np.integer)):
value = self._fix_data(value, self._parent)
# otherwise assume that they have the right shape
# numpy will throw errors if it can't broadcast
self.buffer.data[key] = value
self._update_range(key)
# avoid creating dicts constantly if there are no events to handle
if len(self._event_handlers) > 0:
self._feature_changed(key, value)
def _update_range(self, key):
self._update_range_indices(key)
def _feature_changed(self, key, new_data):
if key is not None:
key = cleanup_slice(key, self._upper_bound)
if isinstance(key, (int, np.integer)):
indices = [key]
elif isinstance(key, slice):
indices = range(key.start, key.stop, key.step)
elif isinstance(key, np.ndarray):
indices = key
elif key is None:
indices = None
pick_info = {
"index": indices,
"collection-index": self._collection_index,
"world_object": self._parent.world_object,
"new_data": new_data,
}
event_data = FeatureEvent(type="data", pick_info=pick_info)
self._call_event_handlers(event_data)
def __repr__(self) -> str:
s = f"PointsDataFeature for {self._parent}, call `<graphic>.data()` to get values"
return s
class ImageDataFeature(GraphicFeatureIndexable):
"""
Access to the Texture buffer shown in an ImageGraphic.
"""
def __init__(self, parent, data: Any):
if data.ndim not in (2, 3):
raise ValueError(
"`data.ndim` must be 2 or 3, ImageGraphic data shape must be "
"``[x_dim, y_dim]`` or ``[x_dim, y_dim, rgb]``"
)
super().__init__(parent, data)
@property
def buffer(self) -> pygfx.Texture:
"""Texture buffer for the image data"""
return self._parent.world_object.geometry.grid
def update_gpu(self):
"""Update the GPU with the buffer"""
self._update_range(None)
def __call__(self, *args, **kwargs):
return self.buffer.data
def __getitem__(self, item):
return self.buffer.data[item]
def __setitem__(self, key, value):
# make sure float32
value = to_gpu_supported_dtype(value)
self.buffer.data[key] = value
self._update_range(key)
# avoid creating dicts constantly if there are no events to handle
if len(self._event_handlers) > 0:
self._feature_changed(key, value)
def _update_range(self, key):
self.buffer.update_range((0, 0, 0), size=self.buffer.size)
def _feature_changed(self, key, new_data):
if key is not None:
key = cleanup_slice(key, self._upper_bound)
if isinstance(key, int):
indices = [key]
elif isinstance(key, slice):
indices = range(key.start, key.stop, key.step)
elif key is None:
indices = None
pick_info = {
"index": indices,
"world_object": self._parent.world_object,
"new_data": new_data,
}
event_data = FeatureEvent(type="data", pick_info=pick_info)
self._call_event_handlers(event_data)
def __repr__(self) -> str:
s = f"ImageDataFeature for {self._parent}, call `<graphic>.data()` to get values"
return s
class HeatmapDataFeature(ImageDataFeature):
@property
def buffer(self) -> List[pygfx.Texture]:
"""list of Texture buffer for the image data"""
return [img.geometry.grid for img in self._parent.world_object.children]
def __getitem__(self, item):
return self._data[item]
def __call__(self, *args, **kwargs):
return self._data
def __setitem__(self, key, value):
# make sure supported type, not float64 etc.
value = to_gpu_supported_dtype(value)
self._data[key] = value
self._update_range(key)
# avoid creating dicts constantly if there are no events to handle
if len(self._event_handlers) > 0:
self._feature_changed(key, value)
def _update_range(self, key):
for buffer in self.buffer:
buffer.update_range((0, 0, 0), size=buffer.size)
def _feature_changed(self, key, new_data):
if key is not None:
key = cleanup_slice(key, self._upper_bound)
if isinstance(key, int):
indices = [key]
elif isinstance(key, slice):
indices = range(key.start, key.stop, key.step)
elif key is None:
indices = None
pick_info = {
"index": indices,
"world_object": self._parent.world_object,
"new_data": new_data,
}
event_data = FeatureEvent(type="data", pick_info=pick_info)
self._call_event_handlers(event_data)