Beispiel #1
0
def test_compute_csd():
    """Test computing cross-spectral density from ndarray."""
    epochs = _get_real_data()

    tmin = 0.04
    tmax = 0.15
    tmp = np.where(np.logical_and(epochs.times >= tmin,
                                  epochs.times <= tmax))[0]

    picks_meeg = mne.pick_types(epochs[0].info,
                                meg=True,
                                eeg=True,
                                eog=False,
                                ref_meg=False,
                                exclude='bads')

    epochs_data = [e[picks_meeg][:, tmp].copy() for e in epochs]
    n_trials = len(epochs)
    n_series = len(picks_meeg)
    X = np.concatenate(epochs_data, axis=0)
    X = np.reshape(X, (n_trials, n_series, -1))
    X_list = epochs_data

    sfreq = epochs.info['sfreq']

    # Check data types and sizes are checked
    diff_types = [np.random.randn(3, 5), "error"]
    err_data = [np.random.randn(3, 5), np.random.randn(2, 4)]
    raises(ValueError, csd_array, err_data, sfreq)
    raises(ValueError, csd_array, diff_types, sfreq)
    raises(ValueError, csd_array, np.random.randn(3), sfreq)

    # Check that wrong parameters are recognized
    raises(ValueError, csd_array, X, sfreq, mode='notamode')
    raises(ValueError, csd_array, X, sfreq, fmin=20, fmax=10)
    raises(ValueError, csd_array, X, sfreq, fmin=20, fmax=20.1)

    # Test deprecation warning
    with warnings.catch_warnings(record=True) as ws:
        warnings.simplefilter('always')
        csd_mt = csd_array(X, sfreq, mode='multitaper', fmin=8, fmax=12)
    assert len([w for w in ws
                if issubclass(w.category, DeprecationWarning)]) == 1

    csd_fourier = csd_array(X, sfreq, mode='fourier', fmin=8, fmax=12)

    # Test as list too
    csd_mt_list = csd_array(X_list, sfreq, mode='multitaper', fmin=8, fmax=12)
    csd_fourier_list = csd_array(X_list,
                                 sfreq,
                                 mode='fourier',
                                 fmin=8,
                                 fmax=12)

    assert_array_equal(csd_mt._data, csd_mt_list._data)
    assert_array_equal(csd_fourier._data, csd_fourier_list._data)
    assert_array_equal(csd_mt.frequencies, csd_mt_list.frequencies)
    assert_array_equal(csd_fourier.frequencies, csd_fourier_list.frequencies)

    # Check shape of the CSD matrix
    n_chan = len(epochs.ch_names)
    csd_mt_data = csd_mt.get_data()
    csd_fourier_data = csd_fourier.get_data()
    assert csd_mt_data.shape == (n_chan, n_chan)
    assert csd_fourier_data.shape == (n_chan, n_chan)

    # Check if the CSD matrix is hermitian
    assert_array_equal(np.tril(csd_mt_data).T.conj(), np.triu(csd_mt_data))
    assert_array_equal(
        np.tril(csd_fourier_data).T.conj(), np.triu(csd_fourier_data))

    # Computing induced power for comparison
    epochs.crop(tmin=0.04, tmax=0.15)
    tfr = tfr_morlet(epochs, freqs=[10], n_cycles=0.6, return_itc=False)
    power = np.mean(tfr.data, 2)

    # Maximum PSD should occur for specific channel
    max_ch_power = power.argmax()
    max_ch_mt = csd_mt_data.diagonal().argmax()
    max_ch_fourier = csd_fourier_data.diagonal().argmax()
    assert max_ch_mt == max_ch_power
    assert max_ch_fourier == max_ch_power

    # Maximum CSD should occur for specific channel
    ch_csd_mt = np.abs(csd_mt_data[max_ch_power])
    ch_csd_mt[max_ch_power] = 0.
    max_ch_csd_mt = np.argmax(ch_csd_mt)
    ch_csd_fourier = np.abs(csd_fourier_data[max_ch_power])
    ch_csd_fourier[max_ch_power] = 0.
    max_ch_csd_fourier = np.argmax(ch_csd_fourier)
    assert max_ch_csd_mt == max_ch_csd_fourier

    # Check a list of CSD matrices is returned for multiple frequencies within
    # a given range when fsum=False
    csd_fsum = csd_array(X, sfreq, mode='fourier', fmin=8, fmax=20, fsum=True)
    csds = csd_array(X, sfreq, mode='fourier', fmin=8, fmax=20, fsum=False)

    assert csds._data.shape[1] == 2
    assert len(csds.frequencies) == 2
    assert_array_equal(csd_fsum.frequencies[0], csds.frequencies)
    assert_array_equal(csd_fsum._data, csds._data.sum(axis=1, keepdims=True))
