Example #1
0
def test_add_reorder(n_ref):
    """Test that a reference channel can be added and then data reordered."""
    # gh-8300
    raw = read_raw_fif(raw_fname).crop(0, 0.1).del_proj().pick('eeg')
    assert len(raw.ch_names) == 60
    chs = ['EEG %03d' % (60 + ii) for ii in range(1, n_ref)] + ['EEG 000']
    with pytest.raises(RuntimeError, match='preload'):
        with pytest.warns(None):  # ignore multiple warning
            add_reference_channels(raw, chs, copy=False)
    raw.load_data()
    if n_ref == 1:
        ctx = nullcontext()
    else:
        assert n_ref == 2
        ctx = pytest.warns(RuntimeWarning, match='locations of multiple')
    with ctx:
        add_reference_channels(raw, chs, copy=False)
    data = raw.get_data()
    assert_array_equal(data[-1], 0.)
    assert raw.ch_names[-n_ref:] == chs
    raw.reorder_channels(raw.ch_names[-1:] + raw.ch_names[:-1])
    assert raw.ch_names == ['EEG %03d' % ii for ii in range(60 + n_ref)]
    data_new = raw.get_data()
    data_new = np.concatenate([data_new[1:], data_new[:1]])
    assert_allclose(data, data_new)
Example #2
0
def test_hp_lp_reversed(fname, lo, hi, warns, monkeypatch):
    """Test HP/LP reversed (gh-8584)."""
    fname = str(fname)
    raw = read_raw_edf(fname)
    assert raw.info['lowpass'] == lo
    assert raw.info['highpass'] == hi
    monkeypatch.setattr(edf.edf, '_read_edf_header', _hp_lp_rev)
    if warns:
        ctx = pytest.warns(RuntimeWarning, match='greater than lowpass')
        new_lo, new_hi = raw.info['sfreq'] / 2., 0.
    else:
        ctx = nullcontext()
        new_lo, new_hi = lo, hi
    with ctx:
        raw = read_raw_edf(fname)
    assert raw.info['lowpass'] == new_lo
    assert raw.info['highpass'] == new_hi
Example #3
0
def test_notch_filters(method, filter_length, line_freq, tol):
    """Test notch filters."""
    # let's use an ugly, prime sfreq for fun
    rng = np.random.RandomState(0)
    sfreq = 487
    sig_len_secs = 21
    t = np.arange(0, int(round(sig_len_secs * sfreq))) / sfreq

    # make a "signal"
    a = rng.randn(int(sig_len_secs * sfreq))
    orig_power = np.sqrt(np.mean(a**2))
    # make line noise
    a += np.sum([np.sin(2 * np.pi * f * t) for f in line_freqs], axis=0)

    # only allow None line_freqs with 'spectrum_fit' mode
    pytest.raises(ValueError, notch_filter, a, sfreq, None, 'fft')
    pytest.raises(ValueError, notch_filter, a, sfreq, None, 'iir')
    if method == 'spectrum_fit' and filter_length == 'auto':
        ctx = pytest.deprecated_call(match='will change to 10.')
    else:
        ctx = nullcontext()
    with catch_logging() as log_file:
        with ctx:
            b = notch_filter(a,
                             sfreq,
                             line_freq,
                             filter_length,
                             method=method,
                             verbose=True)
    if line_freq is None:
        out = [
            line.strip().split(':')[0]
            for line in log_file.getvalue().split('\n') if line.startswith(' ')
        ]
        assert len(out) == 4, 'Detected frequencies not logged properly'
        out = np.array(out, float)
        assert_array_almost_equal(out, line_freqs)
    new_power = np.sqrt(sum_squared(b) / b.size)
    assert_almost_equal(new_power, orig_power, tol)