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_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)
Exemplo n.º 3
0
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))
np.save('{}/{}.npy'.format(save_path, fp), cross)
Exemplo n.º 4
0
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)