NOTE IN 2026: Some of the limitations mentioned here are no longer applicable, this needs to be updated by current users.

Berg2 has a Galvo-Galvo-Res scan system (see this page in the ScanImage documentation for an explanation of the concept behind this system) that allows us to conduct near-simultaneous 2P photostimulation and imaging despite the lack of a dedicated photostimulation laser and optical path (with some important limitations). If you have questions about this process you can ask Michael about it if he’s still in the lab (or if he’s no longer in the lab but you ask very nicely).

Overview#

This technique relies on turning off the resonance scanner and using just the X and Y galvos to execute a custom scanning path using ScanImage’s Arbitrary Line Scanning feature. To maintain a reasonable “scan cycle” rate (i.e. “frame rate”), you won’t be able to raster scan your imaging ROIs to generate a 2D image, but you can define a coarser scan over the target area and then average together acquired pixels from that phase of the cycle to get a pretty good approximation of the mean fluorescence in that area (note that this means your data will be more affected by brain movement/Z-drift than in normal imaging modes, since you cannot run any motion-correction on the results).

Once you’ve created a scan path that includes both your photostimulation and imaging “ROIs”, you can write a custom user function in Matlab that will count the number of cycles that have occurred since the beginning of the acquisition and adjust the laser power levels in each ROI accordingly in order to turn the photostimulation on and off at a predetermined time.

IMPORTANT NOTE: one of the significant drawbacks of this technique is that due to intrinsic limitations of ScanImage, the precise timing of the photostimulation onset and offset will not be completely deterministic—depending on the state of the system’s internal buffer, the change in the laser power will sometimes vary by a couple scan cycles, up to maybe 500 msec or so. This means that this technique is not suitable for experiments in which you need to precisely time the stimulation to be triggered by some other external stimulus or event. You will however be able to determine exactly when the laser power changed after the fact (most easily by simply looking for the change in recorded fluorescence in the control and stimulus ROIs), so you shouldn’t have any trouble precisely aligning the photostim times to neural or behavioral events that may have been caused by the stimulation.

1. Acquire a reference stack#

This first step is just to give you an anatomy stack to help position your stimulation and imaging ROIs. It doesn’t need to be as accurate or detailed as an anatomy stack you might take at the end of an experiment, so try to keep the laser power as low as possible to avoid photobleaching or frying the brain.

  1. Make sure Configuration>Active Imaging System is set to ResScanner.
  2. Adjust the scan resolution in the Configuration window (choose whatever resolution you need to confidently place the ROIs)
  3. Manually position the objective so that the “zero” position in Z (i.e. the first place acquired if you grab a stack) is far above your dorsalmost imaging/stimulation targets – ideally 90 uM or so.
  4. In the “Fast Z Controls”, set the # slices and Step/Slice values.
  5. Step/Slice should be however small it needs to be for you to confidently target the desired location…2-3 uM should be fine, and you might be able to get away with larger steps.
  6. Set the # slices so that all your imaging/stimulation targets are visible in the stack (accounting for the extra space you left above it previously).
  7. In the same window, set the number of volumes to be acquired and averaged together (you can play around with this to see what works, but maybe start with ~10 or so), and Grab the stack when you are ready.
  8. I can’t remember for sure, but you might need to set Image Controls>Frame Rolling Average Factor to match the number of volumes you’re acquiring before you grab the stack.
  9. Once you are satisfied with the acquired stack, click Main Controls>Edit ROIs to open the “ROI group editor”. Click the Snap Shot button near the bottom to save your reference stack for later use.
  10. If you are using one PMT channel for GCaMP and another to visualize the Chrimson-expressing neurons (e.g. you’re using ChR:tdTomato vs. ChR:mVenus), you will have to take two of these snap shots, one in each channel, so you can see where to place both your photostimulation and imaging ROIs.

2. Set up ROIs and scan path#

