示例#1
0
def test_detect_bursts_consistent():
    """
    Confirm consistency in burst detection results on a generated neural signal
    """
    # Load data and ground-truth filtered signal
    x = _load_example_data(data_idx=1)
    Fs = 1000
    f_range = (13, 30)
    f_oi = np.floor(np.mean(f_range))
    f_range_slope = (3, 50)
    f_slope_excl = f_range

    # Load past burst findings
    bursting_true_deviation = np.load(os.path.dirname(neurodsp.__file__) +
                                      '/tests/data/sample_data_1_burst_deviation.npy')
    bursting_true_bosc = np.load(os.path.dirname(neurodsp.__file__) +
                                 '/tests/data/sample_data_1_burst_bosc.npy')

    # Detect bursts with different algorithms
    bursting_deviation = neurodsp.detect_bursts(x, Fs, f_range, 'deviation',
                                                dual_thresh=(0.9, 2.0))
    bursting_bosc = neurodsp.detect_bursts_bosc(x, Fs, f_oi, f_range_slope, f_slope_excl)

    assert np.isclose(np.sum(bursting_deviation - bursting_true_deviation), 0)
    assert np.isclose(np.sum(bursting_bosc - bursting_true_bosc), 0)
示例#2
0
def test_swm_consistent():
    """
    Confirm consistency in beta bandpass filter results on a neural signal
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000

    # Load ground truth lagged coherence
    avg_window_true = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_swm.npy')

    # Compute lagged coherence
    L = .055
    G = .2
    np.random.seed(1)
    avg_window, _, _ = shape.sliding_window_matching(x,
                                                     Fs,
                                                     L,
                                                     G,
                                                     max_iterations=500)

    # Compute difference between current and past signals
    signal_diff = avg_window - avg_window_true
    assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10**-5)
示例#3
0
def test_interpolated_phase_consistent():
    """
    Confirm consistency in beta bandpass filter results on a neural signal
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth phase time series
    phaPTRD_true = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_phaPTRD.npy')

    # Compute phase time series
    Ps, Ts = shape.find_extrema(x, Fs, f_range)
    zeroxR, zeroxD = shape.find_zerox(x, Ps, Ts)
    phaPTRD = shape.extrema_interpolated_phase(x,
                                               Ps,
                                               Ts,
                                               zeroxR=zeroxR,
                                               zeroxD=zeroxD)

    # Compute difference between current and past filtered signals
    signal_diff = phaPTRD - phaPTRD_true
    assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10**-5)
def test_comodulogram_consistent():
    """
    Confirm consistency in estimation of pac comodulogram
    with computations in previous versions
    """

    # Compute pacs
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000

    # Parameters for comodulogram function
    f_pha_bin_edges = np.arange(2, 42, 2)
    f_amp_bin_edges = np.arange(20, 200, 4)
    N_cycles_pha = 5
    N_cycles_amp = 11

    # Compute comodulogram
    comod = neurodsp.compute_pac_comodulogram(x, x, Fs,
                                              f_pha_bin_edges, f_amp_bin_edges,
                                              N_cycles_pha=N_cycles_pha,
                                              N_cycles_amp=N_cycles_amp,
                                              pac_method='ozkurt')

    # Load ground truth comodulogram
    comod_true = np.load(os.path.dirname(neurodsp.__file__) +
                         '/tests/data/sample_data_' + str(data_idx) + '_comod.npy')

    # Compute difference between current and past signals
    assert np.allclose(np.sum(np.abs(comod - comod_true)), 0, atol=10 ** -5)
示例#5
0
def test_cyclefeatures_consistent():
    """
    Confirm consistency in peak finding
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth lagged coherence
    df_true = pd.read_csv(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_cyclefeatures.csv')

    # Compute lagged coherence
    true_oscillating_periods_kwargs = {
        'restrict_by_amplitude_consistency': False,
        'restrict_by_period_consistency': False,
        'amplitude_fraction_threshold': .3
    }

    df = shape.features_by_cycle(
        x,
        Fs,
        f_range,
        center_extrema='T',
        estimate_oscillating_periods=True,
        true_oscillating_periods_kwargs=true_oscillating_periods_kwargs)

    # Compute difference between calculated and ground truth values for each column
    for k in df.keys():
        signal_diff = df[k].values - df_true[k].values
        assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10**-5)
def test_psd():
    """
    Confirm consistency in PSD computation
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    fs = 1000.

    # load "ground truth" PSDs
    gt_psd = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_psd.npz')

    # try all 3 different methods
    freq, Pmean = spectral.psd(x, fs, method='mean', nperseg=fs * 2)
    freq, Pmed = spectral.psd(x, fs, method='median', nperseg=fs * 2)
    freqmf, Pmedfilt = spectral.psd(x, fs, method='medfilt')

    # compute the difference
    assert np.allclose(np.sum(np.abs(gt_psd['PSDmean'] - Pmean)),
                       0,
                       atol=10**-5)
    assert np.allclose(np.sum(np.abs(gt_psd['PSDmed'] - Pmed)), 0, atol=10**-5)
    assert np.allclose(np.sum(np.abs(gt_psd['PSDmedfilt'] - Pmedfilt)),
                       0,
                       atol=10**-5)
    assert np.allclose(np.sum(np.abs(gt_psd['freq'] - freq)), 0, atol=10**-5)
    assert np.allclose(np.sum(np.abs(gt_psd['freqmf'] - freqmf)),
                       0,
                       atol=10**-5)
