{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bd9352e",
   "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": "fdf8fcbb",
   "metadata": {},
   "source": [
    "# Kymographs\n",
    "\n",
    "[Download this page as a Jupyter notebook](_downloads/9e90862a89b3ca28932e9f941e8165f6/kymographs.ipynb)\n",
    "\n",
    "To load an HDF5 file and lists all of the kymographs inside of it, run:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78b72621",
   "metadata": {},
   "outputs": [],
   "source": [
    "import lumicks.pylake as lk\n",
    "\n",
    "file = lk.File(\"example.h5\")\n",
    "list(file.kymos)  # e.g. shows: \"['reference', 'sytox']\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b347f958",
   "metadata": {},
   "source": [
    "Once again, `.kymos` is a regular Python dictionary so we can easily iterate over it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c396899b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot all kymos in a file\n",
    "for name, kymo in file.kymos.items():\n",
    "    kymo.plot_rgb()\n",
    "    plt.savefig(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9b3bdcb",
   "metadata": {},
   "source": [
    "Or just pick a single one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7784db83",
   "metadata": {},
   "outputs": [],
   "source": [
    "kymo = file.kymos[\"name\"]\n",
    "kymo.plot_red()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45ad7095",
   "metadata": {},
   "source": [
    "## Kymo data and details\n",
    "\n",
    "Access the raw image data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f4bb200",
   "metadata": {},
   "outputs": [],
   "source": [
    "rgb = kymo.rgb_image  # matrix with `shape == (h, w, 3)`\n",
    "blue = kymo.blue_image  # single color so `shape == (h, w)`\n",
    "\n",
    "# Plot manually\n",
    "plt.imshow(rgb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96829a84",
   "metadata": {},
   "source": [
    "Kymographs can also be sliced in order to obtain a specific time range. For example, one can plot the region of the kymograph between 175 and 180 seconds using:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d56cd9b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "kymo[\"175s\":\"180s\"].plot_red()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21794d6a",
   "metadata": {},
   "source": [
    "There are also several properties available for convenient access to the kymograph metadata:\n",
    "\n",
    "* `kymo.center_point_um` provides a dictionary of the central x, y, and z coordinates of the scan in micrometers relative to the brightfield field of view\n",
    "  \n",
    "* `kymo.size_um` provides a list of scan sizes in micrometers along the axes of the scan\n",
    "  \n",
    "* `kymo.pixelsize_um` provides the pixel size in micrometers\n",
    "  \n",
    "* `kymo.pixels_per_line` provides the number of pixels in each line of the kymograph\n",
    "  \n",
    "* `kymo.fast_axis` provides the axis that was scanned (x or y)\n",
    "  \n",
    "* `kymo.line_time_seconds` provides the time between successive lines\n",
    "  \n",
    "## Plotting and exporting\n",
    "\n",
    "There are also convenience functions to plot individual color channels and the full RGB image:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c655567",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.subplot(2, 1, 1)\n",
    "kymo.plot_rgb()\n",
    "plt.subplot(2, 1, 2)\n",
    "kymo.plot_blue()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "594910c7",
   "metadata": {},
   "source": [
    "The images can also be exported in the TIFF format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abd704dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "kymo.save_tiff(\"image.tiff\")"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}