def test_ajdc_expl_var(rndstate): # Test get_src_expl_var n_subjects, n_conditions, n_channels, n_times = 2, 2, 3, 256 X = rndstate.randn(n_subjects, n_conditions, n_channels, n_times) ajdc = AJDC().fit(X) n_matrices = 4 X_new = rndstate.randn(n_matrices, n_channels, n_times) v = ajdc.get_src_expl_var(X_new) assert v.shape == (n_matrices, ajdc.n_sources_) with pytest.raises(ValueError): # not 3 dims ajdc.get_src_expl_var(X_new[0]) with pytest.raises(ValueError): # unequal # of chans ajdc.get_src_expl_var(rndstate.randn(n_matrices, n_channels + 1, 1))
def test_ajdc_transform_error(rndstate): n_subjects, n_conditions, n_channels, n_times = 2, 2, 3, 256 X = rndstate.randn(n_subjects, n_conditions, n_channels, n_times) ajdc = AJDC().fit(X) n_matrices = 4 X_new = rndstate.randn(n_matrices, n_channels, n_times) with pytest.raises(ValueError): # not 3 dims ajdc.transform(X_new[0]) with pytest.raises(ValueError): # unequal # of chans ajdc.transform(rndstate.randn(n_matrices, n_channels + 1, 1))
def test_ajdc_fit_error(rndstate): n_conditions, n_channels, n_times = 3, 8, 512 ajdc = AJDC() with pytest.raises(ValueError): # unequal # of conditions ajdc.fit([ rndstate.randn(n_conditions, n_channels, n_times), rndstate.randn(n_conditions + 1, n_channels, n_times), ]) with pytest.raises(ValueError): # unequal # of channels ajdc.fit([ rndstate.randn(n_conditions, n_channels, n_times), rndstate.randn(n_conditions, n_channels + 1, n_times), ])
def test_ajdc_fit_variable_input(rndstate): n_subjects, n_cond, n_chan, n_times = 2, 2, 3, 256 X = rndstate.randn(n_subjects, n_cond, n_chan, n_times) ajdc = AJDC() # 3 subjects, same # conditions and channels, different # of times X = [ rndstate.randn(n_cond, n_chan, n_times + rndstate.randint(500)) for _ in range(3) ] ajdc.fit(X) # 2 subjects, 2 conditions, same # channels, different # of times X = [ [ rndstate.randn(n_chan, n_times + rndstate.randint(500)) for _ in range(2) ], [ rndstate.randn(n_chan, n_times + rndstate.randint(500)) for _ in range(2) ], ] ajdc.fit(X)
def test_ajdc_inverse_transform(rndstate): n_subjects, n_conditions, n_channels, n_times = 2, 2, 3, 256 X = rndstate.randn(n_subjects, n_conditions, n_channels, n_times) ajdc = AJDC().fit(X) n_matrices = 4 X_new = rndstate.randn(n_matrices, n_channels, n_times) Xt = ajdc.transform(X_new) Xit = ajdc.inverse_transform(Xt) assert_array_equal(Xit.shape, [n_matrices, n_channels, n_times]) with pytest.raises(ValueError): # not 3 dims ajdc.inverse_transform(Xt[0]) with pytest.raises(ValueError): # unequal # of sources shape = (n_matrices, ajdc.n_sources_ + 1, 1) ajdc.inverse_transform(rndstate.randn(*shape)) Xit = ajdc.inverse_transform(Xt, supp=[ajdc.n_sources_ - 1]) assert Xit.shape == (n_matrices, n_channels, n_times) with pytest.raises(ValueError): # not a list ajdc.inverse_transform(Xt, supp=1)
def test_ajdc_fit(rndstate): n_subjects, n_conditions, n_channels, n_times = 5, 3, 8, 512 X = rndstate.randn(n_subjects, n_conditions, n_channels, n_times) ajdc = AJDC().fit(X) assert ajdc.forward_filters_.shape == (ajdc.n_sources_, n_channels) assert ajdc.backward_filters_.shape == (n_channels, ajdc.n_sources_)
def test_ajdc_init(): ajdc = AJDC(fmin=1, fmax=32, fs=64) assert ajdc.window == 128 assert ajdc.overlap == 0.5 assert ajdc.dim_red is None assert ajdc.verbose
sfreq=sfreq) ch_info.set_montage('standard_1020') signal = RawArray(signal_raw, ch_info, verbose=False) signal.plot(duration=duration, start=0, n_channels=ch_count, scalings={'eeg': 3e1}, color={'eeg': 'steelblue'}, title='Original EEG signal', show_scalebars=False) ############################################################################### # AJDC: Second-Order Statistics (SOS)-based BSS, diagonalizing cospectra # ---------------------------------------------------------------------- # Compute and diagonalize Fourier cospectral matrices between 1 and 32 Hz window, overlap = sfreq, 0.5 fmin, fmax = 1, 32 ajdc = AJDC(window=window, overlap=overlap, fmin=fmin, fmax=fmax, fs=sfreq, expl_var=0.99) ajdc.fit(signal_raw[np.newaxis, np.newaxis, ...]) freqs = ajdc.freqs_ # Plot cospectra in channel space, after trace-normalization by frequency: each # cospectrum, associated to a frequency, is a covariance matrix plot_cospectra(ajdc._cosp_channels, freqs, ylabels=ch_names, title='Cospectra, in channel space') ############################################################################### # Plot diagonalized cospectra in source space sr_count = ajdc.n_sources_ sr_names = ['S' + str(s).zfill(2) for s in range(sr_count)] plot_cospectra(ajdc._cosp_sources, freqs, ylabels=sr_names,
n_channels=ch_count, scalings={'eeg': 3e1}, color={'eeg': 'steelblue'}, title='Original EEG signal', show_scalebars=False) ############################################################################### # AJDC: Second-Order Statistics (SOS)-based BSS, diagonalizing cospectra # ---------------------------------------------------------------------- # Compute and diagonalize Fourier cospectral matrices between 1 and 32 Hz window, overlap = sfreq, 0.5 fmin, fmax = 1, 32 ajdc = AJDC(window=window, overlap=overlap, fmin=fmin, fmax=fmax, fs=sfreq, dim_red={'max_cond': 100}) ajdc.fit(signal_raw[np.newaxis, np.newaxis, ...]) freqs = ajdc.freqs_ # Plot cospectra in channel space, after trace-normalization by frequency: each # cospectrum, associated to a frequency, is a covariance matrix plot_cospectra(ajdc._cosp_channels, freqs, ylabels=ch_names, title='Cospectra, in channel space') ############################################################################### # Plot diagonalized cospectra in source space
def test_AJDC(): """Test AJDC""" rs = np.random.RandomState(33) n_subjects, n_conditions, n_channels, n_samples = 5, 3, 8, 512 X = rs.randn(n_subjects, n_conditions, n_channels, n_samples) # Test Init with pytest.raises(ValueError): # value out of bounds AJDC(expl_var=0) with pytest.raises(ValueError): # value out of bounds AJDC(expl_var=1.1) ajdc = AJDC(fmin=1, fmax=32, fs=64) assert ajdc.window == 128 assert ajdc.overlap == 0.5 assert ajdc.expl_var == 0.999 assert ajdc.verbose # Test fit ajdc.fit(X) assert ajdc.n_channels_ == n_channels assert ajdc.n_sources_ <= n_channels assert_array_equal(ajdc.forward_filters_.shape, [ajdc.n_sources_, n_channels]) assert_array_equal(ajdc.backward_filters_.shape, [n_channels, ajdc.n_sources_]) with pytest.raises(ValueError): # unequal # of conditions ajdc.fit([rs.randn(n_conditions, n_channels, n_samples), rs.randn(n_conditions + 1, n_channels, n_samples)]) with pytest.raises(ValueError): # unequal # of channels ajdc.fit([rs.randn(n_conditions, n_channels, n_samples), rs.randn(n_conditions, n_channels + 1, n_samples)]) # 3 subjects, same # conditions and channels, different # of samples X = [rs.randn(n_conditions, n_channels, n_samples), rs.randn(n_conditions, n_channels, n_samples + 200), rs.randn(n_conditions, n_channels, n_samples + 500)] ajdc.fit(X) # 2 subjects, 2 conditions, same # channels, different # of samples X = [[rs.randn(n_channels, n_samples), rs.randn(n_channels, n_samples + 200)], [rs.randn(n_channels, n_samples + 500), rs.randn(n_channels, n_samples + 100)]] ajdc.fit(X) # Test transform n_trials = 4 X = rs.randn(n_trials, n_channels, n_samples) Xt = ajdc.transform(X) assert_array_equal(Xt.shape, [n_trials, ajdc.n_sources_, n_samples]) with pytest.raises(ValueError): # not 3 dims ajdc.transform(X[0]) with pytest.raises(ValueError): # unequal # of chans ajdc.transform(rs.randn(n_trials, n_channels + 1, 1)) # Test inverse_transform Xtb = ajdc.inverse_transform(Xt) assert_array_equal(Xtb.shape, [n_trials, n_channels, n_samples]) with pytest.raises(ValueError): # not 3 dims ajdc.inverse_transform(Xt[0]) with pytest.raises(ValueError): # unequal # of sources ajdc.inverse_transform(rs.randn(n_trials, ajdc.n_sources_ + 1, 1)) Xtb = ajdc.inverse_transform(Xt, supp=[ajdc.n_sources_ - 1]) assert_array_equal(Xtb.shape, [n_trials, n_channels, n_samples]) with pytest.raises(ValueError): # not a list ajdc.inverse_transform(Xt, supp=1) # Test get_src_expl_var v = ajdc.get_src_expl_var(X) assert_array_equal(v.shape, [n_trials, ajdc.n_sources_]) with pytest.raises(ValueError): # not 3 dims ajdc.get_src_expl_var(X[0]) with pytest.raises(ValueError): # unequal # of chans ajdc.get_src_expl_var(rs.randn(n_trials, n_channels + 1, 1))