{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import statsmodels.api as sm\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import dautil as dl\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def filter_outliers(a):\n",
    "    b = a.copy()\n",
    "    bmin, bmax = dl.stats.outliers(b)\n",
    "    b[bmin > b] = np.nan\n",
    "    b[b > bmax] = np.nan\n",
    "\n",
    "    return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "starsCYG = sm.datasets.get_rdataset(\"starsCYG\", \"robustbase\", cache=True).data\n",
    "\n",
    "clipped = starsCYG.apply(dl.stats.clip_outliers)\n",
    "\n",
    "filtered = starsCYG.copy()\n",
    "filtered['log.Te'] = filter_outliers(filtered['log.Te'].values)\n",
    "filtered['log.light'] = filter_outliers(filtered['log.light'].values)\n",
    "filtered.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('outliers')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(3, 1, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(3, 1, context)\n",
    "sp.label()\n",
    "sns.regplot(x='log.Te', y='log.light', data=starsCYG, ax=sp.ax)\n",
    "sp.label(advance=True)\n",
    "sns.regplot(x='log.Te', y='log.light', data=clipped, ax=sp.ax)\n",
    "sp.label(advance=True)\n",
    "sns.regplot(x='log.Te', y='log.light', data=filtered, ax=sp.ax)\n",
    "plt.tight_layout()\n",
    "HTML(dl.report.HTMLBuilder().watermark())"
   ]
  },
  {
   "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
}
