Exemple #1
0
def test_plot_raw_filtered(filtorder, raw, browse_backend):
    """Test filtering of raw plots."""
    with pytest.raises(ValueError, match='lowpass.*Nyquist'):
        raw.plot(lowpass=raw.info['sfreq'] / 2., filtorder=filtorder)
    with pytest.raises(ValueError, match='highpass must be > 0'):
        raw.plot(highpass=0, filtorder=filtorder)
    with pytest.raises(ValueError, match='Filter order must be'):
        raw.plot(lowpass=1, filtorder=-1)
    with pytest.raises(ValueError, match="Invalid value for the 'clipping'"):
        raw.plot(clipping='foo')
    raw.plot(lowpass=40, clipping='transparent', filtorder=filtorder)
    raw.plot(highpass=1, clipping='clamp', filtorder=filtorder)
    raw.plot(lowpass=40, butterfly=True, filtorder=filtorder)
    # shouldn't break if all shown are non-data
    RawArray(np.zeros((1, 100)), create_info(1, 20., 'stim')).plot(lowpass=5)
def test_date_none(tmpdir):
    """Test that DATE_NONE is used properly."""
    # Regression test for gh-5908
    n_chans = 139
    n_samps = 20
    data = np.random.random_sample((n_chans, n_samps))
    ch_names = ['E{}'.format(x) for x in range(n_chans)]
    ch_types = ['eeg'] * n_chans
    info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=2048)
    assert info['meas_date'] is None
    raw = RawArray(data=data, info=info)
    fname = op.join(str(tmpdir), 'test-raw.fif')
    raw.save(fname)
    raw_read = read_raw_fif(fname, preload=True)
    assert raw_read.info['meas_date'] is None
Exemple #3
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""
        fname = self.data_path(subject)

        data = loadmat(
            fname,
            squeeze_me=True,
            struct_as_record=False,
            verify_compressed_data_integrity=False,
        )["eeg"]

        # fmt: off
        eeg_ch_names = [
            "Fp1", "AF7", "AF3", "F1", "F3", "F5", "F7", "FT7", "FC5", "FC3", "FC1",
            "C1", "C3", "C5", "T7", "TP7", "CP5", "CP3", "CP1", "P1", "P3", "P5", "P7",
            "P9", "PO7", "PO3", "O1", "Iz", "Oz", "POz", "Pz", "CPz", "Fpz", "Fp2",
            "AF8", "AF4", "AFz", "Fz", "F2", "F4", "F6", "F8", "FT8", "FC6", "FC4",
            "FC2", "FCz", "Cz", "C2", "C4", "C6", "T8", "TP8", "CP6", "CP4", "CP2",
            "P2", "P4", "P6", "P8", "P10", "PO8", "PO4", "O2",
        ]
        # fmt: on
        emg_ch_names = ["EMG1", "EMG2", "EMG3", "EMG4"]
        ch_names = eeg_ch_names + emg_ch_names + ["Stim"]
        ch_types = ["eeg"] * 64 + ["emg"] * 4 + ["stim"]
        montage = make_standard_montage("standard_1005")
        imagery_left = data.imagery_left - data.imagery_left.mean(axis=1, keepdims=True)
        imagery_right = data.imagery_right - data.imagery_right.mean(
            axis=1, keepdims=True
        )

        eeg_data_l = np.vstack([imagery_left * 1e-6, data.imagery_event])
        eeg_data_r = np.vstack([imagery_right * 1e-6, data.imagery_event * 2])

        # trials are already non continuous. edge artifact can appears but
        # are likely to be present during rest / inter-trial activity
        eeg_data = np.hstack(
            [eeg_data_l, np.zeros((eeg_data_l.shape[0], 500)), eeg_data_r]
        )
        log.warning(
            "Trials demeaned and stacked with zero buffer to create "
            "continuous data -- edge effects present"
        )

        info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=data.srate)
        raw = RawArray(data=eeg_data, info=info, verbose=False)
        raw.set_montage(montage)

        return {"session_0": {"run_0": raw}}
Exemple #4
0
def test_crop():
    """Test cropping raw files
    """
    # split a concatenated file to test a difficult case
    raw = Raw([fif_fname, fif_fname], preload=False)
    split_size = 10.  # in seconds
    sfreq = raw.info['sfreq']
    nsamp = (raw.last_samp - raw.first_samp + 1)

    # do an annoying case (off-by-one splitting)
    tmins = np.r_[1., np.round(np.arange(0., nsamp - 1, split_size * sfreq))]
    tmins = np.sort(tmins)
    tmaxs = np.concatenate((tmins[1:] - 1, [nsamp - 1]))
    tmaxs /= sfreq
    tmins /= sfreq
    raws = [None] * len(tmins)
    for ri, (tmin, tmax) in enumerate(zip(tmins, tmaxs)):
        raws[ri] = raw.copy().crop(tmin, tmax, copy=False)
    all_raw_2 = concatenate_raws(raws, preload=False)
    assert_equal(raw.first_samp, all_raw_2.first_samp)
    assert_equal(raw.last_samp, all_raw_2.last_samp)
    assert_array_equal(raw[:, :][0], all_raw_2[:, :][0])

    tmins = np.round(np.arange(0., nsamp - 1, split_size * sfreq))
    tmaxs = np.concatenate((tmins[1:] - 1, [nsamp - 1]))
    tmaxs /= sfreq
    tmins /= sfreq

    # going in revere order so the last fname is the first file (need it later)
    raws = [None] * len(tmins)
    for ri, (tmin, tmax) in enumerate(zip(tmins, tmaxs)):
        raws[ri] = raw.copy().crop(tmin, tmax, copy=False)
    # test concatenation of split file
    all_raw_1 = concatenate_raws(raws, preload=False)

    all_raw_2 = raw.copy().crop(0, None, copy=False)
    for ar in [all_raw_1, all_raw_2]:
        assert_equal(raw.first_samp, ar.first_samp)
        assert_equal(raw.last_samp, ar.last_samp)
        assert_array_equal(raw[:, :][0], ar[:, :][0])

    # test shape consistency of cropped raw
    data = np.zeros((1, 1002001))
    info = create_info(1, 1000)
    raw = RawArray(data, info)
    for tmin in range(0, 1001, 100):
        raw1 = raw.copy().crop(tmin=tmin, tmax=tmin + 2, copy=False)
        assert_equal(raw1[:][0].shape, (1, 2001))
