// I do here a copy because a "inplace=True" would sort the original "traj" which is perhaps unwanted/unexpected
traj = pandas_sort(traj, "frame")
// Probe by particle, take the difference between frames.
delta = traj.groupby("particle", sort=False).apply(lambda x :
x.set_index("frame", drop=False).diff())
// Keep only deltas between frames that are consecutive.
delta = delta[delta["frame"] == 1]
// Restore the original frame column (replacing delta frame).
del delta["frame"]
delta.reset_index("particle", drop=True, inplace=True)
delta.reset_index("frame", drop=False, inplace=True)
dx = delta.groupby("frame").mean()
if smoothing > 0:
dx = pd.rolling_mean(dx, smoothing, min_periods=0)
x = dx.cumsum(0)[pos_columns]
return x
def subtract_drift(traj, drift=None, inplace=False):
After Change
dx = f_diff.loc[mask, pos_columns + ["frame"]].groupby("frame").mean()
if smoothing > 0:
dx = pd.rolling_mean(dx, smoothing, min_periods=0)
return dx.cumsum()
def subtract_drift(traj, drift=None, inplace=False):
Return a copy of particle trajectories with the overall drift subtracted