Пример #1
0
def test_raw_index_as_time():
    """ Test index as time conversion"""
    raw = Raw(fif_fname, preload=True)
    t0 = raw.index_as_time([0], True)[0]
    t1 = raw.index_as_time([100], False)[0]
    t2 = raw.index_as_time([100], True)[0]
    assert_true((t2 - t1) == t0)
Пример #2
0
def test_cov_estimation_on_raw_segment():
    """Test estimation from raw on continuous recordings (typically empty room)
    """
    raw = Raw(raw_fname, preload=False)
    cov = compute_raw_data_covariance(raw)
    cov_mne = read_cov(erm_cov_fname)
    assert_true(cov_mne.ch_names == cov.ch_names)
    assert_true(linalg.norm(cov.data - cov_mne.data, ord='fro')
                / linalg.norm(cov.data, ord='fro') < 1e-4)

    # test IO when computation done in Python
    cov.save(op.join(tempdir, 'test-cov.fif'))  # test saving
    cov_read = read_cov(op.join(tempdir, 'test-cov.fif'))
    assert_true(cov_read.ch_names == cov.ch_names)
    assert_true(cov_read.nfree == cov.nfree)
    assert_array_almost_equal(cov.data, cov_read.data)

    # test with a subset of channels
    picks = pick_channels(raw.ch_names, include=raw.ch_names[:5])
    cov = compute_raw_data_covariance(raw, picks=picks)
    assert_true(cov_mne.ch_names[:5] == cov.ch_names)
    assert_true(linalg.norm(cov.data - cov_mne.data[picks][:, picks],
                ord='fro') / linalg.norm(cov.data, ord='fro') < 1e-4)
    # make sure we get a warning with too short a segment
    raw_2 = raw.crop(0, 1)
    with warnings.catch_warnings(record=True) as w:
        cov = compute_raw_data_covariance(raw_2)
        assert_true(len(w) == 1)
Пример #3
0
def test_preload_modify():
    """ Test preloading and modifying data
    """
    for preload in [False, True, "memmap.dat"]:
        raw = Raw(fif_fname, preload=preload)

        nsamp = raw.last_samp - raw.first_samp + 1
        picks = pick_types(raw.info, meg="grad", exclude="bads")

        data = np.random.randn(len(picks), nsamp / 2)

        try:
            raw[picks, : nsamp / 2] = data
        except RuntimeError as err:
            if not preload:
                continue
            else:
                raise err

        tmp_fname = op.join(tempdir, "raw.fif")
        raw.save(tmp_fname, overwrite=True)

        raw_new = Raw(tmp_fname)
        data_new, _ = raw_new[picks, : nsamp / 2]

        assert_allclose(data, data_new)
Пример #4
0
def test_preload_modify():
    """ Test preloading and modifying data
    """
    for preload in [False, True, 'memmap.dat']:
        raw = Raw(fif_fname, preload=preload)

        nsamp = raw.last_samp - raw.first_samp + 1
        picks = pick_types(raw.info, meg='grad', exclude='bads')

        data = np.random.randn(len(picks), nsamp / 2)

        try:
            raw[picks, :nsamp / 2] = data
        except RuntimeError as err:
            if not preload:
                continue
            else:
                raise err

        tmp_fname = op.join(tempdir, 'raw.fif')
        raw.save(tmp_fname)

        raw_new = Raw(tmp_fname)
        data_new, _ = raw_new[picks, :nsamp / 2]

        assert_array_almost_equal(data, data_new)
Пример #5
0
def test_io_complex():
    """Test IO with complex data types
    """
    dtypes = [np.complex64, np.complex128]

    raw = Raw(fif_fname, preload=True)
    picks = np.arange(5)
    start, stop = raw.time_as_index([0, 5])

    data_orig, _ = raw[picks, start:stop]

    for di, dtype in enumerate(dtypes):
        imag_rand = np.array(1j * np.random.randn(data_orig.shape[0],
                             data_orig.shape[1]), dtype)

        raw_cp = raw.copy()
        raw_cp._data = np.array(raw_cp._data, dtype)
        raw_cp._data[picks, start:stop] += imag_rand
        # this should throw an error because it's complex
        with warnings.catch_warnings(record=True) as w:
            raw_cp.save(op.join(tempdir, 'raw.fif'), picks, tmin=0, tmax=5)
            # warning only gets thrown on first instance
            assert_equal(len(w), 1 if di == 0 else 0)

        raw2 = Raw(op.join(tempdir, 'raw.fif'))
        raw2_data, _ = raw2[picks, :]
        n_samp = raw2_data.shape[1]
        assert_array_almost_equal(raw2_data[:, :n_samp],
                                  raw_cp._data[picks, :n_samp])
        # with preloading
        raw2 = Raw(op.join(tempdir, 'raw.fif'), preload=True)
        raw2_data, _ = raw2[picks, :]
        n_samp = raw2_data.shape[1]
        assert_array_almost_equal(raw2_data[:, :n_samp],
                                  raw_cp._data[picks, :n_samp])
Пример #6
0
def _get_data():
    # Read raw data
    raw = Raw(raw_fname)
    raw.info['bads'] = ['MEG 2443', 'EEG 053']  # 2 bads channels

    # Set picks
    picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False, eog=False,
                                stim=False, exclude='bads')

    # Read several epochs
    event_id, tmin, tmax = 1, -0.2, 0.5
    events = mne.read_events(event_fname)[0:100]
    epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
                        picks=picks, baseline=(None, 0), preload=True,
                        reject=dict(grad=4000e-13, mag=4e-12))

    # Create an epochs object with one epoch and one channel of artificial data
    event_id, tmin, tmax = 1, 0.0, 1.0
    epochs_sin = mne.Epochs(raw, events[0:5], event_id, tmin, tmax, proj=True,
                            picks=[0], baseline=(None, 0), preload=True,
                            reject=dict(grad=4000e-13))
    freq = 10
    epochs_sin._data = np.sin(2 * np.pi * freq
                              * epochs_sin.times)[None, None, :]
    return epochs, epochs_sin
Пример #7
0
def test_copy_append():
    """Test raw copying and appending combinations
    """
    raw = Raw(fif_fname, preload=True).copy()
    raw_full = Raw(fif_fname)
    raw_full.append(raw)
    data = raw_full[:, :][0]
    assert_true(data.shape[1] == 2 * raw._data.shape[1])
Пример #8
0
def test_as_data_frame():
    """Test Pandas exporter"""
    raw = Raw(fif_fname, preload=True)
    df = raw.as_data_frame()
    assert_true((df.columns == raw.ch_names).all())
    df = raw.as_data_frame(use_time_index=False)
    assert_true('time' in df.columns)
    assert_array_equal(df.values[:, 1], raw._data[0] * 1e13)
    assert_array_equal(df.values[:, 3], raw._data[2] * 1e15)
Пример #9
0
def test_drop_channels_mixin():
    """Test channels-dropping functionality
    """
    raw = Raw(fif_fname, preload=True)
    drop_ch = raw.ch_names[:3]
    ch_names = raw.ch_names[3:]
    raw.drop_channels(drop_ch)
    assert_equal(ch_names, raw.ch_names)
    assert_equal(len(ch_names), raw._data.shape[0])
Пример #10
0
def test_hilbert():
    """ Test computation of analytic signal using hilbert """
    raw = Raw(fif_fname, preload=True)
    picks_meg = pick_types(raw.info, meg=True, exclude='bads')
    picks = picks_meg[:4]

    raw2 = raw.copy()
    raw.apply_hilbert(picks)
    raw2.apply_hilbert(picks, envelope=True, n_jobs=2)

    env = np.abs(raw._data[picks, :])
    assert_array_almost_equal(env, raw2._data[picks, :])
Пример #11
0
def test_hilbert():
    """ Test computation of analytic signal using hilbert """
    raw = Raw(fif_fname, preload=True)
    picks_meg = pick_types(raw.info, meg=True, exclude="bads")
    picks = picks_meg[:4]

    raw2 = raw.copy()
    raw.apply_hilbert(picks)
    raw2.apply_hilbert(picks, envelope=True, n_jobs=2)

    env = np.abs(raw._data[picks, :])
    assert_allclose(env, raw2._data[picks, :], rtol=1e-2, atol=1e-13)
Пример #12
0
def test_equalize_channels():
    """Test equalization of channels
    """
    raw1 = Raw(fif_fname)

    raw2 = raw1.copy()
    ch_names = raw1.ch_names[2:]
    raw1.drop_channels(raw1.ch_names[:1])
    raw2.drop_channels(raw2.ch_names[1:2])
    my_comparison = [raw1, raw2]
    equalize_channels(my_comparison)
    for e in my_comparison:
        assert_equal(ch_names, e.ch_names)
Пример #13
0
def test_raw_to_nitime():
    """ Test nitime export """
    raw = Raw(fif_fname, preload=True)
    picks_meg = pick_types(raw.info, meg=True, exclude='bads')
    picks = picks_meg[:4]
    raw_ts = raw.to_nitime(picks=picks)
    assert_true(raw_ts.data.shape[0] == len(picks))

    raw = Raw(fif_fname, preload=False)
    picks_meg = pick_types(raw.info, meg=True, exclude='bads')
    picks = picks_meg[:4]
    raw_ts = raw.to_nitime(picks=picks)
    assert_true(raw_ts.data.shape[0] == len(picks))

    raw = Raw(fif_fname, preload=True)
    picks_meg = pick_types(raw.info, meg=True, exclude='bads')
    picks = picks_meg[:4]
    raw_ts = raw.to_nitime(picks=picks, copy=False)
    assert_true(raw_ts.data.shape[0] == len(picks))

    raw = Raw(fif_fname, preload=False)
    picks_meg = pick_types(raw.info, meg=True, exclude='bads')
    picks = picks_meg[:4]
    raw_ts = raw.to_nitime(picks=picks, copy=False)
    assert_true(raw_ts.data.shape[0] == len(picks))
Пример #14
0
def test_raw_copy():
    """ Test Raw copy"""
    raw = Raw(fif_fname, preload=True)
    data, _ = raw[:, :]
    copied = raw.copy()
    copied_data, _ = copied[:, :]
    assert_array_equal(data, copied_data)
    assert_equal(sorted(raw.__dict__.keys()), sorted(copied.__dict__.keys()))

    raw = Raw(fif_fname, preload=False)
    data, _ = raw[:, :]
    copied = raw.copy()
    copied_data, _ = copied[:, :]
    assert_array_equal(data, copied_data)
    assert_equal(sorted(raw.__dict__.keys()), sorted(copied.__dict__.keys()))
Пример #15
0
def test_compute_proj_raw():
    """Test SSP computation on raw"""
    # Test that the raw projectors work
    raw_time = 2.5  # Do shorter amount for speed
    raw = Raw(raw_fname, preload=True).crop(0, raw_time, False)
    for ii in (0.25, 0.5, 1, 2):
        with warnings.catch_warnings(True) as w:
            projs = compute_proj_raw(raw, duration=ii - 0.1, stop=raw_time,
                                     n_grad=1, n_mag=1, n_eeg=0)
            assert_true(len(w) == 1)

        # test that you can compute the projection matrix
        projs = activate_proj(projs)
        proj, nproj, U = make_projector(projs, raw.ch_names, bads=[])

        assert_true(nproj == 2)
        assert_true(U.shape[1] == 2)

        # test that you can save them
        raw.info['projs'] += projs
        raw.save(op.join(tempdir, 'foo_%d_raw.fif' % ii))

    # Test that purely continuous (no duration) raw projection works
    with warnings.catch_warnings(True) as w:
        projs = compute_proj_raw(raw, duration=None, stop=raw_time,
                                 n_grad=1, n_mag=1, n_eeg=0)
        assert_true(len(w) == 1)

    # test that you can compute the projection matrix
    projs = activate_proj(projs)
    proj, nproj, U = make_projector(projs, raw.ch_names, bads=[])

    assert_true(nproj == 2)
    assert_true(U.shape[1] == 2)

    # test that you can save them
    raw.info['projs'] += projs
    raw.save(op.join(tempdir, 'foo_rawproj_continuous_raw.fif'))

    # test resampled-data projector, upsampling instead of downsampling
    # here to save an extra filtering (raw would have to be LP'ed to be equiv)
    raw_resamp = cp.deepcopy(raw)
    raw_resamp.resample(raw.info['sfreq'] * 2, n_jobs=2)
    projs = compute_proj_raw(raw_resamp, duration=None, stop=raw_time,
                             n_grad=1, n_mag=1, n_eeg=0)
    projs = activate_proj(projs)
    proj_new, _, _ = make_projector(projs, raw.ch_names, bads=[])
    assert_array_almost_equal(proj_new, proj, 4)
