Tutorial about rendering LocData

from abc import ABCMeta
from pathlib import Path

%matplotlib inline
# %matplotlib widget

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import locan as lc
lc.show_versions(system=False, dependencies=False, verbose=False)
Locan:
   version: 0.24.0.dev1+g34ee40bb9

Python:
   version: 3.11.15

Synthetic data

Localizations are simulated that are distributed according to a Neyman-Scott distribution (blobs).

rng = np.random.default_rng(seed=1)
locdata = lc.simulate_Thomas(
    parent_intensity=1e-5,
    region=((0, 1000), (0, 1000)),
    cluster_mu=100,
    cluster_std=10,
    seed=rng,
)

locdata.print_summary()
Jupyter environment detected. Enabling Open3D WebVisualizer.
[Open3D INFO] WebRTC GUI backend enabled.
[Open3D INFO] WebRTCWindowSystem: HTTP handshake server disabled.
identifier: "1"
comment: ""
source: SIMULATION
state: RAW
element_count: 705
frame_count: 0
creation_time {
  2026-11-30T11:45:17.387204Z
}

Since localization data is kept as a pandas dataframe standard plotting routines from pandas or matplotlip can be used.

fig, ax = plt.subplots(nrows=1, ncols=1)
locdata.data.plot.scatter(
    x="position_x", y="position_y", ax=ax, color="Blue", label="locdata"
)
plt.show()
../../_images/9fdba53c3cfbd7a72855fe12a58d6c4bd2ee7a225db9296eb13e4ff50c052460.png

Render by simple 2D binning

A method for simply binning localization data in 2D pixels is provided.

img, bins, label = lc.histogram(locdata, bin_size=10)

The intensity values of the binned locdata are distributed as:

plt.hist(img.ravel(), bins=256, range=(1, 50), fc="k", ec="k");
../../_images/04927a14401c4717ba07ed41c4a9909776addbd62af2a9fd1fe0405b9321dfca.png

The intensity values can be rescaled in many ways. There are normalization classes and a convenience function in the locan.render.transformation module with predefined transformations as listed in locan.Trafo:

list(lc.Trafo)
[<Trafo.NONE: 0>,
 <Trafo.STANDARDIZE: 1>,
 <Trafo.STANDARDIZE_UINT8: 2>,
 <Trafo.ZERO: 3>,
 <Trafo.ZERO_UINT8: 4>,
 <Trafo.EQUALIZE: 5>,
 <Trafo.EQUALIZE_UINT8: 6>,
 <Trafo.EQUALIZE_ALL: 7>,
 <Trafo.EQUALIZE_ALL_UINT8: 8>,
 <Trafo.EQUALIZE_0P3: 9>,
 <Trafo.EQUALIZE_0P3_UINT8: 10>,
 <Trafo.EQUALIZE_0P3_ALL: 11>,
 <Trafo.EQUALIZE_0P3_ALL_UINT8: 12>]
img_new = lc.adjust_contrast(img, rescale=lc.Trafo.STANDARDIZE)
epsilon = np.finfo(float).resolution
plt.hist(img_new.ravel(), bins=256, range=(epsilon, 1), fc="k", ec="k");
../../_images/7715051f4209f3080195406bf2b242376aa11e210e4613cb6e08cbcb08b6ac5b.png
norm = lc.HistogramEqualization(power=1, mask=img > 0)
img_new = norm(img)
plt.hist(img_new.ravel(), bins=256, range=(epsilon, 1), fc="k", ec="k");
../../_images/8b816800cb4dbdae7275029a7f67261ac8bb0f59406fcf03de86eb18364aa213.png

The render_2d method can directly provide a new figure as output.

lc.render_2d(locdata, bin_size=10);
../../_images/5f1b35ef33972a84ca4f07c295bc3270ff30ec04f8cd69b9a688abe1f38649cd.png

Or it can be used within the matplotlib environment.

fig, ax = plt.subplots(nrows=1, ncols=1)
lc.render_2d(locdata, ax=ax, bin_size=10, cmap="viridis")
plt.show()
../../_images/44052010782ed23b6d4d479dfe3a81096782cdb9df2fa386ca0e5e9102617247.png

Intensity is per default scaled to the min and max intensity values but can be rescaled by applying any norm function as described for matplotlib.imshow:

norm = plt.Normalize(vmax=10)
lc.render_2d(locdata, bin_size=10, norm=norm);
../../_images/9177d9e11449240d84aee5a0b07c49e61c244a8926627f063996ce000d80e13e.png

