-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmne_setup_forward_model.py
150 lines (136 loc) · 4.21 KB
/
mne_setup_forward_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
"""Create a BEM model for a subject.
Examples
--------
.. code-block:: console
$ mne setup_forward_model -s 'sample'
"""
import os
import sys
import mne
from mne.utils import get_subjects_dir, warn
def run():
"""Run command."""
from mne.commands.utils import _add_verbose_flag, get_optparser
parser = get_optparser(__file__)
parser.add_option(
"-s", "--subject", dest="subject", help="Subject name (required)", default=None
)
parser.add_option(
"--model",
dest="model",
help="Output file name. Use a name <dir>/<name>-bem.fif",
default=None,
type="string",
)
parser.add_option(
"--ico",
dest="ico",
help="The surface ico downsampling to use, e.g. "
" 5=20484, 4=5120, 3=1280. If None, no subsampling"
" is applied.",
default=None,
type="int",
)
parser.add_option(
"--brainc",
dest="brainc",
help="Defines the brain compartment conductivity. "
"The default value is 0.3 S/m.",
default=0.3,
type="float",
)
parser.add_option(
"--skullc",
dest="skullc",
help="Defines the skull compartment conductivity. "
"The default value is 0.006 S/m.",
default=None,
type="float",
)
parser.add_option(
"--scalpc",
dest="scalpc",
help="Defines the scalp compartment conductivity. "
"The default value is 0.3 S/m.",
default=None,
type="float",
)
parser.add_option(
"--homog",
dest="homog",
help="Use a single compartment model (brain only) "
"instead a three layer one (scalp, skull, and "
" brain). If this flag is specified, the options "
"--skullc and --scalpc are irrelevant.",
default=None,
action="store_true",
)
parser.add_option(
"-d",
"--subjects-dir",
dest="subjects_dir",
help="Subjects directory",
default=None,
)
_add_verbose_flag(parser)
options, args = parser.parse_args()
if options.subject is None:
parser.print_help()
sys.exit(1)
subject = options.subject
fname = options.model
subjects_dir = options.subjects_dir
ico = options.ico
brainc = options.brainc
skullc = options.skullc
scalpc = options.scalpc
homog = True if options.homog is not None else False
verbose = True if options.verbose is not None else False
# Parse conductivity option
if homog is True:
if skullc is not None:
warn(
"Trying to set the skull conductivity for a single layer "
"model. To use a 3 layer model, do not set the --homog flag."
)
if scalpc is not None:
warn(
"Trying to set the scalp conductivity for a single layer "
"model. To use a 3 layer model, do not set the --homog flag."
)
# Single layer
conductivity = [brainc]
else:
if skullc is None:
skullc = 0.006
if scalpc is None:
scalpc = 0.3
conductivity = [brainc, skullc, scalpc]
# Create source space
bem_model = mne.make_bem_model(
subject,
ico=ico,
conductivity=conductivity,
subjects_dir=subjects_dir,
verbose=verbose,
)
# Generate filename
if fname is None:
n_faces = list(str(len(surface["tris"])) for surface in bem_model)
fname = subject + "-" + "-".join(n_faces) + "-bem.fif"
else:
if not (fname.endswith("-bem.fif") or fname.endswith("_bem.fif")):
fname = fname + "-bem.fif"
# Save to subject's directory
subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
fname = subjects_dir / subject / "bem" / fname
# Save source space to file
mne.write_bem_surfaces(fname, bem_model)
# Compute the solution
sol_fname = os.path.splitext(str(fname))[0] + "-sol.fif"
bem_sol = mne.make_bem_solution(bem_model, verbose=verbose)
mne.write_bem_solution(sol_fname, bem_sol)
mne.utils.run_command_if_main()