{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import time\n",
      "import datetime\n",
      "\n",
      "import collections\n",
      "\n",
      "from bokeh.plotting import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "output_notebook(name=\"default\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "found config for default\n",
        "{}\n",
        "loading it!\n",
        "if you don't wish to load this config, please pass load_from_config=False\n"
       ]
      },
      {
       "html": [
        "<p>Connecting notebook to document \"IPython Session at Fri Feb 14 20:42:10 2014\" at server http://localhost:5006/</p>"
       ],
       "metadata": {},
       "output_type": "display_data"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "TS_MULT_us = 1e6\n",
      "UNIX_EPOCH = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime\n",
      "\n",
      "def int2dt(ts, ts_mult=TS_MULT_us):\n",
      "    \"\"\"Convert timestamp (integer) to datetime\"\"\"\n",
      "    return(datetime.datetime.utcfromtimestamp(float(ts)/ts_mult))\n",
      "        \n",
      "def td2int(td, ts_mult=TS_MULT_us):\n",
      "    \"\"\"Convert timedelta to integer\"\"\"\n",
      "    return(int(td.total_seconds()*ts_mult))\n",
      "        \n",
      "def dt2int(dt, ts_mult=TS_MULT_us):\n",
      "    \"\"\"Convert datetime to integer\"\"\"\n",
      "    delta = dt - UNIX_EPOCH\n",
      "    return(int(delta.total_seconds()*ts_mult))\n",
      "        \n",
      "def int_from_last_sample(dt, td):\n",
      "    return(dt2int(dt) - dt2int(dt) % td2int(td))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "TS_MULT = 1e3\n",
      "td_delay = datetime.timedelta(seconds=0.5)\n",
      "delay_s = td_delay.total_seconds()\n",
      "delay_int = td2int(td_delay, TS_MULT)\n",
      "\n",
      "value = 1000 # initial value\n",
      "N = 100 # number of elements into circular buffer\n",
      "\n",
      "buff = collections.deque([value]*N, maxlen=N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "t_now = datetime.datetime.utcnow()\n",
      "ts_now = dt2int(t_now, TS_MULT)\n",
      "t = collections.deque(np.arange(ts_now-N*delay_int, ts_now, delay_int), maxlen=N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "line(list(t), list(buff), color=\"#0000FF\", tools=\"pan,wheel_zoom,box_zoom,reset,previewsave\",\n",
      "    name=\"line_example\", x_axis_type=\"datetime\"\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "<bokeh.objects.Plot at 0x10b731cd0>"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "UNIX_EPOCH = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "renderer = [r for r in curplot().renderers if isinstance(r, Glyph)][0]\n",
      "ds = renderer.data_source\n",
      "show()\n",
      "while(True):\n",
      "    ts_now = dt2int(datetime.datetime.utcnow(), 1e3)\n",
      "    t.append(ts_now)\n",
      "    ds.data['x'] = list(t)\n",
      "\n",
      "    value += np.random.uniform(-1, 1)\n",
      "    buff.append(value)\n",
      "    ds.data['y'] = list(buff)\n",
      "\n",
      "    ds._dirty = True\n",
      "    session().store_obj(ds)\n",
      "    \n",
      "    time.sleep(delay_s)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<script src=\"http://localhost:5006//bokeh/embed.js\" bokeh_plottype=\"serverconn\"\n",
        "        bokeh_docid=\"7269810c-1d6e-4d5d-999e-5d8acee1c8eb\" bokeh_ws_conn_string=\"ws://localhost:5006/bokeh/sub\"\n",
        "        bokeh_docapikey=\"504b2afe-1a46-4d5f-b47b-3dd7163441b5\" bokeh_root_url=\"http://localhost:5006/\"\n",
        "        bokeh_modelid=\"line_example\" bokeh_modeltype=\"Plot\" async=\"true\"></script>\n",
        "        "
       ],
       "metadata": {},
       "output_type": "display_data"
      },
      {
       "ename": "KeyboardInterrupt",
       "evalue": "",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-8-053f91ec5a36>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     14\u001b[0m     \u001b[0msession\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstore_obj\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m     \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdelay_s\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}