Exemplo n.º 1
0
def test_brainvision_data():
    """Test reading raw Brain Vision files
    """
    raw_py = read_raw_brainvision(vhdr_path, elp_path, elp_names, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_py, times_py = raw_py[picks]

    print(raw_py)  # to test repr
    print(raw_py.info)  # to test Info repr

    # compare with a file that was generated using MNE-C
    raw_bin = Raw(eeg_bin, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_bin, times_bin = raw_bin[picks]

    assert_array_almost_equal(data_py, data_bin)
    assert_array_almost_equal(times_py, times_bin)

    # Make sure EOG channels are marked correctly
    raw_py = read_raw_brainvision(vhdr_path, elp_path, elp_names, eog=eog,
                                  preload=True)
    for ch in raw_py.info['chs']:
        if ch['ch_name'] in eog:
            assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH)
        elif ch['ch_name'] in elp_names:
            assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH)
        elif ch['ch_name'] == 'STI 014':
            assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH)
        else:
            raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])
Exemplo n.º 2
0
def test_vhdr_codepage_ansi():
    """Test BV reading with ANSI codepage."""
    raw_init = read_raw_brainvision(vhdr_path)
    data_expected, times_expected = raw_init[:]
    tempdir = _TempDir()
    ansi_vhdr_path = op.join(tempdir, op.split(vhdr_path)[-1])
    ansi_vmrk_path = op.join(tempdir, op.split(vmrk_path)[-1])
    ansi_eeg_path = op.join(tempdir, op.split(eeg_path)[-1])
    # copy data file
    shutil.copy(eeg_path, ansi_eeg_path)
    # modify header file
    with open(ansi_vhdr_path, 'wb') as fout:
        with open(vhdr_path, 'rb') as fin:
            for line in fin:
                # Common Infos section
                if line.startswith(b'Codepage'):
                    line = b'Codepage=ANSI\n'
                fout.write(line)
    # modify marker file
    with open(ansi_vmrk_path, 'wb') as fout:
        with open(vmrk_path, 'rb') as fin:
            for line in fin:
                # Common Infos section
                if line.startswith(b'Codepage'):
                    line = b'Codepage=ANSI\n'
                fout.write(line)

    raw = read_raw_brainvision(ansi_vhdr_path)
    data_new, times_new = raw[:]

    assert_equal(raw_init.ch_names, raw.ch_names)
    assert_allclose(data_new, data_expected, atol=1e-15)
    assert_allclose(times_new, times_expected, atol=1e-15)
Exemplo n.º 3
0
def test_read_segment():
    """Test writing raw eeg files when preload is False
    """
    tempdir = _TempDir()
    raw1 = read_raw_brainvision(vhdr_path, eog=eog, preload=False)
    raw1_file = op.join(tempdir, 'test1-raw.fif')
    raw1.save(raw1_file, overwrite=True)
    raw11 = Raw(raw1_file, preload=True)
    data1, times1 = raw1[:, :]
    data11, times11 = raw11[:, :]
    assert_array_almost_equal(data1, data11, 8)
    assert_array_almost_equal(times1, times11)
    assert_equal(sorted(raw1.info.keys()), sorted(raw11.info.keys()))

    raw2 = read_raw_brainvision(vhdr_path, eog=eog, preload=True)
    raw2_file = op.join(tempdir, 'test2-raw.fif')
    raw2.save(raw2_file, overwrite=True)
    data2, times2 = raw2[:, :]
    assert_array_equal(data1, data2)
    assert_array_equal(times1, times2)

    raw1 = Raw(raw1_file, preload=True)
    raw2 = Raw(raw2_file, preload=True)
    assert_array_equal(raw1._data, raw2._data)

    # save with buffer size smaller than file
    raw3_file = op.join(tempdir, 'test3-raw.fif')
    raw3 = read_raw_brainvision(vhdr_path, eog=eog)
    raw3.save(raw3_file, buffer_size_sec=2)
    raw3 = Raw(raw3_file, preload=True)
    assert_array_equal(raw3._data, raw1._data)
def test_brainvision_data():
    """Test reading raw Brain Vision files."""
    pytest.raises(IOError, read_raw_brainvision, vmrk_path)
    pytest.raises(ValueError, read_raw_brainvision, vhdr_path, montage,
                  preload=True, scale="foo")

    raw_py = _test_raw_reader(
        read_raw_brainvision, vhdr_fname=vhdr_path, montage=montage,
        eog=eog, misc='auto', event_id=event_id)

    assert ('RawBrainVision' in repr(raw_py))

    assert_equal(raw_py.info['highpass'], 0.)
    assert_equal(raw_py.info['lowpass'], 250.)

    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_py, times_py = raw_py[picks]

    # compare with a file that was generated using MNE-C
    raw_bin = read_raw_fif(eeg_bin, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_bin, times_bin = raw_bin[picks]

    assert_array_almost_equal(data_py, data_bin)
    assert_array_almost_equal(times_py, times_bin)

    # Make sure EOG channels are marked correctly
    for ch in raw_py.info['chs']:
        if ch['ch_name'] in eog:
            assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH)
        elif ch['ch_name'] == 'STI 014':
            assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH)
        elif ch['ch_name'] in ('CP5', 'CP6'):
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_NONE)
        elif ch['ch_name'] == 'ReRef':
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_CEL)
        elif ch['ch_name'] in raw_py.info['ch_names']:
            assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_V)
        else:
            raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])

    # test loading v2
    read_raw_brainvision(vhdr_v2_path, eog=eog, preload=True,
                         event_id=event_id,
                         trig_shift_by_type={'response': 1000},
                         verbose='error')
    # For the nanovolt unit test we use the same data file with a different
    # header file.
    raw_nV = _test_raw_reader(
        read_raw_brainvision, vhdr_fname=vhdr_nV_path, montage=montage,
        eog=eog, misc='auto', event_id=event_id)
    assert_equal(raw_nV.info['chs'][0]['ch_name'], 'FP1')
    assert_equal(raw_nV.info['chs'][0]['kind'], FIFF.FIFFV_EEG_CH)
    data_nanovolt, _ = raw_nV[0]
    assert_array_almost_equal(data_py[0, :], data_nanovolt[0, :])
Exemplo n.º 5
0
def test_brainvision_with_montage():
    """Test reading embedded montage information."""
    raw = read_raw_brainvision(vhdr_v2_path, eog=eog, misc=['ReRef'])
    for i, d in enumerate(raw.info['dig'], 1):
        assert_equal(d['coord_frame'], FIFF.FIFFV_COORD_HEAD)
        assert_equal(d['ident'], i)
        assert_equal(d['kind'], FIFF.FIFFV_POINT_EEG)
        assert_equal(len(d['r']), 3)

    raw_none = read_raw_brainvision(vhdr_v2_path, verbose='error')
    for r, n in zip(raw.info['chs'], raw_none.info['chs']):
        if r['kind'] != n['kind']:
            assert_array_equal(r['loc'], n['loc'])
Exemplo n.º 6
0
def test_brainvision_data():
    """Test reading raw Brain Vision files
    """
    assert_raises(IOError, read_raw_brainvision, vmrk_path)
    assert_raises(ValueError, read_raw_brainvision, vhdr_path, montage,
                  preload=True, scale="foo")

    with warnings.catch_warnings(record=True) as w:  # event parsing
        raw_py = _test_raw_reader(
            read_raw_brainvision, vhdr_fname=vhdr_path, montage=montage,
            eog=eog, misc='auto')
    assert_true(all('parse triggers that' in str(ww.message) for ww in w))

    assert_true('RawBrainVision' in repr(raw_py))

    assert_equal(raw_py.info['highpass'], 0.)
    assert_equal(raw_py.info['lowpass'], 250.)

    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_py, times_py = raw_py[picks]

    # compare with a file that was generated using MNE-C
    raw_bin = Raw(eeg_bin, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_bin, times_bin = raw_bin[picks]

    assert_array_almost_equal(data_py, data_bin)
    assert_array_almost_equal(times_py, times_bin)

    # Make sure EOG channels are marked correctly
    for ch in raw_py.info['chs']:
        if ch['ch_name'] in eog:
            assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH)
        elif ch['ch_name'] == 'STI 014':
            assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH)
        elif ch['ch_name'] == 'CP6':
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_NONE)
        elif ch['ch_name'] == 'ReRef':
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_CEL)
        elif ch['ch_name'] in raw_py.info['ch_names']:
            assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_V)
        else:
            raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])

    # test loading v2
    read_raw_brainvision(vhdr_v2_path, eog=eog, preload=True,
                         response_trig_shift=1000)
Exemplo n.º 7
0
def test_fif_dig_montage():
    """Test FIF dig montage support"""
    dig_montage = read_dig_montage(fif=fif_dig_montage_fname)

    # Make a BrainVision file like the one the user would have had
    with warnings.catch_warnings(record=True) as w:
        raw_bv = read_raw_brainvision(bv_fname, preload=True)
    assert_true(any('will be dropped' in str(ww.message) for ww in w))
    raw_bv_2 = raw_bv.copy()
    mapping = dict()
    for ii, ch_name in enumerate(raw_bv.ch_names[:-1]):
        mapping[ch_name] = 'EEG%03d' % (ii + 1,)
    raw_bv.rename_channels(mapping)
    for ii, ch_name in enumerate(raw_bv_2.ch_names[:-1]):
        mapping[ch_name] = 'EEG%03d' % (ii + 33,)
    raw_bv_2.rename_channels(mapping)
    raw_bv.drop_channels(['STI 014'])
    raw_bv.add_channels([raw_bv_2])

    # Set the montage
    raw_bv.set_montage(dig_montage)

    # Check the result
    evoked = read_evokeds(evoked_fname)[0]

    assert_equal(len(raw_bv.ch_names), len(evoked.ch_names))
    for ch_py, ch_c in zip(raw_bv.info['chs'], evoked.info['chs']):
        assert_equal(ch_py['ch_name'], ch_c['ch_name'].replace('EEG ', 'EEG'))
        # C actually says it's unknown, but it's not (?):
        # assert_equal(ch_py['coord_frame'], ch_c['coord_frame'])
        assert_equal(ch_py['coord_frame'], FIFF.FIFFV_COORD_HEAD)
        assert_allclose(ch_py['loc'], ch_c['loc'])
    assert_dig_allclose(raw_bv.info, evoked.info)
Exemplo n.º 8
0
def test_brainvision_data_filters():
    """Test reading raw Brain Vision files
    """
    raw = read_raw_brainvision(vhdr_highpass_path, elp_path, elp_names,
                               preload=False)
    assert_equal(raw.info['highpass'], 0.1)
    assert_equal(raw.info['lowpass'], 250.)
Exemplo n.º 9
0
def test_read_vhdr_annotations_and_events():
    """Test load brainvision annotations and parse them to events."""
    sfreq = 1000.0
    expected_orig_time = 1384359243.794231
    expected_onset_latency = np.array(
        [0, 486., 496., 1769., 1779., 3252., 3262., 4935., 4945., 5999., 6619.,
         6629., 7629., 7699.]
    )
    expected_annot_description = [
        'New Segment/', 'Stimulus/S253', 'Stimulus/S255', 'Stimulus/S254',
        'Stimulus/S255', 'Stimulus/S254', 'Stimulus/S255', 'Stimulus/S253',
        'Stimulus/S255', 'Response/R255', 'Stimulus/S254', 'Stimulus/S255',
        'SyncStatus/Sync On', 'Optic/O  1'
    ]
    expected_events = np.stack([
        expected_onset_latency,
        np.zeros_like(expected_onset_latency),
        [99999, 253, 255, 254, 255, 254, 255, 253, 255, 1255, 254, 255, 99998,
         2001],
    ]).astype('int64').T
    expected_event_id = {'New Segment/': 99999, 'Stimulus/S253': 253,
                         'Stimulus/S255': 255, 'Stimulus/S254': 254,
                         'Response/R255': 1255, 'SyncStatus/Sync On': 99998,
                         'Optic/O  1': 2001}

    raw = read_raw_brainvision(vhdr_path, eog=eog)

    # validate annotations
    assert raw.annotations.orig_time == expected_orig_time
    assert_allclose(raw.annotations.onset, expected_onset_latency / sfreq)
    assert_array_equal(raw.annotations.description, expected_annot_description)

    # validate event extraction
    events, event_id = events_from_annotations(raw)
    assert_array_equal(events, expected_events)
    assert event_id == expected_event_id

    # validate that None gives us a sorted list
    expected_none_event_id = {desc: idx + 1 for idx, desc in enumerate(sorted(
        event_id.keys()))}
    events, event_id = events_from_annotations(raw, event_id=None)
    assert event_id == expected_none_event_id

    # Add some custom ones, plus a 2-digit one
    s_10 = 'Stimulus/S 10'
    raw.annotations.append([1, 2, 3], 10, ['ZZZ', s_10, 'YYY'])
    expected_event_id.update(YYY=10001, ZZZ=10002)  # others starting at 10001
    expected_event_id[s_10] = 10
    _, event_id = events_from_annotations(raw)
    assert event_id == expected_event_id

    # Concatenating two shouldn't change the resulting event_id
    # (BAD and EDGE should be ignored)
    with pytest.warns(RuntimeWarning, match='expanding outside'):
        raw_concat = concatenate_raws([raw.copy(), raw.copy()])
    _, event_id = events_from_annotations(raw_concat)
    assert event_id == expected_event_id
Exemplo n.º 10
0
def test_brainvision_data_filters():
    """Test reading raw Brain Vision files
    """
    raw = read_raw_brainvision(vhdr_highpass_path, montage, eog=eog,
                               preload=True)
    assert_equal(raw.info['highpass'], 0.1)
    assert_equal(raw.info['lowpass'], 250.)
    raw.info["lowpass"] = None
    raw.filter(1, 30)
Exemplo n.º 11
0
def test_vmrk_meas_date():
    """Test successful extraction of measurement date."""
    # Test file that does have a specific date
    with pytest.warns(RuntimeWarning, match='will be dropped'):
        raw = read_raw_brainvision(vhdr_path)
    assert_allclose(raw.info['meas_date'], [1384359243, 794231])
    assert '2013-11-13 16:14:03 GMT' in repr(raw.info)

    # Test file with multiple dates ... we should only take the first
    with pytest.warns(RuntimeWarning, match='software filter'):
        raw = read_raw_brainvision(vhdr_old_path)
    assert_allclose(raw.info['meas_date'], [1184588560, 937453])
    assert '2007-07-16 12:22:40 GMT' in repr(raw.info)

    # Test files with no date, we should get DATE_NONE from mne.io.write
    with pytest.warns(RuntimeWarning, match='coordinate information'):
        raw = read_raw_brainvision(vhdr_v2_path)
    assert_allclose(raw.info['meas_date'], DATE_NONE)
    assert 'unspecified' in repr(raw.info)
