|
1 | 1 | """ |
2 | 2 | .. _ex-multi-dipole: |
3 | 3 |
|
4 | | -================================================================= |
5 | | -Computing source timecourses with an XFit-like multi-dipole model |
6 | | -================================================================= |
7 | | -
|
8 | | -MEGIN's XFit program offers a "guided ECD modeling" interface, where multiple |
9 | | -dipoles can be fitted interactively. By manually selecting subsets of sensors |
10 | | -and time ranges, dipoles can be fitted to specific signal components. Then, |
11 | | -source timecourses can be computed using a multi-dipole model. The advantage of |
12 | | -using a multi-dipole model over fitting each dipole in isolation, is that when |
13 | | -multiple dipoles contribute to the same signal component, the model can make |
14 | | -sure that activity assigned to one dipole is not also assigned to another. This |
15 | | -example shows how to build a multi-dipole model for estimating source |
16 | | -timecourses for evokeds or single epochs. |
17 | | -
|
18 | | -The XFit program is the recommended approach for guided ECD modeling, because |
19 | | -it offers a convenient graphical user interface for it. These dipoles can then |
20 | | -be imported into MNE-Python by using the :func:`mne.read_dipole` function for |
21 | | -building and applying the multi-dipole model. In addition, this example will |
22 | | -also demonstrate how to perform guided ECD modeling using only MNE-Python |
23 | | -functionality, which is less convenient than using XFit, but has the benefit of |
24 | | -being reproducible. |
| 4 | +====================== |
| 5 | +Guided dipole modeling |
| 6 | +====================== |
| 7 | +
|
| 8 | +Inspired by MEGIN's XFit program, MNE-Python offers a guided dipole modeling interface. |
| 9 | +Through this interface, you can fit dipoles at specific timings using selected subsets |
| 10 | +of sensors to gradually build up a multi dipole source estimate. This is the manual |
| 11 | +alternative to using automated algorithms such as TRAP-MUSIC and MxNE. |
25 | 12 | """ |
26 | 13 | # Author: Marijn van Vliet <w.m.vanvliet@gmail.com> |
27 | 14 | # |
28 | 15 | # License: BSD-3-Clause |
29 | 16 | # Copyright the MNE-Python contributors. |
30 | 17 |
|
31 | | -############################################################################### |
32 | | -# Importing everything and setting up the data paths for the MNE-Sample |
33 | | -# dataset. |
| 18 | +######################################################################################## |
| 19 | +# Read the MEG data from the audvis experiment. Make epochs for the left auditory |
| 20 | +# condition. |
34 | 21 | import matplotlib.pyplot as plt |
35 | 22 | import numpy as np |
36 | 23 |
|
37 | 24 | import mne |
38 | | -from mne.channels import read_vectorview_selection |
39 | 25 | from mne.datasets import sample |
40 | | -from mne.minimum_norm import apply_inverse, apply_inverse_epochs, make_inverse_operator |
41 | 26 |
|
42 | 27 | data_path = sample.data_path() |
43 | 28 | meg_path = data_path / "MEG" / "sample" |
44 | 29 | raw_fname = meg_path / "sample_audvis_raw.fif" |
45 | | -cov_fname = meg_path / "sample_audvis-shrunk-cov.fif" |
46 | | -bem_dir = data_path / "subjects" / "sample" / "bem" |
47 | | -bem_fname = bem_dir / "sample-5120-5120-5120-bem-sol.fif" |
48 | | - |
49 | | -############################################################################### |
50 | | -# Read the MEG data from the audvis experiment. Make epochs and evokeds for the |
51 | | -# left and right auditory conditions. |
52 | 30 | raw = mne.io.read_raw_fif(raw_fname) |
53 | | -raw = raw.pick(picks=["meg", "eog", "stim"]) |
54 | 31 | info = raw.info |
55 | 32 |
|
56 | 33 | # Create epochs for auditory events |
57 | 34 | events = mne.find_events(raw) |
58 | | -event_id = dict(right=1, left=2) |
| 35 | +event_id = dict(left=2) |
59 | 36 | epochs = mne.Epochs( |
60 | 37 | raw, |
61 | 38 | events, |
|
67 | 44 | ) |
68 | 45 |
|
69 | 46 | # Create evokeds for left and right auditory stimulation |
70 | | -evoked_left = epochs["left"].average() |
71 | | -evoked_right = epochs["right"].average() |
| 47 | +evoked = epochs["left"].average() |
| 48 | + |
| 49 | +######################################################################################## |
| 50 | +# Guided dipole modeling, meaning fitting dipoles to a manually selected subset of |
| 51 | +# sensors as a manually chosen time, can now be performed using the |
| 52 | +# :class:`mne.gui.DipoleFitting` GUI. By specifying only ``evokeds``, a default noise |
| 53 | +# covariance matrix and 1-layer spherical head model will be used. |
| 54 | +mne.gui.dipolefit(evoked) |
72 | 55 |
|
73 | 56 | ############################################################################### |
74 | | -# Guided dipole modeling, meaning fitting dipoles to a manually selected subset |
75 | | -# of sensors as a manually chosen time, can now be performed in MEGINs XFit on |
76 | | -# the evokeds we computed above. However, it is possible to do it completely |
77 | | -# in MNE-Python. |
| 57 | +# The source modeling can be made more precise by using a noise covariance matrix and |
| 58 | +# MRI-based 3-layer BEM head model: |
| 59 | +cov_fname = meg_path / "sample_audvis-shrunk-cov.fif" |
| 60 | +subjects_dir = data_path / "subjects" |
| 61 | +bem_dir = subjects_dir / "sample" / "bem" |
| 62 | +bem_fname = bem_dir / "sample-5120-5120-5120-bem-sol.fif" |
| 63 | +trans_fname = meg_path / "sample_audvis_raw-trans.fif" |
78 | 64 |
|
79 | | -# Setup conductor model |
80 | 65 | cov = mne.read_cov(cov_fname) # bad channels were already excluded here |
81 | 66 | bem = mne.read_bem_solution(bem_fname) |
| 67 | +trans = mne.read_trans(trans_fname) |
| 68 | + |
| 69 | +mne.gui.dipolefit( |
| 70 | + evoked, |
| 71 | + cov=cov.as_diag(), |
| 72 | + bem=bem, |
| 73 | + trans=trans, |
| 74 | + subject="sample", |
| 75 | + subjects_dir=subjects_dir, |
| 76 | +) |
82 | 77 |
|
83 | 78 | # Fit two dipoles at t=80ms. The first dipole is fitted using only the sensors |
84 | 79 | # on the left side of the helmet. The second dipole is fitted using only the |
|
0 commit comments