Created
February 28, 2023 17:20
-
-
Save manzt/616ab136c8e3b647e08ba0da59b158c6 to your computer and use it in GitHub Desktop.
jupyter-scatter derived state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3f71b5d6-2128-42d9-a444-53203ae5a42f", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"\n", | |
"import jscatter\n", | |
"import ipywidgets\n", | |
"import traitlets\n", | |
"\n", | |
"# Create some example data\n", | |
"\n", | |
"data = np.random.rand(500, 4)\n", | |
"df = pd.DataFrame(data, columns=['mass', 'speed', 'pval', 'group'])\n", | |
"df['group'] = df['group'].map(lambda c: chr(65 + round(c)), na_action=None)\n", | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2cd08802-0951-4092-a023-f0ffd82b2b70", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"# create a scatter plot\n", | |
"scatter = jscatter.Scatter(x=\"mass\", y=\"speed\", data=df)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c80934c7-896c-4e31-8f12-1b8bdfecdfd4", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"# Wire up the Scatter plot with additional \"pieces\" of derived state\n", | |
"\n", | |
"# create a new piece of state for the Jupyter Scatter widget\n", | |
"scatter.widget.add_traits(\n", | |
" selection_df=traitlets.Any(allow_none=True),\n", | |
" selection_summary=traitlets.Any(allow_none=True),\n", | |
")\n", | |
"\n", | |
"# specify a directional link such that any time `selection` changes (the indices)\n", | |
"# we update this summary\n", | |
"traitlets.dlink(\n", | |
" source=(scatter.widget, \"selection\"),\n", | |
" target=(scatter.widget, \"selection_df\"),\n", | |
" transform=lambda sel: scatter._data.iloc[sel]\n", | |
")\n", | |
"\n", | |
"# create a summary function (could return anything!)\n", | |
"def summarize(df: pd.DataFrame) -> ...:\n", | |
" return df.describe()\n", | |
"\n", | |
"traitlets.dlink(\n", | |
" source=(scatter.widget, \"selection_df\"),\n", | |
" target=(scatter.widget, \"selection_summary\"),\n", | |
" transform=summarize,\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "50f4f9de-6c5b-4b5a-b8e1-55086c5159af", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"# Create an observer for the \"selection_summary_state\"\n", | |
"\n", | |
"output = ipywidgets.Output()\n", | |
"\n", | |
"@output.capture(clear_output=True)\n", | |
"def _on_summary_change(change):\n", | |
" display(change.new)\n", | |
" \n", | |
"scatter.widget.observe(_on_summary_change, names=\"selection_summary\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "95740cfb-6d59-4385-9ad2-6056d62d21cc", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"# Create the joint visualization\n", | |
"\n", | |
"ipywidgets.HBox([scatter.show(), output])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e595a0dd-388c-49ca-b3c3-094ac804c02b", | |
"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.11.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment