def trackmsd(track, dt0, dtau): """ trackmsd(track, dt0, dtau) finds the track msd, as function of tau, averaged over t0, for one track (worldline) """ trackdots = data[trackids==track] if dt0 == dtau == 1: if verbose: print "Using correlation" xy = np.column_stack([trackdots['x'], trackdots['y']]) return corr.msd(xy, ret_taus=True) trackbegin, trackend = trackdots['f'][[0,-1]] tracklen = trackend - trackbegin + 1 if verbose: print "tracklen =",tracklen print "\t from %d to %d"%(trackbegin, trackend) if isinstance(dtau, float): taus = helpy.farange(dt0, tracklen, dtau) elif isinstance(dtau, int): taus = xrange(dtau, tracklen, dtau) tmsd = [] for tau in taus: # for tau in T, by factor dtau #print "tau =", tau avg = t0avg(trackdots, tracklen, tau) #print "avg =", avg if avg > 0 and not np.isnan(avg): tmsd.append([tau,avg[0]]) if verbose: print "\t...actually", len(tmsd) return tmsd
def trackmsd(track, dt0, dtau): """ finds the mean squared displacement as a function of tau, averaged over t0, for one track (particle) parameters ---------- track : a single integer giving the track id to be calculated dt0 : spacing stepsize for values of t0, gives the number of starting points averaged over in `t0avg` dtau : spacing stepsize for values of tau, gives the spacing of the points in time at which the msd is evaluated For dt0, dtau: Small values will take longer to calculate without adding to the statistics. Large values calculate faster but give worse statistics. For the special case dt0 = dtau = 1, a correlation is used for a signicant speedup returns ------- a list of tuples (tau, msd(tau)) the value of tau and the mean squared displacement for a single track at that value of tau """ trackdots = data[trackids==track] if dt0 == dtau == 1: if verbose: print "Using correlation" xy = np.column_stack([trackdots['x'], trackdots['y']]) return corr.msd(xy, ret_taus=True) trackbegin, trackend = trackdots['f'][[0,-1]] tracklen = trackend - trackbegin + 1 if verbose: print "tracklen =",tracklen print "\t from %d to %d"%(trackbegin, trackend) if isinstance(dtau, float): taus = helpy.farange(dt0, tracklen, dtau) elif isinstance(dtau, int): taus = xrange(dtau, tracklen, dtau) tmsd = [] for tau in taus: # for tau in T, by factor dtau #print "tau =", tau avg = t0avg(trackdots, tracklen, tau) #print "avg =", avg if avg > 0 and not np.isnan(avg): tmsd.append([tau,avg[0]]) if verbose: print "\t...actually", len(tmsd) return tmsd
def trackmsd(trackset, dt0, dtau): """ finds the mean squared displacement as a function of tau, averaged over t0, for one track (particle) parameters ---------- trackset : a subset of the data for a given track dt0 : spacing stepsize for values of t0, gives the number of starting points averaged over in `t0avg` dtau : spacing stepsize for values of tau, gives the spacing of the points in time at which the msd is evaluated For dt0, dtau: Small values will take longer to calculate without adding to the statistics. Large values calculate faster but give worse statistics. For the special case dt0 = dtau = 1, a correlation is used for a signicant speedup returns ------- a list of tuples (tau, msd(tau)) the value of tau and the mean squared displacement for a single track at that value of tau """ if dt0 == dtau == 1: xy = helpy.consecutive_fields_view(trackset, 'xy') return corr.msd(xy, ret_taus=True) trackbegin, trackend = trackset['f'][[0,-1]] tracklen = trackend - trackbegin + 1 if verbose: print "length {} from {} to {}".format(tracklen, trackbegin, trackend) if isinstance(dtau, float): taus = helpy.farange(dt0, tracklen, dtau) elif isinstance(dtau, int): taus = xrange(dtau, tracklen, dtau) tmsd = [] for tau in taus: avg = t0avg(trackset, tracklen, tau) if avg > 0 and not np.isnan(avg): tmsd.append([tau,avg[0]]) if verbose: print "\t...actually", len(tmsd) return tmsd
def trackmsd(track, dt0, dtau, data, trackids, odata, omask, mod_2pi=False): """ trackmsd(track, dt0, dtau, data, trackids, odata, omask) finds the track msd, as function of tau, averaged over t0, for one track (worldline) """ tmask = (trackids==track) & omask trackdots = data[tmask] if mod_2pi: trackodata = odata[tmask]['orient'] else: from orientation import track_orient trackodata = track_orient(odata, track, trackids, omask) if dt0 == dtau == 1: if verbose: print "Using correlation" from correlation import msd as corrmsd return corrmsd(trackodata, ret_taus=True) trackbegin, trackend = trackdots['f'][[0,-1]] tracklen = trackend - trackbegin + 1 if verbose: print "tracklen =",tracklen print "\t from %d to %d"%(trackbegin, trackend) if isinstance(dtau, float): taus = helpy.farange(dt0, tracklen, dtau) elif isinstance(dtau, int): taus = xrange(dtau, tracklen, dtau) tmsd = [] for tau in taus: # for tau in T, by factor dtau #print "tau =", tau avg = t0avg(trackdots, tracklen, tau, trackodata, dt0, mod_2pi=mod_2pi) #print "avg =", avg if avg > 0 and not np.isnan(avg): tmsd.append([tau,avg[0]]) if verbose: print "\t...actually", len(tmsd) return tmsd
def trackmsd(track, dt0, dtau, data, trackids, odata, omask, mod_2pi=False): """ trackmsd(track, dt0, dtau, data, trackids, odata, omask) finds the track msd, as function of tau, averaged over t0, for one track (worldline) """ tmask = (trackids == track) & omask trackdots = data[tmask] if mod_2pi: trackodata = odata[tmask]['orient'] else: from orientation import track_orient trackodata = track_orient(odata, track, trackids, omask) if dt0 == dtau == 1: if verbose: print "Using correlation" from correlation import msd as corrmsd return corrmsd(trackodata, ret_taus=True) trackbegin, trackend = trackdots['f'][[0, -1]] tracklen = trackend - trackbegin + 1 if verbose: print "tracklen =", tracklen print "\t from %d to %d" % (trackbegin, trackend) if isinstance(dtau, float): taus = helpy.farange(dt0, tracklen, dtau) elif isinstance(dtau, int): taus = xrange(dtau, tracklen, dtau) tmsd = [] for tau in taus: # for tau in T, by factor dtau #print "tau =", tau avg = t0avg(trackdots, tracklen, tau, trackodata, dt0, mod_2pi=mod_2pi) #print "avg =", avg if avg > 0 and not np.isnan(avg): tmsd.append([tau, avg[0]]) if verbose: print "\t...actually", len(tmsd) return tmsd
def plot_msd(msds, msdids, dtau, dt0, nframes, tnormalize=False, prefix='', show_tracks=True, figsize=(8,6), plfunc=plt.semilogx, meancol='', title=None, xlim=None, ylim=None, fignum=None, errorbars=False, lw=1, singletracks=None, fps=1, S=1, ang=False, sys_size=0, kill_flats=0, kill_jumps=1e9, show_legend=False, save='', show=True): """ Plots the MS(A)Ds """ A = 1 if ang else S**2 if verbose: print "using dtau = {}, dt0 = {}".format(dtau, dt0) print "using S = {} pixels, thus A = {} px^2".format(S, A) try: dtau = np.asscalar(dtau) except AttributeError: pass if isinstance(dtau, (float, np.float)): taus = helpy.farange(dt0, nframes+1, dtau) elif isinstance(dtau, (int, np.int)): taus = np.arange(dtau, nframes+1, dtau, dtype=float) fig = plt.figure(fignum, figsize) # Get the mean of msds msd = mean_msd(msds, taus, msdids, kill_flats=kill_flats, kill_jumps=kill_jumps, show_tracks=show_tracks, singletracks=singletracks, tnormalize=tnormalize, errorbars=errorbars, fps=fps, A=A) if errorbars: msd, msd_err = msd #print "Coefficient of diffusion ~", msd[np.searchsorted(taus, fps)]/A #print "Diffusion timescale ~", taus[np.searchsorted(msd, A)]/fps taus = taus[:len(msd)] taus /= fps msd /= A if errorbars: msd_err /= A if tnormalize: plfunc(taus, msd/taus**tnormalize, meancol, label="Mean Sq {}Disp/Time{}".format( "Angular " if ang else "", "^{}".format(tnormalize) if tnormalize != 1 else '')) plfunc(taus, msd[0]*taus**(1-tnormalize)/dtau, 'k-', label="ref slope = 1", lw=2) plfunc(taus, (twopi**2 if ang else 1)/(taus)**tnormalize, 'k--', lw=2, label=r"$(2\pi)^2$" if ang else ("One particle area" if S>1 else "One Pixel")) plt.ylim([0, 1.3*np.max(msd/taus**tnormalize)]) else: plt.loglog(taus, msd, meancol, lw=lw, label="Mean Squared {}Displacement".format('Angular '*ang)) #plt.loglog(taus, msd[0]*taus/dtau/2, meancol+'--', lw=2, # label="slope = 1") if errorbars: plt.errorbar(taus, msd/taus**tnormalize, msd_err/taus**tnormalize, fmt=meancol, capthick=0, elinewidth=1, errorevery=errorbars) if sys_size: plt.axhline(sys_size, ls='--', lw=.5, c='k', label='System Size') plt.title("Mean Sq {}Disp".format("Angular " if ang else "") if title is None else title) plt.xlabel('Time (' + ('s)' if fps > 1 else 'frames)'), fontsize='x-large') if ang: plt.ylabel('Squared Angular Displacement ($rad^2$)', fontsize='x-large') else: plt.ylabel('Squared Displacement ('+('particle area)' if S>1 else 'square pixels)'), fontsize='x-large') if xlim is not None: plt.xlim(*xlim) if ylim is not None: plt.ylim(*ylim) if show_legend: plt.legend(loc='best') if save is True: save = prefix + "_MS{}D.pdf".format('A' if ang else '') if save: print "saving to", save plt.savefig(save) if show: plt.show() return [fig] + fig.get_axes() + [taus] + [msd, msd_err] if errorbars else [msd]
def plot_msd(msds, msdids, dtau, dt0, nframes, tnormalize=False, prefix='', show_tracks=True, figsize=(8,6), plfunc=pl.semilogx, meancol='', title=None, xlim=None, ylim=None, fignum=None, errorbars=False, lw=1, singletracks=xrange(1000), fps=1, S=1, ang=False, sys_size=0, kill_flats=0, kill_jumps=1e9, show_legend=False, save='', show=True): """ Plots the MS(A)Ds """ print "using dtau = {}, dt0 = {}".format(dtau, dt0) A = 1 if ang else S**2 print "using S = {} pixels, thus A = {} px^2".format(S, A) try: dtau = np.asscalar(dtau) except AttributeError: pass if isinstance(dtau, (float, np.float)): taus = helpy.farange(dt0, nframes-1, dtau) elif isinstance(dtau, (int, np.int)): taus = np.arange(dtau, nframes-1, dtau, dtype=float) fig = pl.figure(fignum, figsize) # Get the mean of msds msd = mean_msd(msds, taus, msdids, kill_flats=kill_flats, kill_jumps=kill_jumps, show_tracks=show_tracks, singletracks=singletracks, tnormalize=tnormalize, errorbars=errorbars, fps=fps, A=A) if errorbars: msd, msd_err = msd #print "Coefficient of diffusion ~", msd[np.searchsorted(taus, fps)]/A #print "Diffusion timescale ~", taus[np.searchsorted(msd, A)]/fps if tnormalize: plfunc(taus/fps, msd/A/(taus/fps)**tnormalize, meancol, label="Mean Sq {}Disp/Time{}".format( "Angular " if ang else "", "^{}".format(tnormalize) if tnormalize != 1 else '')) plfunc(taus/fps, msd[0]/A*(taus/fps)**(1-tnormalize)/dtau, 'k-', label="ref slope = 1", lw=2) plfunc(taus/fps, (twopi**2 if ang else 1)/(taus/fps)**tnormalize, 'k--', lw=2, label=r"$(2\pi)^2$" if ang else ("One particle area" if S>1 else "One Pixel")) pl.ylim([0, 1.3*np.max(msd/A/(taus/fps)**tnormalize)]) else: pl.loglog(taus/fps, msd/A, meancol, lw=lw, label=prefix+'\ndt0=%d dtau=%d'%(dt0,dtau)) #pl.loglog(taus/fps, msd[0]/A*taus/dtau/2, meancol+'--', lw=2, # label="slope = 1") if errorbars: pl.errorbar(taus/fps, msd/A/(taus/fps)**tnormalize, msd_err/A/(taus/fps)**tnormalize, fmt=meancol, capthick=0, elinewidth=1, errorevery=errorbars) if sys_size: pl.axhline(sys_size, ls='--', lw=.5, c='k', label='System Size') pl.title("Mean Sq {}Disp".format("Angular " if ang else "") if title is None else title) pl.xlabel('Time (' + ('s)' if fps > 1 else 'frames)'), fontsize='x-large') if ang: pl.ylabel('Squared Angular Displacement ($rad^2$)', fontsize='x-large') else: pl.ylabel('Squared Displacement ('+('particle area)' if S>1 else 'square pixels)'), fontsize='x-large') if xlim is not None: pl.xlim(*xlim) if ylim is not None: pl.ylim(*ylim) if show_legend: pl.legend(loc='best') if save is None: save = locdir + prefix + "_MS{}D.pdf".format('A' if ang else '') if save: print "saving to", save pl.savefig(save) if show: pl.show() return [fig] + fig.get_axes() + [taus] + [msd, msd_err] if errorbars else [msd]