Tutorial about nearest neighbor distances#

from pathlib import Path

%matplotlib inline

import matplotlib.pyplot as plt

import locan as lc
lc.show_versions(system=False, dependencies=False, verbose=False)
Locan:
   version: 0.20.0.dev41+g755b969

Python:
   version: 3.11.6

Load rapidSTORM data file#

Identify some data in the test_data directory and provide a path using pathlib.Path

path = lc.ROOT_DIR / 'tests/test_data/rapidSTORM_dstorm_data.txt'
print(path, '\n')

dat = lc.load_rapidSTORM_file(path=path, nrows=1000)
/home/docs/checkouts/readthedocs.org/user_builds/locan/envs/latest/lib/python3.11/site-packages/locan/tests/test_data/rapidSTORM_dstorm_data.txt 
Jupyter environment detected. Enabling Open3D WebVisualizer.
[Open3D INFO] WebRTC GUI backend enabled.
[Open3D INFO] WebRTCWindowSystem: HTTP handshake server disabled.

Print information about the data:

print(dat.data.head(), '\n')
print('Summary:')
dat.print_summary()
print('Properties:')
print(dat.properties)
   position_x  position_y  frame  intensity  chi_square  local_background
0     9657.40     24533.5      0   33290.10   1192250.0        767.732971
1    16754.90     18770.0      0   21275.40   2106810.0        875.460999
2    14457.60     18582.6      0   20748.70    526031.0        703.369995
3     6820.58     16662.8      0    8531.77   3179190.0        852.789001
4    19183.20     22907.2      0   14139.60    448631.0        662.770020 

Summary:
identifier: "1"
comment: ""
source: EXPERIMENT
state: RAW
element_count: 14
frame_count: 1
file {
  type: RAPIDSTORM
  path: "/home/docs/checkouts/readthedocs.org/user_builds/locan/envs/latest/lib/python3.11/site-packages/locan/tests/test_data/rapidSTORM_dstorm_data.txt"
}
creation_time {
  2024-03-14T11:07:49.827896Z
}

Properties:
{'localization_count': 14, 'position_x': 15873.847142857145, 'uncertainty_x': 2361.4490857013648, 'position_y': 17403.909285714286, 'uncertainty_y': 1803.9975262697349, 'intensity': 183987.66999999998, 'local_background': 675.0614, 'frame': 0, 'region_measure_bb': 730882123.3259, 'localization_density_bb': 1.915493559521281e-08, 'subregion_measure_bb': 108337.2}

Visualization#

lc.render_2d(dat, bin_size=1000, rescale=(0,100));
../../_images/1b25b0627dbee2c855a501441d331a6fa16f0e72ddb7af0634b9f21e2343144a.png

Analyze nearest neighbor distances#

The k-nearest neighbor distances can be analyzed for all localizations or a random subset.

nn = lc.NearestNeighborDistances()
nn.compute(dat)
nn.results.head()
nn_distance nn_index
0 4219.766477 12
1 2304.930812 2
2 2291.302095 9
3 3974.974397 10
4 4797.193422 1

The histogram shows the experimental distribution and for comparision the expectation for a spatial distribution of complete spatial randomness given the experimental localization density (per default relative to the minimum bounding box region).

nn.hist();
../../_images/a779949bbb2ed2096c93efd335306c79a0809e5ac72dbf2b68976cb6bd846366.png

The localization_density relative to the minimum bounding box region is retweated by the localization_density attribute.

nn.localization_density
1.915493559521281e-08

Meta data for the analysis procedure#

nn.meta
identifier: "1"
method {
  name: "NearestNeighborDistances"
  parameter: "{\'k\': 1}"
}
creation_time {
  seconds: 1710414470
  nanos: 710585000
}