{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.sampledata.glucose import data\n",
    "from bokeh.plotting import figure, show, output_notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p1 = figure(x_axis_type=\"datetime\")\n",
    "\n",
    "p1.line(data.index, data['glucose'], color='red', legend='glucose')\n",
    "p1.line(data.index, data['isig'], color='blue', legend='isig')\n",
    "\n",
    "p1.title = \"Glucose Measurements\"\n",
    "\n",
    "show(p1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "day = data.ix['2010-10-06']\n",
    "highs = day[day['glucose'] > 180]\n",
    "lows = day[day['glucose'] < 80]\n",
    "\n",
    "p2 = figure(x_axis_type=\"datetime\")\n",
    "\n",
    "p2.line(day.index, day['glucose'], line_color=\"gray\", line_dash=\"4 4\", line_width=1, legend=\"glucose\")\n",
    "p2.scatter(highs.index, highs['glucose'], size=6, color='tomato', legend=\"high\")\n",
    "p2.scatter(lows.index, lows['glucose'], size=6, color='navy', legend=\"low\")\n",
    "\n",
    "p2.title = \"Glucose Range\"\n",
    "p2.xgrid.grid_line_color=None\n",
    "p2.ygrid.grid_line_alpha=0.5\n",
    "\n",
    "show(p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data['inrange'] = (data['glucose'] < 180) & (data['glucose'] > 80)\n",
    "window = 30.5*288 #288 is average number of samples in a month\n",
    "inrange = pd.rolling_sum(data.inrange, window)\n",
    "inrange = inrange.dropna()\n",
    "inrange = inrange/float(window)\n",
    "\n",
    "p3 = figure(x_axis_type=\"datetime\")\n",
    "\n",
    "p3.line(inrange.index, inrange, line_color=\"navy\")\n",
    "\n",
    "p3.title = \"Glucose In-Range Rolling Sum\"\n",
    "\n",
    "show(p3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
