def test_crossspec(self): # use less neurons (0.2*self.N) for full matrix (memory error!) Nloc = int(0.2 * self.N) Nloceff = int(0.2 * self.Neff) sp = cthlp.create_poisson_spiketrains(self.rate, self.T, Nloceff) sp_ids, sp_srt = cthlp.sort_gdf_by_id(sp, 0, Nloc) bins, bsp = cthlp.instantaneous_spike_count(sp_srt, self.tbin) bsp = cthlp.centralize(bsp, time=True) freq_power, power = ctana.powerspec(bsp, self.tbin) freq_cross, cross = ctana.crossspec(bsp, self.tbin) self.assertEqual(len(freq_power), len(freq_cross)) self.assertEqual(np.min(freq_power), np.min(freq_cross)) self.assertEqual(np.max(freq_power), np.max(freq_cross)) for i in xrange(Nloc): for j in xrange(Nloc): if i != j: # poisson trains are uncorrelated self.assertTrue(abs(np.mean(cross[i, j])) < 1e0) else: # compare with auto spectra self.assertTrue( abs(np.mean(cross[i, i] - power[i])) < 1e-12) sp = cthlp.create_poisson_spiketrains(self.rate, self.T, self.N) sp_ids, sp_srt = cthlp.sort_gdf_by_id(sp) bins, bsp = cthlp.instantaneous_spike_count(sp_srt, self.tbin) bsp = cthlp.centralize(bsp, time=True) freq_cross, cross = ctana.crossspec(bsp, self.tbin, units=True) self.assertTrue(abs(np.mean(cross)) < 1e-2) freq_cross, cross = ctana.crossspec( bsp, self.tbin, Df=self.Df, units=True) self.assertTrue(self.Df <= freq_cross[1])
def test_crosscorrfunc(self): Nloc = int(0.1 * self.N) Nloceff = int(0.1 * self.Neff) sp = cthlp.create_correlated_spiketrains_sip(self.rate, self.T, Nloceff, self.cc) sp_ids, sp_srt = cthlp.sort_gdf_by_id(sp, 0, Nloc) bins, bsp = cthlp.instantaneous_spike_count(sp_srt, self.tbin) freq, power = ctana.powerspec(bsp, self.tbin) freq_cross, cross = ctana.crossspec(bsp, self.tbin) time_auto, autof = ctana.autocorrfunc(freq, power) time_cross, crossf = ctana.crosscorrfunc(freq_cross, cross) if len(crossf[0, 0]) % 2 == 0: mid = int(len(crossf[0, 0]) / 2 - 1) else: mid = int(np.floor(len(crossf[0, 0]) / 2.)) offset = self.tbin / self.T * \ (self.rate + self.rate ** 2 * self.T * 1e-3) for i in range(Nloceff): # consistency check with auto-correlation function self.assertTrue(abs(np.sum(autof[i] - crossf[i, i])) < 1e-10) for j in range(Nloceff): if i != j: # c(0) = corrcoef*rate+offset self.assertTrue( abs(crossf[i, j][mid] - (self.cc * self.rate + offset)) < (self.cc * self.rate + offset) * 1e-1) # c(0)/a(0) = corrcoef self.assertTrue( abs((crossf[i, j][mid] - offset) / np.sqrt( (crossf[i, i][mid] - offset) * (crossf[j, j][mid] - offset)) - self.cc) < self.cc * 5e-2) freq, power = ctana.powerspec(bsp, self.tbin, units=True) freq_cross, cross = ctana.crossspec(bsp, self.tbin, units=True) time, autof = ctana.autocorrfunc(freq, power) time_cross, crossf = ctana.crosscorrfunc(freq, cross) offset_auto = self.p * self.tbin / self.T * \ (self.rate + self.rate ** 2 * self.T * 1e-3) offset_cross = 1. * Nloceff * \ (Nloceff - 1) / Nloc / (Nloc - 1) * self.tbin / \ self.T * (self.rate + self.rate ** 2 * self.T * 1e-3) # c(0) ~ self.p**2*corrcoef*rate+offset self.assertTrue( abs(crossf[mid] - (1. * Nloceff * (Nloceff - 1) / Nloc / (Nloc - 1) * self.cc * self.rate + offset_cross)) < (1. * Nloceff * (Nloceff - 1) / Nloc / (Nloc - 1) * self.cc * self.rate + offset_cross) * 2e-1) # c(0)/a(0) = corrcoef self.assertTrue( abs((crossf[mid] - offset_cross) / (autof[mid] - offset_auto) - 1. * (Nloceff - 1.) / (Nloc - 1.) * self.cc) < 1. * (Nloceff - 1.) / (Nloc - 1.) * self.cc * 2e-1)
def test_compound_crossspec(self): # use less neurons (0.2*self.N) for full matrix (memory error!) Nloc = int(0.2 * self.N) Nloceff = int(0.2 * self.Neff) # population a sp_a = cthlp.create_poisson_spiketrains(self.rate, self.T, Nloceff) sp_a_ids, sp_a_srt = cthlp.sort_gdf_by_id(sp_a, 0, Nloc) bins_a, bsp_a = cthlp.instantaneous_spike_count( sp_a_srt, self.tbin, tmin=0., tmax=self.T) bsp_a = cthlp.centralize(bsp_a, time=True) # population b sp_b = cthlp.create_poisson_spiketrains(self.rate, self.T, Nloceff) sp_b_ids, sp_b_srt = cthlp.sort_gdf_by_id(sp_b, 0, Nloc) bins_b, bsp_b = cthlp.instantaneous_spike_count( sp_b_srt, self.tbin, tmin=0., tmax=self.T) bsp_b = cthlp.centralize(bsp_b, time=True) freq_a, power_a = ctana.compound_powerspec(bsp_a, self.tbin) freq_b, power_b = ctana.compound_powerspec(bsp_b, self.tbin) freq_cross, cross = ctana.compound_crossspec([bsp_a, bsp_b], self.tbin) self.assertTrue(abs(np.sum(power_a - cross[0, 0])) < 1e-10) self.assertTrue(abs(np.sum(power_b - cross[1, 1])) < 1e-10) freq_cross_alt, cross_alt = ctana.crossspec( np.array([np.sum(bsp_a, axis=0), np.sum(bsp_b, axis=0)]), self.tbin) self.assertTrue(abs(np.sum(cross_alt[0, 1] - cross[0, 1])) < 1e-12) self.assertTrue(abs(np.sum(cross_alt[1, 0] - cross[1, 0])) < 1e-12)
def test_coherence(self): # use less neurons (0.2*self.N) for full matrix (memory error!) Nloc = int(0.15 * self.N) Nloceff = int(0.15 * self.Neff) sp = cthlp.create_correlated_spiketrains_sip(self.rate, self.T, Nloceff, self.cc) sp_ids, sp_srt = cthlp.sort_gdf_by_id(sp, 0, Nloc) bins, bsp = cthlp.instantaneous_spike_count(sp_srt, self.tbin) # test all pairs of active neurons # TODO why does the result not depend on centralizing? # bsp = cthlp.centralize(bsp, time=True) freq_cross, cross = ctana.crossspec(bsp, self.tbin, Df=self.Df) df = freq_cross[1] - freq_cross[0] a_lfcoh = [] for i in range(Nloc): for j in range(Nloc): if i != j: if np.sum(cross[i, i]) > 0. and np.sum(cross[j, j]) > 0.: lfcoh = np.mean((np.real(cross[i, j]) / np.sqrt( cross[i, i] * cross[j, j]))[:int(self.fcut / df)]) a_lfcoh.append(lfcoh) self.assertTrue(abs(lfcoh - self.cc) < self.cc * 4e-1) else: a_lfcoh.append(0.) # average correlation coefficient is p**2 smaller than correlation # between active neurons self.assertTrue( abs(np.mean(a_lfcoh) - self.p**2 * self.cc) < self.cc * 1e-1) # test coherence of population averaged signals # (careful with interpretation!) freq_power, power = ctana.powerspec(bsp, self.tbin, Df=self.Df, units=True) freq_cross, cross = ctana.crossspec(bsp, self.tbin, Df=self.Df, units=True) # make sure frequencies are the same for power and cross self.assertEqual(len(freq_power), len(freq_cross)) self.assertEqual(np.min(freq_power), np.min(freq_cross)) self.assertEqual(np.max(freq_power), np.max(freq_cross)) df = freq_cross[1] - freq_cross[0] lfcoh = np.mean((cross / power)[:int(self.fcut / df)]) # low frequency coherence should coincide with corrcoef self.assertTrue(abs(lfcoh - self.p * self.cc) < self.cc * 1e-1)
def test_corrcoef(self): Nloc = int(0.1 * self.N) Nloceff = int(0.1 * self.Neff) sp = cthlp.create_correlated_spiketrains_sip( self.rate, self.T, Nloceff, self.cc) sp_ids, sp_srt = cthlp.sort_gdf_by_id(sp, 0, Nloc) bins, bsp = cthlp.instantaneous_spike_count(sp_srt, self.tbin) freq_cross, cross = ctana.crossspec(bsp, self.tbin) time_cross, crossf = ctana.crosscorrfunc(freq_cross, cross) corrcoef = ctana.corrcoef(time_cross, crossf) for i in xrange(Nloc): for j in xrange(Nloc): if i < Nloceff and j < Nloceff: if i == j: self.assertTrue(abs(corrcoef[i, j] - 1.) < 1e-12) else: self.assertTrue( abs(corrcoef[i, j] - self.cc) < self.cc * 1e-1) else: self.assertTrue(abs(corrcoef[i, j]) < 1e-12)
rate_time_series2 = np.load(fn2) fn = os.path.join(load_path, 'rate_time_series_full_Parameters.json') with open(fn, 'r') as f: params = json.load(f) i_min = int(500. - params['t_min']) i_max = int(T - params['t_min']) rates = [rate_time_series1[i_min:i_max], rate_time_series2[i_min:i_max]] dat = [ch.centralize(rates[0], units=True), ch.centralize(rates[1], units=True)] freq, crossspec = corr.crossspec(dat, 1.) t, cross = corr.crosscorrfunc(freq, crossspec) sigma = 2. time_range = np.arange(-5., 5.) kernel = 1 / (np.sqrt(2.0 * np.pi) * sigma) * np.exp(-(time_range ** 2 / (2 * sigma ** 2))) cross_conv = np.zeros_like(cross) cross_conv[0][0] = np.convolve(kernel, cross[0][0], mode='same') cross_conv[0][1] = np.convolve(kernel, cross[0][1], mode='same') cross_conv[1][0] = np.convolve(kernel, cross[1][0], mode='same') cross_conv[1][1] = np.convolve(kernel, cross[1][1], mode='same') cross = cross_conv fp = '_'.join(('cross_correlation', area1, area2))
def crosscorrf(times, a_s, tmax, tbin): times_bin, st = hlp.bin_binary_data(times, a_s, tbin) freq, cross = ctana.crossspec(st, tbin, units=True) return ctana.crosscorrfunc(freq, cross)