{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial about analyzing localization properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "%matplotlib inline\n", "\n", "import numpy as np\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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A path in which test data can be found:\n", "TEST_DIR: Path = Path.cwd().parents[2] / \"tests\"\n", "TEST_DIR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load rapidSTORM data file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Identify some data in the test_data directory and provide a path using `pathlib.Path` (returned by `lc.ROOT_DIR`)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = TEST_DIR / 'test_data/rapidSTORM_dstorm_data.txt'\n", "print(path, '\\n')\n", "\n", "dat = lc.load_rapidSTORM_file(path=path, nrows=1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print information about the data: " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(dat.data.head(), '\\n')\n", "print('Summary:')\n", "dat.print_summary()\n", "print('Properties:')\n", "print(dat.properties)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lc.render_2d(dat, bin_size=1000, rescale=(0,100));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analyze a localization property" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a look at a certain localization property in locdata. \n", "\n", "The analysis class `LocalizationProperty` provides a dataframe with the property as function of another property (index), and a plot or histogram of this property." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop = lc.LocalizationProperty(loc_property='intensity', index='frame')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.compute(dat)\n", "print(lprop.results.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The plot shows results smoothed by a running average according to the specified window." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.plot(window=100);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The histogram shows the probability density function of results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.hist(fit=False);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Per default the distribution is fitted to an exponential decay." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.hist();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fit results (as derived using the lmfit library) are provided in the distribution_statistics attribute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.distribution_statistics.parameter_dict()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.results.min()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fitting different distribution models" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Per default the 'with_constraints' flag is True to apply standard fit constraints. This can be set to false and other parameters can be passed to the fit function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.fit_distributions(with_constraints=False, floc=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.distribution_statistics.parameter_dict()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.hist(fit=True)\n", "print(lprop.distribution_statistics.parameter_dict())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Showing correlations between two properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By setting the index to another localization property correlations can be shown." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop = lc.LocalizationProperty(loc_property='intensity', index='local_background').compute(dat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop.plot(marker='o', linestyle=\"\", alpha=0.1);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Correlation coefficients can be investigated in more detail using the LocalizationPropertyCorrelation class that is just a visualization of `pandas.DataFrame.corr()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lpcorr = lc.LocalizationPropertyCorrelations(loc_properties=['intensity', 'local_background']).compute(dat)\n", "lpcorr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lpcorr.plot();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2-dimensional distribution of localization properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to investigate a certain localization property in 2D you can just print the image with a color code representing the mean value of the chosen localization property in each bin." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lc.render_2d_mpl(dat, other_property='local_background', bin_size=500);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Otherwise use a specific class to analyse localization properties in 2d. Per default a bimodal normal distribution is fitted. This can e.g. help to check on even illumination during the recording." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop2d = lc.LocalizationProperty2d(loc_properties=None, other_property='local_background', bin_size=500).compute(dat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop2d.plot_deviation_from_mean();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop2d.plot(colors=\"r\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lprop2d.report()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.6" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }