11import numpy as np
22import matplotlib .pyplot as plt
33
4- from scipy .ndimage import gaussian_filter
5-
64from matplotlib .cm import ScalarMappable
75from matplotlib .ticker import AutoLocator
86from matplotlib .colorbar import ColorbarBase
97from matplotlib .colors import ListedColormap , Normalize , NoNorm
108
9+ from scipy .ndimage import gaussian_filter
10+
1111
1212def chromosome_viewer (chrom_lengths ,
1313 colors = None ,
@@ -17,7 +17,7 @@ def chromosome_viewer(chrom_lengths,
1717 Simple matplotlib-based visualization of individual chromosomes
1818
1919 Parameters
20- ----------
20+ ----------
2121 chrom_lengths : Cx1 int array
2222 List of the sizes of each chromosome
2323 colors : Nx4 float array or None
@@ -72,24 +72,31 @@ def rasterize(positions,
7272 box_size ,
7373 resolution = 100 ,
7474 length_unit = 50 ,
75- gaussian_width = 250 ):
75+ gaussian_width = 250 ,
76+ normalize = False ):
7677 """
7778 3D positions rasterizer for numerical microscopy analysis
7879
7980 Parameters
80- ----------
81+ ----------
8182 positions : Nx3 float array
82- List of X,Y,Z particle positions to be rasterize .
83+ List of X,Y,Z particle positions to be rasterized (in model units) .
8384 Assumes particle coordinates are wrapped in PBCs within the range [-box_size/2,+box_size/2]
8485 box_size : float
85- Linear dimension of periodic box
86+ Linear dimension of periodic box (in model units)
8687 resolution : float
87- Linear dimension of output voxels (in nm)
88+ Linear dimension of output voxels (in nm)
8889 length_unit : float
8990 Model unit of length (in nm)
9091 gaussian_width : float
91- Width of Gaussian point-spread function to be used.
92+ Width of Gaussian point-spread function to be used (in nm) .
9293 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
93100 """
94101
95102 n_voxels = int (box_size / (resolution / length_unit ))
@@ -105,36 +112,41 @@ def rasterize(positions,
105112 raster = bincounts .reshape ((n_voxels ,n_voxels ,n_voxels ))
106113
107114 if gaussian_width > 0 :
108- smoothed_raster = gaussian_filter (raster / raster .max (), sigma = gaussian_width / resolution , mode = 'wrap' )
109-
110- return smoothed_raster
115+ raster = gaussian_filter (raster / raster .max (), sigma = gaussian_width / resolution , mode = 'wrap' )
111116
112- return raster / raster .max ()
117+ if normalize :
118+ raster /= raster .max ()
119+
120+ return raster
113121
114122
115- def raster_map_2D (raster ,
116- cmap ,
117- vmin = None ,
118- vmax = None ,
119- mode = 'max' ,
120- axis = 2 ):
123+ def voxels_to_pixels_RGB (raster ,
124+ cmap ,
125+ vmin = None ,
126+ vmax = None ,
127+ mode = 'max' ,
128+ axis = 2 ):
121129 """
122- RGB mapping of 2D raster projection
130+ Project voxels onto 2D pixels and map values to RGB
123131
124132 Parameters
125- ----------
126- raster : Mx3 float array
127- 3D normalized raster to visualize.
133+ ----------
134+ raster : MxMxM float array
135+ 3D voxel array to visualize
128136 cmap : matplotlib colormap object or str
129137 Colormap to be used
130138 vmin, vmax : float or None
131- Color dynamic range.
139+ RGB dynamic range.
132140 If set to None, use full data range
133141 mode : str
134142 Projection mode to be used.
135143 Set to either 'max' for maximum intensity or 'sum' for summed intensity projection
136144 axis : int
137145 Index of projection axis
146+
147+ Returns
148+ -------
149+ MxMx3 image array of RGB pixels
138150 """
139151
140152 if mode == 'max' :
@@ -144,12 +156,12 @@ def raster_map_2D(raster,
144156 else :
145157 raise RuntimeError (f"Unsupported projection mode { mode } " )
146158
147- vmin = vmin if vmin else raster_2D . min ()
148- vmax = vmax if vmax else raster_2D . max ()
159+ vmin = raster_2D . min () if vmin is None else vmin
160+ vmax = raster_2D . max () if vmax is None else vmax
149161
150162 norm = Normalize (vmin = vmin , vmax = vmax )
151163 cm = ScalarMappable (norm = norm , cmap = cmap )
152164
153165 im = cm .to_rgba (raster_2D )
154166
155- return im [:, : , :3 ]
167+ return im [... , :3 ]
0 commit comments