{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from lumicks import pylake\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FD curves\n",
    "\n",
    "[Download this page as a Jupyter notebook](_downloads/fdcurves.ipynb)\n",
    "\n",
    "The following code loads an HDF5 file and lists all of the FD curves inside of it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lumicks import pylake\n",
    "\n",
    "file = pylake.File(\"example.h5\")\n",
    "list(file.fdcurves)  # e.g. shows: \"['baseline', '1', '2']\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To visualizes an FD curve, you can use the built-in `.plot_scatter()` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pick a single FD curve\n",
    "fd = file.fdcurves[\"baseline\"]\n",
    "fd.plot_scatter()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, `.fdcurves` is a standard Python dictionary, so we can do standard `dict` thing with it. For example, we can iterate over all the FD curve in a file and plot them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for name, fd in file.fdcurves.items():\n",
    "    fd.plot_scatter()\n",
    "    plt.savefig(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, the FD channel pair is `downsampled_force2` and `distance1`. This assumes that the force extension was done by moving trap 1, which is the most common. In that situation the force measured by trap 2 is more precise because that trap is static. The channels can be switched with the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alt_fd = fd.with_channels(force='1x', distance='2')\n",
    "alt_fd.plot_scatter()\n",
    "\n",
    "# or as quick one-liner for plotting\n",
    "fd.with_channels(force='2y', distance='2').plot_scatter()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The raw data can be accessed as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Access the raw data: default force and distance channels\n",
    "force = fd.f\n",
    "distance = fd.d\n",
    "\n",
    "# Access the raw data: specific channels\n",
    "force = fd.downsampled_force1y\n",
    "distance = fd.distance2\n",
    "\n",
    "# Plot manually: FD curve\n",
    "plt.scatter(distance.data, force.data)\n",
    "# Plot manually: force timetrace\n",
    "plt.plot(force.timestamps, force.data)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}