File: mne_setup_source_space.py

package info (click to toggle)
python-mne 1.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 131,492 kB
  • sloc: python: 213,302; javascript: 12,910; sh: 447; makefile: 144
file content (183 lines) | stat: -rw-r--r-- 5,069 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

"""Set up bilateral hemisphere surface-based source space with subsampling.

Examples
--------
.. code-block:: console

    $ mne setup_source_space --subject sample


 .. note : Only one of --ico, --oct or --spacing options can be set at the same
           time. Default to oct6.

"""

import sys

import mne
from mne.utils import _check_option


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(
        "--src",
        dest="fname",
        help="Output file name. Use a name <dir>/<name>-src.fif",
        metavar="FILE",
        default=None,
    )
    parser.add_option(
        "--morph",
        dest="subject_to",
        help="morph the source space to this subject",
        default=None,
    )
    parser.add_option(
        "--surf",
        dest="surface",
        help="The surface to use. (default to white)",
        default="white",
        type="string",
    )
    parser.add_option(
        "--spacing",
        dest="spacing",
        help="Specifies the approximate grid spacing of the "
        "source space in mm. (default to 7mm)",
        default=None,
        type="int",
    )
    parser.add_option(
        "--ico",
        dest="ico",
        help="use the recursively subdivided icosahedron "
        "to create the source space.",
        default=None,
        type="int",
    )
    parser.add_option(
        "--oct",
        dest="oct",
        help="use the recursively subdivided octahedron to create the source space.",
        default=None,
        type="int",
    )
    parser.add_option(
        "-d",
        "--subjects-dir",
        dest="subjects_dir",
        help="Subjects directory",
        default=None,
    )
    parser.add_option(
        "-n",
        "--n-jobs",
        dest="n_jobs",
        help="The number of jobs to run in parallel "
        "(default 1). Requires the joblib package. "
        "Will use at most 2 jobs"
        " (one for each hemisphere).",
        default=1,
        type="int",
    )
    parser.add_option(
        "--add-dist",
        dest="add_dist",
        help='Add distances. Can be "True", "False", or "patch" '
        "to only compute cortical patch statistics (like the --cps option in MNE-C)",
        default="True",
    )
    parser.add_option(
        "-o",
        "--overwrite",
        dest="overwrite",
        help="to write over existing files",
        default=None,
        action="store_true",
    )
    _add_verbose_flag(parser)

    options, args = parser.parse_args()

    if options.subject is None:
        parser.print_help()
        sys.exit(1)

    subject = options.subject
    subject_to = options.subject_to
    fname = options.fname
    subjects_dir = options.subjects_dir
    spacing = options.spacing
    ico = options.ico
    oct_ = options.oct
    surface = options.surface
    n_jobs = options.n_jobs
    add_dist = options.add_dist
    _check_option("add_dist", add_dist, ("True", "False", "patch"))
    add_dist = {"True": True, "False": False, "patch": "patch"}[add_dist]
    verbose = True if options.verbose is not None else False
    overwrite = True if options.overwrite is not None else False

    # Parse source spacing option
    spacing_options = [ico, oct_, spacing]
    n_options = len([x for x in spacing_options if x is not None])
    use_spacing = "oct6"
    if n_options > 1:
        raise ValueError("Only one spacing option can be set at the same time")
    elif n_options == 0:
        # Default to oct6
        pass
    elif n_options == 1:
        if ico is not None:
            use_spacing = "ico" + str(ico)
        elif oct_ is not None:
            use_spacing = "oct" + str(oct_)
        elif spacing is not None:
            use_spacing = spacing
    del ico, oct_, spacing
    # Generate filename
    if fname is None:
        if subject_to is None:
            fname = subject + "-" + str(use_spacing) + "-src.fif"
        else:
            fname = subject_to + "-" + subject + "-" + str(use_spacing) + "-src.fif"
    else:
        if not (fname.endswith("_src.fif") or fname.endswith("-src.fif")):
            fname = fname + "-src.fif"
    # Create source space
    src = mne.setup_source_space(
        subject=subject,
        spacing=use_spacing,
        surface=surface,
        subjects_dir=subjects_dir,
        n_jobs=n_jobs,
        add_dist=add_dist,
        verbose=verbose,
    )
    # Morph source space if --morph is set
    if subject_to is not None:
        src = mne.morph_source_spaces(
            src,
            subject_to=subject_to,
            subjects_dir=subjects_dir,
            surf=surface,
            verbose=verbose,
        )

    # Save source space to file
    src.save(fname=fname, overwrite=overwrite)


mne.utils.run_command_if_main()