def test_pcorrelate_normalization(data): """Test pcorrelate algorithm using np.histogram.""" t, u, unit = data bins = pyc.make_loglags(-6, 0, 20, base=10, return_int=False) / unit Gn = pyc.pcorrelate(t, u, bins, normalize=True) G = pyc.pcorrelate(t, u, bins, normalize=False) Gn2 = pyc.pnormalize(G, t, u, bins) assert (Gn == Gn2).all()
def calc_fcs_dd_da(burstsph, bins, seed=1): ts_d = burstsph.loc[burstsph.stream == 'DexDem', 'timestamp'].values.copy() ts_a = burstsph.loc[burstsph.stream == 'DexAem', 'timestamp'].values.copy() np.random.seed(seed) ts_d += np.random.randint(-25, 25, size=ts_d.size) ts_a += np.random.randint(-25, 25, size=ts_a.size) CC_DA = pyc.pcorrelate(ts_d, ts_a, bins, normalize=True) + 1 AC_DD = pyc.pcorrelate(ts_d, ts_d, bins, normalize=True) + 1 return CC_DA, AC_DD
def xcorrFine(x, y, bins): print('starting xcorr') print('len(x), len(y)', len(x), len(y)) print('len(bins)', len(bins)) cc = pyc.pcorrelate(x, y, bins) zero_idx = np.floor(len(cc) / 2) shift = np.argmax(cc) - zero_idx print('shift', shift) return cc, shift
def antibunching(self, samples, timegate): rep = self.meta['tags']['TTResult_SyncRate']['value'] maxCorr = round(1 / rep * 1e6, 1) + (round((1 / rep * 1e6) / 2, 1)) l = -maxCorr * 1e-6 m = maxCorr * 1e-6 sp = samples p = (m - l) / sp lags = np.arange(l, m, p) self.antibunchX = (lags[:len(lags) - 1]) * 1e6 correctNanotimes = self.nanotimes * self.meta['nanotimes_unit'] a = self.truetime[(self.detectors == 0) & (correctNanotimes > (timegate / 1e9))] b = self.truetime[(self.detectors == 1) & (correctNanotimes > (timegate / 1e9))] self.G = pyc.pcorrelate(a, b, lags, 1) self.H = pyc.pcorrelate(b, a, lags, 1) self.antibunchY = (self.G + self.H) / 2 return self.antibunchY, self.antibunchX
def test_pcorrelate_vs_ucorrelate(data): t, u, unit = data binwidth = 50e-6 bins_tt = np.arange(0, t.max() * unit, binwidth) / unit bins_uu = np.arange(0, u.max() * unit, binwidth) / unit tx, _ = np.histogram(t, bins=bins_tt) ux, _ = np.histogram(u, bins=bins_uu) Gu = pyc.ucorrelate(tx, ux) maxlag_sec = 1.2 # seconds lagbins = (np.arange(0, maxlag_sec, binwidth) / unit).astype('int64') Gp = pyc.pcorrelate(t, u, lagbins) * int(binwidth / unit) n = 6000 err = np.abs(Gp[:n] - Gu[:n]) / (0.5 * (Gp[:n] + Gu[:n])) assert err.max() < 0.23 assert err.mean() < 0.05
def corr4(i1, i2, window, bin_length=0.001): """ Correlation between spike trains. Accepts spike rasters. Parameters -------- i1, i2: Indices of spike rasters to compare window: The maximum time lag to consider, in seconds. bin_length: Length of each bin. Default is 1 ms. """ i1 = clusters[i1][:2] i2 = clusters[i2][:2] x1 = asc.read_raster(exp, stim, *i1) x2 = asc.read_raster(exp, stim, *i2) corr_bins = np.arange(-window, window, bin_length) corr = pycorrelate.pcorrelate(x1, x2, corr_bins, normalize=True) return corr
def test_pcorrelate(data): """Test pcorrelate algorithm using np.histogram.""" t, u, unit = data n = 10000 # ~ 3s of data t, u = t[:n], u[:n] bins = pyc.make_loglags(1, 8, 20, base=10, return_int=True) nbins = len(bins) - 1 G = pyc.pcorrelate(t, u, bins) Y = np.zeros(nbins, dtype=np.int64) for ti in t: Yc, _ = np.histogram(u - ti, bins=bins) Y += Yc Gn = Y / np.diff(bins) # The last bin in np.histogram includes the right edge while pcorrelate # does not. This creates a potential discrepancy for the last bin. assert np.allclose(G, Gn) assert np.allclose(G[:-1], Gn[:-1]) # However the value in the last bin when using np.histogram needs to be # >= than pcorrelate value. assert (Gn[-1] >= G[-1]).all()
x = correlator.get_x_axis_normalized() y = correlator.get_corr_normalized() p.semilogx(x, y) p.show() # Use this to compare noise of the correlation curve (sth. seems to be off in the pycorrelate # implementation... import pycorrelate t1 = mt[ch1_indeces] t2 = mt[ch2_indeces] bins = pycorrelate.make_loglags(0, np.log10(tau[-2]), 30) G = pycorrelate.pcorrelate(t1.astype(np.float), t2.astype(np.float), bins, True) p.semilogx(bins[1:], G) p.show() %time G=pycorrelate.pcorrelate(t1.astype(np.float), t2.astype(np.float), bins, True) ####################################### # Selection based on time windows ####################################### import tttrlib import numpy as np import pylab as p photons = tttrlib.TTTR('../../examples/bh/BH_SPC132.spc', 2) mt = photons.macro_times
import numpy as np import time from scipy.fftpack import fft, ifft, fftshift import matplotlib.pyplot as plt from numpy.lib.stride_tricks import as_strided import random from scipy.linalg import toeplitz import pycorrelate as pyc arr1 = np.array(range(1, 1001)) arr2a = np.array([]) arr2b = np.array(range(500, 1001)) arr2 = np.concatenate((arr2a, arr2b)) lowerIdx = -1000 upperIdx = 0 numBins = 500 binSize = (upperIdx - lowerIdx) / numBins bins = np.linspace(lowerIdx, upperIdx, numBins) cc = pyc.pcorrelate(arr2, arr1, bins) plt.figure() plt.plot(bins[1:len(bins)], cc) plt.show() shift = (np.argmax(cc) - int(numBins / 2)) * binSize print('shift', shift)
def corr2(x1, x2=None, window=0.05, res=0.001): if x2 is None: x2 = x1 corr = pycorrelate.pcorrelate(x1, x2, window, res) return corr
#%% from scipy import signal def plotpeaks(i, j): xcorr = xcorrs[i, j, :] peaks = signal.find_peaks(xcorr, prominence=200)[0] plt.plot(xcorr) plt.plot(peaks, xcorr[peaks], 'x') plt.axvline(xcorr.shape[0] / 2, color='grey', linestyle='dashed') plt.show() plotpeaks(10, 1) #%% spikes = asc.read_raster(exp, stim, 1, 1) spikes2 = asc.read_raster(exp, stim, 1, 3) t = np.arange(0, spikes[-1], 0.001) bsp = asc.binspikes(spikes, t) bsp2 = asc.binspikes(spikes2, t) #plt.plot(corr(bsp, window=40)) sprcorwindow = 0.05 corr_bin = np.arange(-sprcorwindow, sprcorwindow, .001) pycorp = pycorrelate.pcorrelate(spikes, spikes2, corr_bin, normalize=True) plt.plot(pycorp) plt.show()