Changes Saved

How to use Flika to detect Ca2+ puffs

The algorithm for detecting puffs in detecting puffs as published in (Ellefsen K.L., Settle B., Parker I., Smith I.F. An algorithm for automated detection, localization and measurement of local calcium signals from camera-based imaging, Cell Calcium (2014)) uses the raw data to generate two new movies.  The first movie is the commonly used F/F0, generated by subtracting the scalar black level and dividing by the baseline fluorescence.  The second is the binary movie of detected events, where each pixel-frame is 'True' if an event was detected there or 'False' if it wasn't.  To generate these two movies, we need to create a script.  Install Flika if you haven't already, then run it.  I recommend opening Flika by running the code in the Python IDE PyCharm. Select Scripts->New... and create a new script called 'Detect Puffs'.  Now that new script will be located in the 'Scripts' menu.  Open it, and insert the following code:

first_flash_frame=0
last_flash_frame=0
black_level=700
first_ratio_frame=1
num_ratio_frames=100
min_rise_time=2
max_rise_time=5
last_black_level_frame=0
threshold_constant=6
blur_sigma=3


subtract(black_level)
data_window=ratio(first_ratio_frame,num_ratio_frames,'average')
gaussian_blur(blur_sigma,keepSourceWindow=True)
boxcar_differential_filter(min_rise_time,max_rise_time)
ratio(first_ratio_frame,num_ratio_frames,'standard deviation')
threshold(threshold_constant)
binary_window=remove_small_blobs(3, 40)
puffAnalyzer=average_origin(binary_window,data_window)

All the commands perform operations on the "Current Window".  The subtract command subtracts a value from each pixel-frame in the movie, and creates a new window.  The new window is the new "Current Window".  We subsequently ratio the new movie with the 'ratio' command, and store this in the 'data_window' variable.  We now have the FF0FF0 movie!  The rest of the steps (gaussian_blur, boxcar_differential_filter, ratio, set_value, threshold, remove_small_blobs) are all done to create the binary movie of detected events.  You can see the output of each step if, rather than running the entire script at the same time, you highlight the line you want to run and press 'F9' on your keyboard.  Another, more interactive way to see the results of some steps is to select the step in either the 'Image' or 'Process' dropdown menus.  Some of these commands allow you to preview the results of a step before performing it.  Internally, executing a step in the script interface or via the dropdown menus is identical.  The last command 'average_origin' creates a puffAnalyzer object which contains all the information about the puffs you've detected.  Play around with it and see what you can do. Here is a list of some non-obvious commands:

  • Right click and drag inside the ROI to move it around.  Drag near a corner to change its size.

  • Delete all the puffs by pressing the delete button when your mouse is inside the ROI.

  • Delete a single puff by pressing the delete button when that puff is selected and your mouse is outside the ROI.

  • Group all the puffs in the ROI into a site by pressing the 'g' button.

  • Ungroup by pressing the 'u' button. You will only be able to see this after you press the 'Toggle Sites' button.

  • To view if events were detected in ROI, right click on the ROI and select 'copy'. Then right click on the binary window and select 'paste'. Then right click on the new ROI and select 'plot'. A line will appear that will plot the average inside that ROI.

Here is a more complicated and realistic example script for puff detection:

open_file()
subtract(660) #subtract baseline
ratio(179,244,'average'); #ratio(first_frame, nFrames, ratio_type), now we are in F/F0
set_value(1,0,175) #remove the pre-laser baseline. This would otherwise disturb our filter
data_window=set_value(1, 433, 1143) #remove the UV flash for the same reason
high_pass=butterworth_filter(1,.003,1,keepSourceWindow=True) # High pass filter
low_pass=image_calculator(data_window,high_pass,'Subtract',keepSourceWindow=True) # we will use the low pass image as an approximation for the variance of the photon noise. low_pass.image[low_pass.image