def test_demean(show=False): """Test demean.""" n_trials = 100 n_chans = 8 n_times = 1000 x = np.random.randn(n_times, n_chans, n_trials) x, s = _stim_data(n_times, n_chans, n_trials, 8, SNR=10) # demean and check trial average is almost zero x1 = demean(x) assert_almost_equal(x1.mean(2).mean(0), np.zeros((n_chans, ))) # now use weights times = np.arange(n_times) weights = np.zeros_like(times) weights[:100] = 1 x2 = demean(x, weights) if show: import matplotlib.pyplot as plt plt.plot(x2.mean(2)) plt.gca().set_prop_cycle(None) plt.plot(s.mean(2), 'k:') plt.show() # assert mean is ~= 0 during baseline assert_almost_equal(x2[:100].mean(2).mean(0), np.zeros((n_chans, ))) # assert trial average is close to source signal assert np.all(x2.mean(2) - s.mean(2) < 1)
def test_demean(show=False): """Test demean.""" n_trials = 100 n_chans = 8 n_times = 1000 x = np.random.randn(n_times, n_chans, n_trials) x, s = _sim_data(n_times, n_chans, n_trials, 8, SNR=10) # 1. demean and check trial average is almost zero x1 = demean(x) assert_almost_equal(x1.mean(2).mean(0), np.zeros((n_chans,))) assert_almost_equal(x1.mean(2), mean_over_trials(x1)[0]) # 2. now use weights : baseline = first 100 samples times = np.arange(n_times) weights = np.zeros_like(times) weights[:100] = 1 x2 = demean(x, weights) if show: import matplotlib.pyplot as plt f, ax = plt.subplots(3, 1) ax[0].plot(times, x[:, 0].mean(-1), label='noisy_data') ax[0].plot(times, s[:, 0].mean(-1), label='signal') ax[0].legend() ax[1].plot(times, x1[:, 0].mean(-1), label='mean over entire epoch') ax[1].legend() ax[2].plot(x2[:, 0].mean(-1), label='weighted mean') plt.gca().set_prop_cycle(None) ax[2].plot(s[:, 0].mean(-1), 'k:') ax[2].legend() plt.show() # assert mean is ~= 0 during baseline assert_almost_equal(x2[:100].mean(2).mean(0), np.zeros((n_chans,))) # assert trial average is close to source signal assert np.all(x2.mean(2) - s.mean(2) < 1) # 3. try different shape weights (these should all be equivalent) weights = np.zeros((len(times), 1, 1)) weights[:100] = 1 x1 = demean(x, weights) weights = np.zeros((len(times), n_chans, 1)) weights[:100] = 1 x2 = demean(x, weights) weights = np.zeros((len(times), n_chans, n_trials)) weights[:100] = 1 x3 = demean(x, weights) np.testing.assert_array_equal(x1, x2) np.testing.assert_array_equal(x1, x3)
def test_tspca_sns_dss(): # TODO """Test TSPCA, SNS, DSS. Requires data stored in a time X channels X trials matrix. Remove environmental noise with TSPCA (shifts=-50:50). Remove sensor noise with SNS. Remove non-repeatable components with DSS. """ # Random data (time*chans*trials) data = np.random.random((800, 102, 200)) ref = np.random.random((800, 3, 200)) # remove means noisy_data = demean(data) noisy_ref = demean(ref) # Apply TSPCA # ------------------------------------------------------------------------- # shifts = np.arange(-50, 51) # print('TSPCA...') # y_tspca, idx = tspca.tsr(noisy_data, noisy_ref, shifts)[0:2] # print('\b OK!') y_tspca = noisy_data # Apply SNS # ------------------------------------------------------------------------- nneighbors = 10 print('SNS...') y_tspca_sns, r = sns.sns(y_tspca, nneighbors) print('\b OK!') # apply DSS # ------------------------------------------------------------------------- print('DSS...') # Keep all PC components y_tspca_sns = demean(y_tspca_sns) print(y_tspca_sns.shape) todss, fromdss, _, _ = dss.dss1(y_tspca_sns) print('\b OK!') # c3 = DSS components y_tspca_sns_dss = fold(np.dot(unfold(y_tspca_sns), todss), y_tspca_sns.shape[0]) return y_tspca, y_tspca_sns, y_tspca_sns_dss
def sim_data(n_samples, n_chans, f, SNR): target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f) target = target[:, np.newaxis] noise = np.random.randn(n_samples, n_chans - 3) x0 = ( normcol(np.dot(noise, np.random.randn(noise.shape[1], n_chans))) + SNR * target * np.random.randn(1, n_chans)) x0 = demean(x0) artifact = np.zeros(x0.shape) for k in np.arange(n_chans): artifact[k * 100 + np.arange(20), k] = 1 x = x0 + 20 * artifact return x, x0
def test_star1(): """Test STAR 1.""" # N channels, 1 sinusoidal target, N-3 noise sources, temporally local # artifacts on each channel. n_samples = 1000 n_chans = 10 f = 2 def sim_data(n_samples, n_chans, f, SNR): target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f) target = target[:, np.newaxis] noise = np.random.randn(n_samples, n_chans - 3) x0 = ( normcol(np.dot(noise, np.random.randn(noise.shape[1], n_chans))) + SNR * target * np.random.randn(1, n_chans)) x0 = demean(x0) artifact = np.zeros(x0.shape) for k in np.arange(n_chans): artifact[k * 100 + np.arange(20), k] = 1 x = x0 + 20 * artifact return x, x0 # Test SNR=1 x, x0 = sim_data(n_samples, n_chans, f, SNR=np.sqrt(1)) y, w, _ = star(x, 2, verbose='debug') assert_allclose(demean(y), x0) # check that denoised signal ~ x0 # Test more unfavourable SNR x, x0 = sim_data(n_samples, n_chans, f, SNR=np.sqrt(1e-7)) y, w, _ = star(x, 2) assert_allclose(demean(y), x0) # check that denoised signal ~ x0 # Test an all-zero channel is properly handled x = x - x[:, 0][:, np.newaxis] y, w, _ = star(x, 2) assert_allclose(demean(y)[:, 0], x[:, 0])
# Simulated data consist of N channels, 1 sinusoidal target, N-3 noise sources, # with temporally local artifacts on each channel. # Create simulated data nchans = 10 n_samples = 1000 f = 2 target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f) target = target[:, np.newaxis] noise = np.random.randn(n_samples, nchans - 3) # Create artifact signal SNR = np.sqrt(1) x0 = (normcol(np.dot(noise, np.random.randn(noise.shape[1], nchans))) + SNR * target * np.random.randn(1, nchans)) x0 = demean(x0) artifact = np.zeros(x0.shape) for k in np.arange(nchans): artifact[k * 100 + np.arange(20), k] = 1 x = x0 + 10 * artifact # This is to compare with matlab numerically # from scipy.io import loadmat # mat = loadmat('/Users/nicolas/Toolboxes/NoiseTools/TEST/X.mat') # x = mat['x'] # x0 = mat['x0'] ############################################################################### # Apply STAR # ----------------------------------------------------------------------------- y, w, _ = star.star(x, 2)
from meegkit import star, dss # noqa:E402 from meegkit.utils import demean, normcol, tscov # noqa:E402 # Create simulated data np.random.seed(9) n_chans, n_samples = 10, 1000 f = 2 target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f) target = target[:, np.newaxis] noise = np.random.randn(n_samples, n_chans - 3) # Create artifact signal SNR = np.sqrt(1) x0 = (normcol(np.dot(noise, np.random.randn(noise.shape[1], n_chans))) + SNR * target * np.random.randn(1, n_chans)) x0 = demean(x0) artifact = np.zeros(x0.shape) for k in np.arange(n_chans): artifact[k * 100 + np.arange(20), k] = 1 x = x0 + 10 * artifact # Basic function to fit a sinusoidal trend for DSS def _sine_fit(x): guess_mean = np.mean(x) guess_std = np.std(x) guess_phase = 0 t = np.linspace(0, 4 * np.pi, x.shape[0]) # Optimization function, in this case, we want to minimize the difference # between the actual data and our "guessed" parameters