|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | +""" |
| 4 | +Source-level RSA using ROI's |
| 5 | +============================ |
| 6 | +
|
| 7 | +In this example, we use anatomical labels as Regions Of Interest (ROIs). Rather |
| 8 | +than using a searchlight, we compute DSMs for each ROI and then compute RSA |
| 9 | +with a single model DSM. |
| 10 | +
|
| 11 | +The dataset will be the MNE-sample dataset: a collection of 288 epochs in which |
| 12 | +the participant was presented with an auditory beep or visual stimulus to |
| 13 | +either the left or right ear or visual field. |
| 14 | +""" |
| 15 | +# sphinx_gallery_thumbnail_number=2 |
| 16 | +# Import required packages |
| 17 | +import mne |
| 18 | +import mne_rsa |
| 19 | + |
| 20 | +mne.set_log_level(True) # Be less verbose |
| 21 | +mne.viz.set_3d_backend('pyvista') |
| 22 | + |
| 23 | +############################################################################### |
| 24 | +# We'll be using the data from the MNE-sample set. To speed up computations in |
| 25 | +# this example, we're going to use one of the sparse source spaces from the |
| 26 | +# testing set. |
| 27 | +sample_root = mne.datasets.sample.data_path(verbose=True) |
| 28 | +testing_root = mne.datasets.testing.data_path(verbose=True) |
| 29 | +sample_path = sample_root / 'MEG' / 'sample' |
| 30 | +testing_path = testing_root / 'MEG' / 'sample' |
| 31 | +subjects_dir = sample_root / 'subjects' |
| 32 | + |
| 33 | +############################################################################### |
| 34 | +# Creating epochs from the continuous (raw) data. We downsample to 100 Hz to |
| 35 | +# speed up the RSA computations later on. |
| 36 | +raw = mne.io.read_raw_fif(sample_path / 'sample_audvis_filt-0-40_raw.fif') |
| 37 | +events = mne.read_events(sample_path / 'sample_audvis_filt-0-40_raw-eve.fif') |
| 38 | +event_id = {'audio/left': 1, |
| 39 | + 'audio/right': 2, |
| 40 | + 'visual/left': 3, |
| 41 | + 'visual/right': 4} |
| 42 | +epochs = mne.Epochs(raw, events, event_id, preload=True) |
| 43 | +epochs.resample(100) |
| 44 | + |
| 45 | +############################################################################### |
| 46 | +# It's important that the model DSM and the epochs are in the same order, so |
| 47 | +# that each row in the model DSM will correspond to an epoch. The model DSM |
| 48 | +# will be easier to interpret visually if the data is ordered such that all |
| 49 | +# epochs belonging to the same experimental condition are right next to |
| 50 | +# each-other, so patterns jump out. This can be achieved by first splitting the |
| 51 | +# epochs by experimental condition and then concatenating them together again. |
| 52 | +epoch_splits = [epochs[cl] for cl in ['audio/left', 'audio/right', |
| 53 | + 'visual/left', 'visual/right']] |
| 54 | +epochs = mne.concatenate_epochs(epoch_splits) |
| 55 | + |
| 56 | +############################################################################### |
| 57 | +# Now that the epochs are in the proper order, we can create a DSM based on the |
| 58 | +# experimental conditions. This type of DSM is referred to as a "sensitivity |
| 59 | +# DSM". Let's create a sensitivity DSM that will pick up the left auditory |
| 60 | +# response when RSA-ed against the MEG data. Since we want to capture areas |
| 61 | +# where left beeps generate a large signal, we specify that left beeps should |
| 62 | +# be similar to other left beeps. Since we do not want areas where visual |
| 63 | +# stimuli generate a large signal, we specify that beeps must be different from |
| 64 | +# visual stimuli. Furthermore, since in areas where visual stimuli generate |
| 65 | +# only a small signal, random noise will dominate, we also specify that visual |
| 66 | +# stimuli are different from other visual stimuli. Finally left and right |
| 67 | +# auditory beeps will be somewhat similar. |
| 68 | + |
| 69 | + |
| 70 | +def sensitivity_metric(event_id_1, event_id_2): |
| 71 | + """Determine similarity between two epochs, given their event ids.""" |
| 72 | + if event_id_1 == 1 and event_id_2 == 1: |
| 73 | + return 0 # Completely similar |
| 74 | + if event_id_1 == 2 and event_id_2 == 2: |
| 75 | + return 0.5 # Somewhat similar |
| 76 | + elif event_id_1 == 1 and event_id_2 == 2: |
| 77 | + return 0.5 # Somewhat similar |
| 78 | + elif event_id_1 == 2 and event_id_1 == 1: |
| 79 | + return 0.5 # Somewhat similar |
| 80 | + else: |
| 81 | + return 1 # Not similar at all |
| 82 | + |
| 83 | + |
| 84 | +model_dsm = mne_rsa.compute_dsm(epochs.events[:, 2], metric=sensitivity_metric) |
| 85 | +mne_rsa.plot_dsms(model_dsm, title='Model DSM') |
| 86 | + |
| 87 | +############################################################################### |
| 88 | +# This example is going to be on source-level, so let's load the inverse |
| 89 | +# operator and apply it to obtain a cortical surface source estimate for each |
| 90 | +# epoch. To speed up the computation, we going to load an inverse operator from |
| 91 | +# the testing dataset that was created using a sparse source space with not too |
| 92 | +# many vertices. |
| 93 | +inv = mne.minimum_norm.read_inverse_operator( |
| 94 | + f'{testing_path}/sample_audvis_trunc-meg-eeg-oct-4-meg-inv.fif') |
| 95 | +epochs_stc = mne.minimum_norm.apply_inverse_epochs(epochs, inv, lambda2=0.1111) |
| 96 | + |
| 97 | +############################################################################### |
| 98 | +# ROIs need to be defined as ``mne.Label`` objects. Here, we load the APARC |
| 99 | +# parcellation generated by FreeSurfer and treat each parcel as an ROI. |
| 100 | +rois = mne.read_labels_from_annot(parc='aparc', subject='sample', |
| 101 | + subjects_dir=subjects_dir) |
| 102 | + |
| 103 | +############################################################################### |
| 104 | +# Performing the RSA. To save time, we don't use a searchlight over time, just |
| 105 | +# over the ROIs. The results are returned not only as a NumPy `ndarray`, but |
| 106 | +# also as an `mne.SourceEstimate` object, where each vertex beloning to the |
| 107 | +# same ROI has the same value. |
| 108 | +rsa_vals, stc = mne_rsa.rsa_stcs_rois(epochs_stc, model_dsm, inv['src'], rois, |
| 109 | + temporal_radius=None, n_jobs=1, |
| 110 | + verbose=False) |
| 111 | + |
| 112 | +############################################################################### |
| 113 | +# To plot the RSA values on a brain, we can use one of MNE-RSA's own |
| 114 | +# visualization functions. |
| 115 | +brain = mne_rsa.plot_roi_map(rsa_vals, rois, subject='sample', |
| 116 | + subjects_dir=subjects_dir) |
| 117 | +brain.show_view('lateral', distance=600) |
0 commit comments