{
 "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",
    "import execnet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "STATS = []\n",
    "\n",
    "\n",
    "def run(channel, data=[]):\n",
    "    while not channel.isclosed():\n",
    "        index = channel.receive()\n",
    "\n",
    "        if index % 10 == 0:\n",
    "            print('Bootstrap {}'.format(\n",
    "                index))\n",
    "\n",
    "        total = 0\n",
    "\n",
    "        for x in data:\n",
    "            total += x\n",
    "\n",
    "        channel.send((total - data[index])/(len(data) - 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def serial(arr, n):\n",
    "    for i in range(n):\n",
    "        total = 0\n",
    "\n",
    "        for x in arr:\n",
    "            total += x\n",
    "\n",
    "        STATS.append((total - arr[i])/(len(arr) - 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def parallel(arr, n):\n",
    "    gw = execnet.makegateway()\n",
    "    channel = gw.remote_exec(run, data=arr.tolist())\n",
    "\n",
    "    for i in range(n):\n",
    "        channel.send(i)\n",
    "        STATS.append(channel.receive())\n",
    "\n",
    "    gw.exit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ws = dl.data.Weather.load()['WIND_SPEED'].dropna().values\n",
    "np.random.seed(33)\n",
    "parallel_times = ch12util.time_many(partial(parallel, ws))\n",
    "serial_times = ch12util.time_many(partial(serial, ws))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "ch12util.plot_times(plt.gca(), serial_times, parallel_times)\n",
    "plt.legend(loc='best')\n",
    "\n",
    "plt.figure()\n",
    "STATS = np.array(STATS)\n",
    "ch12util.plot_distro(plt.gca(), STATS, ws.mean())\n",
    "plt.title('Distribution of the Means')\n",
    "plt.legend(loc='best')"
   ]
  },
  {
   "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
}