Beispiel #2
0
def test_compute_csd():
    """Test computing cross-spectral density from ndarray."""
    epochs = _get_real_data()

    tmin = 0.04
    tmax = 0.15
    tmp = np.where(np.logical_and(epochs.times >= tmin,
                                  epochs.times <= tmax))[0]

    picks_meeg = mne.pick_types(epochs[0].info, meg=True, eeg=True, eog=False,
                                ref_meg=False, exclude='bads')

    epochs_data = [e[picks_meeg][:, tmp].copy() for e in epochs]
    n_trials = len(epochs)
    n_series = len(picks_meeg)
    X = np.concatenate(epochs_data, axis=0)
    X = np.reshape(X, (n_trials, n_series, -1))
    X_list = epochs_data

    sfreq = epochs.info['sfreq']

    # Check data types and sizes are checked
    diff_types = [np.random.randn(3, 5), "error"]
    err_data = [np.random.randn(3, 5), np.random.randn(2, 4)]
    with warnings.catch_warnings(record=True):  # deprecation
        raises(ValueError, csd_array, err_data, sfreq)
        raises(ValueError, csd_array, diff_types, sfreq)
        raises(ValueError, csd_array, np.random.randn(3), sfreq)

        # Check that wrong parameters are recognized
        raises(ValueError, csd_array, X, sfreq, mode='notamode')
        raises(ValueError, csd_array, X, sfreq, fmin=20, fmax=10)
        raises(ValueError, csd_array, X, sfreq, fmin=20, fmax=20.1)

    # Test deprecation warning
    with warnings.catch_warnings(record=True) as ws:
        warnings.simplefilter('always')
        csd_mt = csd_array(X, sfreq, mode='multitaper', fmin=8, fmax=12)
    assert len([w for w in ws
                if issubclass(w.category, DeprecationWarning)]) == 1

    with warnings.catch_warnings(record=True):  # deprecation
        csd_fourier = csd_array(X, sfreq, mode='fourier', fmin=8, fmax=12)

    # Test as list too
    with warnings.catch_warnings(record=True):  # deprecation
        csd_mt_list = csd_array(X_list, sfreq, mode='multitaper',
                                fmin=8, fmax=12)
        csd_fourier_list = csd_array(X_list, sfreq, mode='fourier', fmin=8,
                                     fmax=12)

    assert_array_equal(csd_mt._data, csd_mt_list._data)
    assert_array_equal(csd_fourier._data, csd_fourier_list._data)
    assert_array_equal(csd_mt.frequencies, csd_mt_list.frequencies)
    assert_array_equal(csd_fourier.frequencies, csd_fourier_list.frequencies)

    # Check shape of the CSD matrix
    n_chan = len(epochs.ch_names)
    csd_mt_data = csd_mt.get_data()
    csd_fourier_data = csd_fourier.get_data()
    assert csd_mt_data.shape == (n_chan, n_chan)
    assert csd_fourier_data.shape == (n_chan, n_chan)

    # Check if the CSD matrix is hermitian
    assert_array_equal(np.tril(csd_mt_data).T.conj(),
                       np.triu(csd_mt_data))
    assert_array_equal(np.tril(csd_fourier_data).T.conj(),
                       np.triu(csd_fourier_data))

    # Computing induced power for comparison
    epochs.crop(tmin=0.04, tmax=0.15)
    tfr = tfr_morlet(epochs, freqs=[10], n_cycles=0.6, return_itc=False)
    power = np.mean(tfr.data, 2)

    # Maximum PSD should occur for specific channel
    max_ch_power = power.argmax()
    max_ch_mt = csd_mt_data.diagonal().argmax()
    max_ch_fourier = csd_fourier_data.diagonal().argmax()
    assert max_ch_mt == max_ch_power
    assert max_ch_fourier == max_ch_power

    # Maximum CSD should occur for specific channel
    ch_csd_mt = np.abs(csd_mt_data[max_ch_power])
    ch_csd_mt[max_ch_power] = 0.
    max_ch_csd_mt = np.argmax(ch_csd_mt)
    ch_csd_fourier = np.abs(csd_fourier_data[max_ch_power])
    ch_csd_fourier[max_ch_power] = 0.
    max_ch_csd_fourier = np.argmax(ch_csd_fourier)
    assert max_ch_csd_mt == max_ch_csd_fourier

    # Check a list of CSD matrices is returned for multiple frequencies within
    # a given range when fsum=False
    with warnings.catch_warnings(record=True):  # deprecation
        csd_fsum = csd_array(X, sfreq, mode='fourier', fmin=8, fmax=20,
                             fsum=True)
        csds = csd_array(X, sfreq, mode='fourier', fmin=8, fmax=20,
                         fsum=False)

    assert csds._data.shape[1] == 2
    assert len(csds.frequencies) == 2
    assert_array_equal(csd_fsum.frequencies[0], csds.frequencies)
    assert_array_equal(csd_fsum._data, csds._data.sum(axis=1, keepdims=True))
