{
 "metadata": {
  "name": "",
  "signature": "sha256:58188a6da505e6bc689a072c14ab60ed4e52b4078dde58566b9a5b1fb0aa2ae6"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "--------------------------------------------------"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Experiment log explorer"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%matplotlib inline\n",
      "import cPickle as pickle\n",
      "from IPython.html.widgets import interact, IntSliderWidget, DropdownWidget\n",
      "from IPython.display import display, HTML\n",
      "\n",
      "import matplotlib.pyplot as plt\n",
      "import pandas as pd\n",
      "import numpy as np\n",
      "from path_helpers import path\n",
      "\n",
      "# Load experiment log data into `log` variable.\n",
      "log_root = path('.').abspath()\n",
      "log_path = log_root.joinpath('data')\n",
      "log = log_path.pickle_load()\n",
      "print 'Read log data from: %s' % log_path\n",
      "\n",
      "# Create widgets that we'll display below to navigate log data.\n",
      "def index_selected(name, index):\n",
      "    dropdown.values = dict([(k, pickle.loads(v))\n",
      "                            for k, v in log.data[index].iteritems()])\n",
      "    \n",
      "index_widget = IntSliderWidget(min=0, max=len(log.data) - 1)\n",
      "index_widget.value = index_widget.min\n",
      "# Update the plugin dropdown list whenever log index is changed.\n",
      "index_widget.on_trait_change(index_selected, 'value')\n",
      "\n",
      "dropdown = DropdownWidget(label='Plugin')\n",
      "index_selected('value', index_widget.value)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[interfaces] <_MainThread(MainThread, started 139882913400640)>\n",
        "Read log data from: /home/christian/.microdrop/devices/DMF-90-pin-array/logs/551/data\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "--------------------------------------------------"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Explore"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "`log.data` is a list containing log data for each step.  Note that the first\n",
      "entry (i.e., at index 0) does not correspond to a step, but instead contains\n",
      "details about hardware and software versions used for the experiment.\n",
      "\n",
      "Each log entry contains a dictionary mapping each plugin name to its corresponding\n",
      "data for the step.\n",
      "\n",
      "The data for each plugin is [serialized][1] using the Python [`pickle`][2] library.\n",
      "To load the step data for plugin, we can \"unpickle\" the corresponding dictionary\n",
      "entry.\n",
      "\n",
      "For example, the interactive demo below can be used to load and display the data\n",
      "for each log entry by plugin name.  Drag the slider to change the entry index.\n",
      "\n",
      "[1]: http://en.wikipedia.org/wiki/Serialization\n",
      "[2]: https://docs.python.org/2/library/pickle.html"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "import pprint\n",
      "\n",
      "\n",
      "def summarize(data):\n",
      "    summary = pprint.pformat(data)\n",
      "    if len(summary) > 1000:\n",
      "        summary = summary[:1000] + '...'\n",
      "    return summary\n",
      "    \n",
      "\n",
      "def display_plugin_names(index=0):\n",
      "    data_i = log.data[index]\n",
      "    for plugin_name, plugin_pickled_data in data_i.iteritems():\n",
      "        display(HTML('<h2>%s</h2>' % plugin_name))\n",
      "        plugin_data = pickle.loads(plugin_pickled_data)\n",
      "        print summarize(plugin_data)\n",
      "    \n",
      "    \n",
      "interact_function = interact(display_plugin_names, index=(0, len(log.data) - 1))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<h2>core</h2>"
       ],
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "<IPython.core.display.HTML at 0x7f38dcfb7e50>"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "{'control board hardware version': '2.1',\n",
        " 'control board name': 'Arduino DMF Controller',\n",
        " 'control board serial number': 2,\n",
        " 'control board software version': '1.0.65-async-feedback',\n",
        " 'i2c devices': {10: '?',\n",
        "                 32: 'HV Switching Board v2.1 (Firmware v0.2.54-pwm, S/N 4294967295)'},\n",
        " 'start time': 1431358960.857258}\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "--------------------------------------------------"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Select"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We saw above how to navigate through summaries of the experiment log data, but\n",
      "we can also select data for further analysis.\n",
      "\n",
      "Using the widgets below, the data for a particular plugin at a chosen log index\n",
      "may be chosen.  The data is then available through the `value` attribute of the\n",
      "`dropdown` variable."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def display_data(plugin_data=None):\n",
      "    print summarize(plugin_data)\n",
      "    return plugin_data\n",
      "\n",
      "display(index_widget)\n",
      "interact(display_data, plugin_data=dropdown)\n",
      "pass"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "{'control board hardware version': '2.1',\n",
        " 'control board name': 'Arduino DMF Controller',\n",
        " 'control board serial number': 2,\n",
        " 'control board software version': '1.0.65-async-feedback',\n",
        " 'i2c devices': {10: '?',\n",
        "                 32: 'HV Switching Board v2.1 (Firmware v0.2.54-pwm, S/N 4294967295)'},\n",
        " 'start time': 1431358960.857258}\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dropdown.value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "{'control board hardware version': '2.1',\n",
        " 'control board name': 'Arduino DMF Controller',\n",
        " 'control board serial number': 2,\n",
        " 'control board software version': '1.0.65-async-feedback',\n",
        " 'i2c devices': {10: '?',\n",
        "  32: 'HV Switching Board v2.1 (Firmware v0.2.54-pwm, S/N 4294967295)'},\n",
        " 'start time': 1431358960.857258}"
       ]
      }
     ],
     "prompt_number": 4
    }
   ],
   "metadata": {}
  }
 ]
}