{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial about transforming LocData" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Locan provides methods for transforming localization data sets into new LocData objects." ] }, { "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 pandas as pd\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.mplot3d import Axes3D\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": [ "## Spatially randomize a structured set of localizations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assume that localizations are somehow structured throughout a region. Often it is helpful to compare analysis results to a similar dataset in which localizations are homogeneously Poisson distributed. A LocData object with this kind of data can be provided by the randomize function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rng = np.random.default_rng(seed=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "locdata = lc.simulate_Thomas(parent_intensity=1e-5, region=((0, 1000), (0, 1000)), cluster_mu=100, cluster_std=10, seed=rng)\n", "locdata.print_summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "locdata_random = lc.randomize(locdata, hull_region='bb', seed=rng)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=2)\n", "locdata.data.plot.scatter(x='position_x', y='position_y', ax=ax[0], color='Blue', label='locdata')\n", "locdata_random.data.plot.scatter(x='position_x', y='position_y', ax=ax[1], color='Blue', label='locdata')\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Area of bounding box for structured data: {:.0f}'.format(locdata.properties['region_measure_bb']))\n", "print('Area of bounding box for randomized data: {:.0f}'.format(locdata_random.properties['region_measure_bb']))\n", "print('Ratio: {:.4f}'.format(locdata_random.properties['region_measure_bb'] / locdata.properties['region_measure_bb']))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Regions other from bounding box can be specified as RoiRegion instance." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "region = lc.ConvexHull(locdata.coordinates).region\n", "locdata_random = lc.randomize(locdata, hull_region=region)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=2)\n", "locdata.data.plot.scatter(x='position_x', y='position_y', ax=ax[0], color='Blue', label='locdata')\n", "locdata_random.data.plot.scatter(x='position_x', y='position_y', ax=ax[1], color='Blue', label='locdata')\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Area of bounding box for structured data: {:.0f}'.format(locdata.properties['region_measure_bb']))\n", "print('Area of bounding box for randomized data: {:.0f}'.format(locdata_random.properties['region_measure_bb']))\n", "print('Ratio: {:.4f}'.format(locdata_random.properties['region_measure_bb'] / locdata.properties['region_measure_bb']))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Apply an affine transformation to localization coordinates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A wrapper function provides affine transformations based on either numpy or open3d methods." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix = ((-1, 0), (0, -1))\n", "offset = (10, 10)\n", "pre_translation = (100, 100)\n", "\n", "locdata_transformed = lc.transform_affine(locdata, matrix, offset, pre_translation, method='numpy')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "locdata.data.plot.scatter(x='position_x', y='position_y',color='Blue', label='locdata', ax=ax)\n", "locdata_transformed.data.plot.scatter(x='position_x', y='position_y', color='Red', label='locdata', ax=ax);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Apply a BunwarpJ transformation to localization coordinates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often a transformation matrix was computed using ImageJ. The `bunwarp` function allows applying a transformation from the raw matrix of the ImageJ/Fiji plugin BunwarpJ. Here we show a very small region with a single fluorescent bead that is recorded on a red and a green dSTORM channel." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix_path = TEST_DIR / 'test_data/transform/BunwarpJ_transformation_raw_green.txt'\n", "locdata_green = lc.load_asdf_file(path=TEST_DIR /\n", " 'test_data/transform/rapidSTORM_beads_green.asdf')\n", "locdata_red = lc.load_asdf_file(path=TEST_DIR /\n", " 'test_data/transform/rapidSTORM_beads_red.asdf')\n", "\n", "locdata_green_transformed = lc.bunwarp(locdata=locdata_green, matrix_path=matrix_path, pixel_size=(10, 10), flip=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "locdata_red.data.plot.scatter(x='position_x', y='position_y',color='Red', label='locdata_red', alpha=0.5, ax=ax)\n", "locdata_green_transformed.data.plot.scatter(x='position_x', y='position_y', color='Green', label='locdata_green_transformed', alpha=0.5, ax=ax)\n", "locdata_green.data.plot.scatter(x='position_x', y='position_y',color='Blue', label='locdata_green', alpha=0.5, ax=ax);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(10, 8))\n", "lc. render_2d_rgb_mpl([locdata_red, locdata_green_transformed, locdata_green], bin_size=5, bin_range=((200, 800), (700, 1400)), rescale=lc.Trafo.EQUALIZE_0P3, ax=ax);" ] } ], "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 }