Tutorial Contents

Template-Based Detection

Store/Retrieve templates

Error metric

Average template

Subtract from trace

Increase frequency

Adaptive templates

Optimal scale/offset

Multi-Trace Templates

Contents

Template-Based Waveform (Spike) Detection

One common use of events is to mark the time of occurrence of waveform fragments with a particular shape, such as spikes generated by a single axon within an extracellular recording showing the combined activity of several axons. One way of doing this is through template-based event creation.

crayfish ventral nerve cord with caudal photoreceptor activity
Extracellular recording from the ventral nerve cord of a crayfish during a light stimulus. The light was switched on at about 600 ms, and switched off at 3000 ms (the off-switch generated an artefact, but the on-switch did not).

The data show an extracellular recording from the ventral nerve cord of a crayfish. A light was switched on shortly after the start of the recording, and most but not all of the spikes are from the caudal photoreceptor (CPR). These are the spikes we want to analyse.

You should see a single spike more-or-less centered in the screen. Normally you would locate this or a similar spike using the navigation controls within the program, but for the tutorial it is important that you pick the same spike that I did, so that the numbers work out the same.

Generate a template

The two cursors should bracket the displayed spike. Again, normally you would place these by eye, using the mouse to drag them to the correct positions.

Note that this is a modeless dialog, so you can carry out other actions without having to dismiss it (and if it gets in the way of the display, you can minimize it).

The dialog should now look like this:

Template recognition dialog
The Template recognition dialog with a spike template captured from a recording.

Detect template matches

You are now ready to search the recording for waveforms that match this captured template. The closeness of match required is set by the Allowed errorThere is also a Goodness-of-fit parameter, but this is simply the reciprocal of the allowed error multiplied by 100 parameter, which has a default value of 25 (the error metric is explained below). Any matches that are found will be marked as events in channel a.

There are two other facilities that can aid in waveform detection if viewed during the process, so we will activate these now.

You are now ready to search for matches to the template.

After a brief pause, the Template recognition dialog informs you that 305 waveforms have been found that match the template, and the scatter graph and the scope view now become populated. However, there are three problems:

  1. In the Scope view, the initial part of the waveforms are quite similar but there are clearly two categories distinguishable in the later part of the display.
  2. The Frequency vs time scatter graph shows a clear pattern that matches with the overall visual appearance of the recording in the main view, but there are many high-frequency outliers that are unlikely to come from the same neuron since they would require an unusually short refractory period.
  3. In the long-timebase main view (1) events mark many extracellular spikes, but this includes the low-frequency spikes at the start of the recording, before the light stimulus was applied.

It therefore seems likely that non-cpr spikes have been included in the analysis.

Now only 239 spikes are recognized as template matches. The spikes visible in the Scope view now look identical within the limits of noise, and the high-frequency outliers have disappeared from the Scatter graph. Furthermore, in the long timebase main view (1) the pre-stimulus spikes are no longer marked by events. This is much better!

A comparison of the Scope view and scattergraphs is shown below:

a
Scope view of template match 1
b
Template scope view error 12
c
Template scattergraph 1
d
Template scattergraph 12
Comparison of waveforms detected as template matches with different allowed errors. a. Scope view of waveforms with allowed error 25. Two categories are visible. b. Scope view of waveforms with allowed error 12. Only a single category is visible. c. Frequency vs time scattergraph with allowed error 25. There are numerous high frequency outliers. d. Frequency vs time scattergraph with allowed error 12. There are no high frequency outliers. Note that the Y axis is different in c and d because it has autoscaled.

The Scope view and Scatter graph data suggest that with the lower allowed error, the spikes generated by the crayfish CPR have been identified with reasonable accuracy.

The other spike

Deciding on the original spike to use as a template was in this case a matter of luck (with a bit of judgement). What happens if you chose the wrong spike?

  • In the zoomed-in window, set the start time to 100 ms.
    A single spike should be visible on the left. This is one of the pre-stimulus spikes.
  • Set vertical cursors at 106 and 107 ms.
  • In the Template recognition dialog
    • Change the Event channel to b.
    • Reset the Allowed error to 25.
    • Capture a new template.
    • Analyse the data.

