Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
391 changes: 391 additions & 0 deletions examples/event_handler.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,391 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a0b458c1-53c6-43d9-a72a-41f51bfe493d",
"metadata": {},
"source": [
"### notebook for learning event handler system"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "638b6a65-6d78-459c-ac88-35312233d22a",
"metadata": {},
"outputs": [],
"source": [
"from mesmerize_core import *\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"import pandas as pd\n",
"from fastplotlib import GridPlot, Image, Plot, Line, Heatmap\n",
"from scipy.spatial import distance\n",
"from ipywidgets.widgets import IntSlider, VBox\n",
"import pygfx"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "80938adc-1775-4751-94dd-89fb94ed673a",
"metadata": {},
"outputs": [],
"source": [
"class Contour_Selection():\n",
" def __init__(\n",
" self,\n",
" gp: GridPlot,\n",
" coms,\n",
" ):\n",
" self.gp = gp\n",
" self.heatmap = self.gp.subplots[0, 1].scene.children[0]\n",
" self.image = None\n",
" self._contour_index = None \n",
" \n",
" for child in self.gp.subplots[0, 0].scene.children:\n",
" if isinstance(child, pygfx.Image):\n",
" self.image = child\n",
" break;\n",
" if self.image == None:\n",
" raise ValueError(\"No image found!\")\n",
" self.coms = np.array(coms)\n",
" \n",
" self.image.add_event_handler(self.event_handler, \"click\")\n",
" \n",
" # first need to add event handler for when contour is clicked on\n",
" # should also trigger highlighting in heatmap\n",
" def event_handler(self, event):\n",
" if self._contour_index is not None:\n",
" self.remove_highlight()\n",
" self.add_highlight(event)\n",
" else:\n",
" self.add_highlight(event)\n",
" \n",
" def add_highlight(self, event):\n",
" click_location = np.array(event.pick_info[\"index\"])\n",
" self._contour_index = np.linalg.norm((self.coms - click_location), axis=1).argsort()[0] + 1\n",
" line = self.gp.subplots[0, 0].scene.children[self._contour_index]\n",
" line.geometry.colors.data[:] = np.array([1.0, 1.0, 1.0, 1.0]) \n",
" line.geometry.colors.update_range()\n",
" #self.heatmap.add_highlight(self._contour_index)\n",
" \n",
" def remove_highlight(self):\n",
" # change color of highlighted index back to normal\n",
" line = self.gp.subplots[0, 0].scene.children[self._contour_index]\n",
" line.geometry.colors.data[:] = np.array([1., 0., 0., 0.7]) \n",
" line.geometry.colors.update_range()\n",
" # for h in self.heatmap._highlights:\n",
" # self.heatmap.remove_highlight(h)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7b3129e6-5d82-4f39-b887-e584421c3a74",
"metadata": {},
"outputs": [],
"source": [
"set_parent_raw_data_path(\"/home/kushal/caiman_data/\")\n",
"\n",
"batch_path = \"/home/clewis7/caiman_data/cnmf_practice/batch.pickle\"\n",
"\n",
"movie_path = \"/home/kushal/caiman_data/example_movies/Sue_2x_3000_40_-46.tif\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "776c6dd8-cf07-47ee-bcc4-5fe84bc030f5",
"metadata": {},
"outputs": [],
"source": [
"df = load_batch(batch_path)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b22fd79b-5a7a-48fb-898c-4d3f3a34c118",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>algo</th>\n",
" <th>item_name</th>\n",
" <th>input_movie_path</th>\n",
" <th>params</th>\n",
" <th>outputs</th>\n",
" <th>comments</th>\n",
" <th>uuid</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>mcorr</td>\n",
" <td>my_movie</td>\n",
" <td>example_movies/Sue_2x_3000_40_-46.tif</td>\n",
" <td>{'main': {'max_shifts': (24, 24), 'strides': (...</td>\n",
" <td>{'mean-projection-path': 1ed8feb3-9fc8-4a78-8f...</td>\n",
" <td>None</td>\n",
" <td>1ed8feb3-9fc8-4a78-8f6d-164620822016</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>cnmf</td>\n",
" <td>my_movie</td>\n",
" <td>1ed8feb3-9fc8-4a78-8f6d-164620822016/1ed8feb3-...</td>\n",
" <td>{'main': {'fr': 30, 'p': 1, 'nb': 2, 'merge_th...</td>\n",
" <td>{'mean-projection-path': 5f4e3e27-ac1f-4ede-90...</td>\n",
" <td>None</td>\n",
" <td>5f4e3e27-ac1f-4ede-903b-be43bd81fddc</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" algo item_name input_movie_path \\\n",
"0 mcorr my_movie example_movies/Sue_2x_3000_40_-46.tif \n",
"1 cnmf my_movie 1ed8feb3-9fc8-4a78-8f6d-164620822016/1ed8feb3-... \n",
"\n",
" params \\\n",
"0 {'main': {'max_shifts': (24, 24), 'strides': (... \n",
"1 {'main': {'fr': 30, 'p': 1, 'nb': 2, 'merge_th... \n",
"\n",
" outputs comments \\\n",
"0 {'mean-projection-path': 1ed8feb3-9fc8-4a78-8f... None \n",
"1 {'mean-projection-path': 5f4e3e27-ac1f-4ede-90... None \n",
"\n",
" uuid \n",
"0 1ed8feb3-9fc8-4a78-8f6d-164620822016 \n",
"1 5f4e3e27-ac1f-4ede-903b-be43bd81fddc "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "03045957-837e-49e3-83aa-3e069af977a1",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3b283ef0f3084d828f80fd3a6cb25413",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"RFBOutputContext()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8b104ac5f10e4d53866953a4401593df",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(JupyterWgpuCanvas(), IntSlider(value=0, max=2999)))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gp = GridPlot(shape=(1,2))\n",
"\n",
"contours, coms = df.iloc[-1].cnmf.get_contours()\n",
"movie = df.iloc[-1].cnmf.get_input_memmap()\n",
"temporal = df.iloc[-1].cnmf.get_temporal()\n",
"\n",
"contour_graphic = Image(movie[0].T, cmap=\"gnuplot2\")\n",
"heatmap = Heatmap(data=temporal[:,0:1000], cmap=\"jet\")\n",
"\n",
"slider = IntSlider(value=0, min=0, max=movie.shape[0] - 1, step=1)\n",
"\n",
"gp.subplots[0,0].add_graphic(contour_graphic)\n",
"gp.subplots[0,1].add_graphic(heatmap)\n",
"\n",
"for coor in contours:\n",
" # line data has to be 3D\n",
" zs = np.ones(coor.shape[0]) # this will place it above the image graphic\n",
" c3d = [coor[:, 0], coor[:, 1], zs]\n",
" coors_3d = np.dstack(c3d)[0]\n",
"\n",
" # make all the lines red, [R, G, B, A] array\n",
" colors = np.vstack([[1., 0., 0., 0.7]] * coors_3d.shape[0])\n",
" line_graphic = Line(data=coors_3d, colors=colors, zlevel=1)\n",
" gp.subplots[0, 0].add_graphic(line_graphic)\n",
"\n",
"previous_slider_value = 0\n",
"def update_frame(): \n",
" if slider.value == previous_slider_value:\n",
" return\n",
" contour_graphic.update_data(data=movie[slider.value].T)\n",
"\n",
"gp.add_animations([update_frame])\n",
"\n",
"VBox([gp.show(), slider])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2bf4a1ea-3559-4846-bc32-4dddaca2d470",
"metadata": {},
"outputs": [],
"source": [
"contour_selection = Contour_Selection(gp=gp, coms=coms)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5ecdbdea-4e77-462f-a18d-5ed9b45faada",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(155, 3000)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temporal.shape"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "51b3feea-af91-4158-97a2-8dbadd2480b5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(coms[0])"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "3b2011f4-046b-4396-a592-81a5128d2482",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([7.12861818, 9.84114483])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coms[0]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "c5791bf4-a116-4093-bce1-eee7bc25221c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<fastplotlib.subplot.Subplot at 0x7f5b5d218430>"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gp.subplots[0, 1]"
]
},
{
"cell_type": "markdown",
"id": "ae9cce16-370f-44d2-b532-dc442cce379d",
"metadata": {},
"source": [
"next steps:\n",
" clicking on a contour should highlight it and the heatmap row"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading