File: fieldtrip.py

package info (click to toggle)
python-mne 0.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 95,104 kB
  • sloc: python: 110,639; makefile: 222; sh: 15
file content (174 lines) | stat: -rw-r--r-- 6,339 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
# -*- coding: UTF-8 -*-
# Authors: Thomas Hartmann <thomas.hartmann@th-ht.de>
#          Dirk Gütlin <dirk.guetlin@stud.sbg.ac.at>
#
# License: BSD (3-clause)

import numpy as np

from .utils import _create_info, _set_tmin, _create_events, \
    _create_event_metadata
from .. import RawArray
from ...epochs import EpochsArray
from ...evoked import EvokedArray


def read_raw_fieldtrip(fname, info, data_name='data'):
    """Load continuous (raw) data from a FieldTrip preprocessing structure.

    This function expects to find single trial raw data (FT_DATATYPE_RAW) in
    the structure data_name is pointing at.

    .. warning:: FieldTrip does not normally store the original information
                 concerning channel location, orientation, type etc. It is
                 therefore **highly recommended** to provide the info field.
                 This can be obtained by reading the original raw data file
                 with MNE functions (without preload). The returned object
                 contains the necessary info field.

    Parameters
    ----------
    fname: str
        Path and filename of the .mat file containing the data.
    info: dict or None
        The info dict of the raw data file corresponding to the data to import.
        If this is set to None, limited information is extracted from the
        FieldTrip structure.
    data_name: str
        Name of heading dict/ variable name under which the data was originally
        saved in MATLAB.

    Returns
    -------
    raw: Instance of RawArray
        A Raw Object containing the loaded data.

    """
    from ...externals.pymatreader.pymatreader import read_mat

    ft_struct = read_mat(fname,
                         ignore_fields=['previous'],
                         variable_names=[data_name])

    # load data and set ft_struct to the heading dictionary
    ft_struct = ft_struct[data_name]

    info = _create_info(ft_struct, info)  # create info structure
    data = np.array(ft_struct['trial'])  # create the main data array

    if data.ndim > 2:
        data = np.squeeze(data)

    if data.ndim == 1:
        data = data[np.newaxis, ...]

    if data.ndim != 2:
        raise RuntimeError('The data you are trying to load does not seem to '
                           'be raw data')

    raw = RawArray(data, info)  # create an MNE RawArray
    return raw


def read_epochs_fieldtrip(fname, info, data_name='data',
                          trialinfo_column=0):
    """Load epoched data from a FieldTrip preprocessing structure.

    This function expects to find epoched data in the structure data_name is
    pointing at.

    .. warning:: Only epochs with the same amount of channels and samples are
                 supported!

    .. warning:: FieldTrip does not normally store the original information
                 concerning channel location, orientation, type etc. It is
                 therefore **highly recommended** to provide the info field.
                 This can be obtained by reading the original raw data file
                 with MNE functions (without preload). The returned object
                 contains the necessary info field.

    Parameters
    ----------
    fname: str
        Path and filename of the .mat file containing the data.
    info: dict or None
        The info dict of the raw data file corresponding to the data to import.
        If this is set to None, limited information is extracted from the
        FieldTrip structure.
    data_name: str
        Name of heading dict/ variable name under which the data was originally
        saved in MATLAB.
    trialinfo_column: int
        Column of the trialinfo matrix to use for the event codes

    Returns
    -------
    epochs: Instance of EpochsArray
        An EpochsArray containing the loaded data.


    """
    from ...externals.pymatreader.pymatreader import read_mat
    ft_struct = read_mat(fname,
                         ignore_fields=['previous'],
                         variable_names=[data_name])

    # load data and set ft_struct to the heading dictionary
    ft_struct = ft_struct[data_name]

    info = _create_info(ft_struct, info)  # create info structure
    data = np.array(ft_struct['trial'])  # create the epochs data array
    events = _create_events(ft_struct, trialinfo_column)
    metadata = _create_event_metadata(ft_struct)
    tmin = _set_tmin(ft_struct)  # create start time

    epochs = EpochsArray(data=data, info=info, tmin=tmin,
                         events=events, metadata=metadata, proj=False)
    return epochs


def read_evoked_fieldtrip(fname, info, comment=None,
                          data_name='data'):
    """Load evoked data from a FieldTrip timelocked structure.

    This function expects to find timelocked data in the structure data_name is
    pointing at.

    .. warning:: FieldTrip does not normally store the original information
                 concerning channel location, orientation, type etc. It is
                 therefore **highly recommended** to provide the info field.
                 This can be obtained by reading the original raw data file
                 with MNE functions (without preload). The returned object
                 contains the necessary info field.

    Parameters
    ----------
    fname: str
        Path and filename of the .mat file containing the data.
    info: dict or None
        The info dict of the raw data file corresponding to the data to import.
        If this is set to None, limited information is extracted from the
        FieldTrip structure.
    comment: str
        Comment on dataset. Can be the condition.
    data_name: str
        Name of heading dict/ variable name under which the data was originally
        saved in MATLAB.

    Returns
    -------
    evoked: Instance of EvokedArray
        An EvokedArray containing the loaded data.

    """
    from ...externals.pymatreader.pymatreader import read_mat
    ft_struct = read_mat(fname,
                         ignore_fields=['previous'],
                         variable_names=[data_name])
    ft_struct = ft_struct[data_name]

    info = _create_info(ft_struct, info)  # create info structure
    data_evoked = ft_struct['avg']  # create evoked data

    evoked = EvokedArray(data_evoked, info, comment=comment)
    return evoked