{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial about Ripley's k function" ] }, { "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": "markdown", "metadata": {}, "source": [ "## Synthetic data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We simulate localization data that is homogeneously Poisson distributed (also described as complete spatial randomness, csr)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rng = np.random.default_rng(seed=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "locdata_csr = lc.simulate_Poisson(intensity=1e-3, region=((0,1000), (0,1000)), seed=rng)\n", "\n", "print('Data head:')\n", "print(locdata_csr.data.head(), '\\n')\n", "print('Summary:')\n", "locdata_csr.print_summary()\n", "print('Properties:')\n", "print(locdata_csr.properties)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also simulate data that follows a Neyman-Scott distribution (blobs): " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "locdata_blob = lc.simulate_Thomas(parent_intensity=1e-4, region=((0, 1000), (0, 1000)), cluster_mu=100, cluster_std=5, seed=rng)\n", "\n", "print('Data head:')\n", "print(locdata_blob.data.head(), '\\n')\n", "print('Summary:')\n", "locdata_blob.print_summary()\n", "print('Properties:')\n", "print(locdata_blob.properties)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scatter plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=2)\n", "locdata_csr.data.plot.scatter(x='position_x', y='position_y', ax=ax[0], color='Blue', s=1, alpha=0.1, label='locdata_csr')\n", "locdata_blob.data.plot.scatter(x='position_x', y='position_y', ax=ax[1], color='Blue', s=1, alpha=0.1, label='locdata_blobs')\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyze Ripley's h function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a look at the Ripley's h function from all localizations in locdata. \n", "\n", "The analysis class Ripley_h_function provides numerical results, and a plot of results versus radii." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_csr = lc.RipleysHFunction(radii=np.linspace(0, 200, 50))\n", "rhf_csr.compute(locdata_csr)\n", "rhf_csr.results.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_blob = lc.RipleysHFunction(radii=np.linspace(0, 200, 50))\n", "rhf_blob.compute(locdata_blob)\n", "rhf_blob.results.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The plot reflects the amount of clustering. For homogeneous distributed data it decreases towards negative values since edge effects are not taken into account." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_csr.plot()\n", "rhf_blob.plot();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The maximum of the computed H-function is provided by the attribute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_blob.Ripley_h_maximum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Estimate Ripley's h function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can speed up the computation of an estimated Ripley's k function by providing a subset of the original localizations as test points." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first take a random subset of the original localizations as test data. Here we provide 10 shuffeled data sets. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from locan.process.filter import random_subset\n", "subsets = [lc.random_subset(locdata_blob, n_points=5, seed=rng) for i in range(10)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We then compute the estimated Ripley's h function'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_estimate = lc.RipleysHFunction(radii=np.linspace(0, 200, 50)).compute(locdata_blob, other_locdata=subsets[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_estimate.plot();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do the same for all subsets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_estimates = [lc.RipleysHFunction(radii=np.linspace(0, 200, 50)).compute(locdata_blob, other_locdata=subset) for subset in subsets]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=1)\n", "for estimate in rhf_estimates:\n", " estimate.plot(ax=ax)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute Ripley's k, l and h function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can compute Ripley's k, l and h function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rkf_csr = lc.RipleysKFunction(radii=np.linspace(0, 200, 20)).compute(locdata_csr)\n", "rlf_csr = lc.RipleysLFunction(radii=np.linspace(0, 200, 20)).compute(locdata_csr)\n", "rhf_csr = lc.RipleysHFunction(radii=np.linspace(0, 200, 20)).compute(locdata_csr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))\n", "for estimate, ax in zip([rkf_csr, rlf_csr, rhf_csr], axes.ravel()):\n", " estimate.plot(marker='o', ax=ax)\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Estimate Ripley's h function for 3D data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dat_blob_3D = lc.simulate_Thomas(parent_intensity=1e-7, region=((0, 1000), (0, 1000), (0, 1000)), cluster_mu=100, cluster_std=5, seed=rng)\n", "dat_blob_3D.print_summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sub = lc.random_subset(dat_blob_3D, n_points=1000, seed=rng)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_3D = lc.RipleysHFunction(radii=np.linspace(0, 200, 100)).compute(dat_blob_3D, other_locdata=sub)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rhf_3D.plot();" ] } ], "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 }