Compute RSA between RDMs#

This example showcases the most basic version of RSA: computing the similarity between two RDMs. Then we continue with computing RSA between many RDMs efficiently.

# sphinx_gallery_thumbnail_number=2

# Import required packages
import pandas as pd
from matplotlib import pyplot as plt
import mne
import mne_rsa

MNE-Python contains a build-in data loader for the kiloword dataset, which is used here as an example dataset. Since we only need the words shown during the experiment, which are in the metadata, we can pass preload=False to prevent MNE-Python from loading the EEG data, which is a nice speed gain.

data_path = mne.datasets.kiloword.data_path(verbose=True)
epochs = mne.read_epochs(data_path / "kword_metadata-epo.fif")

# Show the metadata of 10 random epochs
epochs.metadata.sample(10)
WORD Concreteness WordFrequency OrthographicDistance NumberOfLetters BigramFrequency ConsonantVowelProportion VisualComplexity
439 standard 3.400000 2.813581 2.75 8.0 523.500000 0.750000 67.901905
28 bone 6.150000 2.680336 1.10 4.0 815.500000 0.500000 74.291708
175 crystal 6.000000 2.340444 2.85 7.0 402.857143 0.714286 57.555947
23 site 5.050000 2.971740 1.20 4.0 543.000000 0.500000 59.566074
848 ritual 3.900000 2.477121 2.50 6.0 275.666667 0.500000 51.703629
629 special 2.550000 3.526598 2.50 7.0 371.714286 0.571429 64.664890
325 miner 6.050000 1.623249 1.25 5.0 986.000000 0.600000 64.068807
836 device 4.450000 2.732394 1.90 6.0 556.833333 0.500000 67.498168
711 nautilus 4.333333 1.041393 3.40 8.0 377.375000 0.500000 58.136889
115 engine 5.650000 2.897077 2.20 6.0 471.833333 0.500000 70.399609


Compute RDMs based on word length and visual complexity.

metadata = epochs.metadata
rdm1 = mne_rsa.compute_rdm(metadata.NumberOfLetters, metric="euclidean")
rdm2 = mne_rsa.compute_rdm(metadata.VisualComplexity, metric="euclidean")

# Plot the RDMs
mne_rsa.plot_rdms([rdm1, rdm2], names=["Word length", "Vis. complexity"])
Word length, Vis. complexity
<Figure size 400x200 with 3 Axes>

Perform RSA between the two RDMs using Spearman correlation

rsa_result = mne_rsa.rsa(rdm1, rdm2, metric="spearman")
print("RSA score:", rsa_result)
RSA score: 0.02643988328911863

We can compute RSA between multiple RDMs by passing lists to the mne_rsa.rsa() function.

# Create RDMs for each stimulus property
columns = metadata.columns[1:]  # Skip the first column: WORD
rdms = [mne_rsa.compute_rdm(metadata[col], metric="euclidean") for col in columns]

# Plot the RDMs
fig = mne_rsa.plot_rdms(rdms, names=columns, n_rows=2)
fig.set_size_inches(12, 4)

# Compute RSA between the first two RDMs (Concreteness and WordFrequency) and
# the others.
rsa_results = mne_rsa.rsa(rdms[:2], rdms[2:], metric="spearman")

# Pack the result into a Pandas DataFrame for easy viewing
print(pd.DataFrame(rsa_results, index=columns[:2], columns=columns[2:]))
Concreteness, WordFrequency, OrthographicDistance, NumberOfLetters, BigramFrequency, ConsonantVowelProportion, VisualComplexity
               OrthographicDistance  NumberOfLetters  BigramFrequency  ConsonantVowelProportion  VisualComplexity
Concreteness               0.031064         0.026832        -0.004681                  0.005647          0.004263
WordFrequency              0.058385         0.013607         0.001970                 -0.003850         -0.009620

What if we have many RDMs? The mne_rsa.rsa() function is optimized for the case where the first parameter (the “data” RDMs) is a large list of RDMs and the second parameter (the “model” RDMs) is a smaller list. To save memory, you can also pass generators instead of lists.

Let’s create a generator that creates RDMs for each time-point in the EEG data and compute the RSA between those RDMs and all the “model” RDMs we computed above. This is a basic example of using a “searchlight” and in other examples, you can learn how to use the searchlight generator to build more advanced searchlights. However, since this is such a simple case, it is educational to construct the generator manually.