Exemple #5
0
def test_raw_reject():
    """Test raw data getter with annotation reject."""
    sfreq = 100.
    info = create_info(['a', 'b', 'c', 'd', 'e'], sfreq, ch_types='eeg')
    raw = RawArray(np.ones((5, 15000)), info)
    with pytest.warns(RuntimeWarning, match='outside the data range'):
        raw.set_annotations(Annotations([2, 100, 105, 148],
                                        [2, 8, 5, 8], 'BAD'))
    data, times = raw.get_data([0, 1, 3, 4], 100, 11200,  # 1-112 sec
                               'omit', return_times=True)
    bad_times = np.concatenate([np.arange(200, 400),
                                np.arange(10000, 10800),
                                np.arange(10500, 11000)])
    expected_times = np.setdiff1d(np.arange(100, 11200), bad_times) / sfreq
    assert_allclose(times, expected_times)

    # with orig_time and complete overlap
    raw = read_raw_fif(fif_fname)
    raw.set_annotations(Annotations(onset=[1, 4, 5] + raw._first_time,
                                    duration=[1, 3, 1],
                                    description='BAD',
                                    orig_time=raw.info['meas_date']))
    t_stop = 18.
    assert raw.times[-1] > t_stop
    n_stop = int(round(t_stop * raw.info['sfreq']))
    n_drop = int(round(4 * raw.info['sfreq']))
    assert len(raw.times) >= n_stop
    data, times = raw.get_data(range(10), 0, n_stop, 'omit', True)
    assert data.shape == (10, n_stop - n_drop)
    assert times[-1] == raw.times[n_stop - 1]
    assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0])

    data, times = raw.get_data(range(10), 0, n_stop, 'NaN', True)
    assert_array_equal(data.shape, (10, n_stop))
    assert times[-1] == raw.times[n_stop - 1]
    t_1, t_2 = raw.time_as_index([1, 2], use_rounding=True)
    assert np.isnan(data[:, t_1:t_2]).all()  # 1s -2s
    assert not np.isnan(data[:, :t_1].any())
    assert not np.isnan(data[:, t_2:].any())
    assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0])
    assert_array_equal(raw.get_data(), raw[:][0])

    # Test _sync_onset
    times = [10, -88, 190]
    onsets = _sync_onset(raw, times)
    assert_array_almost_equal(onsets, times - raw.first_samp /
                              raw.info['sfreq'])
    assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
Exemple #6
0
def test_append():
    """Test appending raw edf objects using Raw.append"""
    for preload in (True, False):
        raw = read_raw_edf(bdf_path, preload=False)
        raw0 = raw.copy()
        raw1 = raw.copy()
        raw0.append(raw1)
        assert_true(2 * len(raw) == len(raw0))
        assert_allclose(np.tile(raw[:, :][0], (1, 2)), raw0[:, :][0])

    # different types can't combine
    raw = read_raw_edf(bdf_path, preload=True)
    raw0 = raw.copy()
    raw1 = raw.copy()
    raw2 = RawArray(raw[:, :][0], raw.info)
    assert_raises(ValueError, raw.append, raw2)
Exemple #7
0
def test_pick_types_csd():
    """Test pick_types(csd=True)."""
    # info with laplacian/CSD channels at indices 1, 2
    names = ['F1', 'F2', 'C1', 'C2', 'A1', 'A2', 'misc1', 'CSD1']
    info1 = create_info(
        names, 256, ["eeg", "eeg", "eeg", "eeg", "mag", "mag", 'misc', 'csd'])
    raw = RawArray(np.zeros((8, 512)), info1)
    raw.set_montage(make_standard_montage('standard_1020'), verbose='error')
    raw_csd = compute_current_source_density(raw, verbose='error')

    assert_array_equal(pick_types(info1, csd=True), [7])

    # pick from the raw object
    assert raw_csd.copy().pick_types(csd=True).ch_names == [
        'F1', 'F2', 'C1', 'C2', 'CSD1'
    ]
Exemple #8
0
    def _convert_one_session(self, data, mrk, session, trig_offset=0):
        eeg = data[session].x.T * 1e-6
        trig = np.zeros((1, eeg.shape[1]))
        idx = (mrk[session].time - 1) // 5
        trig[0, idx] = mrk[session].event.desc // 16 + trig_offset
        eeg = np.vstack([eeg, trig])
        ch_names = list(data[session].clab) + ['Stim']
        ch_types = ['eeg'] * 30 + ['eog'] * 2 + ['stim']

        montage = make_standard_montage('standard_1005')
        info = create_info(ch_names=ch_names,
                           ch_types=ch_types,
                           sfreq=200.,
                           montage=montage)
        raw = RawArray(data=eeg, info=info, verbose=False)
        return {'run_0': raw}
Exemple #9
0
def test_annotation_epoching():
    """Test that annotations work properly with concatenated edges."""
    # Create data with just a DC component
    data = np.ones((1, 1000))
    info = create_info(1, 1000., 'eeg')
    raw = concatenate_raws([RawArray(data, info) for ii in range(3)])
    events = np.array([[a, 0, 1] for a in [0, 500, 1000, 1500, 2000]])
    epochs = Epochs(raw,
                    events,
                    tmin=0,
                    tmax=0.999,
                    baseline=None,
                    preload=True)  # 1000 samples long
    assert_equal(len(epochs.drop_log), len(events))
    assert_equal(len(epochs), 3)
    assert_equal([0, 2, 4], epochs.selection)
