def test_high_sig_identical(): # Test that we have the same result with high_sig=low_sig and high_sig=None for method in ALL_PAC_METRICS: if method in BICOHERENCE_PAC_METRICS: continue comod_0 = fast_comod(method=method) comod_1 = fast_comod(high_sig=signal, method=method) assert_array_equal(comod_0, comod_1)
def test_fake_parallel(): # Test that _FakeParallel does nothing but unrolling the generator generator = (twice(b) for b in range(10)) results = _FakeParallel(n_jobs=1)(generator) reference = [twice(b) for b in range(10)] assert_array_equal(np.array(results), np.array(reference)) # test fake parameters _FakeParallel(n_jobs=1, foo='bar', baz=42)
def _compare_values(v, v2): if isinstance(v, np.ndarray): assert_array_equal(v, v2) elif isinstance(v, dict): for key, value in v.items(): _compare_values(v[key], v2[key]) elif isinstance(v, np.random.RandomState): for s, s2 in zip(v.get_state(), v2.get_state()): _compare_values(s, s2) else: assert_equal(v, v2)
def test_surrogates(): # Test the surrogates comodulogram for method in ALL_PAC_METRICS: msg = 'with method=%s' % method if method in BICOHERENCE_PAC_METRICS or method == 'jiang': continue n_surrogates = 10 est = ComodTest(method=method, n_surrogates=n_surrogates).fit(signal) assert_array_equal(est.comod_.shape, (n_low, n_high), err_msg=msg) assert_array_equal(est.surrogates_.shape, (n_surrogates, n_low, n_high), err_msg=msg) # z-score z_score = est.comod_z_score_ assert_array_equal(z_score.shape, (n_low, n_high), err_msg=msg) if method != 'jiang': # 'jiang' method does not estimate CFC but CFD assert_greater(z_score[1, 1], z_score[-1, -1], msg=msg) # surrogate_max surrogate_max = est.surrogate_max_ assert_array_equal(surrogate_max.shape, (n_surrogates, )) assert_greater(est.comod_[1, 1], surrogate_max.max(), msg=msg) assert_greater(surrogate_max.max(), est.comod_[-1, -1], msg=msg) # Smoke test with contours in the plotting function est.plot(contour_level=0.01, contour_method='comod_max') est.plot(contour_level=3, contour_method='z_score') plt.close('all')
def test_mask_iterator(): # Test the mask iterator length and the shape of the elements fs = 200. tmin, tmax = -0.05, 0.2 n_points = 200 events = np.arange(0, n_points, int(fs * 0.3)) masks = MaskIterator(events, tmin, tmax, n_points, fs) masks_list = [m for m in masks] # test that the length is correct n_masks_0 = len(masks) n_masks_1 = len(masks_list) assert_equal(n_masks_0, n_masks_1) # test that the masks are of the correct shape for mask in masks_list: assert_array_equal(mask.shape, (1, n_points))
def test_comod_correct_maximum(): # Test that the PAC is maximum at the correct location in the comodulogram for method in ALL_PAC_METRICS: est = ComodTest(method=method, progress_bar=True).fit(signal) comod = est.comod_ # test the shape of the comodulogram assert_array_equal(comod.shape, (n_low, n_high)) # the bicoherence metrics fail this test with current parameters if method in BICOHERENCE_PAC_METRICS or method == 'jiang': continue low_fq_0, high_fq_0, max_pac = est.get_maximum_pac() assert_equal(low_fq_0, low_fq) assert_equal(high_fq_0, high_fq) assert_equal(max_pac, comod.max()) assert_true(np.all(comod > 0))
def test_amplitude(): # Test that the amplitude parameters change the amplitude sig_0 = simulate_pac_default(high_fq_amp=0, low_fq_amp=0, noise_level=0) zeros = np.zeros_like(sig_0) assert_array_equal(sig_0, zeros) sig_1 = simulate_pac_default(high_fq_amp=1, low_fq_amp=0, noise_level=0) sig_2 = simulate_pac_default(high_fq_amp=2, low_fq_amp=0, noise_level=0) assert_almost_equal(sig_1.std() * 2, sig_2.std(), decimal=7) sig_1 = simulate_pac_default(high_fq_amp=0, low_fq_amp=0, noise_level=1) sig_2 = simulate_pac_default(high_fq_amp=0, low_fq_amp=0, noise_level=2) assert_almost_equal(sig_1.std(), 1, decimal=1) assert_almost_equal(sig_2.std(), 2, decimal=1) sig_1 = simulate_pac_default(high_fq_amp=0, low_fq_amp=1, noise_level=0) sig_2 = simulate_pac_default(high_fq_amp=0, low_fq_amp=2, noise_level=0) assert_almost_equal(sig_1.std(), 1, decimal=0) assert_almost_equal(sig_2.std(), 2, decimal=0)
def test_dehumming_improve_snr(): # Test that dehumming removes the added noise rng = np.random.RandomState(0) fs = 250. enf = 50. clean = rng.randn(512) noisy = clean.copy() # add ENF noise time = np.arange(512) / float(fs) for harmonic in (1, 2): noisy += np.sin(time * 2 * np.pi * enf * harmonic) / harmonic for dim in [(1, 512), (512, ), (512, 1)]: dehummed = dehummer( noisy.reshape(*dim), fs, enf=enf) assert_greater(norm(noisy - clean), norm(dehummed.ravel() - clean)) assert_array_equal(dehummed.shape, dim) # test that the dehumming does not accept 2d arrays assert_raises(ValueError, dehummer, noisy.reshape(2, 256), fs)
def test_amplitude(): # Test that the amplitude parameters change the amplitude sig_0 = simulate_pac_default(high_fq_amp=0, low_fq_amp=0, noise_level=0) zeros = np.zeros_like(sig_0) assert_array_equal(sig_0, zeros) sig_1 = simulate_pac_default(high_fq_amp=1, low_fq_amp=0, noise_level=0) sig_2 = simulate_pac_default(high_fq_amp=2, low_fq_amp=0, noise_level=0) # with a driver equal to zero at all time, the sigmoid modulates at 0.5 assert_almost_equal(sig_1.std(), 0.5, decimal=7) assert_almost_equal(sig_2.std(), 1, decimal=7) sig_1 = simulate_pac_default(high_fq_amp=0, low_fq_amp=0, noise_level=1) sig_2 = simulate_pac_default(high_fq_amp=0, low_fq_amp=0, noise_level=2) assert_almost_equal(sig_1.std(), 1, decimal=1) assert_almost_equal(sig_2.std(), 2, decimal=1) sig_1 = simulate_pac_default(high_fq_amp=0, low_fq_amp=1, noise_level=0) sig_2 = simulate_pac_default(high_fq_amp=0, low_fq_amp=2, noise_level=0) assert_almost_equal(sig_1.std(), 1, decimal=0) assert_almost_equal(sig_2.std(), 2, decimal=0)
def test_signal_unchanged(): # Test that signal has not been changed during the test assert_array_equal(signal_copy, signal)