multi_locus_analysis.stats.moments

For computing moments of particle motions.

When dealing with trajectory data, most of the common readouts can simply be thought of as moments of the trajectory with different time lags or discrete derivatives applied.

More precisely, suppose you have trajectory data \(X_i(t_k)\), then we define the discrete velocity (the first discrete derivative of the particle as \(V_i^\delta(t_k) = X_i(t_k + \delta) - X_i(t_k)\) and the discrete relative velocities \(V_{ij}^\delta(t_k) = X_{ij}(t_k + \delta) - X_{ij}(t_k)\) where \(X_{ij}\) is the distance between the points \(i\) and \(j\). This module simply helps quickly calculate moments of the \(V^\delta(t)\).

For example, the ensemble MSD is simply \(E_i[(V_i^\delta(0))^2]\) as a function of \(\delta\), where \(E_\chi[\cdot]\) indicates the average is taken over the variable \(\chi\). The time-averaged MSD for a single particle on the other hand is given by \(E_t[(V_i^\delta(t))^2]\).

This module provides a simple set of functions for easily extracting the moment of any product of any derivatives with any combination of time lags of a set of trajectories, \(X_i(t_k)\).

multi_locus_analysis.stats.moments.all_vel_to_corr(vel, dxcol='vx', dycol='vy', dzcol=None, framecol='tf', max_dt=None, max_t_over_delta=4, allowed_dts=None)[source]
>>> all_vel.reset_index(level=pandas_util.multiindex_col_ix(all_vel, 'ti'), inplace=True)
multi_locus_analysis.stats.moments.combine_moments(data)[source]

Takes a column from output of df.groupby([…]).apply(moments) and combines the values to get overall moments for full dataset.

Example:

grouped_stats = df.groupby(['experiment']).apply(lp.moments)
overall_stats = grouped_stats.groupby(level=0, axis=1).apply(lp.combine_moments).unstack()

Example:

def grouper(g):
    return g.groupby(level=0, axis=1).apply(lp.combine_moments).unstack()
grouped_stats[new_col] = make_interesting_value(grouped_stats)
overall_stats = df.groupby(new_col).apply(grouper)
multi_locus_analysis.stats.moments.corr_prod(df, xcol, tcol=None, max_dt=None, max_t_over_delta=4, allowed_dts=None, t_step=None)[source]

Calculates the time averaged autocorraltion correlation over a column, with an optional time column.

The time average can be written .. math:

C_X(t) = E_\tau[X_{\tau + t}X_\tau]
multi_locus_analysis.stats.moments.cvv_by_hand_make_usable(cvv_stats, group_cols)[source]

add columns to vvc_stats_by_hand output that we typically want. (cvv, std, ste, cvv_normed, ste_normed). requires the group-by columns used to do the normalization via msds using groupby

multi_locus_analysis.stats.moments.groupby_apply_efficient(df, group_cols, apply_fun, n_between_gc=50, print_progress=False, apply_to_cols=None)[source]

An attempt to make groupby not leak memory. Kinda worked at one point (late 2016).

multi_locus_analysis.stats.moments.moments(df, cols=None, ns=[0, 1, 2])[source]

Take a DataFrame and optionally a list of columns of interest and for each n in ns, calculate the nth moment of each column, where the 0th moment in defined for convenience to be the count.

Returns a series with multi-index with two levels. Level 0 contains the requested columns and level 1 contains the requested moment numbers (ns).

multi_locus_analysis.stats.moments.pos_to_all_vel(trace, xcol='x', ycol='y', zcol=None, framecol='i', delta=None, delta_max=None, deltas=None, fixed_dt=None, force_independence=False)[source]

For getting displacement distributions for all possible deltas simultaneously.

>>> all_vel = df.groupby(bp.track_columns).apply(lambda df:
            mla.pos_to_all_vel(df, xcol='dX', ycol='dY', framecol='tp.n'
    ))

fixed_dt should be the value of the fixed value of dt between consecutive frames, if such a value exists

multi_locus_analysis.stats.moments.traj_to_msds(traj, xcol='x', ycol='y', framecol='frame id')[source]

Data should be groupby’d trajectories. Takes x,y coordinate columns and the “time” column, expects integer index for time column.

multi_locus_analysis.stats.moments.vels_to_cvvs_by_hand(vels, groups, file_name, framecol='ti', dxcol='vx', dycol='vy', dzcol='vz', max_t_over_delta=None, max_dt=None, allowed_dts=None, deltas_of_interest=None, include_group=True)[source]

should be passed a velocities dataframe (as from pos_to_all_vel.

groups vels by groups, optionally ignores group labels to make a flat file with all vvc information with repeats of t,delta pairs as appropriate.

multi_locus_analysis.stats.moments.vvc_stats_by_hand(file_name, groups, print_interval=None, skip_lines=None)[source]

Calculates moments using the output of vels_to_cvvs_by_hand.