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()
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");
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");
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");
The render_2d method can directly provide a new figure as output.
lc.render_2d(locdata, bin_size=10);
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()
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);
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);
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);
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);
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");
lc.render_2d(locdata, bin_size=10, bin_range=((0, 300), (200, 500)));
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
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);