File: mne_flash_bem.py

package info (click to toggle)
python-mne 1.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 100,172 kB
  • sloc: python: 166,349; pascal: 3,602; javascript: 1,472; sh: 334; makefile: 236
file content (125 lines) | stat: -rw-r--r-- 4,991 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
#!/usr/bin/env python
"""Create 3-layer BEM model from Flash MRI images.

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

    $ mne flash_bem --subject=sample
    $ mne flash_bem -s sample -n --registered -5 sample/mri/mef05.mgz -3 sample/mri/mef30.mgz
    $ mne flash_bem -s sample -n --registered -5 sample/mri/flash/mef05_*.mgz -3 sample/mri/flash/mef30_*.mgz

Notes
-----
This program assumes that FreeSurfer and MNE are installed and
sourced properly.

This function extracts the BEM surfaces (outer skull, inner skull, and
outer skin) from multiecho FLASH MRI data with spin angles of 5 and 30
degrees. The multiecho FLASH data can be input as .mgz or .nii files.
This function assumes that the Freesurfer segmentation of the subject
has been completed. In particular, the T1.mgz and brain.mgz MRI volumes
should be, as usual, in the subject's mri directory.

"""  # noqa E501
# Authors: Lorenzo De Santis
#          Alexandre Gramfort

import mne
from mne.bem import convert_flash_mris, make_flash_bem


def _vararg_callback(option, opt_str, value, parser):
    assert value is None
    value = []

    for arg in parser.rargs:
        # stop on --foo like options
        if arg[:2] == "--" and len(arg) > 2:
            break
        if arg[:1] == "-" and len(arg) > 1:
            break
        value.append(arg)

    del parser.rargs[:len(value)]
    setattr(parser.values, option.dest, value)


def run():
    """Run command."""
    from mne.commands.utils import get_optparser

    parser = get_optparser(__file__)

    parser.add_option("-s", "--subject", dest="subject",
                      help="Subject name", default=None)
    parser.add_option("-d", "--subjects-dir", dest="subjects_dir",
                      help="Subjects directory", default=None)
    parser.add_option("-3", "--flash30", "--noflash30", dest="flash30",
                      action="callback", callback=_vararg_callback,
                      help=("The 30-degree flip angle data. If no argument do "
                            "not use flash30. If arguments are given, them as "
                            "file names."))
    parser.add_option("-5", "--flash5", dest="flash5",
                      action="callback", callback=_vararg_callback,
                      help=("Path to the multiecho flash 5 images. "
                            "Can be one file or one per echo."),)
    parser.add_option("-r", "--registered", dest="registered",
                      action="store_true", default=False,
                      help=("Set if the Flash MRI images have already "
                            "been registered with the T1.mgz file."))
    parser.add_option("-n", "--noconvert", dest="noconvert",
                      action="store_true", default=False,
                      help=("[DEPRECATED] Assume that the Flash MRI images "
                            "have already been converted to mgz files"))
    parser.add_option("-u", "--unwarp", dest="unwarp",
                      action="store_true", default=False,
                      help=("Run grad_unwarp with -unwarp <type> "
                            "option on each of the converted data sets"))
    parser.add_option("-o", "--overwrite", dest="overwrite",
                      action="store_true", default=False,
                      help="Write over existing .surf files in bem folder")
    parser.add_option("-v", "--view", dest="show", action="store_true",
                      help="Show BEM model in 3D for visual inspection",
                      default=False)
    parser.add_option("--copy", dest="copy",
                      help="Use copies instead of symlinks for surfaces",
                      action="store_true")
    parser.add_option("-p", "--flash-path", dest="flash_path",
                      default=None,
                      help="[DEPRECATED] The directory containing flash5.mgz "
                      "files (defaults to "
                      "$SUBJECTS_DIR/$SUBJECT/mri/flash/parameter_maps")

    options, _ = parser.parse_args()

    subject = options.subject
    subjects_dir = options.subjects_dir
    flash5 = options.flash5
    if flash5 is None or len(flash5) == 0:
        flash5 = True
    flash30 = options.flash30
    if flash30 is None:
        flash30 = True
    elif len(flash30) == 0:
        flash30 = False
    register = not options.registered
    unwarp = options.unwarp
    overwrite = options.overwrite
    show = options.show
    copy = options.copy

    if options.subject is None:
        parser.print_help()
        raise RuntimeError('The subject argument must be set')

    flash5_img = convert_flash_mris(
        subject=subject, subjects_dir=subjects_dir, flash5=flash5,
        flash30=flash30, unwarp=unwarp, verbose=True
    )
    make_flash_bem(subject=subject, subjects_dir=subjects_dir,
                   overwrite=overwrite, show=show, copy=copy,
                   register=register, flash5_img=flash5_img, verbose=True)


mne.utils.run_command_if_main()