{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfde1a11",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import lumicks.pylake as lk\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ef945c5",
   "metadata": {},
   "source": [
    "# Notebook widgets\n",
    "\n",
    "[Download this page as a Jupyter notebook](_downloads/884331a5a4433fbacd3a35ce80e717dd/nbwidgets.ipynb)\n",
    "\n",
    "When analyzing notebooks, it can be helpful to make use of interactive widgets. For this, we provide some widgets to help you analyze your data. To enable such widgets, start the notebook with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "619dbbfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89073f02",
   "metadata": {},
   "source": [
    "## Channel slicing\n",
    "\n",
    "Let’s say we want to do some analyses on slices of channel data. It would be nice to just quickly visually select some regions using a widget. Let’s load the file and run the widget:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d1d8c2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "file = lk.File(\"file.h5\")\n",
    "channel = file[\"Force LF\"][\"Force 1x\"]\n",
    "selector = channel.range_selector()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5072349",
   "metadata": {},
   "source": [
    "You can use the left mouse button to select time ranges (by clicking the left and then the right boundary of the region you wish to select). The right mouse button can be used to remove previous selections. We can access the selected timestamps of the ranges we selected by invoking `ranges`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c371c214",
   "metadata": {},
   "outputs": [],
   "source": [
    "selector.ranges"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1209bc6",
   "metadata": {},
   "source": [
    "And the actual slices from `slices`. If we want to plot all of our selections in separate plots for instance, we can do the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b969ed8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "for data_slice in selector.slices:\n",
    "    plt.figure()\n",
    "    data_slice.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53dd0114",
   "metadata": {},
   "source": [
    "## F,d selection\n",
    "\n",
    "### Range selection by time\n",
    "\n",
    "Assume we have an F,d curve we want to analyze. We know that this file contains one F,d curve which should be split up into two segments that we should be analyzing separately:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a7daaef",
   "metadata": {},
   "outputs": [],
   "source": [
    "fdcurves = file.fdcurves\n",
    "selector = lk.FdRangeSelector(fdcurves)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0130c58",
   "metadata": {},
   "source": [
    "This opens up a little widget, where you can use the left mouse button to select time ranges and the right mouse button to remove previous selections.\n",
    "\n",
    "Once we’ve selected some time ranges, we can output the timestamps:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8007d2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "selector.ranges"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e010193f",
   "metadata": {},
   "source": [
    "These timestamps can directly be used to extract the relevant data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78aaa08e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t_start, t_stop in selector.ranges[\"Fd pull #6\"]:\n",
    "    plt.figure()\n",
    "    plt.plot(fdcurves[\"Fd pull #6\"].f[t_start:t_stop].data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b3e3558",
   "metadata": {},
   "source": [
    "This produces a separate plot for each selection. There’s also a more direct way to get these plots, namely through `FdRangeSelector.fdcurves`. This gives you an `FdCurve` for each section you selected:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "597ae939",
   "metadata": {},
   "outputs": [],
   "source": [
    "for fdcurve in selector.fdcurves[\"Fd pull #6\"]:\n",
    "    plt.figure()\n",
    "    fdcurve.plot_scatter()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16cede11",
   "metadata": {},
   "source": [
    "### Processing multiple files\n",
    "\n",
    "Now let’s say our experiment is split up over multiple files, each containing a few F,d curves. We would like to load these curves all at once and make our selections. We can do this using automatically using `glob`. With `glob.glob` we grab a list of all `.h5` files in the directory `my_directory`. We then iterate over this list and open each file. Then, for all those files, we add each individual curves to our variable `fdcurves`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc7e6cc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "\n",
    "fdcurves = {}\n",
    "for filename in glob.glob('my_directory/*.h5'):\n",
    "    file = lk.File(filename)\n",
    "    for key, curve in file.fdcurves.items():\n",
    "        fdcurves[key] = curve"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f3231e7",
   "metadata": {},
   "source": [
    "Using this dictionary, we can open our widget and see all the data at once:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19cd36e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "selector = lk.FdRangeSelector(fdcurves)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a91d11d0",
   "metadata": {},
   "source": [
    "Plotting the curves can be done similarly as before. Here `.values()` indicates that we want the values from the dictionary of curve sets, and not the keys (which in our case are the curve names):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc7c6887",
   "metadata": {},
   "outputs": [],
   "source": [
    "for curve_set in selector.fdcurves.values():\n",
    "    for fdcurve in curve_set:\n",
    "        plt.figure()\n",
    "        fdcurve.plot_scatter()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d990a42",
   "metadata": {},
   "source": [
    "### Range selection by distance\n",
    "\n",
    "It is also possible to select a portion of an F,d curve based on distance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58ac7d14",
   "metadata": {},
   "outputs": [],
   "source": [
    "selector = lk.FdDistanceRangeSelector(fdcurves)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f7e8889",
   "metadata": {},
   "source": [
    "Again, we can retrieve the selected data just as with `FdRangeSelector`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3860d2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "original = fdcurves[\"Fd pull #6\"]\n",
    "sliced = selector.fdcurves[\"Fd pull #6\"][0]\n",
    "\n",
    "plt.figure()\n",
    "\n",
    "plt.subplot(2, 1, 1)\n",
    "original.plot_scatter(label=\"original\")\n",
    "sliced.plot_scatter(label=\"sliced\")\n",
    "plt.legend()\n",
    "\n",
    "plt.subplot(2, 1, 2)\n",
    "original.f.plot()\n",
    "sliced.f.plot(start=original.start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4c18baf",
   "metadata": {},
   "source": [
    "The returned F,d curves correspond to the longest contiguous (in time) stretch of data that falls within the distance thresholds. However, noise in the distance measurement can lead to short gaps of the time trace falling slightly outside of the thresholds, as illustrated below:\n",
    "\n",
    "To avoid premature truncation caused by this noise, there is an additional `max_gap` keyword argument to `FdDistanceRangeSelector` that can be used to adjust the acceptable length of noise gaps. The default values is zero, such that all data points are guaranteed to fall within the selected distance range. The effect of this argument is shown below for an F,d curve sliced with the same distance thresholds:\n",
    "\n",
    "### Range selection of single curve\n",
    "\n",
    "The selector widgets can also be easily accessed from single F,d curve instances:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9a4f7d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "fdcurve = fdcurves[\"Fd pull #6\"]\n",
    "t_selector = fdcurve.range_selector()\n",
    "d_selector = fdcurve.distance_range_selector(max_gap=3)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}