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.

Authors:
Marijn van Vliet <marijn.vanvliet@aalto.fi>
# sphinx_gallery_thumbnail_number=2

import mne
import mne_rsa

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

MNE-Python contains a built-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)
Reading /home/runner/mne_data/MNE-kiloword-data/kword_metadata-epo.fif ...
Isotrak not found
    Found the data of interest:
        t =    -100.00 ...     920.00 ms
        0 CTF compensation matrices available
Adding metadata with 8 columns
960 matching events found
No baseline correction applied
0 projection items activated
WORD Concreteness WordFrequency OrthographicDistance NumberOfLetters BigramFrequency ConsonantVowelProportion VisualComplexity
576 police 5.250000 3.567614 1.85 6.0 567.833333 0.500000 62.796116
862 maiden 4.850000 1.763428 1.90 6.0 650.833333 0.500000 73.318786
878 economy 4.300000 3.148911 2.90 7.0 161.285714 0.428571 72.410834
692 doctrine 2.950000 2.459392 2.85 8.0 677.500000 0.625000 61.243776
676 religion 3.250000 2.963788 2.70 8.0 695.250000 0.500000 58.875421
154 husband 5.050000 3.381296 2.70 7.0 331.714286 0.714286 73.318974
844 expert 3.500000 2.521138 1.95 6.0 719.333333 0.666667 66.074619
383 umpire 6.100000 1.414973 2.40 6.0 268.833333 0.500000 67.719435
338 figure 4.450000 3.279895 2.30 6.0 379.833333 0.500000 61.844238
715 ordnance 4.105263 1.414973 3.05 8.0 248.125000 0.625000 67.573595


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.026439883289118636

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  ...  VisualComplexity
Concreteness               0.031064  ...          0.004263
WordFrequency              0.058385  ...         -0.009620

[2 rows x 5 columns]

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 mne_rsa.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
  0%|          | 0/102 [00:00<?, ?RDM/s]
  1%|          | 1/102 [00:00<00:18,  5.44RDM/s]
  4%|▍         | 4/102 [00:00<00:06, 15.10RDM/s]
  7%|▋         | 7/102 [00:00<00:04, 19.46RDM/s]
 10%|▉         | 10/102 [00:00<00:04, 21.77RDM/s]
 13%|█▎        | 13/102 [00:00<00:03, 23.18RDM/s]
 16%|█▌        | 16/102 [00:00<00:03, 24.05RDM/s]
 19%|█▊        | 19/102 [00:00<00:03, 24.65RDM/s]
 22%|██▏       | 22/102 [00:00<00:03, 25.04RDM/s]
 25%|██▍       | 25/102 [00:01<00:03, 25.36RDM/s]
 27%|██▋       | 28/102 [00:01<00:02, 25.57RDM/s]
 30%|███       | 31/102 [00:01<00:02, 25.70RDM/s]
 33%|███▎      | 34/102 [00:01<00:02, 25.79RDM/s]
 36%|███▋      | 37/102 [00:01<00:02, 25.81RDM/s]
 39%|███▉      | 40/102 [00:01<00:02, 25.75RDM/s]
 42%|████▏     | 43/102 [00:01<00:02, 25.38RDM/s]
 45%|████▌     | 46/102 [00:01<00:02, 25.51RDM/s]
 48%|████▊     | 49/102 [00:02<00:02, 25.60RDM/s]
 51%|█████     | 52/102 [00:02<00:01, 25.70RDM/s]
 54%|█████▍    | 55/102 [00:02<00:01, 25.77RDM/s]
 57%|█████▋    | 58/102 [00:02<00:01, 25.70RDM/s]
 60%|█████▉    | 61/102 [00:02<00:01, 25.75RDM/s]
 63%|██████▎   | 64/102 [00:02<00:01, 25.81RDM/s]
 66%|██████▌   | 67/102 [00:02<00:01, 25.79RDM/s]
 69%|██████▊   | 70/102 [00:02<00:01, 25.81RDM/s]
 72%|███████▏  | 73/102 [00:02<00:01, 25.81RDM/s]
 75%|███████▍  | 76/102 [00:03<00:01, 25.80RDM/s]
 77%|███████▋  | 79/102 [00:03<00:00, 25.79RDM/s]
 80%|████████  | 82/102 [00:03<00:00, 25.89RDM/s]
 83%|████████▎ | 85/102 [00:03<00:00, 25.91RDM/s]
 86%|████████▋ | 88/102 [00:03<00:00, 25.94RDM/s]
 89%|████████▉ | 91/102 [00:03<00:00, 25.90RDM/s]
 92%|█████████▏| 94/102 [00:03<00:00, 25.88RDM/s]
 95%|█████████▌| 97/102 [00:03<00:00, 25.77RDM/s]
 98%|█████████▊| 100/102 [00:04<00:00, 25.70RDM/s]
100%|██████████| 102/102 [00:04<00:00, 24.89RDM/s]

<matplotlib.legend.Legend object at 0x7f120faa2570>

Total running time of the script: (0 minutes 7.233 seconds)

Gallery generated by Sphinx-Gallery