[e72cf6]: / eval_utils.py

Download this file

627 lines (548 with data), 27.5 kB

  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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
from math import inf
import os
import logging
import numpy as np
import scanpy as sc
import anndata as ad
import pandas as pd
import matplotlib
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from scipy.sparse.csr import spmatrix
from scipy.stats import chi2
from typing import Mapping, Sequence, Tuple, Iterable, Union
from scipy.sparse import issparse
from sklearn.metrics import adjusted_rand_score, normalized_mutual_info_score, silhouette_samples
from sklearn.neighbors import NearestNeighbors
import psutil
import scib
_cpu_count: Union[None, int] = psutil.cpu_count(logical=False)
if _cpu_count is None:
_cpu_count: int = psutil.cpu_count(logical=True)
_logger = logging.getLogger(__name__)
def evaluate(adata: ad.AnnData,
n_epoch: int,
embedding_key: str = 'delta',
n_neighbors: int = 15,
resolutions: Iterable[float] = [0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64],
clustering_method: str = "leiden",
cell_type_col: str = "cell_types",
batch_col: Union[str, None] = "batch_indices",
color_by: Iterable[str] = None,
return_fig: bool = False,
plot_fname: str = "umap",
plot_ftype: str = "jpg",
plot_dir: Union[str, None] = None,
plot_dpi: int = 300,
min_dist: float = 0.3,
spread: float = 1,
n_jobs: int = 1,
random_state: Union[None, int, np.random.RandomState, np.random.Generator] = 0,
umap_kwargs: dict = dict()
) -> Mapping[str, Union[float, None, Figure]]:
"""Evaluates the clustering and batch correction performance of the given
embeddings, and optionally plots the embeddings.
Embeddings will be plotted if return_fig is True or plot_dir is provided.
When tensorboard_dir is provided, will also save the embeddings using a
tensorboard SummaryWriter.
NOTE: Set n_jobs to 1 if you encounter pickling error.
Args:
adata: the dataset with the embedding to be evaluated.
embedding_key: the key to the embedding. Must be in adata.obsm.
n_neighbors: #neighbors used when computing neithborhood graph and
calculating entropy of batch mixing / kBET.
resolutions: a sequence of resolutions used for clustering.
clustering_method: clustering method used. Should be one of 'leiden' or
'louvain'.
cell_type_col: a key in adata.obs to the cell type column.
batch_col: a key in adata.obs to the batch column.
return_fig: whether to return the Figure object. Useful for visualizing
the plot.
color_by: a list of adata.obs column keys to color the embeddings by.
If None, will look up adata.uns['color_by']. Only used if is
drawing.
plot_fname: file name of the generated plot. Only used if is drawing.
plot_ftype: file type of the generated plot. Only used if is drawing.
plot_dir: directory to save the generated plot. If None, do not save
the plot.
plot_dpi: dpi to save the plot.
writer: an initialized SummaryWriter to save the UMAP plot to. Only
used if is drawing.
min_dist: the min_dist argument in sc.tl.umap. Only used is drawing.
spread: the spread argument in sc.tl.umap. Only used if is drawing.
n_jobs: # jobs to generate. If <= 0, this is set to the number of
physical cores.
random_state: random state for knn calculation.
umap_kwargs: other kwargs to pass to sc.pl.umap.
Returns:
A dict storing the ari, nmi, asw, ebm and k_bet of the cell embeddings
with key "ari", "nmi", "asw", "ebm", "k_bet", respectively. If draw is
True and return_fig is True, will also store the plotted figure with
key "fig".
"""
if cell_type_col and not pd.api.types.is_categorical_dtype(adata.obs[cell_type_col]):
#_logger.warning("scETM.evaluate assumes discrete cell types. Converting cell_type_col to categorical.")
adata.obs[cell_type_col] = adata.obs[cell_type_col].astype(str).astype('category')
if batch_col and not pd.api.types.is_categorical_dtype(adata.obs[batch_col]):
#_logger.warning("scETM.evaluate assumes discrete batches. Converting batch_col to categorical.")
adata.obs[batch_col] = adata.obs[batch_col].astype(str).astype('category')
# calculate neighbors
_get_knn_indices(adata, use_rep=embedding_key, n_neighbors=n_neighbors, random_state=random_state, calc_knn=True)
# calculate clustering metrics
if cell_type_col in adata.obs and len(resolutions) > 0:
cluster_key, best_ari, best_nmi = clustering(adata, resolutions=resolutions, cell_type_col=cell_type_col, batch_col=batch_col, clustering_method=clustering_method)
else:
cluster_key = best_ari = best_nmi = None
if adata.obs[cell_type_col].nunique() > 1:
sw = silhouette_samples(adata.X if embedding_key == 'X' else adata.obsm[embedding_key],
adata.obs[cell_type_col])
adata.obs['silhouette_width'] = sw
asw = np.mean(sw)
#print(f'{embedding_key}_ASW: {asw:7.4f}')
asw_2 = scib.me.silhouette(adata, group_key=cell_type_col, embed=embedding_key)
if batch_col and cell_type_col:
sw_table = adata.obs.pivot_table(index=cell_type_col, columns=batch_col, values="silhouette_width",
aggfunc="mean")
#print(f'SW: {sw_table}')
if plot_dir is not None:
sw_table.to_csv(os.path.join(plot_dir, f'{plot_fname}.csv'))
else:
asw = 0.
asw_2 = 0.
# calculate batch correction metrics
need_batch = batch_col and adata.obs[batch_col].nunique() > 1
if need_batch:
ebm = calculate_entropy_batch_mixing(adata,
use_rep=embedding_key,
batch_col=batch_col,
n_neighbors=n_neighbors,
calc_knn=False,
n_jobs=n_jobs,
)
#print(f'{embedding_key}_BE: {ebm:7.4f}')
k_bet = calculate_kbet(adata,
use_rep=embedding_key,
batch_col=batch_col,
n_neighbors=n_neighbors,
calc_knn=False,
n_jobs=n_jobs,
)[2]
#print(f'{embedding_key}_kBET: {k_bet:7.4f}')
batch_asw = scib.me.silhouette_batch(adata, batch_key=batch_col, group_key='cell_type', embed=embedding_key, verbose=False)
batch_graph_score = get_graph_connectivity(adata, use_rep=embedding_key,)
else:
ebm = k_bet = batch_asw = batch_graph_score = None
# plot UMAP embeddings
if return_fig or plot_dir is not None:
if color_by is None:
color_by = [batch_col, cell_type_col] if need_batch else [cell_type_col]
color_by = list(color_by)
if 'color_by' in adata.uns:
for col in adata.uns['color_by']:
if col not in color_by:
color_by.insert(0, col)
if cluster_key is not None:
color_by = [cluster_key] + color_by
fig = draw_embeddings(adata=adata, color_by=color_by,
min_dist=min_dist, spread=spread,
ckpt_dir=plot_dir, fname=f'{plot_fname+str(n_epoch)}.{plot_ftype}', return_fig=return_fig,
dpi=plot_dpi,
umap_kwargs=umap_kwargs)
else:
fig = None
return dict(
ari=best_ari,
nmi=best_nmi,
asw=asw,
asw_2=asw_2,
ebm=ebm,
k_bet=k_bet,
batch_asw=batch_asw,
batch_graph_score=batch_graph_score,
fig=fig
)
def evaluate_ari(adata: ad.AnnData,
n_epoch: int,
embedding_key: str = 'delta',
n_neighbors: int = 15,
resolutions: Iterable[float] = [0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64],
clustering_method: str = "leiden",
cell_type_col: str = "cell_types",
batch_col: Union[str, None] = "batch_indices",
color_by: Iterable[str] = None,
return_fig: bool = False,
plot_fname: str = "umap",
plot_ftype: str = "jpg",
plot_dir: Union[str, None] = None,
plot_dpi: int = 300,
min_dist: float = 0.3,
spread: float = 1,
n_jobs: int = 1,
random_state: Union[None, int, np.random.RandomState, np.random.Generator] = 0,
umap_kwargs: dict = dict()
) -> Mapping[str, Union[float, None, Figure]]:
"""Evaluates the clustering and batch correction performance of the given
embeddings, and optionally plots the embeddings.
Embeddings will be plotted if return_fig is True or plot_dir is provided.
When tensorboard_dir is provided, will also save the embeddings using a
tensorboard SummaryWriter.
NOTE: Set n_jobs to 1 if you encounter pickling error.
Args:
adata: the dataset with the embedding to be evaluated.
embedding_key: the key to the embedding. Must be in adata.obsm.
n_neighbors: #neighbors used when computing neithborhood graph and
calculating entropy of batch mixing / kBET.
resolutions: a sequence of resolutions used for clustering.
clustering_method: clustering method used. Should be one of 'leiden' or
'louvain'.
cell_type_col: a key in adata.obs to the cell type column.
batch_col: a key in adata.obs to the batch column.
return_fig: whether to return the Figure object. Useful for visualizing
the plot.
color_by: a list of adata.obs column keys to color the embeddings by.
If None, will look up adata.uns['color_by']. Only used if is
drawing.
plot_fname: file name of the generated plot. Only used if is drawing.
plot_ftype: file type of the generated plot. Only used if is drawing.
plot_dir: directory to save the generated plot. If None, do not save
the plot.
plot_dpi: dpi to save the plot.
writer: an initialized SummaryWriter to save the UMAP plot to. Only
used if is drawing.
min_dist: the min_dist argument in sc.tl.umap. Only used is drawing.
spread: the spread argument in sc.tl.umap. Only used if is drawing.
n_jobs: # jobs to generate. If <= 0, this is set to the number of
physical cores.
random_state: random state for knn calculation.
umap_kwargs: other kwargs to pass to sc.pl.umap.
Returns:
A dict storing the ari, nmi, asw, ebm and k_bet of the cell embeddings
with key "ari", "nmi", "asw", "ebm", "k_bet", respectively. If draw is
True and return_fig is True, will also store the plotted figure with
key "fig".
"""
if cell_type_col and not pd.api.types.is_categorical_dtype(adata.obs[cell_type_col]):
#_logger.warning("scETM.evaluate assumes discrete cell types. Converting cell_type_col to categorical.")
adata.obs[cell_type_col] = adata.obs[cell_type_col].astype(str).astype('category')
if batch_col and not pd.api.types.is_categorical_dtype(adata.obs[batch_col]):
#_logger.warning("scETM.evaluate assumes discrete batches. Converting batch_col to categorical.")
adata.obs[batch_col] = adata.obs[batch_col].astype(str).astype('category')
# calculate neighbors
_get_knn_indices(adata, use_rep=embedding_key, n_neighbors=n_neighbors, random_state=random_state, calc_knn=True)
# calculate clustering metrics
if cell_type_col in adata.obs and len(resolutions) > 0:
cluster_key, best_ari, best_nmi = clustering(adata, resolutions=resolutions, cell_type_col=cell_type_col, batch_col=batch_col, clustering_method=clustering_method)
else:
cluster_key = best_ari = best_nmi = None
return best_ari
def _eff_n_jobs(n_jobs: Union[None, int]) -> int:
"""If n_jobs <= 0, set it as the number of physical cores _cpu_count"""
if n_jobs is None:
return 1
return int(n_jobs) if n_jobs > 0 else _cpu_count
def _calculate_kbet_for_one_chunk(knn_indices, attr_values, ideal_dist, n_neighbors):
dof = ideal_dist.size - 1
ns = knn_indices.shape[0]
results = np.zeros((ns, 2))
for i in range(ns):
# NOTE: Do not use np.unique. Some of the batches may not be present in
# the neighborhood.
observed_counts = pd.Series(attr_values[knn_indices[i, :]]).value_counts(sort=False).values
expected_counts = ideal_dist * n_neighbors
stat = np.sum((observed_counts - expected_counts) ** 2 / expected_counts)
p_value = 1 - chi2.cdf(stat, dof)
results[i, 0] = stat
results[i, 1] = p_value
return results
def _get_knn_indices(adata: ad.AnnData,
use_rep: str = "delta",
n_neighbors: int = 25,
random_state: int = 0,
calc_knn: bool = True
) -> np.ndarray:
if calc_knn:
assert use_rep == 'X' or use_rep in adata.obsm, f'{use_rep} not in adata.obsm and is not "X"'
neighbors = sc.Neighbors(adata)
neighbors.compute_neighbors(n_neighbors=n_neighbors, knn=True, use_rep=use_rep, random_state=random_state,
write_knn_indices=True)
adata.obsp['distances'] = neighbors.distances
adata.obsp['connectivities'] = neighbors.connectivities
adata.obsm['knn_indices'] = neighbors.knn_indices
adata.uns['neighbors'] = {
'connectivities_key': 'connectivities',
'distances_key': 'distances',
'knn_indices_key': 'knn_indices',
'params': {
'n_neighbors': n_neighbors,
'use_rep': use_rep,
'metric': 'euclidean',
'method': 'umap'
}
}
else:
assert 'neighbors' in adata.uns, 'No precomputed knn exists.'
assert adata.uns['neighbors']['params'][
'n_neighbors'] >= n_neighbors, f"pre-computed n_neighbors is {adata.uns['neighbors']['params']['n_neighbors']}, which is smaller than {n_neighbors}"
return adata.obsm['knn_indices']
def get_graph_connectivity(
adata: ad.AnnData,
use_rep: str = "delta",):
sc.pp.neighbors(adata, use_rep=use_rep)
score = scib.me.graph_connectivity(adata, label_key='cell_type')
return score
def calculate_kbet(
adata: ad.AnnData,
use_rep: str = "delta",
batch_col: str = "batch_indices",
n_neighbors: int = 25,
alpha: float = 0.05,
random_state: int = 0,
n_jobs: Union[None, int] = None,
calc_knn: bool = True
) -> Tuple[float, float, float]:
"""Calculates the kBET metric of the data.
kBET measures if cells from different batches mix well in their local
neighborhood.
Args:
adata: annotated data matrix.
use_rep: the embedding to be used. Must exist in adata.obsm.
batch_col: a key in adata.obs to the batch column.
n_neighbors: # nearest neighbors.
alpha: acceptance rate threshold. A cell is accepted if its kBET
p-value is greater than or equal to alpha.
random_state: random seed. Used only if method is "hnsw".
n_jobs: # jobs to generate. If <= 0, this is set to the number of
physical cores.
calc_knn: whether to re-calculate the kNN graph or reuse the one stored
in adata.
Returns:
stat_mean: mean kBET chi-square statistic over all cells.
pvalue_mean: mean kBET p-value over all cells.
accept_rate: kBET Acceptance rate of the sample.
"""
_logger.info('Calculating kbet...')
assert batch_col in adata.obs
if adata.obs[batch_col].dtype.name != "category":
_logger.warning(f'Making the column {batch_col} of adata.obs categorical.')
adata.obs[batch_col] = adata.obs[batch_col].astype('category')
ideal_dist = (
adata.obs[batch_col].value_counts(normalize=True, sort=False).values
) # ideal no batch effect distribution
nsample = adata.shape[0]
nbatch = ideal_dist.size
attr_values = adata.obs[batch_col].values.copy()
attr_values.categories = range(nbatch)
knn_indices = _get_knn_indices(adata, use_rep, n_neighbors, random_state, calc_knn)
# partition into chunks
n_jobs = min(_eff_n_jobs(n_jobs), nsample)
starts = np.zeros(n_jobs + 1, dtype=int)
quotient = nsample // n_jobs
remainder = nsample % n_jobs
for i in range(n_jobs):
starts[i + 1] = starts[i] + quotient + (1 if i < remainder else 0)
from joblib import Parallel, delayed, parallel_backend
with parallel_backend("loky", n_jobs=n_jobs):
kBET_arr = np.concatenate(
Parallel()(
delayed(_calculate_kbet_for_one_chunk)(
knn_indices[starts[i]: starts[i + 1], :], attr_values, ideal_dist, n_neighbors
)
for i in range(n_jobs)
)
)
res = kBET_arr.mean(axis=0)
stat_mean = res[0]
pvalue_mean = res[1]
accept_rate = (kBET_arr[:, 1] >= alpha).sum() / nsample
return (stat_mean, pvalue_mean, accept_rate)
def _entropy(hist_data):
_, counts = np.unique(hist_data, return_counts=True)
freqs = counts / counts.sum()
return (-freqs * np.log(freqs + 1e-30)).sum()
def _entropy_batch_mixing_for_one_pool(batches, knn_indices, nsample, n_samples_per_pool):
indices = np.random.choice(
np.arange(nsample), size=n_samples_per_pool)
return np.mean(
[
_entropy(batches[knn_indices[indices[i]]])
for i in range(n_samples_per_pool)
]
)
def calculate_entropy_batch_mixing(
adata: ad.AnnData,
use_rep: str = "delta",
batch_col: str = "batch_indices",
n_neighbors: int = 50,
n_pools: int = 50,
n_samples_per_pool: int = 100,
random_state: int = 0,
n_jobs: Union[None, int] = None,
calc_knn: bool = True
) -> float:
"""Calculates the entropy of batch mixing of the data.
kBET measures if cells from different batches mix well in their local
neighborhood.
Args:
adata: annotated data matrix.
use_rep: the embedding to be used. Must exist in adata.obsm.
batch_col: a key in adata.obs to the batch column.
n_neighbors: # nearest neighbors.
n_pools: #pools of cells to calculate entropy of batch mixing.
n_samples_per_pool: #cells per pool to calculate within-pool entropy.
random_state: random seed. Used only if method is "hnsw".
n_jobs: # jobs to generate. If <= 0, this is set to the number of
physical cores.
calc_knn: whether to re-calculate the kNN graph or reuse the one stored
in adata.
Returns:
score: the mean entropy of batch mixing, averaged from n_pools samples.
"""
_logger.info('Calculating batch mixing entropy...')
nsample = adata.n_obs
knn_indices = _get_knn_indices(adata, use_rep, n_neighbors, random_state, calc_knn)
from joblib import Parallel, delayed, parallel_backend
with parallel_backend("loky", n_jobs=n_jobs, inner_max_num_threads=1):
score = np.mean(
Parallel()(
delayed(_entropy_batch_mixing_for_one_pool)(
adata.obs[batch_col], knn_indices, nsample, n_samples_per_pool
)
for _ in range(n_pools)
)
)
return score
def clustering(
adata: ad.AnnData,
resolutions: Sequence[float],
clustering_method: str = "leiden",
cell_type_col: str = "cell_types",
batch_col: str = "batch_indices"
) -> Tuple[str, float, float]:
"""Clusters the data and calculate agreement with cell type and batch
variable.
This method cluster the neighborhood graph (requires having run sc.pp.
neighbors first) with "clustering_method" algorithm multiple times with the
given resolutions, and return the best result in terms of ARI with cell
type.
Other metrics such as NMI with cell type, ARi with batch are logged but not
returned. (TODO: also return these metrics)
Args:
adata: the dataset to be clustered. adata.obsp shouhld contain the keys
'connectivities' and 'distances'.
resolutions: a list of leiden/louvain resolution parameters. Will
cluster with each resolution in the list and return the best result
(in terms of ARI with cell type).
clustering_method: Either "leiden" or "louvain".
cell_type_col: a key in adata.obs to the cell type column.
batch_col: a key in adata.obs to the batch column.
Returns:
best_cluster_key: a key in adata.obs to the best (in terms of ARI with
cell type) cluster assignment column.
best_ari: the best ARI with cell type.
best_nmi: the best NMI with cell type.
"""
assert len(resolutions) > 0, f'Must specify at least one resolution.'
if clustering_method == 'leiden':
clustering_func = sc.tl.leiden
elif clustering_method == 'louvain':
clustering_func = sc.tl.louvain
else:
raise ValueError("Please specify louvain or leiden for the clustering method argument.")
#_logger.info(f'Performing {clustering_method} clustering')
assert cell_type_col in adata.obs, f"{cell_type_col} not in adata.obs"
best_res, best_ari, best_nmi = None, -inf, -inf
for res in resolutions:
col = f'{clustering_method}_{res}'
clustering_func(adata, resolution=res, key_added=col)
ari = adjusted_rand_score(adata.obs[cell_type_col], adata.obs[col])
nmi = normalized_mutual_info_score(adata.obs[cell_type_col], adata.obs[col])
n_unique = adata.obs[col].nunique()
if ari > best_ari:
best_res = res
best_ari = ari
if nmi > best_nmi:
best_nmi = nmi
if batch_col in adata.obs and adata.obs[batch_col].nunique() > 1:
ari_batch = adjusted_rand_score(adata.obs[batch_col], adata.obs[col])
#print(f'Resolution: {res:5.3g}\tARI: {ari:7.4f}\tNMI: {nmi:7.4f}\tbARI: {ari_batch:7.4f}\t# labels: {n_unique}')
else:
#print(f'Resolution: {res:5.3g}\tARI: {ari:7.4f}\tNMI: {nmi:7.4f}\t# labels: {n_unique}')
a=None
return f'{clustering_method}_{best_res}', best_ari, best_nmi
def draw_embeddings(adata: ad.AnnData,
color_by: Union[str, Sequence[str], None] = None,
min_dist: float = 0.3,
spread: float = 1,
ckpt_dir: str = '.',
fname: str = "umap.pdf",
return_fig: bool = False,
dpi: int = 300,
umap_kwargs: dict = dict()
) -> Union[None, Figure]:
"""Embeds, plots and optionally saves the neighborhood graph with UMAP.
Requires having run sc.pp.neighbors first.
Args:
adata: the dataset to draw. adata.obsp shouhld contain the keys
'connectivities' and 'distances'.
color_by: a str or a list of adata.obs keys to color the points in the
scatterplot by. E.g. if both cell_type_col and batch_col is in
color_by, then we would have two plots colored by cell type and
batch variables, respectively.
min_dist: The effective minimum distance between embedded points.
Smaller values will result in a more clustered/clumped embedding
where nearby points on the manifold are drawn closer together,
while larger values will result on a more even dispersal of points.
spread: The effective scale of embedded points. In combination with
`min_dist` this determines how clustered/clumped the embedded
points are.
ckpt_dir: where to save the plot. If None, do not save the plot.
fname: file name of the saved plot. Only used if ckpt_dir is not None.
return_fig: whether to return the Figure object. Useful for visualizing
the plot.
dpi: the dpi of the saved plot. Only used if ckpt_dir is not None.
umap_kwargs: other kwargs to pass to sc.pl.umap.
Returns:
If return_fig is True, return the figure containing the plot.
"""
#_logger.info(f'Plotting UMAP embeddings...')
sc.tl.umap(adata, min_dist=min_dist, spread=spread)
fig = sc.pl.umap(adata, color=color_by, show=False, return_fig=True, **umap_kwargs)
if ckpt_dir is not None:
assert os.path.exists(ckpt_dir), f'ckpt_dir {ckpt_dir} does not exist.'
fig.savefig(
os.path.join(ckpt_dir, fname),
dpi=dpi, bbox_inches='tight'
)
if return_fig:
return fig
fig.clf()
plt.close(fig)
def set_figure_params(
matplotlib_backend: str = 'agg',
dpi: int = 120,
frameon: bool = True,
vector_friendly: bool = True,
fontsize: int = 10,
figsize: Sequence[int] = (10, 10)
):
"""Set figure parameters.
Args
backend: the backend to switch to. This can either be one of th
standard backend names, which are case-insensitive:
- interactive backends:
GTK3Agg, GTK3Cairo, MacOSX, nbAgg,
Qt4Agg, Qt4Cairo, Qt5Agg, Qt5Cairo,
TkAgg, TkCairo, WebAgg, WX, WXAgg, WXCairo
- non-interactive backends:
agg, cairo, pdf, pgf, ps, svg, template
or a string of the form: ``module://my.module.name``.
dpi: resolution of rendered figures – this influences the size of
figures in notebooks.
frameon: add frames and axes labels to scatter plots.
vector_friendly: plot scatter plots using `png` backend even when
exporting as `pdf` or `svg`.
fontsize: the fontsize for several `rcParams` entries.
figsize: plt.rcParams['figure.figsize'].
"""
matplotlib.use(matplotlib_backend)
sc.set_figure_params(dpi=dpi, figsize=figsize, fontsize=fontsize, frameon=frameon, vector_friendly=vector_friendly)