volumetric imaging

Processing Volumetric Data

After collecting volumetric data with the Optotune you can now process your data as follows.

First, use sbxsplit() to generate separate data files for each “slice” of the optotune.

For example, if I have a data file gn7_008_001.sbx collected some data with an optotune waveform having a period of 5 the command

>> sbxsplit('gn7_008_001')

will generate a set of 5 files named gn7_008_001_ot_NNN, where NNN.sbx will range from 000 to 004, corresponding to each separate optical plane.

Second, the resulting files can then be aligned and segmented by treating each plane individually.  This will generate the corresponding *.signals and *.segment files.

Finally, you can call:

>> sbxmerge('gn7_008_001')

This will generate gn7_008_001_merged.signals and gn7_008_001_merged.segment.

The signals matrix will have as many rows as frames were present during acquisition while interpolating the missing samples for each plane (that is, when the optotune was sampling from other planes).

The interpolated signals are then deconvolved as usual to generate an estimate of spiking in the spks matrix.  From here on you can process the data as if it came from a typical experiment where only one plane was sampled.

The mask variable in the segmented file will have a size of [ny nx plane], where [ny, nx] is the size of each frame and plane is the period of the optotune waveform.  Each cell has a unique ID value corresponding to its column in the signals matrix.


Note that each setting of the optotune waveform is treated independently, even though thay may potentially represent the same plane (as it may happen using sinusoidal or triangular z-scanning).

Visualizing Individual Slices during Volumetic Imaging

During volumetric imaging, Scanbox displays all images as they are acquired. This can be inconvenient if we are trying to visually assess the activity of neurons within any one optical slice.  One solution is to display the images separately in a montage by means of a Scanbox plug-in.  However, it would be better to have such an option integrated into Scanbox.  A new feature offers this possibility.

Turning on a  “Slice” checkbox within the Optotune panel will make a pull-down menu within the display appear.  This menu allows selection of the optical slice you want to visualize. Unchecking the slice checkbox allows Scanbox to go back to its normal operation of showing all images in the incoming stream.

Here is a brief demo showing this feature.  Enjoy…




Automatic Optotune Calibration

We previously explained how to calibrate the optotune manually.  With the introduction of Knobby 2, we are able to make this process automatic.  You will now find a ‘Calibration’ button in the Optotune panel.  To use it, do the following:

  1. Set the optotune slider to its lowest value (slide all the way down)
  2. Bring some pollen grains into focus
  3. Stop focusing
  4. Make sure the data directory has a directory named xx0
  5. Click the ‘Calibrate’ button
  6. Sit back and relax.  Wait for the process to complete.

Knobby will run some z-stack acquisitions for different values of the optotune current setting.  The volumetric data will be used to calculate the shift in z at various values of the current. A panel will display the progress in processing the images (it takes about 2 min). Scanbox will then plot the raw data and a fit by a quadratic polynomial, which may look something like this:


Scanbox will write a calibration file which will take effect next time you start Scanbox.

After restarting Scanbox, you can check the calibration as follows:

  1. Set the optotune slider to its lowest value (slide all the way down)
  2. Set Knobby to super-fine mode
  3. Focus on some pollen grains
  4. Zero Knobby (XYZ)
  5. Move out-of-focus by moving the optotune slider up to some value
  6. Now bring sample back into focus using the z-axis knob
  7. Compare the reading of the z-axis in Knobby’s screen with the depth noted in the optotune panel. These two numbers should match very closely.


Surface sampling in Scanbox

Answering a request from colleagues in London, the upcoming version of the Scanbox firmware will allow users to change the depth of imaging on a line-by-line basis.

Up to now, during volumetric imaging, users were allowed to change the setting of the electronically tunable lens (ETL) once per frame (during the transition from one frame to another).

The new version offers the possibility of changing the depth (z) as a function of the line number (y).  For example, the image of a pollen grain below, was obtained using a sinusoidal modulation in depth.


There are at least two potential uses for this new feature.

First, one can increase the yield of imaged cells by first measuring a z-stack and then designing a (smooth) sampling surface z = f(y) that maximizes the number of cells that can be imaged.

