**Reference**

HowToGuides

Manuals

LabAlumni

DataAnalysis

Advice for...

**Admin**

analysis:nsb2014:week12

Goals:

- Understand the format of Neuralynx video tracking data
- Learn to plot place cell tuning curves (rate maps), raw and smoothed
- Implement a basic Bayesian decoding algorithm
- Compare decoded and actual position by exporting to a movie file

Resources:

- (if you have not encountered Bayes' rule and conditional probability before) A brief introduction by Ken Murphy at UBC
- (for reference) Zhang et al. 1998, first application of decoding to place cell data, with nice explanations and derivations
- (for reference) Brown et al. 1998, an example of a more sophisticated decoding method

**Note**: this module uses an externally compiled function, `ndhist`

. By default, this will not work on non-Windows machines, but the source code is provided. If you are a non-Windows user and able to compile this, I would be grateful if you could share the binary here.

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.

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:

cd('C:\data\R042-2013-08-18'); %% cfg = []; cfg.load_questionable_cells = 1; cfg.useClustersFile = 0; S = LoadSpikes(cfg); cfg = []; pos = LoadPos(cfg);

NOTE: If you are using data that was saved with MClust 4.1, you should use the `cfg.useClustersFile = 0;`

option. (You need this even if your data was saved with MClust 3.5 -AC).

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.

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

t = [3250 5650]; S = restrict(S,t(1),t(2)); pos = restrict(pos,t(1),t(2));

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. There are also a few spikes on the pedestals, where the rat rests in between runs on the track.

This figure is a useful visualization of the raw data, but it is not a tuning curve.

A simple way to obtain a tuning curve is to bin the spikes (not in time, as in the previous module, but now in space):

SET_xmin = 10; SET_ymin = 10; SET_xmax = 640; SET_ymax = 480; SET_nxBins = 63; SET_nyBins = 47; spk_binned = ndhist(cat(1,spk_x',spk_y'),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); imagesc(spk_binned'); axis xy; colorbar;

`ndhist`

, for “N-dimensional histogram” can bin data in more than one dimension, in this case a 2-D grid with `x`

and `y`

position as the dimensions. It needs to know the minimum and maximum values for each dimension, as well as the number of bins in each; the data should be formatted with one dimension per row.

This gives:

This spike count can then be divided by how much time the rat spent in each of the bins (the “occupancy”, a value in seconds) to get a firing rate:

occ_binned = ndhist(cat(1,getd(pos,'x'),getd(pos,'y')),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); % this is a sample count, so need to convert to seconds (1/30s per sample) to get time VT_Fs = 30; tc = spk_binned./(occ_binned .* (1./VT_Fs)); % firing rate is spike count divided by time pcolor(tc'); shading flat; axis xy; colorbar; axis off;

Note that the occupancy is obtained by multiplying the number of samples in each bin with the sampling interval.

You should get:

`pcolor()`

ensures that bins which are NaN (such as would occur from division by zero occupancy) show up as white; `imagesc()`

shows NaNs the same way as zeros.

We now have a tuning curve for our cell, sometimes also called a “rate map” because we are dealing with (x,y) position. However, it suffers from the same issues arising from binning identified in the previous module. An improvement would be to bin at a much finer scale, and then use smoothing:

SET_nxBins = 630; SET_nyBins = 470; % more bins kernel = gausskernel([30 30],8); % 2-D gaussian for smoothing: 30 points in each direction, SD of 8 bins % spikes spk_binned = ndhist(cat(1,spk_x',spk_y'),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); spk_binned = conv2(spk_binned,kernel,'same'); % occupancy occ_binned = ndhist(cat(1,getd(pos,'x'),getd(pos,'y')),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); occ_binned = conv2(occ_binned,kernel,'same'); occ_binned(occ_binned < 0.01) = 0; tc = spk_binned./(occ_binned .* (1 / VT_Fs)); tc(isinf(tc)) = NaN; figure; pcolor(tc'); shading flat; axis off axis xy; colorbar;

This gives:

Now we have a nice smooth estimate of this cell's place field.

☛ What does the `occ_binned(occ_binned < 0.01) = 0;`

line accomplish? Uncomment it and re-run the code.

☛ What happens if you don't do `tc(isinf(tc)) = NaN;`

?

The procedure of Bayesian decoding is 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})\]

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 given by the tuning curve. Specifically, 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 Paninski et al. 2007) but this basic method works well for many applications.

