(Group) Bayesian Representational Similarity Analysis

Overview

This demo shows how to use the Bayesian Representational Similarity Analysis (Bayesian RSA, Cai et al., 2016,2019) with a simulated dataset.

Bayesian RSA builds a generative model for fMRI data which include a covariance structure as estimation target. The covariance structure specifies the distribution by which the unknown spatial neural response patterns for each task condition follow. In the model, the task-evoked patterns modulated by the design matrix, together with spontaneous activity and noise, constitute the fMRI data. By marginalizing the intermediate unknown variables including voxel-wise response amplitudes to the task conditions, the model computes a log likelihood for any possible covariance structure of condition-evoked response patterns to give rise to the fMRI data. It can then directly infer the most likely covariance structure, and convert it to the similarity structure of the neural representation for the task conditions in a region of interest. This is different from the traditional RSA approach which first estimates neural response patterns from data and then treats the noisy estimated patterns as true patterns to calculate their similarity structure. It was shown (Cai et al., 2016,2019) that the traditional approach can introduce bias to the estimated similarity structure and BRSA reduces this bias.

The BrainIAK Bayesian RSA module (brainiak.reprsimil.brsa) includes the following variants of Bayesian RSA:

This notebook demonstrates the usage of GBRSA on multiple subjects using simulated data. The usage of BRSA method is similar, and readers can refer to this example

To run GBRSA on single subject, you can provide data to GBRSA in the same format as for a group of subjects shown in this notebook, except that the data is a list of numpy array of one subject's data

Annotated Bibliography

  1. Cai, M. B., Schuck, N. W., Pillow, J. W., & Niv, Y. (2016). A Bayesian method for reducing bias in neural representational similarity analysis. Advances in Neural Information Processing Systems (pp. 4951-4959). link Describes potential biases in computing RSA that could lead to spurious results and introduces a Bayesian approach to reduce bias.

  2. Cai, M. B., Schuck, N. W., Pillow, J. W., & Niv, Y. (2019). Representational structure or task structure? Bias in neural representational similarity analysis and a Bayesian method for reducing bias. PLoS Computational Biology, 15(5), e1006299. link This paper improves the earlier version of BRSA by additionally modeling the spatial noise correlation and marginalizing voxel-wise noise parameters. The paper also introduces group BRSA and the use of cross-validation to compare an estimated model against a null model, and further extends BRSA to task-signal decoding, using the estimated similarity structure as an empirical prior for estimating neural patterns.

Table of contents

Notes on data preparation

When you apply this tool to real fMRI data, it is required that the data of each participant to be motion corrected. If multiple runs are acquired for each participant, they should be spatially aligned. Slice-timing correction is recommended.

You will need to have the mask of the Region of Interest (ROI) ready (typically defined anatomically or by independent tasks). nilearn provides tools to extract signal from mask. You can refer to this tutorial

Notes on model assumption

Please note that the model assumes that the covariance matrix U which all $\beta_i$ follow describe a multi-variate Gaussian distribution that is zero-meaned. This assumption does not imply that there must be both positive and negative responses across voxels. However, it means that (Group) Bayesian RSA treats the task-evoked activity against baseline BOLD level as signal, while in other RSA tools the deviation of task-evoked activity in each voxel from the average task-evoked activity level across voxels may be considered as signal of interest. Due to this assumption in (G)BRSA, relatively high degree of similarity may be expected when the activity patterns of two task conditions share a strong sensory driven components. When two task conditions elicit exactly the same activity pattern but only differ in their global magnitudes, under the assumption in (G)BRSA, their similarity is 1. However, if one take the assumption that only deviation of pattern from average patterns is signal of interest (which is currently not supported by (G)BRSA), their similarity would be -1 because the deviations of the two patterns from their average pattern are exactly opposite.

Load some package which we will use in this demo.

If you see error related to loading any package, you can install that package. For example, if you use Anaconda, you can use "conda install matplotlib" to install matplotlib.

You might want to keep a log of the output.

Simulate data

We want to simulate some data in which each voxel responds to different task conditions differently, but following a common covariance structure.

This example simulate 5 subjects. If you find the whole notebook runs for too long on your computer, you can reduce the number of simulated subjects by changing n_subj below.

Load an example design matrix.

To use BRSA, you need to prepare design matrix for the task with your favorate software, such as using 3ddeconvolve of AFNI, or using SPM or FSL.

