{
 "metadata": {
  "name": "",
  "signature": "sha256:a44b7617fe69d587ddd96dae9189162a0492202c71e1d3555c8545874d8cdcc5"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "\n",
      "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": [
      "hold()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dates = data.index.to_series()\n",
      "\n",
      "figure(x_axis_type=\"datetime\", tools=\"pan,wheel_zoom,box_zoom,reset,previewsave\")\n",
      "\n",
      "line(dates, data['glucose'], color='red', legend='glucose')\n",
      "line(dates, data['isig'], color='blue', legend='isig')\n",
      "\n",
      "curplot().title = \"Glucose Measurements\"\n",
      "\n",
      "show()"
     ],
     "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",
      "figure(x_axis_type=\"datetime\", tools=\"pan,wheel_zoom,box_zoom,reset,previewsave\")\n",
      "\n",
      "line(day.index.to_series(), day['glucose'],\n",
      "    line_color=\"gray\", line_dash=\"4 4\", line_width=1, legend=\"glucose\")\n",
      "scatter(highs.index.to_series(), highs['glucose'], size=6, color='tomato', legend=\"high\")\n",
      "scatter(lows.index.to_series(), lows['glucose'], size=6, color='navy', legend=\"low\")\n",
      "\n",
      "curplot().title = \"Glucose Range\"\n",
      "xgrid()[0].grid_line_color=None\n",
      "ygrid()[0].grid_line_alpha=0.5\n",
      "\n",
      "show()"
     ],
     "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",
      "figure(x_axis_type=\"datetime\", tools=\"pan,wheel_zoom,box_zoom,reset,previewsave\")\n",
      "\n",
      "line(inrange.index.to_series(), inrange, line_color=\"navy\")\n",
      "\n",
      "curplot().title = \"Glucose In-Range Rolling Sum\"\n",
      "\n",
      "show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}