Neighbourhood per localization

Analysis › Process · version 2

Per localization, what is around it: the neighbours within a radius,

What it does

Writes one number per localization that describes what is around it, as a new attribute of the table. The attribute can then be filtered on (keep the localizations in dense places, drop the isolated ones), coloured by in the Render tab, or measured like any other. It segments nothing: finding clusters is Find clusters's job.

Three quantities:

Two layers say what is counted: around, the layer whose localizations get the value, and neighbours from, the layer whose localizations are counted. Each is read as that layer shows it, with its filter and with or without merged blinks – so what is counted is decided where it always is, in the layer. The same layer twice is the ordinary local density. Two layers – two colour channels – give a cross-density: how many of channel B lie around each localization of channel A, which is a measure of colocalization.

Local L and relative density. A count of 12 means little until it is compared with what the mean density would put in the circle. Relative to the mean density divides by that: 1 in a uniform field, 5 where the localizations are five times as dense as on average. Local L(r) is the radius a circle would need to hold that many neighbours at the mean density (Getis and Franklin 1987): in a uniform field, more in a cluster – the per-localization version of Spatial statistics' L(r), whose mean over the localizations it is. Both are corrected at the edge of the window, so a localization at the edge of the cell is not called sparse for having half a circle of data around it.

DBSCAN thresholds the same quantity. A localization is a core point of DBSCAN when it has at least neighbours within radius – the count here, thresholded. So a filter on the count (or on local L, which is the count on a scale that does not depend on the density of the whole data set) shows which localizations DBSCAN would call core before it joins them into clusters: Getis and Franklin's clustering – threshold local L and join what is dense – is DBSCAN, and is not a separate algorithm here.

Use the count when the structures have a size you know (a radius about the size of a cluster); use the Voronoi density when they do not, or as the first step of a Voronoi clustering. Neither says anything reliable within a radius of the edge of the field, where part of the circle or the cell is outside the data.

How it works

1. The two tables. The localizations of the around layer that pass its filter, and those of the neighbours from layer that pass its own. On a layer that merges blinks these are the blinks. Each file of the session is done on its own: a localization of one file is never another file's neighbour.

2. The neighbours. For every localization around, the neighbours within the radius, and in 3D also within the z half-height above or below it. A localization is never its own neighbour, so one with nobody near has 0.

3. The value. Their number; or, with Gaussian weight, the sum of over them, where is the distance and the radius, so a neighbour close by counts nearly 1 and one at counts 0.14; or the chosen statistic of the attribute over them, the localization's own value included; or, for the Voronoi density, one over the area of its cell.

4. The attribute. The value goes onto the localizations of the around layer that passed its filter, and every other localization gets NaN (no value), so a filter on the new attribute also drops them. A blink's value goes onto each of its localizations.

The local density brings out the structures: the ring and the lines have tens of neighbours within 50 nm, the scattered molecules a few (left). The Voronoi density (right) sees only the nearest neighbours, so a lone molecule's own repeated localizations make its cells as small as the ring's: merge blinks before using it.

A cross-density: each localization of the ring (dye 1) coloured by the dye-2 localizations within 100 nm. It is high only where the two lines cross the ring, where the two labels are close.

In detail

Counting. For an around localization at the count is the number of neighbours with

(the second only in 3D, with the z half-height), . In a uniform field of localizations per nm² the count is Poisson distributed with mean , and in a cylinder. With Gaussian weight each neighbour contributes

out to laterally and in z, as in SMAP; the mean in a uniform field is (times for the cut).

In a uniform field the counts follow the Poisson distribution of mean pi r^2 rho (line), here 20 neighbours at r = 80 nm.

Expressed. With localizations of the neighbours layer in the file's window (area ; others for a localization that is itself among them), CSR expects neighbours around localization , where is the area of its circle inside the window – away from the edge, half that at a straight edge. The relative density is over that, and the local L is

With Gaussian weight is the integral of the weight over the part of its 3-sigma circle inside the window, and local L is times the square root of the relative density. The window is Spatial statistics': the drawn ROI, or, without one, the occupied area (the localizations of both layers binned at 200 nm, closed) or the bounding box, per file (window). is the whole circle's for a localization whose circle lies inside the window, and otherwise integrated over the window's pixels – exactly across the pixel rows, by Gauss-Legendre along x. Expressed counts are in x and y only; in 3D is not used. The mean of local L is a little below in a uniform field, since a square root of a count scatters below the square root of its mean.

Clusters in a uniform field: local L at r = 60 nm is about 60 nm for the scattered localizations – at the window's edge, where half the circle is outside, as well as inside – and several times that in the clusters.

The statistic is over the neighbours with a finite value of the attribute, the localization itself among them (this is a smoothing filter: a localization alone in its circle keeps its own value). A quantile is linear between the order statistics, as numpy's: with values sorted, the value at position .

