{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial about how to use a standard Analysis class" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "%matplotlib inline\n", "\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 SMLM data file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Identify some data in the test_data directory and provide a path using pathlib.Path" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = TEST_DIR / 'test_data/npc_gp210.asdf'\n", "print(path, '\\n')\n", "dat = lc.load_locdata(path=path, file_type=lc.FileType.ASDF)" ] }, { "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": "markdown", "metadata": {}, "source": [ "For visualizing the data use one of the rendering methods." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lc.render_2d_mpl(dat, bin_size=10, rescale=lc.Trafo.EQUALIZE);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A simple analysis procedure: localization precision" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Instantiation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an instance of the analysis class. By doing this you set all parameters in the parameter attribute. To start the actual computation you have to call instance.compute()." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp = lc.LocalizationPrecision(radius=50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each analysis class provides some attributes and methods for the most common interactions with the computed results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "attributes = [x for x in dir(lp) if not x.startswith('_')]\n", "attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The results attribute" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A standard analysis class has an attribute *results* to hold the most fundamental results as number, numpy array or pandas series or dataframe. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.compute(dat)\n", "print('type of lpf.results: ', type(lp.results), '\\n')\n", "print(lp.results.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Results can be saved by specifying a path" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "from pathlib import Path\n", "temp_directory = Path('.') / 'temp'\n", "temp_directory.mkdir(parents=True, exist_ok=True)\n", "\n", "path = temp_directory / 'results.txt'\n", "path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and saving the data using numpy or pandas routines" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.results.to_csv(path, sep='\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Delete the file and empty directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path.unlink()\n", "temp_directory.rmdir()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A simple standardized plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most likely the results should be inspected by looking at a typical plot.\n", "In this case the plot shows results smoothed by a running average according to the specified window." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.plot(window=10);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more advanced plotting schemes use the matplotlip framework." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8,3))\n", "lp.plot(ax=ax[0], window=10, loc_property='position_delta_x')\n", "lp.plot(ax=ax[1], window=10, loc_property='position_delta_y')\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A simple standardized histogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quite often the results are best presented as a histogram. The histogram for the distances per default includes a fit to a distribution expected for normal distributed localizations. Sigma is the localization precision.\n", "\n", "The histogram per default provides automatic bins and is normalized to show a probability density function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.hist(loc_property='position_delta_x');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively the position deltas can be histogrammed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=2)\n", "lp.hist(ax=ax[0], loc_property='position_delta_x')\n", "lp.hist(ax=ax[1], loc_property='position_delta_y')\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Secondary results " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Secondary results are e.g. fit parameter derived from analyzing the distribution of *results* values. Secondary results are different for each analysis routine. They are included as additional attributes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Localization precision can e.g. be derived from fitting the position distances to an appropriate distribution and estimating the sigma parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.distribution_statistics.parameter_dict()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('position_distance_sigma: ', lp.distribution_statistics.parameter_dict()['position_distance_sigma'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Metadata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each analysis class is supplied with meta data. The main purpose is to (i) capture methods and parameters that were supplied in each instantiation and (ii) provide information on the dataset on which the particular analysis was carried out. Metadata is structured using protocol buffers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.meta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add some user-defined key-value pairs:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.meta.map['some key'] = 'some value'\n", "lp.meta.map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Metadata can be used to rerun the analysis with the same parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.meta.method.name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lp.meta.method.parameter" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "import ast\n", "import locan.analysis\n", "params = ast.literal_eval(lp.meta.method.parameter)\n", "print(params)\n", "lp_2 = getattr(locan.analysis, lp.meta.method.name)(**params)\n", "lp_2.compute(dat)\n", "lp_2.results.head()" ] } ], "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 }