Exemplo n.º 12
0
def test_brainvision_data():
    """Test reading raw Brain Vision files
    """
    assert_raises(IOError, read_raw_brainvision, vmrk_path)
    assert_raises(TypeError, read_raw_brainvision, vhdr_path, montage,
                  preload=True, scale="0")
    raw_py = read_raw_brainvision(vhdr_path, montage, eog=eog, preload=True)
    raw_py.load_data()  # currently does nothing
    assert_true('RawBrainVision' in repr(raw_py))

    assert_equal(raw_py.info['highpass'], 0.)
    assert_equal(raw_py.info['lowpass'], 250.)

    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_py, times_py = raw_py[picks]

    print(raw_py)  # to test repr
    print(raw_py.info)  # to test Info repr

    # compare with a file that was generated using MNE-C
    raw_bin = Raw(eeg_bin, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_bin, times_bin = raw_bin[picks]

    assert_array_almost_equal(data_py, data_bin)
    assert_array_almost_equal(times_py, times_bin)

    # Make sure EOG channels are marked correctly
    raw_py = read_raw_brainvision(vhdr_path, montage, eog=eog,
                                  preload=True)
    for ch in raw_py.info['chs']:
        if ch['ch_name'] in eog:
            assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH)
        elif ch['ch_name'] == 'STI 014':
            assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH)
        elif ch['ch_name'] in raw_py.info['ch_names']:
            assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH)
        else:
            raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])

    # Make sure concatenation works
    raw_concat = concatenate_raws([raw_py.copy(), raw_py])
    assert_equal(raw_concat.n_times, 2 * raw_py.n_times)
Exemplo n.º 13
0
def test_read_segment():
    """Test writing raw eeg files when preload is False
    """
    tempdir = _TempDir()
    raw1 = read_raw_brainvision(vhdr_path, eog=eog, preload=False)
    raw1_file = op.join(tempdir, "test1-raw.fif")
    raw1.save(raw1_file, overwrite=True)
    raw11 = Raw(raw1_file, preload=True)
    data1, times1 = raw1[:, :]
    data11, times11 = raw11[:, :]
    assert_array_almost_equal(data1, data11, 8)
    assert_array_almost_equal(times1, times11)
    assert_equal(sorted(raw1.info.keys()), sorted(raw11.info.keys()))

    raw2 = read_raw_brainvision(vhdr_path, eog=eog, preload=True)
    raw2_file = op.join(tempdir, "test2-raw.fif")
    raw2.save(raw2_file, overwrite=True)
    data2, times2 = raw2[:, :]
    assert_array_equal(data1, data2)
    assert_array_equal(times1, times2)

    raw1 = Raw(raw1_file, preload=True)
    raw2 = Raw(raw2_file, preload=True)
    assert_array_equal(raw1._data, raw2._data)

    # save with buffer size smaller than file
    raw3_file = op.join(tempdir, "test3-raw.fif")
    raw3 = read_raw_brainvision(vhdr_path, eog=eog)
    raw3.save(raw3_file, buffer_size_sec=2)
    raw3 = Raw(raw3_file, preload=True)
    assert_array_equal(raw3._data, raw1._data)

    # add reference channel
    raw4_file = op.join(tempdir, "test4-raw.fif")
    raw4 = read_raw_brainvision(vhdr_path, eog=eog, reference="A1")
    raw4.save(raw4_file, buffer_size_sec=2)
    raw4 = Raw(raw4_file, preload=True)
    ref_idx = raw4.ch_names.index("A1")
    assert_equal(len(raw4._data), len(raw1._data) + 1)
    ref_data, _ = raw4[ref_idx]
    assert_array_equal(ref_data, 0)
Exemplo n.º 14
0
def test_orig_units(recwarn):
    """Test exposure of original channel units."""
    raw = read_raw_brainvision(vhdr_path)
    orig_units = raw._orig_units
    assert len(orig_units) == 32
    assert orig_units['FP1'] == u'µV'
    assert orig_units['CP5'] == 'n/a'  # originally BS, not a valid unit
    assert orig_units['CP6'] == u'µS'
    assert orig_units['HL'] == 'n/a'  # originally ARU, not a valid unit
    assert orig_units['HR'] == 'n/a'  # originally uS ...
    assert orig_units['Vb'] == 'S'
    assert orig_units['ReRef'] == 'C'
Exemplo n.º 15
0
def test_fif_dig_montage():
    """Test FIF dig montage support."""
    dig_montage = read_dig_montage(fif=fif_dig_montage_fname)

    # test round-trip IO
    temp_dir = _TempDir()
    fname_temp = op.join(temp_dir, 'test.fif')
    _check_roundtrip(dig_montage, fname_temp)

    # Make a BrainVision file like the one the user would have had
    with warnings.catch_warnings(record=True) as w:
        raw_bv = read_raw_brainvision(bv_fname, preload=True)
    assert_true(any('will be dropped' in str(ww.message) for ww in w))
    raw_bv_2 = raw_bv.copy()
    mapping = dict()
    for ii, ch_name in enumerate(raw_bv.ch_names[:-1]):
        mapping[ch_name] = 'EEG%03d' % (ii + 1,)
    raw_bv.rename_channels(mapping)
    for ii, ch_name in enumerate(raw_bv_2.ch_names[:-1]):
        mapping[ch_name] = 'EEG%03d' % (ii + 33,)
    raw_bv_2.rename_channels(mapping)
    raw_bv.drop_channels(['STI 014'])
    raw_bv.add_channels([raw_bv_2])

    for ii in range(2):
        if ii == 1:
            dig_montage.transform_to_head()  # should have no meaningful effect

        # Set the montage
        raw_bv.set_montage(dig_montage)

        # Check the result
        evoked = read_evokeds(evoked_fname)[0]

        assert_equal(len(raw_bv.ch_names), len(evoked.ch_names))
        for ch_py, ch_c in zip(raw_bv.info['chs'], evoked.info['chs']):
            assert_equal(ch_py['ch_name'],
                         ch_c['ch_name'].replace('EEG ', 'EEG'))
            # C actually says it's unknown, but it's not (?):
            # assert_equal(ch_py['coord_frame'], ch_c['coord_frame'])
            assert_equal(ch_py['coord_frame'], FIFF.FIFFV_COORD_HEAD)
            c_loc = ch_c['loc'].copy()
            c_loc[c_loc == 0] = np.nan
            assert_allclose(ch_py['loc'], c_loc, atol=1e-7)
        assert_dig_allclose(raw_bv.info, evoked.info)

    # Roundtrip of non-FIF start
    names = ['nasion', 'lpa', 'rpa', '1', '2', '3', '4', '5']
    montage = read_dig_montage(hsp, hpi, elp, names, transform=False)
    assert_raises(RuntimeError, montage.save, fname_temp)  # must be head coord
    montage = read_dig_montage(hsp, hpi, elp, names)
    _check_roundtrip(montage, fname_temp)
Exemplo n.º 16
0
def test_ascii():
    """Test ASCII BV reading."""
    raw = read_raw_brainvision(vhdr_path, event_id=event_id)
    tempdir = _TempDir()
    ascii_vhdr_path = op.join(tempdir, op.split(vhdr_path)[-1])
    # copy marker file
    shutil.copy(vhdr_path.replace('.vhdr', '.vmrk'),
                ascii_vhdr_path.replace('.vhdr', '.vmrk'))
    # modify header file
    skipping = False
    with open(ascii_vhdr_path, 'wb') as fout:
        with open(vhdr_path, 'rb') as fin:
            for line in fin:
                # Common Infos section
                if line.startswith(b'DataFormat'):
                    line = b'DataFormat=ASCII\n'
                elif line.startswith(b'DataFile='):
                    line = b'DataFile=test.dat\n'
                # Replace the "'Binary Infos'" section
                elif line.startswith(b'[Binary Infos]'):
                    skipping = True
                    fout.write(b'[ASCII Infos]\nDecimalSymbol=.\nSkipLines=1\n'
                               b'SkipColumns=0\n\n')
                elif skipping and line.startswith(b'['):
                    skipping = False
                if not skipping:
                    fout.write(line)
    # create the .dat file
    data, times = raw[:]
    with open(ascii_vhdr_path.replace('.vhdr', '.dat'), 'wb') as fid:
        fid.write(b' '.join(ch_name.encode('ASCII')
                  for ch_name in raw.ch_names) + b'\n')
        fid.write(b'\n'.join(b' '.join(b'%.3f' % dd for dd in d)
                  for d in data[:-1].T / raw._cals[:-1]))
    raw = read_raw_brainvision(ascii_vhdr_path, event_id=event_id)
    data_new, times_new = raw[:]
    assert_allclose(data_new, data, atol=1e-15)
    assert_allclose(times_new, times)
Exemplo n.º 17
0
def test_events():
    """Test reading and modifying events"""
    tempdir = _TempDir()
    raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True)

    # check that events are read and stim channel is synthesized correcly
    events = raw.get_brainvision_events()
    assert_array_equal(
        events,
        [
            [487, 1, 253],
            [497, 1, 255],
            [1770, 1, 254],
            [1780, 1, 255],
            [3253, 1, 254],
            [3263, 1, 255],
            [4936, 1, 253],
            [4946, 1, 255],
            [6620, 1, 254],
            [6630, 1, 255],
        ],
    )

    mne_events = mne.find_events(raw, stim_channel="STI 014")
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw.set_brainvision_events(events)
    mne_events = mne.find_events(raw, stim_channel="STI 014")
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info["nchan"]
    ch_name = raw.info["chs"][-2]["ch_name"]
    events = np.empty((0, 3))
    raw.set_brainvision_events(events)
    assert_equal(raw.info["nchan"], nchan - 1)
    assert_equal(len(raw._data), nchan - 1)
    assert_equal(raw.info["chs"][-1]["ch_name"], ch_name)
    fname = op.join(tempdir, "evt_raw.fif")
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw.set_brainvision_events(events)
    assert_equal(raw.info["nchan"], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info["chs"][-1]["ch_name"], "STI 014")
Exemplo n.º 18
0
def test_brainvision_vectorized_data():
    """Test reading BrainVision data files with vectorized data."""
    with pytest.warns(RuntimeWarning, match='software filter'):
        raw = read_raw_brainvision(vhdr_old_path, preload=True)

    assert_array_equal(raw._data.shape, (29, 251))

    first_two_samples_all_chs = np.array([[+5.22000008e-06, +5.10000000e-06],
                                          [+2.10000000e-06, +2.27000008e-06],
                                          [+1.15000000e-06, +1.33000002e-06],
                                          [+4.00000000e-07, +4.00000000e-07],
                                          [-3.02999992e-06, -2.82000008e-06],
                                          [+2.71000004e-06, +2.45000000e-06],
                                          [+2.41000004e-06, +2.36000004e-06],
                                          [+1.01999998e-06, +1.18000002e-06],
                                          [-1.33999996e-06, -1.25000000e-06],
                                          [-2.60000000e-06, -2.46000004e-06],
                                          [+6.80000019e-07, +8.00000000e-07],
                                          [+1.48000002e-06, +1.48999996e-06],
                                          [+1.61000004e-06, +1.51000004e-06],
                                          [+7.19999981e-07, +8.60000038e-07],
                                          [-3.00000000e-07, -4.00000006e-08],
                                          [-1.20000005e-07, +6.00000024e-08],
                                          [+8.19999981e-07, +9.89999962e-07],
                                          [+1.13000002e-06, +1.28000002e-06],
                                          [+1.08000002e-06, +1.33999996e-06],
                                          [+2.20000005e-07, +5.69999981e-07],
                                          [-4.09999990e-07, +4.00000006e-08],
                                          [+5.19999981e-07, +9.39999962e-07],
                                          [+1.01000004e-06, +1.51999998e-06],
                                          [+1.01000004e-06, +1.55000000e-06],
                                          [-1.43000002e-06, -1.13999996e-06],
                                          [+3.65000000e-06, +3.65999985e-06],
                                          [+4.15999985e-06, +3.79000015e-06],
                                          [+9.26999969e-06, +8.95999985e-06],
                                          [-7.35999985e-06, -7.18000031e-06],
                                          ])

    assert_array_almost_equal(raw._data[:, :2], first_two_samples_all_chs)
Exemplo n.º 19
0
def test_brainvision_neuroone_export():
    """Test Brainvision file exported with neuroone system."""
    raw = read_raw_brainvision(neuroone_vhdr, verbose='error')
    assert raw.info['meas_date'] is None
    assert len(raw.info['chs']) == 65
    assert raw.info['sfreq'] == 5000.
Exemplo n.º 20
0
def test_brainvision_data():
    """Test reading raw Brain Vision files."""
    pytest.raises(IOError, read_raw_brainvision, vmrk_path)
    pytest.raises(ValueError,
                  read_raw_brainvision,
                  vhdr_path,
                  montage,
                  preload=True,
                  scale="foo")

    raw_py = _test_raw_reader(read_raw_brainvision,
                              vhdr_fname=vhdr_path,
                              montage=montage,
                              eog=eog,
                              misc='auto',
                              event_id=event_id)

    assert ('RawBrainVision' in repr(raw_py))

    assert_equal(raw_py.info['highpass'], 0.)
    assert_equal(raw_py.info['lowpass'], 250.)

    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_py, times_py = raw_py[picks]

    # compare with a file that was generated using MNE-C
    raw_bin = read_raw_fif(eeg_bin, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_bin, times_bin = raw_bin[picks]

    assert_array_almost_equal(data_py, data_bin)
    assert_array_almost_equal(times_py, times_bin)

    # Make sure EOG channels are marked correctly
    for ch in raw_py.info['chs']:
        if ch['ch_name'] in eog:
            assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH)
        elif ch['ch_name'] == 'STI 014':
            assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH)
        elif ch['ch_name'] in ('CP5', 'CP6'):
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_NONE)
        elif ch['ch_name'] == 'ReRef':
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_CEL)
        elif ch['ch_name'] in raw_py.info['ch_names']:
            assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_V)
        else:
            raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])

    # test loading v2
    read_raw_brainvision(vhdr_v2_path,
                         eog=eog,
                         preload=True,
                         event_id=event_id,
                         trig_shift_by_type={'response': 1000},
                         verbose='error')
    # For the nanovolt unit test we use the same data file with a different
    # header file.
    raw_nV = _test_raw_reader(read_raw_brainvision,
                              vhdr_fname=vhdr_nV_path,
                              montage=montage,
                              eog=eog,
                              misc='auto',
                              event_id=event_id)
    assert_equal(raw_nV.info['chs'][0]['ch_name'], 'FP1')
    assert_equal(raw_nV.info['chs'][0]['kind'], FIFF.FIFFV_EEG_CH)
    data_nanovolt, _ = raw_nV[0]
    assert_array_almost_equal(data_py[0, :], data_nanovolt[0, :])
