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. # Create DSMs for each stimulus property columns = metadata.columns[1:] # Skip the first column: WORD dsms = [ mne_rsa.compute_dsm(metadata[col], metric='euclidean') for col in columns
# 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 model_dsm, # The model DSM we constructed above
# 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 ###############################################################################
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 temporal_radius=0.05, # Temporal radius of the searchlight path
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