Exemple #10
0
def load_csv_as_raw(filename,
                    sfreq,
                    ch_ind,
                    aux_ind=None,
                    replace_ch_names=None,
                    verbose=1):
    """"""
    ch_ind = copy.deepcopy(ch_ind)
    n_eeg = len(ch_ind)
    if aux_ind is not None:
        n_aux = len(aux_ind)
        ch_ind += aux_ind
    else:
        n_aux = 0

    raw = []
    for fn in filename:
        # Read the file
        data = pd.read_csv(fn)

        # Channel names and types
        ch_names = [list(data.columns)[i] for i in ch_ind] + ['stim']
        print(ch_names)
        ch_types = ['eeg'] * n_eeg + ['misc'] * n_aux + ['stim']

        if replace_ch_names is not None:
            ch_names = [
                c if c not in replace_ch_names.keys() else replace_ch_names[c]
                for c in ch_names
            ]

        # Transpose EEG data and convert from uV to Volts
        data = data.values[:, ch_ind + [-1]].T
        data[:-1] *= 1e-6

        # create MNE object
        info = create_info(ch_names=ch_names,
                           ch_types=ch_types,
                           sfreq=sfreq,
                           verbose=1)
        raw.append(RawArray(data=data, info=info, verbose=verbose))

    raws = concatenate_raws(raw, verbose=verbose)
    montage = make_standard_montage('standard_1005')
    raws.set_montage(montage)

    return raws
Exemple #11
0
def test_filter_auto():
    """Test filter auto parameters."""
    # test that our overlap-add filtering doesn't introduce strange
    # artifacts (from mne_analyze mailing list 2015/06/25)
    N = 300
    sfreq = 100.
    lp = 10.
    sine_freq = 1.
    x = np.ones(N)
    t = np.arange(N) / sfreq
    x += np.sin(2 * np.pi * sine_freq * t)
    x_orig = x.copy()
    for pad in ('reflect_limited', 'reflect', 'edge'):
        for fir_design in ('firwin2', 'firwin'):
            kwargs = dict(fir_design=fir_design, pad=pad)
            x = x_orig.copy()
            x_filt = filter_data(x, sfreq, None, lp, **kwargs)
            assert_array_equal(x, x_orig)
            n_edge = 10
            assert_allclose(x[n_edge:-n_edge],
                            x_filt[n_edge:-n_edge],
                            atol=1e-2)
            assert_array_equal(x_filt,
                               filter_data(x, sfreq, None, lp, None, **kwargs))
            assert_array_equal(x, x_orig)
            assert_array_equal(x_filt, filter_data(x, sfreq, None, lp,
                                                   **kwargs))
            assert_array_equal(x, x_orig)
            assert_array_equal(
                x_filt, filter_data(x, sfreq, None, lp, copy=False, **kwargs))
            assert_array_equal(x, x_filt)

    # degenerate conditions
    pytest.raises(ValueError, filter_data, x, -sfreq, 1, 10)
    pytest.raises(ValueError, filter_data, x, sfreq, 1, sfreq * 0.75)
    with pytest.raises(ValueError, match='Data to be filtered must be real'):
        filter_data(x.astype(np.float32), sfreq, None, 10)
    with pytest.raises(ValueError, match='Data to be filtered must be real'):
        filter_data([1j], 1000., None, 40.)
    with pytest.raises(TypeError, match='instance of ndarray'):
        filter_data('foo', 1000., None, 40.)
    # gh-10258
    raw = RawArray([[0.]], create_info(1, 1000., 'eeg'))
    with pytest.raises(TypeError, match=r'.*copy\(\)\.filter\(\.\.\.\)` in.*'):
        filter_data(raw, 1000., None, 40.)
    with pytest.raises(TypeError, match=r'.*copy\(\)\.notch_filter\(\.\.\..*'):
        notch_filter(raw, 1000., [60.])
Exemple #12
0
    def _get_single_subject_data(self, subject):
        """return data for a single subejct"""

        sessions = {}
        file_path_list = self.data_path(subject)

        for session in range(1, 3):
            data = loadmat(file_path_list[session - 1])

            # Create channel info and montage
            eeg_ch_names = data["EEG_MI_train"][0, 0][8][0]
            ch_names = [elem[0] for elem in eeg_ch_names] + ["stim"]
            ch_types = ["eeg"] * 62 + ["stim"]
            sfreq = data["EEG_MI_train"][0, 0][3][0, 0]
            info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=sfreq)
            montage = make_standard_montage("standard_1005")

            # Create raw_data
            raw_train_data = np.transpose(data["EEG_MI_train"][0, 0][0], (1, 2, 0))
            raw_test_data = np.transpose(data["EEG_MI_test"][0, 0][0], (1, 2, 0))
            raw_data = np.concatenate([raw_train_data, raw_test_data], axis=0)

            # Create raw_event
            train_event_id = data["EEG_MI_train"][0, 0][4].ravel()
            test_event_id = data["EEG_MI_test"][0, 0][4].ravel()
            event_id = np.concatenate([train_event_id, test_event_id], axis=0)
            raw_events = np.zeros((raw_data.shape[0], 1, raw_data.shape[2]))
            raw_events[:, 0, 0] = event_id

            # Zero pad the data
            data = np.concatenate([raw_data, raw_events], axis=1)
            zeroshape = (data.shape[0], data.shape[1], 50)
            data = np.concatenate(
                [np.zeros(zeroshape), data, np.zeros(zeroshape)], axis=2
            )

            # Create RawArray
            raw = RawArray(
                data=np.concatenate(list(data), axis=1), info=info, verbose=False
            )
            raw.set_montage(montage)

            # add the data to sessions
            session_name = "session_{}".format(session)
            sessions[session_name] = {"run_1": raw}

        return sessions
