Exercise 6.1: Detecting dropped frames


In our lesson on scripting, 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.

This is an exercise in scripting and file management.

Solution


[1]:
import glob
import os
import re

Determining which frames were dropped is quite easy after completing the scripting lesson 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.

[2]:
# Specify Leica directory
leica_dir = 'data/leica_tiffs_renamed'

# Get list of files
file_list = sorted(glob.glob(os.path.join(leica_dir, '*_t*.tif')))

# String to search for (the time stamp)
regex = re.compile('_t\d+_')

# Pull frame numbers and check against running list
correct_frame_num = 0
dropped_frames = []
for fname in file_list:
    # Pull out _t00000000_ string
    time_str = regex.search(fname).group()

    # Get integer frame number
    fnum = int(time_str[2:-1])

    # Compare
    if fnum > correct_frame_num:
        # Record which frames were dropped
        dropped_frames += list(range(correct_frame_num, fnum))
        correct_frame_num = fnum + 1
    else:
        correct_frame_num += 1

# Which frames were dropped?
print('The following frames were dropped: ', dropped_frames)
The following frames were dropped:  [33, 113]

Computing environment

[3]:
%load_ext watermark
%watermark -v -p jupyterlab
Python implementation: CPython
Python version       : 3.9.12
IPython version      : 8.3.0

jupyterlab: 3.3.2