{
 "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": [
    "# Confocal images\n",
    "\n",
    "[Download this page as a Jupyter notebook](_downloads/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,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lumicks import pylake\n",
    "\n",
    "file = pylake.File(\"example.h5\")\n",
    "list(file.scans)  # e.g. shows: \"['reference', 'bleach', 'imaging']\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once again, `.scans` is a regular Python dictionary so we can easily iterate over it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
   "metadata": {},
   "source": [
    "Or just pick a single one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scan = file.scans[\"name\"]\n",
    "scan.plot_red()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Access the raw image data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
   "metadata": {},
   "source": [
    "The images contain pixel data where each pixel is made up a multiple photon count samples collected by the scanner. For an even lower-level look at data, the raw photon count samples can be accessed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "photons = scan.red_photons\n",
    "plt.plot(photons.timestamps, photons.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The images can also be exported in the TIFF format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scan.save_tiff(\"image.tiff\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Multi-frame scans are also supported:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}