Exemple #13
0
def test_double_export_edf(tmp_path):
    """Test exporting an EDF file multiple times."""
    rng = np.random.RandomState(123456)
    format = 'edf'
    ch_types = [
        'eeg', 'eeg', 'stim', 'ecog', 'ecog', 'seeg', 'eog', 'ecg', 'emg',
        'dbs', 'bio'
    ]
    info = create_info(len(ch_types), sfreq=1000, ch_types=ch_types)
    data = rng.random(size=(len(ch_types), 1000)) * 1e-5

    # include subject info and measurement date
    info['subject_info'] = dict(first_name='mne',
                                last_name='python',
                                birthday=(1992, 1, 20),
                                sex=1,
                                hand=3)
    raw = RawArray(data, info)

    # export once
    temp_fname = tmp_path / f'test.{format}'
    raw.export(temp_fname, add_ch_type=True)
    raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True)

    # export again
    raw_read.load_data()
    raw_read.export(temp_fname, add_ch_type=True, overwrite=True)
    raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True)

    # stim channel should be dropped
    raw.drop_channels('2')

    assert raw.ch_names == raw_read.ch_names
    # only compare the original length, since extra zeros are appended
    orig_raw_len = len(raw)
    assert_array_almost_equal(raw.get_data(),
                              raw_read.get_data()[:, :orig_raw_len],
                              decimal=4)
    assert_allclose(raw.times,
                    raw_read.times[:orig_raw_len],
                    rtol=0,
                    atol=1e-5)

    # check channel types except for 'bio', which loses its type
    orig_ch_types = raw.get_channel_types()
    read_ch_types = raw_read.get_channel_types()
    assert_array_equal(orig_ch_types, read_ch_types)
def test_allow_nan_durations():
    """Deal with "n/a" strings in BIDS events with nan durations."""
    raw = RawArray(data=np.empty([2, 10], dtype=np.float64),
                   info=create_info(ch_names=2, sfreq=1.),
                   first_samp=0)
    raw.set_meas_date(0)

    ons = [1, 2., 15., 17.]
    dus = [np.nan, 1., 0.5, np.nan]
    descriptions = ['A'] * 4
    onsets = np.asarray(ons, dtype=float)
    durations = np.asarray(dus, dtype=float)
    annot = mne.Annotations(onset=onsets,
                            duration=durations,
                            description=descriptions)
    with pytest.warns(RuntimeWarning, match='Omitted 2 annotation'):
        raw.set_annotations(annot)
Exemple #15
0
def toMNE(X, y=None):
    """Tranform array into MNE for epoching."""
    ch_names = deepcopy(getChannelNames())
    montage = read_montage('standard_1005', ch_names)
    ch_type = ['eeg']*len(ch_names)
    data = X.T
    if y is not None:
        y = y.transpose()
        ch_type.extend(['stim']*N_EVENTS)
        event_names = getEventNames()
        ch_names.extend(event_names)
        # concatenate event file and data
        data = np.concatenate((data, y))
    info = create_info(ch_names, sfreq=128.0, ch_types=ch_type,
                       montage=montage)
    raw = RawArray(data, info, verbose=False)
    return raw
Exemple #16
0
def fc_aec(data, sfreq, freq_bands):
    """
    
    data is  ROI x TIME
    freq bands is a dict of (lower,upper) freq tuples

    using bits from 
    https://martinos.org/mne/dev/auto_tutorials/plot_modifying_data_inplace.html
    
    """

    # N rois
    nr = data.shape[0]

    # Convert time series data to MNE raw datatype so we can use filter and hilbert functions
    ch_names = [str(s) for s in range(0, nr)]
    ch_types = ['eeg' for _ in ch_names]
    info = create_info(ch_names, sfreq, ch_types=ch_types)
    raw = RawArray(data.copy(), info)

    aecs = {}

    for freq_band, (lfreq, hfreq) in freq_bands.items():

        # Filter
        raw_band = raw.copy()
        raw_band.filter(lfreq,
                        hfreq,
                        l_trans_bandwidth=2.,
                        h_trans_bandwidth=2.,
                        fir_design='firwin')
        raw_hilb = raw_band.copy()

        # Compute hilbert transform
        hilb_picks = pick_types(raw_band.info, meg=False, eeg=True)
        raw_hilb.apply_hilbert(hilb_picks)

        # Take the amplitude and phase
        raw_amp = raw_hilb.copy()
        raw_amp.apply_function(np.abs, hilb_picks)
        raw_phase = raw_hilb.copy()
        raw_phase.apply_function(np.angle, hilb_picks)

        aecs[freq_band] = raw_amp.to_data_frame().corr().values

    return aecs
