Skip to content

voronoi

!!!

Short Description

sm.pl.voronoi: The function allows users to generate a voronoi diagram and color it based on any categorical column. Please note, voronoi diagrams are best fitted for small regions with upto 5000 cells. Any larger, the voronoi plots are uninterpretable and takes a long time to generate.

Function

voronoi(adata, color_by=None, colors=None, x_coordinate='X_centroid', y_coordinate='Y_centroid', imageid='imageid', subset=None, x_lim=None, y_lim=None, flip_y=True, voronoi_edge_color='black', voronoi_line_width=0.1, voronoi_alpha=0.5, size_max=np.inf, overlay_points=None, overlay_points_categories=None, overlay_drop_categories=None, overlay_points_colors=None, overlay_point_size=5, overlay_point_alpha=1, overlay_point_shape='.', plot_legend=True, legend_size=6, **kwargs)

Parameters:

Name Type Description Default
adata

Anndata object

required
color_by string

Color the voronoi diagram based on categorical variable (e.g. cell types or neighbourhoods). Pass the name of the column which contains the categorical variable.

None
colors string or Dict

Custom coloring the voronoi diagram. The parameter accepts sns color palettes or a python dictionary mapping the categorical variable with the required color.

None
x_coordinate float

Column name containing the x-coordinates values.

'X_centroid'
y_coordinate float

Column name containing the y-coordinates values.

'Y_centroid'
flip_y bool

Flip the Y-axis if needed. Some algorithms output the XY with the Y-coordinates flipped. If the image overlays do not align to the cells, try again by setting this to False.

True
imageid string

Column name of the column containing the image id.

'imageid'
subset string

imageid of a single image to be subsetted for plotting.

None
voronoi_edge_color string

A Matplotlib color for marking the edges of the voronoi. If facecolor is passed, the edge color will always be the same as the face color.

'black'
voronoi_line_width float

The linewidth of the marker edges. Note: The default edgecolors is 'face'. You may want to change this as well.

0.1
voronoi_alpha float

The alpha blending value, between 0 (transparent) and 1 (opaque).

0.5
x_lim list

Pass the x-coordinates range [x1,x2].

None
y_lim list

Pass the y-coordinates range [y1,y2].

None
overlay_points string

It is possible to overlay a scatter plot on top of the voronoi diagram. Pass the name of the column which contains categorical variable to be overlayed.

None
overlay_points_categories list

If the passed column in overlay_points contains multiple categories, however the user is only interested in a subset of categories, those specific names can be passed as a list. By default all categories will be overlayed on the voronoi diagram.

None
overlay_drop_categories list

Similar to overlay_points_categories. Here for ease of use, especially if large number of categories are present. The user can drop a set of categories.

None
overlay_points_colors string or Dict

Similar to colors.
User can pass in a
a) solid color (like black)
b) sns palettes name (like Set1)
c) python dictionary mapping the categories with custom colors

None
overlay_point_size float

Overlay scatter plot point size.

5
overlay_point_alpha float

The alpha blending value for the overlay, between 0 (transparent) and 1 (opaque).

1
overlay_point_shape string

The marker style. marker can be either an instance of the class or the text shorthand for a particular marker.

'.'
plot_legend bool

Define if the figure legend should be plotted.
Please note the figure legend may be out of view and you may need to resize the image to see it, especially the legend for the scatter plot which will be on the left side of the plot.

True
legend_size float

Resize the legend if needed.

6
Example
1
2
3
4
5
6
7
8
9
sm.pl.voronoi(adata, color_by='phenotype', colors=None, 
         x_coordinate='X_position', y_coordinate='Y_position',
         imageid='ImageId',subset=None,
         voronoi_edge_color = 'black',voronoi_line_width = 0.2, 
         voronoi_alpha = 0.5, size_max=np.inf,
         overlay_points='phenotype', overlay_points_categories=None, 
         overlay_drop_categories=None,
         overlay_point_size = 5, overlay_point_alpha= 1, 
         overlay_point_shape=".", plot_legend=False, legend_size=6)
