{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Title**: Regression selection\n",
    "\n",
    "**Description**: A linked streams example demonstrating how to the Selection1D stream to tap on a datapoint and reveal a regression plot. Highlights how custom interactivity can be used to reveal more information about a dataset.\n",
    "\n",
    "**Dependencies**: Bokeh, SciPy\n",
    "\n",
    "**Backends**: [Bokeh](./Selection1D_tap.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import opts\n",
    "from holoviews.streams import Selection1D\n",
    "from scipy import stats\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_samples(N, corr=0.8):\n",
    "    xx = np.array([-0.51, 51.2])\n",
    "    yy = np.array([0.33, 51.6])\n",
    "    means = [xx.mean(), yy.mean()]  \n",
    "    stds = [xx.std() / 3, yy.std() / 3]\n",
    "    covs = [[stds[0]**2          , stds[0]*stds[1]*corr], \n",
    "            [stds[0]*stds[1]*corr,           stds[1]**2]] \n",
    "\n",
    "    return np.random.multivariate_normal(means, covs, N)\n",
    "\n",
    "data = [('Week %d' % (i%10), np.random.rand(), chr(65+np.random.randint(5)), i) for i in range(100)]\n",
    "sample_data = hv.NdOverlay({i: hv.Points(gen_samples(np.random.randint(1000, 5000), r2))\n",
    "                            for _, r2, _, i in data})\n",
    "points = hv.Scatter(data, 'Date', ['r2', 'block', 'id']).redim.range(r2=(0., 1))\n",
    "stream = Selection1D(source=points)\n",
    "empty = (hv.Points(np.random.rand(0, 2)) * hv.Slope(0, 0)).relabel('No selection')\n",
    "\n",
    "def regression(index):\n",
    "    if not index:\n",
    "        return empty\n",
    "    scatter = sample_data[index[0]]\n",
    "    xs, ys = scatter['x'], scatter['y']\n",
    "    slope, intercep, rval, pval, std = stats.linregress(xs, ys)\n",
    "    return (scatter * hv.Slope(slope, intercep)).relabel('r2: %.3f' % slope)\n",
    "\n",
    "reg = hv.DynamicMap(regression, kdims=[], streams=[stream])\n",
    "\n",
    "average = hv.Curve(points, 'Date', 'r2').aggregate(function=np.mean)\n",
    "layout = points * average + reg\n",
    "layout.opts(\n",
    "    opts.Curve(color='black'),\n",
    "    opts.Slope(color='black', framewise=True),\n",
    "    opts.Scatter(color='block', tools=['tap', 'hover'], width=600, \n",
    "                 marker='triangle', cmap='Set1', size=10, framewise=True),\n",
    "    opts.Points(frame_width=250),\n",
    "    opts.Overlay(toolbar='above', legend_position='right')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"https://assets.holoviews.org/gifs/examples/streams/bokeh/regression_tap.gif\" width=800></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