The design matrix reflects your belief of how fMRI signal should respond to a task (if a voxel does respond). The common assumption is that a neural event that you are interested in will elicit a slow hemodynamic response in some voxels. The response peaks around 4-6 seconds after the event onset and dissipatess ~12 seconds after the event. Therefore, typically you convolve a time series A, composed of delta (stem) functions reflecting the time of each neural event belonging to the same category (e.g. all trials in which a participant sees a face), with a hemodynamic response function B, to form the hypothetic response of any voxel to such type of neural event. One convoluted time course can be generated this way for each type of event. The time courses corresponding to all events, put together, are called design matrix. Our goal is to figure out how the (spatial) response patterns of a population of voxels (in an Region of Interest, ROI) to different types of tasks (e.g., watching different categories of animals, different conditions of a cognitive task) are similar or disimilar. So we need the design matrix in order to estimate the similarity matrix we are interested in.

Basically, (G)BRSA just needs a numpy array which is in size of {time points} * {condition}. We can use the utility called ReadDesign from brainiak.utils to read a design matrix generated from AFNI. For design matrix saved as Matlab data file by SPM or or other toolbox, you can use scipy.io.loadmat('YOURFILENAME') and extract the design matrix from the dictionary returned. You can also generate design matrix using the function gen_design in brainiak.utils. It takes in (names of) event timing files in AFNI or FSL format (denoting onsets, duration, and weight for each event belonging to the same condition) and outputs the design matrix as numpy array.

details about design matrix

In this simulation, we use a design matrix of a task consisting of 16 different conditions, which by design cannot be fully counter-balanced in their orders. For simplicity, we repeat the design matrix of one run by 2 to 3 times, mimicking 2 to 3 fMRI runs with identical timing. Note that different subjects do not have to have the same number of voxels or time points. The timing of the task conditions can also differ across subjects. The simulation below reflects this.

In typical fMRI analysis, some nuisance regressors such as head motion, baseline time series and slow drift are also entered into regression. In using (G)BRSA, you should not include such nuisance regressors into the design matrix, because the spatial spread of such nuisance regressors might be quite different from the spatial spread of task related signal. Including such nuisance regressors in design matrix might influence the pseudo-SNR map, which in turn influences the estimation of the shared covariance matrix. But you may include motion time course in the nuisance parameter.

simulate noise

We simulate noise which is Gaussian Process (mimicking smooth noise in fMRI) in space and AR(1) in time

specify simulated similarity matrix

We assume the magnitude of response to each condition follows a common covariance matrix, underlying a similarity structure displayed below, for easy visualization of the effectiveness of different RSA approaches.

Let's keep the pattern of the ideal covariance / correlation below in mind and see how well BRSA can recover their patterns.

We make a realistic assumption that signal-to-noise ratio (SNR) is not constant across voxels. In the following, the map of pseudo-SNR (proportional to real SNR) is generated from a Gaussian Process defined on a "square" ROI, just for simplicity of code. Task-evoked response amplitudes are then simulated as samples drawn from multi-variate Gaussian distributions with covariance matrix equaling the ideal covariance matrix above, times the pseudo-SNR of each voxel.

Synthesize data by adding signal with noise

Now we take a look at the voxels with the lowest SNR and highest SNR for the participant with median level of overall SNR

The reason that the pseudo-SNRs in the example voxels are not too small, while the signal looks much smaller is because we happen to have low amplitudes in our design matrix. The true SNR depends on both the amplitudes in design matrix and the pseudo-SNR. Therefore, be aware that pseudo-SNR does not directly reflects how much signal the data have, but rather a map indicating the relative strength of signal in differerent voxels.

Fit Group Bayesian RSA to simulated data

In BRSA and GBRSA, the nuisance regressors in typical fMRI analysis (such as head motion signal) are replaced by principal components estimated from residuals after subtracting mean posterior estimation of task-related response. n_nureg tells the model how many principal components to keep from the residual as nuisance regressors, in order to account for spatial correlation in noise. When it is set to None and auto_nuisance=True, this number will be estimated automatically by an algorithm of Gavish & Dohono 2014. If you prefer not using this approach based on principal components of residuals, you can set auto_nuisance=False, and optionally provide your own nuisance regressors as a list (one numpy array per subject) as nuisance argument to GBRSA.fit(). In practice, we find that the result is much better with auto_nuisance=True.

