def test_coh_solution(self, **kwargs): res = ca(data=self.data, method='coh', foilim=[5, 60], output='pow', taper='dpss', tapsmofrq=1.5, **kwargs) # coherence at the harmonic frequencies idx_f1 = np.argmin(res.freq < self.f1) peak_f1 = res.data[0, idx_f1, 0, 1] idx_f2 = np.argmin(res.freq < self.f2) peak_f2 = res.data[0, idx_f2, 0, 1] # check low phase diffusion has high coherence assert peak_f2 > 0.5 # check that with higher phase diffusion the # coherence is lower assert peak_f1 < peak_f2 # check that 5Hz away from the harmonics there # is low coherence null_idx = (res.freq < self.f1 - 5) | (res.freq > self.f1 + 5) null_idx *= (res.freq < self.f2 - 5) | (res.freq > self.f2 + 5) assert np.all(res.data[0, null_idx, 0, 1] < 0.1)
def test_gr_solution(self, **kwargs): Gcaus = ca(self.data, method='granger', taper='dpss', tapsmofrq=3, foi=self.foi, **kwargs) # check all channel combinations with coupling for i, j in zip(*self.cpl_idx): peak = Gcaus.data[0, :, i, j].max() peak_frq = Gcaus.freq[Gcaus.data[0, :, i, j].argmax()] cval = self.AdjMat[i, j] dbg_str = f"{peak:.2f}\t{self.AdjMat[i,j]:.2f}\t {peak_frq:.2f}\t" print(dbg_str, f'\t {i}', f' {j}') # test for directional coupling # at the right frequency range assert peak >= cval assert 35 < peak_frq < 45 # only plot with defaults if len(kwargs) == 0: plot_Granger(Gcaus, i, j) ppl.legend()
def test_corr_selections(self): selections = mk_selection_dicts(self.nTrials, self.nChannels, *self.time_span) for sel_dct in selections: result = ca(self.data, method='corr', select=sel_dct) # check here just for finiteness and positivity assert np.all(np.isfinite(result.data))
def test_gr_selections(self): # trial, channel and toi selections selections = mk_selection_dicts(self.nTrials, self.nChannels, *self.time_span) for sel_dct in selections: Gcaus = ca(self.data, method='granger', select=sel_dct) # check here just for finiteness and positivity assert np.all(np.isfinite(Gcaus.data)) assert np.all(Gcaus.data[0, ...] >= -1e-10)
def test_corr_solution(self, **kwargs): corr = ca(data=self.data, method='corr', **kwargs) # test 0-lag autocorr is 1 for all channels assert np.all(corr.data[0, 0].diagonal() > .99) # test that at exactly the period-lag # correlations remain high w/o phase diffusion period_idx = int(1 / self.f1 * self.fs) # 100 samples is one period assert np.allclose(100, period_idx) auto_00 = corr.data[:, 0, 0, 0] assert np.all(auto_00[::period_idx] > .99) # test for auto-corr minima at half the period assert auto_00[period_idx // 2] < -.99 assert auto_00[period_idx // 2 + period_idx] < -.99 # test signal with phase diffusion (2nd channel) has # decaying correlations (diffusion may lead to later # increases of auto-correlation again, hence we check # only the first 5 periods) auto_11 = corr.data[:, 0, 1, 1] assert np.all(np.diff(auto_11[::period_idx])[:5] < 0) # test that a pi/2 phase shift moves the 1st # crosscorr maximum to 1/4 of the period cross_02 = corr.data[:, 0, 0, 2] lag_idx = int(1 / self.f1 * self.fs * 0.25) # 25 samples is 1/4th period assert np.allclose(25, lag_idx) assert cross_02[lag_idx] > 0.99 # same for a period multiple assert cross_02[lag_idx + period_idx] > .99 # plus half the period a minimum occurs assert cross_02[lag_idx + period_idx // 2] < -.99 # test for (anti-)symmetry cross_20 = corr.data[:, 0, 2, 0] assert cross_20[-lag_idx] > 0.99 assert cross_20[-lag_idx - period_idx] > 0.99 # only plot for simple solution test if len(kwargs) == 0: plot_corr(corr, 0, 0, label='corr 0-0') plot_corr(corr, 1, 1, label='corr 1-1') plot_corr(corr, 0, 2, label='corr 0-2') ppl.xlim((-.01, 0.5)) ppl.ylim((-1.1, 1.3)) ppl.legend(ncol=3)
def test_corr_cfg(self): call = lambda cfg: ca(self.data, cfg) run_cfg_test(call, method='corr', positivity=False)
def test_coh_cfg(self): call = lambda cfg: ca(self.data, cfg) run_cfg_test(call, method='coh')
def test_coh_foi(self): call = lambda foi, foilim: ca( self.data, method='coh', foi=foi, foilim=foilim) run_foi_test(call, foilim=[0, 70])
def test_gr_cfg(self): call = lambda cfg: ca(self.data, cfg) run_cfg_test(call, method='granger')
def test_gr_foi(self): call = lambda foi, foilim: ca( self.data, method='granger', foi=foi, foilim=foilim) run_foi_test(call, foilim=[0, 70])