The RSA computation will take some time. Therefore, we pass a few extra parameters to mne_rsa.rsa() to enable some improvements. First, the verbose=True enables a progress bar. However, since we are using a generator, the progress bar cannot automatically infer how many RDMs there will be. Hence, we provide this information explicitly using the n_data_rdms parameter. Finally, depending on how many CPUs you have on your system, consider increasing the n_jobs parameter to parallelize the computation over multiple CPUs.

epochs.resample(100)  # Downsample to speed things up for this example
eeg_data = epochs.get_data()
n_trials, n_sensors, n_times = eeg_data.shape


def generate_eeg_rdms():
    """Generate RDMs for each time sample."""
    for i in range(n_times):
        yield mne_rsa.compute_rdm(eeg_data[:, :, i], metric="correlation")


rsa_results = mne_rsa.rsa(
    generate_eeg_rdms(),
    rdms,
    metric="spearman",
    verbose=True,
    n_data_rdms=n_times,
    n_jobs=1,
)

# Plot the RSA values over time using standard matplotlib commands
plt.figure(figsize=(8, 4))
plt.plot(epochs.times, rsa_results)
plt.xlabel("time (s)")
plt.ylabel("RSA value")
plt.legend(columns)
plot rsa between rdms
C:\Users\wmvan\projects\mne-rsa\examples\plot_rsa_between_rdms.py:90: FutureWarning: The current default of copy=False will change to copy=True in 1.7. Set the value of copy explicitly to avoid this warning
  eeg_data = epochs.get_data()

  0%|                                                                                                                                                                                                                                               | 0/102 [00:00<?, ?RDM/s]
  1%|██▎                                                                                                                                                                                                                                    | 1/102 [00:01<01:55,  1.15s/RDM]
  2%|████▌                                                                                                                                                                                                                                  | 2/102 [00:02<01:55,  1.15s/RDM]
  3%|██████▊                                                                                                                                                                                                                                | 3/102 [00:03<01:43,  1.05s/RDM]
  4%|█████████                                                                                                                                                                                                                              | 4/102 [00:04<01:38,  1.01s/RDM]
  5%|███████████▎                                                                                                                                                                                                                           | 5/102 [00:05<01:34,  1.03RDM/s]
  6%|█████████████▌                                                                                                                                                                                                                         | 6/102 [00:06<01:31,  1.04RDM/s]
  7%|███████████████▊                                                                                                                                                                                                                       | 7/102 [00:07<01:33,  1.01RDM/s]
  8%|██████████████████                                                                                                                                                                                                                     | 8/102 [00:07<01:31,  1.03RDM/s]
  9%|████████████████████▍                                                                                                                                                                                                                  | 9/102 [00:08<01:29,  1.04RDM/s]
 10%|██████████████████████▌                                                                                                                                                                                                               | 10/102 [00:09<01:29,  1.02RDM/s]
 11%|████████████████████████▊                                                                                                                                                                                                             | 11/102 [00:11<01:37,  1.08s/RDM]
 12%|███████████████████████████                                                                                                                                                                                                           | 12/102 [00:12<01:33,  1.04s/RDM]
 13%|█████████████████████████████▎                                                                                                                                                                                                        | 13/102 [00:13<01:37,  1.10s/RDM]
 14%|███████████████████████████████▌                                                                                                                                                                                                      | 14/102 [00:14<01:36,  1.10s/RDM]
 15%|█████████████████████████████████▊                                                                                                                                                                                                    | 15/102 [00:15<01:30,  1.04s/RDM]
 16%|████████████████████████████████████                                                                                                                                                                                                  | 16/102 [00:16<01:27,  1.01s/RDM]
 17%|██████████████████████████████████████▎                                                                                                                                                                                               | 17/102 [00:17<01:24,  1.01RDM/s]
 18%|████████████████████████████████████████▌                                                                                                                                                                                             | 18/102 [00:18<01:26,  1.03s/RDM]
 19%|██████████████████████████████████████████▊                                                                                                                                                                                           | 19/102 [00:19<01:27,  1.06s/RDM]
 20%|█████████████████████████████████████████████                                                                                                                                                                                         | 20/102 [00:20<01:24,  1.03s/RDM]
 21%|███████████████████████████████████████████████▎                                                                                                                                                                                      | 21/102 [00:21<01:21,  1.00s/RDM]
 22%|█████████████████████████████████████████████████▌                                                                                                                                                                                    | 22/102 [00:22<01:23,  1.04s/RDM]
 23%|███████████████████████████████████████████████████▊                                                                                                                                                                                  | 23/102 [00:23<01:24,  1.07s/RDM]
 24%|██████████████████████████████████████████████████████                                                                                                                                                                                | 24/102 [00:24<01:21,  1.05s/RDM]
 25%|████████████████████████████████████████████████████████▎                                                                                                                                                                             | 25/102 [00:25<01:21,  1.06s/RDM]
 25%|██████████████████████████████████████████████████████████▋                                                                                                                                                                           | 26/102 [00:26<01:18,  1.03s/RDM]
 26%|████████████████████████████████████████████████████████████▉                                                                                                                                                                         | 27/102 [00:27<01:17,  1.03s/RDM]
 27%|███████████████████████████████████████████████████████████████▏                                                                                                                                                                      | 28/102 [00:28<01:16,  1.03s/RDM]
 28%|█████████████████████████████████████████████████████████████████▍                                                                                                                                                                    | 29/102 [00:29<01:12,  1.00RDM/s]
 29%|███████████████████████████████████████████████████████████████████▋                                                                                                                                                                  | 30/102 [00:30<01:10,  1.02RDM/s]
 30%|█████████████████████████████████████████████████████████████████████▉                                                                                                                                                                | 31/102 [00:31<01:11,  1.01s/RDM]
 31%|████████████████████████████████████████████████████████████████████████▏                                                                                                                                                             | 32/102 [00:32<01:10,  1.01s/RDM]
 32%|██████████████████████████████████████████████████████████████████████████▍                                                                                                                                                           | 33/102 [00:33<01:07,  1.02RDM/s]
 33%|████████████████████████████████████████████████████████████████████████████▋                                                                                                                                                         | 34/102 [00:34<01:05,  1.04RDM/s]
 34%|██████████████████████████████████████████████████████████████████████████████▉                                                                                                                                                       | 35/102 [00:35<01:03,  1.06RDM/s]
 35%|█████████████████████████████████████████████████████████████████████████████████▏                                                                                                                                                    | 36/102 [00:36<01:03,  1.04RDM/s]
 36%|███████████████████████████████████████████████████████████████████████████████████▍                                                                                                                                                  | 37/102 [00:37<01:03,  1.02RDM/s]
 37%|█████████████████████████████████████████████████████████████████████████████████████▋                                                                                                                                                | 38/102 [00:38<01:02,  1.03RDM/s]
 38%|███████████████████████████████████████████████████████████████████████████████████████▉                                                                                                                                              | 39/102 [00:39<01:00,  1.04RDM/s]
 39%|██████████████████████████████████████████████████████████████████████████████████████████▏                                                                                                                                           | 40/102 [00:40<00:58,  1.05RDM/s]
 40%|████████████████████████████████████████████████████████████████████████████████████████████▍                                                                                                                                         | 41/102 [00:41<00:59,  1.02RDM/s]
 41%|██████████████████████████████████████████████████████████████████████████████████████████████▋                                                                                                                                       | 42/102 [00:42<00:58,  1.03RDM/s]
 42%|████████████████████████████████████████████████████████████████████████████████████████████████▉                                                                                                                                     | 43/102 [00:43<00:56,  1.05RDM/s]
 43%|███████████████████████████████████████████████████████████████████████████████████████████████████▏                                                                                                                                  | 44/102 [00:44<00:56,  1.03RDM/s]
 44%|█████████████████████████████████████████████████████████████████████████████████████████████████████▍                                                                                                                                | 45/102 [00:45<00:54,  1.05RDM/s]
 45%|███████████████████████████████████████████████████████████████████████████████████████████████████████▋                                                                                                                              | 46/102 [00:46<00:53,  1.04RDM/s]
 46%|█████████████████████████████████████████████████████████████████████████████████████████████████████████▉                                                                                                                            | 47/102 [00:47<00:52,  1.05RDM/s]
 47%|████████████████████████████████████████████████████████████████████████████████████████████████████████████▏                                                                                                                         | 48/102 [00:48<00:53,  1.00RDM/s]
 48%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████▍                                                                                                                       | 49/102 [00:49<00:58,  1.10s/RDM]
 49%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋                                                                                                                     | 50/102 [00:50<00:56,  1.08s/RDM]
 50%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                                                                                   | 51/102 [00:51<00:53,  1.06s/RDM]
 51%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                                                                                | 52/102 [00:52<00:51,  1.03s/RDM]
 52%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌                                                                                                              | 53/102 [00:53<00:49,  1.01s/RDM]
 53%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊                                                                                                            | 54/102 [00:54<00:52,  1.10s/RDM]
 54%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                                                                          | 55/102 [00:55<00:48,  1.04s/RDM]
 55%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                                                                       | 56/102 [00:56<00:46,  1.00s/RDM]
 56%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌                                                                                                     | 57/102 [00:57<00:48,  1.07s/RDM]
 57%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊                                                                                                   | 58/102 [00:58<00:47,  1.08s/RDM]
 58%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                                                                 | 59/102 [01:00<00:46,  1.07s/RDM]
 59%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                                                              | 60/102 [01:00<00:43,  1.03s/RDM]
 60%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌                                                                                            | 61/102 [01:01<00:41,  1.02s/RDM]
 61%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊                                                                                          | 62/102 [01:03<00:42,  1.05s/RDM]
 62%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                                                        | 63/102 [01:04<00:40,  1.04s/RDM]
 63%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                                                     | 64/102 [01:05<00:38,  1.00s/RDM]
 64%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌                                                                                   | 65/102 [01:05<00:36,  1.02RDM/s]
 65%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊                                                                                 | 66/102 [01:07<00:36,  1.01s/RDM]
 66%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                                               | 67/102 [01:08<00:35,  1.02s/RDM]
 67%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                                            | 68/102 [01:09<00:35,  1.04s/RDM]
 68%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌                                                                          | 69/102 [01:10<00:33,  1.03s/RDM]
 69%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊                                                                        | 70/102 [01:11<00:33,  1.04s/RDM]
 70%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                                      | 71/102 [01:12<00:31,  1.03s/RDM]
 71%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                                   | 72/102 [01:13<00:31,  1.06s/RDM]
 72%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌                                                                 | 73/102 [01:14<00:29,  1.03s/RDM]
 73%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊                                                               | 74/102 [01:15<00:27,  1.01RDM/s]
 74%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                                             | 75/102 [01:16<00:27,  1.02s/RDM]
 75%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                                          | 76/102 [01:17<00:25,  1.01RDM/s]
 75%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋                                                        | 77/102 [01:18<00:24,  1.03RDM/s]
 76%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▉                                                      | 78/102 [01:19<00:24,  1.00s/RDM]
 77%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏                                                   | 79/102 [01:20<00:23,  1.01s/RDM]
 78%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍                                                 | 80/102 [01:21<00:22,  1.03s/RDM]
 79%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋                                               | 81/102 [01:22<00:21,  1.02s/RDM]
 80%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▉                                             | 82/102 [01:23<00:20,  1.01s/RDM]
 81%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏                                          | 83/102 [01:24<00:18,  1.02RDM/s]
 82%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍                                        | 84/102 [01:25<00:17,  1.00RDM/s]
 83%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋                                      | 85/102 [01:26<00:16,  1.03RDM/s]
 84%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▉                                    | 86/102 [01:27<00:15,  1.04RDM/s]
 85%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏                                 | 87/102 [01:28<00:14,  1.01RDM/s]
 86%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍                               | 88/102 [01:29<00:14,  1.00s/RDM]
 87%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋                             | 89/102 [01:30<00:12,  1.00RDM/s]
 88%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▉                           | 90/102 [01:31<00:12,  1.05s/RDM]
 89%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏                        | 91/102 [01:32<00:11,  1.06s/RDM]
 90%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍                      | 92/102 [01:33<00:10,  1.09s/RDM]
 91%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋                    | 93/102 [01:34<00:09,  1.06s/RDM]
 92%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▉                  | 94/102 [01:35<00:08,  1.03s/RDM]
 93%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏               | 95/102 [01:36<00:07,  1.00s/RDM]
 94%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍             | 96/102 [01:37<00:06,  1.01s/RDM]
 95%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋           | 97/102 [01:38<00:05,  1.05s/RDM]
 96%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▉         | 98/102 [01:39<00:04,  1.01s/RDM]
 97%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏      | 99/102 [01:40<00:02,  1.01RDM/s]
 98%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌    | 100/102 [01:41<00:01,  1.01RDM/s]
 99%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊  | 101/102 [01:42<00:01,  1.02s/RDM]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 102/102 [01:43<00:00,  1.04s/RDM]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 102/102 [01:43<00:00,  1.02s/RDM]

<matplotlib.legend.Legend object at 0x000002B7A1E800D0>

Total running time of the script: (1 minutes 47.431 seconds)

Gallery generated by Sphinx-Gallery