Пример #16
0
def test_output_formats():
    """Test saving and loading raw data using multiple formats
    """
    formats = ['short', 'int', 'single', 'double']
    tols = [1e-4, 1e-7, 1e-7, 1e-15]

    # let's fake a raw file with different formats
    raw = Raw(fif_fname, preload=True)
    raw.crop(0, 1, copy=False)

    temp_file = op.join(tempdir, 'raw.fif')
    for ii, (format, tol) in enumerate(zip(formats, tols)):
        # Let's test the overwriting error throwing while we're at it
        if ii > 0:
            assert_raises(IOError, raw.save, temp_file, format=format)
        raw.save(temp_file, format=format, overwrite=True)
        raw2 = Raw(temp_file)
        raw2_data = raw2[:, :][0]
        assert_allclose(raw2_data, raw._data, rtol=tol, atol=1e-25)
        assert_true(raw2.orig_format == format)
Пример #17
0
def test_compensation():
    """Test compensation
    """
    raw = Raw(ctf_comp_fname, compensation=None)
    comp1 = make_compensator(raw.info, 3, 1, exclude_comp_chs=False)
    assert_true(comp1.shape == (340, 340))
    comp2 = make_compensator(raw.info, 3, 1, exclude_comp_chs=True)
    assert_true(comp2.shape == (311, 340))

    # make sure that changing the comp doesn't modify the original data
    raw2 = Raw(ctf_comp_fname, compensation=2)
    assert_true(get_current_comp(raw2.info) == 2)
    fname = op.join(tempdir, 'ctf-raw.fif')
    raw2.save(fname)
    raw2 = Raw(fname, compensation=None)
    data, _ = raw[:, :]
    data2, _ = raw2[:, :]
    assert_allclose(data, data2, rtol=1e-9, atol=1e-20)
    for ch1, ch2 in zip(raw.info['chs'], raw2.info['chs']):
        assert_true(ch1['coil_type'] == ch2['coil_type'])
Пример #18
0
def test_crop():
    """Test cropping raw files
    """
    # split a concatenated file to test a difficult case
    raw = Raw([fif_fname, fif_fname], preload=True)
    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.crop(tmin, tmax, True)
    all_raw_2 = concatenate_raws(raws, preload=True)
    assert_true(raw.first_samp == all_raw_2.first_samp)
    assert_true(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()
        raws[ri].crop(tmin, tmax, False)
    # test concatenation of split file
    all_raw_1 = concatenate_raws(raws, preload=True)

    all_raw_2 = raw.crop(0, None, True)
    for ar in [all_raw_1, all_raw_2]:
        assert_true(raw.first_samp == ar.first_samp)
        assert_true(raw.last_samp == ar.last_samp)
        assert_array_equal(raw[:, :][0], ar[:, :][0])
Пример #19
0
def test_compensation_raw():
    raw1 = Raw(ctf_comp_fname, compensation=None)
    assert_true(raw1.comp is None)
    data1, times1 = raw1[:, :]
    raw2 = Raw(ctf_comp_fname, compensation=3)
    data2, times2 = raw2[:, :]
    assert_true(raw2.comp is None)  # unchanged (data come with grade 3)
    assert_array_equal(times1, times2)
    assert_array_equal(data1, data2)
    raw3 = Raw(ctf_comp_fname, compensation=1)
    data3, times3 = raw3[:, :]
    assert_true(raw3.comp is not None)
    assert_array_equal(times1, times3)
    # make sure it's different with a different compensation:
    assert_true(np.mean(np.abs(data1 - data3)) > 1e-12)
    assert_raises(ValueError, Raw, ctf_comp_fname, compensation=33)

    # Try IO with compensation
    temp_file = op.join(tempdir, "raw.fif")

    raw1.save(temp_file, overwrite=True)
    raw4 = Raw(temp_file)
    data4, times4 = raw4[:, :]
    assert_array_equal(times1, times4)
    assert_array_equal(data1, data4)

    # Now save the file that has modified compensation
    # and make sure we can the same data as input ie. compensation
    # is undone
    raw3.save(temp_file, overwrite=True)
    raw5 = Raw(temp_file)
    data5, times5 = raw5[:, :]
    assert_array_equal(times1, times5)
    assert_allclose(data1, data5, rtol=1e-12, atol=1e-22)
Пример #20
0
def test_rank_estimation():
    """Test raw rank estimation
    """
    raw = Raw(fif_fname)
    n_meg = len(pick_types(raw.info, meg=True, eeg=False, exclude="bads"))
    n_eeg = len(pick_types(raw.info, meg=False, eeg=True, exclude="bads"))
    raw = Raw(fif_fname, preload=True)
    assert_array_equal(raw.estimate_rank(), n_meg + n_eeg)
    raw = Raw(fif_fname, preload=False)
    raw.apply_proj()
    n_proj = len(raw.info["projs"])
    assert_array_equal(raw.estimate_rank(tstart=10, tstop=20), n_meg + n_eeg - n_proj)
Пример #21
0
def test_io_complex():
    """ Test IO with complex data types """
    dtypes = [np.complex64, np.complex128]

    raw = Raw(fif_fname, preload=True)
    picks = np.arange(5)
    start, stop = raw.time_to_index(0, 5)

    data_orig, _ = raw[picks, start:stop]

    for dtype in dtypes:
        imag_rand = np.array(1j * np.random.randn(data_orig.shape[0],
                            data_orig.shape[1]), dtype)

        raw_cp = deepcopy(raw)
        raw_cp._data = np.array(raw_cp._data, dtype)
        raw_cp._data[picks, start:stop] += imag_rand
        raw_cp.save('raw.fif', picks, tmin=0, tmax=5)

        raw2 = Raw('raw.fif')
        raw2_data, _ = raw2[picks, :]
        n_samp = raw2_data.shape[1]
        assert_array_almost_equal(raw2_data[:, :n_samp],
                                  raw_cp._data[picks, :n_samp])
Пример #22
0
def test_save():
    """ Test saving raw"""
    raw = Raw(fif_fname, preload=True)
    assert_raises(ValueError, raw.save, fif_fname)
    new_fname = op.join(op.abspath(op.curdir), 'break.fif')
    raw.save(op.join(tempdir, new_fname))
    new_raw = Raw(op.join(tempdir, new_fname))
    assert_raises(ValueError, new_raw.save, new_fname)
    new_raw.close()
    os.remove(new_fname)
Пример #23
0
def test_io_raw():
    """Test IO for raw data (Neuromag + CTF)
    """
    for fname in [fif_fname, ctf_fname]:
        raw = Raw(fname)

        nchan = raw.info['nchan']
        ch_names = raw.info['ch_names']
        meg_channels_idx = [k for k in range(nchan)
                                            if ch_names[k][0] == 'M']
        n_channels = 100
        meg_channels_idx = meg_channels_idx[:n_channels]
        start, stop = raw.time_to_index(0, 5)
        data, times = raw[meg_channels_idx, start:(stop + 1)]
        meg_ch_names = [ch_names[k] for k in meg_channels_idx]

        # Set up pick list: MEG + STI 014 - bad channels
        include = ['STI 014']
        include += meg_ch_names
        picks = pick_types(raw.info, meg=True, eeg=False,
                                stim=True, misc=True, include=include,
                                exclude=raw.info['bads'])
        print "Number of picked channels : %d" % len(picks)

        # Writing with drop_small_buffer True
        raw.save('raw.fif', picks, tmin=0, tmax=4, buffer_size_sec=3,
                 drop_small_buffer=True)
        raw2 = Raw('raw.fif')

        sel = pick_channels(raw2.ch_names, meg_ch_names)
        data2, times2 = raw2[sel, :]
        assert_true(times2.max() <= 3)

        # Writing
        raw.save('raw.fif', picks, tmin=0, tmax=5)

        if fname == fif_fname:
            assert_true(len(raw.info['dig']) == 146)

        raw2 = Raw('raw.fif')

        sel = pick_channels(raw2.ch_names, meg_ch_names)
        data2, times2 = raw2[sel, :]

        assert_array_almost_equal(data, data2)
        assert_array_almost_equal(times, times2)
        assert_array_almost_equal(raw.info['dev_head_t']['trans'],
                                  raw2.info['dev_head_t']['trans'])
        assert_array_almost_equal(raw.info['sfreq'], raw2.info['sfreq'])

        if fname == fif_fname:
            assert_array_almost_equal(raw.info['dig'][0]['r'],
                                      raw2.info['dig'][0]['r'])

        fname = op.join(op.dirname(__file__), 'data', 'test_raw.fif')
Пример #24
0
def test_resample():
    """Test resample (with I/O and multiple files)
    """
    raw = Raw(fif_fname, preload=True).crop(0, 3, False)
    raw_resamp = raw.copy()
    sfreq = raw.info['sfreq']
    # test parallel on upsample
    raw_resamp.resample(sfreq * 2, n_jobs=2)
    assert_true(raw_resamp.n_times == len(raw_resamp._times))
    raw_resamp.save(op.join(tempdir, 'raw_resamp.fif'))
    raw_resamp = Raw(op.join(tempdir, 'raw_resamp.fif'), preload=True)
    assert_true(sfreq == raw_resamp.info['sfreq'] / 2)
    assert_true(raw.n_times == raw_resamp.n_times / 2)
    assert_true(raw_resamp._data.shape[1] == raw_resamp.n_times)
    assert_true(raw._data.shape[0] == raw_resamp._data.shape[0])
    # test non-parallel on downsample
    raw_resamp.resample(sfreq, n_jobs=1)
    assert_true(raw_resamp.info['sfreq'] == sfreq)
    assert_true(raw._data.shape == raw_resamp._data.shape)
    assert_true(raw.first_samp == raw_resamp.first_samp)
    assert_true(raw.last_samp == raw.last_samp)
    # upsampling then downsampling doubles resampling error, but this still
    # works (hooray). Note that the stim channels had to be sub-sampled
    # without filtering to be accurately preserved
    # note we have to treat MEG and EEG+STIM channels differently (tols)
    assert_allclose(raw._data[:306, 200:-200],
                    raw_resamp._data[:306, 200:-200],
                    rtol=1e-2, atol=1e-12)
    assert_allclose(raw._data[306:, 200:-200],
                    raw_resamp._data[306:, 200:-200],
                    rtol=1e-2, atol=1e-7)

    # now check multiple file support w/resampling, as order of operations
    # (concat, resample) should not affect our data
    raw1 = raw.copy()
    raw2 = raw.copy()
    raw3 = raw.copy()
    raw4 = raw.copy()
    raw1 = concatenate_raws([raw1, raw2])
    raw1.resample(10)
    raw3.resample(10)
    raw4.resample(10)
    raw3 = concatenate_raws([raw3, raw4])
    assert_array_equal(raw1._data, raw3._data)
    assert_array_equal(raw1._first_samps, raw3._first_samps)
    assert_array_equal(raw1._last_samps, raw3._last_samps)
    assert_array_equal(raw1._raw_lengths, raw3._raw_lengths)
    assert_equal(raw1.first_samp, raw3.first_samp)
    assert_equal(raw1.last_samp, raw3.last_samp)
    assert_equal(raw1.info['sfreq'], raw3.info['sfreq'])
Пример #25
0
def test_save():
    """ Test saving raw"""
    raw = Raw(fif_fname, preload=False)
    # can't write over file being read
    assert_raises(ValueError, raw.save, fif_fname)
    raw = Raw(fif_fname, preload=True)
    # can't overwrite file without overwrite=True
    assert_raises(IOError, raw.save, fif_fname)

    # test abspath support
    new_fname = op.join(op.abspath(op.curdir), 'break.fif')
    raw.save(op.join(tempdir, new_fname), overwrite=True)
    new_raw = Raw(op.join(tempdir, new_fname), preload=False)
    assert_raises(ValueError, new_raw.save, new_fname)
    # make sure we can overwrite the file we loaded when preload=True
    new_raw = Raw(op.join(tempdir, new_fname), preload=True)
    new_raw.save(op.join(tempdir, new_fname), overwrite=True)
    os.remove(new_fname)
Пример #26
0
def test_io_raw():
    """Test IO for raw data (Neuromag + CTF + gz)
    """
    fnames_in = [fif_fname, fif_gz_fname, ctf_fname]
    fnames_out = ["raw.fif", "raw.fif.gz", "raw.fif"]
    for fname_in, fname_out in zip(fnames_in, fnames_out):
        raw = Raw(fname_in)

        nchan = raw.info["nchan"]
        ch_names = raw.info["ch_names"]
        meg_channels_idx = [k for k in range(nchan) if ch_names[k][0] == "M"]
        n_channels = 100
        meg_channels_idx = meg_channels_idx[:n_channels]
        start, stop = raw.time_as_index([0, 5])
        data, times = raw[meg_channels_idx, start : (stop + 1)]
        meg_ch_names = [ch_names[k] for k in meg_channels_idx]

        # Set up pick list: MEG + STI 014 - bad channels
        include = ["STI 014"]
        include += meg_ch_names
        picks = pick_types(
            raw.info, meg=True, eeg=False, stim=True, misc=True, include=include, exclude=raw.info["bads"]
        )
        print "Number of picked channels : %d" % len(picks)

        # Writing with drop_small_buffer True
        raw.save(fname_out, picks, tmin=0, tmax=4, buffer_size_sec=3, drop_small_buffer=True)
        raw2 = Raw(fname_out, preload=True)

        sel = pick_channels(raw2.ch_names, meg_ch_names)
        data2, times2 = raw2[sel, :]
        assert_true(times2.max() <= 3)

        # Writing
        raw.save(fname_out, picks, tmin=0, tmax=5)

        if fname_in == fif_fname or fname_in == fif_fname + ".gz":
            assert_true(len(raw.info["dig"]) == 146)

        raw2 = Raw(fname_out)

        sel = pick_channels(raw2.ch_names, meg_ch_names)
        data2, times2 = raw2[sel, :]

        assert_array_almost_equal(data, data2)
        assert_array_almost_equal(times, times2)
        assert_array_almost_equal(raw.info["dev_head_t"]["trans"], raw2.info["dev_head_t"]["trans"])
        assert_array_almost_equal(raw.info["sfreq"], raw2.info["sfreq"])

        if fname_in == fif_fname or fname_in == fif_fname + ".gz":
            assert_array_almost_equal(raw.info["dig"][0]["r"], raw2.info["dig"][0]["r"])
Пример #27
0
from mne.fiff import Raw
from mne.minimum_norm import apply_inverse_raw, read_inverse_operator


data_path = sample.data_path()
fname_inv = data_path + '/MEG/sample/sample_audvis-meg-oct-6-meg-inv.fif'
fname_raw = data_path + '/MEG/sample/sample_audvis_raw.fif'
label_name = 'Aud-lh'
fname_label = data_path + '/MEG/sample/labels/%s.label' % label_name

snr = 1.0  # use smaller SNR for raw data
lambda2 = 1.0 / snr ** 2
method = "sLORETA"  # use sLORETA method (could also be MNE or dSPM)

# Load data
raw = Raw(fname_raw)
inverse_operator = read_inverse_operator(fname_inv)
label = mne.read_label(fname_label)

start, stop = raw.time_as_index([0, 15])  # read the first 15s of data

# Compute inverse solution
stc = apply_inverse_raw(raw, inverse_operator, lambda2, method, label,
                        start, stop, pick_ori=None)

# Save result in stc files
stc.save('mne_%s_raw_inverse_%s' % (method, label_name))

###############################################################################
# View activation time-series
plt.plot(1e3 * stc.times, stc.data[::100, :].T)
Пример #28
0
def test_apply_mne_inverse_epochs():
    """Test MNE with precomputed inverse operator on Epochs
    """
    inverse_operator = read_inverse_operator(fname_inv)
    label_lh = read_label(fname_label % 'Aud-lh')
    label_rh = read_label(fname_label % 'Aud-rh')
    event_id, tmin, tmax = 1, -0.2, 0.5
    raw = Raw(fname_raw)

    picks = pick_types(raw.info,
                       meg=True,
                       eeg=False,
                       stim=True,
                       ecg=True,
                       eog=True,
                       include=['STI 014'],
                       exclude='bads')
    reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6)
    flat = dict(grad=1e-15, mag=1e-15)

    events = read_events(fname_event)[:15]
    epochs = Epochs(raw,
                    events,
                    event_id,
                    tmin,
                    tmax,
                    picks=picks,
                    baseline=(None, 0),
                    reject=reject,
                    flat=flat)
    stcs = apply_inverse_epochs(epochs,
                                inverse_operator,
                                lambda2,
                                "dSPM",
                                label=label_lh,
                                pick_ori="normal")

    assert_true(len(stcs) == 4)
    assert_true(3 < stcs[0].data.max() < 10)
    assert_true(stcs[0].subject == 'sample')

    data = sum(stc.data for stc in stcs) / len(stcs)
    flip = label_sign_flip(label_lh, inverse_operator['src'])

    label_mean = np.mean(data, axis=0)
    label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0)

    assert_true(label_mean.max() < label_mean_flip.max())

    # test extracting a BiHemiLabel
    stcs_rh = apply_inverse_epochs(epochs,
                                   inverse_operator,
                                   lambda2,
                                   "dSPM",
                                   label=label_rh,
                                   pick_ori="normal")
    stcs_bh = apply_inverse_epochs(epochs,
                                   inverse_operator,
                                   lambda2,
                                   "dSPM",
                                   label=label_lh + label_rh,
                                   pick_ori="normal")

    n_lh = len(stcs[0].data)
    assert_array_almost_equal(stcs[0].data, stcs_bh[0].data[:n_lh])
    assert_array_almost_equal(stcs_rh[0].data, stcs_bh[0].data[n_lh:])

    # test without using a label (so delayed computation is used)
    stcs = apply_inverse_epochs(epochs,
                                inverse_operator,
                                lambda2,
                                "dSPM",
                                pick_ori="normal")
    assert_true(stcs[0].subject == 'sample')
    label_stc = stcs[0].in_label(label_rh)
    assert_true(label_stc.subject == 'sample')
    assert_array_almost_equal(stcs_rh[0].data, label_stc.data)
data_path = sample.data_path()
fname_inv = data_path + '/MEG/sample/sample_audvis-meg-oct-6-meg-inv.fif'
fname_raw = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
fname_event = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
label_name = 'Aud-lh'
fname_label = data_path + '/MEG/sample/labels/%s.label' % label_name

event_id, tmin, tmax = 1, -0.2, 0.5
snr = 1.0  # use smaller SNR for raw data
lambda2 = 1.0 / snr**2
method = "dSPM"  # use dSPM method (could also be MNE or sLORETA)

# Load data
inverse_operator = read_inverse_operator(fname_inv)
label = mne.read_label(fname_label)
raw = Raw(fname_raw)
events = mne.read_events(fname_event)

# Set up pick list
include = []

# Add a bad channel
raw.info['bads'] += ['EEG 053']  # bads + 1 more

# pick MEG channels
picks = pick_types(raw.info,
                   meg=True,
                   eeg=False,
                   stim=False,
                   eog=True,
                   include=include,
Пример #30
0
print(__doc__)

import mne
import matplotlib.pyplot as plt
import numpy as np
from mne.fiff import Raw
from mne.datasets import sample

# turn on interactive mode
plt.ion()

data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'

raw = Raw(raw_fname)

# For simplicity we will only consider the first 10 epochs
events = mne.read_events(event_fname)[:10]

# Add a bad channel
raw.info['bads'] += ['MEG 2443']
picks = mne.fiff.pick_types(raw.info,
                            meg='grad',
                            eeg=False,
                            eog=True,
                            stim=False,
                            exclude='bads')

tmin, tmax = -0.2, 0.5
baseline = (None, 0)
Пример #31
0
Find events from the stimulation/trigger channel in the raw data.
The plot them to get an idea of the paradigm.
"""
# Author: Alexandre Gramfort <*****@*****.**>
#
# License: BSD (3-clause)

print(__doc__)

import mne
from mne.datasets import sample
from mne.fiff import Raw

data_path = sample.data_path()
fname = data_path + '/MEG/sample/sample_audvis_raw.fif'

# Reading events
raw = Raw(fname)

events = mne.find_events(raw, stim_channel='STI 014')

# Writing events
mne.write_events('events.fif', events)

for ind, before, after in events[:5]:
    print("At sample %d stim channel went from %d to %d" %
          (ind, before, after))

# Plot the events to get an idea of the paradigm
mne.viz.plot_events(events, raw.info['sfreq'], raw.first_samp)
Пример #32
0
from mne.datasets import sample
from mne.fiff import Raw
from mne.minimum_norm import apply_inverse_raw, read_inverse_operator

data_path = sample.data_path('..')
fname_inv = data_path + '/MEG/sample/sample_audvis-meg-oct-6-meg-inv.fif'
fname_raw = data_path + '/MEG/sample/sample_audvis_raw.fif'
label_name = 'Aud-lh'
fname_label = data_path + '/MEG/sample/labels/%s.label' % label_name

snr = 3.0
lambda2 = 1.0 / snr**2
dSPM = True

# Load data
raw = Raw(fname_raw)
inverse_operator = read_inverse_operator(fname_inv)
label = mne.read_label(fname_label)

start, stop = raw.time_to_index(0, 15)  # read the first 15s of data

# Compute inverse solution
stc = apply_inverse_raw(raw, inverse_operator, lambda2, dSPM, label, start,
                        stop)

# Save result in stc files
stc.save('mne_dSPM_raw_inverse_%s' % label_name)

###############################################################################
# View activation time-series
pl.plot(1e3 * stc.times, stc.data[::100, :].T)
Пример #33
0
def test_proj():
    """Test SSP proj operations
    """
    for proj_active in [True, False]:
        raw = Raw(fif_fname, preload=False, proj_active=proj_active)
        assert_true(all(p['active'] == proj_active for p in raw.info['projs']))

        data, times = raw[0:2, :]
        data1, times1 = raw[0:2]
        assert_array_equal(data, data1)
        assert_array_equal(times, times1)

        # test adding / deleting proj
        if proj_active:
            assert_raises(ValueError, raw.add_proj, [],
                          {'remove_existing': True})
            assert_raises(ValueError, raw.del_proj, 0)
        else:
            projs = deepcopy(raw.info['projs'])
            n_proj = len(raw.info['projs'])
            raw.del_proj(0)
            assert_true(len(raw.info['projs']) == n_proj - 1)
            raw.add_proj(projs, remove_existing=False)
            assert_true(len(raw.info['projs']) == 2 * n_proj - 1)
            raw.add_proj(projs, remove_existing=True)
            assert_true(len(raw.info['projs']) == n_proj)

    # test apply_proj() with and without preload
    for preload in [True, False]:
        raw = Raw(fif_fname, preload=preload, proj_active=False)
        data, times = raw[:, 0:2]
        raw.apply_projector()
        data_proj_1 = np.dot(raw._projector, data)

        # load the file again without proj
        raw = Raw(fif_fname, preload=preload, proj_active=False)

        # write the file with proj. activated, make sure proj has been applied
        raw.save(op.join(tempdir, 'raw.fif'), proj_active=True)
        raw2 = Raw(op.join(tempdir, 'raw.fif'), proj_active=False)
        data_proj_2, _ = raw2[:, 0:2]
        assert_array_almost_equal(data_proj_1, data_proj_2)
        assert_true(all(p['active'] for p in raw2.info['projs']))

        # read orig file with proj. active
        raw2 = Raw(fif_fname, preload=preload, proj_active=True)
        data_proj_2, _ = raw2[:, 0:2]
        assert_array_almost_equal(data_proj_1, data_proj_2)
        assert_true(all(p['active'] for p in raw2.info['projs']))

        # test that apply_projector works
        raw.apply_projector()
        data_proj_2, _ = raw[:, 0:2]
        assert_array_almost_equal(data_proj_1, data_proj_2)
        assert_array_almost_equal(data_proj_2,
                                  np.dot(raw._projector, data_proj_2))
Пример #34
0
               'EEG037'], ['EEG002', 'EEG055'], ['EEG025', 'EEG035'], [
                   'EEG009', 'EEG022', 'EEG045', 'EEG046', 'EEG053', 'EEG054',
                   'EEG059'
               ], ['EEG035', 'EEG057'
                   ], ['EEG043'], ['EEG035'], ['EEG017', 'EEG025',
                                               'EEG035'], ['EEG025', 'EEG035'],
              ['EEG025', 'EEG035'], ['EEG025', 'EEG035', 'EEG036', 'EEG017'],
              ['EEG0017', 'EEG0025', 'EEG0036', 'EEG0026', 'EEG0034'])

for i in range(len(ListSubj)):
    for j in range(len(listRunPerSubj[i])):
        print(j)
        print(i)
        # load trans_sss data #
        raw = Raw("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/" +
                  ListSubj[i] + "/mne_python/preproc_" + listRunPerSubj[i][j] +
                  "_trans_sss_raw.fif")
        raw.info['bads'] += EEGbadlist[i]

        # define events #
        tmp = str(j + 1)
        fileE = io.loadmat(
            "/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/From_laptop/Subjects/" +
            ListSubj[i] + "/PsychData/events" + tmp + ".mat")
        eventsFT = fileE['fullsubj']
        eventsMNE = mne.find_events(raw,
                                    stim_channel=['STI101'],
                                    consecutive=False)
        print "%d events found" % len(eventsMNE)

        # find events corresponding to FT run j
 def make_evoked(fname, comp):
     raw = Raw(fname, compensation=comp)
     picks = pick_types(raw.info, meg=True, ref_meg=True)
     events = np.array([[0, 0, 1]], dtype=np.int)
     evoked = Epochs(raw, events, 1, 0, 20e-3, picks=picks).average()
     return evoked
except:
    print "Please run with input file provided. Exiting"
    sys.exit()

res_ch_name = 'STI 013'
sti_ch_name = 'STI 014'
n_components = 0.99
n_pca_components = None
max_pca_components = None

subjects_dir = '/home/qdong/data/'
subject_path = subjects_dir + subject  #Set the data path of the subject
#raw_fname = subject_path + '/MEG/ssp_cleaned_%s_audi_cued-raw_cle.fif' %subject
raw_fname = subject_path + '/MEG/%s_audi_cued-raw_cle.fif' % subject
raw_basename = os.path.splitext(os.path.basename(raw_fname))[0]
raw = Raw(raw_fname, preload=True)
picks = mne.fiff.pick_types(raw.info,
                            meg=True,
                            eeg=False,
                            eog=False,
                            stim=False,
                            exclude='bads')
ica = ICA(n_components=n_components,
          n_pca_components=n_pca_components,
          max_pca_components=max_pca_components,
          random_state=0)
ica.decompose_raw(raw, picks=picks, decim=3)
if trigger == 'resp':  #'1' represents the response channel
    add_from_raw = mne.fiff.pick_types(raw.info,
                                       meg=False,
                                       resp=True,
Пример #37
0
from mne.fiff import Raw, pick_types, read_evokeds
from mne.datasets.megsim import load_data

condition = 'visual'  # or 'auditory' or 'somatosensory'

# Load experimental RAW files for the visual condition
raw_fnames = load_data(condition=condition,
                       data_format='raw',
                       data_type='experimental')

# Load simulation evoked files for the visual condition
evoked_fnames = load_data(condition=condition,
                          data_format='evoked',
                          data_type='simulation')

raw = Raw(raw_fnames[0])
events = find_events(raw, stim_channel="STI 014", shortest_event=1)

# Visualize raw file
raw.plot()

# Make an evoked file from the experimental data
picks = pick_types(raw.info, meg=True, eog=True, exclude='bads')

# Read epochs
event_id, tmin, tmax = 9, -0.2, 0.5
epochs = Epochs(raw,
                events,
                event_id,
                tmin,
                tmax,
Пример #38
0
def test_proj():
    """Test SSP proj operations
    """
    for proj in [True, False]:
        raw = Raw(fif_fname, preload=False, proj=proj)
        assert_true(all(p['active'] == proj for p in raw.info['projs']))

        data, times = raw[0:2, :]
        data1, times1 = raw[0:2]
        assert_array_equal(data, data1)
        assert_array_equal(times, times1)

        # test adding / deleting proj
        if proj:
            assert_raises(ValueError, raw.add_proj, [],
                          {'remove_existing': True})
            assert_raises(ValueError, raw.del_proj, 0)
        else:
            projs = deepcopy(raw.info['projs'])
            n_proj = len(raw.info['projs'])
            raw.del_proj(0)
            assert_true(len(raw.info['projs']) == n_proj - 1)
            raw.add_proj(projs, remove_existing=False)
            assert_true(len(raw.info['projs']) == 2 * n_proj - 1)
            raw.add_proj(projs, remove_existing=True)
            assert_true(len(raw.info['projs']) == n_proj)

    # test apply_proj() with and without preload
    for preload in [True, False]:
        raw = Raw(fif_fname, preload=preload, proj=False)
        data, times = raw[:, 0:2]
        raw.apply_proj()
        data_proj_1 = np.dot(raw._projector, data)

        # load the file again without proj
        raw = Raw(fif_fname, preload=preload, proj=False)

        # write the file with proj. activated, make sure proj has been applied
        raw.save(op.join(tempdir, 'raw.fif'), proj=True, overwrite=True)
        raw2 = Raw(op.join(tempdir, 'raw.fif'), proj=False)
        data_proj_2, _ = raw2[:, 0:2]
        assert_allclose(data_proj_1, data_proj_2)
        assert_true(all(p['active'] for p in raw2.info['projs']))

        # read orig file with proj. active
        raw2 = Raw(fif_fname, preload=preload, proj=True)
        data_proj_2, _ = raw2[:, 0:2]
        assert_allclose(data_proj_1, data_proj_2)
        assert_true(all(p['active'] for p in raw2.info['projs']))

        # test that apply_proj works
        raw.apply_proj()
        data_proj_2, _ = raw[:, 0:2]
        assert_allclose(data_proj_1, data_proj_2)
        assert_allclose(data_proj_2, np.dot(raw._projector, data_proj_2))
Пример #39
0
def test_subject_info():
    """Test reading subject information
    """
    raw = Raw(fif_fname)
    raw.crop(0, 1, False)
    assert_true(raw.info['subject_info'] is None)
    # fake some subject data
    keys = [
        'id', 'his_id', 'last_name', 'first_name', 'birthday', 'sex', 'hand'
    ]
    vals = [1, 'foobar', 'bar', 'foo', (1901, 2, 3), 0, 1]
    subject_info = dict()
    for key, val in zip(keys, vals):
        subject_info[key] = val
    raw.info['subject_info'] = subject_info
    out_fname = op.join(tempdir, 'test_subj_info_raw.fif')
    raw.save(out_fname, overwrite=True)
    raw_read = Raw(out_fname)
    for key in keys:
        assert_equal(subject_info[key], raw_read.info['subject_info'][key])
    raw_read.anonymize()
    assert_true(raw_read.info.get('subject_info') is None)
    out_fname_anon = op.join(tempdir, 'test_subj_info_anon_raw.fif')
    raw_read.save(out_fname_anon, overwrite=True)
    raw_read = Raw(out_fname_anon)
    assert_true(raw_read.info.get('subject_info') is None)
Пример #40
0
from mne.beamformer import lcmv

data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_raw-eve.fif'
fname_fwd = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif'
fname_cov = data_path + '/MEG/sample/sample_audvis-cov.fif'
label_name = 'Aud-lh'
fname_label = data_path + '/MEG/sample/labels/%s.label' % label_name

###############################################################################
# Get epochs
event_id, tmin, tmax = 1, -0.2, 0.5

# Setup for reading the raw data
raw = Raw(raw_fname)
raw.info['bads'] = ['MEG 2443', 'EEG 053']  # 2 bads channels
events = mne.read_events(event_fname)

# Set up pick list: EEG + MEG - bad channels (modify to your needs)
left_temporal_channels = mne.read_selection('Left-temporal')
picks = pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True,
                   exclude='bads', selection=left_temporal_channels)

# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
                    picks=picks, baseline=(None, 0), preload=True,
                    reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))
evoked = epochs.average()

forward = mne.read_forward_solution(fname_fwd)
Пример #41
0
def test_filter():
    """ Test filtering (FIR and IIR) and Raw.apply_function interface """
    raw = Raw(fif_fname, preload=True).crop(0, 10, False)
    sig_dec = 11
    sig_dec_notch = 12
    sig_dec_notch_fit = 12
    picks_meg = pick_types(raw.info, meg=True, exclude='bads')
    picks = picks_meg[:4]

    raw_lp = raw.copy()
    raw_lp.filter(0., 4.0 - 0.25, picks=picks, n_jobs=2)

    raw_hp = raw.copy()
    raw_hp.filter(8.0 + 0.25, None, picks=picks, n_jobs=2)

    raw_bp = raw.copy()
    raw_bp.filter(4.0 + 0.25, 8.0 - 0.25, picks=picks)

    raw_bs = raw.copy()
    raw_bs.filter(8.0 + 0.25, 4.0 - 0.25, picks=picks, n_jobs=2)

    data, _ = raw[picks, :]

    lp_data, _ = raw_lp[picks, :]
    hp_data, _ = raw_hp[picks, :]
    bp_data, _ = raw_bp[picks, :]
    bs_data, _ = raw_bs[picks, :]

    assert_array_almost_equal(data, lp_data + bp_data + hp_data, sig_dec)
    assert_array_almost_equal(data, bp_data + bs_data, sig_dec)

    raw_lp_iir = raw.copy()
    raw_lp_iir.filter(0., 4.0, picks=picks, n_jobs=2, method='iir')
    raw_hp_iir = raw.copy()
    raw_hp_iir.filter(8.0, None, picks=picks, n_jobs=2, method='iir')
    raw_bp_iir = raw.copy()
    raw_bp_iir.filter(4.0, 8.0, picks=picks, method='iir')
    lp_data_iir, _ = raw_lp_iir[picks, :]
    hp_data_iir, _ = raw_hp_iir[picks, :]
    bp_data_iir, _ = raw_bp_iir[picks, :]
    summation = lp_data_iir + hp_data_iir + bp_data_iir
    assert_array_almost_equal(data[:, 100:-100], summation[:, 100:-100],
                              sig_dec)

    # make sure we didn't touch other channels
    data, _ = raw[picks_meg[4:], :]
    bp_data, _ = raw_bp[picks_meg[4:], :]
    assert_array_equal(data, bp_data)
    bp_data_iir, _ = raw_bp_iir[picks_meg[4:], :]
    assert_array_equal(data, bp_data_iir)

    # do a very simple check on line filtering
    raw_bs = raw.copy()
    with warnings.catch_warnings(True) as w:
        raw_bs.filter(60.0 + 0.5, 60.0 - 0.5, picks=picks, n_jobs=2)
        data_bs, _ = raw_bs[picks, :]
        raw_notch = raw.copy()
        raw_notch.notch_filter(60.0, picks=picks, n_jobs=2, method='fft')
    data_notch, _ = raw_notch[picks, :]
    assert_array_almost_equal(data_bs, data_notch, sig_dec_notch)

    # now use the sinusoidal fitting
    raw_notch = raw.copy()
    raw_notch.notch_filter(None, picks=picks, n_jobs=2, method='spectrum_fit')
    data_notch, _ = raw_notch[picks, :]
    data, _ = raw[picks, :]
    assert_array_almost_equal(data, data_notch, sig_dec_notch_fit)
Пример #42
0
def test_find_layout():
    """Test finding layout"""
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        find_layout(chs=test_info['chs'])
        assert_true(w[0].category == DeprecationWarning)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        find_layout(test_info['chs'])
        assert_true(w[0].category == DeprecationWarning)
    assert_raises(ValueError, find_layout, dict())
    assert_raises(ValueError, find_layout, test_info, ch_type='meep')

    sample_info = Raw(fif_fname).info
    grads = pick_types(sample_info, meg='grad')
    sample_info2 = pick_info(sample_info, grads)

    mags = pick_types(sample_info, meg='mag')
    sample_info3 = pick_info(sample_info, mags)

    # mock new convention
    sample_info4 = copy.deepcopy(sample_info)
    for ii, name in enumerate(sample_info4['ch_names']):
        new = name.replace(' ', '')
        sample_info4['ch_names'][ii] = new
        sample_info4['chs'][ii]['ch_name'] = new

    mags = pick_types(sample_info, meg=False, eeg=True)
    sample_info5 = pick_info(sample_info, mags)

    lout = find_layout(sample_info, ch_type=None)
    assert_true(lout.kind == 'Vectorview-all')
    assert_true(all(' ' in k for k in lout.names))

    lout = find_layout(sample_info2, ch_type='meg')
    assert_true(lout.kind == 'Vectorview-all')

    # test new vector-view
    lout = find_layout(sample_info4, ch_type=None)
    assert_true(lout.kind == 'Vectorview-all')
    assert_true(all(not ' ' in k for k in lout.names))

    lout = find_layout(sample_info, ch_type='grad')
    assert_true(lout.kind == 'Vectorview-grad')
    lout = find_layout(sample_info2)
    assert_true(lout.kind == 'Vectorview-grad')
    lout = find_layout(sample_info2, ch_type='grad')
    assert_true(lout.kind == 'Vectorview-grad')
    lout = find_layout(sample_info2, ch_type='meg')
    assert_true(lout.kind == 'Vectorview-all')


    lout = find_layout(sample_info, ch_type='mag')
    assert_true(lout.kind == 'Vectorview-mag')
    lout = find_layout(sample_info3)
    assert_true(lout.kind == 'Vectorview-mag')
    lout = find_layout(sample_info3, ch_type='mag')
    assert_true(lout.kind == 'Vectorview-mag')
    lout = find_layout(sample_info3, ch_type='meg')
    assert_true(lout.kind == 'Vectorview-all')
    #
    lout = find_layout(sample_info, ch_type='eeg')
    assert_true(lout.kind == 'EEG')
    lout = find_layout(sample_info5)
    assert_true(lout.kind == 'EEG')
    lout = find_layout(sample_info5, ch_type='eeg')
    assert_true(lout.kind == 'EEG')
    # no common layout, 'meg' option not supported

    fname_bti_raw = op.join(bti_dir, 'exported4D_linux.fif')
    lout = find_layout(Raw(fname_bti_raw).info)
    assert_true(lout.kind == 'magnesWH3600')

    lout = find_layout(Raw(fname_ctf_raw).info)
    assert_true(lout.kind == 'CTF-275')

    lout = find_layout(read_raw_kit(fname_kit_157).info)
    assert_true(lout.kind == 'KIT-157')

    sample_info5['dig'] = []
    assert_raises(RuntimeError, find_layout, sample_info5)
Пример #43
0
def test_raw_time_as_index():
    """ Test index as time conversion"""
    raw = Raw(fif_fname, preload=True)
    first_samp = raw.time_as_index([0], True)[0]
    assert_true(raw.first_samp == -first_samp)
Пример #44
0
# License: BSD (3-clause)

import matplotlib.pyplot as plt
import numpy as np
import mne
from mne.fiff import Raw
from mne.preprocessing.ica import ICA
from mne.datasets import sample

###############################################################################
# Setup paths and prepare epochs data

data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'

raw = Raw(raw_fname, preload=True)
raw.apply_proj()

picks = mne.fiff.pick_types(raw.info,
                            meg=True,
                            eeg=False,
                            eog=True,
                            ecg=True,
                            stim=False,
                            exclude='bads')

tmin, tmax, event_id = -0.2, 0.5, 1
baseline = (None, 0)
reject = None

events = mne.find_events(raw, stim_channel='STI 014')
import matplotlib.pyplot as plt

import mne
from mne import fiff
from mne.fiff import Raw
from mne.datasets import spm_face
from mne.decoding import time_generalization

data_path = spm_face.data_path()

###############################################################################
# Load and filter data, set up epochs

raw_fname = data_path + '/MEG/spm/SPM_CTF_MEG_example_faces%d_3D_raw.fif'

raw = Raw(raw_fname % 1, preload=True)  # Take first run
raw.append(Raw(raw_fname % 2, preload=True))  # Take second run too

picks = mne.fiff.pick_types(raw.info, meg=True, exclude='bads')
raw.filter(1, 45, method='iir')

events = mne.find_events(raw, stim_channel='UPPT001')
event_id = {"faces": 1, "scrambled": 2}
tmin, tmax = -0.1, 0.5

# Set up pick list
picks = fiff.pick_types(raw.info,
                        meg=True,
                        eeg=False,
                        stim=True,
                        eog=True,
Пример #46
0
def GDAVG_source_trialsfromFT_2cond(condnames,datasource):

#ipython --pylab
import mne
import numpy as np
from scipy import io
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
from mne.fiff import Raw
from mne.minimum_norm import apply_inverse, make_inverse_operator
from copy import deepcopy
import os
os.chdir('/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/SCRIPTS/MNE_PYTHON')
import MatchEventsFT2MNE as match

os.environ['SUBJECTS_DIR'] = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/mri'
os.environ['MNE_ROOT'] = '/neurospin/local/mne'

# get a full list of subject and associated runs
ListSubj= ('sd130343', 'cb130477', 'rb130313', 'jm100042',  'jm100109', 'sb120316', 'tk130502', 'lm130479',
'sg120518','ms130534','ma100253', 'sl130503','mb140004', 'mp140019', 'mm130405', 'dm130250', 'hr130504', 'wl130316', 'rl130571')

listRunPerSubj = (('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run2_GD','run3_DG','run4_DG'),
    ('run1_GD','run2_GD','run3_DG','run4_DG'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG'),
    ('run1_GD','run2_GD','run3_DG','run4_DG'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'),
    ('run1_GD','run2_GD','run3_DG','run4_DG','run5_GD'))

# list of bad EEG channels for EEG processing
EEGbadlist = (['EEG25', 'EEG036'],['EEG035', 'EEG036'],['EEG025', 'EEG035', 'EEG036'],['EEG035'],['EEG017', 'EEG025'],['EEG026', 'EEG036'],
    [],['EEG025', 'EEG035', 'EEG036' 'EEG037'],['EEG002', 'EEG055'],['EEG025', 'EEG035'],
    ['EEG009', 'EEG022', 'EEG045', 'EEG046', 'EEG053', 'EEG054', 'EEG059'],
    ['EEG035', 'EEG057'],['EEG043'],['EEG035'],['EEG017', 'EEG025', 'EEG035'],['EEG025', 'EEG035'],
    ['EEG025', 'EEG035'],['EEG025', 'EEG035', 'EEG036', 'EEG017'],['EEG0017', 'EEG0025', 'EEG0036', 'EEG0026', 'EEG0034'])

for i in range(len(ListSubj)):
	for j in range(len(listRunPerSubj[i])):
		print(j)
                print(i)
		# load trans_sss data #
		raw = Raw("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/preproc_"+listRunPerSubj[i][j]+"_trans_sss_raw.fif")
		raw.info['bads'] += EEGbadlist[i]

		# define events #
		tmp = str(j+1)
		fileE = io.loadmat("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/From_laptop/Subjects/"+ListSubj[i]+"/PsychData/events"+tmp+".mat")
		eventsFT  = fileE['fullsubj']
		eventsMNE = mne.find_events(raw, stim_channel=['STI101'],consecutive = False)
		print "%d events found" %len(eventsMNE)

		# find events corresponding to FT run j
		itemindex = np.where(eventsFT[:,7]==(j+1))
		eventsFT  = eventsFT[itemindex[0],:]

                ######################################################################
		######################################################################
		if datasource == 'EVT':
			origevent = (16,20,24,28,32,17,21,25,29,33)
			# find events corresponding to "Historical events" stimuli
			init      = np.where(eventsFT[:,6]== origevent(0))
			for k in origevent[1:]:
				init = np.append(init,np.where(eventsFT[:,6]== k)[0]) 

			eventsFT = eventsFT[init,:]

		elif datasource == 'EVS':
			origevent = (18,22,26,30,34,19,23,27,31,35)
			# find events corresponding to "Historical events" stimuli
			init      = np.where(eventsFT[:,6]== origevent(0))
			for k in origevent[1:]:
				init = np.append(init,np.where(eventsFT[:,6]== k)[0]) 

			eventsFT = eventsFT[init,:]

		elif datasource == 'QTT':
			origevent = (6,8,10,12,14)
			# find events corresponding to "Historical events" stimuli
			init      = np.where(eventsFT[:,6]== origevent(0))
			for k in origevent[1:]:
				init = np.append(init,np.where(eventsFT[:,6]== k)[0]) 

			eventsFT = eventsFT[init,:]

		elif datasource == 'QTS':
			origevent = (7,9,11,13,15)
			# find events corresponding to "Historical events" stimuli
			init      = np.where(eventsFT[:,6]== origevent(0))
			for k in origevent[1:]:
				init = np.append(init,np.where(eventsFT[:,6]== k)[0]) 

			eventsFT = eventsFT[init,:]
		
		########################################################################
                ########################################################################

		# set the "zero point" to a target event in MNE event
		initsampmne = eventsMNE[np.where(eventsMNE[:,2] == 18)[0][0],0]

		for l in range(eventsMNE.shape[0]):
			eventsMNE[l,0] = eventsMNE[l,0] - initsampmne

		# set the "zero point" to a target event in FT event
		initsampft = eventsFT[np.where(eventsFT[:,6] == 18)[0][0],0]

		for l in range(eventsFT.shape[0]):
			eventsFT[l,0] = eventsFT[l,0] - initsampft

		# reject bad data based on fieldtrip "ft_rejectvisual"
		good     = np.where(eventsFT[:,8]== 1)
		eventsFT = eventsFT[good[0],:]

		# get the FT event in MNE events (should be the same but a small sample of events isn't matched
		# I need to check that soon
		SelEve = match.MatchEventsFT2MNE(eventsMNE,eventsFT)

		# get back to the original timing
		for m in range(SelEve.shape[0]):
			SelEve[m,0] = SelEve[m,0] + initsampmne

		# correct for photodelay
		for n in range(SelEve.shape[0]):
			SelEve[n,0] = SelEve[n,0] + 60;

		TMPset1 = io.loadmat("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/From_laptop/Scripts/event_def_for_mne/"+condnames[0]+".mat")
		TMPset2 = io.loadmat("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/From_laptop/Scripts/event_def_for_mne/"+condnames[1]+".mat")
		DATASET1 = TMPset1['cond']
		DATASET2 = TMPset2['cond']

		# process cond1&2
		event_id1, tmin, tmax = DATASET1[:,0].tolist(), -0.2,1.1 
		event_id2, tmin, tmax = DATASET2[:,0].tolist(), -0.2,1.1 

		# epoched data
		picks  = mne.fiff.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=False, include=[], exclude='bads')

		epochs_cond1 = mne.Epochs(raw, SelEve, event_id1, tmin, tmax, baseline=(-0.2, 0),picks = picks,preload = True,reject = None,on_missing = 'warning')
		epochs_cond2 = mne.Epochs(raw, SelEve, event_id2, tmin, tmax, baseline=(-0.2, 0),picks = picks,preload = True,reject = None,on_missing = 'warning')

		baseline_cond1 = mne.Epochs(raw, SelEve, event_id1, -0.2, 0, baseline=(None, 0),picks = picks,preload = True,reject = None,on_missing = 'warning')
		baseline_cond2 = mne.Epochs(raw, SelEve, event_id2, -0.2, 0, baseline=(None, 0),picks = picks,preload = True,reject = None,on_missing = 'warning')

		# compute noise covariance matrix from emptyroom epochs #
		evokedcond1 = epochs_cond1.average()
		evokedcond2 = epochs_cond2.average()
		noise_cov1 = mne.compute_covariance(baseline_cond1,keep_sample_mean=True, tmin=-0.2, tmax=0)
        	noise_cov2 = mne.compute_covariance(baseline_cond2,keep_sample_mean=True, tmin=-0.2, tmax=0)
        	noise_cov1 = mne.cov.regularize(noise_cov1, evokedcond1.info, grad=0.1, mag=0.1, eeg=0.1)
		noise_cov2 = mne.cov.regularize(noise_cov2, evokedcond2.info, grad=0.1, mag=0.1, eeg=0.1)

		# append subject's runs
		if j == 0:
			epochs_tot_cond1 = deepcopy(epochs_cond1)
			epochs_tot_base1 = deepcopy(baseline_cond1)
			COV1 = noise_cov1
		else:
			epochs_tmp_cond1 = epochs_cond1
			epochs_tmp_base1 = baseline_cond1
 			COV1['data'] += noise_cov1['data']
			COV1['nfree'] += noise_cov1['nfree']
			COV1 = COV1 + noise_cov1

			epochs_tot_cond1._data = np.vstack((epochs_tot_cond1._data,epochs_tmp_cond1._data))
			epochs_tot_cond1.events = np.vstack((epochs_tot_cond1.events,epochs_tmp_cond1.events))
			#epochs_tot_cond1.selection = np.concatenate((epochs_tot_cond1.selection,epochs_tmp_cond1.selection))
			epochs_tot_base1._data = np.vstack((epochs_tot_base1._data,epochs_tmp_base1._data))
			epochs_tot_base1.events = np.vstack((epochs_tot_base1.events,epochs_tmp_base1.events))
			#epochs_tot_base1.selection = np.concatenate((epochs_tot_base1.selection,epochs_tmp_base1.selection))

		# append subject's runs 
		if j == 0:
			epochs_tot_cond2 = deepcopy(epochs_cond2)
			epochs_tot_base2 = deepcopy(baseline_cond2)
			COV2 = noise_cov2
		else:
			epochs_tmp_cond2 = epochs_cond2
			epochs_tmp_base2 = baseline_cond2
			COV2['data'] += noise_cov2['data']
			COV2['nfree'] += noise_cov2['nfree']
			COV2 = COV2 + noise_cov2

			epochs_tot_cond2._data = np.vstack((epochs_tot_cond2._data,epochs_tmp_cond2._data))
			epochs_tot_cond2.events = np.vstack((epochs_tot_cond2.events,epochs_tmp_cond2.events))
			#epochs_tot_cond2.selection = np.concatenate((epochs_tot_cond2.selection,epochs_tmp_cond2.selection))
			epochs_tot_base2._data = np.vstack((epochs_tot_base2._data,epochs_tmp_base2._data))
			epochs_tot_base2.events = np.vstack((epochs_tot_base2.events,epochs_tmp_base2.events))
			#epochs_tot_base2.selection = np.concatenate((epochs_tot_base2.selection,epochs_tmp_base2.selection))

		SelEve = None
	
	# save data
	evokedcond1 = epochs_tot_cond1.average()
	evokedcond1.save("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/"+condnames[1]+"_"+ListSubj[i]+"-ave.fif")

	# save data
	evokedcond2 = epochs_tot_cond2.average()
	evokedcond2.save("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/"+condnames[2]+"_"+ListSubj[i]+"-ave.fif")

	# compute noise covariance matrix from emptyroom epochs #
        NOISE_COV1 =mne.cov.regularize(COV1, evokedcond1.info, grad=0.1, mag=0.1, eeg=0.1)
	NOISE_COV2 =mne.cov.regularize(COV2, evokedcond2.info, grad=0.1, mag=0.1, eeg=0.1)

	print(noise_cov1)
	# Show covariance
	mne.viz.plot_cov(noise_cov1, raw.info, colorbar=True, proj=True,show_svd=False,show=False)
	plt.savefig("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/GROUP/plots/"+ListSubj[i]+"covmat")
	plt.close()
	
	# dSPM solution #
	fname_fwd=("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/run3_ico-5-fwd.fif") 
	forward = mne.read_forward_solution(fname_fwd,surf_ori=True)
	inverse_operator1 = make_inverse_operator(evokedcond1.info, forward, noise_cov1, loose=0.4, depth=0.8)
	inverse_operator2 = make_inverse_operator(evokedcond2.info, forward, noise_cov2, loose=0.4, depth=0.8)
	
	snr = 3.0
	lambda2 = 1.0 / snr **2
	dSPM = True

	stccond1 = apply_inverse(evokedcond1, inverse_operator1, lambda2, 'dSPM', pick_normal=False)
	stccond1.save("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/"+ListSubj[i]+"_"+condnames[0]+"_dPSMinverse_ico-5-fwd.fif")

	stc_fsaverage_cond1 = mne.morph_data(ListSubj[i], 'fsaverage', stccond1)
	stc_fsaverage_cond1.save("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/"+ListSubj[i]+"_"+condnames[0]+"_dPSMinverse_ico-5-fwd-fsaverage.fif")

	stccond2 = apply_inverse(evokedcond2, inverse_operator2, lambda2, 'dSPM', pick_normal=False)
	stccond2.save("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/"+ListSubj[i]+"_"+condnames[1]+"_dPSMinverse_ico-5-fwd.fif")

	stc_fsaverage_cond2 = mne.morph_data(ListSubj[i], 'fsaverage', stccond2)
	stc_fsaverage_cond2.save("/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"+ListSubj[i]+"/mne_python/"+ListSubj[i]+"_"+condnames[1]+"_dPSMinverse_ico-5-fwd-fsaverage.fif")
Пример #47
0
def test_cov_estimation_with_triggers():
    """Test estimation from raw with triggers
    """
    raw = Raw(raw_fname, preload=False)
    events = find_events(raw, stim_channel='STI 014')
    event_ids = [1, 2, 3, 4]
    reject = dict(grad=10000e-13, mag=4e-12, eeg=80e-6, eog=150e-6)

    # cov with merged events and keep_sample_mean=True
    events_merged = merge_events(events, event_ids, 1234)
    epochs = Epochs(raw,
                    events_merged,
                    1234,
                    tmin=-0.2,
                    tmax=0,
                    baseline=(-0.2, -0.1),
                    proj=True,
                    reject=reject,
                    preload=True)

    cov = compute_covariance(epochs, keep_sample_mean=True)
    cov_mne = read_cov(cov_km_fname)
    assert_true(cov_mne.ch_names == cov.ch_names)
    assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro') /
                 linalg.norm(cov.data, ord='fro')) < 0.005)

    # Test with tmin and tmax (different but not too much)
    cov_tmin_tmax = compute_covariance(epochs, tmin=-0.19, tmax=-0.01)
    assert_true(np.all(cov.data != cov_tmin_tmax.data))
    assert_true((linalg.norm(cov.data - cov_tmin_tmax.data, ord='fro') /
                 linalg.norm(cov_tmin_tmax.data, ord='fro')) < 0.05)

    # cov using a list of epochs and keep_sample_mean=True
    epochs = [
        Epochs(raw,
               events,
               ev_id,
               tmin=-0.2,
               tmax=0,
               baseline=(-0.2, -0.1),
               proj=True,
               reject=reject) for ev_id in event_ids
    ]

    cov2 = compute_covariance(epochs, keep_sample_mean=True)
    assert_array_almost_equal(cov.data, cov2.data)
    assert_true(cov.ch_names == cov2.ch_names)

    # cov with keep_sample_mean=False using a list of epochs
    cov = compute_covariance(epochs, keep_sample_mean=False)
    cov_mne = read_cov(cov_fname)
    assert_true(cov_mne.ch_names == cov.ch_names)
    assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro') /
                 linalg.norm(cov.data, ord='fro')) < 0.005)

    # test IO when computation done in Python
    cov.save(op.join(tempdir, 'test-cov.fif'))  # test saving
    cov_read = read_cov(op.join(tempdir, 'test-cov.fif'))
    assert_true(cov_read.ch_names == cov.ch_names)
    assert_true(cov_read.nfree == cov.nfree)
    assert_true((linalg.norm(cov.data - cov_read.data, ord='fro') /
                 linalg.norm(cov.data, ord='fro')) < 1e-5)

    # cov with list of epochs with different projectors
    epochs = [
        Epochs(raw,
               events[:4],
               event_ids[0],
               tmin=-0.2,
               tmax=0,
               baseline=(-0.2, -0.1),
               proj=True,
               reject=reject),
        Epochs(raw,
               events[:4],
               event_ids[0],
               tmin=-0.2,
               tmax=0,
               baseline=(-0.2, -0.1),
               proj=False,
               reject=reject)
    ]
    # these should fail
    assert_raises(ValueError, compute_covariance, epochs)
    assert_raises(ValueError, compute_covariance, epochs, projs=None)
    # these should work, but won't be equal to above
    with warnings.catch_warnings(record=True) as w:  # too few samples warning
        warnings.simplefilter('always')
        cov = compute_covariance(epochs, projs=epochs[0].info['projs'])
        cov = compute_covariance(epochs, projs=[])
    assert_true(len(w) == 2)

    # test new dict support
    epochs = Epochs(raw,
                    events,
                    dict(a=1, b=2, c=3, d=4),
                    tmin=-0.2,
                    tmax=0,
                    baseline=(-0.2, -0.1),
                    proj=True,
                    reject=reject)
    compute_covariance(epochs)
Пример #48
0
def test_array():
    """Test creating raw from array
    """
    # creating
    raw = Raw(fif_fname).crop(2, 5, copy=False)
    data, times = raw[:, :]
    sfreq = raw.info['sfreq']
    ch_names = [(ch[4:] if 'STI' not in ch else ch)
                for ch in raw.info['ch_names']]  # change them, why not
    #del raw
    types = list()
    for ci in range(102):
        types.extend(('grad', 'grad', 'mag'))
    types.extend(['stim'] * 9)
    types.extend(['eeg'] * 60)
    # wrong length
    assert_raises(ValueError, create_info, ch_names, sfreq, types)
    # bad entry
    types.append('foo')
    assert_raises(KeyError, create_info, ch_names, sfreq, types)
    types[-1] = 'eog'
    info = create_info(ch_names, sfreq, types)
    raw2 = RawArray(data, info)
    data2, times2 = raw2[:, :]
    assert_allclose(data, data2)
    assert_allclose(times, times2)

    # saving
    temp_fname = op.join(tempdir, 'raw.fif')
    raw2.save(temp_fname)
    raw3 = Raw(temp_fname)
    data3, times3 = raw3[:, :]
    assert_allclose(data, data3)
    assert_allclose(times, times3)

    # filtering
    picks = pick_types(raw2.info, misc=True, exclude='bads')[:4]
    assert_equal(len(picks), 4)
    raw_lp = raw2.copy()
    with warnings.catch_warnings(record=True):
        raw_lp.filter(0., 4.0 - 0.25, picks=picks, n_jobs=2)
    raw_hp = raw2.copy()
    with warnings.catch_warnings(record=True):
        raw_hp.filter(8.0 + 0.25, None, picks=picks, n_jobs=2)
    raw_bp = raw2.copy()
    with warnings.catch_warnings(record=True):
        raw_bp.filter(4.0 + 0.25, 8.0 - 0.25, picks=picks)
    raw_bs = raw2.copy()
    with warnings.catch_warnings(record=True):
        raw_bs.filter(8.0 + 0.25, 4.0 - 0.25, picks=picks, n_jobs=2)
    data, _ = raw2[picks, :]
    lp_data, _ = raw_lp[picks, :]
    hp_data, _ = raw_hp[picks, :]
    bp_data, _ = raw_bp[picks, :]
    bs_data, _ = raw_bs[picks, :]
    sig_dec = 11
    assert_array_almost_equal(data, lp_data + bp_data + hp_data, sig_dec)
    assert_array_almost_equal(data, bp_data + bs_data, sig_dec)

    # plotting
    import matplotlib
    matplotlib.use('Agg')  # for testing don't use X server
    raw2.plot()
    raw2.plot_psds()

    # epoching
    events = find_events(raw2, stim_channel='STI 014')
    events[:, 2] = 1
    assert_true(len(events) > 2)
    epochs = Epochs(raw2, events, 1, -0.2, 0.4, preload=True)
    epochs.plot_drop_log()
    epochs.plot()
    evoked = epochs.average()
    evoked.plot()
Пример #49
0
def test_stim_elim():
    """Test eliminate stim artifact"""
    raw = Raw(raw_fname, preload=True)
    events = read_events(event_fname)
    event_idx = np.where(events[:, 2] == 1)[0][0]
    tidx = int(events[event_idx, 0] - raw.first_samp)

    # use window around stimulus
    tmin = -0.02
    tmax = 0.02
    test_tminidx = int(-0.01 * raw.info['sfreq'])
    test_tmaxidx = int(0.01 * raw.info['sfreq'])

    raw = eliminate_stim_artifact(raw,
                                  events,
                                  event_id=1,
                                  tmin=tmin,
                                  tmax=tmax,
                                  mode='linear')
    data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)]
    diff_data0 = np.diff(data[0])
    diff_data0 -= np.mean(diff_data0)
    assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0)))
    raw = eliminate_stim_artifact(raw,
                                  events,
                                  event_id=1,
                                  tmin=tmin,
                                  tmax=tmax,
                                  mode='window')
    data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)]
    assert_true(np.all(data) == 0.)

    # use window before stimulus
    tmin = -0.045
    tmax = 0.015
    test_tminidx = int(-0.035 * raw.info['sfreq'])
    test_tmaxidx = int(-0.015 * raw.info['sfreq'])

    raw = eliminate_stim_artifact(raw,
                                  events,
                                  event_id=1,
                                  tmin=tmin,
                                  tmax=tmax,
                                  mode='linear')
    data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)]
    diff_data0 = np.diff(data[0])
    diff_data0 -= np.mean(diff_data0)
    assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0)))
    raw = eliminate_stim_artifact(raw,
                                  events,
                                  event_id=1,
                                  tmin=tmin,
                                  tmax=tmax,
                                  mode='window')
    data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)]
    assert_true(np.all(data) == 0.)

    # use window after stimulus
    tmin = 0.005
    tmax = 0.045
    test_tminidx = int(0.015 * raw.info['sfreq'])
    test_tmaxidx = int(0.035 * raw.info['sfreq'])

    raw = eliminate_stim_artifact(raw,
                                  events,
                                  event_id=1,
                                  tmin=tmin,
                                  tmax=tmax,
                                  mode='linear')
    data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)]
    diff_data0 = np.diff(data[0])
    diff_data0 -= np.mean(diff_data0)
    assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0)))
    raw = eliminate_stim_artifact(raw,
                                  events,
                                  event_id=1,
                                  tmin=tmin,
                                  tmax=tmax,
                                  mode='window')
    data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)]
    assert_true(np.all(data) == 0.)
Пример #50
0
def test_multiple_files():
    """Test loading multiple files simultaneously
    """
    # split file
    raw = Raw(fif_fname, preload=True).crop(0, 10)
    split_size = 3.  # in seconds
    sfreq = raw.info['sfreq']
    nsamp = (raw.last_samp - raw.first_samp)
    tmins = np.round(np.arange(0., nsamp, split_size * sfreq))
    tmaxs = np.concatenate((tmins[1:] - 1, [nsamp]))
    tmaxs /= sfreq
    tmins /= sfreq
    assert_equal(raw.n_times, len(raw._times))

    # going in reverse order so the last fname is the first file (need later)
    raws = [None] * len(tmins)
    for ri in range(len(tmins) - 1, -1, -1):
        fname = op.join(tempdir, 'test_raw_split-%d_raw.fif' % ri)
        raw.save(fname, tmin=tmins[ri], tmax=tmaxs[ri])
        raws[ri] = Raw(fname)
    events = [find_events(r, stim_channel='STI 014') for r in raws]
    last_samps = [r.last_samp for r in raws]
    first_samps = [r.first_samp for r in raws]

    # test concatenation of split file
    all_raw_1 = concatenate_raws(raws, preload=False)
    assert_true(raw.first_samp == all_raw_1.first_samp)
    assert_true(raw.last_samp == all_raw_1.last_samp)
    assert_allclose(raw[:, :][0], all_raw_1[:, :][0])
    raws[0] = Raw(fname)
    all_raw_2 = concatenate_raws(raws, preload=True)
    assert_allclose(raw[:, :][0], all_raw_2[:, :][0])

    # test proper event treatment for split files
    events = concatenate_events(events, first_samps, last_samps)
    events2 = find_events(all_raw_2, stim_channel='STI 014')
    assert_array_equal(events, events2)

    # test various methods of combining files
    raw = Raw(fif_fname, preload=True)
    n_times = len(raw._times)
    # make sure that all our data match
    times = list(range(0, 2 * n_times, 999))
    # add potentially problematic points
    times.extend([n_times - 1, n_times, 2 * n_times - 1])

    raw_combo0 = Raw([fif_fname, fif_fname], preload=True)
    _compare_combo(raw, raw_combo0, times, n_times)
    raw_combo = Raw([fif_fname, fif_fname], preload=False)
    _compare_combo(raw, raw_combo, times, n_times)
    raw_combo = Raw([fif_fname, fif_fname], preload='memmap8.dat')
    _compare_combo(raw, raw_combo, times, n_times)
    assert_raises(ValueError, Raw, [fif_fname, ctf_fname])
    assert_raises(ValueError, Raw, [fif_fname, fif_bad_marked_fname])
    assert_true(raw[:, :][0].shape[1] * 2 == raw_combo0[:, :][0].shape[1])
    assert_true(raw_combo0[:, :][0].shape[1] == len(raw_combo0._times))

    # with all data preloaded, result should be preloaded
    raw_combo = Raw(fif_fname, preload=True)
    raw_combo.append(Raw(fif_fname, preload=True))
    assert_true(raw_combo._preloaded is True)
    assert_true(len(raw_combo._times) == raw_combo._data.shape[1])
    _compare_combo(raw, raw_combo, times, n_times)

    # with any data not preloaded, don't set result as preloaded
    raw_combo = concatenate_raws(
        [Raw(fif_fname, preload=True),
         Raw(fif_fname, preload=False)])
    assert_true(raw_combo._preloaded is False)
    assert_array_equal(find_events(raw_combo, stim_channel='STI 014'),
                       find_events(raw_combo0, stim_channel='STI 014'))
    _compare_combo(raw, raw_combo, times, n_times)

    # user should be able to force data to be preloaded upon concat
    raw_combo = concatenate_raws(
        [Raw(fif_fname, preload=False),
         Raw(fif_fname, preload=True)],
        preload=True)
    assert_true(raw_combo._preloaded is True)
    _compare_combo(raw, raw_combo, times, n_times)

    raw_combo = concatenate_raws(
        [Raw(fif_fname, preload=False),
         Raw(fif_fname, preload=True)],
        preload='memmap3.dat')
    _compare_combo(raw, raw_combo, times, n_times)

    raw_combo = concatenate_raws(
        [Raw(fif_fname, preload=True),
         Raw(fif_fname, preload=True)],
        preload='memmap4.dat')
    _compare_combo(raw, raw_combo, times, n_times)

    raw_combo = concatenate_raws(
        [Raw(fif_fname, preload=False),
         Raw(fif_fname, preload=False)],
        preload='memmap5.dat')
    _compare_combo(raw, raw_combo, times, n_times)

    # verify that combining raws with different projectors throws an exception
    raw.add_proj([], remove_existing=True)
    assert_raises(ValueError, raw.append, Raw(fif_fname, preload=True))

    # now test event treatment for concatenated raw files
    events = [
        find_events(raw, stim_channel='STI 014'),
        find_events(raw, stim_channel='STI 014')
    ]
    last_samps = [raw.last_samp, raw.last_samp]
    first_samps = [raw.first_samp, raw.first_samp]
    events = concatenate_events(events, first_samps, last_samps)
    events2 = find_events(raw_combo0, stim_channel='STI 014')
    assert_array_equal(events, events2)

    # check out the len method
    assert_true(len(raw) == raw.n_times)
    assert_true(len(raw) == raw.last_samp - raw.first_samp + 1)
Пример #51
0
def test_do_forward_solution():
    """Test making forward solution from python
    """
    subjects_dir = os.path.join(data_path, 'subjects')

    raw = Raw(fname_raw)
    mri = read_trans(fname_mri)
    fname_fake = op.join(temp_dir, 'no_have.fif')

    # ## Error checks
    # bad subject
    assert_raises(ValueError, do_forward_solution, 1, fname_raw,
                  subjects_dir=subjects_dir)
    # bad meas
    assert_raises(ValueError, do_forward_solution, 'sample', 1,
                  subjects_dir=subjects_dir)
    # meas doesn't exist
    assert_raises(IOError, do_forward_solution, 'sample', fname_fake,
                  subjects_dir=subjects_dir)
    # don't specify trans and meas
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  subjects_dir=subjects_dir)
    # specify both trans and meas
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  trans='me', mri='you', subjects_dir=subjects_dir)
    # specify non-existent trans
    assert_raises(IOError, do_forward_solution, 'sample', fname_raw,
                  trans=fname_fake, subjects_dir=subjects_dir)
    # specify non-existent mri
    assert_raises(IOError, do_forward_solution, 'sample', fname_raw,
                  mri=fname_fake, subjects_dir=subjects_dir)
    # specify non-string mri
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  mri=1, subjects_dir=subjects_dir)
    # specify non-string trans
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  trans=1, subjects_dir=subjects_dir)
    # test specifying an actual trans in python space -- this should work but
    # the transform I/O reduces our accuracy -- so we'll just hack a test here
    # by making it bomb with eeg=False and meg=False
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  mri=mri, eeg=False, meg=False, subjects_dir=subjects_dir)
    # mindist as non-integer
    assert_raises(TypeError, do_forward_solution, 'sample', fname_raw,
                  mri=fname_mri, mindist=dict(), subjects_dir=subjects_dir)
    # mindist as string but not 'all'
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  mri=fname_mri, eeg=False, mindist='yall',
                  subjects_dir=subjects_dir)
    # src, spacing, and bem as non-str
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  mri=fname_mri, src=1, subjects_dir=subjects_dir)
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  mri=fname_mri, spacing=1, subjects_dir=subjects_dir)
    assert_raises(ValueError, do_forward_solution, 'sample', fname_raw,
                  mri=fname_mri, bem=1, subjects_dir=subjects_dir)
    # no overwrite flag
    assert_raises(IOError, do_forward_solution, 'sample', fname_raw,
                  existing_file, mri=fname_mri, subjects_dir=subjects_dir)
    # let's catch an MNE error, this time about trans being wrong
    assert_raises(CalledProcessError, do_forward_solution, 'sample', fname_raw,
                  existing_file, trans=fname_mri, overwrite=True,
                  spacing='oct-6', subjects_dir=subjects_dir)

    # ## Actually calculate one and check
    # make a meas from raw (tests all steps in creating evoked),
    # don't do EEG or 5120-5120-5120 BEM because they're ~3x slower
    fwd_py = do_forward_solution('sample', raw, mindist=5, spacing='oct-6',
                                 bem='sample-5120', mri=fname_mri, eeg=False,
                                 subjects_dir=subjects_dir)
    fwd = read_forward_solution(fname)
    assert_allclose(fwd['sol']['data'], fwd_py['sol']['data'],
                    rtol=1e-5, atol=1e-8)
    assert_equal(fwd_py['sol']['data'].shape, (306, 22494))
    assert_equal(len(fwd['sol']['row_names']), 306)
Пример #52
0
def test_raw_index_as_time():
    """ Test index as time conversion"""
    raw = Raw(fif_fname, preload=True)
    t0 = raw.index_as_time([0], True)[0]
    t1 = raw.index_as_time([100], False)[0]
    t2 = raw.index_as_time([100], True)[0]
    assert_true((t2 - t1) == t0)
    # ensure we can go back and forth
    t3 = raw.index_as_time(raw.time_as_index([0], True), True)
    assert_array_almost_equal(t3, [0.0], 2)
    t3 = raw.index_as_time(raw.time_as_index(raw.info['sfreq'], True), True)
    assert_array_almost_equal(t3, [raw.info['sfreq']], 2)
    t3 = raw.index_as_time(raw.time_as_index(raw.info['sfreq'], False), False)
    assert_array_almost_equal(t3, [raw.info['sfreq']], 2)
    i0 = raw.time_as_index(raw.index_as_time([0], True), True)
    assert_true(i0[0] == 0)
    i1 = raw.time_as_index(raw.index_as_time([100], True), True)
    assert_true(i1[0] == 100)
    # Have to add small amount of time because we truncate via int casting
    i1 = raw.time_as_index(raw.index_as_time([100.0001], False), False)
    assert_true(i1[0] == 100)
Пример #53
0
def test_load_bad_channels():
    """Test reading/writing of bad channels
    """
    # Load correctly marked file (manually done in mne_process_raw)
    raw_marked = Raw(fif_bad_marked_fname)
    correct_bads = raw_marked.info['bads']
    raw = Raw(fif_fname)
    # Make sure it starts clean
    assert_array_equal(raw.info['bads'], [])

    # Test normal case
    raw.load_bad_channels(bad_file_works)
    # Write it out, read it in, and check
    raw.save(op.join(tempdir, 'foo_raw.fif'))
    raw_new = Raw(op.join(tempdir, 'foo_raw.fif'))
    assert_equal(correct_bads, raw_new.info['bads'])
    # Reset it
    raw.info['bads'] = []

    # Test bad case
    assert_raises(ValueError, raw.load_bad_channels, bad_file_wrong)

    # Test forcing the bad case
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        raw.load_bad_channels(bad_file_wrong, force=True)
        n_found = sum(['1 bad channel' in str(ww.message) for ww in w])
        assert_equal(n_found, 1)  # there could be other irrelevant errors
        # write it out, read it in, and check
        raw.save(op.join(tempdir, 'foo_raw.fif'), overwrite=True)
        raw_new = Raw(op.join(tempdir, 'foo_raw.fif'))
        assert_equal(correct_bads, raw_new.info['bads'])

    # Check that bad channels are cleared
    raw.load_bad_channels(None)
    raw.save(op.join(tempdir, 'foo_raw.fif'), overwrite=True)
    raw_new = Raw(op.join(tempdir, 'foo_raw.fif'))
    assert_equal([], raw_new.info['bads'])
Пример #54
0
def test_io_raw():
    """Test IO for raw data (Neuromag + CTF + gz)
    """
    # test unicode io
    for chars in [b'\xc3\xa4\xc3\xb6\xc3\xa9', b'a']:
        with Raw(fif_fname) as r:
            desc1 = r.info['description'] = chars.decode('utf-8')
            temp_file = op.join(tempdir, 'raw.fif')
            r.save(temp_file, overwrite=True)
            with Raw(temp_file) as r2:
                desc2 = r2.info['description']
            assert_equal(desc1, desc2)

    # Let's construct a simple test for IO first
    raw = Raw(fif_fname, preload=True)
    raw.crop(0, 3.5)
    # put in some data that we know the values of
    data = np.random.randn(raw._data.shape[0], raw._data.shape[1])
    raw._data[:, :] = data
    # save it somewhere
    fname = op.join(tempdir, 'test_copy_raw.fif')
    raw.save(fname, buffer_size_sec=1.0)
    # read it in, make sure the whole thing matches
    raw = Raw(fname)
    assert_true(np.allclose(data, raw[:, :][0], 1e-6, 1e-20))
    # let's read portions across the 1-sec tag boundary, too
    inds = raw.time_as_index([1.75, 2.25])
    sl = slice(inds[0], inds[1])
    assert_true(np.allclose(data[:, sl], raw[:, sl][0], 1e-6, 1e-20))

    # now let's do some real I/O
    fnames_in = [fif_fname, fif_gz_fname, ctf_fname]
    fnames_out = ['raw.fif', 'raw.fif.gz', 'raw.fif']
    for fname_in, fname_out in zip(fnames_in, fnames_out):
        fname_out = op.join(tempdir, fname_out)
        raw = Raw(fname_in)

        nchan = raw.info['nchan']
        ch_names = raw.info['ch_names']
        meg_channels_idx = [k for k in range(nchan) if ch_names[k][0] == 'M']
        n_channels = 100
        meg_channels_idx = meg_channels_idx[:n_channels]
        start, stop = raw.time_as_index([0, 5])
        data, times = raw[meg_channels_idx, start:(stop + 1)]
        meg_ch_names = [ch_names[k] for k in meg_channels_idx]

        # Set up pick list: MEG + STI 014 - bad channels
        include = ['STI 014']
        include += meg_ch_names
        picks = pick_types(raw.info,
                           meg=True,
                           eeg=False,
                           stim=True,
                           misc=True,
                           ref_meg=True,
                           include=include,
                           exclude='bads')

        # Writing with drop_small_buffer True
        raw.save(fname_out,
                 picks,
                 tmin=0,
                 tmax=4,
                 buffer_size_sec=3,
                 drop_small_buffer=True,
                 overwrite=True)
        raw2 = Raw(fname_out, preload=True)

        sel = pick_channels(raw2.ch_names, meg_ch_names)
        data2, times2 = raw2[sel, :]
        assert_true(times2.max() <= 3)

        # Writing
        raw.save(fname_out, picks, tmin=0, tmax=5, overwrite=True)

        if fname_in == fif_fname or fname_in == fif_fname + '.gz':
            assert_true(len(raw.info['dig']) == 146)

        raw2 = Raw(fname_out)

        sel = pick_channels(raw2.ch_names, meg_ch_names)
        data2, times2 = raw2[sel, :]

        assert_true(np.allclose(data, data2, 1e-6, 1e-20))
        assert_allclose(times, times2)
        assert_allclose(raw.info['sfreq'], raw2.info['sfreq'], rtol=1e-5)

        # check transformations
        for trans in ['dev_head_t', 'dev_ctf_t', 'ctf_head_t']:
            if raw.info[trans] is None:
                assert_true(raw2.info[trans] is None)
            else:
                assert_array_equal(raw.info[trans]['trans'],
                                   raw2.info[trans]['trans'])

                # check transformation 'from' and 'to'
                if trans.startswith('dev'):
                    from_id = FIFF.FIFFV_COORD_DEVICE
                else:
                    from_id = FIFF.FIFFV_MNE_COORD_CTF_HEAD
                if trans[4:8] == 'head':
                    to_id = FIFF.FIFFV_COORD_HEAD
                else:
                    to_id = FIFF.FIFFV_MNE_COORD_CTF_HEAD
                for raw_ in [raw, raw2]:
                    assert_true(raw_.info[trans]['from'] == from_id)
                    assert_true(raw_.info[trans]['to'] == to_id)

        if fname_in == fif_fname or fname_in == fif_fname + '.gz':
            assert_allclose(raw.info['dig'][0]['r'], raw2.info['dig'][0]['r'])
import numpy as np
import matplotlib.pyplot as plt

from mne import read_proj, read_forward_solution, read_cov, read_label
from mne.fiff.pick import pick_types_evoked, pick_types_forward
from mne.fiff import read_evokeds, Raw, pick_types
from mne.datasets import sample
from mne.time_frequency import iir_filter_raw, morlet
from mne.viz import plot_evoked, plot_sparse_source_estimates
from mne.simulation import generate_sparse_stc, generate_evoked

###############################################################################
# Load real data as templates
data_path = sample.data_path()

raw = Raw(data_path + '/MEG/sample/sample_audvis_raw.fif')
proj = read_proj(data_path + '/MEG/sample/sample_audvis_ecg_proj.fif')
raw.info['projs'] += proj
raw.info['bads'] = ['MEG 2443', 'EEG 053']  # mark bad channels

fwd_fname = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif'
ave_fname = data_path + '/MEG/sample/sample_audvis-no-filter-ave.fif'
cov_fname = data_path + '/MEG/sample/sample_audvis-cov.fif'

fwd = read_forward_solution(fwd_fname, force_fixed=True, surf_ori=True)
fwd = pick_types_forward(fwd, meg=True, eeg=True, exclude=raw.info['bads'])

cov = read_cov(cov_fname)

condition = 'Left Auditory'
evoked_template = read_evokeds(ave_fname, condition=condition, baseline=None)