def test_nth_diff(): d = {'col1': [1, 2, 3, 4, 5]} df = pd.DataFrame(data=d) test_d = {'col1': [1, 1, 1, 1]} test_df = pd.DataFrame(data=test_d) pdt.assert_series_equal(nth_diff(df['col1'], 1), test_df['col1'])
def test_nth_diff(): d = {'col1': [1, 2, 3, 4, 5]} df = pd.DataFrame(data=d) test_d = {'col1': [1, 1, 1, 1]} test_df = pd.DataFrame(data=test_d) pdt.assert_series_equal(msd.nth_diff(df['col1'], 1), test_df['col1']) #test2 df = np.ones((5, 10)) test_df = np.zeros((5, 9)) npt.assert_equal(msd.nth_diff(df, 1, 1), test_df) df = np.ones((5, 10)) test_df = np.zeros((4, 10)) npt.assert_equal(msd.nth_diff(df, 1, 0), test_df)
def efficiency(track): """Calculates the efficiency and straitness of the input track Parameters ---------- track : pandas.core.frame.DataFrame At a minimum, must contain a Frames and a MSDs column. The function msd_calc can be used to generate the correctly formatted pd dataframe. Returns ------- eff : float Efficiency of the input track. Relates the sum of squared step lengths. Based on Helmuth et al. (2007) and defined as: E = |xpos(N-1)-xpos(0)|**2/SUM(|xpos(i) - xpos(i-1)|**2 strait : float Relates the net displacement netdisp to the sum of step lengths and is defined as: S = |xpos(N-1)-xpos(0)|/SUM(|xpos(i) - xpos(i-1)| Examples -------- >>> frames = 10 >>> data1 = {'Frame': np.linspace(1, frames, frames), ... 'X': np.linspace(1, frames, frames)+5, ... 'Y': np.linspace(1, frames, frames)+3} >>> dframe = pd.DataFrame(data=data1) >>> dframe['MSDs'], dframe['Gauss'] = msd_calc(dframe) >>> ft.efficiency(dframe) (9.0, 0.9999999999999999) >>> frames = 10 >>> data1 = {'Frame': np.linspace(1, frames, frames), ... 'X': np.sin(np.linspace(1, frames, frames))+3, ... 'Y': np.cos(np.linspace(1, frames, frames))+3} >>> dframe = pd.DataFrame(data=data1) >>> dframe['MSDs'], dframe['Gauss'] = msd_calc(dframe) >>> ft.efficiency(dframe) (0.46192924086141945, 0.22655125514290225) """ dframe = track length = dframe.shape[0] num = (msd.nth_diff(dframe['X'], length-1)**2 + msd.nth_diff(dframe['Y'], length-1)**2)[0] num2 = np.sqrt(num) den = np.sum(msd.nth_diff(dframe['X'], 1)**2 + msd.nth_diff(dframe['Y'], 1)**2) den2 = np.sum(np.sqrt(msd.nth_diff(dframe['X'], 1)**2 + msd.nth_diff(dframe['Y'], 1)**2)) eff = num/den strait = num2/den2 return eff, strait
def boundedness(track, framerate=1): """Calculates the boundedness, fractal dimension, and trappedness of the input track. Parameters ---------- track : pandas.core.frame.DataFrame At a minimum, must contain a Frames and a MSDs column. The function msd_calc can be used to generate the correctly formatted pd dataframe. framerate : framrate of the video being analyzed. Actually cancels out. So why did I include this. Default is 1. Returns ------- bound : float Boundedness of the input track. Quantifies how much a particle with diffusion coefficient dcoef is restricted by a circular confinement of radius rad when it diffuses for a time duration N*delt. Defined as bound = dcoef*N*delt/rad**2. For this case, dcoef is the short time diffusion coefficient (after 2 frames), and rad is half the maximum distance between any two positions. fractd : float The fractal path dimension defined as fractd = log(N)/log(N*data1*l**-1) where netdisp is the total length (sum over all steplengths), N is the number of steps, and data1 is the largest distance between any two positions. probf : float The probability that a particle with diffusion coefficient dcoef and traced for a period of time N*delt is trapped in region r0. Given by pt = 1 - exp(0.2048 - 0.25117*(dcoef*N*delt/r0**2)). For this case, dcoef is the short time diffusion coefficient, and r0 is half the maximum distance between any two positions. Examples -------- >>> frames = 10 >>> data1 = {'Frame': np.linspace(1, frames, frames), ... 'X': np.linspace(1, frames, frames)+5, ... 'Y': np.linspace(1, frames, frames)+3} >>> dframe = pd.DataFrame(data=data1) >>> dframe['MSDs'], dframe['Gauss'] = msd_calc(dframe) >>> boundedness(dframe) (1.0, 1.0000000000000002, 0.045311337970735499) >>> frames = 10 >>> data1 = {'Frame': np.linspace(1, frames, frames), ... 'X': np.sin(np.linspace(1, frames, frames)+3), ... 'Y': np.cos(np.linspace(1, frames, frames)+3)} >>> dframe = pd.DataFrame(data=data1) >>> dframe['MSDs'], dframe['Gauss'] = msd_calc(dframe) >>> boundedness(dframe) (0.96037058689895005, 2.9989749477908401, 0.03576118370932313) """ dframe = track assert isinstance(dframe, pd.core.frame.DataFrame), "track must be a pandas\ dataframe." assert isinstance(dframe['X'], pd.core.series.Series), "track must contain\ X column." assert isinstance(dframe['Y'], pd.core.series.Series), "track must contain\ Y column." assert dframe.shape[0] > 0, "track must not be empty." dframe = track if dframe.shape[0] > 2: length = dframe.shape[0] distance = np.zeros((length, length)) for frame in range(0, length-1): distance[frame, 0:length-frame-1] =\ (np.sqrt(msd.nth_diff(dframe['X'], frame+1)**2 + msd.nth_diff(dframe['Y'], frame+1)**2).values) netdisp = np.sum((np.sqrt(msd.nth_diff(dframe['X'], 1)**2 + msd.nth_diff(dframe['Y'], 1)**2).values)) rad = np.max(distance)/2 N = dframe['Frame'][dframe['Frame'].shape[0]-1] fram = N*framerate dcoef = dframe['MSDs'][2]/(4*fram) bound = dcoef*fram/(rad**2) fractd = np.log(N)/np.log(N*2*rad/netdisp) probf = 1 - np.exp(0.2048 - 0.25117*(dcoef*fram/(rad**2))) else: bound = np.nan fractd = np.nan probf = np.nan return bound, fractd, probf
def boundedness(track, framerate=1): """ Calculates the boundedness, fractal dimension, and trappedness of the input track. Parameters ---------- track : pandas DataFrame At a minimum, must contain a Frames and a MSDs column. The function msd_calc can be used to generate the correctly formatted pd dataframe. framerate : framrate of the video being analyzed. Actually cancels out. So why did I include this. Default is 1. Returns ------- B : numpy.float64 Boundedness of the input track. Quantifies how much a particle with diffusion coefficient D is restricted by a circular confinement of radius r when it diffuses for a time duration N*delt. Defined as B = D*N*delt/r**2. For this case, D is the short time diffusion coefficient (after 2 frames), and r is half the maximum distance between any two positions. Df : numpy.float64 The fractal path dimension defined as Df = log(N)/log(N*d*l**-1) where L is the total length (sum over all steplengths), N is the number of steps, and d is the largest distance between any two positions. pf : numpy.float64 The probability that a particle with diffusion coefficient D and traced for a period of time N*delt is trapped in region r0. Given by pt = 1 - exp(0.2048 - 0.25117*(D*N*delt/r0**2)) For this case, D is the short time diffusion coefficient, and r0 is half the maximum distance between any two positions. Examples -------- >>> frames = 10 >>> d = {'Frame': np.linspace(1, frames, frames), 'X': np.linspace(1, frames, frames)+5, 'Y': np.linspace(1, frames, frames)+3} >>> df = pd.DataFrame(data=d) >>> df['MSDs'], df['Gauss'] = msd_calc(df) >>> boundedness(df) (1.0, 1.0000000000000002, 0.045311337970735499) >>> frames = 10 >>> d = {'Frame': np.linspace(1, frames, frames), 'X': np.sin(np.linspace(1, frames, frames)+3), 'Y': np.cos(np.linspace(1, frames, frames)+3)} >>> df = pd.DataFrame(data=d) >>> df['MSDs'], df['Gauss'] = msd_calc(df) >>> boundedness(df) (0.96037058689895005, 2.9989749477908401, 0.03576118370932313) """ assert type( track) == pd.core.frame.DataFrame, "track must be a pandas dataframe." assert type(track['MSDs'] ) == pd.core.series.Series, "track must contain MSDs column." assert type(track['Frame'] ) == pd.core.series.Series, "track must contain Frame column." assert track.shape[0] > 0, "track must not be empty." df = track if df.shape[0] > 2: length = df.shape[0] distance = np.zeros((length, length)) for frame in range(0, length - 1): distance[frame, 0:length - frame - 1] = (np.sqrt( msd.nth_diff(df['X'], frame + 1)**2 + msd.nth_diff(df['Y'], frame + 1)**2).values) L = np.sum(( np.sqrt(msd.nth_diff(df['X'], 1)**2 + msd.nth_diff(df['Y'], 1)**2).values)) r = np.max(distance) / 2 N = df['Frame'][df['Frame'].shape[0] - 1] f = N * framerate D = df['MSDs'][2] / (4 * f) B = D * f / (r**2) Df = np.log(N) / np.log(N * 2 * r / L) pf = 1 - np.exp(0.2048 - 0.25117 * (D * f / (r**2))) else: B = np.nan Df = np.nan pf = np.nan return B, Df, pf