User Tools

Site Tools


This is an old revision of the document!

Spike train analysis II: tuning curves, encoding, decoding


  • Learn to estimate and plot tuning curves, raw and smoothed
  • Implement a basic Bayesian decoding algorithm
  • Compare decoded and actual position by computing the decoding error



To support adaptive behavior, activity in the brain must correspond in some way to relevant sensory events and planned movements, combine many sources of information into multimodal percepts, and recall traces of past events to inform predictions about the future. In other words, neural activity must somehow encode relevant quantities. For instance, it can be demonstrated behaviorally that many animals use estimates of their location and head direction to navigate towards a goal. Where, and how, are these quantities represented in the brain? What are the neural circuits that can compute and update these signals? How do place and direction estimates contribute to which way to go?

This information processing view of the brain has been extremely influential, as highlighted by the enduring appeal of Hubel and Wiesel's demonstrations that single cells in macaque V1 respond to bars of light not only within a particular region of visual space, but also with a specific orientation. Such cells are said to be tuned for orientation [of the bar] and a typical tuning curve would therefore look like this:

This tuning curve describes how the cell responds, on average, to different orientations of the stimulus. If the cell were to respond with the same firing rate across the range of stimulus orientations, then the cell is indifferent to this particular stimulus dimension: it does not encode it. However, because this cell clearly modulates its firing rate with stimulus orientation, it encodes, or represents (I use these terms interchangeably, but some disagree) this quantity in its activity.

We can turn this idea around and note that if orientation is encoded, this implies we can also decode the original stimulus from the cell's activity. For instance, if we noted that this cell was firing at a high rate, we would infer that the stimulus orientation is likely close to the cell's preferred direction. Note that this requires knowledge of the cell's tuning curve, and that based on one cell only, we are unlikely to be able to decode (or reconstruct, which means the same thing) the stimulus perfectly. The more general view is to say that the cell's activity provides a certain amount of information about the stimulus, or equivalently, that our (decoded) estimate of the stimulus is improved by taking the activity of this cell into account.

This module first explores some practical issues in estimating tuning curves of “place cells” recorded from the rat hippocampus. An introduction to a particular decoding method (Bayesian decoding) is followed by application to many simultaneously recorded place cells as a rat performs a T-maze task.

Estimating place cell tuning curves (place fields)

First, load the “place cell” data set also used in the previous module, which contains a number of spike trains recorded simultaneously from the dorsal CA1 area of the hippocampus:

please = []; please.load_questionable_cells = 1;
S = LoadSpikes(please);
pos = LoadPos([]);

The load_questionable_cells option in LoadSpikes() results in the loading of *._t files, in addition to the familiar *.t spike time files. The underscore extension indicates a cell with questionable isolation quality, likely contaminated with noise, spikes from other neurons, and/or missing spikes. In general, you do not want to use such neurons for analysis, but in this case we are not concerned with properties of individual neurons. We are instead interested in the information present in a population of neurons, and for this we will take everything we can get!

Visual inspection

Before looking at the data, we will first exclude the pre- and post-run segments of the data:

S = restrict(S,ExpKeys.TimeOnTrack,ExpKeys.TimeOffTrack);
pos = restrict(pos,ExpKeys.TimeOnTrack,ExpKeys.TimeOffTrack);

Now we can plot the position data:

plot(getd(pos,'x'),getd(pos,'y'),'.','Color',[0.5 0.5 0.5],'MarkerSize',1);
axis off; hold on;

Note that getd() is a utility function that retrieves data associated with a specific label; see Module 2 for details.

Next, we plot the spikes of a single cell at the location where the rat was when each spike was emitted:

iC = 7;
spk_x = interp1(pos.tvec,getd(pos,'x'),S.t{iC},'linear');
spk_y = interp1(pos.tvec,getd(pos,'y'),S.t{iC},'linear');
h = plot(spk_x,spk_y,'.r');

Note the use of interp1() here: it finds the corresponding x and y values for each spike time using linear interpolation. You should see:

This cell seems to have a place field just to the left of the choice point on the track (a T-maze). There are also a few spikes on the pedestals, where the rat rests in between runs on the track.

Estimating tuning curves

This figure is a useful visualization of the raw data, but it is not a tuning curve. As a first step towards estimating this cell's tuning curve (or encoding model, we should restrict the spikes to only those occurring when the rat is running on the track:

ENC_S = restrict(S,run_start,run_end);
ENC_pos = restrict(pos,run_start,run_end);
% check for empties and remove
keep = ~cellfun(@isempty,ENC_S.t); % note use of cellfun and its first argument, a function handle
ENC_S.t = ENC_S.t(keep);
ENC_S.label = ENC_S.label(keep);
S.t = S.t(keep);
S.label = S.label(keep);

We have created ENC_ versions of our spike trains and position data, containing only data from when the rat was running on the track (the run_start and run_end variables have been previously generated by a different script) and removed all cells from the data set that did not have any spikes on the track.

☛ Plot the above scatterfield again for the restricted spike train. Verify that no spikes are occurring off the track by comparing your plot to the previous one for the full spike trains, above.

To estimate tuning curves from the data, we need to divide spike count by time spent for each location on the maze. A simple way of doing that is to obtain 2-D histograms, shown here for the position data:

clear pos_mat;
pos_mat(:,1) = getd(ENC_pos,'y'); % construct input to 2-d histogram
pos_mat(:,2) = getd(ENC_pos,'x'); 
SET_xmin = 80; SET_ymin = 0; % set up bins
SET_xmax = 660; SET_ymax = 520;
SET_xBinSz = 10; SET_yBinSz = 10;
x_edges = SET_xmin:SET_xBinSz:SET_xmax;
y_edges = SET_ymin:SET_yBinSz:SET_ymax;
occ_hist = histcn(pos_mat,y_edges,x_edges); % 2-D version of histc()
no_occ_idx = find(occ_hist == 0); % NaN out bins never visited
occ_hist(no_occ_idx) = NaN;
occ_hist = occ_hist .* (1/30); % convert samples to seconds using video frame rate (30 Hz)
pcolor(occ_hist); shading flat; axis off; colorbar

We can do the same thing for the spikes of our example neuron:

% basic spike histogram
clear spk_mat;
iC = 7;
spk_x = interp1(ENC_pos.tvec,getd(ENC_pos,'x'),ENC_S.t{iC},'linear');
spk_y = interp1(ENC_pos.tvec,getd(ENC_pos,'y'),ENC_S.t{iC},'linear');
spk_mat(:,2) = spk_x; spk_mat(:,1) = spk_y;
spk_hist = histcn(spk_mat,y_edges,x_edges);
spk_hist(no_occ_idx) = NaN;
pcolor(spk_hist); shading flat; axis off; colorbar

..and finally simply divide one by the other:

% rate map
tc = spk_hist./occ_hist;
pcolor(tc); shading flat; axis off; colorbar
title('rate map');

This gives:

Note that from the occupancy map, you can see the rat spent relatively more time at the choice point compared to other segments of the track. However, the rough binning is not very satisfying. Let's see if we can do better with some smoothing:

kernel = gausskernel([4 4],2); % Gaussian kernel of 4x4 pixels, SD of 2 pixels (note this should sum to 1)
[occ_hist,~,~,pos_idx] = histcn(pos_mat,y_edges,x_edges);
occ_hist = conv2(occ_hist,kernel,'same');
occ_hist(no_occ_idx) = NaN;
occ_hist = occ_hist .* (1/30);
pcolor(occ_hist); shading flat; axis off; colorbar
spk_hist = histcn(spk_mat,y_edges,x_edges);
spk_hist = conv2(spk_hist,kernel,'same'); % 2-D convolution
spk_hist(no_occ_idx) = NaN;
pcolor(spk_hist); shading flat; axis off; colorbar
tc = spk_hist./occ_hist;
pcolor(tc); shading flat; axis off; colorbar
title('rate map');

Now you should get:

These are well-formed tuning curves we can use for decoding. Of course we could bin more finely for increased spatial resolution, but this will slow down the decoding, so for now it's not worth it.

Next we obtain a tuning curve for all our cells:

clear tc all_tc
nCells = length(ENC_S.t);
for iC = 1:nCells
    spk_x = interp1(ENC_pos.tvec,getd(ENC_pos,'x'),ENC_S.t{iC},'linear');
    spk_y = interp1(ENC_pos.tvec,getd(ENC_pos,'y'),ENC_S.t{iC},'linear');
    clear spk_mat;
    spk_mat(:,2) = spk_x; spk_mat(:,1) = spk_y;
    spk_hist = histcn(spk_mat,y_edges,x_edges);
    spk_hist = conv2(spk_hist,kernel,'same');
    spk_hist(no_occ_idx) = NaN;
    tc = spk_hist./occ_hist;
    all_tc{iC} = tc;

We can inspect the results as follows:

ppf = 25; % plots per figure
for iC = 1:length(ENC_S.t)
    nFigure = ceil(iC/ppf);
    pcolor(all_tc{iC}); shading flat; axis off;
    caxis([0 10]);

You will see a some textbook “place cells” with a clearly defined single place field. There are also cells with other firing patterns.

The data in this module computes tuning curves for location, but the idea is of course more general. For continuous variables in particular, it is a natural and powerful way to describe the relationship between two quantities – spikes and location in this case, but there is no reason why you couldn't do something like pupil diameter as a function of arm reaching direction, for instance!

Bayesian decoding

As noted in the introduction above, given that we have neurons whose activity seems to encode some stimulus variable (location in this case), we can attempt to decode that variable based on the neurons' time-varying activity.

A popular approach to doing this is “one-step Bayesian decoding”, illustrated in this figure (from van der Meer et al. 2010):

For this particular experiment, the goal of decoding is to recover the location of the rat, given neural activity in some time window. More formally, we wish to know $P(\mathbf{x}|\mathbf{n})$, the probability of the rat being at each possible location $x_i$ ($\mathbf{x}$ in vector notation, to indicate that there are many possible locations) given a vector of spike counts $\mathbf{n}$.

If $P(\mathbf{x}|\mathbf{n})$ (the “posterior”) is the same for every location bin $x_i$ (i.e. is uniform), that means all locations are equally likely and we don't have a good guess; in contrast, if most of the $x_i$ are zero and a small number have a high probability, that means we are confident predicting the most likely location. Of course, there is no guarantee that our decoded estimate will agree with the actual location; we will test this later on.

So how can we obtain $P(\mathbf{x}|\mathbf{n})$? We can start with Bayes' rule:

\[P(\mathbf{x}|\mathbf{n})P(\mathbf{n}) = P(\mathbf{n}|\mathbf{x})P(\mathbf{x})\]

If you have not come across Bayes' rule before, or the above equation looks mysterious to you, review the gentle intro by linked to at the top of the page. In general, it provides a quantitative way to update prior beliefs in the face of new evidence.

The key quantity to estimate is $P(\mathbf{n}|\mathbf{x})$, the probability of observing $n$ spikes in a given time window when the rat is at location $x$. At the basis of estimating this probability (the “likelihood” or evidence) lies the tuning curve: this tells us the average firing rate at each location. We need a way to convert a given number of spikes – whatever we observe in the current time window for which we are trying to decode activity, 3 spikes for cell 1 in the figure above – to a probability. In other words, what is the probability of observing 3 spikes in a 250ms time window, given that for this location the cell fires, say at 5Hz on average?

A convenient answer is to assume that the spike counts follow a Poisson distribution. Assuming this enables us to assign a probability to each possible spike count for a mean firing rate given by the tuning curve. For instance, here are the probabilities of observing different numbers of spikes $k$ (on the horizontal axis) for four different means ($\lambda = $1, 4 and 10):

In general, from the definition of the Poisson distribution, it follows that

\[P(n_i|\mathbf{x}) = \frac{(\tau f_i(\mathbf{x}))^{n_i}}{n_i!} e^{-\tau f_i (x)}\]

$f_i(\mathbf{x})$ is the average firing rate of neuron $i$ over $x$ (i.e. the tuning curve for position), $n_i$ is the number of spikes emitted by neuron $i$ in the current time window, and $\tau$ is the size of the time window used. Thus, $\tau f_i(\mathbf{x})$ is the mean number of spikes we expect from neuron $i$ in a window of size $\tau$; the Poisson distribution describes how likely it is that we observe the actual number of spikes $n_i$ given this expectation.

In reality, place cell spike counts are typically not Poisson-distributed ( Fenton et al. 1998) so this is clearly a simplifying assumption. There are many other, more sophisticated approaches for the estimation of $P(n_i|\mathbf{x})$ (see for instance