The Scope view and Scatter graph look very much like they did with the previous template.

  • Reduce the Allowed error to 12 and re-Analyse.

Now the other spike has been identified. This neuron spikes at low frequency throughout the recording and is unaffected by the light stimulus.

If we had chosen the "wrong" spike for our initial template, we would have had two choices: try again after choosing another spike for the template and hope for better luck, or stay with the larger allowed error to detect both spikes, and use the spike sorting technique described here, or the Spike-Sorter Wizard described here, to separate them out.

Storing and Retrieving templates

When Dataview performs a waveform recognition analysis, it stores a copy of the template with the event channel, and thus we can retrieve the template from the event channel itself. This would allow us to re-analyse the data using the same template, should that be necessary.

Initially the Retrieve button in the dialog is disabled because the default event channel is set to an empty channel (in this case b) to reduce the risk of inadvertently overwriting events acquired previously. The events are stored in channel a, and so this is where the template is also stored.

Of course, templates are only available in event channels that were initially established by template recognition.

Template error metric

The error metric used in Dataview is simply the sum of the absolute deviation of the recorded waveform from the template, normalized such that an error of 100 is that generated by a waveform identical to the template, but shifted on the Y axis by half the peak-to-peak value within the template. This is calculated on a point-by-point basis throughoutOr rather, throughout the region of the recording selected for analysis. The user can opt to only analyse part of a long recording. the recording. If the error drops below the allowed value, the search continues until the error once again exceeds the allowed value, and the template-length section of data with the lowest error in the preceding within-tolerance region generates an event.

In the main view note that the template event now shows a value of 0. This is because the waveform is an exact match to the template - not surprising, of course, since this waveform is the template.

Two new spikes come into view with event values of 7.67 and 9.65. Both have been recognized as template matches within the allowed error of 12 - the values show how close the match is.

Error value

Determining an appropriate value for the allowed error is often done heuristicallyMeaning by trial-and-error, but that phrase introduces too many errors into the sentence, as well as not sounding very professional., using the Scope view and Scatter graph as aids. However, you can also examine the error distribution itself, which can help the decision.

The histogram shows the point-by-point error between the data and template throughout the analysis region. There is a single point in the zero-value bin at the far left of the histogram caused by the exact match (zero error) between the template and the data spike used to create the template. There is then a collection of bins, with a trough occurring at the (draggabble) red cursor. The X-axis value is the error, and the existence of this trough is the reason that the Allowed error value of 12 was chosen. (You can hover the mouse over the histogram to display the X-axis value in the Error at mouse box.)

You should now see a huge set of bins centred around an error of about 60. These are caused by the error between the template and the interspike baseline noise where there are no spikes. This generates the vast majority of the data points in the histogram at this scale.

The histogram should now look like this:

Template error histogram
The error distribution in the template waveform recognition process.

You can see the error signal distribution remains relatively flat up to about 46, before it takes off towards the huge baseline error. There is a trough at the location of the red cursor (error 12), and, with the eye of faith, you can also see a trough in the histogram at about error 28. Drag the red cursor to this position.

Note that the Allowed error in the main Template recognition dialog has now been set to the cursor value (about 28).

Saving the error as a trace

Another facility that helps in getting a feel for this type of analysis is to display the error signal and/or template as a trace.

The error signal is displayed on the lower trace of the new file as a Goodness-of-fit signal (equivalent to 100 divided by the error), so positive shifts in this trace indicate improvements in the fit.

The horizontal cursor should now be positioned at the goodness-of-fit threshold. The spike waveform is only recognized as a match to the template when the lower trace crosses the cursor.

Goodness-of-fit in template recognition
Spikes (upper trace) were marked as events in channel a using template waveform recognition with an allowed error of 12. The goodness-of-fit is shown in the lower trace. The acceptance threshold is indicated with a horizontal cursor placed at 8.33+.

Averaging templates

Note that there is a button From avg just below the Capture button. The template was originally defined by capturing a section of trace from the raw data, and obviously in most cases this waveform will be contaminated with noise. The template recognition algorithm will try to find matches to the noise as well as the underlying signal, which results in sub-optimal identification of genuine matches.

This time 243 events are recognized, which are slightly more that we had previously.

Subtract Average Spike from Trace

Once a spike has been identified it can be useful to remove it from a trace to see what is left.

Expand the timebase by setting it to 100 ms/div, and set the Start time to 500 ms (this is just so you can see the spikes better).

You now see precisely what was matched to the template. The key features of the spike are encompassed, but you do not see its entire waveform.

We are now ready to do the actual subtraction. You can dismiss the Scope view, or leave it running if you wish.

The original recording has a slight baseline offset due to to electronic drift in the amplifier. This is included in the average, and so when subtracting the average, the offset is removed over the time of the spike. This is not what we want, since it produces a discontinuity in the transformed trace.

The compensation works by taking the average of the first and last points within the template as a measure of the offset. This is then added back to the signal after removing the average waveform. This is one reason why it is important to encompass the entire spike waveform in events in the controlling channel.

 

a
Remove waveforms that match the template
b
Expanded view of remove template match
Removing template matches from a recording. a. Trace 1 shows an extracellular recording from the ventral nerve cord of a crayfish during a light stimulus. Spikes generated by the CPR are marked by events in channel a, which were then copied to channel b and adjusted to fully encompass the spikes . Trace 2 shows the recording after subtracting the average CPR waveform during events in channel b. The resulting trace shows non-CPR activity. b. An expanded view of (a) showing both CPR and non-CPR spikes in trace 1, and only the non-CPR spikes in trace 2.

On-the-fly frequency increase

This is exactly the same as file cpr that was used previously, except that the sample frequency has been reduced by a factor of 4 from 20 kHz to 5 kHz by decimation. This still meets the Nyquist criterion, but it means that a simple “join the dots” display of the waveforms produces considerable distortion.

a
Template upsample
b
Template matches at low sample rate
Template waveform recognition with up-sampling. a. The red trace is the raw template captured from the data. The black trace is the template up-sampled by a factor of 8. b. The waveforms recognized as matches to the up-sampled template.

The analysis takes considerably longer because the raw data are being up-sampled in real time as the search for a template match progresses. We could achieve a similar effect by using the Transform: Increase frequency facility to up-sample the entire file, but this would increase the overall file size by a factor of 8, whereas the on-the-fly increase leaves the original data unchanged.

If you want to retrieve the waveform of the up-sampled template, click List. If you want to get the average up-sampled waveform of the recognized spikes, click From avg before you click List. You can also write a new file containing just the up-sampled matched waveforms by checking the Write match-only file box before you click Analyse.

Adaptive template matching

It sometimes happens that a waveform undergoes a systematic change during the course of a recording session. This may be due to things like deterioration in the recording quality (e.g. suction electrodes losing their suck) or some systematic change in neural properties (spike broadening etc).  In these cases it is useful to have a template that can update itself as the waveform changes.

This shows an extracellular recording on which an event analysis was performed to detect a particular spike (the first spike in the record provided the template). The resulting events are stored in event channel b

After the event detection was performed, a filter was applied to this file first to gradually reduce the amplitude of the signal, and then to restore it again.

We will perform an identical event analysis on the filtered data.

The Event count box shows that now only 65 spikes are detected, some at the beginning and some at the end of the trace, but no spikes are detected in the middle where the amplitude has dropped.

You should see that event channel c (constructed using the adaptive template) closely matches event channel b (constructed before filtering the data), but that event channel a misses all the events in the middle of the record where they have shrunk in amplitude.

adaptive template match
Adaptive template matching. A template was constructed from an early spike. Event channel b (red) shows matches to the template occur throughout the file before a size filter was applied to shrink the data. Channel a (magenta) shows that the matches fail in the mid region after the filter was applied. Channel c (blue) shows that matches can be restored using the Adaptive template facility.