Source code in scimap/plotting/_voronoi.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
def voronoi (adata, color_by=None, colors=None, x_coordinate='X_centroid', y_coordinate='Y_centroid',
             imageid='imageid',subset=None, x_lim=None, y_lim=None, flip_y=True,
             voronoi_edge_color='black', voronoi_line_width=0.1, voronoi_alpha=0.5, size_max=np.inf,
             overlay_points=None, overlay_points_categories=None, overlay_drop_categories=None, overlay_points_colors=None,
             overlay_point_size = 5, overlay_point_alpha= 1, overlay_point_shape=".", plot_legend=True, legend_size = 6, **kwargs):
    """
Parameters:
    adata : Anndata object

    color_by (string):  
        Color the voronoi diagram based on categorical variable (e.g. cell types or neighbourhoods).
        Pass the name of the column which contains the categorical variable.

    colors (string or Dict):  
        Custom coloring the voronoi diagram. The parameter accepts `sns color palettes` or a python dictionary
        mapping the categorical variable with the required color.

    x_coordinate (float):  
        Column name containing the x-coordinates values.

    y_coordinate (float):  
        Column name containing the y-coordinates values.

    flip_y (bool):  
        Flip the Y-axis if needed. Some algorithms output the XY with the Y-coordinates flipped.
        If the image overlays do not align to the cells, try again by setting this to `False`.

    imageid (string):  
        Column name of the column containing the image id.

    subset (string):  
        imageid of a single image to be subsetted for plotting.

    voronoi_edge_color (string):  
        A Matplotlib color for marking the edges of the voronoi. 
        If `facecolor` is passed, the edge color will always be the same as the face color.

    voronoi_line_width (float):  
        The linewidth of the marker edges. Note: The default edgecolors is 'face'. You may want to change this as well. 

    voronoi_alpha (float):  
        The alpha blending value, between 0 (transparent) and 1 (opaque).

    x_lim (list):  
        Pass the x-coordinates range [x1,x2].

    y_lim (list):  
        Pass the y-coordinates range [y1,y2].

    overlay_points (string):  
        It is possible to overlay a scatter plot on top of the voronoi diagram.
        Pass the name of the column which contains categorical variable to be overlayed.

    overlay_points_categories (list):  
        If the passed column in `overlay_points` contains multiple categories, however the user is only
        interested in a subset of categories, those specific names can be passed as a list. By default all 
        categories will be overlayed on the voronoi diagram.

    overlay_drop_categories (list):  
        Similar to `overlay_points_categories`. Here for ease of use, especially if large number of categories are present.
        The user can drop a set of categories.

    overlay_points_colors (string or Dict):  
        Similar to `colors`.  
        User can pass in a  
        a) solid color (like `black`)  
        b) sns palettes name (like `Set1`)  
        c) python dictionary mapping the categories with custom colors

    overlay_point_size (float):  
        Overlay scatter plot point size.

    overlay_point_alpha (float):  
        The alpha blending value for the overlay, between 0 (transparent) and 1 (opaque).

    overlay_point_shape (string):  
        The marker style. marker can be either an instance of the class or the text shorthand for a particular marker.

    plot_legend (bool):  
        Define if the figure legend should be plotted.  
        Please note the figure legend may be out of view and you may need to resize the image to see it, especially 
        the legend for the scatter plot which will be on the left side of the plot.

    legend_size (float):  
        Resize the legend if needed.

Example:

    ```python
    sm.pl.voronoi(adata, color_by='phenotype', colors=None, 
             x_coordinate='X_position', y_coordinate='Y_position',
             imageid='ImageId',subset=None,
             voronoi_edge_color = 'black',voronoi_line_width = 0.2, 
             voronoi_alpha = 0.5, size_max=np.inf,
             overlay_points='phenotype', overlay_points_categories=None, 
             overlay_drop_categories=None,
             overlay_point_size = 5, overlay_point_alpha= 1, 
             overlay_point_shape=".", plot_legend=False, legend_size=6)

    ```

    """


    # create the data frame needed
    data = adata.obs

    # Subset the image of interest
    if subset is not None:
        data = data[data[imageid] == subset]


    # subset coordinates if needed
    if x_lim is not None:
        x1 = x_lim [0]
        if len(x_lim) < 2:
            x2 = max(data[x_coordinate])
        else:
            x2 = x_lim [1]
    if y_lim is not None:
        y1 = y_lim [0]
        if len(y_lim) < 2:
            y2 = min(data[y_coordinate])
        else:
            y2 = y_lim [1]
    # do the actuall subsetting
    if x_lim is not None:
        data = data[data[x_coordinate] >= x1]
        data = data[data[x_coordinate] <= x2]
    if y_lim is not None:
        data = data[data[y_coordinate] <= y1]
        data = data[data[y_coordinate] >= y2]


    # create an extra column with index information
    data['index_info'] = np.arange(data.shape[0])

    # generate the x and y coordinates
    points = data[[x_coordinate,y_coordinate]].values

    # invert the Y-axis
    if flip_y is True:
        points[:,1] = max(points[:,1])-points[:,1]

    # Generate colors
    if color_by is None:
        colors = np.repeat('#e5e5e5', len(data))
