{
 "metadata": {
  "name": "",
  "signature": "sha256:8e0e78775cf9ad8aaad4f0eabebcbbe6ebed47dd3bead355ba65b33677d3650c"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh.sampledata.glucose import data\n",
      "from bokeh.plotting import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "output_notebook()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}