Exemple #17
0
 def montageConversion(self):
     info = create_info(ch_names=EDF.montageConversionChannels,
                        ch_types='eeg',
                        sfreq=EDF.sfreq)
     data = [
         self.raw.get_data(Channel.FP1)[0] -
         self.raw.get_data(Channel.F7)[0],
         self.raw.get_data(Channel.F7)[0] -
         self.raw.get_data(Channel.T3)[0],
         self.raw.get_data(Channel.T3)[0] -
         self.raw.get_data(Channel.T5)[0],
         self.raw.get_data(Channel.T5)[0] -
         self.raw.get_data(Channel.O1)[0],
         self.raw.get_data(Channel.FP2)[0] -
         self.raw.get_data(Channel.F8)[0],
         self.raw.get_data(Channel.F8)[0] -
         self.raw.get_data(Channel.T4)[0],
         self.raw.get_data(Channel.T4)[0] -
         self.raw.get_data(Channel.T6)[0],
         self.raw.get_data(Channel.T6)[0] -
         self.raw.get_data(Channel.O2)[0],
         self.raw.get_data(Channel.T3)[0] -
         self.raw.get_data(Channel.C3)[0],
         self.raw.get_data(Channel.C3)[0] -
         self.raw.get_data(Channel.CZ)[0],
         self.raw.get_data(Channel.CZ)[0] -
         self.raw.get_data(Channel.C4)[0],
         self.raw.get_data(Channel.C4)[0] -
         self.raw.get_data(Channel.T4)[0],
         self.raw.get_data(Channel.FP1)[0] -
         self.raw.get_data(Channel.F3)[0],
         self.raw.get_data(Channel.F3)[0] -
         self.raw.get_data(Channel.C3)[0],
         self.raw.get_data(Channel.C3)[0] -
         self.raw.get_data(Channel.P3)[0],
         self.raw.get_data(Channel.P3)[0] -
         self.raw.get_data(Channel.O1)[0],
         self.raw.get_data(Channel.FP2)[0] -
         self.raw.get_data(Channel.F4)[0],
         self.raw.get_data(Channel.F4)[0] -
         self.raw.get_data(Channel.C4)[0],
         self.raw.get_data(Channel.C4)[0] -
         self.raw.get_data(Channel.P4)[0],
         self.raw.get_data(Channel.P4)[0] - self.raw.get_data(Channel.O2)[0]
     ]
     return RawArray(data, info)
Exemple #18
0
def test_bipolar_combinations():
    """Test bipolar channel generation."""
    ch_names = ['CH' + str(ni + 1) for ni in range(10)]
    info = create_info(ch_names=ch_names,
                       sfreq=1000.,
                       ch_types=['eeg'] * len(ch_names))
    raw_data = np.random.randn(len(ch_names), 1000)
    raw = RawArray(raw_data, info)

    def _check_bipolar(raw_test, ch_a, ch_b):
        picks = [raw_test.ch_names.index(ch_a + '-' + ch_b)]
        get_data_res = raw_test.get_data(picks=picks)[0, :]
        manual_a = raw_data[ch_names.index(ch_a), :]
        manual_b = raw_data[ch_names.index(ch_b), :]
        assert_array_equal(get_data_res, manual_a - manual_b)

    # test classic EOG/ECG bipolar reference (only two channels per pair).
    raw_test = set_bipolar_reference(raw, ['CH2'], ['CH1'], copy=True)
    _check_bipolar(raw_test, 'CH2', 'CH1')

    # test all combinations.
    a_channels, b_channels = zip(*itertools.combinations(ch_names, 2))
    a_channels, b_channels = list(a_channels), list(b_channels)
    raw_test = set_bipolar_reference(raw, a_channels, b_channels, copy=True)
    for ch_a, ch_b in zip(a_channels, b_channels):
        _check_bipolar(raw_test, ch_a, ch_b)
    # check if reference channels have been dropped.
    assert (len(raw_test.ch_names) == len(a_channels))

    raw_test = set_bipolar_reference(raw,
                                     a_channels,
                                     b_channels,
                                     drop_refs=False,
                                     copy=True)
    # check if reference channels have been kept correctly.
    assert (len(raw_test.ch_names) == len(a_channels) + len(ch_names))
    for idx, ch_label in enumerate(ch_names):
        manual_ch = raw_data[idx, :]
        assert_array_equal(
            raw_test._data[raw_test.ch_names.index(ch_label), :], manual_ch)

    # test bipolars with a channel in both list (anode & cathode).
    raw_test = set_bipolar_reference(raw, ['CH2', 'CH1'], ['CH1', 'CH2'],
                                     copy=True)
    _check_bipolar(raw_test, 'CH2', 'CH1')
    _check_bipolar(raw_test, 'CH1', 'CH2')
Exemple #19
0
def trigs_from_raw(raw_data, raw_trigs):
    """

        Designed for yokogawa system.
    """
    from mne.io import Raw, RawArray
    import os.path as op
    from warnings import warn

    if isinstance(raw_data, str):
        if op.exists(raw_data):
            raw_data = Raw(raw_data, preload=True, verbose=False)
        else:
            raise ValueError(
                'Could not find path to raw data, {}'.format(raw_data))
    elif not isinstance(raw_data, Raw):
        raise TypeError('raw_data must be either an instance of Raw or path'
                        'to raw file.')

    if isinstance(raw_trigs, str):
        if op.exists(raw_trigs):
            raw_trigs = Raw(raw_trigs, preload=True, verbose=False)
        else:
            raise ValueError(
                'Could not find path to raw data, {}'.format(raw_trigs))
    elif not isinstance(raw_trigs, Raw):
        raise TypeError('raw_trigs must be either an instance of Raw or path'
                        'to raw file.')

    # ensure that the sampling rate is the same across both
    if raw_data.info['sfreq'] != raw_trigs.info['sfreq']:
        warn(('Sampling rates were not the same, data is {} but triggers'
              ' are {}. Resampling to the lower option now').format(
                  raw_data.info['sfreq'], raw_trigs.info['sfreq']))
        if raw_trigs.info['sfreq'] > raw_data.info['sfreq']:
            raw_trigs.resample(raw_data.info['sfreq'])
        else:
            raw_data.resample(raw_trigs.info['sfreq'])

    # get the cleaned data and the raw_data with extra channels
    cldat = raw_data[:][0]
    rwdat = raw_trigs[:][0]
    # put the cleaned data into the raw data
    rwdat[:157, :] = cldat[:157, :].copy()
    # make new raw array with info from the more informative
    return RawArray(rwdat, raw_trigs.info, first_samp=0, verbose=False)