#    elif color_by is None and colors is not None:
#        if isinstance(colors,str):
#            colors = np.repeat(colors, len(data))
    elif color_by is not None and colors is None:
        # auto color the samples
        if len(np.unique(data[color_by])) <= 9:
            c = sns.color_palette('Set1')[0:len(np.unique(data[color_by]))]
        if len(np.unique(data[color_by])) > 9 and len(np.unique(data[color_by])) <= 20:
            c = sns.color_palette('tab20')[0:len(np.unique(data[color_by]))]
        if len(np.unique(data[color_by])) > 20:
            # For large categories generate random colors 
            np.random.seed(0) ; c = np.random.rand(len(np.unique(data[color_by])),3).tolist()
        # merge colors with phenotypes/ categories of interest
        p = np.unique(data[color_by])
        c_p = dict(zip(p, c))
        # map to colors
        colors = list(map(c_p.get, list(data[color_by].values)))
    elif color_by is not None and colors is not None:
        # check if colors is a dictionary or a sns color scale
        if isinstance(colors,str): 
            if len(sns.color_palette(colors)) < len(np.unique(data[color_by])):
                raise ValueError(str(colors) + ' includes a maximun of ' + str(len(sns.color_palette(colors))) + ' colors, while your data need '  + str(len(np.unique(data[color_by]))) + ' colors')
            else:
                c = sns.color_palette(colors)[0:len(np.unique(data[color_by]))]    
                # merge colors with phenotypes/ categories of interest
                p = np.unique(data[color_by])
                c_p = dict(zip(p, c))
        if isinstance(colors,dict):
            if len(colors) < len(np.unique(data[color_by])):
                raise ValueError('Color mapping is not provided for all categories. Please check')
            else:
                c_p = colors
        # map to colors
        colors = list(map(c_p.get, list(data[color_by].values)))



    # create the voronoi object
    vor = Voronoi(points)

    # trim the object
    regions, vertices = voronoi_finite_polygons_2d(vor)

    # plotting
    pts = MultiPoint([Point(i) for i in points])
    mask = pts.convex_hull
    new_vertices = []
    if type(voronoi_alpha)!=list:
        voronoi_alpha = [voronoi_alpha]*len(points)
    areas = []
    for i,(region,alph) in enumerate(zip(regions,voronoi_alpha)):
        polygon = vertices[region]
        shape = list(polygon.shape)
        shape[0] += 1
        p = Polygon(np.append(polygon, polygon[0]).reshape(*shape)).intersection(mask)
        areas+=[p.area]
        if p.area <size_max:
            poly = np.array(list(zip(p.boundary.coords.xy[0][:-1], p.boundary.coords.xy[1][:-1])))
            new_vertices.append(poly)
            if voronoi_edge_color == 'facecolor':
                plt.fill(*zip(*poly), alpha=alph, edgecolor=colors[i], linewidth = voronoi_line_width , facecolor = colors[i])
                plt.xticks([]) ; plt.yticks([]);
            else:
                plt.fill(*zip(*poly), alpha=alph, edgecolor=voronoi_edge_color, linewidth = voronoi_line_width, facecolor = colors[i])
                plt.xticks([]) ; plt.yticks([]);
                #plt.xlim([1097.5,1414.5])
                #plt.ylim([167.3,464.1])


    # Add scatter on top of the voronoi if user requests
    if overlay_points is not None:
        if overlay_points_categories is None:
            d = data
        if overlay_points_categories is not None:
            # convert to list if needed (cells to keep)
            if isinstance(overlay_points_categories,str): 
                overlay_points_categories = [overlay_points_categories]
            # subset cells needed
            d = data[data[overlay_points].isin(overlay_points_categories)]    
        if overlay_drop_categories is not None:
            # conver to list if needed (cells to drop)
            if isinstance(overlay_drop_categories,str): 
                overlay_drop_categories = [overlay_drop_categories]
            # subset cells needed
            d = d[-d[overlay_points].isin(overlay_drop_categories)]

        # Find the x and y coordinates for the overlay category
        #points_scatter = d[[x_coordinate,y_coordinate]].values
        points_scatter = points[d.index_info.values]

        # invert the Y-axis
        #points_scatter[:,1] = max(points_scatter[:,1])-points_scatter[:,1]

        # Generate colors for the scatter plot
        if overlay_points_colors is None and color_by == overlay_points:
            # Borrow color from vornoi
            wanted_keys = np.unique(d[overlay_points]) # The keys to extract
            c_p_scatter = dict((k, c_p[k]) for k in wanted_keys if k in c_p)
        elif overlay_points_colors is None and color_by != overlay_points:
            # Randomly generate colors for all the categories in scatter plot
            # auto color the samples
            if len(np.unique(d[overlay_points])) <= 9:
                c_scatter = sns.color_palette('Set1')[0:len(np.unique(d[overlay_points]))]
            if len(np.unique(d[overlay_points])) > 9 and len(np.unique(d[overlay_points])) <= 20:
                c_scatter = sns.color_palette('tab20')[0:len(np.unique(d[overlay_points]))]
            if len(np.unique(d[overlay_points])) > 20:
                # For large categories generate random colors 
                np.random.seed(1) ; c_scatter = np.random.rand(len(np.unique(d[overlay_points])),3).tolist()
            # merge colors with phenotypes/ categories of interest
            p_scatter = np.unique(d[overlay_points])
            c_p_scatter = dict(zip(p_scatter, c_scatter))
        elif  overlay_points_colors is not None:
            # check if the overlay_points_colors is a pallete
            if isinstance(overlay_points_colors,str):
                try:
                    c_scatter = sns.color_palette(overlay_points_colors)[0:len(np.unique(d[overlay_points]))]
                    if len(sns.color_palette(overlay_points_colors)) < len(np.unique(d[overlay_points])):
                        raise ValueError(str(overlay_points_colors) + ' pallete includes a maximun of ' + str(len(sns.color_palette(overlay_points_colors))) + ' colors, while your data (overlay_points_colors) need '  + str(len(np.unique(d[overlay_points]))) + ' colors') 
                except:
                    c_scatter = np.repeat(overlay_points_colors,len(np.unique(d[overlay_points])))   #[overlay_points_colors]
                # create a dict
                p_scatter = np.unique(d[overlay_points])
                c_p_scatter = dict(zip(p_scatter, c_scatter))
            if isinstance(overlay_points_colors,dict):
                if len(overlay_points_colors) < len(np.unique(d[overlay_points])):
                    raise ValueError('Color mapping is not provided for all categories. Please check overlay_points_colors')
                else:
                    c_p_scatter = overlay_points_colors
        # map to colors
        colors_scatter = list(map(c_p_scatter.get, list(d[overlay_points].values)))

        #plt.scatter(x = points_scatter[:,0], y = points_scatter[:,1], s= overlay_point_size, alpha= overlay_point_alpha, c= colors_scatter, marker=overlay_point_shape)
        plt.scatter(x = points_scatter[:,0], y = points_scatter[:,1], s= overlay_point_size, alpha= overlay_point_alpha, c= colors_scatter, marker=overlay_point_shape,**kwargs)
        plt.xticks([]) ; plt.yticks([]);


    if plot_legend is True:
        # Add legend to voronoi
        patchList = []
        for key in c_p:
                data_key = mpatches.Patch(color=c_p[key], label=key)
                patchList.append(data_key)

        first_legend = plt.legend(handles=patchList, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., prop={'size': legend_size})
        plt.tight_layout()
        # Add the legend manually to the current Axes.
        ax = plt.gca().add_artist(first_legend)

        if overlay_points is not None:
            # Add legend to scatter
            patchList_scatter = []
            for key in c_p_scatter:
                    data_key_scatter = mpatches.Patch(color=c_p_scatter[key], label=key)
                    patchList_scatter.append(data_key_scatter)

            plt.legend(handles=patchList_scatter, bbox_to_anchor=(-0.05, 1), loc=1, borderaxespad=0., prop={'size': legend_size})