\n",
" \n",
"Click [here](https://mybinder.org/v2/gh/institutefordiseasemodeling/covasim/HEAD?urlpath=lab%2Ftree%2Fdocs%2Ftutorials%2Ftut_deployment.ipynb) to open an interactive version of this notebook.\n",
" \n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dask\n",
"\n",
"[Dask](https://dask.org/) is a powerful library for multiprocessing and \"scalable\" analytics. Using Dask (rather than the built-in `multiprocess`) for parallelization is _relatively_ straightforward:"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"```python\n",
"import dask\n",
"from dask.distributed import Client\n",
"import numpy as np\n",
"import covasim as cv\n",
"\n",
"\n",
"def run_sim(index, beta):\n",
" ''' Run a standard simulation '''\n",
" sim = cv.Sim(beta=beta, label=f'Sim {index}, beta={beta}')\n",
" sim.run()\n",
" return sim\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
"\n",
" # Run settings\n",
" n = 8\n",
" n_workers = 4\n",
" betas = np.sort(np.random.random(n))\n",
"\n",
" # Create and queue the Dask jobs\n",
" client = Client(n_workers=n_workers)\n",
" queued = []\n",
" for i,beta in enumerate(betas):\n",
" run = dask.delayed(run_sim)(i, beta)\n",
" queued.append(run)\n",
"\n",
" # Run and process the simulations\n",
" sims = list(dask.compute(*queued))\n",
" msim = cv.MultiSim(sims)\n",
" msim.plot(color_by_sim=True)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter/IPython\n",
"\n",
"Using Jupyter and [VoilĂ ](https://voila.readthedocs.io/), you can build a Covasim-based webapp in minutes. First, install the required dependencies:\n",
"\n",
"```bash\n",
"pip install jupyter jupyterlab jupyterhub ipympl voila \n",
"```\n",
"\n",
"Here is a very simple interactive webapp that runs a multisim (in parallel!) when the button is pressed, and displays the results:"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"```python\n",
"import numpy as np\n",
"import covasim as cv\n",
"import ipywidgets as widgets\n",
"\n",
"# Create the button and output area\n",
"button = widgets.Button(description='Run')\n",
"output = widgets.Output()\n",
"\n",
"@output.capture()\n",
"def run():\n",
" ''' Stochastically run a parallelized multisim '''\n",
" sim = cv.Sim(verbose=0, pop_size=20e3, n_days=100, rand_seed=np.random.randint(99))\n",
" msim = cv.MultiSim(sim)\n",
" msim.run(n_runs=4)\n",
" return msim.plot()\n",
"\n",
"def click(b):\n",
" ''' Rerun on click '''\n",
" output.clear_output(wait=True)\n",
" run()\n",
"\n",
"# Create and show the app\n",
"button.on_click(click)\n",
"app = widgets.VBox([button, output])\n",
"display(app)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you save this as e.g. `msim.ipynb`, then you can turn it into a web server simply by typing `voila msim.ipynb`."
]
}
],
"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.7"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}