{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "deaebe7a",
   "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": "8dd8d0f5",
   "metadata": {},
   "source": [
    "# Correlated stacks\n",
    "\n",
    "[Download this page as a Jupyter notebook](_downloads/5d661324235219aa73f340cf60f079a9/correlatedstacks.ipynb)\n",
    "\n",
    "Bluelake has the ability to export videos from the camera’s. These videos can be opened and sliced using `CorrelatedStack`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2c4364d",
   "metadata": {},
   "outputs": [],
   "source": [
    "stack = lk.CorrelatedStack(\"example.tiff\")  # Loading a stack.\n",
    "stack_slice = stack[2:10]  # Grab frame 2 to 9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5129c2e",
   "metadata": {},
   "source": [
    "Full color RGB images are automatically reconstructed using the alignment matrices from Bluelake if available. This functionality can be turned off with the optional `align` keyword:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c12d7653",
   "metadata": {},
   "outputs": [],
   "source": [
    "stack2 = lk.CorrelatedStack(\"example2.tiff\", align=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57a1d914",
   "metadata": {},
   "source": [
    "Quite often, it is interesting to correlate events on the camera’s to `channel` data. To quickly explore the correlation between images in a `CorrelatedStack` and channel data you can use the following function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf143fa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Making a plot where force is correlated to images in the stack.\n",
    "stack.plot_correlated(file.force1x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "105a5ee6",
   "metadata": {},
   "source": [
    "In some cases, additional processing may be needed, and we desire to have the data downsampled over the video frames. This can be done using the function `Slice.downsampled_over` using timestamps obtained from the `CorrelatedStack`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0e6ddb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Determine the force trace averaged over frame 2...9.\n",
    "file.force1x.downsampled_over(stack[2:10].timestamps)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa8184fa",
   "metadata": {},
   "source": [
    "The aligned image stack can also be exported to tiff format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6013915",
   "metadata": {},
   "outputs": [],
   "source": [
    "stack.export_tiff(\"aligned_stack.tiff\")\n",
    "stack[5:20].export_tiff(\"aligned_short_stack.tiff\") # export a slice of the CorrelatedStack"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f095ef6",
   "metadata": {},
   "source": [
    "Generally, the edges of an aligned image can become corrupted due interopolation artefacts. In this case, we can export a cropped region of interest by supplying the `roi` kwarg in the form `[min_x_pixel, max_x_pixel, min_y_pixel, max_y_pixel]`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2004e01c",
   "metadata": {},
   "outputs": [],
   "source": [
    "stack.export_tiff(\"aligned_cropped_stack.tiff\", roi=[20, 280, 20, 180])"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}