Exemple #1
0
def test_gdf_exclude_channels():
    """Test reading GDF data with excluded channels."""
    raw = read_raw_gdf(gdf1_path + '.gdf', exclude=('FP1', 'O1'))
    assert 'FP1' not in raw.ch_names
    assert 'O1' not in raw.ch_names
    raw = read_raw_gdf(gdf2_path + '.gdf', exclude=('Fp1', 'O1'))
    assert 'Fp1' not in raw.ch_names
    assert 'O1' not in raw.ch_names
Exemple #2
0
def load_comp(preload=False):
    """
	Loads all of the BCI IV competition data in from the data folder in root and 
	concatenates it into a single MNE Raw data structure
  """
    raw = read_raw_gdf('data/competition/raw/A01T.gdf', preload=preload)
    for i in range(2, 10):
        print(f"\n\loading data for participant {i}")

        raw = concatenate_raws([
            raw,
            read_raw_gdf(f'data/competition/raw/A0{i}T.gdf', preload=preload)
        ])

    return raw
Exemple #3
0
def test_gdf_data():
    """Test reading raw GDF 1.x files."""
    raw = read_raw_gdf(gdf1_path + '.gdf', eog=None, misc=None, preload=True)
    picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
    data, _ = raw[picks]

    # Test Status is added as event
    EXPECTED_EVS_ONSETS = raw._raw_extras[0]['events'][1]
    evs, evs_id = events_from_annotations(raw)
    assert_array_equal(evs[:, 0], EXPECTED_EVS_ONSETS)
    assert evs_id == {'Unknown': 1}

    # this .npy was generated using the official biosig python package
    raw_biosig = np.load(gdf1_path + '_biosig.npy')
    raw_biosig = raw_biosig * 1e-6  # data are stored in microvolts
    data_biosig = raw_biosig[picks]

    # Assert data are almost equal
    assert_array_almost_equal(data, data_biosig, 8)

    # Test for events
    assert len(raw.annotations.duration == 963)

    # gh-5604
    assert raw.info['meas_date'] == DATE_NONE
Exemple #4
0
def test_gdf_data():
    """Test reading raw GDF 1.x files."""
    raw = read_raw_gdf(gdf1_path + '.gdf', eog=None, misc=None, preload=True)
    picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
    data, _ = raw[picks]

    # Test Status is added as event
    EXPECTED_EVS_ONSETS = raw._raw_extras[0]['events'][1]
    EXPECTED_EVS_ID = {
        '{}'.format(evs): i
        for i, evs in enumerate([
            32769, 32770, 33024, 33025, 33026, 33027, 33028, 33029, 33040,
            33041, 33042, 33043, 33044, 33045, 33285, 33286
        ], 1)
    }
    evs, evs_id = events_from_annotations(raw)
    assert_array_equal(evs[:, 0], EXPECTED_EVS_ONSETS)
    assert evs_id == EXPECTED_EVS_ID

    # this .npy was generated using the official biosig python package
    raw_biosig = np.load(gdf1_path + '_biosig.npy')
    raw_biosig = raw_biosig * 1e-6  # data are stored in microvolts
    data_biosig = raw_biosig[picks]

    # Assert data are almost equal
    assert_array_almost_equal(data, data_biosig, 8)

    # Test for events
    assert len(raw.annotations.duration == 963)

    # gh-5604
    assert raw.info['meas_date'] is None
Exemple #5
0
def test_gdf2_data():
    """Test reading raw GDF 2.x files."""
    raw = read_raw_gdf(gdf2_path + '.gdf', eog=None, misc=None, preload=True)

    picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
    data, _ = raw[picks]

    # This .mat was generated using the official biosig matlab package
    mat = sio.loadmat(gdf2_path + '_biosig.mat')
    data_biosig = mat['dat'] * 1e-6  # data are stored in microvolts
    data_biosig = data_biosig[picks]

    # Assert data are almost equal
    assert_array_almost_equal(data, data_biosig, 8)

    # Find events
    events = find_events(raw, verbose=1)
    events[:, 2] >>= 8  # last 8 bits are system events in biosemi files
    assert_equal(events.shape[0], 2)  # 2 events in file
    assert_array_equal(events[:, 2], [20, 28])

    # gh-5604
    assert raw.info['meas_date'] is None
    _test_raw_reader(read_raw_gdf,
                     input_fname=gdf2_path + '.gdf',
                     eog=None,
                     misc=None)