Exemplo n.º 21
0
def test_read_vhdr_annotations_and_events():
    """Test load brainvision annotations and parse them to events."""
    sfreq = 1000.0
    expected_orig_time = 1384359243.794231
    expected_onset_latency = np.array([
        0, 486., 496., 1769., 1779., 3252., 3262., 4935., 4945., 5999., 6619.,
        6629., 7629., 7699.
    ])
    expected_annot_description = [
        'New Segment/', 'Stimulus/S253', 'Stimulus/S255', 'Event/254',
        'Stimulus/S255', 'Event/254', 'Stimulus/S255', 'Stimulus/S253',
        'Stimulus/S255', 'Response/R255', 'Event/254', 'Stimulus/S255',
        'SyncStatus/Sync On', 'Optic/O  1'
    ]
    expected_events = np.stack([
        expected_onset_latency,
        np.zeros_like(expected_onset_latency),
        [
            99999, 253, 255, 254, 255, 254, 255, 253, 255, 1255, 254, 255,
            99998, 2001
        ],
    ]).astype('int64').T
    expected_event_id = {
        'New Segment/': 99999,
        'Stimulus/S253': 253,
        'Stimulus/S255': 255,
        'Event/254': 254,
        'Response/R255': 1255,
        'SyncStatus/Sync On': 99998,
        'Optic/O  1': 2001
    }

    raw = read_raw_brainvision(vhdr_path, eog=eog)

    # validate annotations
    assert raw.annotations.orig_time == expected_orig_time
    assert_allclose(raw.annotations.onset, expected_onset_latency / sfreq)
    assert_array_equal(raw.annotations.description, expected_annot_description)

    # validate event extraction
    events, event_id = events_from_annotations(raw)
    assert_array_equal(events, expected_events)
    assert event_id == expected_event_id

    # validate that None gives us a sorted list
    expected_none_event_id = {
        desc: idx + 1
        for idx, desc in enumerate(sorted(event_id.keys()))
    }
    events, event_id = events_from_annotations(raw, event_id=None)
    assert event_id == expected_none_event_id

    # Add some custom ones, plus a 2-digit one
    s_10 = 'Stimulus/S 10'
    raw.annotations.append([1, 2, 3], 10, ['ZZZ', s_10, 'YYY'])
    expected_event_id.update(YYY=10001, ZZZ=10002)  # others starting at 10001
    expected_event_id[s_10] = 10
    _, event_id = events_from_annotations(raw)
    assert event_id == expected_event_id

    # Concatenating two shouldn't change the resulting event_id
    # (BAD and EDGE should be ignored)
    with pytest.warns(RuntimeWarning, match='expanding outside'):
        raw_concat = concatenate_raws([raw.copy(), raw.copy()])
    _, event_id = events_from_annotations(raw_concat)
    assert event_id == expected_event_id
Exemplo n.º 22
0
def read_brainvision(fname, apply_montage=True, preload=False):
    """Load brainvision data. If apply_montage=True, load and apply the standard
    montage for the 64-channel acticap. If add_ref=True add a reference
    channel with all zeros"""

    raw = read_raw_brainvision(fname, preload=preload)
    if apply_montage:
        mapping = {
            "1": "Fp1",
            "2": "Fp2",
            "3": "F7",
            "4": "F3",
            "5": "Fz",
            "6": "F4",
            "7": "F8",
            "8": "FC5",
            "9": "FC1",
            "10": "FC2",
            "11": "FC6",
            "12": "T7",
            "13": "C3",
            "14": "Cz",
            "15": "C4",
            "16": "T8",
            "17": "TP9",
            "18": "CP5",
            "19": "CP1",
            "20": "CP2",
            "21": "CP6",
            "22": "TP10",
            "23": "P7",
            "24": "P3",
            "25": "Pz",
            "26": "P4",
            "27": "P8",
            "28": "PO9",
            "29": "O1",
            "30": "Oz",
            "31": "O2",
            "32": "PO10",
            "33": "AF7",
            "34": "AF3",
            "35": "AF4",
            "36": "AF8",
            "37": "F5",
            "38": "F1",
            "39": "F2",
            "40": "F6",
            "41": "FT9",
            "42": "FT7",
            "43": "FC3",
            "44": "FC4",
            "45": "FT8",
            "46": "FT10",
            "47": "C5",
            "48": "C1",
            "49": "C2",
            "50": "C6",
            "51": "TP7",
            "52": "CP3",
            "53": "CPz",
            "54": "CP4",
            "55": "TP8",
            "56": "P5",
            "57": "P1",
            "58": "P2",
            "59": "P6",
            "60": "PO7",
            "61": "PO3",
            "62": "POz",
            "63": "PO4",
            "64": "PO8"
        }
        raw.rename_channels(mapping)
        montage = read_custom_montage(
            Path(os.environ["EXPDIR"]) / Path("AS-96_REF.bvef"))
        raw.set_montage(montage)
    return raw
Exemplo n.º 23
0
def test_events():
    """Test reading and modifying events"""
    tempdir = _TempDir()

    # check that events are read and stim channel is synthesized correcly
    raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True)
    events = raw.get_brainvision_events()
    assert_array_equal(
        events,
        [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
         [3253, 1, 254], [3263, 1, 255], [4936, 1, 253], [4946, 1, 255],
         [6000, 1, 255], [6620, 1, 254], [6630, 1, 255]])

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are shifted like they're supposed to be.
    raw = read_raw_brainvision(vhdr_path,
                               eog=eog,
                               preload=True,
                               response_trig_shift=1000)
    events = raw.get_brainvision_events()
    assert_array_equal(
        events,
        [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
         [3253, 1, 254], [3263, 1, 255], [4936, 1, 253], [4946, 1, 255],
         [6000, 1, 1255], [6620, 1, 254], [6630, 1, 255]])

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are ignored.
    raw = read_raw_brainvision(vhdr_path,
                               eog=eog,
                               preload=True,
                               response_trig_shift=None)
    events = raw.get_brainvision_events()
    assert_array_equal(
        events, [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
                 [3253, 1, 254], [3263, 1, 255], [4936, 1, 253],
                 [4946, 1, 255], [6620, 1, 254], [6630, 1, 255]])

    assert_raises(TypeError,
                  read_raw_brainvision,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  response_trig_shift=0.1)
    assert_raises(TypeError,
                  read_raw_brainvision,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  response_trig_shift=np.nan)

    mne_events = mne.find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw.set_brainvision_events(events)
    mne_events = mne.find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw.set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan - 1)
    assert_equal(len(raw._data), nchan - 1)
    assert_equal(raw.info['chs'][-1]['ch_name'], ch_name)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw.set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')
Exemplo n.º 24
0
def test_brainvision_data():
    """Test reading raw Brain Vision files."""
    pytest.raises(IOError, read_raw_brainvision, vmrk_path)
    pytest.raises(ValueError, read_raw_brainvision, vhdr_path,
                  preload=True, scale="foo")

    raw_py = _test_raw_reader(
        read_raw_brainvision, vhdr_fname=vhdr_path, eog=eog, misc='auto'
    )

    assert ('RawBrainVision' in repr(raw_py))

    assert_equal(raw_py.info['highpass'], 0.)
    assert_equal(raw_py.info['lowpass'], 250.)

    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_py, times_py = raw_py[picks]

    # compare with a file that was generated using MNE-C
    raw_bin = read_raw_fif(eeg_bin, preload=True)
    picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads')
    data_bin, times_bin = raw_bin[picks]

    assert_array_almost_equal(data_py, data_bin)
    assert_array_almost_equal(times_py, times_bin)

    # Make sure EOG channels are marked correctly
    for ch in raw_py.info['chs']:
        if ch['ch_name'] in eog:
            assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH)
        elif ch['ch_name'] == 'STI 014':
            assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH)
        elif ch['ch_name'] in ('CP5', 'CP6'):
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_NONE)
        elif ch['ch_name'] == 'ReRef':
            assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_CEL)
        elif ch['ch_name'] in raw_py.info['ch_names']:
            assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH)
            assert_equal(ch['unit'], FIFF.FIFF_UNIT_V)
        else:
            raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])

    # test loading v2
    read_raw_brainvision(vhdr_v2_path, eog=eog, preload=True,
                         verbose='error')
    # test different units with alternative header file
    raw_units = _test_raw_reader(
        read_raw_brainvision, vhdr_fname=vhdr_units_path, eog=eog, misc='auto'
    )
    assert_equal(raw_units.info['chs'][0]['ch_name'], 'FP1')
    assert_equal(raw_units.info['chs'][0]['kind'], FIFF.FIFFV_EEG_CH)
    data_units, _ = raw_units[0]
    assert_array_almost_equal(data_py[0, :], data_units.squeeze())

    assert_equal(raw_units.info['chs'][1]['ch_name'], 'FP2')
    assert_equal(raw_units.info['chs'][1]['kind'], FIFF.FIFFV_EEG_CH)
    data_units, _ = raw_units[1]
    assert_array_almost_equal(data_py[1, :], data_units.squeeze())

    assert_equal(raw_units.info['chs'][2]['ch_name'], 'F3')
    assert_equal(raw_units.info['chs'][2]['kind'], FIFF.FIFFV_EEG_CH)
    data_units, _ = raw_units[2]
    assert_array_almost_equal(data_py[2, :], data_units.squeeze())
Exemplo n.º 25
0
def test_events():
    """Test reading and modifying events"""
    tempdir = _TempDir()

    # check that events are read and stim channel is synthesized correcly
    raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True)
    events = raw.get_brainvision_events()
    assert_array_equal(events, [[487, 1, 253],
                                [497, 1, 255],
                                [1770, 1, 254],
                                [1780, 1, 255],
                                [3253, 1, 254],
                                [3263, 1, 255],
                                [4936, 1, 253],
                                [4946, 1, 255],
                                [6000, 1, 255],
                                [6620, 1, 254],
                                [6630, 1, 255]])

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are shifted like they're supposed to be.
    raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True,
                               response_trig_shift=1000)
    events = raw.get_brainvision_events()
    assert_array_equal(events, [[487, 1, 253],
                                [497, 1, 255],
                                [1770, 1, 254],
                                [1780, 1, 255],
                                [3253, 1, 254],
                                [3263, 1, 255],
                                [4936, 1, 253],
                                [4946, 1, 255],
                                [6000, 1, 1255],
                                [6620, 1, 254],
                                [6630, 1, 255]])

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are ignored.
    raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True,
                               response_trig_shift=None)
    events = raw.get_brainvision_events()
    assert_array_equal(events, [[487, 1, 253],
                                [497, 1, 255],
                                [1770, 1, 254],
                                [1780, 1, 255],
                                [3253, 1, 254],
                                [3263, 1, 255],
                                [4936, 1, 253],
                                [4946, 1, 255],
                                [6620, 1, 254],
                                [6630, 1, 255]])

    assert_raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, response_trig_shift=0.1)
    assert_raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, response_trig_shift=np.nan)

    mne_events = mne.find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw.set_brainvision_events(events)
    mne_events = mne.find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw.set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan - 1)
    assert_equal(len(raw._data), nchan - 1)
    assert_equal(raw.info['chs'][-1]['ch_name'], ch_name)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw.set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')
Exemplo n.º 26
0
def test_fetch_brainvision_testing_data():
    """Test downloading of BrainVision testing data (~500kB)."""
    data_path = fetch_brainvision_testing_data()
    raw = read_raw_brainvision(op.join(data_path, 'test.vhdr'))
    assert raw
Exemplo n.º 27
0
def test_brainvision_neuroone_export():
    """Test Brainvision file exported with neuroone system."""
    raw = read_raw_brainvision(neuroone_vhdr, verbose='error')
    assert raw.info['meas_date'] is None
    assert len(raw.info['chs']) == 65
    assert raw.info['sfreq'] == 5000.
Exemplo n.º 28
0
def test_parse_impedance():
    """Test case for parsing the impedances from header."""
    expected_imp_meas_time = datetime.datetime(2013,
                                               11,
                                               13,
                                               16,
                                               12,
                                               27,
                                               tzinfo=datetime.timezone.utc)
    expected_imp_unit = 'kOhm'
    expected_electrodes = [
        'FP1', 'FP2', 'F3', 'F4', 'C3', 'C4', 'P3', 'P4', 'O1', 'O2', 'F7',
        'F8', 'P7', 'P8', 'Fz', 'FCz', 'Cz', 'CPz', 'Pz', 'POz', 'FC1', 'FC2',
        'CP1', 'CP2', 'FC5', 'FC6', 'CP5', 'CP6', 'HL', 'HR', 'Vb', 'ReRef',
        'Ref', 'Gnd'
    ]
    n_electrodes = len(expected_electrodes)
    expected_imps = [np.nan] * (n_electrodes - 2) + [0., 4.]
    expected_imp_lower_bound = 0.
    expected_imp_upper_bound = [100.] * (n_electrodes - 2) + [10., 10.]

    expected_impedances = {
        elec: {
            'imp': expected_imps[i],
            'imp_unit': expected_imp_unit,
            'imp_meas_time': expected_imp_meas_time,
            'imp_lower_bound': expected_imp_lower_bound,
            'imp_upper_bound': expected_imp_upper_bound[i],
            'imp_range_unit': expected_imp_unit,
        }
        for i, elec in enumerate(expected_electrodes)
    }

    raw = read_raw_brainvision(vhdr_path, eog=eog)
    assert object_diff(expected_impedances, raw.impedances) == ''

    # Test "Impedances Imported from actiCAP Control Software"
    expected_imp_meas_time = expected_imp_meas_time.replace(hour=10,
                                                            minute=17,
                                                            second=2)
    tmpidx = expected_electrodes.index('CP6')
    expected_electrodes = expected_electrodes[:tmpidx] + [
        'CP 6', 'ECG+', 'ECG-', 'HEOG+', 'HEOG-', 'VEOG+', 'VEOG-', 'ReRef',
        'Ref', 'Gnd'
    ]
    n_electrodes = len(expected_electrodes)
    expected_imps = [np.nan] * (n_electrodes - 9) + [
        35., 46., 6., 8., 3., 4., 0., 8., 2.5
    ]
    expected_impedances = {
        elec: {
            'imp': expected_imps[i],
            'imp_unit': expected_imp_unit,
            'imp_meas_time': expected_imp_meas_time,
        }
        for i, elec in enumerate(expected_electrodes)
    }

    with pytest.warns(RuntimeWarning, match='different .*pass filters'):
        raw = read_raw_brainvision(vhdr_mixed_lowpass_path,
                                   eog=['HEOG', 'VEOG'],
                                   misc=['ECG'])
    assert object_diff(expected_impedances, raw.impedances) == ''