Voronoi density. The cells come from the Delaunay triangulation (each cell's corners are the centres of the circles through the triangles that meet at its localization). The density is per µm², with the cell's area and the number of localizations at exactly that position (they share one cell). A cell at the edge of the field is open, with no area, and its localization gets NaN. In a uniform field the mean area of a cell is , so the harmonic mean of the densities is ; a single density scatters widely about it (a cell's area varies by about 50 %), which is why it is mostly used through a threshold or after a smoothing.

For millions of localizations the field is cut into tiles of about 50,000, each triangulated with a margin around it. A cell is kept only when every circle through its triangles is empty of the field's other localizations, which proves the triangles are those of the whole field, and is otherwise done again with a wider margin. The answer is the same as from one triangulation of everything.

Speed. The neighbours are found by sorting the localizations into square cells one radius wide, so that each localization tests only the nine cells around it, in compiled code on every core. 5 million localizations over 40 µm at radius 50 nm take about 3 s (SMAP's sweep along x alone tests the whole strip wide and as tall as the field: 23 s for the same data); Gaussian weight about 10 s; the Voronoi density about a minute. A run expected to take more than 20 s asks first. The panel shows the percentage done, and Stop ends the run and leaves the table as it was.

Merged blinks. The attribute is kept as a recipe with the rule mean: when blinks are merged again, a blink takes the mean of its localizations' values (ignoring NaN), which is the blink's own value when it was computed on blinks.

Parameters

settingdefaultwhat it does
around
around
layer 1The layer whose localizations get the value.

The layer whose localizations get the value. Set its filter first: only the localizations it keeps get a value; the rest get NaN.

neighbours from
neighbours_from
layer 1The layer whose localizations are counted; another layer gives a cross-density.

The same layer as around for the local density. Another layer, set to show another channel, gives the cross-density.

quantity
quantity
neighbours within rWhat is written per localization. Choices: neighbours within r; statistic of an attribute; Voronoi density
radius
radius_nm
50 nmR: the circle's radius, or the Gaussian's sigma.

About the size of the structure to tell apart from its surroundings: a cluster's radius, or a few times the localization precision. Too small and most localizations have 0 or 1 neighbours, too large and every cluster blurs into its surroundings. 50 nm suits clusters of proteins; the counts grow with , so compare counts only at the same radius.

at least 0.1 nm
in 3D
use_z
onA cylinder in x, y and z when both tables have z_nm.

Off: the neighbours are counted in a circle whatever their z, as if the data were 2D. Only applies when both layers have z_nm.

z half-height
z_half_nm
100 nmHow far in z a neighbour may be, or the Gaussian's sigma in z.

Two to three times the axial precision, or the thickness of the structure; the default 100 nm suits astigmatic 3D data. SMAP's 12 nm laterally and 24 nm in z were too small for a default: at typical densities they leave most localizations with no neighbour.

at least 0.1 nm
Gaussian weight
gaussian
offWeight each neighbour by a Gaussian of its distance rather than counting a sharp circle.

Smoother than a sharp circle – a neighbour just inside and one just outside the radius no longer differ by a whole count – at about 9 times the cost, since it looks three radii out.

express as
express_as
countThe count, the radius that count needs at the mean density, or the count over the mean density's.

Local L to compare data sets of different densities on one scale, and for a threshold that means the same in each; relative for "how many times the average"; the plain count when the radius and density are fixed, or in 3D.

Choices: count; local L(r); relative to the mean density
window
window
the occupied areaWhere the mean density is taken, without a drawn ROI.

As in Spatial statistics: the occupied area for a cell, the bounding box for data that fill a rectangle. A drawn ROI is the window when there is one.

Choices: the occupied area; the bounding box
attribute
column
-The attribute whose statistic is taken.
statistic
statistic
medianOf the attribute over the neighbours.

Median is robust to the few outliers an attribute like photons has; std shows where a value varies, such as z across the edge of a structure.

Choices: mean; median; min; max; std; quantile
quantile
quantile
0.90.5 is the median. 0 to 1
new attribute
name
–Empty: a name from the settings.

Left empty, the name says what was computed: density_r50, density_gauss_r50, photons_median_r50, voronoi_density, with _from2 when the neighbours came from layer 2. Running again with the same name replaces the attribute; an attribute of the localizations' own (such as photons) is never overwritten.

Output

The new attribute on the localizations (local_l_r50, density_rel_r50 when expressed), and a figure: the around localizations coloured by the value, and its histogram. The result's line says how many localizations got a value and their median.

A count of 0 on most localizations means the radius is small for the density; a histogram with one broad peak and no tail is a uniform distribution with no clusters to find. Clustered data shows two populations: a peak near 0 (the localizations between clusters) and a broad one at the clusters' density.

Differences from SMAP

Based on SMAP's density_calculator, SpatialFilterLocs and the density of ClusterVoronoi (Ries 2020), in one plugin. The main changes:

References