Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 216 additions & 2 deletions examples/selector_performance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"source": [
"from typing import *\n",
"from itertools import product\n",
"from time import time\n",
"\n",
"import numpy as np\n",
"import fastplotlib as fpl\n",
Expand All @@ -30,8 +31,12 @@
" theta = np.linspace(0, 2 * np.pi, n_points)\n",
" xs = radius * np.sin(theta)\n",
" ys = radius * np.cos(theta)\n",
" zs = np.zeros(xs.size)\n",
" \n",
" return np.column_stack([xs, ys]) + center"
" xs += center[0]\n",
" ys += center[1]\n",
" \n",
" return np.ascontiguousarray(np.column_stack([xs, ys, zs]).astype(np.float32))"
]
},
{
Expand Down Expand Up @@ -62,6 +67,108 @@
"len(circles)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df3d5e58-09e4-41ff-8141-3e5dcd96c442",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from wgpu.gui.auto import WgpuCanvas, run\n",
"import pygfx as gfx"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d56311fd-e184-4a18-8016-33b51fe1304e",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"canvas = WgpuCanvas()\n",
"renderer = gfx.WgpuRenderer(canvas)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "145f974a-dbc0-4d38-ac31-3f4b944cadbf",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"scene = gfx.Scene()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c7872e2-c934-472d-89b4-5a64049f7059",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"lines = list()\n",
"\n",
"for c in circles:\n",
" line = gfx.Line(\n",
" gfx.Geometry(positions=c),\n",
" gfx.LineMaterial(thickness=1.2, color=(1, 1, 1, 1)),\n",
" )\n",
" lines.append(line)\n",
" scene.add(line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3fb74456-4b41-4d40-9993-213611bff28c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"gfx.show(scene, camera=gfx.OrthographicCamera(1000, 1000))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6f4a511-536c-43b4-8b2d-ef587fbf1529",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"camera = gfx.OrthographicCamera(1000, 1000)\n",
"controller = gfx.PanZoomController(camera, register_events=renderer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5e34579-1b45-4828-a187-1d88a1058b58",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"t1 = time()\n",
"\n",
"for l in lines[100:1000]:\n",
" l.visible = False\n",
" \n",
"# canvas.request_draw()\n",
"\n",
"time() - t1"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -106,13 +213,120 @@
"source": [
"plot = fpl.GridPlot((1, 2))\n",
"\n",
"contours = plot[0, 0].add_line(np.vstack(circles), thickness=3)\n",
"# contours = plot[0, 0].add_line(np.vstack(circles), thickness=3)\n",
"contours = plot[0, 0].add_line_collection(circles, thickness=1.5)\n",
"heatmap = plot[0, 1].add_heatmap(temporal)\n",
"selector = heatmap.add_linear_region_selector(axis=\"y\")\n",
"\n",
"plot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8135a679-1a77-42bf-bb1f-78f00a984784",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def update_visible(ev):\n",
" ixs_visible = ev.pick_info[\"selected_indices\"]\n",
" ixs_hide = np.setdiff1d(np.arange(len(circles)), ixs_visible)\n",
" \n",
" # very fast, 20 ms to change 1,000\n",
" for i, g in enumerate(contours.graphics):\n",
" if not g.visible and i in ixs_visible:\n",
" g.visible = True\n",
" elif g.visible and i in ixs_hide:\n",
" g.visible = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc3bcba2-fb8b-4583-a893-a2fec273550a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"selector.bounds.add_event_handler(update_visible)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6bb9a952-85f7-4e3b-8301-fb156ad267e1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"selector.bounds.remove_event_handler(update_visible)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4ac3b79-0a90-4b76-9390-6b4097fed08a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"selector.bounds._event_handlers.clear()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f432248e-70e3-438a-9b9b-99247247e6f0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"class Event:\n",
" pick_info = {\"selected_indices\": np.arange(2000, 3000)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c01aa29-232b-4390-b9ed-fef429017f19",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"t1 = time()\n",
"\n",
"update_visible(Event())\n",
"\n",
"time() - t1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "691400bd-0eb4-4cad-a08d-0258c2243337",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"t1 = time()\n",
"\n",
"for c in contours.graphics[100:1000]:\n",
" c.visible = True\n",
" \n",
"# for i in range(100, 1000):\n",
"# contours.graphics[i].world_object.visible = True\n",
" \n",
"time() - t1"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
Loading