Example #1
0
def test_sim_knee():

    # Build the signal and run a smoke test
    sig = sim_knee(N_SECONDS, FS, EXP1, EXP2, KNEE)
    check_sim_output(sig)

    # Check against the power spectrum when you take the Fourier transform
    sig_len = int(FS * N_SECONDS)
    freqs = np.linspace(0, FS / 2, num=sig_len // 2, endpoint=True)

    # Ignore the DC component to avoid division by zero in the Lorentzian
    freqs = freqs[1:]
    true_psd = np.array(
        [1 / (freq**-EXP1 * (freq**(-EXP2 - EXP1) + KNEE)) for freq in freqs])

    # Only look at the frequencies (ignoring DC component) up to the nyquist rate
    sig_hat = np.fft.fft(sig)[1:sig_len // 2]
    numerical_psd = np.abs(sig_hat)**2

    np.allclose(true_psd, numerical_psd, atol=EPS)

    # Accuracy test for a single exponent
    sig = sim_knee(n_seconds=N_SECONDS, fs=FS, chi1=0, chi2=EXP2, knee=KNEE)

    freqs, powers = compute_spectrum(sig, FS, f_range=(1, 200))

    def _estimate_single_knee(xs, offset, knee, exponent):
        return np.zeros_like(xs) + offset - np.log10(xs**exponent + knee)

    ap_params, _ = curve_fit(_estimate_single_knee, freqs, np.log10(powers))
    _, _, EXP2_hat = ap_params[:]

    assert -round(EXP2_hat) == EXP2
Example #2
0
def test_make_bursts():

    is_osc = np.array([False, False, True, True, False, True, False, True, True, False])
    cycle = np.ones([10])

    sig = make_bursts(N_SECONDS, FS, is_osc, cycle)
    check_sim_output(sig)
Example #3
0
def test_sim_combined():

    simulations = {
        'sim_oscillation': {
            'freq': FREQ1
        },
        'sim_powerlaw': {
            'exponent': -2
        }
    }

    out = sim_combined(N_SECONDS, FS, simulations)
    check_sim_output(out)

    # Test case with multiple uses of same function
    simulations = {
        'sim_oscillation': [{
            'freq': FREQ1
        }, {
            'freq': FREQ2
        }],
        'sim_powerlaw': {
            'exponent': -2
        }
    }
    variances = [0.5, 0.5, 1]
    out = sim_combined(N_SECONDS, FS, simulations, variances)
    check_sim_output(out)

    # Check the variance mismatch error
    variances = [0.5, 1]
    with raises(ValueError):
        out = sim_combined(N_SECONDS, FS, simulations, variances)
Example #4
0
def test_sim_synaptic_kernel():

    # Check that valid parameter configurations do not return negative values
    for params in [[0., 0.02], [0.005, 0.02], [0.02, 0.02]]:
        tau_r, tau_d = params
        kernel = sim_synaptic_kernel(N_SECONDS, FS, tau_r, tau_d)
        check_sim_output(kernel)
        assert np.all(kernel >= 0.)
Example #5
0
def test_sim_powerlaw():

    sig = sim_powerlaw(N_SECONDS, FS)
    check_sim_output(sig)

    # Test with a filter applied
    sig = sim_powerlaw(N_SECONDS, FS, f_range=(2, None))
    check_sim_output(sig)
def test_sim_peak_oscillation():

    sig_ap = sim_powerlaw(N_SECONDS, FS)
    sig = sim_peak_oscillation(sig_ap, FS, FREQ1, bw=5, height=10)

    check_sim_output(sig)

    # Ensure the peak is at or close (+/- 5hz) to FREQ1
    _, powers_ap = compute_spectrum(sig_ap, FS)
    _, powers = compute_spectrum(sig, FS)

    assert abs(np.argmax(powers - powers_ap) - FREQ1) < 5
Example #7
0
def test_sim_frac_brownian_motion(chi):

    # Simulate & check time series
    sig = sim_frac_brownian_motion(N_SECONDS, FS, chi=chi)
    check_sim_output(sig)

    # Linear fit the log-log power spectrum & check error based on expected 1/f exponent
    freqs = np.linspace(1, FS // 2, num=FS // 2)
    powers = np.abs(np.fft.fft(sig)[1:FS // 2 + 1])**2
    [_, chi_hat], _ = curve_fit(check_exponent, np.log10(freqs),
                                np.log10(powers))
    assert abs(chi_hat - chi) < 0.4
Example #8
0
def test_phase_shift_cycle():

    cycle = sim_cycle(N_SECONDS, FS, 'sine')

    # Check cycle does not change if not rotated
    cycle_noshift = phase_shift_cycle(cycle, 0.)
    check_sim_output(cycle_noshift)
    assert np.array_equal(cycle, cycle_noshift)

    # Check cycle does change if rotated
    cycle_shifted = phase_shift_cycle(cycle, 0.25)
    check_sim_output(cycle_shifted)
    assert not np.array_equal(cycle, cycle_shifted)
Example #9
0
def test_make_bursts():

    is_osc = np.array(
        [False, False, True, True, False, True, False, True, True, False])
    cycle = np.ones([10])

    sig = make_bursts(N_SECONDS, FS, is_osc, cycle)
    check_sim_output(sig)

    # Test make bursts with uneven division of signal and cycle divisions
    #   In this test, there aren't enough samples in the signal to add last cycle
    is_osc = np.array([False, True, True])
    cycle = np.ones([7])

    sig = make_bursts(2, 10, is_osc, cycle)
    assert sum(sig) > 0
Example #10
0
def test_sim_oscillation():

    sig = sim_oscillation(N_SECONDS, FS, FREQ1)
    check_sim_output(sig)

    # Check some different frequencies, that they get expected length, etc
    for freq in [3.5, 7.0, 13.]:
        check_sim_output(sim_oscillation(N_SECONDS, FS, freq))

    # Check that nothing goes weird with different time & sampling rate inputs
    check_sim_output(sim_oscillation(N_SECONDS_ODD, FS, FREQ1),
                     n_seconds=N_SECONDS_ODD)
    check_sim_output(sim_oscillation(N_SECONDS, FS_ODD, FREQ1), fs=FS_ODD)
Example #11
0
def test_sim_action_potential():

    stds = (.25, .2)
    alphas = (8, .2)
    centers = (.25, .5)
    heights = (15, 2.5)

    cycle = sim_action_potential(N_SECONDS, FS, centers, stds, alphas, heights)
    check_sim_output(cycle, n_seconds=N_SECONDS)

    cycle = sim_action_potential(N_SECONDS_ODD, FS, centers, stds, alphas,
                                 heights)
    check_sim_output(cycle, n_seconds=N_SECONDS_ODD)

    cycle = sim_action_potential(N_SECONDS, FS_ODD, centers, stds, alphas,
                                 heights)
    check_sim_output(cycle, n_seconds=N_SECONDS, fs=FS_ODD)

    cycle = sim_action_potential(N_SECONDS, FS, centers, stds, alphas, heights)
    check_sim_output(cycle, n_seconds=N_SECONDS)
Example #12
0
def test_sim_damped_oscillation():

    sig1 = sim_damped_oscillation(N_SECONDS, FS, FREQ1, .1)
    sig2 = sim_damped_oscillation(N_SECONDS, FS, FREQ1, 50)
    sig3 = sim_damped_oscillation(N_SECONDS, FS, FREQ1, 25, 0.1)

    check_sim_output(sig1)
    check_sim_output(sig2)
    check_sim_output(sig3)

    # Large gammas range between (0, 1), whereas small gammas range between (-1, 1)
    assert sig1.sum() < sig2.sum()
    assert len(sig1) == len(sig2)
Example #13
0
def test_sim_bursty_oscillation():

    # Check default values work
    sig1 = sim_bursty_oscillation(N_SECONDS, FS, FREQ1)
    check_sim_output(sig1)

    # Check probability burst approach
    sig2 = sim_bursty_oscillation(N_SECONDS, FS, FREQ1, \
        burst_def='prob', burst_params={'enter_burst' : 0.3, 'leave_burst' : 0.3})
    check_sim_output(sig2)

    # Check durations burst approach
    sig3 = sim_bursty_oscillation(N_SECONDS, FS, FREQ1, \
        burst_def='durations', burst_params={'n_cycles_burst' : 2, 'n_cycles_off' : 2})
    check_sim_output(sig3)
Example #14
0
def test_create_powerlaw():

    sig = _create_powerlaw(int(N_SECONDS * FS), FS, -2)
    check_sim_output(sig)
Example #15
0
def test_create_cycle_time():

    times = create_cycle_time(N_SECONDS, FS)
    check_sim_output(times)
Example #16
0
def test_sim_gaussian_cycle():

    cycle = sim_gaussian_cycle(N_SECONDS, FS, 2)
    check_sim_output(cycle)
Example #17
0
def test_sim_sawtooth_cycle():

    cycle = sim_sawtooth_cycle(N_SECONDS, FS, 0.5)
    check_sim_output(cycle)
Example #18
0
def test_sim_asine_cycle():

    cycle = sim_asine_cycle(N_SECONDS, FS, 0.25)
    check_sim_output(cycle)
Example #19
0
def test_sim_normalized_cycle():

    cycle = sim_normalized_cycle(N_SECONDS, FS, 'sine')
    check_sim_output(cycle)
Example #20
0
def test_sim_cycle():

    cycle = sim_cycle(N_SECONDS, FS, 'sine')
    check_sim_output(cycle)

    cycle = sim_cycle(N_SECONDS, FS, 'asine', rdsym=0.75)
    check_sim_output(cycle)

    cycle = sim_cycle(N_SECONDS, FS, 'sawtooth', width=0.5)
    check_sim_output(cycle)

    cycle = sim_cycle(N_SECONDS, FS, 'gaussian', std=2)
    check_sim_output(cycle)

    cycle = sim_cycle(N_SECONDS, FS, 'exp', tau_d=0.2)
    check_sim_output(cycle)

    cycle = sim_cycle(N_SECONDS, FS, '2exp', tau_r=0.2, tau_d=0.2)
    check_sim_output(cycle)

    with raises(ValueError):
        sim_cycle(N_SECONDS, FS, 'not_a_cycle')
Example #21
0
def test_sim_oscillation():

    sig = sim_oscillation(N_SECONDS, FS, FREQ1)
    check_sim_output(sig)
Example #22
0
def test_sim_poisson_pop():

    sig = sim_poisson_pop(N_SECONDS, FS)
    check_sim_output(sig)
Example #23
0
def test_sim_synaptic_current():

    sig = sim_synaptic_current(N_SECONDS, FS)
    check_sim_output(sig)
Example #24
0
def test_sim_random_walk():

    sig = sim_random_walk(N_SECONDS, FS)
    check_sim_output(sig)