Skip to content

Interactivity attempt 4 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 29, 2022
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
377 changes: 0 additions & 377 deletions examples/collection.ipynb

This file was deleted.

212 changes: 212 additions & 0 deletions examples/line_collection_event.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f02f7349-ac1a-49b7-b304-d5b701723e0f",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d9f54448-7718-4212-ac6d-163a2d3be146",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from fastplotlib.graphics import LineGraphic, LineCollection, ImageGraphic\n",
"from fastplotlib.plot import Plot\n",
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b683c6d1-d926-43e3-a221-4ec6450e3677",
"metadata": {},
"outputs": [],
"source": [
"contours = pickle.load(open(\"/home/caitlin/Downloads/contours.pickle\", \"rb\"))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "26ced005-c52f-4696-903d-a6974ae6cefc",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2bc75fa52d484cd1b03006a6530f10b3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"RFBOutputContext()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0\n",
"\n"
]
}
],
"source": [
"plot = Plot()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1aea0a61-db63-41e1-b330-5a922da4bac5",
"metadata": {},
"outputs": [],
"source": [
"line_collection = LineCollection(contours, cmap=\"Oranges\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "310fd5b3-49f2-4dbb-831e-cb9f369d58c8",
"metadata": {},
"outputs": [],
"source": [
"plot.add_graphic(line_collection)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "661a5991-0de3-44d7-a626-2ae72704dcec",
"metadata": {},
"outputs": [],
"source": [
"image = ImageGraphic(data=np.ones((180, 180)))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "816f6382-f4ea-4cd4-b9f3-2dc5c232b0a5",
"metadata": {},
"outputs": [],
"source": [
"plot.add_graphic(image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8187fd25-18e1-451f-b2fe-8cd2e7785c8b",
"metadata": {},
"outputs": [],
"source": [
"plot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5ddd3c4-84e2-44a3-a14f-74871aa0bb8f",
"metadata": {},
"outputs": [],
"source": [
"black = np.array([0.0, 0.0, 0.0, 1.0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ac64f1c-21e0-4c21-b968-3953e7858848",
"metadata": {},
"outputs": [],
"source": [
"from typing import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0d0c971-aac9-4c77-b88c-de9d79c7d74e",
"metadata": {},
"outputs": [],
"source": [
"def callback_function(source: Any, target: Any, event, new_data: Any):\n",
" # calculate coms of line collection\n",
" indices = np.array(event.pick_info[\"index\"])\n",
" \n",
" coms = list()\n",
"\n",
" for contour in target.items:\n",
" coors = contour.data.feature_data[~np.isnan(contour.data.feature_data).any(axis=1)]\n",
" com = coors.mean(axis=0)\n",
" coms.append(com)\n",
"\n",
" # euclidean distance to find closest index of com \n",
" indices = np.append(indices, [0])\n",
" \n",
" ix = np.linalg.norm((coms - indices), axis=1).argsort()[0]\n",
" \n",
" ix = int(ix)\n",
" \n",
" print(ix)\n",
" \n",
" target._set_feature(feature=\"colors\", new_data=new_data, indices=ix)\n",
" \n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a46d148-3007-4c7a-bf2b-91057eba855d",
"metadata": {},
"outputs": [],
"source": [
"image.link(event_type=\"click\", target=line_collection, feature=\"colors\", new_data=black, callback_function=callback_function)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8040456e-24a5-423b-8822-99a20e7ea470",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
225 changes: 225 additions & 0 deletions examples/single_contour_event.ipynb

Large diffs are not rendered by default.

Loading