def test_pac_consistent():
    """
    Confirm consistency in estimation of pac
    with computations in previous versions
    """

    # Compute pacs
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000

    # Compute pacs
    all_pac_methods = ['ozkurt', 'plv', 'glm', 'tort', 'canolty']
    f_range_lo = (13, 30)
    f_range_hi = (50, 200)
    N_seconds_lo = .25
    N_seconds_hi = .2

    np.random.seed(0)
    pacs = np.zeros(len(all_pac_methods))
    for i, m in enumerate(all_pac_methods):
        pacs[i] = neurodsp.compute_pac(x, x, Fs, f_range_lo, f_range_hi,
                                       N_seconds_lo=N_seconds_lo, N_seconds_hi=N_seconds_hi,
                                       pac_method=m)

    # Load ground truth pacs
    pacs_true = np.load(os.path.dirname(neurodsp.__file__) +
                        '/tests/data/sample_data_' + str(data_idx) + '_pacs.npy')

    # Compute difference between current and past signals
    assert np.allclose(np.sum(np.abs(pacs - pacs_true)), 0, atol=10 ** -5)
示例#8
0
def test_bandpass_filter_consistent():
    """
    Confirm consistency in beta bandpass filter results on a neural signal
    """
    # Load data and ground-truth filtered signal
    x, x_filt_true = _load_example_data(data_idx=1, filtered=True)

    # filter data
    Fs = 1000
    f_lo = 13
    f_hi = 30
    x_filt = neurodsp.filter(x, Fs, 'bandpass', f_lo=f_lo, f_hi=f_hi, N_cycles=3)

    # Compute difference between current and past filtered signals
    signal_diff = x_filt[~np.isnan(x_filt)] - x_filt_true[~np.isnan(x_filt_true)]
    assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10 ** -5)
示例#9
0
def test_spectralhist():
    """
    Confirm SCV resampling calculation
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    fs = 1000.

    # load ground truth scv
    gt_sphist = np.load(os.path.dirname(neurodsp.__file__) +
                        '/tests/data/sample_data_' + str(data_idx) + '_sphist.npz')

    freq, bins, sp_hist = spectral.spectral_hist(x, fs, nbins=10)
    assert np.allclose(np.sum(np.abs(gt_sphist['freq'] - freq)), 0, atol=10 ** -5)
    assert np.allclose(np.sum(np.abs(gt_sphist['bins'] - bins)), 0, atol=10 ** -5)
    assert np.allclose(np.sum(np.abs(gt_sphist['sp_hist'] - sp_hist)), 0, atol=10 ** -5)
示例#10
0
def test_scv():
    """
    Confirm SCV calculation
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    fs = 1000.

    # load ground truth scv
    gt_scv = np.load(os.path.dirname(neurodsp.__file__) +
                     '/tests/data/sample_data_' + str(data_idx) + '_scv.npz')

    # compute SCV
    freq, SCV = spectral.scv(x, fs)
    assert np.allclose(np.sum(np.abs(gt_scv['freq'] - freq)), 0, atol=10 ** -5)
    assert np.allclose(np.sum(np.abs(gt_scv['SCV'] - SCV)), 0, atol=10 ** -5)
def test_laggedcoherence_consistent():
    """
    Confirm consistency in beta bandpass filter results on a neural signal
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth lagged coherence
    lc_true = np.load(os.path.dirname(neurodsp.__file__) +
                      '/tests/data/sample_data_' + str(data_idx) + '_laggedcoherence.npy')

    # Compute lagged coherence
    lag_coh_beta = neurodsp.lagged_coherence(x, f_range, Fs)

    assert np.allclose(lag_coh_beta, lc_true, atol=10 ** -5)
示例#12
0
def test_freq_by_time_consistent():
    """
    Confirm consistency in beta bandpass filter results on a neural signal
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth frequency time series
    i_f_true = np.load(os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_'+str(data_idx)+'_i_f.npy')

    # Compute frequency time series
    i_f = neurodsp.freq_by_time(x, Fs, f_range)

    # Compute difference between current and past filtered signals
    signal_diff = i_f[~np.isnan(i_f)] - i_f_true[~np.isnan(i_f_true)]
    assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10 ** -5)
