# 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) ############################################################################### # Compute DSMs based on word length and visual complexity. metadata = epochs.metadata dsm1 = mne_rsa.compute_dsm(metadata.NumberOfLetters, metric='euclidean') dsm2 = mne_rsa.compute_dsm(metadata.VisualComplexity, metric='euclidean') # Plot the DSMs mne_rsa.plot_dsms([dsm1, dsm2], names=['Word length', 'Vis. complexity']) ############################################################################### # Perform RSA between the two DSMs using Spearman correlation rsa_result = mne_rsa.rsa(dsm1, dsm2, metric='spearman') print('RSA score:', rsa_result) ############################################################################### # We can compute RSA between multiple DSMs by passing lists to the # :func:`mne_rsa.rsa` function.
def generate_eeg_dsms(): """Generate DSMs for each time sample.""" for i in range(n_times): yield mne_rsa.compute_dsm(eeg_data[:, :, i], metric='correlation')
data_path = mne.datasets.kiloword.data_path(verbose=True) epochs = mne.read_epochs(data_path + '/kword_metadata-epo.fif') epochs = epochs.resample(100) ############################################################################### # The ``epochs`` object contains a ``.metadata`` field that contains # information about the 960 words that were used in the experiment. Let's have # a look at the metadata for the 10 random words: epochs.metadata.sample(10) ############################################################################### # Let's pick something obvious for this example and build a dissimilarity # matrix (DSM) based on the number of letters in each word. dsm_vis = mne_rsa.compute_dsm(epochs.metadata[['NumberOfLetters']], metric='euclidean') mne_rsa.plot_dsms(dsm_vis) ############################################################################### # The above DSM will serve as our "model" DSM. In this example RSA analysis, we # are going to compare the model DSM against DSMs created from the EEG data. # The EEG DSMs will be created using a "searchlight" pattern. We are using # squared Euclidean distance for our DSM metric, since we only have a few data # points in each searlight patch. Feel free to play around with other metrics. rsa_result = mne_rsa.rsa_epochs( epochs, # The EEG data dsm_vis, # The model DSM epochs_dsm_metric='sqeuclidean', # Metric to compute the EEG DSMs rsa_metric='kendall-tau-a', # Metric to compare model and EEG DSMs spatial_radius=45, # Spatial radius of the searchlight patch
# stimuli are different from other visual stimuli. Finally left and right # auditory beeps will be somewhat similar. def sensitivity_metric(event_id_1, event_id_2): """Determine similarity between two epochs, given their event ids.""" if event_id_1 == 1 and event_id_2 == 1: return 0 # Completely similar if event_id_1 == 2 and event_id_2 == 2: return 0.5 # Somewhat similar elif event_id_1 == 1 and event_id_2 == 2: return 0.5 # Somewhat similar elif event_id_1 == 2 and event_id_1 == 1: return 0.5 # Somewhat similar else: return 1 # Not similar at all model_dsm = mne_rsa.compute_dsm(epochs.events[:, 2], metric=sensitivity_metric) mne_rsa.plot_dsms(model_dsm, title='Model DSM') ############################################################################### # This example is going to be on source-level, so let's load the inverse # operator and apply it to obtain a volumetric source estimate for each # epoch. inv = mne.minimum_norm.read_inverse_operator( op.join(sample_path, 'sample_audvis-meg-vol-7-meg-inv.fif')) epochs_stc = mne.minimum_norm.apply_inverse_epochs(epochs, inv, lambda2=0.1111) ############################################################################### # Performing the RSA. This will take some time. Consider increasing ``n_jobs`` # to parallelize the computation across multiple CPUs. rsa_vals = mne_rsa.rsa_stcs( epochs_stc, # The source localized epochs
############################################################################### # Drop "rest" class and sort images by class. We must ensure that all times, # the metadata and the bold images are in sync. Hence, we first perform the # operations on the `meta` pandas DataFrame. Then, we can use the DataFrame's # index to repeat the operations on the BOLD data. meta = meta[meta['labels'] != 'rest'].sort_values('labels') bold = nib.Nifti1Image(bold.get_fdata()[..., meta.index], bold.affine, bold.header) ############################################################################### # We're going to hunt for areas in the brain where the signal differentiates # nicely between the various object categories. We encode this objective in our # "model" DSM: a DSM where stimuli belonging to the same object category have a # dissimilarity of 0 and stimuli belonging to different categories have a # dissimilarity of 1. model_dsm = mne_rsa.compute_dsm(meta['labels'], metric=lambda a, b: 0 if a == b else 1) mne_rsa.plot_dsms(model_dsm, 'Model DSM') ############################################################################### # Performing the RSA. This will take some time. Consider increasing ``n_jobs`` # to parallelize the computation across multiple CPUs. rsa_vals = mne_rsa.rsa_nifti( bold, # The BOLD data model_dsm, # The model DSM we constructed above image_dsm_metric='correlation', # Metric to compute the BOLD DSMs rsa_metric='kendall-tau-a', # Metric to compare model and BOLD DSMs spatial_radius=0.01, # Spatial radius of the searchlight patch roi_mask=mask, # Restrict analysis to the VT ROI n_jobs=1, # Only use one CPU core. verbose=False) # Set to True to display a progress bar
for this is to construct a "model" DSM to RSA against the brain data. In this example, we will create a DSM based on the length of the words shown during an EEG experiment. """ # Import required packages 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', preload=False) # Show the metadata of 10 random epochs print(epochs.metadata.sample(10)) ############################################################################### # Now we are ready to create the "model" DSM, which will encode the difference # in length between the words shown during the experiment. dsm = mne_rsa.compute_dsm(epochs.metadata.NumberOfLetters, metric='euclidean') # Plot the DSM fig = mne_rsa.plot_dsms(dsm, title='Word length DSM') fig.set_size_inches(3, 3) # Make figure a little bigger to show axis properly
def test_set_metric(self): """Test setting distance metric for computing DSMs.""" data = np.array([[1, 2, 3, 4], [2, 4, 6, 8]]) dsm = compute_dsm(data, metric='euclidean') assert dsm.shape == (1, ) assert_allclose(dsm, 5.477226)
def test_invalid_input(self): """Test giving invalid input to compute_dsm.""" data = np.array([[1], [1]]) with pytest.raises(ValueError, match='single feature'): compute_dsm(data, metric='correlation')
def test_basic(self): """Test basic invocation of compute_dsm.""" data = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) dsm = compute_dsm(data) assert dsm.shape == (1, ) assert_allclose(dsm, 0, atol=1E-15)