Beispiel #3
0
def test_csd_epochs():
    """Test computing cross-spectral density from epochs."""
    epochs = _get_real_data()

    # Check that wrong parameters are recognized
    raises(ValueError, csd_epochs, epochs, mode='notamode')
    raises(ValueError, csd_epochs, epochs, fmin=20, fmax=10)
    raises(ValueError, csd_epochs, epochs, fmin=20, fmax=20.1)
    raises(ValueError, csd_epochs, epochs, tmin=0.15, tmax=0.1)
    raises(ValueError, csd_epochs, epochs, tmin=0, tmax=10)
    raises(ValueError, csd_epochs, epochs, tmin=10, tmax=11)

    # Test deprecation warning
    with warnings.catch_warnings(record=True) as ws:
        warnings.simplefilter('always')
        csd_mt = csd_epochs(epochs,
                            mode='multitaper',
                            fmin=8,
                            fmax=12,
                            tmin=0.04,
                            tmax=0.15)
    assert len([w for w in ws
                if issubclass(w.category, DeprecationWarning)]) == 1

    csd_fourier = csd_epochs(epochs,
                             mode='fourier',
                             fmin=8,
                             fmax=12,
                             tmin=0.04,
                             tmax=0.15)

    # Check shape of the CSD matrix
    n_chan = len(csd_mt.ch_names)
    csd_mt_data = csd_mt.get_data()
    csd_fourier_data = csd_fourier.get_data()
    assert csd_mt_data.shape == (n_chan, n_chan)
    assert csd_fourier_data.shape == (n_chan, n_chan)

    # Check if the CSD matrix is hermitian
    assert_array_equal(np.tril(csd_mt_data).T.conj(), np.triu(csd_mt_data))
    assert_array_equal(
        np.tril(csd_fourier_data).T.conj(), np.triu(csd_fourier_data))

    # Computing induced power for comparison
    epochs.crop(tmin=0.04, tmax=0.15)
    tfr = tfr_morlet(epochs, freqs=[10], n_cycles=0.6, return_itc=False)
    power = np.mean(tfr.data, 2)

    # Maximum PSD should occur for specific channel
    max_ch_power = power.argmax()
    max_ch_mt = csd_mt_data.diagonal().argmax()
    max_ch_fourier = csd_fourier_data.diagonal().argmax()
    assert max_ch_mt == max_ch_power
    assert max_ch_fourier == max_ch_power

    # Maximum CSD should occur for specific channel
    ch_csd_mt = np.abs(csd_mt_data[max_ch_power])
    ch_csd_mt[max_ch_power] = 0.
    max_ch_csd_mt = np.argmax(ch_csd_mt)
    ch_csd_fourier = np.abs(csd_fourier_data[max_ch_power])
    ch_csd_fourier[max_ch_power] = 0.
    max_ch_csd_fourier = np.argmax(ch_csd_fourier)
    assert max_ch_csd_mt == max_ch_csd_fourier

    # Check a list of CSD matrices is returned for multiple frequencies within
    # a given range when fsum=False
    csd_fsum = csd_epochs(epochs, mode='fourier', fmin=8, fmax=20, fsum=True)
    csds = csd_epochs(epochs, mode='fourier', fmin=8, fmax=20, fsum=False)
    assert len(csd_fsum.frequencies) == 1
    assert len(csds.frequencies) == 2
    assert_array_equal(csd_fsum.frequencies[0], csds.frequencies)

    csd_sum = csds._data.sum(axis=1, keepdims=True)
    assert_array_equal(csd_fsum._data, csd_sum)