Exemple #20
0
def _convert_run_p300_sl(run, verbose=None):
    """Convert one p300 run from santa lucia file format."""
    montage = make_standard_montage('standard_1005')
    eeg_data = 1e-6 * run.X
    sfreq = 256
    ch_names = list(run.channels) + ['Target stim', 'Flash stim']
    ch_types = ['eeg'] * len(run.channels) + ['stim'] * 2

    flash_stim = run.y_stim
    flash_stim[flash_stim > 0] += 2
    eeg_data = np.c_[eeg_data, run.y, flash_stim]
    event_id = {ev: (ii + 1) for ii, ev in enumerate(run.classes)}
    event_id.update({ev: (ii + 3) for ii, ev in enumerate(run.classes_stim)})
    info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=sfreq)
    raw = RawArray(data=eeg_data.T, info=info, verbose=verbose)
    raw.set_montage(montage)
    return raw, event_id
Exemple #21
0
def add_channels(inst, data, ch_names, ch_types):
    from mne.io import _BaseRaw, RawArray
    from mne.epochs import _BaseEpochs, EpochsArray
    from mne import create_info
    if 'meg' in ch_types or 'eeg' in ch_types:
        return NotImplementedError('Can only add misc, stim and ieeg channels')
    info = create_info(ch_names=ch_names, sfreq=inst.info['sfreq'],
                       ch_types=ch_types)
    if isinstance(inst, _BaseRaw):
        for key in ('buffer_size_sec', 'filename'):
            info[key] = inst.info[key]
        new_inst = RawArray(data, info=info, first_samp=inst._first_samps[0])
    elif isinstance(inst, _BaseEpochs):
        new_inst = EpochsArray(data, info=info)
    else:
        raise ValueError('unknown inst type')
    return inst.add_channels([new_inst], copy=True)
Exemple #22
0
def get_session_erp_epochs(session_datas, markers_const, tmin=-0.1, tmax=0.8):
    event_id = {}
    counter = 1
    for marker in markers_const:
        event_id[marker] = counter
        counter += 1
    epochs_list = []
    for session_data in session_datas:
        df = get_session_df(session_data)
        channels = df.columns.tolist()
        n_channel = len(channels)

        info = create_info(ch_names=channels + ["stim"],
                           ch_types=["eeg"] * n_channel + ["stim"],
                           sfreq=samplingRate)

        markers = get_markers(session_data)

        df["stim"] = [0] * len(df)
        for marker in markers:
            marker_timestamp = marker["timestamp"]
            pandas_timestamp = df.index[df.index.get_loc(marker_timestamp,
                                                         method='nearest')]
            if marker["label"] in markers_const:
                df.at[pandas_timestamp, "stim"] = event_id[marker["label"]]

        nparr = df.to_numpy().T
        raw = RawArray(data=nparr, info=info, verbose=False)

        events = find_events(raw)

        # Create an MNE Epochs object representing all the epochs around stimulus presentation
        epochs = Epochs(raw,
                        events=events,
                        event_id=event_id,
                        tmin=tmin,
                        tmax=tmax,
                        baseline=None,
                        preload=True,
                        verbose=False)
        print('sample drop %: ', (1 - len(epochs.events) / len(events)) * 100)
        epochs_list.append(epochs)
    concat_epochs = concatenate_epochs(epochs_list)

    return concat_epochs
Exemple #23
0
def _create_annotation_based_on_descr(description,
                                      annotation_start_sampl=0,
                                      duration=0,
                                      orig_time=0):
    """Create a raw object with annotations from descriptions.

    The returning raw object contains as many annotations as description given.
    All starting at `annotation_start_sampl`.
    """
    # create dummy raw
    raw = RawArray(data=np.empty([10, 10], dtype=np.float64),
                   info=create_info(ch_names=10, sfreq=1000.),
                   first_samp=0)
    raw.info['meas_date'] = 0

    # create dummy annotations based on the descriptions
    onset = raw.times[annotation_start_sampl]
    onset_matching_desc = np.full_like(description, onset, dtype=type(onset))
    duration_matching_desc = np.full_like(description,
                                          duration,
                                          dtype=type(duration))
    annot = Annotations(description=description,
                        onset=onset_matching_desc,
                        duration=duration_matching_desc,
                        orig_time=orig_time)

    if duration != 0:
        with pytest.warns(RuntimeWarning, match='Limited.*expanding outside'):
            # duration 0.1s is larger than the raw data expand
            raw.set_annotations(annot)
    else:
        raw.set_annotations(annot)

    # Make sure that set_annotations(annot) works
    assert all(raw.annotations.onset == onset)
    if duration != 0:
        expected_duration = (len(raw.times) / raw.info['sfreq']) - onset
    else:
        expected_duration = 0
    _duration = raw.annotations.duration[0]
    assert _duration == approx(expected_duration)
    assert all(raw.annotations.duration == _duration)
    assert all(raw.annotations.description == description)

    return raw