The above equation gives the probability of observing $n$ spikes for a given average firing rate for a single neuron. How can we combine information across neurons? Again we take the simplest possible approach and assume that the spike count probabilities for different neurons are independent. This allows us to simply multiply the probabilities together to give:

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

An analogy here is simply to ask: if the probability of a coin coming up heads is $0.5$, what is the probability of two coints, flipped simultaneously, coming up heads? If the coins are independent then this is simply $0.5*0.5$.

Combining the above with Bayes' rule, and rearranging a bit, gives

\[P(\mathbf{x}|\mathbf{n}) = C(\tau,\mathbf{n}) P(\mathbf{x}) (\prod_{i = 1}^{N} f_i(\mathbf{x})^{n_i}) e^{-\tau f_i (\mathbf{x})} \]

This is more easily evaluated in vectorized MATLAB code. $C(\tau,\mathbf{n})$ is a normalization factor which we simply set to guarantee $\sum_x P(\mathbf{x}|\mathbf{n}) = 1$ (Zhang et al. 1998). For now, we assume that $P(\mathbf{x})$ (the “prior”) is uniform, that is, we have no prior information about the location of the rat and let our estimate be completely determined by the likelihood.

With the math taken care of, we can now start preparing the data for the decoding procedure. First we need to make sure we have tuning curves for all neurons.

Now we need to do the same for *all* cells. For now, we will revert to using the “low-resolution” version (with 63×47 bins) with a small amount of smoothing. Even though this is not as good of an estimate as the high-resolution version, our decoding will be super slow if we try to run it on a high-resolution smoothed estimate.

So first, let's inspect our updated tuning curve example:

kernel = gausskernel([4 4],2); % 2-D gaussian, width 4 bins, SD 2 SET_xmin = 10; SET_ymin = 10; SET_xmax = 640; SET_ymax = 480; SET_nxBins = 63; SET_nyBins = 47; spk_binned = ndhist(cat(1,spk_x',spk_y'),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); spk_binned = conv2(spk_binned,kernel,'same'); % smoothing occ_binned = ndhist(cat(1,getd(pos,'x'),getd(pos,'y')),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); occ_mask = (occ_binned < 5); occ_binned = conv2(occ_binned,kernel,'same'); % smoothing occ_binned(occ_mask) = 0; % don't include bins with less than 5 samples VT_Fs = 30; tc = spk_binned./(occ_binned .* (1 / VT_Fs)); tc(isinf(tc)) = NaN; pcolor(tc'); shading flat; axis xy; colorbar; axis off;

Then, we can do the same for all cells in our data set:

clear tc nCells = length(S.t); for iC = 1:nCells spk_x = interp1(pos.tvec,getd(pos,'x'),S.t{iC},'linear'); spk_y = interp1(pos.tvec,getd(pos,'y'),S.t{iC},'linear'); spk_binned = ndhist(cat(1,spk_x',spk_y'),[SET_nxBins; SET_nyBins],[SET_xmin; SET_ymin],[SET_xmax; SET_ymax]); spk_binned = conv2(spk_binned,kernel,'same'); tc = spk_binned./(occ_binned .* (1 / VT_Fs)); tc(isinf(tc)) = NaN; all_tc{iC} = tc; end

Note that we don't need to recompute the occupancy because it is the same for all cells.

Let's inspect the resulting tuning curves:

ppf = 25; % plots per figure for iC = 1:length(S.t) nFigure = ceil(iC/ppf); figure(nFigure); subplot(5,5,iC-(nFigure-1)*ppf); pcolor(all_tc{iC}); shading flat; axis off; end

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

☛ One cell has a completely green place map. What does this indicate, and under what conditions can this happen?

Since we see cells with fields in some different locations, it seems unlikely that a single sensory cue or nonspatial source can account for this activity. Of course, numerous experiments have demonstrated that many place cells do not depend on any specific sensory cue to maintain a stable firing field.

The tuning curves take care of the $f_i(x)$ term in the decoding equations. Now we need to get $\mathbf{n}$, which are simply spike counts:

clear Q; binsize = 0.25; tvec = t(1):binsize:t(2); for iC = length(S.t):-1:1 spk_t = S.t{iC}; Q(iC,:) = histc(spk_t,tvec); end nActiveNeurons = sum(Q > 0);

This “Q-matrix” of size `[nCells x nTimeBins]`

is the start of a number of analyses, such as the nice ensemble reactivation procedure introduced in Peyrache et al. 2009. Let's inspect it briefly:

% look at it imagesc(tvec,1:nCells,Q) set(gca,'FontSize',16); xlabel('time(s)'); ylabel('cell #');

You should see:

If you zoom in to a smaller slice of time, you will notice that there are gaps in the data, i.e. segments without any activity whatsoever. This is a quirk of this particular data set: the epochs when the rat is in transit between the pedestals and the track have been removed to facilitate spike sorting.

The final step before the actual decoding procedure is to reformat the tuning curves a bit to make the decoding easier to run. Instead of keeping them as a 2-D matrix, we just unwrap this into 1-D:

clear tc nBins = numel(occ_binned); nCells = length(S.t); for iC = nCells:-1:1 tc(:,:,iC) = all_tc{iC}; end tc = reshape(tc,[size(tc,1)*size(tc,2) size(tc,3)]); occUniform = repmat(1/nBins,[nBins 1]);

Aaandd… action!

