/
powerplot.py
485 lines (437 loc) · 11.9 KB
/
powerplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
### A SET OF PLOTTING FUNCTIONS INSPIRED BY EXPLORING VIZGEN MERFISH
### --- START OF VIZGEN MERFISH SECTION
import numpy as np
import datashader as ds
import colorcet
import json
from __init__plots import *
class PlotScale:
"""
arguments: rangex, ragey, [npxlx, npxly, pxl_scale]
one of the three in [] will be required
"""
def __init__(self, rangex, rangey, npxlx=0, npxly=0, pxl_scale=0):
"""
rangex(y) - range of the x(y) axis (in micron)
pxl_scale - number of microns per pixel
npxlx(y) - number of pixels on the x(y)axis
"""
# 1 of the three optional args need to be set
assert (np.array([npxlx, npxly, pxl_scale])==0).sum() == 2
self.rangex = rangex
self.rangey = rangey
if pxl_scale:
pxl_scale = pxl_scale
npxlx = int(rangex/pxl_scale)
npxly = int(rangey/pxl_scale)
if npxlx:
npxlx = int(npxlx)
pxl_scale = rangex/npxlx
npxly = int(rangey/pxl_scale)
if npxly:
npxly = int(npxly)
pxl_scale = rangey/npxly
npxlx = int(rangex/pxl_scale)
self.pxl_scale = pxl_scale
self.npxlx = npxlx
self.npxly = npxly
self.num_pxl = self.npxlx*self.npxly
self.check_dim()
def check_dim(self):
"""
"""
num_pixel_limit = 1e6
assert self.npxlx > 0
assert self.npxly > 0
assert self.num_pxl < num_pixel_limit
return
def len2pixel(self, length):
"""
"""
return int(length/self.pxl_scale)
def pixel2len(self, npixel):
"""
"""
return npixel*self.pxl_scale
class CategoricalColors:
"""
Arguments: labels, [colors]
"""
def __init__(self, labels, colors=[], basis_cmap=colorcet.cm.rainbow):
"""
"""
self.labels = labels
self.indices = np.arange(len(labels))
if not colors:
self.colors = basis_cmap(np.linspace(0, 1, len(self.indices)))
# colors = colorcet.cm.glasbey(np.arange(len(indices)))
# colors = sns.color_palette('husl', len(indices))
else:
self.colors = colors
assert len(self.labels) == len(self.colors)
self.gen_cmap()
def gen_cmap(self):
"""Use a list of colors to generate a categorical cmap
which maps
[0, 1) -> self.colors[0]
[1, 2) -> self.colors[1]
[2, 3) -> self.colors[2]
[3, 4) -> self.colors[3]
...
"""
self.cmap = mpl.colors.ListedColormap(self.colors)
self.bounds = np.arange(len(self.colors)+1)
self.norm = mpl.colors.BoundaryNorm(self.bounds, self.cmap.N)
def add_colorbar(
self,
fig,
cax_dim=[0.95, 0.1, 0.05, 0.8],
shift=0.5,
fontsize=10,
**kwargs,
):
"""
"""
cax = fig.add_axes(cax_dim)
cbar = fig.colorbar(
cm.ScalarMappable(cmap=self.cmap, norm=self.norm),
cax=cax,
boundaries=self.bounds,
ticks=self.bounds[:-1]+shift,
drawedges=True,
**kwargs,
)
cbar.ax.set_yticklabels(self.labels, fontsize=fontsize)
cbar.ax.tick_params(axis=u'both', which=u'both', length=0)
return
def to_dict(self, to_hex=True, output=""):
"""
"""
if to_hex:
self.palette = {label: mpl.colors.to_hex(color)
for label, color
in zip(self.labels, self.colors)
}
else:
self.palette = {label: color
for label, color
in zip(self.labels, self.colors)
}
if output:
with open(output, 'w') as fh:
json.dump(self.palette, fh)
print("saved to file: {}".format(output))
return self.palette
def agg_data(
data,
x, y,
npxlx, npxly,
agg,
):
"""
"""
aggdata = ds.Canvas(plot_width=npxlx, plot_height=npxly).points(data, x, y, agg=agg)
return aggdata
def agg_data_count(
data,
x, y,
npxlx, npxly,
):
agg = ds.count()
aggdata = agg_data(data, x, y, npxlx, npxly, agg)
agg = ds.any()
aggdata_any = agg_data(data, x, y, npxlx, npxly, agg)
aggdata = aggdata/aggdata_any
return aggdata
def agg_data_ps(data, x, y, agg, scale_paras):
"""
"""
# main
rangex = data[x].max() - data[x].min()
rangey = data[y].max() - data[y].min()
ps = PlotScale(rangex, rangey, **scale_paras)
aggdata = agg_data(data, x, y, ps.npxlx, ps.npxly, agg,)
return aggdata, ps
def agg_count_cat(
data, x, y, z, scale_paras,
clip_max=0,
reduce=False,
sharp_boundary=True,
):
"""count categorical data
"""
# collect aggdata and ps
agg = ds.count_cat(z)
aggdata, ps = agg_data_ps(data, x, y, agg, scale_paras)
zlabels = aggdata[z].values
if clip_max:
aggdata = aggdata.clip(max=clip_max)
if reduce:
aggdata = aggdata.argmax(z)
if sharp_boundary:
# normalize by any (set no cells to nan)
agg = ds.any()
aggdata_any = agg_data(data, x, y, ps.npxlx, ps.npxly, agg)
aggdata_any = aggdata_any.astype(int)
aggdata = aggdata/aggdata_any
return aggdata, ps, zlabels
def set_vmin_vmax(
numbers, vmaxp=99
):
"""
"""
vmin, vmax = 0, np.nanpercentile(numbers, vmaxp)
return vmin, vmax
def add_colorbar_unified_colorbar(
fig, cax,
vmin=0, vmax=0,
cmap=sns.cubehelix_palette(as_cmap=True),
**kwargs,
):
"""User specified vmin and vmax
"""
# colorbar
norm = plt.Normalize(vmin, vmax)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm,)
fig.colorbar(sm, cax=cax,
ticks=[vmin, vmax],
label='Normalized expression',
**kwargs,
)
return
def add_colorbar(
fig, cax,
vmaxp=99,
cmap=sns.cubehelix_palette(as_cmap=True),
**kwargs,
):
"""[log10(normalized_counts+1)] further normed by the 99% highest expression)
"""
# colorbar
norm = plt.Normalize(0, vmaxp)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm,)
fig.colorbar(sm, cax=cax,
ticks=[0, vmaxp],
label='Normalized expression\n(normed by 99% highest expression)',
**kwargs,
)
return
def imshow_routine(
ax,
aggdata,
cmap=sns.cubehelix_palette(as_cmap=True),
vmin=None, vmax=None,
origin='lower',
aspect='equal',
**kwargs
):
"""
"""
ax.imshow(aggdata, aspect=aspect, cmap=cmap, origin=origin, vmin=vmin, vmax=vmax, **kwargs)
ax.axis('off')
return ax
def massive_scatterplot(
ax,
data,
x, y,
npxlx, npxly,
agg=ds.count(),
cmap=sns.cubehelix_palette(as_cmap=True),
vmin=0,
vmax=0,
vmaxp=99,
):
"""
"""
aggdata = agg_data(data, x, y, npxlx, npxly, agg)
if vmin==0 and vmax == 0:
vmin, vmax = set_vmin_vmax(aggdata.values, vmaxp)
imshow_routine(ax, aggdata, cmap=cmap,
vmin=vmin, vmax=vmax,
)
else:
imshow_routine(ax, aggdata, cmap=cmap,
vmin=vmin, vmax=vmax,
)
return ax
def massive_scatterplot_withticks(
ax, data, x, y, npxlx, npxly,
aspect='auto',
color_logscale=False,
):
xmin, xmax = data[x].min(), data[x].max()
ymin, ymax = data[y].min(), data[y].max()
aggdata = agg_data_count(data, x, y, npxlx, npxly)
if color_logscale:
aggdata = np.log10(aggdata)
ax.imshow(
aggdata, origin='lower', aspect=aspect,
extent=[xmin, xmax, ymin, ymax])
ax.set_xlabel(x)
ax.set_ylabel(y)
return
def add_arrows(
ax, label,
fontsize=15,
px=-0.01,
py=-0.01,
):
"""
"""
# arrows
ax.arrow(px, py, 0, 0.1,
transform=ax.transAxes,
head_width=0.01, head_length=0.01,
fc='k', ec='k', clip_on=False,)
ax.arrow(px, py, 0.1, 0,
transform=ax.transAxes,
head_width=0.01, head_length=0.01,
fc='k', ec='k', clip_on=False,)
ax.text(px, py-0.01, label,
transform=ax.transAxes,
va='top', ha='left',
fontsize=fontsize)
# end arrows
return ax
def add_scalebar(
ax, left, right, label, fontsize=15,
ax_y=-0.01,
):
"""
"""
ax.hlines(ax_y, left, right, color='k', linewidth=3,
transform=ax.get_xaxis_transform(),
clip_on=False,
)
ax.text(right, ax_y-0.01, label,
va='top', ha='right',
transform=ax.get_xaxis_transform(),
fontsize=fontsize)
# end scale bar
return ax
def plot_gene_insitu_routine(
ax, data, x, y, hue, scale_paras, cmap, title,
arrows=True, scalebar=True,
vmaxp=99,
vmin=0, vmax=0,
):
"""
"""
# main
agg = ds.mean(hue)
rangex = data[x].max() - data[x].min()
rangey = data[y].max() - data[y].min()
ps = PlotScale(rangex, rangey, **scale_paras)
massive_scatterplot(
ax, data, x, y, ps.npxlx, ps.npxly,
agg=agg,
cmap=cmap,
vmaxp=vmaxp,
vmin=vmin,
vmax=vmax,
)
ax.set_title(title)
# arrows
if arrows:
add_arrows(ax, 'in situ')
# scale bar
if scalebar:
bar_length = 1000 # (micron)
add_scalebar(ax, ps.npxlx-ps.len2pixel(bar_length), ps.npxlx, '1 mm')
return ax
def plot_gene_umap_routine(
ax, data, x, y, hue, scale_paras, cmap, title,
arrows=True,
vmaxp=99,
):
"""
"""
# main
agg = ds.mean(hue)
rangex = data[x].max() - data[x].min()
rangey = data[y].max() - data[y].min()
ps = PlotScale(rangex, rangey, **scale_paras)
massive_scatterplot(ax, data, x, y, ps.npxlx, ps.npxly,
agg=agg,
cmap=cmap,
vmaxp=vmaxp,
)
ax.set_title(title)
# arrows
if arrows:
add_arrows(ax, 'UMAP', px=-0.03, py=-0.03)
return ax
def plot_cluster_insitu_routine(
ax,
ps,
aggdata,
hue,
zlabel,
title,
cmap,
arrows=True, scalebar=True,
):
"""
ps - an instance of PlotScale
"""
zlabels = aggdata.coords[hue].values
i = np.where(zlabels==zlabel)[0][0]
imshow_routine(
ax,
aggdata[:,:,i],
cmap=cmap,
)
ax.set_title(title)
# arrows
if arrows:
add_arrows(ax, 'in situ')
# scale bar
if scalebar:
bar_length = 1000 # (micron)
add_scalebar(ax, ps.npxlx-ps.len2pixel(bar_length), ps.npxlx, '1 mm')
return ax
def plot_cluster_umap_routine(
ax,
ps,
aggdata,
hue,
zlabel,
title,
cmap,
arrows=True, scalebar=True,
):
"""
ps - an instance of PlotScale
"""
zlabels = aggdata.coords[hue].values
i = np.where(zlabels==zlabel)[0][0]
imshow_routine(
ax,
aggdata[:,:,i],
cmap=cmap,
)
ax.set_title(title)
# arrows
if arrows:
add_arrows(ax, 'UMAP')
return ax
### END OF VIZGEN MERFISH SECTION
def gen_cdf(array, ax, x_range=[], n_points=1000, show=True, flip=False, **kwargs):
""" returns x and y values
"""
x = np.sort(array)
y = np.arange(len(array))/len(array)
if flip:
# x = x[::-1]
y = 1 - y
if not x_range:
if show:
ax.plot(x, y, **kwargs)
return x, y
else:
start, end = x_range
xbins = np.linspace(start, end, n_points)
ybins = np.interp(xbins, x, y)
if show:
ax.plot(xbins, ybins, **kwargs)
return xbins, ybins