Skip to content
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

Baseline stream timeseries dev workflow #100

Merged
merged 1 commit into from
Jul 9, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1104765d-276b-488a-a44f-7aef27d7c520",
"metadata": {},
"source": [
"# Philipp's first demo"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a4afd67-94aa-4a6a-b506-0edfc3639064",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import numpy as np\n",
"import pandas as pd\n",
"import holoviews as hv\n",
"hv.extension('bokeh')\n",
"\n",
"buffer = hv.streams.Buffer(data=pd.DataFrame(np.random.randn(10, 10).cumsum(axis=1), columns=[chr(65+i) for i in range(10)]))\n",
"def out(data):\n",
" return hv.NdOverlay(\n",
" {i: hv.Curve(data, 'index', (chr(65+i), 'Amplitude')).opts(subcoordinate_y=True) for i in range(10)}\n",
" )\n",
"\n",
"hv.DynamicMap(out, streams=[buffer]).opts(legend_position='right', width=800)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a4a4534-8117-4d40-8a0b-325c004c31ad",
"metadata": {},
"outputs": [],
"source": [
"\n",
"for i in range(2, 1000):\n",
" buffer.event(data=pd.DataFrame(np.random.randn(10, 10).cumsum(axis=1), columns=[chr(65+i) for i in range(10)], index=np.arange(i*10, i*10+10)))\n",
" time.sleep(0.1)"
]
},
{
"cell_type": "markdown",
"id": "616eb087-d3d4-4d6a-9413-efed3d509679",
"metadata": {},
"source": [
"# Demetris' version, threaded stream, buttons, inspection plot\n",
"- Added an inspection plot (because I haven't figured out yet how to do a return to stream)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "caa75986-6d26-4bdc-8826-03396f3eabc5",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import numpy as np\n",
"import pandas as pd\n",
"import holoviews as hv\n",
"import panel as pn\n",
"from threading import Thread\n",
"\n",
"hv.extension('bokeh')\n",
"pn.extension()\n",
"\n",
"initial_data = pd.DataFrame(np.random.randn(10, 10).cumsum(axis=1), columns=[chr(65+i) for i in range(10)])\n",
"buffer = hv.streams.Buffer(data=initial_data, length=1000)\n",
"\n",
"accumulated_data = initial_data.copy()\n",
"\n",
"def create_stream_plot(data):\n",
" return hv.NdOverlay(\n",
" {i: hv.Curve(data, 'index', (chr(65+i), 'Amplitude')).opts(subcoordinate_y=True, tools=['hover']) for i in range(10)}, 'Channel'\n",
" )\n",
"dmap_stream = hv.DynamicMap(create_stream_plot, streams=[buffer]).opts(width=800, axiswise=True, title='Streaming')\n",
"\n",
"def create_inspection_plot(data):\n",
" return hv.NdOverlay(\n",
" {i: hv.Curve(data, 'index', (chr(65+i), 'Amplitude')).opts(subcoordinate_y=True, tools=['hover']) for i in range(10)}, 'Channel'\n",
" ).opts(width=800, axiswise=True, shared_axes=False, title='Inspection')\n",
"\n",
"inspection_plot = create_inspection_plot(initial_data)\n",
"\n",
"# globals to control streaming state\n",
"streaming = False\n",
"paused = False\n",
"counter = 2\n",
"\n",
"def stream_data():\n",
" global streaming, counter, accumulated_data\n",
" while streaming:\n",
" if not paused:\n",
" new_data = pd.DataFrame(np.random.randn(10, 10).cumsum(axis=1), columns=[chr(65+j) for j in range(10)], index=np.arange(counter*10, counter*10+10))\n",
" new_data = new_data.reset_index()\n",
" accumulated_data = pd.concat([accumulated_data, new_data])\n",
" buffer.event(data=new_data)\n",
" counter += 1\n",
" dmap_stream.opts(xlim=(0, counter*10))\n",
" time.sleep(0.07)\n",
"\n",
"\n",
"def start_stream(event):\n",
" global streaming, paused, counter, accumulated_data\n",
" if not streaming:\n",
" buffer.clear()\n",
" accumulated_data = pd.DataFrame(np.zeros((1, 10)), columns=[chr(65+i) for i in range(10)]).reset_index()\n",
" buffer.event(data=accumulated_data)\n",
" counter = 2 # Reset counter\n",
" streaming = True\n",
" paused = False\n",
" thread = Thread(target=stream_data)\n",
" thread.start()\n",
" else:\n",
" paused = False\n",
"\n",
"def pause_stream(event):\n",
" global paused\n",
" paused = True\n",
"\n",
"def stop_stream(event):\n",
" global streaming, paused, accumulated_data\n",
" streaming = False\n",
" paused = False\n",
" buffer.clear() # Clear the buffer when stopping\n",
" accumulated_data = pd.DataFrame(np.zeros((1, 10)), columns=[chr(65+i) for i in range(10)]).reset_index()\n",
" buffer.event(data=accumulated_data)\n",
"\n",
"def inspect_plot(event):\n",
" def update_inspection_plot():\n",
" global inspection_plot\n",
" inspection_data = accumulated_data.copy()\n",
" inspection_plot = create_inspection_plot(inspection_data)\n",
" right_plot_panel.object = inspection_plot\n",
"\n",
" inspect_thread = Thread(target=update_inspection_plot)\n",
" inspect_thread.start()\n",
"\n",
"# Buttons!\n",
"start_button = pn.widgets.Button(name='Start Streaming', button_type='primary')\n",
"pause_button = pn.widgets.Button(name='Pause Streaming', button_type='warning')\n",
"stop_button = pn.widgets.Button(name='Stop Streaming', button_type='danger')\n",
"inspect_button = pn.widgets.Button(name='Inspect', button_type='success')\n",
"\n",
"start_button.on_click(start_stream)\n",
"pause_button.on_click(pause_stream)\n",
"stop_button.on_click(stop_stream)\n",
"inspect_button.on_click(inspect_plot)\n",
"\n",
"right_plot_panel = pn.pane.HoloViews(inspection_plot)\n",
"layout = pn.Column(\n",
" pn.Row(start_button, pause_button, stop_button, inspect_button),\n",
" pn.Row(dmap_stream, right_plot_panel)\n",
")\n",
"\n",
"layout.servable()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b6d4c50-37ae-418d-b296-c7b1a6df4590",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
20 changes: 20 additions & 0 deletions workflows/stream_timeseries/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: neuro-stream-ts
channels:
- conda-forge
dependencies:
- python
- holoviews>=1.19.0
- bokeh>=3.3.1
- hvplot
- panel
- datashader
- numpy
- pandas
- xarray>=2024.5.0
- ipykernel
- jupyterlab
- jupyter_bokeh
- pip
- pip:
- holonote
- tsdownsample>=0.1.3