Intensity can also be rescaled by normalization functions as defined in locan.Trafo. Histogram equlization yields this image:

lc.render_2d(locdata, bin_size=10, rescale=lc.Trafo.EQUALIZE);
../../_images/751792e729a359ac30d113fc9083f40305468ed37777a259f49a708f592bde0a.png

Histogram equlization with a power-intensification of p=0.3 yields this image:

lc.render_2d(locdata, bin_size=10, rescale=lc.Trafo.EQUALIZE_0P3);
../../_images/97e8340c73138a09db0e2f3a938e7da63fa3a9ff02026346686fbf92f164480e.png

Any callable normalization object can be passed into the rescale kwarg:

norm = lc.HistogramEqualization(power=0.3)
lc.render_2d(locdata, bin_size=10, rescale=norm);
../../_images/dc5ae3aec9a7ccaba6d75799cf3d2d90276ef9c8babe39a40ec222659b8b13d6.png

The image size is set automatically to the min and max coordinates but can be set to (0, max) or an arbitrary range.

lc.render_2d(locdata, bin_size=10, bin_range="zero");
../../_images/b61c40bb5ba056bfaebfc1acb83af8b47a6b4946d78c87d6fc54e277a279381f.png
lc.render_2d(locdata, bin_size=10, bin_range=((0, 300), (200, 500)));
../../_images/ad908e6bbf4d3b1bf26bb0cb8a15c624d129a53c3be187b2f95efae762b9bd10.png

Use different libraries for rendering

Rendering can also be carried out with a different render engine. Choose one of the following (MPL is the standard matplotlib):

list(lc.RenderEngine)
[<RenderEngine.MPL: 'matplotlib'>,
 <RenderEngine.MPL_SCATTER_DENSITY: 'mpl_scatter_density'>,
 <RenderEngine.NAPARI: 'napari'>]

napari

As external viewer you can use napari.

or alternatively:

Choose colormaps

Colormaps can be chosen in matplotlib, napari and other visualization tools.

Colormaps are identified through specific class instances or by a name.

locan.Colormap serves as adapter class for the various visualization tools.

A Colormap instance can be created with the get_colormap function:

colormap = lc.get_colormap("viridis")
colormap.name
'viridis'
colormap.matplotlib
viridis
viridis colormap
under
bad
over

A mapping of names on Colormap instances is provided and can be extended by users. If a colormap name is provided to any rendering function, first the colormap_registry is searched, then the maplotlib registry and finally napari colormap names.

lc.colormap_registry
{'cet_fire': <locan.visualize.colormap.Colormap at 0x718f83864cd0>,
 'cet_fire_r': <locan.visualize.colormap.Colormap at 0x718f83890a10>,
 'cet_gray': <locan.visualize.colormap.Colormap at 0x718f838909d0>,
 'cet_gray_r': <locan.visualize.colormap.Colormap at 0x718f838b8dd0>,
 'cet_coolwarm': <locan.visualize.colormap.Colormap at 0x718f838b8d90>,
 'cet_glasbey_dark': <locan.visualize.colormap.Colormap at 0x718f838b8e10>,
 'viridis': <locan.visualize.colormap.Colormap at 0x718f838b8f10>,
 'viridis_r': <locan.visualize.colormap.Colormap at 0x718f838b8fd0>,
 'gray': <locan.visualize.colormap.Colormap at 0x718f838b9090>,
 'gray_r': <locan.visualize.colormap.Colormap at 0x718f838b9150>,
 'turbo': <locan.visualize.colormap.Colormap at 0x718f838b9210>,
 'coolwarm': <locan.visualize.colormap.Colormap at 0x718f838b92d0>,
 'tab20': <locan.visualize.colormap.Colormap at 0x718f838b9390>}

Default colormaps are defined through locan configuration settings:

lc.config.colormaps.model_dump()
{'continuous': 'cet_fire',
 'continuous_reverse': 'cet_fire_r',
 'continuous_gray': 'cet_gray',
 'continuous_gray_reverse': 'cet_gray_r',
 'diverging': 'cet_coolwarm',
 'categorical': 'cet_glasbey_dark',
 'turbo': 'turbo'}
lc.render_2d(locdata, bin_size=10, cmap=lc.config.colormaps.continuous_gray_reverse);
../../_images/11172cc14ad899073cdf6d092a337bc68af3a7cfc1b2da389f9b9657aa262ab6.png