Skip to content

Instantly share code, notes, and snippets.

@cfriedline
Created July 14, 2015 19:14

Revisions

  1. cfriedline created this gist Jul 14, 2015.
    239 changes: 239 additions & 0 deletions contractions.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,239 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 211,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "%matplotlib inline\n",
    "import dill\n",
    "import pandas as pd\n",
    "import timestring\n",
    "import easy_date\n",
    "import plotly.plotly as py\n",
    "from plotly.graph_objs import *\n",
    "\n",
    "from datetime import datetime, timedelta"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 212,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "import json\n",
    "import gspread\n",
    "from oauth2client.client import SignedJwtAssertionCredentials\n",
    "\n",
    "json_key = json.load(open('/Users/chris/Downloads/My Project-6b7faed34510.json'))\n",
    "scope = ['https://spreadsheets.google.com/feeds']\n",
    "\n",
    "credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)\n",
    "\n",
    "gc = gspread.authorize(credentials)\n",
    "sheet = gc.open(\"contractions\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 213,
    "metadata": {
    "collapsed": false,
    "scrolled": false
    },
    "outputs": [],
    "source": [
    "df = pd.DataFrame(sheet.worksheet(\"Sheet1\").get_all_records())\n",
    "df = df.set_index('Time')\n",
    "df = df.convert_objects(convert_dates=True, convert_numeric=True)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 216,
    "metadata": {
    "collapsed": false,
    "scrolled": true
    },
    "outputs": [],
    "source": [
    "times = {}\n",
    "for time, data in df.iterrows():\n",
    " dt = datetime.strptime(time, '%m/%d/%Y %H:%M:%S')\n",
    " times[dt] = data[0]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 217,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "keys = sorted(times.keys())"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 218,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "x = []\n",
    "y = []\n",
    "ymin = 0\n",
    "ymax = 100\n",
    "for k in keys:\n",
    " x.append(k)\n",
    " y.append(times[k])\n",
    " "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 219,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "durations = []\n",
    "color = [['131', '181', '252'], ['251', '177', '219']]\n",
    "for i, k in enumerate(keys):\n",
    " if i % 2 == 0:\n",
    " c = color[0]\n",
    " else:\n",
    " c = color[1]\n",
    " lc = 'rgb(%s)' % ','.join(c)\n",
    " fc = 'rgba(%s, 0.6)' % ','.join(c)\n",
    " durations.append({\n",
    " 'type':'rect',\n",
    " 'x0': k,\n",
    " 'y0': ymin,\n",
    " 'x1': k+timedelta(seconds=times[k]),\n",
    " 'y1': ymax,\n",
    " 'fillcolor': fc,\n",
    " 'line':{\n",
    " 'color': lc,\n",
    " 'width': 0\n",
    " }\n",
    " })"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 220,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "trace0 = Scatter(x=x,y=y)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 221,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "data = Data([trace0])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 222,
    "metadata": {
    "collapsed": false
    },
    "outputs": [
    {
    "data": {
    "text/html": [
    "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~cfriedline/236.embed\" height=\"525\" width=\"100%\"></iframe>"
    ],
    "text/plain": [
    "<plotly.tools.PlotlyDisplay object>"
    ]
    },
    "execution_count": 222,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "layout = {\n",
    " 'title': 'Contractions',\n",
    " 'xaxis': {\n",
    " 'title': 'Time of day'\n",
    " \n",
    " },\n",
    " 'yaxis': {\n",
    " 'range': [ymin, ymax],\n",
    " 'title': 'Duration(s)'\n",
    " },\n",
    " 'shapes': durations\n",
    " \n",
    "}\n",
    "\n",
    "fig = {\n",
    " 'data': data,\n",
    " 'layout': layout\n",
    "}\n",
    "py.iplot(fig, validate=False)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 223,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "%matplotlib inline"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    }
    ],
    "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.10"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 0
    }