File: misc.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 (81 lines) | stat: -rw-r--r-- 2,366 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
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.


def parse_config(fname):
    """Parse a config file (like .ave and .cov files).

    Parameters
    ----------
    fname : path-like
        Config file name.

    Returns
    -------
    conditions : list of dict
        Each condition is indexed by the event type.
        A condition contains as keys::

            tmin, tmax, name, grad_reject, mag_reject,
            eeg_reject, eog_reject
    """
    reject_params = read_reject_parameters(fname)

    with open(fname) as f:
        lines = f.readlines()

    cat_ind = [i for i, x in enumerate(lines) if "category {" in x]
    event_dict = dict()
    for ind in cat_ind:
        for k in range(ind + 1, ind + 7):
            words = lines[k].split()
            if len(words) >= 2:
                key = words[0]
                if key == "event":
                    event = int(words[1])
                    break
        else:
            raise ValueError("Could not find event id.")
        event_dict[event] = dict(**reject_params)
        for k in range(ind + 1, ind + 7):
            words = lines[k].split()
            if len(words) >= 2:
                key = words[0]
                if key == "name":
                    name = " ".join(words[1:])
                    if name[0] == '"':
                        name = name[1:]
                    if name[-1] == '"':
                        name = name[:-1]
                    event_dict[event]["name"] = name
                if key in ["tmin", "tmax", "basemin", "basemax"]:
                    event_dict[event][key] = float(words[1])
    return event_dict


def read_reject_parameters(fname):
    """Read rejection parameters from .cov or .ave config file.

    Parameters
    ----------
    fname : path-like
        Filename to read.

    Returns
    -------
    params : dict
        The rejection parameters.
    """
    with open(fname) as f:
        lines = f.readlines()

    reject_names = ["gradReject", "magReject", "eegReject", "eogReject", "ecgReject"]
    reject_pynames = ["grad", "mag", "eeg", "eog", "ecg"]
    reject = dict()
    for line in lines:
        words = line.split()
        if words[0] in reject_names:
            reject[reject_pynames[reject_names.index(words[0])]] = float(words[1])

    return reject