This is where you will set up the scanning path that the laser will follow during each scan cycle. It can take a while, so once you have a somewhat consistent experimental protocol I strongly recommend that you save the ROI groups that you are using so you can load them as a starting point for the next experiment.

  1. Change Configuration>Active Imaging System to “LinScanner”, and then switch the Scan Type to “Line Scan”.
  2. In the “ROI group editor” window (re-open it if necessary, and make sure the snap shot you took earlier is checked at the bottom) either load a previously saved group of ROIs, or click Add ROI to begin creating a new group of ROIs.
  3. If you loaded a previously saved group, you can just adjust their positions, sizes, and depths as needed and skip step 3.
  4. If you are creating a new group of ROIs from scratch, you should create your scan sequence from “Park”, “Pause” and “Sinesquare” ROI function types. Here are some general tips/guidelines:
  5. “Parks” and “pauses” are used to give the XY galvos and/or Z-piezo time to move to the next stimulation/imaging ROI.
  6. In general you should Use “park” instead of “pause” if you are about to move to a new Z-plane, unless it is a relatively small change in Z. This makes it work better for reasons that are not entirely clear to me.
  7. You can adjust the density of the Sinesquare ROIs by changing the numeric value in the “Fcn Args” fields
  8. Make sure you set up a “photostim” and “control” ROI in the same Z-plane with the same size, density, and duration. The “photostim” ROI should be placed in your target stimulation region, and the “control” ROI should be placed somewhere as far from any visible fluorescence as possible (especially in the Chrimson PMT channel, but ideally in both).
  9. The control ROI exists to ensure that the total laser power being delivered into the brain does not change appreciably during photostimulation, in order to minimize behavioral and neural responses that are unrelated to Chrimson expression (definitely run some genetic control flies before doing too many experiments though, to make sure any effects you see are caused by the Chrimson-expressing neurons).
  10. Set the Beam Power in the control ROI to your desired stimulation power, and set it to the minimum laser power (0.3) in the photostim ROI.
  11. You should set the scanning speed in these ROIs to be as slow as possible for the most effective photostimulation without slowing down your cycle rate too much. A reasonable scan duration depends on both the size and density of the sinesquare, but you might use 50 ms as a starting point.
  12. The “imaging” ROIs should also be sinesquares, but the laser power in those should be set to whatever you are using to image (this should probably be lower than the photostim power). The scan duration in these ROIs should also be drastically shorter (maybe an order of magnitude) than in the photostim/control ROIs, assuming that they are a similar size.

3. Check piezo and galvo tuning#

Now you will have to check to make sure that the piezo and galvos are actually capable of executing the scan path you created in the specified amount of time. This will usually be an iterative process in which you go back and forth making adjustments to various “pause”, “park”, and sometimes “sinesquare” ROIs until the actual measured scan path nearly matches the one you requested.

  1. The majority of your adjustments will likely involve the piezo rather than the galvos, unless you try and make the galvos scan too fast in your imaging ROIs (if you hear a whining noise coming from the scope during the scanning this is probably what’s causing it…also this is probably bad for the galvos so try to fix it right away).
  2. You will have to add up the cumulative duration of the scanning steps in your ROI group to see where the important time periods are (i.e. the photostim/control and imaging ROIs). These are the parts of the scan cycle in which you need to make sure the actual reported piezo position is close to the desired position.
    1. Continue making adjustments to your ROI group settings and re-running the actuator movement test as iteratively until you are satisfied with the piezo position during the scanning epochs.
  3. You can perform a similar check for the galvos in the “Waveform controls” window. Click Update Waveforms and then click “Plot” in the row with “G” in the first column (short for “galvos”). It will attempt to run your current scanning pattern and plot the X and Y components including both the desired and actual measured galvo positions, which ideally should be nearly identical.
  4. If it’s way off, you will have to make some changes to your ROI groups to make sure you’re not asking the galvos to move faster than they’re capable of.
  5. Otherwise, go back to the “Waveform Controls” window and this time click the “Optimize” button in the same row as before. This will automatically make some minor adjustments to the command signal to optimize the galvo movement.

4. Configure custom user functions#

Finally, make sure that custom user functions are enabled and configured correctly. In the “User Functions” window you can choose from a variety of trigger events and assign a custom Matlab function to be called each time that event occurs (I believe the function can be located anywhere on the Matlab path). You can see documentation about how to use these functions here. The general idea is for the function to keep a running count of the number of frames that have been acquired, and then update the laser power in the stimulus and control ROIs at the appropriate times.

  1. Before you start imaging, make sure you have assigned the correct user functions to the desired events (I used ‘acqModeStart’, ‘frameAcquired’, and ‘acqModeDone’ in my code). If you like you can also enter arguments to be passed to the function when it is called, which I used to specify the timing and laser power of the photostimulation.
  2. You will also need to make sure that each trigger/function combination is enabled using the check boxes to the right.

5. Output data#

You can find more detail in the ScanImage documentation, but the data will be saved in four different files for each trial (instead of a single .tif file like with normal imaging).

  • One ends in “meta.txt” and just contains all the ScanImage settings and parameters that were used.
  • One ends in “.ref.dat” and contains the reference images that you drew the ROIs on—it is secretly a Matlab file and can be opened by renaming it with a “.mat” file extension.
  • One ends in “.scnnr.dat” and if I recall correctly contains a record of the actual piezo and galvo movements that were recorded.
  • One ends in “.pmt.dat” and this one contains the actual GCaMP data. The easiest way to extract it is to use the function “scanimage.util.readLineScanDataFiles()”. I believe this uses some of ScanImage’s internal processing and will therefore only work if you run it on the ScanImage computer before transferring your data to the server or your own computer.