{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.1: Detecting dropped frames\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In [our lesson on scripting](l35_intro_to_scripting.ipynb), we worked on parsing a directory of images from a Leica microscope. The frame rate at which those data were taken was really fast for that microscope. On occasion, the system may miss a frame. Determine if any TIFF files are missing from the directory and, if so, which frame numbers are missing.\n", "\n", "This is an exercise in scripting and file management." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution \n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import glob\n", "import os\n", "import re" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Determining which frames were dropped is quite easy after completing [the scripting lesson](l31_intro_to_scripting.ipynb) where we have renamed the files. We just search through our renamed files and find any numbers that are out of order. *Note that the code below will not work if the files have not been renamed.*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The following frames were dropped: [33, 113]\n" ] } ], "source": [ "# Specify Leica directory\n", "leica_dir = 'data/leica_tiffs_renamed'\n", "\n", "# Get list of files\n", "file_list = sorted(glob.glob(os.path.join(leica_dir, '*_t*.tif')))\n", "\n", "# String to search for (the time stamp)\n", "regex = re.compile('_t\\d+_')\n", "\n", "# Pull frame numbers and check against running list\n", "correct_frame_num = 0\n", "dropped_frames = []\n", "for fname in file_list:\n", " # Pull out _t00000000_ string\n", " time_str = regex.search(fname).group()\n", "\n", " # Get integer frame number\n", " fnum = int(time_str[2:-1])\n", " \n", " # Compare\n", " if fnum > correct_frame_num:\n", " # Record which frames were dropped\n", " dropped_frames += list(range(correct_frame_num, fnum))\n", " correct_frame_num = fnum + 1\n", " else:\n", " correct_frame_num += 1\n", "\n", "# Which frames were dropped?\n", "print('The following frames were dropped: ', dropped_frames)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "hide-input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python implementation: CPython\n", "Python version : 3.9.12\n", "IPython version : 8.3.0\n", "\n", "jupyterlab: 3.3.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p jupyterlab" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }