|
5 | 5 | From raw data to dSPM on SPM Faces dataset
|
6 | 6 | ==========================================
|
7 | 7 |
|
8 |
| -Runs a full pipeline using MNE-Python: |
9 |
| -
|
10 |
| - - artifact removal |
11 |
| - - averaging Epochs |
12 |
| - - forward model computation |
13 |
| - - source reconstruction using dSPM on the contrast : "faces - scrambled" |
14 |
| -
|
15 |
| -.. note:: This example does quite a bit of processing, so even on a |
16 |
| - fast machine it can take several minutes to complete. |
| 8 | +Runs a full pipeline using MNE-Python. This example does quite a bit of processing, so |
| 9 | +even on a fast machine it can take several minutes to complete. |
17 | 10 | """
|
18 | 11 | # Authors: Alexandre Gramfort <[email protected]>
|
19 | 12 | # Denis Engemann <[email protected]>
|
20 | 13 | #
|
21 | 14 | # License: BSD-3-Clause
|
22 | 15 | # Copyright the MNE-Python contributors.
|
23 | 16 |
|
24 |
| -# %% |
25 |
| - |
26 |
| -# sphinx_gallery_thumbnail_number = 10 |
27 |
| - |
28 |
| -import matplotlib.pyplot as plt |
29 |
| - |
30 | 17 | import mne
|
31 | 18 | from mne import combine_evoked, io
|
32 | 19 | from mne.datasets import spm_face
|
|
40 | 27 | spm_path = data_path / "MEG" / "spm"
|
41 | 28 |
|
42 | 29 | # %%
|
43 |
| -# Load and filter data, set up epochs |
| 30 | +# Load data, filter it, and fit ICA. |
44 | 31 |
|
45 | 32 | raw_fname = spm_path / "SPM_CTF_MEG_example_faces1_3D.ds"
|
46 |
| - |
47 | 33 | raw = io.read_raw_ctf(raw_fname, preload=True) # Take first run
|
48 | 34 | # Here to save memory and time we'll downsample heavily -- this is not
|
49 | 35 | # advised for real data as it can effectively jitter events!
|
50 |
| -raw.resample(120.0, npad="auto") |
51 |
| - |
52 |
| -picks = mne.pick_types(raw.info, meg=True, exclude="bads") |
53 |
| -raw.filter(1, 30, method="fir", fir_design="firwin") |
| 36 | +raw.resample(100) |
| 37 | +raw.filter(1.0, None) # high-pass |
| 38 | +reject = dict(mag=5e-12) |
| 39 | +ica = ICA(n_components=0.95, max_iter="auto", random_state=0) |
| 40 | +ica.fit(raw, reject=reject) |
| 41 | +# compute correlation scores, get bad indices sorted by score |
| 42 | +eog_epochs = create_eog_epochs(raw, ch_name="MRT31-2908", reject=reject) |
| 43 | +eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name="MRT31-2908") |
| 44 | +ica.plot_scores(eog_scores, eog_inds) # see scores the selection is based on |
| 45 | +ica.plot_components(eog_inds) # view topographic sensitivity of components |
| 46 | +ica.exclude += eog_inds[:1] # we saw the 2nd ECG component looked too dipolar |
| 47 | +ica.plot_overlay(eog_epochs.average()) # inspect artifact removal |
54 | 48 |
|
| 49 | +# %% |
| 50 | +# Epoch data and apply ICA. |
55 | 51 | events = mne.find_events(raw, stim_channel="UPPT001")
|
56 |
| - |
57 |
| -# plot the events to get an idea of the paradigm |
58 |
| -mne.viz.plot_events(events, raw.info["sfreq"]) |
59 |
| - |
60 | 52 | event_ids = {"faces": 1, "scrambled": 2}
|
61 |
| - |
62 | 53 | tmin, tmax = -0.2, 0.6
|
63 |
| -baseline = None # no baseline as high-pass is applied |
64 |
| -reject = dict(mag=5e-12) |
65 |
| - |
66 | 54 | epochs = mne.Epochs(
|
67 | 55 | raw,
|
68 | 56 | events,
|
69 | 57 | event_ids,
|
70 | 58 | tmin,
|
71 | 59 | tmax,
|
72 |
| - picks=picks, |
73 |
| - baseline=baseline, |
| 60 | + picks="meg", |
| 61 | + baseline=None, |
74 | 62 | preload=True,
|
75 | 63 | reject=reject,
|
76 | 64 | )
|
77 |
| - |
78 |
| -# Fit ICA, find and remove major artifacts |
79 |
| -ica = ICA(n_components=0.95, max_iter="auto", random_state=0) |
80 |
| -ica.fit(raw, decim=1, reject=reject) |
81 |
| - |
82 |
| -# compute correlation scores, get bad indices sorted by score |
83 |
| -eog_epochs = create_eog_epochs(raw, ch_name="MRT31-2908", reject=reject) |
84 |
| -eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name="MRT31-2908") |
85 |
| -ica.plot_scores(eog_scores, eog_inds) # see scores the selection is based on |
86 |
| -ica.plot_components(eog_inds) # view topographic sensitivity of components |
87 |
| -ica.exclude += eog_inds[:1] # we saw the 2nd ECG component looked too dipolar |
88 |
| -ica.plot_overlay(eog_epochs.average()) # inspect artifact removal |
| 65 | +del raw |
89 | 66 | ica.apply(epochs) # clean data, default in place
|
90 |
| - |
91 | 67 | evoked = [epochs[k].average() for k in event_ids]
|
92 |
| - |
93 | 68 | contrast = combine_evoked(evoked, weights=[-1, 1]) # Faces - scrambled
|
94 |
| - |
95 | 69 | evoked.append(contrast)
|
96 |
| - |
97 | 70 | for e in evoked:
|
98 | 71 | e.plot(ylim=dict(mag=[-400, 400]))
|
99 | 72 |
|
100 |
| -plt.show() |
101 |
| - |
102 |
| -# estimate noise covarariance |
103 |
| -noise_cov = mne.compute_covariance(epochs, tmax=0, method="shrunk", rank=None) |
104 |
| - |
105 | 73 | # %%
|
106 |
| -# Visualize fields on MEG helmet |
107 |
| - |
108 |
| -# The transformation here was aligned using the dig-montage. It's included in |
109 |
| -# the spm_faces dataset and is named SPM_dig_montage.fif. |
110 |
| -trans_fname = spm_path / "SPM_CTF_MEG_example_faces1_3D_raw-trans.fif" |
111 |
| - |
112 |
| -maps = mne.make_field_map( |
113 |
| - evoked[0], trans_fname, subject="spm", subjects_dir=subjects_dir, n_jobs=None |
114 |
| -) |
115 |
| - |
116 |
| -evoked[0].plot_field(maps, time=0.170, time_viewer=False) |
117 |
| - |
118 |
| -# %% |
119 |
| -# Look at the whitened evoked daat |
| 74 | +# Estimate noise covariance and look at the whitened evoked data |
120 | 75 |
|
| 76 | +noise_cov = mne.compute_covariance(epochs, tmax=0, method="shrunk", rank=None) |
121 | 77 | evoked[0].plot_white(noise_cov)
|
122 | 78 |
|
123 | 79 | # %%
|
124 | 80 | # Compute forward model
|
125 | 81 |
|
| 82 | +trans_fname = spm_path / "SPM_CTF_MEG_example_faces1_3D_raw-trans.fif" |
126 | 83 | src = subjects_dir / "spm" / "bem" / "spm-oct-6-src.fif"
|
127 | 84 | bem = subjects_dir / "spm" / "bem" / "spm-5120-5120-5120-bem-sol.fif"
|
128 | 85 | forward = mne.make_forward_solution(contrast.info, trans_fname, src, bem)
|
129 | 86 |
|
130 | 87 | # %%
|
131 |
| -# Compute inverse solution |
| 88 | +# Compute inverse solution and plot |
| 89 | + |
| 90 | +# sphinx_gallery_thumbnail_number = 8 |
132 | 91 |
|
133 | 92 | snr = 3.0
|
134 | 93 | lambda2 = 1.0 / snr**2
|
135 |
| -method = "dSPM" |
136 |
| - |
137 |
| -inverse_operator = make_inverse_operator( |
138 |
| - contrast.info, forward, noise_cov, loose=0.2, depth=0.8 |
139 |
| -) |
140 |
| - |
141 |
| -# Compute inverse solution on contrast |
142 |
| -stc = apply_inverse(contrast, inverse_operator, lambda2, method, pick_ori=None) |
143 |
| -# stc.save('spm_%s_dSPM_inverse' % contrast.comment) |
144 |
| - |
145 |
| -# Plot contrast in 3D with mne.viz.Brain if available |
| 94 | +inverse_operator = make_inverse_operator(contrast.info, forward, noise_cov) |
| 95 | +stc = apply_inverse(contrast, inverse_operator, lambda2, method="dSPM", pick_ori=None) |
146 | 96 | brain = stc.plot(
|
147 | 97 | hemi="both",
|
148 | 98 | subjects_dir=subjects_dir,
|
149 | 99 | initial_time=0.170,
|
150 | 100 | views=["ven"],
|
151 | 101 | clim={"kind": "value", "lims": [3.0, 6.0, 9.0]},
|
152 | 102 | )
|
153 |
| -# brain.save_image('dSPM_map.png') |
0 commit comments