示例#13
0
def test_amp_by_time_consistent():
    """
    Confirm consistency in beta bandpass filter results on a neural signal
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth amplitude time series
    amp_true = np.load(os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_'+str(data_idx)+'_amp.npy')

    # Compute amplitude time series
    amp = neurodsp.amp_by_time(x, Fs, f_range)

    # Compute difference between current and past filtered signals
    signal_diff = amp - amp_true
    assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10 ** -5)
示例#14
0
def test_cyclefeatures_consistent():
    """
    Confirm consistency in peak finding

    Previous code run:
    import numpy as np
    import os
    import neurodsp
    from neurodsp import shape
    x = np.load(os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_1.npy')
    df = shape.features_by_cycle(x, 1000, (13, 30), center_extrema='T',
                                 estimate_oscillating_periods=True)
    df.to_csv(os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_1_cyclefeatures.csv')
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth cycle features
    df_true = pd.read_csv(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_cyclefeatures.csv')

    df = shape.features_by_cycle(x,
                                 Fs,
                                 f_range,
                                 center_extrema='T',
                                 estimate_oscillating_periods=True)

    # Compute difference between calculated and ground truth values for each column
    for k in df.keys():
        if df[k].dtype == bool:
            signal_diff = df[k].values[~np.isnan(df[k].values)].astype(
                int) - df_true[k].values[~np.isnan(df_true[k].values)].astype(
                    int)
        else:
            signal_diff = df[k].values[~np.isnan(df[k].values)] - df_true[
                k].values[~np.isnan(df_true[k].values)]
        assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10**-5)
示例#15
0
def test_Ps_consistent():
    """
    Confirm consistency in peak finding
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth lagged coherence
    Ps_true = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_Ps.npy')

    # Compute lagged coherence
    Ps, Ts = shape.find_extrema(x, Fs, f_range)

    # Compute difference between current and past signals
    signal_diff = Ps - Ps_true
    assert np.allclose(np.sum(np.abs(signal_diff)), 0, atol=10**-5)
def test_scvrs():
    """
    Confirm SCV resampling calculation
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    fs = 1000.

    # load ground truth scv
    gt_scvrs = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_scvrs.npz')

    # compute SCV and compare differences
    np.random.seed(99)
    freqbs, Tbs, SCVrsbs = spectral.scv_rs(x,
                                           fs,
                                           method='bootstrap',
                                           rs_params=(5, 20))
    assert np.allclose(np.sum(np.abs(gt_scvrs['freqbs'] - freqbs)),
                       0,
                       atol=10**-5)
    assert Tbs is None
    assert np.allclose(np.sum(np.abs(gt_scvrs['SCVrsbs'] - SCVrsbs)),
                       0,
                       atol=10**-5)

    freqro, Tro, SCVrsro = spectral.scv_rs(x,
                                           fs,
                                           method='rolling',
                                           rs_params=(4, 2))
    assert np.allclose(np.sum(np.abs(gt_scvrs['freqro'] - freqro)),
                       0,
                       atol=10**-5)
    assert np.allclose(np.sum(np.abs(gt_scvrs['Tro'] - Tro)), 0, atol=10**-5)
    assert np.allclose(np.sum(np.abs(gt_scvrs['SCVrsro'] - SCVrsro)),
                       0,
                       atol=10**-5)
示例#17
0
def test_timefreq_consistent():
    """
    Confirm consistency in estimation of instantaneous phase, amp, and frequency
    with computations in previous versions
    """
    # Load data
    data_idx = 1
    x = _load_example_data(data_idx=data_idx)
    Fs = 1000
    f_range = (13, 30)

    # Load ground truth phase time series
    pha_true = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_pha.npy')
    # Load ground truth amplitude time series
    amp_true = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_amp.npy')
    # Load ground truth frequency time series
    i_f_true = np.load(
        os.path.dirname(neurodsp.__file__) + '/tests/data/sample_data_' +
        str(data_idx) + '_i_f.npy')

    # Compute phase time series
    pha = neurodsp.phase_by_time(x, Fs, f_range)
    # Compute amplitude time series
    amp = neurodsp.amp_by_time(x, Fs, f_range)
    # Compute frequency time series
    i_f = neurodsp.freq_by_time(x, Fs, f_range)

    # Compute difference between current and past signals
    assert np.allclose(np.sum(np.abs(pha - pha_true)), 0, atol=10**-5)
    assert np.allclose(np.sum(np.abs(amp - amp_true)), 0, atol=10**-5)
    assert np.allclose(np.sum(
        np.abs(i_f[~np.isnan(i_f)] - i_f_true[~np.isnan(i_f_true)])),
                       0,
                       atol=10**-5)