Exemplo n.º 29
0
def bv2fif(dataf,
           corf,
           ch_order=None,
           eogs=('VEOG', 'HEOG'),
           ecg='ECG',
           emg='EMG',
           preload='default',
           ref_ch='Fp1',
           dbs=False,
           new_sfreq=1000.0):
    montage = read_dig_montage(bvct=corf)
    if preload == 'default':
        preload = os.path.dirname(dataf) + '/workfile'

    raw = read_raw_brainvision(dataf, preload=preload)

    if dbs:
        event_ch = get_events(raw)

    # save downsampled raw for multitaper spectrogram
    raw_data = np.zeros(
        (raw._data.shape[0],
         int(raw._data.shape[1] / raw.info['sfreq'] * new_sfreq)))
    raw_info = raw.info.copy()
    raw_info['sfreq'] = new_sfreq
    for i in tqdm(range(len(raw._data))):
        ch = raw._data[i, ::int(raw.info['sfreq'] / new_sfreq)]
        raw_data[i] = ch
        del ch

    raw_resampled = RawArray(raw_data, raw_info)
    raw_resampled.annotations = raw.annotations

    if dbs:
        old_event_ch = [ch for ch in raw.info['ch_names'] if 'STI' in ch]
        if old_event_ch:
            raw_resampled.drop_channels([old_event_ch[0]])
        event_ch._data = event_ch._data[:, ::int(raw.info['sfreq'] /
                                                 new_sfreq)]
        event_ch.info['sfreq'] = new_sfreq
        event_ch.__len__ = len(event_ch._data[0])
        event_ch.info['lowpass'] = raw_resampled.info['lowpass']
        raw_resampled.add_channels([event_ch])

    prepInst(raw_resampled, dataf, 'raw', montage, ref_ch, eogs, ecg, emg)

    events, event_ids = events_from_annotations(raw)
    if len(np.unique(events[:, 2])) > 1:
        events = events[np.where(events[:,
                                        2] == events[1,
                                                     2])[0]]  #skip new segment
    epochs = Epochs(raw,
                    events,
                    tmin=-2,
                    tmax=2,
                    proj=False,
                    preload=op.dirname(dataf) + '/workfile-epo',
                    baseline=(-0.5, -0.1),
                    verbose=False,
                    detrend=1)
    events = events[
        epochs.
        selection]  #in case any epochs don't have data and get thrown out (poorly placed at beginning or end)
    epo_data = np.zeros(
        (epochs._data.shape[0], epochs._data.shape[1],
         int(np.ceil(epochs._data.shape[2] / epochs.info['sfreq'] *
                     new_sfreq))))
    for i in tqdm(range(epochs._data.shape[0])):
        for j in range(epochs._data.shape[1]):
            epo_curr = epochs._data[i,
                                    j, ::int(epochs.info['sfreq'] / new_sfreq)]
            epo_data[i, j] = epo_curr
            del epo_curr
    events[:, 0] = np.array(events[:, 0] * new_sfreq / raw.info['sfreq'],
                            dtype=int)
    epo_resampled = EpochsArray(epo_data, epochs.info.copy(), events, tmin=-2)
    epo_resampled.info['sfreq'] = new_sfreq
    epo_resampled.events[:, 2] = np.arange(len(events))
    epo_resampled.event_id = {str(i): i for i in range(len(events))}

    prepInst(epo_resampled, dataf, 'epo', montage, ref_ch, eogs, ecg, emg)
Exemplo n.º 30
0
def mne_switch(file, ext, downsample, preload=True, **kwargs):
    """Read sleep datasets using mne.io.

    Parameters
    ----------
    file : string
        Filename (without extension).
    ext : string
        File extension (e.g. '.edf'').
    preload : bool | True
        Preload data in memory.
    kwargs : dict | {}
        Further arguments to pass to the mne.io.read function.

    Returns
    -------
    sf : float
        The original sampling-frequency.
    downsample : float
        The down-sampling frequency used.
    dsf : int
        The down-sampling factor.
    data : array_like
        The raw data of shape (n_channels, n_points)
    channels : list
        List of channel names.
    n : int
        Number of time points before down-sampling.
    start_time : datetime.time
        The time offset.
    """
    from mne import io

    # Get full path :
    path = file + ext

    # Preload :
    if preload is False:
        preload = 'temp.dat'
    kwargs['preload'] = preload

    if ext.lower() in ['.edf', '.bdf', '.gdf']:  # EDF / BDF / GDF
        raw = io.read_raw_edf(path, **kwargs)
    elif ext.lower == '.set':   # EEGLAB
        raw = io.read_raw_eeglab(path, **kwargs)
    elif ext.lower() in ['.egi', '.mff']:  # EGI / MFF
        raw = io.read_raw_egi(path, **kwargs)
    elif ext.lower() == '.cnt':  # CNT
        raw = io.read_raw_cnt(path, **kwargs)
    elif ext.lower() == '.vhdr':  # BrainVision
        raw = io.read_raw_brainvision(path, **kwargs)
    else:
        raise IOError("File not supported by mne-python.")

    raw.pick_types(meg=True, eeg=True, ecg=True, emg=True)  # Remove stim lines
    sf = raw.info['sfreq']
    dsf, downsample = get_dsf(downsample, sf)
    channels = raw.info['ch_names']
    data = raw._data
    n = data.shape[1]
    start_time = datetime.time(0, 0, 0)  # raw.info['meas_date']
    anot = raw.annotations

    return sf, downsample, dsf, data[:, ::dsf], channels, n, start_time, anot
Exemplo n.º 31
0
    def save_setup(self):
        de = self.data_entries.copy()
        raw_fname = de.pop('Load New File').get()
        loc_fname = de.pop('Load Location File').get()
        suffix = raw_fname.split('.')[-1]
        if suffix == 'fif':
            try:
                raw = Raw(raw_fname,preload=False)
            except Exception as e:
                print(e)
                messagebox.showerror('Error','Failed to read fif')
                return
            if loc_fname:
                messagebox.showwarning('Warning','Ignoring location file given')
        elif suffix == 'vhdr':
            if not loc_fname:
               messagebox.showerror('Error','Location file not specified')
            try:
                raw = read_raw_brainvision(raw_fname,
                    preload=op.join(self.sub_dirs['tmp'],'raw_tmp'))
            except Exception as e:
                print(e)
                messagebox.showerror('Error','Failed to read BrainVision raw file')
                return
            try:
                montage = read_dig_montage(bvct=loc_fname)
                ref_name = [ch for ch in montage.dig_ch_pos if not
                            ch in raw.info['ch_names']]
                if len(ref_name) != 1:
                    ref_name = simpledialog.askstring('Reference Channel',
                                                      'What is the reference channel name?',
                                                      parent=self.root)
                else:
                    ref_name = ref_name[0]
                info = create_info([ref_name], raw.info['sfreq'], ['eeg'])
                ref = RawArray(np.zeros((1, len(raw.times))), info)
                ref.info['lowpass'] = raw.info['lowpass']
                raw = raw.add_channels([ref]) #, force_update_info=True)
                #
                for ch_type in ['eog','ecg','emg','stim']:
                    chs = [ch for ch in raw.info['ch_names']
                           if ch_type in ch.lower()]
                    for ch in chs:
                        ch_ix = raw.ch_names.index(ch)
                        raw._data[ch_ix, :] *= 1e-6
                        raw.set_channel_types({ch:ch_type})
                raw = raw.set_montage(montage,verbose=False)
            except Exception as e:
                print(e)
                messagebox.showerror('Error','Failed to read BrainVision location file')
                return
        else:
            messagebox.showerror('Error','Unrecognized raw file format')
            return
        self.save_exp(raw)
        transf = self.data_entries['Coord Trans fname'].get() 
        if not op.isfile(transf):
            subject = self.data_entries['Subject'].get()
            os.environ['SUBJECT'] = subject
            fs_dir = self.data_entries['FS Dir'].get()
            os.environ['SUBJECTS_DIR'] = fs_dir
            messagebox.showinfo(
                'Coregistration','In this next interactive GUI, will need to\n' +
                '1. Load the pial surface file -> Load Surface -> Select Pial Surface\n' +
                '2. Load the subject\'s digitization data: File -> Load digitizer data ->' +
                ' Select the raw data file for this session\n' + 
                '3. Open the coordinate alignment window: Adjust -> Coordinate Alignment\n' + 
                '4. Open the viewer window: View -> Show viewer\n' +
                '5. In the coordinate alignment window, click RAP, LAP and Naision, ' + 
                'and then after clicking each of those click on the corresponing ' +
                'fiducial points on the reconstructed head model\n' +
                '6. Click align using fiducials\n' +
                '7. In the View window: select Options -> Show digitizer data\n'
                '8. Adjust the x, y and z coordinates and rotation until ' +
                'the alignment is as close to the ground truth as possible\n' +
                'If you don\'t know these instructions, feel free to copy and paste')

            self.output_to_terminal('mne_analyze')
Exemplo n.º 32
0
def test_events():
    """Test reading and modifying events."""
    tempdir = _TempDir()
    # Note: BrainVision event offsets are 1-based, mne offsets are 0-based.
    # So in all tests below, the "onset" is 1 less than what's in the file

    # check that events are read and stim channel is synthesized correctly
    raw = read_raw_brainvision(vhdr_path, eog=eog, event_id=event_id)
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(events, [[486, 0, 253],
                                [496, 1, 255],
                                [1769, 1, 254],
                                [1779, 1, 255],
                                [3252, 1, 254],
                                [3262, 1, 255],
                                [4935, 1, 253],
                                [4945, 1, 255],
                                [5999, 1, 255],
                                [6619, 1, 254],
                                [6629, 1, 255],
                                [7699, 1, 1]])

    # check that events are read and stim channel is synthesized correctly and
    # response triggers are shifted using the deprecated response_trig_shift.
    with pytest.warns(DeprecationWarning):
        raw = read_raw_brainvision(vhdr_path, eog=eog,
                                   response_trig_shift=1000, event_id=event_id)
    events = raw._get_brainvision_events()
    assert_array_equal(events, [[486, 0, 253],
                                [496, 1, 255],
                                [1769, 1, 254],
                                [1779, 1, 255],
                                [3252, 1, 254],
                                [3262, 1, 255],
                                [4935, 1, 253],
                                [4945, 1, 255],
                                [5999, 1, 1255],
                                [6619, 1, 254],
                                [6629, 1, 255],
                                [7629, 1, 5],
                                [7699, 1, 1]])

    # check that trig_shift_by_type works as well
    raw = read_raw_brainvision(vhdr_path, eog=eog,
                               trig_shift_by_type={'response': 1000,
                                                   'Optic': 2000},
                               event_id=event_id)
    events = raw._get_brainvision_events()
    assert_array_equal(events, [[486, 0, 253],
                                [496, 1, 255],
                                [1769, 1, 254],
                                [1779, 1, 255],
                                [3252, 1, 254],
                                [3262, 1, 255],
                                [4935, 1, 253],
                                [4945, 1, 255],
                                [5999, 1, 1255],
                                [6619, 1, 254],
                                [6629, 1, 255],
                                [7629, 1, 5],
                                [7699, 1, 2001]])

    # Check that we warn if a trigger is dropped
    with pytest.warns(RuntimeWarning, match='to parse triggers'):
        raw = read_raw_brainvision(vhdr_path)
    # check that events are read and stim channel is synthesized correctly and
    # response triggers are ignored.
    raw = read_raw_brainvision(vhdr_path, eog=eog, event_id=event_id,
                               trig_shift_by_type={'response': None})
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(events, [[486, 0, 253],
                                [496, 1, 255],
                                [1769, 1, 254],
                                [1779, 1, 255],
                                [3252, 1, 254],
                                [3262, 1, 255],
                                [4935, 1, 253],
                                [4945, 1, 255],
                                [6619, 1, 254],
                                [6629, 1, 255],
                                [7699, 1, 1]])

    # Error handling of trig_shift_by_type
    pytest.raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, trig_shift_by_type=1)
    pytest.raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, trig_shift_by_type={'response': 0.1})
    pytest.raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, trig_shift_by_type={'response': np.nan})
    pytest.raises(ValueError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, trig_shift_by_type={'response': 1000,
                                                    'Response': 1001})
    with pytest.warns(DeprecationWarning):
        pytest.raises(ValueError, read_raw_brainvision, vhdr_path, eog=eog,
                      preload=True, trig_shift_by_type={'response': 1000},
                      response_trig_shift=1001)

    # Check that events of type "Comment" are read if they contain square
    # brackets (which usually signify a new section within a BrainVision file)
    # If no event_id specified, skip the marker and continue as planned
    with pytest.warns(RuntimeWarning, match='channel types to misc'):
        raw = read_raw_brainvision(vhdr_v2_path)
    events = raw._get_brainvision_events()
    assert events.shape == (11, 3)  # shape of events without comment

    # with event_id specified, get that comment and assert it's there
    tmp_event_id = {'comment using [square] brackets': 999}
    with pytest.warns(RuntimeWarning, match='channel types to misc'):
        raw = read_raw_brainvision(vhdr_v2_path, event_id=tmp_event_id)
    events = raw._get_brainvision_events()
    assert 999 in events[:, -1]
    assert events.shape == (12, 3)  # shape of events with comment

    # check that events are read properly when event_id is specified for
    # auxiliary events
    raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True,
                               trig_shift_by_type={'response': None},
                               event_id=event_id)
    events = raw._get_brainvision_events()
    assert_array_equal(events, [[486, 0, 253],
                                [496, 1, 255],
                                [1769, 1, 254],
                                [1779, 1, 255],
                                [3252, 1, 254],
                                [3262, 1, 255],
                                [4935, 1, 253],
                                [4945, 1, 255],
                                [6619, 1, 254],
                                [6629, 1, 255],
                                [7629, 1, 5],
                                [7699, 1, 1]])

    # to handle the min duration = 1 of stim trig (re)construction ...
    events = np.array([[486, 1, 253],
                       [496, 1, 255],
                       [1769, 1, 254],
                       [1779, 1, 255],
                       [3252, 1, 254],
                       [3262, 1, 255],
                       [4935, 1, 253],
                       [4945, 1, 255],
                       [6619, 1, 254],
                       [6629, 1, 255],
                       [7629, 1, 5],
                       [7699, 1, 1]])

    # Test that both trig_shift_by_type and event_id can be set
    read_raw_brainvision(vhdr_path, eog=eog, preload=False,
                         trig_shift_by_type={'response': 100},
                         event_id=event_id)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw._set_brainvision_events(events)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-2]['ch_name'], ch_name)
    assert_equal(len(find_events(raw, 'STI 014')), 0)
    assert_allclose(raw[-1][0], 0.)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')
Exemplo n.º 33
0
def test_read_dig_captrak(tmpdir):
    """Test reading a captrak montage file."""
    EXPECTED_CH_NAMES_OLD = [
        'AF3', 'AF4', 'AF7', 'AF8', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'CP1',
        'CP2', 'CP3', 'CP4', 'CP5', 'CP6', 'CPz', 'Cz', 'F1', 'F2', 'F3', 'F4',
        'F5', 'F6', 'F7', 'F8', 'FC1', 'FC2', 'FC3', 'FC4', 'FC5', 'FC6',
        'FT10', 'FT7', 'FT8', 'FT9', 'Fp1', 'Fp2', 'Fz', 'GND', 'O1', 'O2',
        'Oz', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'PO10', 'PO3',
        'PO4', 'PO7', 'PO8', 'PO9', 'POz', 'Pz', 'REF', 'T7', 'T8', 'TP10',
        'TP7', 'TP8', 'TP9'
    ]
    EXPECTED_CH_NAMES = [
        'T7', 'FC5', 'F7', 'C5', 'FT7', 'FT9', 'TP7', 'TP9', 'P7', 'CP5',
        'PO7', 'C3', 'CP3', 'P5', 'P3', 'PO3', 'PO9', 'O1', 'Oz', 'POz', 'O2',
        'PO4', 'P1', 'Pz', 'P2', 'CP2', 'CP1', 'CPz', 'Cz', 'C1', 'FC1', 'FC3',
        'REF', 'F3', 'F1', 'Fz', 'F5', 'AF7', 'AF3', 'Fp1', 'GND', 'F2', 'AF4',
        'Fp2', 'F4', 'F8', 'F6', 'AF8', 'FC2', 'FC6', 'FC4', 'C2', 'C4', 'P4',
        'CP4', 'PO8', 'P8', 'P6', 'CP6', 'PO10', 'TP10', 'TP8', 'FT10', 'T8',
        'C6', 'FT8'
    ]
    assert set(EXPECTED_CH_NAMES) == set(EXPECTED_CH_NAMES_OLD)
    montage = read_dig_captrak(
        fname=op.join(data_path, 'montage', 'captrak_coords.bvct')
    )

    # XXX: remove with 0.22 once captrCK is deprecated
    with pytest.warns(DeprecationWarning,
                      match='read_dig_captrack is deprecated'):
        montage2 = read_dig_captrack(
            fname=op.join(data_path, 'montage', 'captrak_coords.bvct')
        )
        assert repr(montage) == repr(montage2)

    assert montage.ch_names == EXPECTED_CH_NAMES
    assert repr(montage) == (
        '<DigMontage | '
        '0 extras (headshape), 0 HPIs, 3 fiducials, 66 channels>'
    )

    montage = transform_to_head(montage)  # transform_to_head has to be tested
    _check_roundtrip(montage=montage, fname=str(tmpdir.join('bvct_test.fif')))

    fid, _ = _get_fid_coords(montage.dig)
    assert_allclose(
        actual=np.array([fid.nasion, fid.lpa, fid.rpa]),
        desired=[[0, 0.11309, 0], [-0.09189, 0, 0], [0.09240, 0, 0]],
        atol=1e-5,
    )

    raw_bv = read_raw_brainvision(bv_raw_fname)
    raw_bv.set_channel_types({"HEOG": 'eog', "VEOG": 'eog', "ECG": 'ecg'})

    raw_bv.set_montage(montage)

    test_raw_bv = read_raw_fif(bv_fif_fname)

    # compare after set_montage using chs loc.
    for actual, expected in zip(raw_bv.info['chs'], test_raw_bv.info['chs']):
        assert_allclose(actual['loc'][:3], expected['loc'][:3])
        if actual['kind'] == FIFF.FIFFV_EEG_CH:
            assert_allclose(actual['loc'][3:6],
                            [-0.005103, 0.05395, 0.144622], rtol=1e-04)
Exemplo n.º 34
0
def test_brainvision_data_filters():
    """Test reading raw Brain Vision files
    """
    raw = read_raw_brainvision(vhdr_highpass_path, montage, eog=eog, preload=False)
    assert_equal(raw.info["highpass"], 0.1)
    assert_equal(raw.info["lowpass"], 250.0)
Exemplo n.º 35
0
"""
Load a file using MNE-python
============================

This example shows how to load data using MNE-Python package.
https://martinos.org/mne/stable/index.html

Install MNE-python : pip install mne

A full list of supported format can be found here:
https://martinos.org/mne/stable/python_reference.html#reading-raw-data

Required dataset at :
https://drive.google.com/drive/folders/0B6vtJiCQZUBvRjc3cFFYcmFIeW8?usp=sharing

.. image:: ../../picture/picsleep/ex_LoadMNE.png
"""
from mne import io
from visbrain import Sleep

raw = io.read_raw_brainvision(vhdr_fname='sub-02.vhdr', preload=True)

# Extract data, sampling frequency and channels names
data, sf, chan = raw._data, raw.info['sfreq'], raw.info['ch_names']

# Now, pass all the arguments to the Sleep module :
Sleep(data=data, sf=sf, channels=chan, hypno_file='sub-02.hyp').show()
Exemplo n.º 36
0
def mne_switch(file, ext, downsample, preload=True, **kwargs):
    """Read sleep datasets using mne.io.

    Parameters
    ----------
    file : string
        Filename (without extension).
    ext : string
        File extension (e.g. '.edf'').
    preload : bool | True
        Preload data in memory.
    kwargs : dict | {}
        Further arguments to pass to the mne.io.read function.

    Returns
    -------
    sf : float
        The original sampling-frequency.
    downsample : float
        The down-sampling frequency used.
    dsf : int
        The down-sampling factor.
    data : array_like
        The raw data of shape (n_channels, n_points)
    channels : list
        List of channel names.
    n : int
        Number of time points before down-sampling.
    start_time : datetime.time
        The time offset.
    """
    from mne import io

    # Get full path :
    path = file + ext

    # Preload :
    if preload is False:
        preload = 'temp.dat'
    kwargs['preload'] = preload

    if ext.lower() in ['.edf', '.bdf', '.gdf']:  # EDF / BDF / GDF
        raw = io.read_raw_edf(path, **kwargs)
    elif ext.lower == '.set':   # EEGLAB
        raw = io.read_raw_eeglab(path, **kwargs)
    elif ext.lower() in ['.egi', '.mff']:  # EGI / MFF
        raw = io.read_raw_egi(path, **kwargs)
    elif ext.lower() == '.cnt':  # CNT
        raw = io.read_raw_cnt(path, **kwargs)
    elif ext.lower() == '.vhdr':  # BrainVision
        raw = io.read_raw_brainvision(path, **kwargs)
    else:
        raise IOError("File not supported by mne-python.")

    raw.pick_types(meg=True, eeg=True, ecg=True, emg=True)  # Remove stim lines
    sf = raw.info['sfreq']
    dsf, downsample = get_dsf(downsample, sf)
    channels = raw.info['ch_names']
    data = raw._data

    # Conversion Volt (MNE) to microVolt (Visbrain):
    if raw._raw_extras[0] is not None and 'units' in raw._raw_extras[0]:
        units = raw._raw_extras[0]['units'][0:data.shape[0]]
        data /= np.array(units).reshape(-1, 1)

    n = data.shape[1]
    start_time = datetime.time(0, 0, 0)  # raw.info['meas_date']
    anot = raw.annotations

    return sf, downsample, dsf, data[:, ::dsf], channels, n, start_time, anot
Exemplo n.º 37
0
def test_read_vhdr_annotations_and_events(tmp_path):
    """Test load brainvision annotations and parse them to events."""
    # First we add a custom event that contains a comma in its description
    for src, dest in zip((vhdr_path, vmrk_path, eeg_path),
                         ('test.vhdr', 'test.vmrk', 'test.eeg')):
        shutil.copyfile(src, tmp_path / dest)

    # Commas are encoded as "\1"
    with open(tmp_path / 'test.vmrk', 'a') as fout:
        fout.write(r"Mk15=Comma\1Type,CommaValue\11,7800,1,0\n")

    sfreq = 1000.0
    expected_orig_time = _stamp_to_dt((1384359243, 794232))
    expected_onset_latency = np.array(
        [0, 486., 496., 1769., 1779., 3252., 3262., 4935., 4945., 5999., 6619.,
         6629., 7629., 7699., 7799.]
    )
    expected_annot_description = [
        'New Segment/', 'Stimulus/S253', 'Stimulus/S255', 'Event/254',
        'Stimulus/S255', 'Event/254', 'Stimulus/S255', 'Stimulus/S253',
        'Stimulus/S255', 'Response/R255', 'Event/254', 'Stimulus/S255',
        'SyncStatus/Sync On', 'Optic/O  1', 'Comma,Type/CommaValue,1'
    ]
    expected_events = np.stack([
        expected_onset_latency,
        np.zeros_like(expected_onset_latency),
        [99999, 253, 255, 254, 255, 254, 255, 253, 255, 1255, 254, 255, 99998,
         2001, 10001],
    ]).astype('int64').T
    expected_event_id = {'New Segment/': 99999, 'Stimulus/S253': 253,
                         'Stimulus/S255': 255, 'Event/254': 254,
                         'Response/R255': 1255, 'SyncStatus/Sync On': 99998,
                         'Optic/O  1': 2001, 'Comma,Type/CommaValue,1': 10001}

    raw = read_raw_brainvision(tmp_path / 'test.vhdr', eog=eog)

    # validate annotations
    assert raw.annotations.orig_time == expected_orig_time
    assert_allclose(raw.annotations.onset, expected_onset_latency / sfreq)
    assert_array_equal(raw.annotations.description, expected_annot_description)

    # validate event extraction
    events, event_id = events_from_annotations(raw)
    assert_array_equal(events, expected_events)
    assert event_id == expected_event_id

    # validate that None gives us a sorted list
    expected_none_event_id = {desc: idx + 1 for idx, desc in enumerate(sorted(
        event_id.keys()))}
    events, event_id = events_from_annotations(raw, event_id=None)
    assert event_id == expected_none_event_id

    # Add some custom ones, plus a 2-digit one
    s_10 = 'Stimulus/S 10'
    raw.annotations.append([1, 2, 3], 10, ['ZZZ', s_10, 'YYY'])
    # others starting at 10001 ...
    # we already have "Comma,Type/CommaValue,1" as 10001
    expected_event_id.update(YYY=10002, ZZZ=10003)
    expected_event_id[s_10] = 10
    _, event_id = events_from_annotations(raw)
    assert event_id == expected_event_id

    # Concatenating two shouldn't change the resulting event_id
    # (BAD and EDGE should be ignored)
    with pytest.warns(RuntimeWarning, match='expanding outside'):
        raw_concat = concatenate_raws([raw.copy(), raw.copy()])
    _, event_id = events_from_annotations(raw_concat)
    assert event_id == expected_event_id
Exemplo n.º 38
0
def copyfile_brainvision(vhdr_src, vhdr_dest, anonymize=None, verbose=False):
    """Copy a BrainVision file triplet to a new location and repair links.

    The BrainVision file format consists of three files: .vhdr, .eeg, and .vmrk
    The .eeg and .vmrk files associated with the .vhdr file will be given names
    as in `vhdr_dest` with adjusted extensions. Internal file pointers will be
    fixed.

    Parameters
    ----------
    vhdr_src : str
        The src path of the .vhdr file to be copied.
    vhdr_dest : str
        The destination path of the .vhdr file.
    anonymize : dict | None
        If None (default), no anonymization is performed.
        If dict, data will be anonymized depending on the keys provided with
        the dict: `daysback` is a required key, `keep_his` is an optional key.

        `daysback` : int
            Number of days by which to move back the recording date in time.
            In studies with multiple subjects the relative recording date
            differences between subjects can be kept by using the same number
            of `daysback` for all subject anonymizations. `daysback` should be
            great enough to shift the date prior to 1925 to conform with BIDS
            anonymization rules.

        `keep_his` : bool
            By default (False), all subject information next to the recording
            date will be overwritten as well. If True, keep subject information
            apart from the recording date.

    verbose : bool
        Determine whether results should be logged. Defaults to False.

    See Also
    --------
    mne.io.anonymize_info

    """
    # Get extenstion of the brainvision file
    fname_src, ext_src = _parse_ext(vhdr_src)
    fname_dest, ext_dest = _parse_ext(vhdr_dest)
    if ext_src != ext_dest:
        raise ValueError(f'Need to move data with same extension'
                         f' but got "{ext_src}", "{ext_dest}"')

    eeg_file_path, vmrk_file_path = _get_brainvision_paths(vhdr_src)

    # extract encoding from brainvision header file, or default to utf-8
    enc = _get_brainvision_encoding(vhdr_src, verbose)

    # Copy data .eeg ... no links to repair
    sh.copyfile(eeg_file_path, fname_dest + '.eeg')

    # Write new header and marker files, fixing the file pointer links
    # For that, we need to replace an old "basename" with a new one
    # assuming that all .eeg, .vhdr, .vmrk share one basename
    __, basename_src = op.split(fname_src)
    assert basename_src + '.eeg' == op.split(eeg_file_path)[-1]
    assert basename_src + '.vmrk' == op.split(vmrk_file_path)[-1]
    __, basename_dest = op.split(fname_dest)
    search_lines = ['DataFile=' + basename_src + '.eeg',
                    'MarkerFile=' + basename_src + '.vmrk']

    with open(vhdr_src, 'r', encoding=enc) as fin:
        with open(vhdr_dest, 'w', encoding=enc) as fout:
            for line in fin.readlines():
                if line.strip() in search_lines:
                    line = line.replace(basename_src, basename_dest)
                fout.write(line)

    with open(vmrk_file_path, 'r', encoding=enc) as fin:
        with open(fname_dest + '.vmrk', 'w', encoding=enc) as fout:
            for line in fin.readlines():
                if line.strip() in search_lines:
                    line = line.replace(basename_src, basename_dest)
                fout.write(line)

    if anonymize is not None:
        raw = read_raw_brainvision(vhdr_src, preload=False, verbose=0)
        daysback, keep_his = _check_anonymize(anonymize, raw, '.vhdr')
        raw.info = anonymize_info(raw.info, daysback=daysback,
                                  keep_his=keep_his)
        _anonymize_brainvision(fname_dest + '.vhdr',
                               date=raw.info['meas_date'])

    if verbose:
        for ext in ['.eeg', '.vhdr', '.vmrk']:
            _, fname = os.path.split(fname_dest + ext)
            dirname = op.dirname(op.realpath(vhdr_dest))
            print(f'Created "{fname}" in "{dirname}".')
        if anonymize:
            print('Anonymized all dates in VHDR and VMRK.')