Exemple #6
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""

        sessions = []
        if self.imagined:
            sessions.append('imagination')

        if self.executed:
            sessions.append('execution')

        out = {}
        for session in sessions:
            paths = self.data_path(subject, session=session)

            eog = ['eog-l', 'eog-m', 'eog-r']
            montage = make_standard_montage('standard_1005')
            data = {}
            for ii, path in enumerate(paths):
                raw = read_raw_gdf(path,
                                   eog=eog,
                                   misc=range(64, 96),
                                   preload=True,
                                   verbose='ERROR')
                raw.set_montage(montage)
                # there is nan in the data
                raw._data[np.isnan(raw._data)] = 0
                data['run_%d' % ii] = raw

            out[session] = data
        return out
Exemple #7
0
def test_gdf2_data():
    """Test reading raw GDF 2.x files."""
    raw = read_raw_gdf(gdf2_path + '.gdf', eog=None, misc=None, preload=True)

    picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
    data, _ = raw[picks]

    # This .mat was generated using the official biosig matlab package
    mat = sio.loadmat(gdf2_path + '_biosig.mat')
    data_biosig = mat['dat'] * 1e-6  # data are stored in microvolts
    data_biosig = data_biosig[picks]

    # Assert data are almost equal
    assert_array_almost_equal(data, data_biosig, 8)

    # Find events
    events = find_events(raw, verbose=1)
    events[:, 2] >>= 8  # last 8 bits are system events in biosemi files
    assert_equal(events.shape[0], 2)  # 2 events in file
    assert_array_equal(events[:, 2], [20, 28])

    # gh-5604
    assert raw.info['meas_date'] == DATE_NONE
    _test_raw_reader(read_raw_gdf, input_fname=gdf2_path + '.gdf',
                     eog=None, misc=None)
Exemple #8
0
def test_gdf_data():
    """Test reading raw GDF 1.x files."""
    raw = read_raw_gdf(gdf1_path + '.gdf', eog=None, misc=None, preload=True)
    picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
    data, _ = raw[picks]

    # Test Status is added as event
    EXPECTED_EVS_ONSETS = raw._raw_extras[0]['events'][1]
    evs, evs_id = events_from_annotations(raw)
    assert_array_equal(evs[:, 0], EXPECTED_EVS_ONSETS)
    assert evs_id == {'Unknown': 1}

    # this .npy was generated using the official biosig python package
    raw_biosig = np.load(gdf1_path + '_biosig.npy')
    raw_biosig = raw_biosig * 1e-6  # data are stored in microvolts
    data_biosig = raw_biosig[picks]

    # Assert data are almost equal
    assert_array_almost_equal(data, data_biosig, 8)

    # Test for events
    assert len(raw.annotations.duration == 963)

    # gh-5604
    assert raw.info['meas_date'] == DATE_NONE
Exemple #9
0
def read_file(object_name):
    for i in runMove:
        files.append(
            read_raw_gdf(f'..\\{object_name}\\{object_name} Run {i}.gdf',
                         stim_channel=None,
                         eog=[61, 62, 63],
                         preload=True))
Exemple #10
0
def load_comp_array(preload=False):
    """
	Loads all of the BCI IV competition data in from the data folder in root and 
	adds each individual Raw data structure to an array so they remain separate
  """
    raws = [[] for i in range(0, 9)]
    for i in range(0, 9):
        print(f"\nloading data for participant {i+1}")
        raws[i] = read_raw_gdf(f'data/competition/raw/A0{i+1}T.gdf',
                               preload=preload)

    return raws
Exemple #11
0
def test_gdf2_birthday(tmpdir):
    """Test reading raw GDF 2.x files."""
    new_fname = str(tmpdir.join('temp.gdf'))
    shutil.copyfile(gdf2_path + '.gdf', new_fname)
    d = int(3.1e15)  # chosen by trial and error to give a reasonable age
    with open(new_fname, 'r+b') as fid:
        fid.seek(176, 0)
        assert np.fromfile(fid, np.uint64, 1)[0] == 0
        fid.seek(176, 0)
        fid.write(np.array([d], np.uint64).tobytes())
        fid.seek(176, 0)
        assert np.fromfile(fid, np.uint64, 1)[0] == d
    raw = read_raw_gdf(new_fname, eog=None, misc=None, preload=True)
    assert raw._raw_extras[0]['subject_info']['age'] == 44
    # XXX this is a bug, it should be populated...
    assert raw.info['subject_info'] is None
Exemple #12
0
def test_gdf2_birthday(tmp_path):
    """Test reading raw GDF 2.x files."""
    new_fname = tmp_path / 'temp.gdf'
    shutil.copyfile(gdf2_path + '.gdf', new_fname)
    # go back 44.5 years so the subject should show up as 44
    offset_edf = (  # to their ref
        datetime.now(tz=timezone.utc) - datetime(1, 1, 1, tzinfo=timezone.utc))
    offset_44_yr = offset_edf - timedelta(days=int(365 * 44.5))  # 44.5 yr ago
    offset_44_yr_days = offset_44_yr.total_seconds() / (24 * 60 * 60)  # days
    d = (int(offset_44_yr_days) + 367) * 2**32  # with their conversion
    with open(new_fname, 'r+b') as fid:
        fid.seek(176, 0)
        assert np.fromfile(fid, np.uint64, 1)[0] == 0
        fid.seek(176, 0)
        fid.write(np.array([d], np.uint64).tobytes())
        fid.seek(176, 0)
        assert np.fromfile(fid, np.uint64, 1)[0] == d
    raw = read_raw_gdf(new_fname, eog=None, misc=None, preload=True)
    assert raw._raw_extras[0]['subject_info']['age'] == 44
    # XXX this is a bug, it should be populated...
    assert raw.info['subject_info'] is None
Exemple #13
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""

        sessions = []
        if self.imagined:
            sessions.append("imagination")

        if self.executed:
            sessions.append("execution")

        out = {}
        for session in sessions:
            paths = self.data_path(subject, session=session)

            eog = ["eog-l", "eog-m", "eog-r"]
            montage = make_standard_montage("standard_1005")
            data = {}
            for ii, path in enumerate(paths):
                raw = read_raw_gdf(path,
                                   eog=eog,
                                   misc=range(64, 96),
                                   preload=True,
                                   verbose="ERROR")
                raw.set_montage(montage)
                # there is nan in the data
                raw._data[np.isnan(raw._data)] = 0
                # Modify the annotations to match the name of the command
                stim = raw.annotations.description.astype(np.dtype("<21U"))
                stim[stim == "1536"] = "right_elbow_flexion"
                stim[stim == "1537"] = "right_elbow_extension"
                stim[stim == "1538"] = "right_supination"
                stim[stim == "1539"] = "right_pronation"
                stim[stim == "1540"] = "right_hand_close"
                stim[stim == "1541"] = "right_hand_open"
                stim[stim == "1542"] = "rest"
                raw.annotations.description = stim
                data["run_%d" % ii] = raw

            out[session] = data
        return out
Exemple #14
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""

        sessions = []
        if self.imagined:
            sessions.append('imagination')

        if self.executed:
            sessions.append('execution')

        out = {}
        for session in sessions:
            paths = self.data_path(subject, session=session)

            eog = ['eog-l', 'eog-m', 'eog-r']
            montage = make_standard_montage('standard_1005')
            data = {}
            for ii, path in enumerate(paths):
                raw = read_raw_gdf(path, eog=eog, misc=range(64, 96),
                                   preload=True, verbose='ERROR')
                raw.set_montage(montage)
                # there is nan in the data
                raw._data[np.isnan(raw._data)] = 0
                # Modify the annotations to match the name of the command
                stim = raw.annotations.description.astype(np.dtype('<21U'))
                stim[stim == '1536'] = "right_elbow_flexion"
                stim[stim == '1537'] = "right_elbow_extension"
                stim[stim == '1538'] = "right_supination"
                stim[stim == '1539'] = "right_pronation"
                stim[stim == '1540'] = "right_hand_close"
                stim[stim == '1541'] = "right_hand_open"
                stim[stim == '1542'] = "rest"
                raw.annotations.description = stim
                data['run_%d' % ii] = raw

            out[session] = data
        return out
Exemple #15
0
def extract(filepaths, ica=None, fit_ica=True):
    if ica is None:
        ica = get_ica_transformers("infomax")

    epochs, labels = list(), list()
    for filepath in filepaths:
        print("loading", filepath)
        try:
            gdf = read_raw_gdf(
                filepath,
                eog=["EOG-left", "EOG-central", "EOG-right"],
                exclude=["EOG-left", "EOG-central", "EOG-right"])
            events = events_from_annotations(gdf,
                                             event_id={
                                                 "769": 0,
                                                 "770": 1,
                                                 "771": 2,
                                                 "772": 3
                                             })
            epoch = Epochs(gdf,
                           events[0],
                           event_repeated="drop",
                           reject_by_annotation=True,
                           tmin=-.3,
                           tmax=.7,
                           reject=dict(eeg=1e-4))
            epoch.drop_bad()
        except ValueError:
            print("Error in", filepath)
            continue
        epochs.append(epoch)
        labels.append(epoch.events[:, 2])

    labels = np.concatenate(labels)
    n_epochs, n_channels, n_times = epochs[0].get_data().shape
    ica_vec = [
        epoch.get_data().transpose(1, 0, 2).reshape(n_channels, -1).T
        for epoch in epochs
    ]
    ica_vec = np.concatenate(ica_vec, axis=0)

    if fit_ica:
        ica.fit(ica_vec)

    transformed = ica.transform(ica_vec)
    transformed = ica_vec

    transformed = transformed.T.reshape(n_channels, -1,
                                        n_times).transpose(1, 0, 2)

    features, freqs = psd_array_multitaper(transformed,
                                           250.,
                                           fmin=0,
                                           fmax=20,
                                           bandwidth=2)

    n_epochs, _, _ = features.shape
    #    features = features.reshape(n_epochs, -1)
    features = features.mean(axis=2)
    labels_placeholder = np.zeros((len(labels), 4))

    for i, l in enumerate(labels):
        labels_placeholder[i, l] = 1

    labels = labels_placeholder
    return features, labels, ica