For example, assume the projection of cell bodies within the volume is given by the scatter-plot below. Then sampling with the solid curve line will yield many more cells (red circles) than sampling with any horizontal plane (such as the dashed horizontal lines).


I will explain how to calculate an optimal (smooth) function of depth that maximizes the number of cells sampled in a separate post (the algorithm is too large to fit in the margin here.) [Actually, I added it below].

The surface sampling method offers a compromise between random access and volumetric imaging across planes, many of which may not contain many cells, thereby reducing the temporal resolution unnecessarily.  Have any groups done something like this before?

The second use of this new feature is that it can allow us to correct for the ringing in ETL focal plane that results from a step change.

If you have been doing volumetric imaging, where N frames are sampled at each depth, you might have realized that a few of the lines at the top of the frame on the first frame after a depth transition is screwy.  This is due to the ringing of the ETL. One can potentially use the ability to change ETL command on the fly to compensate for this ringing.

I have not yet implemented this.  Are there any Scanbox users doing volumetric imaging that are willing to help?


Given a set of cell body projections (y_i, z_i) we want to find a smooth function z=f(y) that intersects as many cell bodies as  possible.

We do this by restricting z(y) to be sum of the first N harmonics, z(y) =a_0+\sum_{k=1}^N a_k \cos ( 2 \pi k y + \phi_k ), so it is both smooth and periodic as well, which will prevent ringing in the ETL during fly back to the first line. The function has 2N+1 parameters.  For simplicity, here we normalize the total number of lines in the frame is normalized to be in the range [0,2 \pi].

Given a set of parameters, denote by d_i the minimum distance between the location of cell i and any of the points on the curve z(y). Our objective function is J= \sum_{k=1}^M \tanh ( (d_i - r_0)  \beta ).  Here, r_0 represents the average radius of a cell and \beta controls the sharpness of the error function near that boundary, M is the total number of cells in the volume. For a give sent of points, we used Matlab’s fminsearch to find the optimal parameters for the curve.

How much improvement in yield can we expect using surface sampling versus just one plane?

We ran a few simulations where the number of points is uniformly distributed within the volume and calculated the fraction of cells we can intersect as a function of the number of harmonics used. Zero harmonics means just the horizontal plane that intersects the maximum number of cells.yield  The graph on the left shows the results.

Even in the simple case of a uniform distribution one can more than double the yield expected from a single plane by having a few (~5-6) harmonics. In other words, using surface sampling we can double the number of cells with respect to a horizontal plane without any penalty in temporal resolution.  Not bad at all.

We will re-do this analysis with some actual volumetric data from our Lab soon. I suspect this estimate represents a lower bound on what can actually be achieved.


Bidirectional scanning and resonant driver calibration

Resonant mirrors vary somewhat in their resonant frequency and the settings of the mirror controller must be adjusted to obtain the best possible images in bidirectional mode.

A synchronization signal from the resonant board must be adjusted so that it is both symmetric and its phase is aligned with the zero crossings of the mirror velocity.

First, connect an oscilloscope to the HSYNC signal in the extension header of Scanbox.  The HSYNC signal is exposed on the header only in the latest version of the firmware, so please download and install it from Github before proceeding.  It is the fourth pin from the left on the top row of the connector as one looks from the top.  Tip: A jumper wire makes it easy to get access to this signal without shorting nearby pins with your scope probe.


Look at this signal on the oscilloscope while scanning.  Any modern oscilloscope will allow you to measure the period of the signal as well as the positive and negative pulse width.  In the example below, the resonant frequency is 7928 Hz, the positive pulse width is 63.22 us and the negative pulse width is 62.91 us.


The first step consists in making sure the signal is symmetric.  In other words, the positive and negative pulse widths should be equal.  You can adjust this by rotating the SYM pot on the resonant scanner driver board, which is the small board mounted on top of the main Scanbox card.  There are 3 pots on this card labeled SCALE, SYM and PHASE.  The SYM pot is the one in the middle.  Slowly rotate the pot until you get the high and low pulse widths to be within 0.5 us of each other.  In the example above the difference is 0.31um.