Beispiel #4
0
def test_csd_epochs():
    """Test computing cross-spectral density from epochs."""
    epochs = _get_real_data()

    # Check that wrong parameters are recognized
    with warnings.catch_warnings(record=True):  # deprecation
        raises(ValueError, csd_epochs, epochs, mode='notamode')
        raises(ValueError, csd_epochs, epochs, fmin=20, fmax=10)
        raises(ValueError, csd_epochs, epochs, fmin=20, fmax=20.1)
        raises(ValueError, csd_epochs, epochs, tmin=0.15, tmax=0.1)
        raises(ValueError, csd_epochs, epochs, tmin=0, tmax=10)
        raises(ValueError, csd_epochs, epochs, tmin=10, tmax=11)

    # Test deprecation warning
    with warnings.catch_warnings(record=True) as ws:
        warnings.simplefilter('always')
        csd_mt = csd_epochs(epochs, mode='multitaper', fmin=8, fmax=12,
                            tmin=0.04, tmax=0.15)
    assert len([w for w in ws
                if issubclass(w.category, DeprecationWarning)]) == 1

    with warnings.catch_warnings(record=True):  # deprecation
        csd_fourier = csd_epochs(epochs, mode='fourier', fmin=8, fmax=12,
                                 tmin=0.04, tmax=0.15)

    # Check shape of the CSD matrix
    n_chan = len(csd_mt.ch_names)
    csd_mt_data = csd_mt.get_data()
    csd_fourier_data = csd_fourier.get_data()
    assert csd_mt_data.shape == (n_chan, n_chan)
    assert csd_fourier_data.shape == (n_chan, n_chan)

    # Check if the CSD matrix is hermitian
    assert_array_equal(np.tril(csd_mt_data).T.conj(),
                       np.triu(csd_mt_data))
    assert_array_equal(np.tril(csd_fourier_data).T.conj(),
                       np.triu(csd_fourier_data))

    # Computing induced power for comparison
    epochs.crop(tmin=0.04, tmax=0.15)
    tfr = tfr_morlet(epochs, freqs=[10], n_cycles=0.6, return_itc=False)
    power = np.mean(tfr.data, 2)

    # Maximum PSD should occur for specific channel
    max_ch_power = power.argmax()
    max_ch_mt = csd_mt_data.diagonal().argmax()
    max_ch_fourier = csd_fourier_data.diagonal().argmax()
    assert max_ch_mt == max_ch_power
    assert max_ch_fourier == max_ch_power

    # Maximum CSD should occur for specific channel
    ch_csd_mt = np.abs(csd_mt_data[max_ch_power])
    ch_csd_mt[max_ch_power] = 0.
    max_ch_csd_mt = np.argmax(ch_csd_mt)
    ch_csd_fourier = np.abs(csd_fourier_data[max_ch_power])
    ch_csd_fourier[max_ch_power] = 0.
    max_ch_csd_fourier = np.argmax(ch_csd_fourier)
    assert max_ch_csd_mt == max_ch_csd_fourier

    # Check a list of CSD matrices is returned for multiple frequencies within
    # a given range when fsum=False
    with warnings.catch_warnings(record=True):  # deprecation
        csd_fsum = csd_epochs(epochs, mode='fourier', fmin=8, fmax=20,
                              fsum=True)
        csds = csd_epochs(epochs, mode='fourier', fmin=8, fmax=20, fsum=False)
    assert len(csd_fsum.frequencies) == 1
    assert len(csds.frequencies) == 2
    assert_array_equal(csd_fsum.frequencies[0], csds.frequencies)

    csd_sum = csds._data.sum(axis=1, keepdims=True)
    assert_array_equal(csd_fsum._data, csd_sum)