{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "from scipy import signal\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "temp = dl.data.Weather.load()['TEMP'].dropna()\n",
    "monthly = temp.resample('M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('analyzing_peaks')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "max_locs = signal.argrelmax(monthly.values)\n",
    "sp.ax.plot(monthly.index, monthly, label='Monthly means')\n",
    "sp.ax.plot(monthly.index[max_locs], monthly.values[max_locs], \n",
    "           'o', label='Tops')\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "annual_max = dl.ts.groupby_year(temp).max()\n",
    "sp.next_ax().plot(annual_max.index, annual_max, label='Annual Maximum Series')\n",
    "dl.plotting.plot_polyfit(sp.ax, annual_max.index, annual_max.values)\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "_, threshhold = dl.stats.outliers(temp, method='percentiles')\n",
    "over_threshhold = temp > threshhold\n",
    "streaks = dl.ts.groupby_year(over_threshhold).apply(\n",
    "    lambda x: dl.collect.longest_streak(x, 1))\n",
    "sp.next_ax().plot(streaks.index, streaks)\n",
    "dl.plotting.plot_polyfit(sp.ax, streaks.index, streaks.values)\n",
    "over_threshhold = dl.ts.groupby_year(over_threshhold).mean()\n",
    "sp.label()\n",
    "\n",
    "sp.label(advance=True)\n",
    "sns.distplot(annual_max, ax=sp.ax)\n",
    "sp.label(xlabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
