Skip to content

Commit 12e55e0

Browse files
authored
catchup with pygfx linalg refactor (#203)
* WIP, mapping to points and selectors not updated * everything updated for linalg refactor except rectangle selector * update toolbar w.r.t. linalg refactor * update examples w.r.t. linalg refactor * update readme
1 parent f872155 commit 12e55e0

19 files changed

Lines changed: 159 additions & 228 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Questions, ideas? Post an issue or [chat on gitter](https://gitter.im/fastplotli
2121

2222
**See the examples directory. Start out with `simple.ipynb`.**
2323

24+
**IMPORTANT NOTE: If you install `fastplotlib` and `pygfx` from `pypi` (i.e. pip install pygfx), you will need to use the examples from this commit until `pygfx` publishes a new release to `pypi`: https://github.com/kushalkolar/fastplotlib/tree/f872155eb687b18e3cc9b3b720eb9e241a9f974c/examples .**
25+
The current examples will work if you installed `fastplotlib` and `pygfx` directly from github.
26+
2427
### Neuroscience usecase demonstrating some of fastplotlib's capabilities
2528

2629
https://user-images.githubusercontent.com/9403332/210304485-e554b648-50b4-4243-b292-a9ed30514a2d.mp4

examples/linear_region_selector.ipynb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"sine_graphic_y = gp[0, 1].add_line(np.column_stack([sine_y, xs]))\n",
4242
"\n",
4343
"# offset the position of the graphic to demonstrate `get_selected_data()` later\n",
44-
"sine_graphic_y.position.set_x(50)\n",
45-
"sine_graphic_y.position.set_y(50)\n",
44+
"sine_graphic_y.position_x = 50\n",
45+
"sine_graphic_y.position_y = 50\n",
4646
"\n",
4747
"# add linear selectors\n",
4848
"ls_x = sine_graphic_x.add_linear_region_selector() # default axis is \"x\"\n",
@@ -256,18 +256,6 @@
256256
"plot.show()"
257257
]
258258
},
259-
{
260-
"cell_type": "code",
261-
"execution_count": null,
262-
"id": "3fa61ffd-43d5-42d0-b3e1-5541f58185cd",
263-
"metadata": {
264-
"tags": []
265-
},
266-
"outputs": [],
267-
"source": [
268-
"plot[0, 0].auto_scale()"
269-
]
270-
},
271259
{
272260
"cell_type": "code",
273261
"execution_count": null,

examples/linear_selector.ipynb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@
5555
"VBox([plot.show(), ipywidget_slider])"
5656
]
5757
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"id": "a632c8ee-2d4c-44fc-9391-7b2880223fdb",
62+
"metadata": {
63+
"tags": []
64+
},
65+
"outputs": [],
66+
"source": [
67+
"selector.step = 0.1"
68+
]
69+
},
5870
{
5971
"cell_type": "markdown",
6072
"id": "2c49cdc2-0555-410c-ae2e-da36c3bf3bf0",

examples/lineplot.ipynb

Lines changed: 5 additions & 62 deletions
Large diffs are not rendered by default.

examples/scatter.ipynb

Lines changed: 18 additions & 63 deletions
Large diffs are not rendered by default.

fastplotlib/graphics/_base.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from .features._base import cleanup_slice
88

99
from pygfx import WorldObject, Group
10-
from pygfx.linalg import Vector3
11-
1210
from .features import GraphicFeature, PresentFeature, GraphicFeatureIndexable
1311

1412
from abc import ABC, abstractmethod
@@ -83,10 +81,38 @@ def _set_world_object(self, wo: WorldObject):
8381
WORLD_OBJECTS[hex(id(self))] = wo
8482

8583
@property
86-
def position(self) -> Vector3:
84+
def position(self) -> np.ndarray:
8785
"""The position of the graphic. You can access or change
8886
using position.x, position.y, etc."""
89-
return self.world_object.position
87+
return self.world_object.world.position
88+
89+
@property
90+
def position_x(self) -> float:
91+
return self.world_object.world.x
92+
93+
@property
94+
def position_y(self) -> float:
95+
return self.world_object.world.y
96+
97+
@property
98+
def position_z(self) -> float:
99+
return self.world_object.world.z
100+
101+
@position.setter
102+
def position(self, val):
103+
self.world_object.world.position = val
104+
105+
@position_x.setter
106+
def position_x(self, val):
107+
self.world_object.world.x = val
108+
109+
@position_y.setter
110+
def position_y(self, val):
111+
self.world_object.world.y = val
112+
113+
@position_z.setter
114+
def position_z(self, val):
115+
self.world_object.world.z = val
90116

91117
@property
92118
def visible(self) -> bool:
@@ -99,7 +125,7 @@ def visible(self, v: bool):
99125
self.world_object.visible = v
100126

101127
@property
102-
def children(self) -> WorldObject:
128+
def children(self) -> List[WorldObject]:
103129
"""Return the children of the WorldObject."""
104130
return self.world_object.children
105131

fastplotlib/graphics/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def add_linear_selector(self, selection: int = None, padding: float = None, **kw
5858
)
5959

6060
self._plot_area.add_graphic(selector, center=False)
61-
selector.position.z = self.position.z + 1
61+
selector.position_z = self.position_z + 1
6262

6363
return weakref.proxy(selector)
6464

@@ -97,7 +97,7 @@ def add_linear_region_selector(self, padding: float = None, **kwargs) -> LinearR
9797

9898
self._plot_area.add_graphic(selector, center=False)
9999
# so that it is above this graphic
100-
selector.position.set_z(self.position.z + 3)
100+
selector.position_z = self.position_z + 3
101101

102102
# PlotArea manages this for garbage collection etc. just like all other Graphics
103103
# so we should only work with a proxy on the user-end

fastplotlib/graphics/line.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def __init__(
9797
self._set_world_object(world_object)
9898

9999
if z_position is not None:
100-
self.world_object.position.z = z_position
100+
self.position_z = z_position
101101

102102
def add_linear_selector(self, selection: int = None, padding: float = 50, **kwargs) -> LinearSelector:
103103
"""
@@ -137,7 +137,7 @@ def add_linear_selector(self, selection: int = None, padding: float = 50, **kwar
137137
)
138138

139139
self._plot_area.add_graphic(selector, center=False)
140-
selector.position.z = self.position.z + 1
140+
selector.position_z = self.position_z + 1
141141

142142
return weakref.proxy(selector)
143143

@@ -175,7 +175,7 @@ def add_linear_region_selector(self, padding: float = 100.0, **kwargs) -> Linear
175175

176176
self._plot_area.add_graphic(selector, center=False)
177177
# so that it is below this graphic
178-
selector.position.set_z(self.position.z - 1)
178+
selector.position_z = self.position_z - 1
179179

180180
# PlotArea manages this for garbage collection etc. just like all other Graphics
181181
# so we should only work with a proxy on the user-end
@@ -192,7 +192,7 @@ def _get_linear_selector_init_args(self, padding: float, **kwargs):
192192
axis = "x"
193193

194194
if axis == "x":
195-
offset = self.position.x
195+
offset = self.position_x
196196
# x limits
197197
limits = (data[0, 0] + offset, data[-1, 0] + offset)
198198

@@ -203,13 +203,13 @@ def _get_linear_selector_init_args(self, padding: float, **kwargs):
203203
position_y = (data[:, 1].min() + data[:, 1].max()) / 2
204204

205205
# need y offset too for this
206-
origin = (limits[0] - offset, position_y + self.position.y)
206+
origin = (limits[0] - offset, position_y + self.position_y)
207207

208208
# endpoints of the data range
209209
# used by linear selector but not linear region
210210
end_points = (self.data()[:, 1].min() - padding, self.data()[:, 1].max() + padding)
211211
else:
212-
offset = self.position.y
212+
offset = self.position_y
213213
# y limits
214214
limits = (data[0, 1] + offset, data[-1, 1] + offset)
215215

@@ -220,7 +220,7 @@ def _get_linear_selector_init_args(self, padding: float, **kwargs):
220220
position_x = (data[:, 0].min() + data[:, 0].max()) / 2
221221

222222
# need x offset too for this
223-
origin = (position_x + self.position.x, limits[0] - offset)
223+
origin = (position_x + self.position_x, limits[0] - offset)
224224

225225
end_points = (self.data()[:, 0].min() - padding, self.data()[:, 0].max() + padding)
226226

fastplotlib/graphics/line_collection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def add_linear_selector(self, selection: int = None, padding: float = 50, **kwar
265265
)
266266

267267
self._plot_area.add_graphic(selector, center=False)
268-
selector.position.z = self.position.z + 1
268+
selector.position_z = self.position_z + 1
269269

270270
return weakref.proxy(selector)
271271

@@ -302,7 +302,7 @@ def add_linear_region_selector(self, padding: float = 100.0, **kwargs) -> Linear
302302
)
303303

304304
self._plot_area.add_graphic(selector, center=False)
305-
selector.position.set_z(self.position.z - 1)
305+
selector.position_z = self.position_z - 1
306306

307307
return weakref.proxy(selector)
308308

@@ -346,7 +346,7 @@ def _get_linear_selector_init_args(self, padding, **kwargs):
346346

347347
# a better way to get the max y value?
348348
# graphics y-position + data y-max + padding
349-
end_points[1] = self.graphics[-1].position.y + self.graphics[-1].data()[:, 1].max() + padding
349+
end_points[1] = self.graphics[-1].position_y + self.graphics[-1].data()[:, 1].max() + padding
350350

351351
else:
352352
# just the biggest one if not stacked
@@ -521,7 +521,11 @@ def __init__(
521521

522522
axis_zero = 0
523523
for i, line in enumerate(self.graphics):
524-
getattr(line.position, f"set_{separation_axis}")(axis_zero)
524+
if separation_axis == "x":
525+
line.position_x = axis_zero
526+
elif separation_axis == "y":
527+
line.position_y = axis_zero
528+
525529
axis_zero = axis_zero + line.data()[:, axes[separation_axis]].max() + separation
526530

527531
self.separation = separation

fastplotlib/graphics/scatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ def __init__(
7979

8080
self._set_world_object(world_object)
8181

82-
self.world_object.position.z = z_position
82+
self.position_z = z_position

0 commit comments

Comments
 (0)