Now, measure the laser frequency at the wavelength you typically image.  This can be done by measuring the frequency of the signal that comes out of the SYNC OUT of the Chameleon.  It will be close (but not exactly) to 80 MHz.  In my case, I get 80.10 MHz.


Now, we need to calculate the number of samples that comprise one line.  This is ratio between the laser frequency and the resonant frequency divided by 8 (because each period is two lines and each pixel is 4 samples).  Given the numbers above, we get: round(80100000/7928/8) = 1263 samples per line.

Ideally, this is where lines should be “folded” during bidirectional scanning to get even and odd lines aligned.  So, lets start by setting the ncolbi variable to be 1263 (or the value that you found for your setup) for all 3 entries in the scanbox_config file.  (Remember you need to restart Scanbox for these new values to take effect).

Now scan a target that has some nice structure at a magnification of x1.  It may still be the case that the lines are not perfectly aligned.  While looking at the image, rotate the PHASE pot on the resonant controller to make the even and odd lines align as close as possible.  Warning: this pot is VERY sensitive, so turn it slowly.  The result does not need to be perfect, but close enough so that even and odd lines do not look obviously displaced from each other.

Now, with the target in place, run the auto-calibration procedure described here.  Scanbox should be able to find the optimal values that makes the even/odd lines align as much as possible.

Note: your PMT amplifiers should be set at full-bandwidth and a gain of 10^4, resulting in a bandwidth of 80 MHz for this procedure (and for imaging in general).

After going through this procedure you should be getting nice, sharp images in bidirectional mode across the entire field (both in the center and the sides of the image).

Update 9/28/16:

It is typical for some resonant mirrors to experience a small change in frequency with time.  A typical measurement looks like this:


Resulting in 2 parts per 8000 change in frequency over the first 5 min of operation.  This change can misalign the odd/even lines by ~1 or 2 pixels, which is visible.  To ensure stable calibration and operation during bidirectional scanning, we recommend you use Scanbox in continuous resonant mode.  Here, the resonant mirror will remain in operation throughout your acquisition session.  Of course, you should also perform the above calibration after the mirror has been operating for 5 min or more.

Calibrating the Optotune

If you have been using the Optotune, you must have noticed that the values displayed within the optotune panel for the setting of the slider and the parameters of the z-scanning waveform are unit-less and not very useful.

The latest update to Scanbox allows you to calibrate the Optotune and have all the units within the Optotune panel in micrometers.

A new configuration variable “optocal” in scanbox_config.m can be either empty or contain calibration data.  If optocal is empty, Scanbox behaves as it has done up to now — showing the unit-less values.

Start by setting optocal=[].

To calibrate simply take a green fluorescent slide and make a small marking on its surface.  Small dots with a fine, black sharpie should do.  Focus the microscope near the very top of the slide and pick one feature with the optotune slider set at zero.  Now, zero the position counters.  Our first point, corresponding to pairs of (values, depth) is (0,0).

Move the optotune slider up to a value near 200.  Then, compensate by lowering the objective (z-position) to bring back the feature you selected into focus. Once this is done, write down your next pair (value, depth), as the value of the slider and the depth you read in the position panel (or in Knobby). Continue this process, increasing the value of the optotune slider in steps of ~200, up to a value of ~2000.

Here is the set of measurements I obtained in my setup:


The curve saturates at a value of ~1760. This is because the current source has reached its maximum output voltage. The range of depths spanned by the optotune is ~340um (vertical axis). The smooth rising part of the curve, before saturation, is well approximated by a second-order polynomial (red curve).  To obtain the values of the coefficients use Matlab’s polyfit function by calling “polyfit(vals,depth,2)” (don’t include the saturating part of the curve!).  The coefficients of these polynomial are what the optocal variable should be set to.  In my case, I get the optimal coefficients to be [0.0001  0.0732 1.3162] and, therefore, I modified the config file so that optocal = [0.0001  0.0732  1.3162].


Once the optocal variable is set, you can restart Scanbox.  The result should be that all the values within the Optotune panel will read in microns (as shown on the left). This includes the slider and the parameters to the waveforms.  Moreover, as the function is slightly nonlinear, the actual waveforms will be linearized accordingly — so a linear ramp should very closely approximate a linear change in depth.