{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo caching\n",
    "\n",
    "This notebook shows how caching of daily results is organised. First we show the low-level approach, then a high-level function is used.\n",
    "\n",
    "## Low-level approach"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from opengrid.library import misc\n",
    "from opengrid.library import houseprint\n",
    "from opengrid.library import caching\n",
    "from opengrid.library.analysis import DailyAgg\n",
    "import charts\n",
    "hp = houseprint.Houseprint()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We demonstrate the caching for the minimal daily water consumption (should be close to zero unless there is a water leak).  We create a cache object by specifying what we like to store and retrieve through this object.  The cached data is saved as a single csv per sensor in a folder specified in the opengrid.cfg.  Add the path to a folder where you want these csv-files to be stored as follows to your opengrid.cfg\n",
    "\n",
    "    [data]\n",
    "    folder: path_to_folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cache_water = caching.Cache(variable='water_daily_min')\n",
    "df_cache = cache_water.get(sensors=hp.get_sensors(sensortype='water'))\n",
    "charts.plot(df_cache.ix[-8:], stock=True, show='inline')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If this is the first time you run this demo, no cached data will be found, and you get an empty graph. \n",
    "\n",
    "Let's store some results in this cache.  We start from the water consumption of last week."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "hp.sync_tmpos()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "start = pd.Timestamp('now') - pd.Timedelta(weeks=1)\n",
    "df_water = hp.get_data(sensortype='water', head=start, )\n",
    "df_water.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use the method *daily_min()* from the analysis module to obtain a dataframe with daily minima for each sensor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "daily_min = DailyAgg(df_water, agg='min').result\n",
    "daily_min.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "daily_min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cache_water.update(daily_min)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can get the daily water minima from the cache directly. Pass a *start* or *end* date to limit the returned dataframe."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sensors = hp.get_sensors(sensortype='water') # sensor objects\n",
    "charts.plot(cache_water.get(sensors=sensors, start=start, end=None), show='inline', stock=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A high-level cache function\n",
    "\n",
    "The caching of daily results is very similar for all kinds of results.  Therefore, a high-level function is defined that can be parametrised to cache a lot of different things."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from opengrid.library import misc\n",
    "from opengrid.library import houseprint\n",
    "from opengrid.library import caching\n",
    "from opengrid.library import analysis\n",
    "import charts   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hp = houseprint.Houseprint()\n",
    "#hp.sync_tmpos()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sensors = hp.get_sensors(sensortype='water')\n",
    "caching.cache_results(hp=hp, sensors=sensors, resultname='water_daily_min', AnalysisClass=DailyAgg, agg='min')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cache = caching.Cache('water_daily_min')\n",
    "daily_min = cache.get(sensors = sensors, start = '20151201')\n",
    "charts.plot(daily_min, stock=True, show='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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