Exemple #24
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""
        fname = self.data_path(subject)

        data = loadmat(fname,
                       squeeze_me=True,
                       struct_as_record=False,
                       verify_compressed_data_integrity=False)['eeg']

        eeg_ch_names = [
            'Fp1', 'AF7', 'AF3', 'F1', 'F3', 'F5', 'F7', 'FT7', 'FC5', 'FC3',
            'FC1', 'C1', 'C3', 'C5', 'T7', 'TP7', 'CP5', 'CP3', 'CP1', 'P1',
            'P3', 'P5', 'P7', 'P9', 'PO7', 'PO3', 'O1', 'Iz', 'Oz', 'POz',
            'Pz', 'CPz', 'FPz', 'FP2', 'AF8', 'AF4', 'AFz', 'Fz', 'F2', 'F4',
            'F6', 'F8', 'FT8', 'FC6', 'FC4', 'FC2', 'FCz', 'Cz', 'C2', 'C4',
            'C6', 'T8', 'TP8', 'CP6', 'CP4', 'CP2', 'P2', 'P4', 'P6', 'P8',
            'P10', 'PO8', 'PO4', 'O2'
        ]
        emg_ch_names = ['EMG1', 'EMG2', 'EMG3', 'EMG4']
        ch_names = eeg_ch_names + emg_ch_names + ['Stim']
        ch_types = ['eeg'] * 64 + ['emg'] * 4 + ['stim']
        montage = read_montage('standard_1005')
        imagery_left = data.imagery_left - \
            data.imagery_left.mean(axis=1, keepdims=True)
        imagery_right = data.imagery_right - \
            data.imagery_right.mean(axis=1, keepdims=True)

        eeg_data_l = np.vstack([imagery_left * 1e-6, data.imagery_event])
        eeg_data_r = np.vstack([imagery_right * 1e-6, data.imagery_event * 2])

        # trials are already non continuous. edge artifact can appears but
        # are likely to be present during rest / inter-trial activity
        eeg_data = np.hstack(
            [eeg_data_l,
             np.zeros((eeg_data_l.shape[0], 500)), eeg_data_r])
        log.warning("Trials demeaned and stacked with zero buffer to create "
                    "continuous data -- edge effects present")

        info = create_info(ch_names=ch_names,
                           ch_types=ch_types,
                           sfreq=data.srate,
                           montage=montage)
        raw = RawArray(data=eeg_data, info=info, verbose=False)

        return {'session_0': {'run_0': raw}}
Exemple #25
0
def load_raw_eeg(filename):
    data = pd.read_csv(filename)
    ch_names = list(data.columns[1:])

    data = 1e-7 * np.array(data[ch_names]).T
    ch_types = ['eeg'] * len(ch_names)
    montage = read_montage('standard_1020', ch_names)

    ch_locations = []
    for i in range(len(ch_names)):
        ch_name = ch_names[i]
        ch_location = montage.pos[i]
        ch_locations.append(
            [ch_name, ch_location[0], ch_location[1], ch_location[2]])

    info = create_info(ch_names, sfreq=512, ch_types=ch_types, montage=montage)

    return RawArray(data, info, verbose=False), ch_locations
Exemple #26
0
def test_equalize_channels():
    """Test equalizing channels and their ordering."""
    # This function only tests the generic functionality of equalize_channels.
    # Additional tests for each instance type are included in the accompanying
    # test suite for each type.
    pytest.raises(TypeError,
                  equalize_channels, ['foo', 'bar'],
                  match='Instances to be modified must be an instance of')

    raw = RawArray([[1.], [2.], [3.], [4.]],
                   create_info(['CH1', 'CH2', 'CH3', 'CH4'], sfreq=1.))
    epochs = EpochsArray([[[1.], [2.], [3.]]],
                         create_info(['CH5', 'CH2', 'CH1'], sfreq=1.))
    cov = make_ad_hoc_cov(
        create_info(['CH2', 'CH1', 'CH8'], sfreq=1., ch_types='eeg'))
    cov['bads'] = ['CH1']
    ave = EvokedArray([[1.], [2.]], create_info(['CH1', 'CH2'], sfreq=1.))

    raw2, epochs2, cov2, ave2 = equalize_channels([raw, epochs, cov, ave],
                                                  copy=True)

    # The Raw object was the first in the list, so should have been used as
    # template for the ordering of the channels. No bad channels should have
    # been dropped.
    assert raw2.ch_names == ['CH1', 'CH2']
    assert_array_equal(raw2.get_data(), [[1.], [2.]])
    assert epochs2.ch_names == ['CH1', 'CH2']
    assert_array_equal(epochs2.get_data(), [[[3.], [2.]]])
    assert cov2.ch_names == ['CH1', 'CH2']
    assert cov2['bads'] == cov['bads']
    assert ave2.ch_names == ave.ch_names
    assert_array_equal(ave2.data, ave.data)

    # All objects should have been copied, except for the Evoked object which
    # did not have to be touched.
    assert raw is not raw2
    assert epochs is not epochs2
    assert cov is not cov2
    assert ave is ave2

    # Test in-place operation
    raw2, epochs2 = equalize_channels([raw, epochs], copy=False)
    assert raw is raw2
    assert epochs is epochs2
def get_noisy_channels(epochs: Epochs, with_ransac: bool = False) -> list:
    """
    Find bad channels using a range of methods as described in the PREP pipeline.
    Note that low-frequency trends should be removed from the EEG signal prior
    to bad channel detection.
    Read the documentation for further information about the methods:
    https://pyprep.readthedocs.io/en/latest/generated/pyprep.NoisyChannels.html#pyprep.NoisyChannels

    References
    ----------
    Bigdely-Shamlo, N., Mullen, T., Kothe, C., Su, K. M., Robbins, K. A. (2015).
    The PREP pipeline: standardized preprocessing for large-scale EEG analysis.
    Frontiers in Neuroinformatics, 9, 16.

    Parameters
    ----------
    epochs: Epochs object to use for bad channels detection
    with_ransac: whether RANSAC should be used for bad channel detection,
    in addition to the other methods.

    Returns
    -------
    list of bad channels names detected
    """
    # transform epochs to continuous data
    # to shape of (n_channels, n_epochs, n_times)
    data = np.transpose(epochs.get_data(), (1, 0, 2))
    # reshape to (n_channels, n_epochs * n_times) continuous data
    data = data.reshape((data.shape[0], data.shape[1] * data.shape[2]))
    # create Raw object from continuous data
    raw = RawArray(data=data, info=epochs.info)

    noisy_channels = NoisyChannels(raw=raw, do_detrend=False, random_state=42)
    noisy_channels.find_all_bads(ransac=with_ransac)

    bads = noisy_channels.get_bads(verbose=False)
    if bads:
        logger.info("\nNoisyChannels REPORT\n"
                    "------------------------"
                    f"\n{np.round(len(bads) / len(epochs.ch_names), 2) * 100}%"
                    f" of the channels were detected as noisy."
                    f'\n({len(bads)}) channels: {", ".join(bads)}')
    return bads
