{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import ch12util\n",
    "from functools import partial\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy.stats import skew\n",
    "import asyncio\n",
    "import time\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "STATS = []\n",
    "\n",
    "\n",
    "def resample(arr):\n",
    "    sample = ch12util.bootstrap(arr)\n",
    "    STATS.append((sample.mean(), sample.std(), skew(sample)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Bootstrapper():\n",
    "    def __init__(self, data, queue):\n",
    "        self.data = data\n",
    "        self.log = dl.log_api.conf_logger(__name__)\n",
    "        self.queue = queue\n",
    "\n",
    "    @asyncio.coroutine\n",
    "    def run(self):\n",
    "        while not self.queue.empty():\n",
    "            index = yield from self.queue.get()\n",
    "\n",
    "            if index % 10 == 0:\n",
    "                self.log.debug('Bootstrap {}'.format(\n",
    "                    index))\n",
    "\n",
    "            resample(self.data)\n",
    "            # simulates slow IO\n",
    "            yield from asyncio.sleep(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def serial(arr, n):\n",
    "    for i in range(n):\n",
    "        resample(arr)\n",
    "        # simulates slow IO\n",
    "        time.sleep(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def parallel(arr, n):\n",
    "    q = asyncio.Queue()\n",
    "\n",
    "    for i in range(n):\n",
    "        q.put_nowait(i)\n",
    "\n",
    "    bootstrapper = Bootstrapper(arr, q)\n",
    "    policy = asyncio.get_event_loop_policy()\n",
    "    policy.set_event_loop(policy.new_event_loop())\n",
    "    loop = asyncio.get_event_loop()\n",
    "\n",
    "    tasks = [asyncio.async(bootstrapper.run())\n",
    "             for i in range(n)]\n",
    "\n",
    "    loop.run_until_complete(asyncio.wait(tasks))\n",
    "    loop.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "pressure = dl.data.Weather.load()['PRESSURE'].dropna().values\n",
    "np.random.seed(33)\n",
    "parallel_times = ch12util.time_many(partial(parallel, pressure))\n",
    "serial_times = ch12util.time_many(partial(serial, pressure))\n",
    "\n",
    "dl.options.mimic_seaborn()\n",
    "ch12util.plot_times(plt.gca(), serial_times, parallel_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('accessing_asyncio')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "ch12util.plot_times(sp.ax, serial_times, parallel_times)\n",
    "\n",
    "STATS = np.array(STATS)\n",
    "ch12util.plot_distro(sp.next_ax(), STATS.T[0], pressure.mean())\n",
    "sp.label()\n",
    "\n",
    "ch12util.plot_distro(sp.next_ax(), STATS.T[1], pressure.std())\n",
    "sp.label()\n",
    "\n",
    "ch12util.plot_distro(sp.next_ax(), STATS.T[2], skew(pressure))\n",
    "sp.label()\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