Exemplo n.º 39
0
def _read_raw(raw_fname,
              electrode=None,
              hsp=None,
              hpi=None,
              config=None,
              montage=None,
              verbose=None):
    """Read a raw file into MNE, making inferences based on extension."""
    fname, ext = _parse_ext(raw_fname)

    # MEG File Types
    # --------------
    # KIT systems
    if ext in ['.con', '.sqd']:
        raw = io.read_raw_kit(raw_fname,
                              elp=electrode,
                              hsp=hsp,
                              mrk=hpi,
                              preload=False)

    # Neuromag or converted-to-fif systems
    elif ext in ['.fif']:
        raw = io.read_raw_fif(raw_fname, preload=False)

    # BTi systems
    elif ext == '.pdf':
        if os.path.isfile(raw_fname):
            raw = io.read_raw_bti(raw_fname,
                                  config_fname=config,
                                  head_shape_fname=hsp,
                                  preload=False,
                                  verbose=verbose)

    # CTF systems
    elif ext == '.ds':
        raw = io.read_raw_ctf(raw_fname)

    # EEG File Types
    # --------------
    # BrainVision format by Brain Products, links to  a .eeg and a .vmrk file
    elif ext == '.vhdr':
        raw = io.read_raw_brainvision(raw_fname)

    # EDF (european data format) or BDF (biosemi) format
    elif ext == '.edf' or ext == '.bdf':
        raw = io.read_raw_edf(raw_fname, preload=True)

    # EEGLAB .set format, a .fdt file is potentially linked from the .set
    elif ext == '.set':
        raw = io.read_raw_eeglab(raw_fname)

    # Neuroscan .cnt format
    elif ext == '.cnt':
        raw = io.read_raw_cnt(raw_fname, montage=montage)

    # No supported data found ...
    # ---------------------------
    else:
        raise ValueError("Raw file name extension must be one of %\n"
                         "Got %" % (ALLOWED_EXTENSIONS, ext))
    return raw
