by Tatsuo Okubo, Stephen Holtz, Helen Yang, Jenny Lu, Michael Marquis and Melanie Basnak

We all use different custom code to analyze our calcium imaging data, but there are some steps that most of us need to take. Next is a description of these steps, along with a list of the different methods used in the lab:

Removing flyback frames#

This step is required if you are doing volumetric imaging. When you acquire a volume, a piezo platform moves the objective, so that you can acquire slices that are further down in z as the volume progresses. When you reach your final slice, the platform needs to move for the scope to start acquiring at z=0 again. This takes a few frames, and you will need to discard them. You can see what this motion should ideally be and what it actually is in scanimage, in the ‘Fast Z controls’ window (making sure ‘Enable’ is checked), clicking the ‘actuator tuning’ button. It gives you a curve like the following:

In the image, we can see that the actual movement of the actuator deviates from the desired one, and in this case, it looks like the first 4 frames of each volume should be removed. Note that the time allowed for the piezo to return to the starting position is controlled by the value in the “Flyback Time” box in the Fast Z Controls window.

To remove the flyback frames from your data you have several options:

  • Use ScanImage’s metadata, which stores this information in the property SI.hFastZ.numDiscardFlybackFrames (warning: This might not work for Scanimage versions prior to 2019).
  • Collect the signal from the piezo moving the stage in the NiDaq and use that signal to determine the flyback frames (you will be able to reconstruct a curve like the red line from the figure above).

Registering images#

The brain often moves during an imaging session. In order to analyze the data, we first need to align the images acquired during the session to compensate for that movement: this is called registration. If you are doing single plane imaging, you will need to register in x and y, and if you’re doing volumetric imaging, you might need to register in x, y and z. There are different options for registration:

  • NoRMCorre: This algorithm was published in 2017. It performs non-rigid motion correction of calcium imaging data. Matlab’s implementation of this method can be found here.
  • Elastix: This is a toolbox for rigid and non-rigid registration of images. It is documented on a GitHub wiki. You can run it in Python using SimpleElastix or PyElastix.
  • Scikit-image: This is a toolkit for image processing in Python. It has a module for image registration.
  • Efficient subpixel motion registration: This algorithm performs 2D rigid registration, so it can be used for xy registration. The original paper can be found here, and the Matlab implementation of it can be found here.

If your imaging sessions are long, image registration can be pretty intensive, so some people run it in the O2 cluster. You can find information about setting up an account and running jobs in O2 here, and contact research computing if you have doubts. If your trials are not very long, running the registration in O2 might be more time consuming than running it locally, so some lab members choose to run it locally. This also makes it simpler to debug issues that might occur at this registration stage.

Selecting ROIs#

Once you have registered your images, you will want to select the regions of interest (ROIs) that you will use for measuring fluorescence changes. These might be somata, or specific neuropil regions (e.g., glomeruli). To select ROIs, most people in the lab build their own ROI selector, or adaptone from another lab member. Some options to make ROI selectors are:

  • Use the Matlab GUIDE designer (although this will soon be obsolete for GUI development)
  • Use the Matlab App designer (this is superior to GUIDE in basically every way)
  • Use Python or your programming language of choice.

If using Matlab (with or without a GUI designer tool), the images.roi.Freehand class is a convenient and flexible way to create ROIs.

If you are imaging somata, you could use automatic segmentation methods to detect your cell bodies and avoid the ROI selection. To go about this, you can:

  • Use a built software or package for automatic segmentation, like suite-2p.
  • Build a custom pipeline that segments the signal in each time point and then merges signals that come from the same cell (there are useful libraries/toolboxes for this in Python and Matlab)

Combining data across z-slices#

If you are doing volumetric imaging, the signal for each of your ROIs might be contained in more than one z slice. Different people use different methods to decide how to account for the different signal components in the fluorescence calculation:

  • You can sum the fluorescence across all slices that have a signal for that ROI.
  • You can average the fluorescence across all slices that have a signal for that ROI.
  • You can use the signal from the z slice where that ROI is the brightest.

In addition to this step, some lab members select a ‘background ROI’ in their image (a region that doesn’t have a cell body or neuropil, ideally outside of the brain), and subtract the average fluorescence from that background ROI from the fluorescence of their real ROIs to lower the noise. This could be particularly helpful in cases where there is bleedthrough from the visual stimulus into the PMTs, and some of the perceived changes in fluorescence might actually come from changes in the visual stimulus. In that case, subtracting the background ROI may get rid of that.

Calculating ΔF/F#

In order to normalize for differences in the number of GCaMP molecules in different ROIs, it is typical to divide changes in fluorescence (ΔF) by some measure of baseline fluorescence (F). The resulting metric (ΔF/F) is then reported as a fold-change over baseline

To calculate ΔF/F, we first need to obtain our baseline. Once again, different methods are used to obtain this.

  1. Choose a time window for calculating F (baseline fluorescience)
  • You can compute F from the full experiment.
  • You can compute F over a specific time window within the experiment, which might be a single long trial, or even a portion of a trial.
  • You can use a window prior to the stimulus presentation (for example a period of darkness prior to turning on the panels).

In terms of the boundaries of the baseline: 1) You can define the baseline as the 5th percentile of the fluorescence data in whatever time window you chose above. 2) You can define the baseline as the 10th percentile of the fluorescence data in whatever time window you chose above. 3) You can fit the fluorescence data in the period you defined above as that over which you will compute the baseline and with the sum of two exponentials (this is to take into account bleaching of the signal). You can then use the fit to obtain values of predicted fluorescence for each timepoint across the full experimental session, and those can serve as the baseline. This method will give you different baseline values for each timepoint.

#

Processing imaging data with behavior data

  • To downsample behavior data to a volumetric imaging rate (typically much slower than behavioral data acquisition rates), you can use a resampling method. Resampling is probably the best method to do this because downsample requires an integer downsampling ratio, and other downsampling methods (i.e. linspace in MATLAB) are less precise.