Exemple #16
0
    def __init__(self, file, min_chunk_size=4):
        Node.__init__(self, None)

        filename, self._file_extension = os.path.splitext(file)
        self._events = []
        if self._file_extension in ['.gdf', '.set', '.vhdr']:
            if self._file_extension == '.gdf':
                self._raw = read_raw_gdf(file)
            elif self._file_extension == '.set':
                self._raw = read_raw_eeglab(file)
            elif self._file_extension == '.vhdr':
                self._raw = read_raw_brainvision(file)
            self._sampling_frequency = self._raw.info['sfreq']
            self._channels = self._raw.info.ch_names
            try:
                # to test
                events = find_events(self._raw)
                logging.debug('Get from find_events')
            except ValueError:
                events = events_from_annotations(self._raw)
                logging.debug('Get from events_from_annotations')
            nb_to_event = {events[1][key]: key for key in events[1]}
            for h in events[0]:
                try:
                    value = float(nb_to_event[h[2]])
                except ValueError:
                    value = nb_to_event[h[2]]
                self._events.append((h[0] / 1000, value))
            self._end_record = self._raw.times[-1]
            self._start_record = self._raw.times[0]
        elif self._file_extension == '.xdf':
            streams, header = pyxdf.load_xdf(file,
                                             synchronize_clocks=False,
                                             verbose=False)
            logging.info(f'Found {len(streams)} streams in xdf file')
            for ix, stream in enumerate(streams):
                sampling_frequency = float(stream['info']['nominal_srate'][0])
                if sampling_frequency == 0:
                    logging.debug(f'Get marker from stream {ix}')
                    for timestamp, event in zip(stream['time_stamps'],
                                                stream['time_series']):
                        self._events.append((timestamp, float(event)))
                else:
                    logging.debug(f'Get data from stream {ix}')
                    self._sampling_frequency = sampling_frequency
                    nb_chan = int(stream['info']['channel_count'][0])
                    self._channels = [(stream['info']['desc'][0]['channels'][0]
                                       ['channel'][i]['label'][0])
                                      for i in range(nb_chan)]
                    self._df = pd.DataFrame(stream['time_series'],
                                            stream['time_stamps'],
                                            self._channels)
                    self._start_record = stream['time_stamps'][0]
                    self._end_record = stream['time_stamps'][-1]

        self.marker_output = Port()

        self.marker_output.set_parameters(data_type='marker',
                                          channels=['marker'],
                                          sampling_frequency=0,
                                          meta='')

        self.output.set_parameters(data_type='signal',
                                   channels=self._channels,
                                   sampling_frequency=self._sampling_frequency,
                                   meta='')

        Node.log_instance(
            self, {
                'marquers output': self.marker_output.id,
                'sampling frequency': self._sampling_frequency,
                'channels': self._channels,
                'min chunk size': min_chunk_size,
                'from file': file
            })

        self._last_t = None
        self._min_period = min_chunk_size / self._sampling_frequency
        self._start_time = None
        self._flag = True
Exemple #17
0
def test_gdf_include():
    """Test reading GDF data with include."""
    raw = read_raw_gdf(gdf1_path + '.gdf', include=('FP1', 'O1'))
    assert sorted(raw.ch_names) == ['FP1', 'O1']
Exemple #18
0
            'EOG-central': 'eog',
            'EOG-right': 'eog'
        }
    )  # set EOG channels to EOG type because they are incorrectly marked as EEG
    raw = raw.reorder_channels(sorted(
        raw.ch_names))  # reorder channels into alphabetical
    raw = raw.set_eeg_reference(ch_type='auto')  # set eeg reference to auto

    raw = raw.set_montage(
        'standard_1020',
        on_missing='warn')  # set montage to 10/20, warn on missing

    return raw


raw = prep(read_raw_gdf('data/competition/raw/A01T.gdf', preload=True),
           CHANNEL_MAP,
           GOODS,
           l_freq=LO_FREQ,
           h_freq=HI_FREQ)

### ICA preprocessing
from mne.preprocessing import ICA

ica = ICA(n_components=21)
ica.fit(raw)
eog_indices, eog_scores = ica.find_bads_eog(raw)
ica.exclude = eog_indices
ica.apply(raw)

### epoching
Exemple #19
0
def test_one_channel_gdf():
    """Test a one-channel GDF file."""
    with pytest.warns(RuntimeWarning, match='different highpass'):
        ecg = read_raw_gdf(gdf_1ch_path, preload=True)
    assert ecg['ECG'][0].shape == (1, 4500)
    assert 150.0 == ecg.info['sfreq']
Exemple #20
0
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
'''
Parameters:
'''
tmin = 0
tmax = 10
'''
Parameters:
'''
tmin = 0
tmax = 10

file_P01_Run3 = read_raw_gdf('..\P01\P01 Run 3.gdf',
                             stim_channel=None,
                             eog=[61, 62, 63])
eventDescription_offline_paradigm = {
    '768': "trial start",
    '785': "beep",
    '786': "fixation cross",
    '776': "supinationclass cue",
    '777': "pronationclass cue",
    '779': "hand openclass cue",
    '925': "palmar graspclass cue",
    '926': "ateral graspclass cue",
}
# narrow it down to two
eventDescription_offline_paradigm = {
    '785': "beep",
    '925': "palmar graspclass cue",