Exemplo n.º 40
0
"""
import os
from mne import io
from visbrain import Sleep
from visbrain.io import download_file, path_to_visbrain_data

###############################################################################
#                               LOAD YOUR FILE
###############################################################################
current_path = path_to_visbrain_data()
target_path = os.path.join(current_path, 'sleep_data', 'brainvision')

# Download dataset :
download_file("sleep_brainvision.zip", unzip=True, to_path=target_path)

dfile = os.path.join(target_path, 'sub-02.vhdr')
hfile = os.path.join(target_path, 'sub-02.hyp')

# Read raw data using MNE-python :
raw = io.read_raw_brainvision(vhdr_fname=dfile, preload=True)

# Extract data, sampling frequency and channels names
data, sf, chan = raw._data, raw.info['sfreq'], raw.info['ch_names']

# Now, pass all the arguments to the Sleep module :
Sleep(data=data, sf=sf, channels=chan, hypno=hfile).show()

# Alternatively, these steps can be done automatically by using the 'use_mne'
# input argument of sleep
# Sleep(data=dfile, hypno=hfile, use_mne=True).show()
Exemplo n.º 41
0
    def _read_continuous_data(self, continuous_name):
        self.subject_induced_averages = {}

        for g in self._groups:

            self.subject_induced_averages[g] = []

            for s in self._subjects[g]:
                subject_data = {
                    name: list()
                    for name in self._continuous_conditions
                }

                for c in self._continuous_conditions:
                    fn = self._data_dir + s + continuous_name

                    frequency_map = {}

                    for band, fmin, fmax in iter_freqs:
                        # (re)load the data to save memory
                        raw = io.read_raw_brainvision(fn + str('.vhdr'),
                                                      verbose='ERROR',
                                                      preload=True)
                        events = mne.find_events(raw,
                                                 shortest_event=1,
                                                 verbose='ERROR')

                        # bandpass filter and compute Hilbert
                        raw.filter(
                            fmin,
                            fmax,
                            n_jobs=1,  # use more jobs to speed up.
                            l_trans_bandwidth=
                            1,  # make sure filter params are the same
                            h_trans_bandwidth=
                            1,  # in each band and skip "auto" option.
                            fir_design='firwin')
                        raw.apply_hilbert(n_jobs=1, envelope=False)

                        epochs = mne.Epochs(raw,
                                            events,
                                            self._continuous_conditions[c],
                                            -0.2,
                                            1.0,
                                            baseline=None,
                                            preload=True)
                        # remove evoked response and get analytic signal (envelope)
                        epochs.subtract_evoked()
                        epochs = mne.EpochsArray(data=np.abs(
                            epochs.get_data() * 1e6),
                                                 info=epochs.info,
                                                 tmin=epochs.tmin)
                        # now average and move on
                        average = epochs.average()
                        gfp = np.std(average.data, axis=0)

                        gfp = mne.baseline.rescale(gfp,
                                                   average.times,
                                                   baseline=(None, 0))

                        frequency_map[band] = gfp
                    subject_data[c] = frequency_map
                self.subject_induced_averages[g].append(subject_data)
                print('Completed induced response for subject %s' % (s))
Exemplo n.º 42
0
def get_model(transfer_paths=None):
    K.clear_session()

    model = EEGNet(nb_classes=3,
                   Chans=9,
                   Samples=250,
                   dropoutRate=0.5,
                   kernLength=64,
                   F1=8,
                   D=2,
                   F2=16,
                   dropoutType='Dropout')

    # compile model loss function and optimizer for transfer learning
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    # load base model
    model.load_weights(BASE_WEIGHTS)

    # K.clear_session()

    print(transfer_paths)

    if not transfer_paths or len(transfer_paths) is 0:
        return (model, True)
    print(len(transfer_paths))
    try:
        transfer_raw = read_raw_brainvision(transfer_paths[0], preload=True)
        if len(transfer_paths) > 1:
            for i in range(1, len(transfer_paths)):
                print(i)
                transfer_raw = concatenate_raws([
                    transfer_raw,
                    read_raw_brainvision(transfer_paths[i], preload=True)
                ])
    except Exception as e:
        print('failed', e)
        return (model, None)

    transX, transY = epoch_pilot(transfer_raw,
                                 n_classes=3,
                                 good_channels=GOODS,
                                 resample=RESAMPLE,
                                 trange=T_RANGE,
                                 l_freq=LO_FREQ,
                                 h_freq=HI_FREQ)

    # separate 4:1 train:validation
    transX, transY = stratify(transX, transY, 5)

    trans_trainX, trans_valX = add_kernel_dim(get_fold(transX, 5, 0,
                                                       test_rest_split),
                                              kernels=1)
    trans_trainY, trans_valY = onehot(get_fold(transY, 5, 0, test_rest_split))
    trans_valY, _ = onehot((trans_valY, []))

    # perform transfer learning on the base model and selected transfer file
    train(model, {
        "x": trans_trainX,
        "y": trans_trainY
    }, {
        "x": trans_valX,
        "y": trans_valY
    },
          epochs=EPOCHS)

    return (model, False)
Exemplo n.º 43
0
def load_data_epochs(data_path,
                     channels,
                     onsets,
                     trial_epoch=(-1, 3),
                     baseline_norm=None,
                     baseline_epoch=(-1, -0.1)):
    """
    Load and epoch the data into a matrix based on channels, stimulus onsets and the epoch range (relative to the onsets)

    Args:
        data_path (str):                Path to the data file or folder
        channels (list or tuple):       The channels that should read from the data, the output will be sorted
                                        according to this input argument.
        onsets (list or tuple):         The onsets of the stimuli (e.g. trials) around which to epoch the data
        trial_epoch (tuple):            The time-span that will be considered as the signal belonging to a single trial.
                                        Expressed as a tuple with the start- and end-point in seconds relative to
                                        stimulation onset of the trial (e.g. the standard tuple of '-1, 3' will extract
                                        the signal in the period from 1s before stimulation onset to 3s after
                                        stimulation onset).
        baseline_norm (None or str):    Baseline normalization setting [None, 'Mean' or 'Median']. If other than None,
                                        normalizes each trial epoch by subtracting the mean or median of part of the
                                        trial (the epoch of the trial indicated in baseline_epoch)
        baseline_epoch (tuple):         The time-span on which the baseline is calculated, expressed as a tuple with the
                                        start- and end-point in seconds relative to stimulation onset (e.g. the
                                        standard tuple of '-1, -.1' will use the period from 1s before stimulation onset
                                        to 100ms before stimulation onset to calculate the baseline on); this arguments
                                        is only used when baseline_norm is set to mean or median

    Returns:
        sampling_rate (int or double):  the sampling rate at which the data was acquired
        data (ndarray):                 A three-dimensional array with data epochs per channel (format: channel x
                                        trials/epochs x time); or None when an error occurs

    Note: this function's units are in time relative to stimulation (e.g. trial) onset because the sample rate will
          only be known till after we read the data
    """

    # initialize the return variables to a default
    sampling_rate = None
    data = None

    # TODO: handle different units in data format

    #
    # check input
    #

    # data-set format
    data_extension = data_path[data_path.rindex("."):]
    if data_extension == '.edf':
        data_format = 0
    elif data_extension == '.vhdr' or data_extension == '.vmrk' or data_extension == '.eeg':
        data_format = 1
    elif data_extension == '.mefd':
        data_format = 2
    else:
        logging.error('Unknown data format (' + data_extension + ')')
        return None, None

    #
    if trial_epoch[1] < trial_epoch[0]:
        logging.error(
            'Invalid \'trial_epoch\' parameter, the given end-point (at ' +
            str(trial_epoch[1]) + ') lies before the start-point (at ' +
            str(trial_epoch[0]) + ')')
        return None, None

    # baseline normalization
    baseline_method = 0
    if baseline_norm is not None or len(baseline_norm) > 0:
        if baseline_norm.lower() == 'mean' or baseline_norm.lower(
        ) == 'average':
            baseline_method = 1
        elif baseline_norm.lower() == 'median':
            baseline_method = 2
        elif baseline_norm.lower() == 'none':
            baseline_method = 0
        else:
            logging.error('Unknown normalization argument (' + baseline_norm +
                          ')')
            return None, None

        #
        if baseline_epoch[1] < baseline_epoch[0]:
            logging.error(
                'Invalid \'baseline_epoch\' parameter, the given end-point (at '
                + str(baseline_epoch[1]) +
                ') lies before the start-point (at ' + str(baseline_epoch[0]) +
                ')')
            return None, None
        if data_format == 2:
            if baseline_epoch[0] < trial_epoch[0]:
                logging.error(
                    'Invalid \'baseline_epoch\' parameter, the given baseline start-point (at '
                    + str(baseline_epoch[0]) +
                    ') lies before the trial start-point (at ' +
                    str(trial_epoch[0]) + ')')
                return None, None
            if baseline_epoch[1] > trial_epoch[1]:
                logging.error(
                    'Invalid \'baseline_epoch\' parameter, the given baseline end-point (at '
                    + str(baseline_epoch[1]) +
                    ') lies after the trial end-point (at ' +
                    str(trial_epoch[1]) + ')')
                return None, None

    #
    # read and process the data
    #

    if data_format == 0 or data_format == 1:
        # EDF or BrainVision format, use MNE to read

        # Alternative for EDF (use pyedflib), low memory usage solution since it has the ability to read per channel
        #from pyedflib import EdfReader
        #f = EdfReader(data_path)
        #n = f.signals_in_file
        #signal_labels = f.getSignalLabels()
        #sampling_rate = f.getSampleFrequencies()[0]
        #size_time_s = int(ceil(abs(trial_epoch[1] - trial_epoch[0]) * sampling_rate))
        #data = np.empty((len(channels_include), len(onsets), size_time_s))
        #data.fill(np.nan)
        #for iChannel in range(len(channels)):
        #    channel_index = signal_labels.index(channels[iChannel])
        #    signal = f.readSignal(channel_index)
        #    for iTrial in range(len(onsets)):
        #        sample_start = int(round(onsets[iTrial] * sampling_rate))
        #        data[iChannel, iTrial, :] = signal[sample_start:sample_start + size_time_s]

        # read the data
        try:
            if data_format == 0:
                mne_raw = read_raw_edf(data_path,
                                       eog=[],
                                       misc=[],
                                       stim_channel=[],
                                       preload=True,
                                       verbose=None)
                #mne_raw = read_raw_edf(data_path, eog=None, misc=None, stim_channel=[], exclude=channels_non_ieeg, preload=True, verbose=None)
            if data_format == 1:
                mne_raw = read_raw_brainvision(
                    data_path[:data_path.rindex(".")] + '.vhdr', preload=True)
        except Exception as e:
            logging.error('MNE could not read data, message: ' + str(e))
            return None, None

        # retrieve the sample-rate
        sampling_rate = mne_raw.info['sfreq']

        # calculate the size of the time dimension (in samples)
        size_time_s = int(
            ceil(abs(trial_epoch[1] - trial_epoch[0]) * sampling_rate))

        # initialize a data buffer (channel x trials/epochs x time)
        data = allocate_array((len(channels), len(onsets), size_time_s))
        if data is None:
            return None, None

        # loop through the included channels
        for iChannel in range(len(channels)):

            # (try to) retrieve the index of the channel
            try:
                channel_index = mne_raw.info['ch_names'].index(
                    channels[iChannel])
            except ValueError:
                logging.error('Could not find channel \'' +
                              channels[iChannel] + '\' in the dataset')
                return None, None

            # loop through the trials
            for iTrial in range(len(onsets)):

                #
                trial_sample_start = int(
                    round((onsets[iTrial] + trial_epoch[0]) * sampling_rate))
                if trial_sample_start < 0 or trial_sample_start + size_time_s >= len(
                        mne_raw):
                    logging.error(
                        'Cannot extract the trial with onset ' +
                        str(onsets[iTrial]) +
                        ', the range for extraction lies outside of the data')
                    return None, None

                #
                if baseline_method > 0:
                    baseline_start_sample = int(
                        round((onsets[iTrial] + baseline_epoch[0]) *
                              sampling_rate))
                    baseline_end_sample = int(
                        round((onsets[iTrial] + baseline_epoch[1]) *
                              sampling_rate))
                    if baseline_start_sample < 0 or baseline_end_sample >= len(
                            mne_raw):
                        logging.error(
                            'Cannot extract the baseline for the trial with onset '
                            + str(onsets[iTrial]) +
                            ', the range for the baseline lies outside of the data'
                        )
                        return None, None

                # extract the trial data and perform baseline normalization on the trial if needed
                if baseline_method == 0:
                    data[iChannel, iTrial, :] = mne_raw[
                        channel_index,
                        trial_sample_start:trial_sample_start + size_time_s][0]
                elif baseline_method == 1:
                    baseline_mean = np.nanmean(
                        mne_raw[channel_index,
                                baseline_start_sample:baseline_end_sample][0])
                    data[iChannel, iTrial, :] = mne_raw[
                        channel_index, trial_sample_start:trial_sample_start +
                        size_time_s][0] - baseline_mean
                elif baseline_method == 2:
                    baseline_median = np.nanmedian(
                        mne_raw[channel_index,
                                baseline_start_sample:baseline_end_sample][0])
                    data[iChannel, iTrial, :] = mne_raw[
                        channel_index, trial_sample_start:trial_sample_start +
                        size_time_s][0] - baseline_median

        # TODO: clear memory in MNE, close() doesn't seem to work, neither does remove the channels, issue MNE?
        mne_raw.close()
        del mne_raw

        # MNE always returns data in volt, convert to micro-volt
        data = data * 1000000

    elif data_format == 2:
        # MEF3 format

        # read the session metadata
        try:
            mef = MefSession(data_path, '', read_metadata=True)
        except Exception:
            logging.error(
                'PyMef could not read data, either a password is needed or the data is corrupt'
            )
            return None, None

        # retrieve the sample-rate and total number of samples in the data-set
        sampling_rate = mef.session_md['time_series_metadata']['section_2'][
            'sampling_frequency'].item(0)
        num_samples = mef.session_md['time_series_metadata']['section_2'][
            'number_of_samples'].item(0)

        # calculate the size of the time dimension (in samples)
        size_time_s = int(
            ceil(abs(trial_epoch[1] - trial_epoch[0]) * sampling_rate))

        # initialize a data buffer (channel x trials/epochs x time)
        data = allocate_array((len(channels), len(onsets), size_time_s))
        if data is None:
            return None, None

        # create a progress bar
        print_progressbar(0,
                          len(onsets),
                          prefix='Progress:',
                          suffix='Complete',
                          length=50)

        # loop through the trials
        for iTrial in range(len(onsets)):

            #
            trial_sample_start = int(
                round((onsets[iTrial] + trial_epoch[0]) * sampling_rate))
            if trial_sample_start < 0 or trial_sample_start + size_time_s >= num_samples:
                logging.error(
                    'Cannot extract the trial with onset ' +
                    str(onsets[iTrial]) +
                    ', the range for extraction lies outside of the data')
                return None, None

            #
            if baseline_method > 0:
                baseline_start_sample = int(
                    round((onsets[iTrial] + baseline_epoch[0]) *
                          sampling_rate)) - trial_sample_start
                baseline_end_sample = int(
                    round((onsets[iTrial] + baseline_epoch[1]) *
                          sampling_rate)) - trial_sample_start
                if baseline_start_sample < 0 or baseline_end_sample >= size_time_s:
                    logging.error(
                        'Cannot extract the baseline, the range for the baseline lies outside of the trial epoch'
                    )
                    return None, None

            # load the trial data
            try:
                trial_data = mef.read_ts_channels_sample(
                    channels,
                    [trial_sample_start, trial_sample_start + size_time_s])
                if trial_data is None or (len(trial_data) > 0
                                          and trial_data[0] is None):
                    return None, None
            except Exception:
                logging.error(
                    'PyMef could not read data, either a password is needed or the data is corrupt'
                )
                return None, None

            # loop through the channels
            for iChannel in range(len(channels)):

                if baseline_method == 0:
                    data[iChannel, iTrial, :] = trial_data[iChannel]
                elif baseline_method == 1:
                    baseline_mean = np.nanmean(
                        trial_data[iChannel]
                        [baseline_start_sample:baseline_end_sample])
                    data[iChannel,
                         iTrial, :] = trial_data[iChannel] - baseline_mean
                elif baseline_method == 2:
                    baseline_median = np.nanmedian(
                        trial_data[iChannel]
                        [baseline_start_sample:baseline_end_sample])
                    data[iChannel,
                         iTrial, :] = trial_data[iChannel] - baseline_median

            del trial_data

            # update progress bar
            print_progressbar(iTrial + 1,
                              len(onsets),
                              prefix='Progress:',
                              suffix='Complete',
                              length=50)

    #
    return sampling_rate, data
Exemplo n.º 44
0
 def save_setup(self):
     de = self.data_entries.copy()
     raw_fname = de.pop('New File').get()
     loc_fname = de.pop('Loc File').get()
     suffix = raw_fname.split('.')[-1]
     if suffix == 'fif':
         try:
             raw = Raw(raw_fname, preload=False)
         except Exception as e:
             print(e)
             messagebox.showerror('Error', 'Failed to read fif')
             return
         if loc_fname:
             messagebox.showwarning('Warning',
                                    'Ignoring location file given')
     elif suffix == 'vhdr':
         if not loc_fname:
             messagebox.showerror('Error', 'Location file not specified')
         try:
             raw = read_raw_brainvision(raw_fname,
                                        preload=os.path.join(
                                            self.sub_dirs['tmp'],
                                            'raw_tmp'))
         except Exception as e:
             print(e)
             messagebox.showerror('Error',
                                  'Failed to read BrainVision raw file')
             return
         try:
             montage = read_dig_montage(bvct=loc_fname)
             ref_name = [
                 ch for ch in montage.dig_ch_pos
                 if not ch in raw.info['ch_names']
             ]
             if len(ref_name) != 1:
                 ref_name = simpledialog.askstring(
                     'Reference Channel',
                     'What is the reference channel name?',
                     parent=self.root)
             else:
                 ref_name = ref_name[0]
             info = create_info([ref_name], raw.info['sfreq'], ['eeg'])
             ref = RawArray(np.zeros((1, len(raw.times))), info)
             ref.info['lowpass'] = raw.info['lowpass']
             raw = raw.add_channels([ref])  #, force_update_info=True)
             #
             for ch_type in ['eog', 'ecg', 'emg', 'stim']:
                 chs = [
                     ch for ch in raw.info['ch_names']
                     if ch_type in ch.lower()
                 ]
                 for ch in chs:
                     ch_ix = raw.ch_names.index(ch)
                     raw._data[ch_ix, :] *= 1e-6
                     raw.set_channel_types({ch: ch_type})
             raw = raw.set_montage(montage, verbose=False)
         except Exception as e:
             print(e)
             messagebox.showerror(
                 'Error', 'Failed to read BrainVision location file')
             return
     else:
         messagebox.showerror('Error', 'Unrecognized raw file format')
         return
     self.save_exp(raw)
Exemplo n.º 45
0
def test_events():
    """Test reading and modifying events."""
    tempdir = _TempDir()
    # Note: BrainVision event offsets are 1-based, mne offsets are 0-based.
    # So in all tests below, the "onset" is 1 less than what's in the file

    # check that events are read and stim channel is synthesized correctly
    raw = read_raw_brainvision_old(vhdr_path, eog=eog, event_id=event_id)
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(
        events,
        [[486, 0, 253], [496, 0, 255], [1769, 0, 254], [1779, 0, 255],
         [3252, 0, 254], [3262, 0, 255], [4935, 0, 253], [4945, 0, 255],
         [5999, 0, 255], [6619, 0, 254], [6629, 0, 255], [7699, 0, 1]])

    # check that events are read and stim channel is synthesized correctly and
    # response triggers are shifted using the deprecated response_trig_shift.
    with pytest.deprecated_call():
        raw = read_raw_brainvision_old(vhdr_path,
                                       eog=eog,
                                       response_trig_shift=1000,
                                       event_id=event_id)
    events = raw._get_brainvision_events()
    assert_array_equal(
        events,
        [[486, 0, 253], [496, 0, 255], [1769, 0, 254], [1779, 0, 255],
         [3252, 0, 254], [3262, 0, 255], [4935, 0, 253], [4945, 0, 255],
         [5999, 0, 1255], [6619, 0, 254], [6629, 0, 255], [7629, 0, 5],
         [7699, 0, 1]])

    # check that trig_shift_by_type works as well
    raw = read_raw_brainvision_old(vhdr_path,
                                   eog=eog,
                                   trig_shift_by_type={
                                       'response': 1000,
                                       'Optic': 2000
                                   },
                                   event_id=event_id)
    events = raw._get_brainvision_events()
    assert_array_equal(
        events,
        [[486, 0, 253], [496, 0, 255], [1769, 0, 254], [1779, 0, 255],
         [3252, 0, 254], [3262, 0, 255], [4935, 0, 253], [4945, 0, 255],
         [5999, 0, 1255], [6619, 0, 254], [6629, 0, 255], [7629, 0, 5],
         [7699, 0, 2001]])

    # Check that we warn if a trigger is dropped
    with pytest.warns(RuntimeWarning, match='will be dropped'):
        raw = read_raw_brainvision_old(vhdr_path)
    # check that events are read and stim channel is synthesized correctly and
    # response triggers are ignored.
    raw = read_raw_brainvision_old(vhdr_path,
                                   eog=eog,
                                   event_id=event_id,
                                   trig_shift_by_type={'response': None})
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(
        events, [[486, 0, 253], [496, 0, 255], [1769, 0, 254], [1779, 0, 255],
                 [3252, 0, 254], [3262, 0, 255], [4935, 0, 253],
                 [4945, 0, 255], [6619, 0, 254], [6629, 0, 255], [7699, 0, 1]])

    # Error handling of trig_shift_by_type
    pytest.raises(TypeError,
                  read_raw_brainvision_old,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  trig_shift_by_type=1)
    pytest.raises(TypeError,
                  read_raw_brainvision_old,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  trig_shift_by_type={'response': 0.1})
    pytest.raises(TypeError,
                  read_raw_brainvision_old,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  trig_shift_by_type={'response': np.nan})
    pytest.raises(ValueError,
                  read_raw_brainvision_old,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  trig_shift_by_type={
                      'response': 1000,
                      'Response': 1001
                  })
    with pytest.warns(DeprecationWarning):
        pytest.raises(ValueError,
                      read_raw_brainvision_old,
                      vhdr_path,
                      eog=eog,
                      preload=True,
                      trig_shift_by_type={'response': 1000},
                      response_trig_shift=1001)

    # Check that events of type "Comment" are read if they contain square
    # brackets (which usually signify a new section within a BrainVision file)
    # If no event_id specified, skip the marker and continue as planned
    with pytest.warns(RuntimeWarning, match='channel types to misc'):
        raw = read_raw_brainvision_old(vhdr_v2_path)
    events = raw._get_brainvision_events()
    assert events.shape == (11, 3)  # shape of events without comment

    # with event_id specified, get that comment and assert it's there
    tmp_event_id = {'comment using [square] brackets': 999}
    with pytest.warns(RuntimeWarning, match='channel types to misc'):
        raw = read_raw_brainvision_old(vhdr_v2_path, event_id=tmp_event_id)
    events = raw._get_brainvision_events()
    assert 999 in events[:, -1]
    assert events.shape == (12, 3)  # shape of events with comment

    # check that events are read properly when event_id is specified for
    # auxiliary events
    raw = read_raw_brainvision_old(vhdr_path,
                                   eog=eog,
                                   preload=True,
                                   trig_shift_by_type={'response': None},
                                   event_id=event_id)
    events = raw._get_brainvision_events()

    expected_events = np.array([[486, 0, 253], [496, 0, 255], [1769, 0, 254],
                                [1779, 0, 255], [3252, 0, 254], [3262, 0, 255],
                                [4935, 0, 253], [4945, 0, 255], [6619, 0, 254],
                                [6629, 0, 255], [7629, 0, 5], [7699, 0, 1]])

    assert_array_equal(events, expected_events)

    # Test that both trig_shift_by_type and event_id can be set
    read_raw_brainvision(vhdr_path,
                         eog=eog,
                         preload=False,
                         trig_shift_by_type={'response': 100},
                         event_id=event_id)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw._set_brainvision_events(events)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-2]['ch_name'], ch_name)
    assert_equal(len(find_events(raw, 'STI 014')), 0)
    assert_allclose(raw[-1][0], 0.)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')

    # degenerate
    with pytest.raises(TypeError, match='stim_channel must be an instance of'):
        read_raw_brainvision(vhdr_path, stim_channel='foo')
Exemplo n.º 46
0
# mapu, ve ktere jsou ulozeny nazvy testovacich souboru a jejich targetove nazvy
files_testing_map = load_file_names.load_predicting_data_names()

data_training_count = len(files_training_map)
data_predicting_count = len(files_testing_map)

##############################################
#
#            Loading data to train_nn
#
##############################################
raw = []
for i in range(data_training_count):
    path = DATA_FOLDER + (files_training_map[i][first_item])
    raw.append(io.read_raw_brainvision(vhdr_fname=path, preload=True))
    raw[i].filter(low_filter_frequency, high_filter_frequency)

##############################################
#
#             Loading data to predict
#
##############################################
raw_to_predict = []
true_prediction = []
for i in range(data_predicting_count):
    path = DATA_FOLDER + (files_testing_map[i][first_item])
    true_prediction.append(files_testing_map[i][second_item])
    true_prediction[i] = true_prediction[i].strip()
    raw_to_predict.append(
        io.read_raw_brainvision(vhdr_fname=path, preload=True))
Exemplo n.º 47
0
    print(f'  BIDS Path: {bids_path.directory}')
    print(f'  Derivative Path: {deriv_path}')

    # If EEG file alread writen skip this person
    exist_check = bids_path.copy().update(suffix='eeg', extension='vhdr')
    if exist_check.fpath.is_file() and not overwrite:
        print('SUBJECT BIDS DATA EXISTS: SKIPPING')
        continue

    # WRITE EEG TO BIDS FORMAT
    # Define the source data file
    source_vhdr = source_path / f'{sub}_task-{task}_run-01_eeg.vhdr'

    # Read in raw bv from source
    raw = read_raw_brainvision(source_vhdr,
                               misc=['Photosensor'],
                               eog=['VEOG', 'HEOG'])

    # HANDLE SPECIAL SUBJECTS
    # Fix channel order (swap VEOG and HEOG)
    if bids_id in ['121', '230', '237']:
        ch_names = raw.copy().info['ch_names']
        ch_names[-1], ch_names[-2] = ch_names[-2], ch_names[-1]
        raw = raw.reorder_channels(ch_names)
        raw.rename_channels(dict(VEOG='HEOG', HEOG='VEOG'))

    # Update line frequency to 60 Hz
    raw.info['line_freq'] = 60.0

    # Update event descriptions
    description = raw.annotations.description
Exemplo n.º 48
0
def test_events():
    """Test reading and modifying events."""
    tempdir = _TempDir()

    # check that events are read and stim channel is synthesized correcly
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True)
        events = raw._get_brainvision_events()
        assert_array_equal(
            events,
            [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
             [3253, 1, 254], [3263, 1, 255], [4936, 1, 253], [4946, 1, 255],
             [6000, 1, 255], [6620, 1, 254], [6630, 1, 255]])
        assert_equal(len(w), 1)  # for dropping Sync & R255 events

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are shifted like they're supposed to be.
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path,
                                   eog=eog,
                                   preload=True,
                                   response_trig_shift=1000)
        events = raw._get_brainvision_events()
        assert_array_equal(
            events,
            [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
             [3253, 1, 254], [3263, 1, 255], [4936, 1, 253], [4946, 1, 255],
             [6000, 1, 1255], [6620, 1, 254], [6630, 1, 255]])
        assert_equal(len(w), 1)  # for dropping Sync & R255 events

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are ignored.
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path,
                                   eog=eog,
                                   preload=True,
                                   response_trig_shift=None)
        events = raw._get_brainvision_events()
        assert_array_equal(
            events,
            [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
             [3253, 1, 254], [3263, 1, 255], [4936, 1, 253], [4946, 1, 255],
             [6620, 1, 254], [6630, 1, 255]])
        assert_equal(len(w), 1)  # for dropping Sync & R255 events

    # check that events are read properly when event_id is specified for
    # auxiliary events
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path,
                                   eog=eog,
                                   preload=True,
                                   response_trig_shift=None,
                                   event_id={'Sync On': 5})
        events = raw._get_brainvision_events()
        assert_array_equal(
            events,
            [[487, 1, 253], [497, 1, 255], [1770, 1, 254], [1780, 1, 255],
             [3253, 1, 254], [3263, 1, 255], [4936, 1, 253], [4946, 1, 255],
             [6620, 1, 254], [6630, 1, 255], [7630, 1, 5]])
        assert_equal(len(w), 1)  # parsing Sync event, missing R255

    assert_raises(TypeError,
                  read_raw_brainvision,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  response_trig_shift=0.1)
    assert_raises(TypeError,
                  read_raw_brainvision,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  response_trig_shift=np.nan)

    # Test that both response_trig_shit and event_id can be set
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        read_raw_brainvision(vhdr_path,
                             eog=eog,
                             preload=False,
                             response_trig_shift=100,
                             event_id={'Sync On': 5})

        mne_events = find_events(raw, stim_channel='STI 014')
        assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])
        assert_equal(len(w), 0)  # parsing the Sync event

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw._set_brainvision_events(events)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-2]['ch_name'], ch_name)
    assert_equal(len(find_events(raw, 'STI 014')), 0)
    assert_allclose(raw[-1][0], 0.)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')
Exemplo n.º 49
0
.. image:: ../../_static/examples/ex_LoadMNE.png
"""
import os
from mne import io
from visbrain.gui import Sleep
from visbrain.io import download_file, path_to_visbrain_data

###############################################################################
#                               LOAD YOUR FILE
###############################################################################
# Download dataset :
download_file("sleep_brainvision.zip", unzip=True, astype='example_data')
target_path = path_to_visbrain_data(folder='example_data')

dfile = os.path.join(target_path, 'sub-02.vhdr')
hfile = os.path.join(target_path, 'sub-02.hyp')

# Read raw data using MNE-python :
raw = io.read_raw_brainvision(vhdr_fname=dfile, preload=True)

# Extract data, sampling frequency and channels names
data, sf, chan = raw._data, raw.info['sfreq'], raw.info['ch_names']

# Now, pass all the arguments to the Sleep module :
Sleep(data=data, sf=sf, channels=chan, hypno=hfile).show()

# Alternatively, these steps can be done automatically by using the 'use_mne'
# input argument of sleep
# Sleep(data=dfile, hypno=hfile, use_mne=True).show()
Exemplo n.º 50
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
Exemplo n.º 51
0
ids = pd.read_csv('../data_bids/subject_data/subject_ids.tsv',
                  sep='\t',
                  header=0)

###############################################################################
# 2) loop through files and crate copy of the file in question, extract the
# necessary information and export the copy in a way BIDS-compliant way
for index, row in ids.iterrows():

    print(index, row)

    # path and filename
    fname = os.path.join(input_path, row['file_name'])

    # *** 1) import file and extract file info ***
    raw = read_raw_brainvision(fname, preload=False)
    # sampling rate
    sfreq = raw.info['sfreq']
    # event onsets and events ids (annotations entailed in the EEG-file)
    events, event_ids = events_from_annotations(raw, event_id=event_ids)

    # *** 2) get start and end event markers for the desired tasks ***
    # name of fix cross end marker
    start_marker = 0
    fix_marker = event_ids['Bedingung/B100']
    for task in tasks:

        if task == 'congruentstroop':
            # name of start marker
            start_marker = event_ids['Bedingung/B 21']
        elif task == 'incongruentstroop':
Exemplo n.º 52
0
def test_vmrk_meas_date():
    """Test successful extraction of measurement date."""
    # Test file that does have a specific date
    raw = read_raw_brainvision(vhdr_path)
    assert_allclose(raw.info['meas_date'], [1384359243, 794231])
    assert '2013-11-13 16:14:03 GMT' in repr(raw.info)

    # Test file with multiple dates ... we should only take the first
    with pytest.warns(RuntimeWarning, match='software filter'):
        raw = read_raw_brainvision(vhdr_old_path)
    assert_allclose(raw.info['meas_date'], [1184588560, 937453])
    assert '2007-07-16 12:22:40 GMT' in repr(raw.info)

    # Test files with no date, we should get DATE_NONE from mne.io.write
    with pytest.warns(RuntimeWarning, match='coordinate information'):
        raw = read_raw_brainvision(vhdr_v2_path)
    assert raw.info['meas_date'] is None
    assert 'unspecified' in repr(raw.info)

    # Test files with faulty dates introduced by segmenting a file without
    # date information. Should not raise a strptime ValueError
    raw = read_raw_brainvision(vhdr_bad_date)
    assert raw.info['meas_date'] is None
    assert 'unspecified' in repr(raw.info)

    # Reuse our vmrk to test several other cases with dates
    tmpdir = _TempDir()
    tmp_vhdr_file = op.join(tmpdir, op.basename(vhdr_bad_date))
    tmp_vmrk_file = tmp_vhdr_file.replace('.vhdr', '.vmrk')
    tmp_eeg_file = op.join(tmpdir, op.basename(eeg_path))
    sh.copyfile(vhdr_bad_date, tmp_vhdr_file)
    sh.copyfile(vhdr_bad_date.replace('.vhdr', '.vmrk'), tmp_vmrk_file)
    sh.copyfile(eeg_path, tmp_eeg_file)

    # We'll exclusively manipulate the "New Segment" line in vmrk
    with open(tmp_vmrk_file, 'r') as fin:
        lines = fin.readlines()
    idx = lines.index('Mk1=New Segment,,1,1,0,00000000000304125000\n')

    # Now perform some tests
    # Test that we get no error for trying to extract meas_date if there is
    # no marker of type "New Segment" in the data
    lines[idx] = 'Mk1=STATUS,,1,1,0\n'
    with open(tmp_vmrk_file, 'w') as fout:
        fout.writelines(lines)
    raw = read_raw_brainvision(tmp_vhdr_file)
    assert raw.info['meas_date'] is None
    assert 'unspecified' in repr(raw.info)

    # Test that we extract no date, if "New Segment", but no date specified
    # Note the trailing comma but missing data following that comma
    lines[idx] = 'Mk1=New Segment,,1,1,0,\n'
    with open(tmp_vmrk_file, 'w') as fout:
        fout.writelines(lines)
    raw = read_raw_brainvision(tmp_vhdr_file)
    assert raw.info['meas_date'] is None
    assert 'unspecified' in repr(raw.info)

    # Test that no error if "New Segment", but no date specified and no
    # trailing comma
    lines[idx] = 'Mk1=New Segment,,1,1,0\n'
    with open(tmp_vmrk_file, 'w') as fout:
        fout.writelines(lines)
    raw = read_raw_brainvision(tmp_vhdr_file)
    assert raw.info['meas_date'] is None
    assert 'unspecified' in repr(raw.info)

    # Test that a fine date gets recognized, use 2000-01-01:12:00:00, assuming
    # UTC timezone, which would be in unix time: 946728000
    lines[idx] = 'Mk1=New Segment,,1,1,0,20000101120000000000\n'
    with open(tmp_vmrk_file, 'w') as fout:
        fout.writelines(lines)
    raw = read_raw_brainvision(tmp_vhdr_file)
    assert raw.info['meas_date'] is not None
    assert raw.info['meas_date'][1] == 0  # no microseconds
    assert raw.info['meas_date'][0] == 946728000
Exemplo n.º 53
0
def test_events():
    """Test reading and modifying events"""
    tempdir = _TempDir()

    # check that events are read and stim channel is synthesized correcly
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True)
        events = raw._get_brainvision_events()
        assert_array_equal(events, [[487, 1, 253],
                                    [497, 1, 255],
                                    [1770, 1, 254],
                                    [1780, 1, 255],
                                    [3253, 1, 254],
                                    [3263, 1, 255],
                                    [4936, 1, 253],
                                    [4946, 1, 255],
                                    [6000, 1, 255],
                                    [6620, 1, 254],
                                    [6630, 1, 255]])
        assert_equal(len(w), 1)  # for dropping Sync & R255 events

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are shifted like they're supposed to be.
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True,
                                   response_trig_shift=1000)
        events = raw._get_brainvision_events()
        assert_array_equal(events, [[487, 1, 253],
                                    [497, 1, 255],
                                    [1770, 1, 254],
                                    [1780, 1, 255],
                                    [3253, 1, 254],
                                    [3263, 1, 255],
                                    [4936, 1, 253],
                                    [4946, 1, 255],
                                    [6000, 1, 1255],
                                    [6620, 1, 254],
                                    [6630, 1, 255]])
        assert_equal(len(w), 1)  # for dropping Sync & R255 events

    # check that events are read and stim channel is synthesized correcly and
    # response triggers are ignored.
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True,
                                   response_trig_shift=None)
        events = raw._get_brainvision_events()
        assert_array_equal(events, [[487, 1, 253],
                                    [497, 1, 255],
                                    [1770, 1, 254],
                                    [1780, 1, 255],
                                    [3253, 1, 254],
                                    [3263, 1, 255],
                                    [4936, 1, 253],
                                    [4946, 1, 255],
                                    [6620, 1, 254],
                                    [6630, 1, 255]])
        assert_equal(len(w), 1)  # for dropping Sync & R255 events

    # check that events are read properly when event_id is specified for
    # auxiliary events
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw = read_raw_brainvision(vhdr_path, eog=eog, preload=True,
                                   response_trig_shift=None,
                                   event_id={'Sync On': 5})
        events = raw._get_brainvision_events()
        assert_array_equal(events, [[487, 1, 253],
                                    [497, 1, 255],
                                    [1770, 1, 254],
                                    [1780, 1, 255],
                                    [3253, 1, 254],
                                    [3263, 1, 255],
                                    [4936, 1, 253],
                                    [4946, 1, 255],
                                    [6620, 1, 254],
                                    [6630, 1, 255],
                                    [7630, 1, 5]])
        assert_equal(len(w), 1)  # parsing Sync event, missing R255

    assert_raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, response_trig_shift=0.1)
    assert_raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                  preload=True, response_trig_shift=np.nan)

    # Test that both response_trig_shit and event_id can be set
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        read_raw_brainvision(vhdr_path, eog=eog, preload=False,
                             response_trig_shift=100,
                             event_id={'Sync On': 5})

        mne_events = find_events(raw, stim_channel='STI 014')
        assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])
        assert_equal(len(w), 0)  # parsing the Sync event

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw._set_brainvision_events(events)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-2]['ch_name'], ch_name)
    assert_equal(len(find_events(raw, 'STI 014')), 0)
    assert_allclose(raw[-1][0], 0.)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')
Exemplo n.º 54
0
def test_events():
    """Test reading and modifying events."""
    tempdir = _TempDir()
    # Note: BrainVision event offsets are 1-based, mne offsets are 0-based.
    # So in all tests below, the "onset" is 1 less than what's in the file

    # check that events are read and stim channel is synthesized correctly
    raw = read_raw_brainvision(vhdr_path, eog=eog, event_id=event_id)
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(
        events,
        [[486, 0, 253], [496, 1, 255], [1769, 1, 254], [1779, 1, 255],
         [3252, 1, 254], [3262, 1, 255], [4935, 1, 253], [4945, 1, 255],
         [5999, 1, 255], [6619, 1, 254], [6629, 1, 255]])

    # check that events are read and stim channel is synthesized correctly and
    # response triggers are shifted like they're supposed to be.
    raw = read_raw_brainvision(vhdr_path,
                               eog=eog,
                               response_trig_shift=1000,
                               event_id=event_id)
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(
        events,
        [[486, 0, 253], [496, 1, 255], [1769, 1, 254], [1779, 1, 255],
         [3252, 1, 254], [3262, 1, 255], [4935, 1, 253], [4945, 1, 255],
         [5999, 1, 1255], [6619, 1, 254], [6629, 1, 255]])

    # check that events are read and stim channel is synthesized correctly and
    # response triggers are ignored.
    with warnings.catch_warnings(record=True):  # ignored events
        raw = read_raw_brainvision(vhdr_path,
                                   eog=eog,
                                   response_trig_shift=None)
    events = raw._get_brainvision_events()
    events = events[events[:, 2] != event_id['Sync On']]
    assert_array_equal(
        events, [[486, 0, 253], [496, 1, 255], [1769, 1, 254], [1779, 1, 255],
                 [3252, 1, 254], [3262, 1, 255], [4935, 1, 253],
                 [4945, 1, 255], [6619, 1, 254], [6629, 1, 255]])

    # check that events are read properly when event_id is specified for
    # auxiliary events
    with warnings.catch_warnings(record=True):  # dropped events
        raw = read_raw_brainvision(vhdr_path,
                                   eog=eog,
                                   preload=True,
                                   response_trig_shift=None,
                                   event_id=event_id)
    events = raw._get_brainvision_events()
    assert_array_equal(
        events, [[486, 0, 253], [496, 1, 255], [1769, 1, 254], [1779, 1, 255],
                 [3252, 1, 254], [3262, 1, 255], [4935, 1, 253],
                 [4945, 1, 255], [6619, 1, 254], [6629, 1, 255], [7629, 1, 5]])

    pytest.raises(TypeError,
                  read_raw_brainvision,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  response_trig_shift=0.1)
    pytest.raises(TypeError,
                  read_raw_brainvision,
                  vhdr_path,
                  eog=eog,
                  preload=True,
                  response_trig_shift=np.nan)

    # to handle the min duration = 1 of stim trig (re)construction ...
    events = np.array([[486, 1, 253], [496, 1, 255], [1769, 1, 254],
                       [1779, 1, 255], [3252, 1, 254], [3262, 1, 255],
                       [4935, 1, 253], [4945, 1, 255], [6619, 1, 254],
                       [6629, 1, 255], [7629, 1, 5]])

    # Test that both response_trig_shit and event_id can be set
    read_raw_brainvision(vhdr_path,
                         eog=eog,
                         preload=False,
                         response_trig_shift=100,
                         event_id=event_id)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # modify events and check that stim channel is updated
    index = events[:, 2] == 255
    events = events[index]
    raw._set_brainvision_events(events)
    mne_events = find_events(raw, stim_channel='STI 014')
    assert_array_equal(events[:, [0, 2]], mne_events[:, [0, 2]])

    # remove events
    nchan = raw.info['nchan']
    ch_name = raw.info['chs'][-2]['ch_name']
    events = np.empty((0, 3))
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-2]['ch_name'], ch_name)
    assert_equal(len(find_events(raw, 'STI 014')), 0)
    assert_allclose(raw[-1][0], 0.)
    fname = op.join(tempdir, 'evt_raw.fif')
    raw.save(fname)

    # add events back in
    events = [[10, 1, 2]]
    raw._set_brainvision_events(events)
    assert_equal(raw.info['nchan'], nchan)
    assert_equal(len(raw._data), nchan)
    assert_equal(raw.info['chs'][-1]['ch_name'], 'STI 014')