Created
January 30, 2020 14:41
-
-
Save pfaion/115e13e822b308f0896b59bc7971d676 to your computer and use it in GitHub Desktop.
Pupil Network Monitor
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": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from IPython.display import display, clear_output\n", | |
"import pandas as pd\n", | |
"import zmq\n", | |
"from msgpack import loads\n", | |
"import time\n", | |
"\n", | |
"def update(*args):\n", | |
" clear_output(wait=True)\n", | |
" display(*args)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<style type=\"text/css\" >\n", | |
" #T_543b6be4_436e_11ea_bc87_b42e990f985drow0_col0 {\n", | |
" background-color: #008066;\n", | |
" color: #f1f1f1;\n", | |
" } #T_543b6be4_436e_11ea_bc87_b42e990f985drow1_col0 {\n", | |
" background-color: #008066;\n", | |
" color: #f1f1f1;\n", | |
" } #T_543b6be4_436e_11ea_bc87_b42e990f985drow2_col0 {\n", | |
" background-color: #008066;\n", | |
" color: #f1f1f1;\n", | |
" } #T_543b6be4_436e_11ea_bc87_b42e990f985drow3_col0 {\n", | |
" background-color: #158a66;\n", | |
" color: #000000;\n", | |
" } #T_543b6be4_436e_11ea_bc87_b42e990f985drow4_col0 {\n", | |
" background-color: #168a66;\n", | |
" color: #000000;\n", | |
" } #T_543b6be4_436e_11ea_bc87_b42e990f985drow5_col0 {\n", | |
" background-color: #ffff66;\n", | |
" color: #000000;\n", | |
" } #T_543b6be4_436e_11ea_bc87_b42e990f985drow6_col0 {\n", | |
" background-color: #ffff66;\n", | |
" color: #000000;\n", | |
" }</style><table id=\"T_543b6be4_436e_11ea_bc87_b42e990f985d\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >last</th> <th class=\"col_heading level0 col1\" >count</th> </tr></thead><tbody>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row0\" class=\"row_heading level0 row0\" >pupil.0</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow0_col0\" class=\"data row0 col0\" >0.00</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow0_col1\" class=\"data row0 col1\" >10662</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row1\" class=\"row_heading level0 row1\" >pupil.1</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow1_col0\" class=\"data row1 col0\" >0.00</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow1_col1\" class=\"data row1 col1\" >10672</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row2\" class=\"row_heading level0 row2\" >gaze.2d.01.</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow2_col0\" class=\"data row2 col0\" >0.01</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow2_col1\" class=\"data row2 col1\" >19186</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row3\" class=\"row_heading level0 row3\" >gaze.2d.1.</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow3_col0\" class=\"data row3 col0\" >0.79</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow3_col1\" class=\"data row3 col1\" >1015</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row4\" class=\"row_heading level0 row4\" >gaze.2d.0.</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow4_col0\" class=\"data row4 col0\" >0.82</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow4_col1\" class=\"data row4 col1\" >1133</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row5\" class=\"row_heading level0 row5\" >logging.debug</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow5_col0\" class=\"data row5 col0\" >9.42</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow5_col1\" class=\"data row5 col1\" >37</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th id=\"T_543b6be4_436e_11ea_bc87_b42e990f985dlevel0_row6\" class=\"row_heading level0 row6\" >notify.backend.ndsi_source_found</th>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow6_col0\" class=\"data row6 col0\" >9.42</td>\n", | |
" <td id=\"T_543b6be4_436e_11ea_bc87_b42e990f985drow6_col1\" class=\"data row6 col1\" >10</td>\n", | |
" </tr>\n", | |
" </tbody></table>" | |
], | |
"text/plain": [ | |
"<pandas.io.formats.style.Styler at 0x2360341e198>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"context = zmq.Context()\n", | |
"addr = \"127.0.0.1\"\n", | |
"port = \"50020\"\n", | |
"req = context.socket(zmq.REQ)\n", | |
"req.connect(f\"tcp://{addr}:{port}\")\n", | |
"req.send_string('SUB_PORT')\n", | |
"sub_port = req.recv_string()\n", | |
"\n", | |
"sub = context.socket(zmq.SUB)\n", | |
"sub.connect(f\"tcp://{addr}:{sub_port}\")\n", | |
"sub.setsockopt_string(zmq.SUBSCRIBE, '')\n", | |
"\n", | |
"wait = 1\n", | |
"\n", | |
"data = pd.DataFrame(columns=(\"last\", \"count\"))\n", | |
"try:\n", | |
" t = time.time()\n", | |
" while True:\n", | |
" topic = sub.recv_string()\n", | |
" msg = sub.recv()\n", | |
" msg = loads(msg, encoding='utf-8')\n", | |
" now = time.time()\n", | |
" if topic not in data.index:\n", | |
" data.loc[topic] = {\"last\": now, \"count\": 1}\n", | |
" data.loc[topic, \"last\"] = now\n", | |
" data.loc[topic, \"count\"] += 1\n", | |
" \n", | |
" if now - t > wait:\n", | |
" update(\n", | |
" data.assign(last=now - data[\"last\"])\n", | |
" .sort_values(by=[\"last\"])\n", | |
" .style\n", | |
" .format({\"last\": \"{:.2f}\", \"count\": int})\n", | |
" .background_gradient(cmap=\"summer\", axis=\"rows\", subset=[\"last\"])\n", | |
" )\n", | |
" t = now\n", | |
"except KeyboardInterrupt:\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.6.8 64-bit", | |
"language": "python", | |
"name": "python36864bit78f0a2013f8f4e8daae46f0b4cbd86a5" | |
}, | |
"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.6.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment