Skip to content

Commit 5cc9dec

Browse files
authored
rasterutils
Implemented rasterization utilities
2 parents 4bf3646 + 2d29655 commit 5cc9dec

File tree

4 files changed

+297
-6
lines changed

4 files changed

+297
-6
lines changed

data/hoomd_snapshot.gsd

12.3 MB
Binary file not shown.

examples/rasterize.ipynb

Lines changed: 188 additions & 0 deletions
Large diffs are not rendered by default.

polykit/renderers/viewers.py

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
33

4+
from matplotlib.cm import ScalarMappable
45
from matplotlib.ticker import AutoLocator
56
from matplotlib.colorbar import ColorbarBase
6-
from matplotlib.colors import ListedColormap, NoNorm
7+
from matplotlib.colors import ListedColormap, Normalize, NoNorm
78

9+
from scipy.ndimage import gaussian_filter
810

9-
def chromosome_viewer(chrom_lengths,
11+
12+
def chromosome_viewer(chrom_lengths,
1013
colors=None,
1114
height=0.5,
1215
width=10.):
1316
"""
14-
Simple matplotlib-based visualization of individual chromosomes
17+
Simple matplotlib-based visualization of individual chromosomes
1518
1619
Parameters
17-
----------
20+
----------
1821
chrom_lengths : Cx1 int array
1922
List of the sizes of each chromosome
2023
colors : Nx4 float array or None
21-
Colors to be assigned to each monomer.
22-
If omitted, random colors will be attributed to each chromosome
24+
Colors to be assigned to each monomer.
25+
If omitted, random colors will be attributed to each chromosome
2326
height : float
2427
Per-chromosome height of the output figure (in inches)
2528
width : float
@@ -63,3 +66,102 @@ def chromosome_viewer(chrom_lengths,
6366
cb.update_ticks()
6467

6568
plt.show()
69+
70+
71+
def rasterize(positions,
72+
box_size,
73+
resolution=100,
74+
length_unit=50,
75+
gaussian_width=250,
76+
normalize=False):
77+
"""
78+
3D positions rasterizer for numerical microscopy analysis
79+
80+
Parameters
81+
----------
82+
positions : Nx3 float array
83+
List of X,Y,Z particle positions to be rasterized (in model units).
84+
Assumes particle coordinates are wrapped in PBCs within the range [-box_size/2,+box_size/2]
85+
box_size : float
86+
Linear dimension of periodic box (in model units)
87+
resolution : float
88+
Linear dimension of output voxels (in nm)
89+
length_unit : float
90+
Model unit of length (in nm)
91+
gaussian_width : float
92+
Width of Gaussian point-spread function to be used (in nm).
93+
If gaussian_width<=0, returns the raw (undiffracted) raster
94+
normalize : bool
95+
Set to True to scale maximum voxel intensity to 1
96+
97+
Returns
98+
-------
99+
MxMxM raster array of 3D voxels
100+
"""
101+
102+
n_voxels = int(box_size / (resolution/length_unit))
103+
bins = np.linspace(-box_size/2., box_size/2., num=n_voxels+1)
104+
105+
digitized_x = np.digitize(positions[:,0], bins) - 1
106+
digitized_y = np.digitize(positions[:,1], bins) - 1
107+
digitized_z = np.digitize(positions[:,2], bins) - 1
108+
109+
digitized = digitized_x + digitized_y*n_voxels + digitized_z*n_voxels**2
110+
bincounts = np.bincount(digitized, minlength=n_voxels**3)
111+
112+
raster = bincounts.reshape((n_voxels,n_voxels,n_voxels))
113+
114+
if gaussian_width > 0:
115+
raster = gaussian_filter(raster/raster.max(), sigma=gaussian_width/resolution, mode='wrap')
116+
117+
if normalize:
118+
raster /= raster.max()
119+
120+
return raster
121+
122+
123+
def voxels_to_pixels_RGB(raster,
124+
cmap,
125+
vmin=None,
126+
vmax=None,
127+
mode='max',
128+
axis=2):
129+
"""
130+
Project voxels onto 2D pixels and map values to RGB
131+
132+
Parameters
133+
----------
134+
raster : MxMxM float array
135+
3D voxel array to visualize
136+
cmap : matplotlib colormap object or str
137+
Colormap to be used
138+
vmin, vmax : float or None
139+
RGB dynamic range.
140+
If set to None, use full data range
141+
mode : str
142+
Projection mode to be used.
143+
Set to either 'max' for maximum intensity or 'sum' for summed intensity projection
144+
axis : int
145+
Index of projection axis
146+
147+
Returns
148+
-------
149+
MxMx3 image array of RGB pixels
150+
"""
151+
152+
if mode == 'max':
153+
raster_2D = raster.max(axis=axis)
154+
elif mode == 'sum':
155+
raster_2D = raster.sum(axis=axis)
156+
else:
157+
raise RuntimeError(f"Unsupported projection mode {mode}")
158+
159+
vmin = raster_2D.min() if vmin is None else vmin
160+
vmax = raster_2D.max() if vmax is None else vmax
161+
162+
norm = Normalize(vmin=vmin, vmax=vmax)
163+
cm = ScalarMappable(norm=norm, cmap=cmap)
164+
165+
im = cm.to_rgba(raster_2D)
166+
167+
return im[..., :3]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ numpy<1.24
22
scipy>=0.16
33
pandas>=0.19
44
matplotlib>=2.0
5+
gsd
56
rowan
67
cooler
78
joblib

0 commit comments

Comments
 (0)