Skip to content

Commit 0bdddf7

Browse files
authored
Merge pull request #51 from kushalkolar/high-level-widgets
ImageWidget
2 parents daf6984 + 8fc63b3 commit 0bdddf7

5 files changed

Lines changed: 1377 additions & 1 deletion

File tree

examples/image_widget.ipynb

Lines changed: 393 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,393 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "04f453ca-d0bc-411f-b2a6-d38294dd0a26",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from fastplotlib.widgets import ImageWidget\n",
11+
"import numpy as np"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "e933771b-f172-4fa9-b2f8-129723efb808",
17+
"metadata": {},
18+
"source": [
19+
"# Single image sequence"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"id": "ea87f9a6-437f-41f6-8739-c957fb04bdbf",
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"a = np.random.rand(500, 512, 512)"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 3,
35+
"id": "8b7a6066-ff69-4bee-bae6-160fb4038393",
36+
"metadata": {},
37+
"outputs": [
38+
{
39+
"data": {
40+
"application/vnd.jupyter.widget-view+json": {
41+
"model_id": "6d575ba7671047ca88c36606344714fa",
42+
"version_major": 2,
43+
"version_minor": 0
44+
},
45+
"text/plain": [
46+
"RFBOutputContext()"
47+
]
48+
},
49+
"metadata": {},
50+
"output_type": "display_data"
51+
}
52+
],
53+
"source": [
54+
"iw = ImageWidget(\n",
55+
" data=a, \n",
56+
" slider_dims=[\"t\"],\n",
57+
" vmin_vmax_sliders=True,\n",
58+
" cmap=\"gnuplot2\"\n",
59+
")"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 4,
65+
"id": "3d4cb44e-2c71-4bff-aeed-b2129f34d724",
66+
"metadata": {},
67+
"outputs": [
68+
{
69+
"data": {
70+
"application/vnd.jupyter.widget-view+json": {
71+
"model_id": "8de187407b7746168c8d20a428d8712e",
72+
"version_major": 2,
73+
"version_minor": 0
74+
},
75+
"text/plain": [
76+
"VBox(children=(JupyterWgpuCanvas(), IntSlider(value=0, description='dimension: t', max=499), FloatRangeSlider(…"
77+
]
78+
},
79+
"metadata": {},
80+
"output_type": "display_data"
81+
}
82+
],
83+
"source": [
84+
"iw.show()"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"id": "9908103c-c35c-4f33-ada1-0fc357c3fd5e",
90+
"metadata": {},
91+
"source": [
92+
"### Play with setting different window functions\n",
93+
"\n",
94+
"These can also be given as kwargs to `ImageWidget` during instantiation"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 5,
100+
"id": "f278b26a-1b71-4e76-9cc7-efaddbd7b122",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"# must be in the form of {dim: (func, window_size)}\n",
105+
"iw.window_funcs = {\"t\": (np.mean, 13)}"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 6,
111+
"id": "cb4d4b7c-919f-41c0-b1cc-b4496473d760",
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"# change the winow size\n",
116+
"iw.window_funcs[\"t\"].window_size = 23"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 7,
122+
"id": "2eea6432-4d38-4d42-ab75-f6aa1bab36f4",
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"# change the function\n",
127+
"iw.window_funcs[\"t\"].func = np.max"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": 8,
133+
"id": "afa2436f-2741-49d6-87f6-7a91a343fe0e",
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"# or set it again\n",
138+
"iw.window_funcs = {\"t\": (np.min, 11)}"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"id": "aca22179-1b1f-4c51-97bf-ce2d7044e451",
144+
"metadata": {},
145+
"source": [
146+
"# Gridplot of txy data"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": 10,
152+
"id": "882162eb-c873-42df-a945-d5e05ad141c9",
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"dims = (100, 512, 512)\n",
157+
"data = [np.random.rand(*dims) for i in range(4)]"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 12,
163+
"id": "bf9f92b6-38ad-4d78-b88c-a32d473b6462",
164+
"metadata": {},
165+
"outputs": [
166+
{
167+
"data": {
168+
"application/vnd.jupyter.widget-view+json": {
169+
"model_id": "005bcbc7755748cfaf0644e28beb3b0e",
170+
"version_major": 2,
171+
"version_minor": 0
172+
},
173+
"text/plain": [
174+
"RFBOutputContext()"
175+
]
176+
},
177+
"metadata": {},
178+
"output_type": "display_data"
179+
}
180+
],
181+
"source": [
182+
"iw = ImageWidget(\n",
183+
" data=data, \n",
184+
" slider_dims=[\"t\"], \n",
185+
" # dims_order=\"txy\", # you can set this manually if dim order is not the usual\n",
186+
" vmin_vmax_sliders=True,\n",
187+
" names=[\"zero\", \"one\", \"two\", \"three\"],\n",
188+
" window_funcs={\"t\": (np.mean, 5)},\n",
189+
" cmap=\"gnuplot2\", \n",
190+
")"
191+
]
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"id": "0721dc40-677e-431d-94c6-da59606199cb",
196+
"metadata": {},
197+
"source": [
198+
"### pan-zoom controllers are all synced in a `ImageWidget`"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": 13,
204+
"id": "403dde31-981a-46fb-b005-1bcef19c4f2c",
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"data": {
209+
"application/vnd.jupyter.widget-view+json": {
210+
"model_id": "2b0a10be5d5b43b5a08f51a9d8f9b1dc",
211+
"version_major": 2,
212+
"version_minor": 0
213+
},
214+
"text/plain": [
215+
"VBox(children=(JupyterWgpuCanvas(), IntSlider(value=0, description='dimension: t', max=99), FloatRangeSlider(v…"
216+
]
217+
},
218+
"metadata": {},
219+
"output_type": "display_data"
220+
}
221+
],
222+
"source": [
223+
"iw.show()"
224+
]
225+
},
226+
{
227+
"cell_type": "markdown",
228+
"id": "82545214-13c4-475e-87da-962117085834",
229+
"metadata": {},
230+
"source": [
231+
"### Index the subplots using the names given to `ImageWidget`"
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": 14,
237+
"id": "b59d95e2-9092-4915-beef-01661d164781",
238+
"metadata": {},
239+
"outputs": [
240+
{
241+
"data": {
242+
"text/plain": [
243+
"two: Subplot @ 0x7f91486a7a00\n",
244+
" parent: None\n",
245+
" Graphics:\n",
246+
"\tfastplotlib.ImageGraphic @ 0x7f914881ceb0"
247+
]
248+
},
249+
"execution_count": 14,
250+
"metadata": {},
251+
"output_type": "execute_result"
252+
}
253+
],
254+
"source": [
255+
"iw.plot[\"two\"]"
256+
]
257+
},
258+
{
259+
"cell_type": "markdown",
260+
"id": "dc727d1a-681e-4cbf-bfb2-898ceb31cbe0",
261+
"metadata": {},
262+
"source": [
263+
"### change window functions just like before"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 15,
269+
"id": "a8f070db-da11-4062-95aa-f19b96351ee8",
270+
"metadata": {},
271+
"outputs": [],
272+
"source": [
273+
"iw.window_funcs[\"t\"].func = np.max"
274+
]
275+
},
276+
{
277+
"cell_type": "markdown",
278+
"id": "3e89c10f-6e34-4d63-9805-88403d487432",
279+
"metadata": {},
280+
"source": [
281+
"## Gridplot of volumetric data"
282+
]
283+
},
284+
{
285+
"cell_type": "code",
286+
"execution_count": 16,
287+
"id": "b1587410-a08e-484c-8795-195a413d6374",
288+
"metadata": {},
289+
"outputs": [
290+
{
291+
"data": {
292+
"application/vnd.jupyter.widget-view+json": {
293+
"model_id": "a2e4d723405345e0a7bd7b005330d018",
294+
"version_major": 2,
295+
"version_minor": 0
296+
},
297+
"text/plain": [
298+
"RFBOutputContext()"
299+
]
300+
},
301+
"metadata": {},
302+
"output_type": "display_data"
303+
}
304+
],
305+
"source": [
306+
"dims = (256, 256, 5, 100)\n",
307+
"data = [np.random.rand(*dims) for i in range(4)]\n",
308+
"\n",
309+
"iw = ImageWidget(\n",
310+
" data=data, \n",
311+
" slider_dims=[\"t\", \"z\"], \n",
312+
" dims_order=\"xyzt\", # example of how you can set this for non-standard orders\n",
313+
" vmin_vmax_sliders=True,\n",
314+
" names=[\"zero\", \"one\", \"two\", \"three\"],\n",
315+
" # window_funcs={\"t\": (np.mean, 5)}, # window functions can be slow when indexing multiple dims\n",
316+
" cmap=\"gnuplot2\", \n",
317+
")"
318+
]
319+
},
320+
{
321+
"cell_type": "code",
322+
"execution_count": 17,
323+
"id": "3ccea6c6-9580-4720-bce8-a5507cf867a3",
324+
"metadata": {},
325+
"outputs": [
326+
{
327+
"data": {
328+
"application/vnd.jupyter.widget-view+json": {
329+
"model_id": "78a4ed0f59734124a7f3ee23e373e64a",
330+
"version_major": 2,
331+
"version_minor": 0
332+
},
333+
"text/plain": [
334+
"VBox(children=(JupyterWgpuCanvas(), IntSlider(value=0, description='dimension: t', max=99), IntSlider(value=0,…"
335+
]
336+
},
337+
"metadata": {},
338+
"output_type": "display_data"
339+
}
340+
],
341+
"source": [
342+
"iw.show()"
343+
]
344+
},
345+
{
346+
"cell_type": "markdown",
347+
"id": "2382809c-4c7d-4da4-9955-71d316dee46a",
348+
"metadata": {},
349+
"source": [
350+
"### window functions, can be slow when you have \"t\" and \"z\""
351+
]
352+
},
353+
{
354+
"cell_type": "code",
355+
"execution_count": 18,
356+
"id": "fd4433a9-2add-417c-a618-5891371efae0",
357+
"metadata": {},
358+
"outputs": [],
359+
"source": [
360+
"iw.window_funcs = {\"t\": (np.mean, 11)}"
361+
]
362+
},
363+
{
364+
"cell_type": "code",
365+
"execution_count": null,
366+
"id": "3090a7e2-558e-4975-82f4-6a67ae141900",
367+
"metadata": {},
368+
"outputs": [],
369+
"source": []
370+
}
371+
],
372+
"metadata": {
373+
"kernelspec": {
374+
"display_name": "Python 3 (ipykernel)",
375+
"language": "python",
376+
"name": "python3"
377+
},
378+
"language_info": {
379+
"codemirror_mode": {
380+
"name": "ipython",
381+
"version": 3
382+
},
383+
"file_extension": ".py",
384+
"mimetype": "text/x-python",
385+
"name": "python",
386+
"nbconvert_exporter": "python",
387+
"pygments_lexer": "ipython3",
388+
"version": "3.10.5"
389+
}
390+
},
391+
"nbformat": 4,
392+
"nbformat_minor": 5
393+
}

0 commit comments

Comments
 (0)