{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "955b5be0",
   "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": "11cb5a84",
   "metadata": {},
   "source": [
    "# Confocal images\n",
    "\n",
    "[Download this page as a Jupyter notebook](_downloads/cb9baaae2187ac0cc74a72037c19a183/images.ipynb)\n",
    "\n",
    "The following code uses scans as an example. Kymographs work the same way – just substitute `file.scans` with `file.kymos`. To load an HDF5 file and lists all of the scans inside of it, run:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af3e3627",
   "metadata": {},
   "outputs": [],
   "source": [
    "import lumicks.pylake as lk\n",
    "\n",
    "file = lk.File(\"example.h5\")\n",
    "list(file.scans)  # e.g. shows: \"['reference', 'bleach', 'imaging']\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75e7e35e",
   "metadata": {},
   "source": [
    "Once again, `.scans` is a regular Python dictionary so we can easily iterate over it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f8b5550",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot all scans in a file\n",
    "for name, scan in file.scans.items():\n",
    "    scan.plot_rgb()\n",
    "    plt.savefig(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e3d2d7",
   "metadata": {},
   "source": [
    "Or just pick a single one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af9a0a4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "scan = file.scans[\"name\"]\n",
    "scan.plot_red()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08a8fa79",
   "metadata": {},
   "source": [
    "## Scan data and details\n",
    "\n",
    "You can access the raw image data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75a912fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "rgb = scan.rgb_image  # matrix with `shape == (h, w, 3)`\n",
    "blue = scan.blue_image  # single color so `shape == (h, w)`\n",
    "\n",
    "# Plot manually\n",
    "plt.imshow(rgb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97a43ddb",
   "metadata": {},
   "source": [
    "The images contain pixel data where each pixel represents summed photon counts. For an even lower-level look at data, the raw photon count samples can be accessed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "965d1823",
   "metadata": {},
   "outputs": [],
   "source": [
    "photons = scan.red_photons\n",
    "plt.plot(photons.timestamps, photons.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1be310a",
   "metadata": {},
   "source": [
    "There are also several properties available for convenient access to the scan metadata:\n",
    "\n",
    "* `scan.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",
    "* `scan.size_um` provides a list of scan sizes in micrometers along the axes of the scan\n",
    "  \n",
    "* `scan.pixelsize_um` provides the pixel size in micrometers\n",
    "  \n",
    "* `scan.lines_per_frame` provides the number scanned lines in each frame (number of rows in the raw data array)\n",
    "  \n",
    "* `scan.pixels_per_line` provides the number of pixels in each line of the scan (number of columns in the raw data array)\n",
    "  \n",
    "* `scan.fast_axis` provides the fastest axis that was scanned (x or y)\n",
    "  \n",
    "* `scan.num_frames` provides the number of frames available\n",
    "  \n",
    "## Plotting and Exporting\n",
    "\n",
    "As shown above, there are convenience functions for plotting either the full RGB image or a single color channel. If a few pixels dominate the image, one might want to set the scale by hand. We can pass an extra argument to `plot_red` named `vmax` to accomplish this. This parameter gets forwarded to [`matplotlib.pyplot.imshow()`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib.pyplot.imshow):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49ea0c04",
   "metadata": {},
   "outputs": [],
   "source": [
    "scan.plot_red(vmax=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e85a1508",
   "metadata": {},
   "source": [
    "Multi-frame scans are also supported:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9859131b",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(scan.num_frames)\n",
    "print(scan.blue_image.shape)  # (self.num_frames, h, w) -> single color channel\n",
    "print(scan.rgb_image.shape)  # (self.num_frames, h, w, 3) -> three color channels\n",
    "\n",
    "scan.plot(frame=3)  # plot the third frame -- defaults to the first frame if no argument is given"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23d5269d",
   "metadata": {},
   "source": [
    "The images can also be exported in the TIFF format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2863099e",
   "metadata": {},
   "outputs": [],
   "source": [
    "scan.save_tiff(\"image.tiff\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4c33ff5",
   "metadata": {},
   "source": [
    "Scans can also be exported to video formats. Exporting the red channel of a multi-scan GIF can be done as follows for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a621086",
   "metadata": {},
   "outputs": [],
   "source": [
    "scan.export_video_red(\"test_red.gif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fde88610",
   "metadata": {},
   "source": [
    "Or if we want to export a subset of frames (the first frame being 10, and the last frame being 40) of all three channels at a frame rate of 40 frames per second, we can do this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5c743c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "scan.export_video_rgb(\"test_rgb.gif\", start_frame=10, end_frame=40, fps=40)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9100756a",
   "metadata": {},
   "source": [
    "For other video formats such as `.mp4` or `.avi`, ffmpeg must be installed. See [installation instructions](tutorial/../install.html#ffmpeg-installation) for more information on this."
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}