Skip to content

Commit 0f57931

Browse files
kushalkolarclewis7
andauthored
use numpy array instead of tuple for indexing collections (#215)
* use numpy array instead of tuple for indexing collections * set_data() (#216) * set_data() * fix small logic piece * requested changes * refactor reset_vmin_vmax() to work without sliders * should be functional, need to do some more extensive testing * removing unneccessary method * tweaks, not yet tested * image widget set_data fully works --------- Co-authored-by: kushalkolar <kushalkolar@gmail.com> * use numpy array instead of tuple for indexing collections * update nb * progresS --------- Co-authored-by: Caitlin Lewis <69729525+clewis7@users.noreply.github.com>
1 parent 43cd73c commit 0f57931

4 files changed

Lines changed: 270 additions & 59 deletions

File tree

examples/selector_performance.ipynb

Lines changed: 216 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"source": [
1212
"from typing import *\n",
1313
"from itertools import product\n",
14+
"from time import time\n",
1415
"\n",
1516
"import numpy as np\n",
1617
"import fastplotlib as fpl\n",
@@ -30,8 +31,12 @@
3031
" theta = np.linspace(0, 2 * np.pi, n_points)\n",
3132
" xs = radius * np.sin(theta)\n",
3233
" ys = radius * np.cos(theta)\n",
34+
" zs = np.zeros(xs.size)\n",
3335
" \n",
34-
" return np.column_stack([xs, ys]) + center"
36+
" xs += center[0]\n",
37+
" ys += center[1]\n",
38+
" \n",
39+
" return np.ascontiguousarray(np.column_stack([xs, ys, zs]).astype(np.float32))"
3540
]
3641
},
3742
{
@@ -62,6 +67,108 @@
6267
"len(circles)"
6368
]
6469
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"id": "df3d5e58-09e4-41ff-8141-3e5dcd96c442",
74+
"metadata": {
75+
"tags": []
76+
},
77+
"outputs": [],
78+
"source": [
79+
"from wgpu.gui.auto import WgpuCanvas, run\n",
80+
"import pygfx as gfx"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"id": "d56311fd-e184-4a18-8016-33b51fe1304e",
87+
"metadata": {
88+
"tags": []
89+
},
90+
"outputs": [],
91+
"source": [
92+
"canvas = WgpuCanvas()\n",
93+
"renderer = gfx.WgpuRenderer(canvas)"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"id": "145f974a-dbc0-4d38-ac31-3f4b944cadbf",
100+
"metadata": {
101+
"tags": []
102+
},
103+
"outputs": [],
104+
"source": [
105+
"scene = gfx.Scene()"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"id": "1c7872e2-c934-472d-89b4-5a64049f7059",
112+
"metadata": {
113+
"tags": []
114+
},
115+
"outputs": [],
116+
"source": [
117+
"lines = list()\n",
118+
"\n",
119+
"for c in circles:\n",
120+
" line = gfx.Line(\n",
121+
" gfx.Geometry(positions=c),\n",
122+
" gfx.LineMaterial(thickness=1.2, color=(1, 1, 1, 1)),\n",
123+
" )\n",
124+
" lines.append(line)\n",
125+
" scene.add(line)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"id": "3fb74456-4b41-4d40-9993-213611bff28c",
132+
"metadata": {
133+
"tags": []
134+
},
135+
"outputs": [],
136+
"source": [
137+
"gfx.show(scene, camera=gfx.OrthographicCamera(1000, 1000))"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"id": "a6f4a511-536c-43b4-8b2d-ef587fbf1529",
144+
"metadata": {
145+
"tags": []
146+
},
147+
"outputs": [],
148+
"source": [
149+
"camera = gfx.OrthographicCamera(1000, 1000)\n",
150+
"controller = gfx.PanZoomController(camera, register_events=renderer)"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": null,
156+
"id": "c5e34579-1b45-4828-a187-1d88a1058b58",
157+
"metadata": {
158+
"tags": []
159+
},
160+
"outputs": [],
161+
"source": [
162+
"t1 = time()\n",
163+
"\n",
164+
"for l in lines[100:1000]:\n",
165+
" l.visible = False\n",
166+
" \n",
167+
"# canvas.request_draw()\n",
168+
"\n",
169+
"time() - t1"
170+
]
171+
},
65172
{
66173
"cell_type": "code",
67174
"execution_count": null,
@@ -106,13 +213,120 @@
106213
"source": [
107214
"plot = fpl.GridPlot((1, 2))\n",
108215
"\n",
109-
"contours = plot[0, 0].add_line(np.vstack(circles), thickness=3)\n",
216+
"# contours = plot[0, 0].add_line(np.vstack(circles), thickness=3)\n",
217+
"contours = plot[0, 0].add_line_collection(circles, thickness=1.5)\n",
110218
"heatmap = plot[0, 1].add_heatmap(temporal)\n",
111219
"selector = heatmap.add_linear_region_selector(axis=\"y\")\n",
112220
"\n",
113221
"plot.show()"
114222
]
115223
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"id": "8135a679-1a77-42bf-bb1f-78f00a984784",
228+
"metadata": {
229+
"tags": []
230+
},
231+
"outputs": [],
232+
"source": [
233+
"def update_visible(ev):\n",
234+
" ixs_visible = ev.pick_info[\"selected_indices\"]\n",
235+
" ixs_hide = np.setdiff1d(np.arange(len(circles)), ixs_visible)\n",
236+
" \n",
237+
" # very fast, 20 ms to change 1,000\n",
238+
" for i, g in enumerate(contours.graphics):\n",
239+
" if not g.visible and i in ixs_visible:\n",
240+
" g.visible = True\n",
241+
" elif g.visible and i in ixs_hide:\n",
242+
" g.visible = False"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": null,
248+
"id": "bc3bcba2-fb8b-4583-a893-a2fec273550a",
249+
"metadata": {
250+
"tags": []
251+
},
252+
"outputs": [],
253+
"source": [
254+
"selector.bounds.add_event_handler(update_visible)"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": null,
260+
"id": "6bb9a952-85f7-4e3b-8301-fb156ad267e1",
261+
"metadata": {
262+
"tags": []
263+
},
264+
"outputs": [],
265+
"source": [
266+
"selector.bounds.remove_event_handler(update_visible)"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": null,
272+
"id": "c4ac3b79-0a90-4b76-9390-6b4097fed08a",
273+
"metadata": {
274+
"tags": []
275+
},
276+
"outputs": [],
277+
"source": [
278+
"selector.bounds._event_handlers.clear()"
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": null,
284+
"id": "f432248e-70e3-438a-9b9b-99247247e6f0",
285+
"metadata": {
286+
"tags": []
287+
},
288+
"outputs": [],
289+
"source": [
290+
"class Event:\n",
291+
" pick_info = {\"selected_indices\": np.arange(2000, 3000)}"
292+
]
293+
},
294+
{
295+
"cell_type": "code",
296+
"execution_count": null,
297+
"id": "4c01aa29-232b-4390-b9ed-fef429017f19",
298+
"metadata": {
299+
"tags": []
300+
},
301+
"outputs": [],
302+
"source": [
303+
"t1 = time()\n",
304+
"\n",
305+
"update_visible(Event())\n",
306+
"\n",
307+
"time() - t1"
308+
]
309+
},
310+
{
311+
"cell_type": "code",
312+
"execution_count": null,
313+
"id": "691400bd-0eb4-4cad-a08d-0258c2243337",
314+
"metadata": {
315+
"tags": []
316+
},
317+
"outputs": [],
318+
"source": [
319+
"t1 = time()\n",
320+
"\n",
321+
"for c in contours.graphics[100:1000]:\n",
322+
" c.visible = True\n",
323+
" \n",
324+
"# for i in range(100, 1000):\n",
325+
"# contours.graphics[i].world_object.visible = True\n",
326+
" \n",
327+
"time() - t1"
328+
]
329+
},
116330
{
117331
"cell_type": "code",
118332
"execution_count": null,

0 commit comments

Comments
 (0)