The adaptive template works by replacing the original template with a running average of the waveforms of recognised events. The number of events constituting the average is set in the Adaptive avg edit box (default 8), and as each new event is recognised, the earliest event in the average drops out to be replaced by the most recent. If you select an average of 1, then the template is successively replaced by each identified event, so that each found event constitutes the template for the next event search.

Optimally scaled template matching

The fixed template mode of recognition is used when you want to find events in the waveform that closely match the selected template in all respects (shape, size and dc-offset). However, there are occasions when you are looking for events that are the same shape as the template, but which may vary from it in both amplitude and mean potential. A classic example is the detection of miniature synaptic potentials, which all have a similar shape of waveform, but which vary considerably in amplitude.

In this situation the Optimally scaled/offset option is appropriate. This utilises an ingenious algorithm described by Clements & Bekkers (1997). The basic idea is that you start with a “pure” template with normalized amplitude and no noise. Clements and Bekkers constructed their templates using a mathematical equation to describe the waveform, but in DataView the template is captured from the data. The template is then passed through the data record on a successive point-by-point basis, and at each point it is optimally scaled and offset (the equations that do this are the clever bit of the algorithm) to produce the best possible match to the segment data at that point. The standard error between this optimally scaled and offset template and the data is then calculated. The ratio between the scale factor and the standard error is taken as a goodness-of-fit criterion. Since the original template is normalised to an amplitude of 1, the scale factor measures the real amplitude of the matched template in the signal, and thus the acceptance criterion is closely related to the signal-to-noise ratio of the selected waveform in the data. The goodness-of-fit criterion is thus affected by two factors, the size of the waveform in the data, and the closeness with which its waveform matches the optimally scaled template. A goodness-of-fit criterion value of about 4 usually provides a good balance between missing genuine event matches, and capturing false ones. (All of this information comes from the cited paper.)

Trace 1 is simulated data (constructed using Neurosim) showing quantal variation in miniature EJPs in response to constant-frequency 20 Hz stimulation. Trace 2 is exactly the same as trace 1, except a DC offset has been added to the mid region for testing purposes.

The first potential near the beginning of the record was used as a template, and using standard Template recognition with an Allowed error of 12 the program detected just 4 events in trace 1 that matched that template EJP. These are stored in channel a. In trace 2 only 2 events were detected (stored in channel b), because the offset in the mid region prevented a match to the other EJPs. None of the larger or smaller EJPs were detected, which is as it should be.

A fragment of the recording before and after analysis with the optimally scaled/offset facility is shown below:

Optimal scale and offset detection
Template waveform recognition with optimal scaling and offset. The data traces show stimulus-evoked miniature EJPs of varying sizes (simulated data) . The lower trace is identical to the upper trace, except a pulse DC offset has been introduced, visible as a step change in baseline in the later part of the display. An EJP waveform was selected as a template (not shown). With standard template recognition only the leftmost EJP matches the template (channel b). With optimal scaling and offset, all the EJPs in the recording were detected, including those within the region of DC offset (channel d).

To help the next stage of analysis:

Now we can try the optimally scaled/offset facility.

Now 61 waveforms are detected. The Scope view shows many EJPs, but also one almost flat sweeps. The Scatter graph shows a steady frequency pattern at 20 Hz, but with a pair of high outliers.

The problem is that there is a small deviation in the baseline noise that happens to match a massively reduced version of the EJP waveform.

There are now 60 waveforms recognized as a match, and the Scope view shows that these all look like EJPs. Furthermore the Scattergraph shows a uniform frequency distribution, which is as it should be because the EJPs were elicited by stimulation at fixed frequency.

What about the trace with the offset?

Again, 60 events are detected. If you switch the event channels in the Scope view and Scattergraph between c and d there is no change in the display. The optimal offset part of the algorithm successfully discounts the offset introduced into trace 2.

Multi-Trace Template Match

It is sometimes useful to detect repeating patterns of activity that span several different traces, and the Event edit: Multi-Trace Template option provides this facility.

