Skip to content

Commit

Permalink
offline inference
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbouteiller committed Nov 27, 2021
1 parent 012ef6a commit 18ea528
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 1,089 deletions.
180 changes: 180 additions & 0 deletions notebooks/offline_inference.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# library imports:\n",
"\n",
"import pandas as pd\n",
"from pathlib import Path\n",
"import portiloop_software\n",
"import torch\n",
"from portiloop_software import run_offline_unlabelled, get_final_model_config_dict\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# path to the portiloop software package:\n",
"\n",
"path_software = Path(portiloop_software.__file__).parent.absolute()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# path to the folder containing pre-trained models:\n",
"\n",
"path_experiments = path_software / 'experiments'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# path to the non-annotated data, change this to your own for custom applications:\n",
"\n",
"path_data = path_software / 'dataset' / 'example_data_not_annotated.txt'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load non-annotated data into a pytorch Tensor:\n",
"\n",
"data = torch.Tensor(pd.read_csv(path_data, header=None).values).squeeze()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"The data has been loaded as a torch Tensor of {len(data)} data points:\\n{data}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# configuration dictionary of the model:\n",
"\n",
"config_dict = get_final_model_config_dict()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run offline inference (on all data points):\n",
"\n",
"output, true_idx = run_offline_unlabelled(config_dict=config_dict,\n",
" path_experiments=path_experiments,\n",
" unlabelled_segment=data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# convert result for visualization:\n",
"\n",
"out = output.numpy() - 5\n",
"idx = true_idx.numpy()\n",
"sig = data[idx].numpy()\n",
"idx_s = idx / config_dict['fe']\n",
"last_tick = idx_s[-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plot results\n",
"\n",
"start = 115\n",
"duration = 10\n",
"THRESHOLD = 0.5\n",
"\n",
"stop = start + duration\n",
"\n",
"def y1axtoy2ax(y):\n",
" res = y - 5\n",
" return res\n",
"\n",
"def y1axtoy2ax(y):\n",
" res = y + 5\n",
" return res\n",
"\n",
"plt.rcParams['figure.figsize'] = [15, 4]\n",
"fig, ax = plt.subplots()\n",
"ax.plot(idx_s, sig, label=\"Signal\")\n",
"ax.plot(idx_s, out, label=\"ANN output\")\n",
"ax.hlines(-5 + THRESHOLD, 0, last_tick, colors='lightgrey', label=\"Threshold\", linestyles=':')\n",
"ax.set_ylim((-5,5))\n",
"ax.set_xlim((start, stop))\n",
"ax.set_xlabel(\"Time (s)\")\n",
"ax.set_ylabel(\"Signal (arb. unit)\")\n",
"secy = ax.secondary_yaxis('right', functions=(y1axtoy2ax, y1axtoy2ax))\n",
"secy.set_yticks([0,1])\n",
"ax.axes.yaxis.set_visible(False)\n",
"ax.set_title(f\"Threshold {THRESHOLD}\")\n",
"plt.tight_layout()\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 18ea528

Please sign in to comment.