IRC-SPHERE/HyperStream

View on GitHub
examples/tutorial_02.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img style=\"float: right;\" src=\"images/hyperstream.svg\">\n",
    "\n",
    "# HyperStream Tutorial 2: Your own tools\n",
    "\n",
    "HyperStream has a set of predefined tools in hyperstream.tools. However, it is possible to define your own tools and factors. In this tutorial, we show how to create a simple plugin that reads a CSV file. In this tutorial, we already created the tool and made all the configurations necessary for it to work. We will describe how this one was created, and how can you create a new one.\n",
    "\n",
    "## Creating a plugin tool to read csv files\n",
    "\n",
    "### 1. Create a folder in plugins\n",
    "\n",
    "First of all, we need to create a new folder to contain our new tool. The new folder needs to be in the folder __plugins__, in this example __plugins/example/tools/csv_reader/__. We need to create an **\\__init\\__.py** file in every subfolder to be able to treat all the folders as a Python package.\n",
    "\n",
    "    plugins/\n",
    "        |- __init__.py\n",
    "        |- one_plugin/\n",
    "        |   |- __init__.py\n",
    "        |   |- tools/\n",
    "        |       |- __init__.py\n",
    "        |       |- tool_a\n",
    "        |           |- __init__.py\n",
    "        |           |- 2017-06-20_v0.0.1.py\n",
    "        |           |- 2017-06-22_v0.0.3.py\n",
    "        |- another_plugin/\n",
    "            |- __init__.py\n",
    "            |- tools/\n",
    "                |- tool_b/\n",
    "                |   |- __init__.py\n",
    "                |   |- 2017-06-20_v0.0.1.py\n",
    "                |   |- 2017-06-22_v0.1.0.py\n",
    "                |- tool_c/\n",
    "                    |- __init__.py\n",
    "                    |- 2017-06-20_v0.0.2.py\n",
    "\n",
    "### 2. Write the plugin in Python\n",
    "\n",
    "Then, we need to create a new Python file following the name convention &lt;year>-&lt;month\\>-&lt;day\\>_v&lt;version\\>.&lt;subversion\\>.&lt;subsubversion\\>.py. In this example you can find the file with the following content in **./plugins/example/tools/csv_reader/2017-06-20_v0.0.1.py**\n",
    "\n",
    "```Python\n",
    "from hyperstream import Tool, StreamInstance\n",
    "from hyperstream.utils import check_input_stream_count\n",
    "\n",
    "from dateutil.parser import parse\n",
    "\n",
    "\n",
    "class CsvReader(Tool):\n",
    "    def __init__(self, filename):\n",
    "        super(CsvReader, self).__init__(filename=filename)\n",
    "\n",
    "    @check_input_stream_count(0)\n",
    "    def _execute(self, sources, alignment_stream, interval):\n",
    "\n",
    "        # Let's make the assumption that the first field is the timestamp\n",
    "\n",
    "        first = True\n",
    "\n",
    "        with open(self.filename, 'rU') as f:\n",
    "            for line in f.readlines():\n",
    "                if first:\n",
    "                    first = False\n",
    "                    continue\n",
    "                elements = line.split(',')\n",
    "                dt = parse(elements[0])\n",
    "                if dt in interval:\n",
    "                    yield StreamInstance(dt, map(float, elements[1:]))\n",
    "\n",
    "```\n",
    "\n",
    "### 3. Add HyperStream configuration\n",
    "\n",
    "Now, it is necessary to add information about this plugin into the **hyperstream_config.json**. In particular, we need to add the following information in the plugin section:\n",
    "\n",
    "- channel_id_prefix: This is to create Channels (explained in another tutorial).\n",
    "- channel_names: A list of available Channels\n",
    "- path: path to the new plugin\n",
    "- has_tools: If the new plugin has tools\n",
    "- has_assets: If it contains folders or files that are needed by the plugin\n",
    "\n",
    "Next, we have an example of an configuration file with the new plugin:\n",
    "\n",
    "```json\n",
    "{\n",
    "    \"mongo\": {\n",
    "        \"host\": \"localhost\",\n",
    "        \"port\": 27017,\n",
    "        \"tz_aware\": true,\n",
    "        \"db\": \"hyperstream\"\n",
    "    },\n",
    "    \"plugins\": [{\n",
    "        \"channel_id_prefix\": \"example\",\n",
    "        \"channel_names\": [],\n",
    "        \"path\": \"plugins/example\",\n",
    "        \"has_tools\": true,\n",
    "        \"has_assets\": false\n",
    "    }],\n",
    "    \"online_engine\": {\n",
    "        \"interval\": {\n",
    "            \"start\": -60,\n",
    "            \"end\": -10\n",
    "        },\n",
    "        \"sleep\": 5,\n",
    "        \"iterations\": 100\n",
    "    }\n",
    "}\n",
    "\n",
    "```\n",
    "\n",
    "## Using the new tool\n",
    "\n",
    "Now we can write some HyperStream code that uses the new plugin."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPython 2.7.6\n",
      "IPython 5.3.0\n",
      "\n",
      "hyperstream 0.3.0-beta\n",
      "\n",
      "compiler   : GCC 4.8.4\n",
      "system     : Linux\n",
      "release    : 3.19.0-80-generic\n",
      "machine    : x86_64\n",
      "processor  : x86_64\n",
      "CPU cores  : 4\n",
      "interpreter: 64bit\n",
      "Git hash   : f0e911526041b91fe7999a8968c80618d410e741\n",
      "HyperStream version 0.3.0-beta, connected to mongodb://localhost:27017/hyperstream, session id <no session>\n"
     ]
    }
   ],
   "source": [
    "%load_ext watermark\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"../\") # Add parent dir in the Path\n",
    "\n",
    "from hyperstream import HyperStream\n",
    "from hyperstream import TimeInterval\n",
    "from hyperstream.utils import UTC\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "from utils import plot_high_chart\n",
    "from utils import plot_multiple_stock\n",
    "\n",
    "%watermark -v -m -p hyperstream -g\n",
    "\n",
    "hs = HyperStream(loglevel=20)\n",
    "print hs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading the new plugin\n",
    "\n",
    "After instantiating HyperStream, if the configuration of the plugin and the plugin are in the right place, we will be able to load our new tool **csv_reader**, specifying where is the input file.\n",
    "\n",
    "The data is the Polar Ice data that can be found in [this link][1]\n",
    "\n",
    "[1]: http://new.censusatschool.org.nz/resource/time-series-data-sets-2012/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "reader_tool = hs.plugins.example.tools.csv_reader('data/sea_ice.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a stream\n",
    "\n",
    "Now we can create a stream to store all the results in memory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sea_ice_stream = hs.channel_manager.memory.get_or_create_stream(\"sea_ice\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Execute the tool\n",
    "\n",
    "We can now execute the tool in the interval of interest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimeIntervals([TimeInterval(start=datetime.datetime(1990, 1, 1, 0, 0, tzinfo=<UTC>), end=datetime.datetime(2011, 11, 1, 0, 0, tzinfo=<UTC>))])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ti = TimeInterval(datetime(1990, 1, 1).replace(tzinfo=UTC), datetime(2011, 11, 1).replace(tzinfo=UTC))\n",
    "\n",
    "reader_tool.execute(sources=[], sink=sea_ice_stream, interval=ti)\n",
    "\n",
    "sea_ice_stream.calculated_intervals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Query the stream\n",
    "\n",
    "And finally we can query certain period of time to the tool and store the information in the created stream"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1995-02-01 00:00:00+00:00]: [13.3, 2.12]\n",
      "[1995-03-01 00:00:00+00:00]: [13.28, 2.74]\n",
      "[1995-04-01 00:00:00+00:00]: [12.32, 5.35]\n",
      "[1995-05-01 00:00:00+00:00]: [10.76, 8.23]\n",
      "[1995-06-01 00:00:00+00:00]: [8.86, 10.37]\n",
      "[1995-07-01 00:00:00+00:00]: [6.05, 12.47]\n",
      "[1995-08-01 00:00:00+00:00]: [4.61, 14.16]\n",
      "[1995-09-01 00:00:00+00:00]: [4.38, 14.42]\n",
      "[1995-10-01 00:00:00+00:00]: [5.91, 13.47]\n",
      "[1995-11-01 00:00:00+00:00]: [8.95, 11.38]\n",
      "[1995-12-01 00:00:00+00:00]: [11.02, 7.03]\n",
      "[1996-01-01 00:00:00+00:00]: [12.07, 3.43]\n"
     ]
    }
   ],
   "source": [
    "ti = TimeInterval(datetime(1995, 1, 1).replace(tzinfo=UTC), datetime(1996, 1, 1).replace(tzinfo=UTC))\n",
    "\n",
    "for key, value in sea_ice_stream.window(ti).items():\n",
    "    print '[%s]: %s' % (key, value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualize all the interval\n",
    "\n",
    "We can now visualize one of the values of the Stream, in this case the sea level in the Antarctica."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <html>\n",
       "        <head>\n",
       "        <title>Sea level in the Antarctica</title>\n",
       "           <!--<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js\"></script>-->\n",
       "           <!--<script src=\"https://code.highcharts.com/stock/highstock.js\"></script>-->\n",
       "           <!--<script src=\"https://code.highcharts.com/stock/modules/exporting.js\"></script>-->\n",
       "           <script src=\"./scripts/jquery.min.js\"></script>\n",
       "           <script src=\"./scripts/highstock.js\"></script>\n",
       "           <script src=\"./scripts/exporting.js\"></script>\n",
       "        </head>\n",
       "        <body>\n",
       "\n",
       "        <div id=\"container5512\" style=\"width: 800px; height: 600px; margin: 125 auto\"></div>\n",
       "\n",
       "        <script language=\"JavaScript\">\n",
       "               var data = [[633830400000.0, 2.15], [636249600000.0, 2.71], [638928000000.0, 5.1], [641520000000.0, 7.37], [644198400000.0, 10.26], [646790400000.0, 12.17], [649468800000.0, 13.95], [652147200000.0, 14.3], [654739200000.0, 13.71], [657417600000.0, 11.24], [660009600000.0, 6.56], [662688000000.0, 3.39], [665366400000.0, 2.01], [667785600000.0, 2.54], [670464000000.0, 5.1], [673056000000.0, 7.77], [675734400000.0, 9.87], [678326400000.0, 12.12], [681004800000.0, 13.96], [683683200000.0, 14.38], [686275200000.0, 13.7], [688953600000.0, 11.35], [691545600000.0, 6.48], [694224000000.0, 3.12], [696902400000.0, 1.79], [699408000000.0, 2.39], [702086400000.0, 4.91], [704678400000.0, 7.53], [707356800000.0, 9.9], [709948800000.0, 12.48], [712627200000.0, 14.08], [715305600000.0, 14.04], [717897600000.0, 13.76], [720576000000.0, 11.09], [723168000000.0, 6.0], [725846400000.0, 2.43], [728524800000.0, 1.3], [730944000000.0, 2.15], [733622400000.0, 4.82], [736214400000.0, 7.8], [738892800000.0, 10.28], [741484800000.0, 12.63], [744163200000.0, 13.83], [746841600000.0, 14.62], [749433600000.0, 14.09], [752112000000.0, 11.72], [754704000000.0, 6.49], [757382400000.0, 3.26], [760060800000.0, 2.17], [762480000000.0, 3.13], [765158400000.0, 5.38], [767750400000.0, 8.12], [770428800000.0, 10.63], [773020800000.0, 13.03], [775699200000.0, 14.39], [778377600000.0, 14.52], [780969600000.0, 13.98], [783648000000.0, 11.65], [786240000000.0, 6.95], [788918400000.0, 3.62], [791596800000.0, 2.12], [794016000000.0, 2.74], [796694400000.0, 5.35], [799286400000.0, 8.23], [801964800000.0, 10.37], [804556800000.0, 12.47], [807235200000.0, 14.16], [809913600000.0, 14.42], [812505600000.0, 13.47], [815184000000.0, 11.38], [817776000000.0, 7.03], [820454400000.0, 3.43], [823132800000.0, 1.69], [825638400000.0, 2.56], [828316800000.0, 5.28], [830908800000.0, 8.57], [833587200000.0, 11.0], [836179200000.0, 12.79], [838857600000.0, 13.8], [841536000000.0, 14.45], [844128000000.0, 13.85], [846806400000.0, 11.14], [849398400000.0, 6.04], [852076800000.0, 2.65], [854755200000.0, 1.66], [857174400000.0, 2.23], [859852800000.0, 4.76], [862444800000.0, 7.82], [865123200000.0, 10.12], [867715200000.0, 12.66], [870393600000.0, 14.0], [873072000000.0, 14.49], [875664000000.0, 14.01], [878342400000.0, 11.49], [880934400000.0, 6.84], [883612800000.0, 2.91], [886291200000.0, 1.81], [888710400000.0, 2.64], [891388800000.0, 5.03], [893980800000.0, 7.82], [896659200000.0, 10.25], [899251200000.0, 12.56], [901929600000.0, 13.98], [904608000000.0, 14.76], [907200000000.0, 14.45], [909878400000.0, 11.76], [912470400000.0, 6.61], [915148800000.0, 2.99], [917827200000.0, 1.7], [920246400000.0, 2.51], [922924800000.0, 5.05], [925516800000.0, 7.99], [928195200000.0, 10.74], [930787200000.0, 12.97], [933465600000.0, 14.15], [936144000000.0, 14.53], [938736000000.0, 14.13], [941414400000.0, 11.4], [944006400000.0, 6.62], [946684800000.0, 2.79], [949363200000.0, 1.63], [951868800000.0, 2.6], [954547200000.0, 5.23], [957139200000.0, 8.47], [959817600000.0, 10.95], [962409600000.0, 12.89], [965088000000.0, 14.55], [967766400000.0, 14.98], [970358400000.0, 14.16], [973036800000.0, 11.71], [975628800000.0, 6.77], [978307200000.0, 3.55], [980985600000.0, 2.38], [983404800000.0, 3.3], [986083200000.0, 5.22], [988675200000.0, 7.92], [991353600000.0, 10.47], [993945600000.0, 12.49], [996624000000.0, 13.66], [999302400000.0, 14.27], [1001894400000.0, 13.24], [1004572800000.0, 10.88], [1007164800000.0, 6.16], [1009843200000.0, 2.81], [1012521600000.0, 1.75], [1014940800000.0, 2.29], [1017619200000.0, 4.49], [1020211200000.0, 7.09], [1022889600000.0, 9.6], [1025481600000.0, 12.42], [1028160000000.0, 13.65], [1030838400000.0, 14.0], [1033430400000.0, 13.73], [1036108800000.0, 11.55], [1038700800000.0, 7.47], [1041379200000.0, 4.01], [1044057600000.0, 2.6], [1046476800000.0, 3.16], [1049155200000.0, 5.48], [1051747200000.0, 8.35], [1054425600000.0, 11.07], [1057017600000.0, 13.08], [1059696000000.0, 13.66], [1062374400000.0, 14.05], [1064966400000.0, 13.78], [1067644800000.0, 11.36], [1070236800000.0, 7.03], [1072915200000.0, 3.69], [1075593600000.0, 2.28], [1078099200000.0, 3.1], [1080777600000.0, 5.33], [1083369600000.0, 8.09], [1086048000000.0, 10.8], [1088640000000.0, 13.07], [1091318400000.0, 14.15], [1093996800000.0, 14.51], [1096588800000.0, 14.03], [1099267200000.0, 11.94], [1101859200000.0, 6.76], [1104537600000.0, 3.02], [1107216000000.0, 1.91], [1109635200000.0, 2.66], [1112313600000.0, 5.12], [1114905600000.0, 7.87], [1117584000000.0, 10.2], [1120176000000.0, 12.63], [1122854400000.0, 13.86], [1125532800000.0, 14.76], [1128124800000.0, 13.92], [1130803200000.0, 11.69], [1133395200000.0, 6.12], [1136073600000.0, 2.67], [1138752000000.0, 1.72], [1141171200000.0, 1.94], [1143849600000.0, 4.2], [1146441600000.0, 7.08], [1149120000000.0, 10.49], [1151712000000.0, 12.79], [1154390400000.0, 14.45], [1157068800000.0, 15.09], [1159660800000.0, 14.58], [1162339200000.0, 11.5], [1164931200000.0, 6.28], [1167609600000.0, 2.87], [1170288000000.0, 1.89], [1172707200000.0, 2.48], [1175385600000.0, 4.62], [1177977600000.0, 7.44], [1180656000000.0, 10.39], [1183248000000.0, 12.52], [1185926400000.0, 13.94], [1188604800000.0, 15.29], [1191196800000.0, 14.5], [1193875200000.0, 12.03], [1196467200000.0, 8.28], [1199145600000.0, 3.91], [1201824000000.0, 2.32], [1204329600000.0, 3.62], [1207008000000.0, 6.14], [1209600000000.0, 8.51], [1212278400000.0, 11.31], [1214870400000.0, 12.8], [1217548800000.0, 13.96], [1220227200000.0, 14.27], [1222819200000.0, 14.0], [1225497600000.0, 11.81], [1228089600000.0, 7.15], [1230768000000.0, 3.33], [1233446400000.0, 1.89], [1235865600000.0, 2.94], [1238544000000.0, 5.86], [1241136000000.0, 8.32], [1243814400000.0, 10.97], [1246406400000.0, 12.95], [1249084800000.0, 14.29], [1251763200000.0, 14.92], [1254355200000.0, 14.19], [1257033600000.0, 11.64], [1259625600000.0, 6.87], [1262304000000.0, 3.16], [1264982400000.0, 2.03], [1267401600000.0, 2.58], [1270080000000.0, 4.88], [1272672000000.0, 8.19], [1275350400000.0, 11.51], [1277942400000.0, 13.6], [1280620800000.0, 14.78], [1283299200000.0, 14.24], [1285891200000.0, 14.26], [1288569600000.0, 11.75], [1291161600000.0, 6.67], [1293840000000.0, 2.87], [1296518400000.0, 1.8], [1298937600000.0, 2.18]];\n",
       "\n",
       "            Highcharts.stockChart('container5512', {\n",
       "                chart: {\n",
       "                    zoomType: 'x'\n",
       "                },\n",
       "                title: {\n",
       "                    text: 'Sea level in the Antarctica'\n",
       "                },\n",
       "                xAxis: {\n",
       "                    type: 'datetime', ordinal: false\n",
       "                },\n",
       "                yAxis: {\n",
       "                    title: {\n",
       "                        text: 'meters'\n",
       "                    }\n",
       "                },\n",
       "                legend: {\n",
       "                    enabled: false\n",
       "                },\n",
       "\n",
       "                series: [{\n",
       "                    type: 'spline',\n",
       "                    name: 'meters',\n",
       "                    data: data\n",
       "                }]\n",
       "            });\n",
       "        </script>\n",
       "\n",
       "        </body>\n",
       "        </html>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_time, my_data = zip(*[(key.__str__(), value[1]) for key, value in sea_ice_stream.window().items()])\n",
    "\n",
    "plot_high_chart(my_time, my_data, type=\"high_stock\", title='Sea level in the Antarctica', yax='meters')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also visualize both of the Stream values, the Arctic and the Antarctica sea levels:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "     <html>\n",
       "        <head>\n",
       "        <title>Sea level</title>\n",
       "           <script src=\"./scripts/jquery.min.js\"></script>\n",
       "           <script src=\"./scripts/highstock.js\"></script>\n",
       "           <script src=\"./scripts/exporting.js\"></script>\n",
       "        </head>\n",
       "        <body>\n",
       "\n",
       "        <div id=\"container8344\" style=\"width: 800px; height: 600px; margin: 125 auto\"></div>\n",
       "\n",
       "        <script language=\"JavaScript\">\n",
       "            var seriesOptions = [{'type': 'spline', 'data': [[633830400000.0, 13.33], [636249600000.0, 13.44], [638928000000.0, 12.16], [641520000000.0, 10.84], [644198400000.0, 9.12], [646790400000.0, 6.44], [649468800000.0, 4.92], [652147200000.0, 4.5], [654739200000.0, 6.67], [657417600000.0, 9.58], [660009600000.0, 11.4], [662688000000.0, 12.49], [665366400000.0, 13.15], [667785600000.0, 13.35], [670464000000.0, 12.79], [673056000000.0, 11.44], [675734400000.0, 9.6], [678326400000.0, 6.66], [681004800000.0, 5.14], [683683200000.0, 4.46], [686275200000.0, 7.7], [688953600000.0, 9.19], [691545600000.0, 11.19], [694224000000.0, 12.54], [696902400000.0, 13.51], [699408000000.0, 13.41], [702086400000.0, 12.76], [704678400000.0, 11.37], [707356800000.0, 9.89], [709948800000.0, 7.14], [712627200000.0, 5.35], [715305600000.0, 5.37], [717897600000.0, 7.69], [720576000000.0, 9.69], [723168000000.0, 11.66], [725846400000.0, 12.85], [728524800000.0, 13.54], [730944000000.0, 13.71], [733622400000.0, 12.95], [736214400000.0, 11.35], [738892800000.0, 9.2], [741484800000.0, 6.17], [744163200000.0, 4.65], [746841600000.0, 4.52], [749433600000.0, 6.98], [752112000000.0, 9.49], [754704000000.0, 11.41], [757382400000.0, 12.8], [760060800000.0, 13.46], [762480000000.0, 13.47], [765158400000.0, 12.84], [767750400000.0, 11.6], [770428800000.0, 9.62], [773020800000.0, 6.85], [775699200000.0, 5.1], [778377600000.0, 5.08], [780969600000.0, 7.17], [783648000000.0, 9.64], [786240000000.0, 11.43], [788918400000.0, 12.72], [791596800000.0, 13.3], [794016000000.0, 13.28], [796694400000.0, 12.32], [799286400000.0, 10.76], [801964800000.0, 8.86], [804556800000.0, 6.05], [807235200000.0, 4.61], [809913600000.0, 4.38], [812505600000.0, 5.91], [815184000000.0, 8.95], [817776000000.0, 11.02], [820454400000.0, 12.07], [823132800000.0, 12.9], [825638400000.0, 12.83], [828316800000.0, 12.23], [830908800000.0, 11.22], [833587200000.0, 9.78], [836179200000.0, 7.36], [838857600000.0, 5.65], [841536000000.0, 5.58], [844128000000.0, 7.49], [846806400000.0, 8.7], [849398400000.0, 10.96], [852076800000.0, 12.3], [854755200000.0, 13.36], [857174400000.0, 13.24], [859852800000.0, 12.48], [862444800000.0, 11.17], [865123200000.0, 9.14], [867715200000.0, 6.41], [870393600000.0, 5.03], [873072000000.0, 4.84], [875664000000.0, 6.39], [878342400000.0, 9.0], [880934400000.0, 11.19], [883612800000.0, 12.73], [886291200000.0, 13.7], [888710400000.0, 13.5], [891388800000.0, 12.77], [893980800000.0, 11.35], [896659200000.0, 9.11], [899251200000.0, 6.38], [901929600000.0, 4.61], [904608000000.0, 4.24], [907200000000.0, 6.65], [909878400000.0, 8.82], [912470400000.0, 10.85], [915148800000.0, 12.54], [917827200000.0, 13.38], [920246400000.0, 13.47], [922924800000.0, 13.08], [925516800000.0, 11.61], [928195200000.0, 9.18], [930787200000.0, 6.49], [933465600000.0, 4.67], [936144000000.0, 4.22], [938736000000.0, 6.77], [941414400000.0, 9.21], [944006400000.0, 10.93], [946684800000.0, 12.22], [949363200000.0, 13.02], [951868800000.0, 13.1], [954547200000.0, 12.51], [957139200000.0, 11.18], [959817600000.0, 8.99], [962409600000.0, 6.31], [965088000000.0, 4.71], [967766400000.0, 4.31], [970358400000.0, 6.64], [973036800000.0, 8.81], [975628800000.0, 10.9], [978307200000.0, 12.27], [980985600000.0, 13.14], [983404800000.0, 13.57], [986083200000.0, 12.99], [988675200000.0, 11.32], [991353600000.0, 9.01], [993945600000.0, 6.22], [996624000000.0, 4.87], [999302400000.0, 4.55], [1001894400000.0, 6.59], [1004572800000.0, 9.02], [1007164800000.0, 10.49], [1009843200000.0, 12.19], [1012521600000.0, 13.36], [1014940800000.0, 13.36], [1017619200000.0, 12.35], [1020211200000.0, 11.11], [1022889600000.0, 9.13], [1025481600000.0, 6.34], [1028160000000.0, 4.23], [1030838400000.0, 3.98], [1033430400000.0, 6.2], [1036108800000.0, 8.69], [1038700800000.0, 10.58], [1041379200000.0, 12.21], [1044057600000.0, 13.16], [1046476800000.0, 13.36], [1049155200000.0, 12.38], [1051747200000.0, 10.82], [1054425600000.0, 9.05], [1057017600000.0, 6.06], [1059696000000.0, 4.44], [1062374400000.0, 4.01], [1064966400000.0, 5.93], [1067644800000.0, 8.5], [1070236800000.0, 10.74], [1072915200000.0, 12.19], [1075593600000.0, 12.87], [1078099200000.0, 12.93], [1080777600000.0, 12.08], [1083369600000.0, 10.86], [1086048000000.0, 9.18], [1088640000000.0, 6.43], [1091318400000.0, 4.57], [1093996800000.0, 4.35], [1096588800000.0, 6.35], [1099267200000.0, 8.9], [1101859200000.0, 10.91], [1104537600000.0, 11.84], [1107216000000.0, 12.27], [1109635200000.0, 12.67], [1112313600000.0, 12.16], [1114905600000.0, 10.83], [1117584000000.0, 8.74], [1120176000000.0, 5.81], [1122854400000.0, 4.14], [1125532800000.0, 4.03], [1128124800000.0, 5.71], [1130803200000.0, 8.73], [1133395200000.0, 10.51], [1136073600000.0, 11.57], [1138752000000.0, 12.28], [1141171200000.0, 12.44], [1143849600000.0, 11.97], [1146441600000.0, 10.39], [1149120000000.0, 8.34], [1151712000000.0, 5.71], [1154390400000.0, 4.24], [1157068800000.0, 3.97], [1159660800000.0, 5.74], [1162339200000.0, 8.07], [1164931200000.0, 10.1], [1167609600000.0, 11.79], [1170288000000.0, 12.52], [1172707200000.0, 12.49], [1175385600000.0, 11.75], [1177977600000.0, 10.71], [1180656000000.0, 8.15], [1183248000000.0, 5.03], [1185926400000.0, 3.11], [1188604800000.0, 2.78], [1191196800000.0, 4.21], [1193875200000.0, 8.0], [1196467200000.0, 10.11], [1199145600000.0, 11.89], [1201824000000.0, 12.81], [1204329600000.0, 13.17], [1207008000000.0, 12.42], [1209600000000.0, 10.83], [1212278400000.0, 8.47], [1214870400000.0, 5.7], [1217548800000.0, 3.43], [1220227200000.0, 2.93], [1222819200000.0, 5.4], [1225497600000.0, 8.7], [1228089600000.0, 10.6], [1230768000000.0, 11.91], [1233446400000.0, 12.82], [1235865600000.0, 13.04], [1238544000000.0, 12.54], [1241136000000.0, 11.08], [1243814400000.0, 8.86], [1246406400000.0, 5.71], [1249084800000.0, 3.79], [1251763200000.0, 3.42], [1254355200000.0, 4.85], [1257033600000.0, 7.98], [1259625600000.0, 10.17], [1262304000000.0, 11.59], [1264982400000.0, 12.52], [1267401600000.0, 13.11], [1270080000000.0, 12.43], [1272672000000.0, 10.48], [1275350400000.0, 7.98], [1277942400000.0, 5.21], [1280620800000.0, 3.54], [1283299200000.0, 3.02], [1285891200000.0, 5.13], [1288569600000.0, 8.06], [1291161600000.0, 10.02], [1293840000000.0, 11.38], [1296518400000.0, 12.32], [1298937600000.0, 12.4]], 'name': 'Arctic'}, {'type': 'spline', 'data': [[633830400000.0, 2.15], [636249600000.0, 2.71], [638928000000.0, 5.1], [641520000000.0, 7.37], [644198400000.0, 10.26], [646790400000.0, 12.17], [649468800000.0, 13.95], [652147200000.0, 14.3], [654739200000.0, 13.71], [657417600000.0, 11.24], [660009600000.0, 6.56], [662688000000.0, 3.39], [665366400000.0, 2.01], [667785600000.0, 2.54], [670464000000.0, 5.1], [673056000000.0, 7.77], [675734400000.0, 9.87], [678326400000.0, 12.12], [681004800000.0, 13.96], [683683200000.0, 14.38], [686275200000.0, 13.7], [688953600000.0, 11.35], [691545600000.0, 6.48], [694224000000.0, 3.12], [696902400000.0, 1.79], [699408000000.0, 2.39], [702086400000.0, 4.91], [704678400000.0, 7.53], [707356800000.0, 9.9], [709948800000.0, 12.48], [712627200000.0, 14.08], [715305600000.0, 14.04], [717897600000.0, 13.76], [720576000000.0, 11.09], [723168000000.0, 6.0], [725846400000.0, 2.43], [728524800000.0, 1.3], [730944000000.0, 2.15], [733622400000.0, 4.82], [736214400000.0, 7.8], [738892800000.0, 10.28], [741484800000.0, 12.63], [744163200000.0, 13.83], [746841600000.0, 14.62], [749433600000.0, 14.09], [752112000000.0, 11.72], [754704000000.0, 6.49], [757382400000.0, 3.26], [760060800000.0, 2.17], [762480000000.0, 3.13], [765158400000.0, 5.38], [767750400000.0, 8.12], [770428800000.0, 10.63], [773020800000.0, 13.03], [775699200000.0, 14.39], [778377600000.0, 14.52], [780969600000.0, 13.98], [783648000000.0, 11.65], [786240000000.0, 6.95], [788918400000.0, 3.62], [791596800000.0, 2.12], [794016000000.0, 2.74], [796694400000.0, 5.35], [799286400000.0, 8.23], [801964800000.0, 10.37], [804556800000.0, 12.47], [807235200000.0, 14.16], [809913600000.0, 14.42], [812505600000.0, 13.47], [815184000000.0, 11.38], [817776000000.0, 7.03], [820454400000.0, 3.43], [823132800000.0, 1.69], [825638400000.0, 2.56], [828316800000.0, 5.28], [830908800000.0, 8.57], [833587200000.0, 11.0], [836179200000.0, 12.79], [838857600000.0, 13.8], [841536000000.0, 14.45], [844128000000.0, 13.85], [846806400000.0, 11.14], [849398400000.0, 6.04], [852076800000.0, 2.65], [854755200000.0, 1.66], [857174400000.0, 2.23], [859852800000.0, 4.76], [862444800000.0, 7.82], [865123200000.0, 10.12], [867715200000.0, 12.66], [870393600000.0, 14.0], [873072000000.0, 14.49], [875664000000.0, 14.01], [878342400000.0, 11.49], [880934400000.0, 6.84], [883612800000.0, 2.91], [886291200000.0, 1.81], [888710400000.0, 2.64], [891388800000.0, 5.03], [893980800000.0, 7.82], [896659200000.0, 10.25], [899251200000.0, 12.56], [901929600000.0, 13.98], [904608000000.0, 14.76], [907200000000.0, 14.45], [909878400000.0, 11.76], [912470400000.0, 6.61], [915148800000.0, 2.99], [917827200000.0, 1.7], [920246400000.0, 2.51], [922924800000.0, 5.05], [925516800000.0, 7.99], [928195200000.0, 10.74], [930787200000.0, 12.97], [933465600000.0, 14.15], [936144000000.0, 14.53], [938736000000.0, 14.13], [941414400000.0, 11.4], [944006400000.0, 6.62], [946684800000.0, 2.79], [949363200000.0, 1.63], [951868800000.0, 2.6], [954547200000.0, 5.23], [957139200000.0, 8.47], [959817600000.0, 10.95], [962409600000.0, 12.89], [965088000000.0, 14.55], [967766400000.0, 14.98], [970358400000.0, 14.16], [973036800000.0, 11.71], [975628800000.0, 6.77], [978307200000.0, 3.55], [980985600000.0, 2.38], [983404800000.0, 3.3], [986083200000.0, 5.22], [988675200000.0, 7.92], [991353600000.0, 10.47], [993945600000.0, 12.49], [996624000000.0, 13.66], [999302400000.0, 14.27], [1001894400000.0, 13.24], [1004572800000.0, 10.88], [1007164800000.0, 6.16], [1009843200000.0, 2.81], [1012521600000.0, 1.75], [1014940800000.0, 2.29], [1017619200000.0, 4.49], [1020211200000.0, 7.09], [1022889600000.0, 9.6], [1025481600000.0, 12.42], [1028160000000.0, 13.65], [1030838400000.0, 14.0], [1033430400000.0, 13.73], [1036108800000.0, 11.55], [1038700800000.0, 7.47], [1041379200000.0, 4.01], [1044057600000.0, 2.6], [1046476800000.0, 3.16], [1049155200000.0, 5.48], [1051747200000.0, 8.35], [1054425600000.0, 11.07], [1057017600000.0, 13.08], [1059696000000.0, 13.66], [1062374400000.0, 14.05], [1064966400000.0, 13.78], [1067644800000.0, 11.36], [1070236800000.0, 7.03], [1072915200000.0, 3.69], [1075593600000.0, 2.28], [1078099200000.0, 3.1], [1080777600000.0, 5.33], [1083369600000.0, 8.09], [1086048000000.0, 10.8], [1088640000000.0, 13.07], [1091318400000.0, 14.15], [1093996800000.0, 14.51], [1096588800000.0, 14.03], [1099267200000.0, 11.94], [1101859200000.0, 6.76], [1104537600000.0, 3.02], [1107216000000.0, 1.91], [1109635200000.0, 2.66], [1112313600000.0, 5.12], [1114905600000.0, 7.87], [1117584000000.0, 10.2], [1120176000000.0, 12.63], [1122854400000.0, 13.86], [1125532800000.0, 14.76], [1128124800000.0, 13.92], [1130803200000.0, 11.69], [1133395200000.0, 6.12], [1136073600000.0, 2.67], [1138752000000.0, 1.72], [1141171200000.0, 1.94], [1143849600000.0, 4.2], [1146441600000.0, 7.08], [1149120000000.0, 10.49], [1151712000000.0, 12.79], [1154390400000.0, 14.45], [1157068800000.0, 15.09], [1159660800000.0, 14.58], [1162339200000.0, 11.5], [1164931200000.0, 6.28], [1167609600000.0, 2.87], [1170288000000.0, 1.89], [1172707200000.0, 2.48], [1175385600000.0, 4.62], [1177977600000.0, 7.44], [1180656000000.0, 10.39], [1183248000000.0, 12.52], [1185926400000.0, 13.94], [1188604800000.0, 15.29], [1191196800000.0, 14.5], [1193875200000.0, 12.03], [1196467200000.0, 8.28], [1199145600000.0, 3.91], [1201824000000.0, 2.32], [1204329600000.0, 3.62], [1207008000000.0, 6.14], [1209600000000.0, 8.51], [1212278400000.0, 11.31], [1214870400000.0, 12.8], [1217548800000.0, 13.96], [1220227200000.0, 14.27], [1222819200000.0, 14.0], [1225497600000.0, 11.81], [1228089600000.0, 7.15], [1230768000000.0, 3.33], [1233446400000.0, 1.89], [1235865600000.0, 2.94], [1238544000000.0, 5.86], [1241136000000.0, 8.32], [1243814400000.0, 10.97], [1246406400000.0, 12.95], [1249084800000.0, 14.29], [1251763200000.0, 14.92], [1254355200000.0, 14.19], [1257033600000.0, 11.64], [1259625600000.0, 6.87], [1262304000000.0, 3.16], [1264982400000.0, 2.03], [1267401600000.0, 2.58], [1270080000000.0, 4.88], [1272672000000.0, 8.19], [1275350400000.0, 11.51], [1277942400000.0, 13.6], [1280620800000.0, 14.78], [1283299200000.0, 14.24], [1285891200000.0, 14.26], [1288569600000.0, 11.75], [1291161600000.0, 6.67], [1293840000000.0, 2.87], [1296518400000.0, 1.8], [1298937600000.0, 2.18]], 'name': 'Antarctica'}];\n",
       "\n",
       "            /**\n",
       "             * Create the chart when all data is loaded\n",
       "             * @returns {undefined}\n",
       "             */\n",
       "            Highcharts.stockChart('container8344', {\n",
       "              chart: {\n",
       "                zoomType: 'x'\n",
       "              },\n",
       "              title: {\n",
       "                  text: 'Sea level'\n",
       "              },\n",
       "              plotOptions: {\n",
       "                series: {\n",
       "                  showInNavigator: true,\n",
       "                }\n",
       "              },\n",
       "                yAxis: {\n",
       "                    title: {\n",
       "                        text: 'meters'\n",
       "                    }\n",
       "                },\n",
       "              tooltip: {\n",
       "                pointFormat: '<span style=\"color:{series.color}\">{series.name}</span>:<b>{point.y}</b>',\n",
       "                valueDecimals: 2,\n",
       "                split: true\n",
       "              },\n",
       "              series: seriesOptions\n",
       "            });\n",
       "        </script>\n",
       "\n",
       "        </body>\n",
       "        </html>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "time = [key.__str__() for key, value in sea_ice_stream.window().items()]\n",
    "data = [list(a) for a in zip(*[value for key, value in sea_ice_stream.window().items()])]\n",
    "htype= 'spline'\n",
    "names = ['Arctic', 'Antarctica']\n",
    "        \n",
    "plot_multiple_stock(data, time=time, names=names, htype=htype, title='Sea level', ylabel='meters')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualize specific interval\n",
    "\n",
    "We can visualize the reduced time interval that we specified above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "     <html>\n",
       "        <head>\n",
       "        <title>test multi-output</title>\n",
       "           <script src=\"./scripts/jquery.min.js\"></script>\n",
       "           <script src=\"./scripts/highstock.js\"></script>\n",
       "           <script src=\"./scripts/exporting.js\"></script>\n",
       "        </head>\n",
       "        <body>\n",
       "\n",
       "        <div id=\"container7086\" style=\"width: 800px; height: 600px; margin: 125 auto\"></div>\n",
       "\n",
       "        <script language=\"JavaScript\">\n",
       "            var seriesOptions = [{'type': 'spline', 'data': [[791596800000.0, 13.3], [794016000000.0, 13.28], [796694400000.0, 12.32], [799286400000.0, 10.76], [801964800000.0, 8.86], [804556800000.0, 6.05], [807235200000.0, 4.61], [809913600000.0, 4.38], [812505600000.0, 5.91], [815184000000.0, 8.95], [817776000000.0, 11.02], [820454400000.0, 12.07]], 'name': 'Arctic'}, {'type': 'spline', 'data': [[791596800000.0, 2.12], [794016000000.0, 2.74], [796694400000.0, 5.35], [799286400000.0, 8.23], [801964800000.0, 10.37], [804556800000.0, 12.47], [807235200000.0, 14.16], [809913600000.0, 14.42], [812505600000.0, 13.47], [815184000000.0, 11.38], [817776000000.0, 7.03], [820454400000.0, 3.43]], 'name': 'Antarctica'}];\n",
       "\n",
       "            /**\n",
       "             * Create the chart when all data is loaded\n",
       "             * @returns {undefined}\n",
       "             */\n",
       "            Highcharts.stockChart('container7086', {\n",
       "              chart: {\n",
       "                zoomType: 'x'\n",
       "              },\n",
       "              title: {\n",
       "                  text: 'test multi-output'\n",
       "              },\n",
       "              plotOptions: {\n",
       "                series: {\n",
       "                  showInNavigator: true,\n",
       "                }\n",
       "              },\n",
       "                yAxis: {\n",
       "                    title: {\n",
       "                        text: 'meters'\n",
       "                    }\n",
       "                },\n",
       "              tooltip: {\n",
       "                pointFormat: '<span style=\"color:{series.color}\">{series.name}</span>:<b>{point.y}</b>',\n",
       "                valueDecimals: 2,\n",
       "                split: true\n",
       "              },\n",
       "              series: seriesOptions\n",
       "            });\n",
       "        </script>\n",
       "\n",
       "        </body>\n",
       "        </html>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "time = [key.__str__() for key, value in sea_ice_stream.window(ti).items()]\n",
    "data = [list(a) for a in zip(*[value for key, value in sea_ice_stream.window(ti).items()])]\n",
    "htype= 'spline'\n",
    "names = ['Arctic', 'Antarctica']\n",
    "        \n",
    "plot_multiple_stock(data, time=time, names=names, htype=htype, title='test multi-output', ylabel='meters')"
   ]
  },
  {
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}