len = length(tvec); p = nan(length(tvec),nBins); for iB = 1:nBins tempProd = nansum(log(repmat(tc(iB,:)',1,len).^Q)); tempSum = exp(-binsize*nansum(tc(iB,:),2)); p(:,iB) = exp(tempProd)*tempSum*occUniform(iB); end

Note, on my 2-year old laptop this takes about 2 minutes to run. The computation can be speeded up substantially by only looping over bins that have non-zero occupancy, but this would complicate the code a bit.

☛ Compare these steps with the equations above. There is no log in there; why does it appear here?

Finally we renormalize and set the posterior time windows without any neural activity to zero (done for display purposes):

p = p./repmat(sum(p,2),1,nBins); p(nActiveNeurons < 1,:) = 0;

The hard work is done. Now we just need to display the results. Before we do so, we should convert the rat's actual position into our binned form, so that we can compare it to the decoded estimate:

xBinEdges = linspace(SET_xmin,SET_xmax,SET_nxBins+1); yBinEdges = linspace(SET_ymin,SET_ymax,SET_nyBins+1); xTempD = getd(pos,'x'); xTempR = pos.tvec; yTempD = getd(pos,'y'); gS = find(~isnan(xTempD) & ~isnan(yTempD)); xi = interp1(xTempR(gS),xTempD(gS),tvec,'linear'); yi = interp1(xTempR(gS),yTempD(gS),tvec,'linear'); xBinned = (xi-xBinEdges(1))./median(diff(xBinEdges)); yBinned = (yi-yBinEdges(1))./median(diff(yBinEdges));

Now we can visualize the decoding (press Ctrl-C to break out of the loop):

goodOccInd = find(occ_binned > 0); for iT = 1:size(p,1) cla; temp = reshape(p(iT,:),[SET_nxBins SET_nyBins]); toPlot = nan(SET_nxBins,SET_nyBins); toPlot(goodOccInd) = temp(goodOccInd); pcolor(toPlot); axis xy; hold on; caxis([0 0.5]); shading flat; axis off; hold on; plot(yBinned(iT),xBinned(iT),'ow','MarkerSize',15); h = title(sprintf('t %.2f, nCells %d',tvec(iT),nActiveNeurons(iT))); if nActiveNeurons(iT) == 0 set(h,'Color',[1 0 0]); else set(h,'Color',[0 0 0]); end drawnow; pause(0.1); end

Initially, when the rat is moving around at the base of the maze (its actual position is indicated by the white `o`

), no decoding results will be shown because there are no cells active. However, after a few seconds the rat starts running up the stem of the maze, and some pixels in the plot will change color, indicating $P(\mathbf{x}|\mathbf{n})$, the “posterior probability” that is the output of the decoding procedure. As you can see, the decoding seems to track the rat's actual location as it moves.

☛ No decoding is available for those bins where no neurons are active, because we manually set the posterior to zero. However, there also seem to be some frames in the animation where some neurons are active (as indicated in the title), yet no decoded estimate is visible. What is the explanation for this?

By making the MATLAB animation into a movie file, it is often easier to explore the results. To do this, we can run the animation code above, with a few small modifications. First, before entering the main plotting loop, set the figure to be used to a specific size:

h = figure; set(h,'Position',[100 100 320 240]);

This is important first, to keep the size of the resulting movie file manageable (the above sets a 320×240 pixel figure size), and second, because many movie encoders (such as the excellent XVid) will only work with certain sizes.

Next, we need to store each frame into a variable that we can later write to file. Modify the last two lines inside the loop to:

f(iT) = getframe(gcf); % store current frame drawnow;

If you now run the code again, each frame gets stored in the `f`

variable as the loop runs. Break out of the loop after a few seconds to test the writing-to-file part:

fname = 'test.avi'; movie2avi(f,fname,'COMPRESSION','XVid','FPS',10);

The above will only work if you have the XVid codec installed: I highly recommend this because it creates movie files that are an order of magnitude smaller than uncompressed files. If you have trouble with XVid, you can of course still save an uncompressed file for now. For longer movies, it is often required to save a file, say, every 500 frames, to prevent the `f`

variable getting too large. These segments can then be merged with a video editing program such as VirtualDub (Windows only AFAIK; please suggest OSX/Linux alternatives if you know any that work well!).

Visual inspection of the animation or movie suggests that the decoding does a decent job of tracking the rat's true location. However, especially because of the number of parameters involved in the analysis (bin size, how firing rates are computed, the Poisson and independence assumptions, etc.) it is important to quantify how well we are doing.

☛ Modify the visualization code above to also compute a *decoding error* for each frame. This should be the distance between the rat's actual location and the location with the highest posterior probability (the “maximum a posteriori” or MAP estimate). Plot this error over time, excluding those bins where no cells were active. How does this error change over the course of the session? How does it change if you reduce the bin size for decoding to 100ms?

analysis/nsb2014/week12.txt · Last modified: 2018/07/07 10:19 (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International

## Discussion

(David Xu here, a student of Jeff, but using his account) Under section “Visual inspection”, the wiki suggests we use cfg.load_questionable_cells = 1, and produce a mapping for cell 5. However, the image on the wiki is not the same when following the code leading up to that section.

I had to actually set: cfg.load_questionable_cells = 0; cfg.useClustersFile = 0 in order to get the same result for cell 5.

(David Xu here, a student of Jeff, but using his account) To add on to my previous comment: Starting from the section “Preparing tuning curves for decoding”, my figures no longer match with cfg.load_questionable_cells = 0, I had to switch back to cfg.load_questionable_cells = 1.

Thank you, I have updated the text so that now everything should work with cfg.load_questionable_cells = 1.