Skip to content

Commit 011bab6

Browse files
authored
Merge pull request #93 from kushalkolar/interactivity-rebase
interactivity using `Graphic.link()`, works with pygfx click events, and GraphicFeatures, tested with colors only so far.
2 parents 97a5680 + 118b3a8 commit 011bab6

File tree

14 files changed

+796
-595
lines changed

14 files changed

+796
-595
lines changed

examples/collection.ipynb

Lines changed: 0 additions & 377 deletions
This file was deleted.
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "f02f7349-ac1a-49b7-b304-d5b701723e0f",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"%load_ext autoreload\n",
11+
"%autoreload 2"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"id": "d9f54448-7718-4212-ac6d-163a2d3be146",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"import numpy as np\n",
22+
"from fastplotlib.graphics import LineGraphic, LineCollection, ImageGraphic\n",
23+
"from fastplotlib.plot import Plot\n",
24+
"import pickle"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 2,
30+
"id": "b683c6d1-d926-43e3-a221-4ec6450e3677",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"contours = pickle.load(open(\"/home/caitlin/Downloads/contours.pickle\", \"rb\"))"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 3,
40+
"id": "26ced005-c52f-4696-903d-a6974ae6cefc",
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"data": {
45+
"application/vnd.jupyter.widget-view+json": {
46+
"model_id": "2bc75fa52d484cd1b03006a6530f10b3",
47+
"version_major": 2,
48+
"version_minor": 0
49+
},
50+
"text/plain": [
51+
"RFBOutputContext()"
52+
]
53+
},
54+
"metadata": {},
55+
"output_type": "display_data"
56+
},
57+
{
58+
"name": "stderr",
59+
"output_type": "stream",
60+
"text": [
61+
"MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0\n",
62+
"\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"plot = Plot()"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 4,
73+
"id": "1aea0a61-db63-41e1-b330-5a922da4bac5",
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"line_collection = LineCollection(contours, cmap=\"Oranges\")"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 5,
83+
"id": "310fd5b3-49f2-4dbb-831e-cb9f369d58c8",
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"plot.add_graphic(line_collection)"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 6,
93+
"id": "661a5991-0de3-44d7-a626-2ae72704dcec",
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"image = ImageGraphic(data=np.ones((180, 180)))"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 7,
103+
"id": "816f6382-f4ea-4cd4-b9f3-2dc5c232b0a5",
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"plot.add_graphic(image)"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"id": "8187fd25-18e1-451f-b2fe-8cd2e7785c8b",
114+
"metadata": {},
115+
"outputs": [],
116+
"source": [
117+
"plot.show()"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"id": "d5ddd3c4-84e2-44a3-a14f-74871aa0bb8f",
124+
"metadata": {},
125+
"outputs": [],
126+
"source": [
127+
"black = np.array([0.0, 0.0, 0.0, 1.0])"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"id": "6ac64f1c-21e0-4c21-b968-3953e7858848",
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"from typing import *"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"id": "d0d0c971-aac9-4c77-b88c-de9d79c7d74e",
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"def callback_function(source: Any, target: Any, event, new_data: Any):\n",
148+
" # calculate coms of line collection\n",
149+
" indices = np.array(event.pick_info[\"index\"])\n",
150+
" \n",
151+
" coms = list()\n",
152+
"\n",
153+
" for contour in target.items:\n",
154+
" coors = contour.data.feature_data[~np.isnan(contour.data.feature_data).any(axis=1)]\n",
155+
" com = coors.mean(axis=0)\n",
156+
" coms.append(com)\n",
157+
"\n",
158+
" # euclidean distance to find closest index of com \n",
159+
" indices = np.append(indices, [0])\n",
160+
" \n",
161+
" ix = np.linalg.norm((coms - indices), axis=1).argsort()[0]\n",
162+
" \n",
163+
" ix = int(ix)\n",
164+
" \n",
165+
" print(ix)\n",
166+
" \n",
167+
" target._set_feature(feature=\"colors\", new_data=new_data, indices=ix)\n",
168+
" \n",
169+
" return None"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"id": "1a46d148-3007-4c7a-bf2b-91057eba855d",
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"image.link(event_type=\"click\", target=line_collection, feature=\"colors\", new_data=black, callback_function=callback_function)"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": null,
185+
"id": "8040456e-24a5-423b-8822-99a20e7ea470",
186+
"metadata": {},
187+
"outputs": [],
188+
"source": []
189+
}
190+
],
191+
"metadata": {
192+
"kernelspec": {
193+
"display_name": "Python 3 (ipykernel)",
194+
"language": "python",
195+
"name": "python3"
196+
},
197+
"language_info": {
198+
"codemirror_mode": {
199+
"name": "ipython",
200+
"version": 3
201+
},
202+
"file_extension": ".py",
203+
"mimetype": "text/x-python",
204+
"name": "python",
205+
"nbconvert_exporter": "python",
206+
"pygments_lexer": "ipython3",
207+
"version": "3.9.2"
208+
}
209+
},
210+
"nbformat": 4,
211+
"nbformat_minor": 5
212+
}

examples/single_contour_event.ipynb

Lines changed: 225 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)