# that is, if f.min() > fmin for a track, then it still aligns at zero rncorrs = helpy.pad_uneven([ rn[np.searchsorted(f, fmin):np.searchsorted(f, fmax)] for f, rn in rncorrs if f.min() <= fmin ], np.nan) tcorr = np.arange(fmin, fmax)/fps meancorr = np.nanmean(rncorrs, 0) added = np.sum(np.isfinite(rncorrs), 0) errcorr = np.nanstd(rncorrs, 0)/np.sqrt(added - 1) sigma = errcorr + errcorr.std() # add something small to prevent 0 if args.verbose: print "Merged rn corrs" # Fit to capped exponential growth fitform = lambda s, v_D, D=D_R:\ 0.5*np.sign(s)*v_D*(1 - corr.exp_decay(np.abs(s), 1/D)) fitstr = r'$\frac{v_0}{2D_R}(1 - e^{-D_R|s|})\operatorname{sign}(s)$' # p0 = [v_0/D_R, D_R] p0 = [1] if not args.nn or args.fitdr: p0 += [D_R] print "=============" print "Fits to <rn>:" try: popt, pcov = curve_fit(fitform, tcorr, meancorr, p0=p0, sigma=sigma) except RuntimeError as e: try: p0[0] = -1 # If dots are backwards, has trouble fitting with v0>0 popt, pcov = curve_fit(fitform, tcorr, meancorr, p0=p0, sigma=sigma) except RuntimeError as e:
sincorrs = [ corr.autocorr(np.sin(otrackset), cumulant=False, norm=False) for otrackset in otracksets.values() ] # Gather all the track correlations and average them allcorr = coscorrs + sincorrs allcorr = helpy.pad_uneven(allcorr, np.nan) tcorr = np.arange(allcorr.shape[1])/fps meancorr = np.nanmean(allcorr, 0) errcorr = np.nanstd(allcorr, 0)/sqrt(len(allcorr)) if verbose: print "Merged nn corrs" # Fit to exponential decay tmax = 50 fmax = np.searchsorted(tcorr, tmax) fitform = lambda *args, **kwargs: 0.5*corr.exp_decay(*args, **kwargs) popt, pcov = curve_fit(fitform, tcorr[:fmax], meancorr[:fmax], p0=[1], sigma=errcorr[:fmax].mean() + errcorr[:fmax]) D_R = 1/popt[0] print 'D_R: {:.4f}'.format(D_R) pl.figure() plot_individual = True if plot_individual: pl.semilogy(tcorr, allcorr.T, 'b', alpha=.25) pl.errorbar(tcorr, meancorr, errcorr, None, 'ok', capthick=0, elinewidth=1, errorevery=3) pl.plot(tcorr, fitform(tcorr, *popt), 'r', label=r"$\frac{1}{2}e^{-D_R t}$" + '\n' +\ "$D_R$: {:.4f}, $1/D_R$: {:.3f}".format(D_R, 1/D_R)) pl.xlim(0, tmax)