Exemple #28
0
def test_annotations():
    """Test annotation class."""
    raw = read_raw_fif(fif_fname, add_eeg_ref=False)
    onset = np.array(range(10))
    duration = np.ones(10)
    description = np.repeat('test', 10)
    dt = datetime.utcnow()
    meas_date = raw.info['meas_date']
    # Test time shifts.
    for orig_time in [None, dt, meas_date[0], meas_date]:
        annot = Annotations(onset, duration, description, orig_time)

    assert_raises(ValueError, Annotations, onset, duration, description[:9])
    assert_raises(ValueError, Annotations, [onset, 1], duration, description)
    assert_raises(ValueError, Annotations, onset, [duration, 1], description)

    # Test combining annotations with concatenate_raws
    raw2 = raw.copy()
    orig_time = (meas_date[0] + meas_date[1] * 0.000001 +
                 raw2.first_samp / raw2.info['sfreq'])
    annot = Annotations(onset, duration, description, orig_time)
    raw2.annotations = annot
    assert_array_equal(raw2.annotations.onset, onset)
    concatenate_raws([raw, raw2])
    assert_array_almost_equal(onset + 20., raw.annotations.onset, decimal=2)
    assert_array_equal(annot.duration, raw.annotations.duration)
    assert_array_equal(raw.annotations.description, np.repeat('test', 10))

    # Test combining with RawArray and orig_times
    data = np.random.randn(2, 1000) * 10e-12
    sfreq = 100.
    info = create_info(ch_names=['MEG1', 'MEG2'],
                       ch_types=['grad'] * 2,
                       sfreq=sfreq)
    info['meas_date'] = 0
    raws = []
    for i, fs in enumerate([1000, 100, 12]):
        raw = RawArray(data.copy(), info, first_samp=fs)
        ants = Annotations([1., 2.], [.5, .5], 'x', fs / sfreq)
        raw.annotations = ants
        raws.append(raw)
    raw = concatenate_raws(raws)
    assert_array_equal(raw.annotations.onset, [1., 2., 11., 12., 21., 22.])
Exemple #29
0
def test_set_montage_with_mismatching_ch_names():
    """Test setting a DigMontage with mismatching ch_names."""
    raw = read_raw_fif(fif_fname)
    montage = make_standard_montage('mgh60')

    # 'EEG 001' and 'EEG001' won't match
    missing_err = '60 channel positions not present'
    with pytest.raises(ValueError, match=missing_err):
        raw.set_montage(montage)

    montage.ch_names = [  # modify the names in place
        name.replace('EEG', 'EEG ') for name in montage.ch_names
    ]
    raw.set_montage(montage)  # does not raise

    # Case sensitivity
    raw.rename_channels(lambda x: x.lower())
    with pytest.raises(ValueError, match=missing_err):
        raw.set_montage(montage)
    # should work
    raw.set_montage(montage, match_case=False)
    raw.rename_channels(lambda x: x.upper())  # restore
    assert 'EEG 001' in raw.ch_names and 'eeg 001' not in raw.ch_names
    raw.rename_channels({'EEG 002': 'eeg 001'})
    assert 'EEG 001' in raw.ch_names and 'eeg 001' in raw.ch_names
    raw.set_channel_types({'eeg 001': 'misc'})
    raw.set_montage(montage)
    raw.set_channel_types({'eeg 001': 'eeg'})
    with pytest.raises(ValueError, match='1 channel position not present'):
        raw.set_montage(montage)
    with pytest.raises(ValueError, match='match_case=False as 1 channel name'):
        raw.set_montage(montage, match_case=False)
    raw = RawArray(np.zeros((1, 1000)), create_info(['EEG 001'], 1000., 'eeg'))
    mon = make_dig_montage({
        'EEG 001': np.zeros(3),
        'eeg 001': np.zeros(3)
    },
                           nasion=[0, 1., 0],
                           rpa=[1., 0, 0],
                           lpa=[-1., 0, 0])
    raw.set_montage(mon)
    with pytest.raises(ValueError, match='match_case=False as 1 montage name'):
        raw.set_montage(mon, match_case=False)
Exemple #30
0
def test_bad_channels(method, allow_ref_meg):
    """Test exception when unsupported channels are used."""
    _skip_check_picard(method)
    chs = [i for i in _kind_dict]
    info = create_info(len(chs), 500, chs)
    rng = np.random.RandomState(0)
    data = rng.rand(len(chs), 50)
    raw = RawArray(data, info)
    data = rng.rand(100, len(chs), 50)
    epochs = EpochsArray(data, info)

    n_components = 0.9
    data_chs = list(_DATA_CH_TYPES_SPLIT + ('eog', ))
    if allow_ref_meg:
        data_chs.append('ref_meg')
    chs_bad = list(set(chs) - set(data_chs))
    ica = ICA(n_components=n_components,
              method=method,
              allow_ref_meg=allow_ref_meg)
    for inst in [raw, epochs]:
        for ch in chs_bad:
            if allow_ref_meg:
                # Test case for only bad channels
                picks_bad1 = pick_types(inst.info,
                                        meg=False,
                                        ref_meg=False,
                                        **{str(ch): True})
                # Test case for good and bad channels
                picks_bad2 = pick_types(inst.info,
                                        meg=True,
                                        ref_meg=True,
                                        **{str(ch): True})
            else:
                # Test case for only bad channels
                picks_bad1 = pick_types(inst.info,
                                        meg=False,
                                        **{str(ch): True})
                # Test case for good and bad channels
                picks_bad2 = pick_types(inst.info, meg=True, **{str(ch): True})

            pytest.raises(ValueError, ica.fit, inst, picks=picks_bad1)
            pytest.raises(ValueError, ica.fit, inst, picks=picks_bad2)
        pytest.raises(ValueError, ica.fit, inst, picks=[])