This shows activity in the motorneuron pool of the 3 most posterior abdominal segments in a fly larvae, as monitored by fluorescence of a calcium indicator dye (data kindly provided by Dr Stefan Pulver, University of St Andrews). The waves of activity relate to abdominal contractions as the animal is crawling, and visual inspection shows at least two patterns. There are relatively sharp-peaked waves that progress from anterior to posterior (up the screen) indicating backwards crawling, and broader and flatter waves that are relatively synchronous, which indicate forwards crawling. The single events in event channels a, b and c mark waves in one sequence of a backwards crawl, while events in channels d, e and f mark a sequence of a forwards crawl. The events were inserted manually after visual inspection, to mark what was judged to be a “typical” episode of each type. Channels a and d have trace 1 set as their parent, b and e have trace 2, and c and f have trace 3.

fly larva crawling
Neural activity in a fly larva while crawling, monitored by a calcium indicator dye. See text for details.

 

Before starting the analysis it is helpful to activate the Scope view so that the recognitio parameters can be "tuned" while viewing the outcome.

Of course, at this time there are no events in channel g and so the Scope view is blank.

Note the waveform that displays in the dialog: this is the section of waveform in trace 1 encompassed by the single event in channel a, and it acts as one of the templates. You can view the other templates by selecting them from the drop-down Primary template list. The displayed template acts as the primary template, the non-displayed templates are the secondary templates. The difference will be explained in a moment.

The program searches trace 1 for all sequences that match the Primary template (assuming this is still defined by channel a). It uses the same search error criteria as described previously. In this case it finds 48 matches, as indicated by the Primary count value in the Recognition dialog. It then goes on to search for matches to the secondary templates, as follows. For each primary match, it searches trace 2 for a match to the template defined by channel b and trace 3 for a match to the template defined by channel c, but these matches have to occur in the same time relation to the primary match as the channel b and c events have to the channel a event. If does not find matches at the appropriate times, then the primary match itself is deleted. The result is that the events in the Destination channel g reflect episodes in the recording where the waveform pattern expressed in traces 1, 2 and 3 match, within the specified error, those underlying the template events. With the current settings there are 10 such episodes, of which several are visible on screen, with the match to the template episode highlighted.

The specifications as set require an exact match in timing, and this is quite restrictive.

Now the program searches within a time window that extends 0.5 s on either side of the “expected” time defined by the templates, and two more episode matches are discovered. However, there are many apparent episodes of backwards crawl which have not yet been detected, including several visible in the display.

Now the program scales and offsets each section of data to achieve the closest fit to the template, before checking the match in terms of the Allowed error (this is described in more detail earlier). This means that waveforms which are the same shape as the template, but differ in amplitude, can be detected as matches. Now we find 68 matches to the primary template, and 30 matches to the full template pattern.

You can now interactively adjust the Allowed error and/or Search slack (in the template dialog) to try to optimize detection, while observing the detected pattern in the Scope View. With an Allowed error of 35, 36 episodes of the backwards crawl pattern are detected (below a).

Increasing the allowance much beyond this starts to bring in patterns that are not good matches to the template.

Now we will try to find the other pattern. In the Template dialog box

a
multi-trace template matches
b
multi-trace template match
Crawling waves in a fly larva detected using the multi-trace template facility and displayed in the event-triggered Scope view. The average wave is shown superimposed on the individual raw waves. a. Backwards crawling: the wave progresses from anterior to posterior. b. Forwards crawling: the waves are more synchronous.

In the main view display click the Show all toolbar button (below, channels a–f hidden). The recording is from an experiment in which a drug was applied between the two obvious “glitch” markers in the traces. You can see that prior to drug application (the left of the screen) most of the episodes are matched by events in channel g, indicating that they are backwards crawls, but after drug application there are many more events in channel h, indicating forwards crawling. 

Fly larva crawling activity
Fly larva crawling activity. Events in channel g mark backwards crawling, events in channel h mark forward crawling.