Apparently one can imagine that the choice of the number of principal components used as nuisance regressors can influence the result. If you just choose 1 or 2, perhaps only the global drift would be captured. But including too many nuisance regressors would slow the fitting speed and might have risk of overfitting. Among all the algorithms we have tested with simulation data, the Gavish & Donoho algorithm appears the most robust and the estimate is closest to the true simulated number. But it does have a tendency to under-estimate the number of components, which is one limitation in (G)BRSA module.

Prepare onset information for model fitting

When you have multiple runs, the noise won't be correlated between runs. Therefore, you should tell BRSA when is the onset of each scan.

Note that the data (variable Y above) you feed to BRSA is the concatenation of data from all runs along the time dimension, as a 2-D matrix of time x space

Instantiate a GBRSA object

To reduce the running time at a cost of inaccuracy, you may specify SNR_prior='equal' and a smaller rho_bins parameter, as the code commented out below. There are multiple other parameters, we take the defaults in this example.

Fit model to the data

This may take a while.

Evaluation

Result of GBRSA

We can have a look at the estimated similarity in matrix gbrsa.C_.

We can also compare the ideal covariance above with the one recovered, gbrsa.U_

Compare with result of traditional RSA

In this approach, estimated activation patterns of each task condition ($\beta$) are obtained by regressing data against design matrix. And then Pearson correlation or other (dis)similarity metric is calculated between the noisy estimates of $\beta$s of different conditions.

Compare the root mean squared error between the estimated similarity and simulated similarity structures

Evaluating estimated pseudo-SNR map against the simulated SNR map

If you have chosen SNR_prior='equal', the estimated pseudo-SNR map will be flat

Examine the recovery of activation pattern

Scatter plot of the recovered $\beta$s and true $\beta$s. Each column is result from one subject. The closer the dots are close to the diagnal line, the better the recovery is. Top: result by GBRSA. Bottom: result by standard regression analysis

Other usage: "decoding" task-related signal from new data

Now we generate a new data set. We keep the signal the same as in training data, but generate new noise.

We can use the transform() function of gbrsa to estimate the "design matrix" in this new dataset.

Model selection by cross-validataion

Both BRSA and GBRSA can compare full model against a null model (without task-related responses) by cross-validating the parameters of one model learnt from some training data on some testing data. GBRSA provides a score() function, which returns a pair of cross-validated log likelihood for the testing data.

The first returned item is a numpy array of the cross-validated log likelihood of the model you have specified, for the testing data of all the subjects. The second is a numpy arrary of those of a null model which assumes everything else the same except that there is no task-related activity.

Notice that comparing the score of your model of interest against its corresponding null model is not the only way to compare models. You might also want to compare against a model using the same set of design matrix, but a different rank (especially rank 1, which means all task conditions have the same response pattern, only differing in their magnitude).

In general, in the context of GBRSA, a model means the timing of each event and the way these events are grouped, together with other trivial parameters such as the rank of the covariance matrix and the number of nuisance regressors. All these parameters can influence model performance. In future, we may provide interface to evaluate the predictive power for the data by different predefined similarity matrix or covariance matrix.

Avoid false discovery

If a model is fit to pure noise, some result of similarity structure can still be returned. How do we know if the result is valid?

One approach is to examine the cross-validation score on left-out scans acquired with the same task.

Below, we fit the model to data containing only noise and cross-validate it on another set of noise.

When fitted to noise, the result should be meaningless. Ideally the cross-validated log likelihood of the full model should be smaller than that of the null model.

We can see that the difference is smaller but full model generally performs slightly worse, because of overfitting. This is expected.

So, after fitting a model to your data, you should also check cross-validated log likelihood on separate runs from the same group of participants, and make sure your model is at least better than a null model before you trust your similarity matrix.

Another diagnostic of bad model to your data is very small diagonal values in the shared covariance structure U_ as shown below:

Summary

(Group) Bayesian RSA models the covariance structure of the task-related activation patterns, and how the activation patterns and task-unrelated fluctuations together contribute to the temporal correlation in the fMRI data. By simulating fMRI data containing both task-related activation and spatio-temporally correlated noise, we show that (Group) Bayesian RSA recovers the similarity structure of activation patterns better than traditional RSA and suffers less from spurious correlation structure. GBRSA can additionally be used to decode task-related signals from new data. GBRSA also provides the ability to cross-validate the full model containing task-related signals against a null model assuming only spatiotemporally correlated noise, to prevent false discoveries.