{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial about tracking LocData objects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tracking refers to link localizations that are close in space over multiple frames and collect those localizations in individual tracks.\n", "We here make use of the trackpy package through wrapper functions to deal with LocData objects." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "\n", "import locan as lc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lc.show_versions(system=False, dependencies=False, verbose=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Synthetic data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A random dataset is created." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dat = lc.simulate_tracks(n_walks=5, n_steps=100, ranges=((0,1000),(0,1000)),\n", " diffusion_constant=1, seed=1)\n", "\n", "dat.print_meta()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dat.data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=1)\n", "dat.data.plot.scatter(x='position_x', y='position_y', ax=ax, color='Blue', label='locdata')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Track locdata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The track function collects tracks in a new locdata object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tracks, track_numbers = lc.track(dat, search_range=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tracks.print_summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tracks.data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tracks.references[0].data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=1)\n", "dat.data.plot.scatter(x='position_x', y='position_y', ax=ax, color='Blue', label='locdata')\n", "tracks.references[0].data.plot.scatter(x='position_x', y='position_y', ax=ax, color='Red', label='track 0')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=1)\n", "dat.data.plot.scatter(x='position_x', y='position_y', ax=ax, color='Blue', label='locdata')\n", "tracks.data.plot.scatter(x='position_x', y='position_y', ax=ax, color='Red', label='tracks')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To show individual tracks make use of the reference attribute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=1)\n", "\n", "jet= plt.get_cmap('jet')\n", "colors = iter(jet(np.linspace(0, 1, len(tracks))))\n", "\n", "for ref in tracks.references:\n", " c=next(colors)\n", " ref.data.plot.scatter(x='position_x', y='position_y', ax=ax, color=(c,), label=ref.meta.identifier)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively to a new locdata object, a pandas DataFrame can be generated using the link_locdata method." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "links = lc.link_locdata(dat, search_range=10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "links.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the following to add particle column to original locdata dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dat.data.loc[links.index,'track']=links" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dat.data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Track using trackpy with locdata.data as input" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a detailed tracking analysis you might want to use [trackpy](https://soft-matter.github.io/trackpy) functions with the pandas DataFrame carrying localization data as input. The following examples will igve a short illustration of using trackpy functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import trackpy as tp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = tp.link_df(dat.data, search_range=100, memory=1, pos_columns=['position_x', 'position_y'], t_column='frame')\n", "\n", "t.head(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "tp.plot_traj(t, pos_columns=['position_x', 'position_y'], t_column='frame');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## filter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1 = tp.filter_stubs(t, 10).reset_index(drop=True)\n", "len(t1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "tp.plot_traj(t1, pos_columns=['position_x', 'position_y']);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## drift" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = tp.compute_drift(t1, pos_columns=['position_x', 'position_y'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mean square displacement (msd)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "em = tp.emsd(t1,0.1, 100, pos_columns=['position_x', 'position_y']) # microns per pixel = 100/285., frames per second = 24" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "ax.plot(em.index, em, 'o')\n", "#ax.set_xscale('log')\n", "#ax.set_yscale('log')\n", "ax.set(ylabel=r'$\\langle \\Delta r^2 \\rangle$ [$\\mu$m$^2$]',\n", " xlabel='lag time $t$')\n", "#ax.set(ylim=(1e-2, 10));" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }