-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathline.py
More file actions
303 lines (235 loc) · 8.97 KB
/
line.py
File metadata and controls
303 lines (235 loc) · 8.97 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
from typing import *
import numpy as np
import pygfx
from ._positions_base import PositionsGraphic
from .selectors import LinearRegionSelector, LinearSelector, RectangleSelector
from ._features import Thickness, SizeSpace
class LineGraphic(PositionsGraphic):
_features = {"data", "colors", "cmap", "thickness", "size_space"}
def __init__(
self,
data: Any,
thickness: float = 2.0,
colors: str | np.ndarray | Iterable = "w",
uniform_color: bool = False,
alpha: float = 1.0,
cmap: str = None,
cmap_transform: np.ndarray | Iterable = None,
isolated_buffer: bool = True,
size_space: str = "screen",
**kwargs,
):
"""
Create a line Graphic, 2d or 3d
Parameters
----------
data: array-like
Line data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3]
thickness: float, optional, default 2.0
thickness of the line
colors: str, array, or iterable, default "w"
specify colors as a single human-readable string, a single RGBA array,
or an iterable of strings or RGBA arrays
uniform_color: bool, default ``False``
if True, uses a uniform buffer for the line color,
basically saves GPU VRAM when the entire line has a single color
alpha: float, optional, default 1.0
alpha value for the colors
cmap: str, optional
apply a colormap to the line instead of assigning colors manually, this
overrides any argument passed to "colors"
cmap_transform: 1D array-like of numerical values, optional
if provided, these values are used to map the colors from the cmap
size_space: str, default "screen"
coordinate space in which the size is expressed ("screen", "world", "model")
**kwargs
passed to Graphic
"""
super().__init__(
data=data,
colors=colors,
uniform_color=uniform_color,
alpha=alpha,
cmap=cmap,
cmap_transform=cmap_transform,
isolated_buffer=isolated_buffer,
size_space=size_space,
**kwargs,
)
self._thickness = Thickness(thickness)
if thickness < 1.1:
MaterialCls = pygfx.LineThinMaterial
else:
MaterialCls = pygfx.LineMaterial
if uniform_color:
geometry = pygfx.Geometry(positions=self._data.buffer)
material = MaterialCls(
thickness=self.thickness,
color_mode="uniform",
color=self.colors,
pick_write=True,
thickness_space=self.size_space,
)
else:
material = MaterialCls(
thickness=self.thickness,
color_mode="vertex",
pick_write=True,
thickness_space=self.size_space,
)
geometry = pygfx.Geometry(
positions=self._data.buffer, colors=self._colors.buffer
)
world_object: pygfx.Line = pygfx.Line(geometry=geometry, material=material)
self._set_world_object(world_object)
@property
def thickness(self) -> float:
"""line thickness"""
return self._thickness.value
@thickness.setter
def thickness(self, value: float):
self._thickness.set_value(self, value)
def add_linear_selector(
self, selection: float = None, padding: float = 0.0, axis: str = "x", **kwargs
) -> LinearSelector:
"""
Adds a linear selector.
Parameters
----------
Parameters
----------
selection: float, optional
selected point on the linear selector, computed from data if not provided
axis: str, default "x"
axis that the selector resides on
padding: float, default 0.0
Extra padding to extend the linear selector along the orthogonal axis to make it easier to interact with.
kwargs
passed to :class:`.LinearSelector`
Returns
-------
LinearSelector
"""
bounds_init, limits, size, center = self._get_linear_selector_init_args(
axis, padding
)
if selection is None:
selection = bounds_init[0]
selector = LinearSelector(
selection=selection,
limits=limits,
size=size,
center=center,
axis=axis,
parent=self,
**kwargs,
)
self._plot_area.add_graphic(selector, center=False)
# place selector above this graphic
selector.offset = selector.offset + (0.0, 0.0, self.offset[-1] + 1)
return selector
def add_linear_region_selector(
self,
selection: tuple[float, float] = None,
padding: float = 0.0,
axis: str = "x",
**kwargs,
) -> LinearRegionSelector:
"""
Add a :class:`.LinearRegionSelector`. Selectors are just ``Graphic`` objects, so you can manage,
remove, or delete them from a plot area just like any other ``Graphic``.
Parameters
----------
selection: (float, float), optional
the starting bounds of the linear region selector, computed from data if not provided
axis: str, default "x"
axis that the selector resides on
padding: float, default 0.0
Extra padding to extend the linear region selector along the orthogonal axis to make it easier to interact with.
kwargs
passed to ``LinearRegionSelector``
Returns
-------
LinearRegionSelector
linear selection graphic
"""
bounds_init, limits, size, center = self._get_linear_selector_init_args(
axis, padding
)
if selection is None:
selection = bounds_init
# create selector
selector = LinearRegionSelector(
selection=selection,
limits=limits,
size=size,
center=center,
axis=axis,
parent=self,
**kwargs,
)
self._plot_area.add_graphic(selector, center=False)
# place selector below this graphic
selector.offset = selector.offset + (0.0, 0.0, self.offset[-1] - 1)
# PlotArea manages this for garbage collection etc. just like all other Graphics
# so we should only work with a proxy on the user-end
return selector
def add_rectangle_selector(
self,
selection: tuple[float, float, float, float] = None,
**kwargs,
) -> RectangleSelector:
"""
Add a :class:`.RectangleSelector`. Selectors are just ``Graphic`` objects, so you can manage,
remove, or delete them from a plot area just like any other ``Graphic``.
Parameters
----------
selection: (float, float, float, float), optional
initial (xmin, xmax, ymin, ymax) of the selection
"""
# computes args to create selectors
n_datapoints = self.data.value.shape[0]
value_25p = int(n_datapoints / 4)
# remove any nans
data = self.data.value[~np.any(np.isnan(self.data.value), axis=1)]
x_axis_vals = data[:, 0]
y_axis_vals = data[:, 1]
ymin = np.floor(y_axis_vals.min()).astype(int)
ymax = np.ceil(y_axis_vals.max()).astype(int)
# default selection is 25% of the image
if selection is None:
selection = (x_axis_vals[0], x_axis_vals[value_25p], ymin, ymax)
# min/max limits
limits = (x_axis_vals[0], x_axis_vals[-1], ymin * 1.5, ymax * 1.5)
selector = RectangleSelector(
selection=selection,
limits=limits,
parent=self,
**kwargs,
)
self._plot_area.add_graphic(selector, center=False)
return selector
# TODO: this method is a bit of a mess, can refactor later
def _get_linear_selector_init_args(
self, axis: str, padding
) -> tuple[tuple[float, float], tuple[float, float], float, float]:
# computes args to create selectors
n_datapoints = self.data.value.shape[0]
value_25p = int(n_datapoints / 4)
# remove any nans
data = self.data.value[~np.any(np.isnan(self.data.value), axis=1)]
if axis == "x":
# xvals
axis_vals = data[:, 0]
# yvals to get size and center
magn_vals = data[:, 1]
elif axis == "y":
axis_vals = data[:, 1]
magn_vals = data[:, 0]
bounds_init = axis_vals[0], axis_vals[value_25p]
limits = axis_vals[0], axis_vals[-1]
# width or height of selector
size = np.ptp(magn_vals) * 1.5 + padding
# center of selector along the other axis
center = np.ptp(magn_vals) / 2#np.nanmean(magn_vals)
return bounds_init, limits, size, center