forked from codyheiser/furry-couscous
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fcc_utils.py
1253 lines (1091 loc) · 42.3 KB
/
fcc_utils.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
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Utility functions for dimensionality reduction structural preservation analysis
"""
import warnings
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as pd
import scanpy as sc
from ot import wasserstein_1d
from scipy.spatial.distance import cdist, pdist
from scipy.stats import pearsonr
from sklearn.neighbors import kneighbors_graph
from sklearn.preprocessing import normalize
import seaborn as sns
sc.set_figure_params(dpi=90, color_map="viridis")
sns.set(style="white")
def arcsinh(adata, layer=None, norm="l1", scale=1000):
"""
Returns arcsinh-normalized values for each element in anndata counts matrix
Parameters
----------
adata : anndata.AnnData
AnnData object
layer : str, optional (default=None)
name of layer to perform arcsinh-normalization on. if None, use `adata.X`
norm : str {"l1","l2"}, optional (default="l1")
normalization strategy prior to arcsinh transform. None=do not normalize data.
"l1"=divide each count by sum of counts for each cell. "l2"=divide each count
by sqrt of sum of squares of counts for cell.
scale : float, optional (default=1000)
factor to multiply normalized counts by
Returns
-------
`adata` is edited in place to add `adata.layers["arcsinh_norm"]`
"""
if layer is None:
mat = adata.X
else:
mat = adata.layers[layer]
adata.layers["arcsinh_norm"] = np.arcsinh(normalize(mat, axis=1, norm=norm) * scale)
def knn_graph(dist_matrix, k, adata, save_rep="knn"):
"""
Builds simple binary k-nearest neighbor graph and add to anndata object
Parameters
----------
dist_matrix : np.array
distance matrix to calculate knn graph for (i.e. `pdist(adata.obsm["X_pca"])`)
k : int
number of nearest neighbors to determine
adata : anndata.AnnData
AnnData object to add resulting graph to (in `.uns` slot)
save_rep : str, optional (default="knn")
name of `.uns` key to save knn graph to within adata
Returns
-------
`adata` is edited in place, adding knn graph to `adata.uns[save_rep]`
"""
adata.uns[save_rep] = {
"graph": kneighbors_graph(
dist_matrix, k, mode="connectivity", include_self=False, n_jobs=-1
).toarray(),
"k": k,
}
def subset_uns_by_ID(adata, uns_keys, obs_col, IDs):
"""
Subsets symmetrical distance matrices and knn graphs in `adata.uns` by one or more
IDs defined in `adata.obs`
Parameters
----------
adata : anndata.AnnData
AnnData object
uns_keys : list of str
list of keys in `adata.uns` to subset. new `adata.uns` keys will be saved with
ID appended to name (i.e. `adata.uns["knn"]` -> `adata.uns["knn_ID1"]`)
obs_col : str
name of column in `adata.obs` to use as cell IDs (i.e. "leiden")
IDs : list of str
list of IDs to include in subset
Returns
-------
`adata` is edited in place, adding new `.uns` keys for each ID
"""
for key in uns_keys:
tmp = adata.uns[key][
adata.obs[obs_col].isin(IDs), :
] # subset symmetrical uns matrix along axis 0
tmp = tmp[
:, adata.obs[obs_col].isin(IDs)
] # subset symmetrical uns matrix along axis 1
adata.uns[
"{}_{}".format(key, "_".join([str(x) for x in IDs]))
] = tmp # save new .uns key by appending IDs to original key name
def find_centroids(adata, use_rep, obs_col="leiden"):
"""
Finds cluster centroids
Parameters
----------
adata : anndata.AnnData
AnnData object
use_rep : str
"X" or `adata.obsm` key containing space to calculate centroids in
(i.e. "X_pca")
obs_col "str, optional (default="leiden")
`adata.obs` column name containing cluster IDs
Returns
-------
`adata` is edited in place, adding `adata.uns["{}_centroids"]`,
`adata.uns["{}_centroid_distances"]`, and `adata.uns["{}_centroid_MST"]`
containing centroid coordinates, distance matrix between all centoids, and a
minimum spanning tree graph between the centroids, respectively
"""
# calculate centroids
clu_names = adata.obs[obs_col].unique().astype(str)
if use_rep == "X":
adata.uns["{}_centroids".format(use_rep)] = np.array(
[
np.mean(adata.X[adata.obs[obs_col].astype(str) == clu, :], axis=0)
for clu in clu_names
]
)
else:
adata.uns["{}_centroids".format(use_rep)] = np.array(
[
np.mean(
adata.obsm[use_rep][adata.obs[obs_col].astype(str) == clu, :],
axis=0,
)
for clu in clu_names
]
)
# calculate distances between all centroids
adata.uns["{}_centroid_distances".format(use_rep)] = cdist(
adata.uns["{}_centroids".format(use_rep)],
adata.uns["{}_centroids".format(use_rep)],
)
# build networkx minimum spanning tree between centroids
G = nx.from_numpy_matrix(adata.uns["{}_centroid_distances".format(use_rep)])
G = nx.relabel_nodes(G, mapping=dict(zip(list(G.nodes), clu_names)), copy=True)
adata.uns["{}_centroid_MST".format(use_rep)] = nx.minimum_spanning_tree(G)
# dimensionality reduction plotting class #
class DR_plot:
"""
Class defining pretty plots of dimension-reduced embeddings such as PCA, t-SNE,
and UMAP
Attributes
----------
.fig : matplotlib.figure
the figure object on which data will be plotted
.ax : matplotlib.axes.ax
the axes within `self.fig`
.cmap : matplotlib.pyplot.cmap
color map to use for plotting; default="plasma"
Methods
-------
.plot()
utility plotting function that can be passed any numpy array in the `data`
parameter
.plot_IDs()
plots one or more cluster IDs on top of an `.obsm` from an AnnData object
.plot_centroids()
plots cluster centroids defined using `find_centroids()` function on AnnData
object
"""
def __init__(self, dim_name="dim", figsize=(5, 5), ax_labels=True):
"""
Initializes `DR_plot` class
Parameters
----------
dim_name : str, optional (default="dim")
how to label axes ("dim 1" on x and "dim 2" on y by default)
figsize : tuple of float, optional (default=(5,5))
size of resulting figure in inches
ax_labels : bool, optional (default=True)
draw arrows and dimension names in lower left corner of plot
Returns
-------
Initializes `self.fig` and `self.ax` according to input specs
"""
self.fig, self.ax = plt.subplots(1, figsize=figsize)
self.cmap = plt.get_cmap("plasma")
if ax_labels:
plt.xlabel("{} 1".format(dim_name), fontsize=14)
self.ax.xaxis.set_label_coords(0.2, -0.025)
plt.ylabel("{} 2".format(dim_name), fontsize=14)
self.ax.yaxis.set_label_coords(-0.025, 0.2)
plt.annotate(
"",
textcoords="axes fraction",
xycoords="axes fraction",
xy=(-0.006, 0),
xytext=(0.2, 0),
arrowprops=dict(arrowstyle="<-", lw=2, color="black"),
)
plt.annotate(
"",
textcoords="axes fraction",
xycoords="axes fraction",
xy=(0, -0.006),
xytext=(0, 0.2),
arrowprops=dict(arrowstyle="<-", lw=2, color="black"),
)
plt.tick_params(labelbottom=False, labelleft=False)
sns.despine(left=True, bottom=True)
plt.tight_layout()
def plot(self, data, color, pt_size=75, legend=None, save_to=None):
"""
General plotting function for dimensionality reduction outputs with cute
arrows and labels
Parameters
----------
data : np.array
array containing variables in columns and observations in rows
color : list
list of length `nrow(data)` to determine how points should be colored (ie.
`adata.obs["leiden"].values` to color by "leiden" cluster categories)
pt_size : float, optional (default=75)
size of points in plot
legend : str {"full","brief"}, optional (default=None)
string describing the legend size. None for no legend
save_to : str, optional (default=None)
path to `.png` file to save output. do not save if None
Returns
-------
`self.fig`, `self.ax` edited; plot saved to `.png` file if `save_to` is not
None
"""
sns.scatterplot(
x=data[:, 0],
y=data[:, 1],
s=pt_size,
alpha=0.7,
hue=color,
legend=legend,
edgecolor="none",
ax=self.ax,
)
if legend is not None:
plt.legend(
bbox_to_anchor=(1, 1, 0.2, 0.2),
loc="lower left",
frameon=False,
fontsize="small",
)
if save_to is None:
return
else:
plt.savefig(fname=save_to, transparent=True, bbox_inches="tight", dpi=1000)
def plot_IDs(
self, adata, use_rep, obs_col="leiden", IDs="all", pt_size=75, save_to=None
):
"""
General plotting function for dimensionality reduction outputs with
categorical colors (i.e. "leiden" or "louvain") and cute arrows and labels
Parameters
----------
adata : anndata.AnnData
object to pull dimensionality reduction from
use_rep : str
`adata.obsm` key to plot from (i.e. "X_pca")
obs_col : str, optional (default="leiden")
name of column in `adata.obs` to use as cell IDs (i.e. "leiden")
IDs : list of str, optional (default="all")
list of IDs to plot, graying out cells not assigned to those IDs. if
"all", show all ID categories.
pt_size : float, optional (default=75)
size of points in plot
save_to : str, optional (default=None)
path to `.png` file to save output. do not save if None
Returns
-------
`self.fig`, `self.ax` edited; plot saved to `.png` file if `save_to` is not
None
"""
plotter = adata.obsm[use_rep]
clu_names = adata.obs[obs_col].unique().astype(str)
# use existing scanpy colors, if applicable
if obs_col == "leiden" and "leiden_colors" in adata.uns.keys():
colors = [
adata.uns["leiden_colors"][x]
for x in adata.obs.leiden.unique().astype(int)
]
elif obs_col == "louvain" and "louvain_colors" in adata.uns.keys():
colors = [
adata.uns["louvain_colors"][x]
for x in adata.obs.louvain.unique().astype(int)
]
# otherwise, get new color mapping from obs_col using self.cmap
else:
colors = self.cmap(np.linspace(0, 1, len(clu_names)))
cdict = dict(zip(clu_names, colors))
if IDs == "all":
self.ax.scatter(
x=plotter[:, 0],
y=plotter[:, 1],
s=pt_size,
alpha=0.7,
c=[cdict[x] for x in adata.obs[obs_col].astype(str)],
edgecolor="none",
)
else:
sns.scatterplot(
x=plotter[-adata.obs[obs_col].isin(IDs), 0],
y=plotter[-adata.obs[obs_col].isin(IDs), 1],
ax=self.ax,
s=pt_size,
alpha=0.1,
color="gray",
legend=False,
edgecolor="none",
)
plt.scatter(
x=plotter[adata.obs[obs_col].isin(IDs), 0],
y=plotter[adata.obs[obs_col].isin(IDs), 1],
s=pt_size,
alpha=0.7,
c=[
cdict[x]
for x in adata.obs.loc[
adata.obs[obs_col].isin(IDs), obs_col
].astype(str)
],
edgecolor="none",
)
if save_to is None:
return
else:
plt.savefig(fname=save_to, transparent=True, bbox_inches="tight", dpi=1000)
def plot_centroids(
self,
adata,
use_rep,
obs_col="leiden",
ctr_size=300,
pt_size=75,
draw_edges=True,
highlight_edges=False,
save_to=None,
):
"""
General plotting function for cluster centroid graph and MST
(i.e. from "leiden" or "louvain") and cute arrows and labels
Parameters
----------
adata : anndata.AnnData
object to pull dimensionality reduction from
use_rep : str
`adata.obsm` key to plot from (i.e. "X_pca")
obs_col : str, optional (default="leiden")
name of column in `adata.obs` to use as cell IDs (i.e. "leiden")
ctr_size : float, optional (default=300)
size of centroid points in plot
pt_size : float, optional (default=75)
size of points in plot
draw_edges : bool, optional (default=True)
draw edges of minimum spanning tree between all centroids
highlight_edges : list of int, optional (default=False)
list of edge IDs as tuples to highlight in red on plot. e.g.
`set(adata.uns['X_tsne_centroid_MST'].edges).difference(set(adata.uns['X_umap_centroid_MST'].edges))`
with output {(0,3), (0,7)} says that edges from centroid 0 to 3 and 0 to 7
are found in 'X_tsne_centroids' but not in 'X_umap_centroids'. highlight
the edges to show this.
save_to : str, optional (default=None)
path to `.png` file to save output. do not save if None
Returns
-------
`self.fig`, `self.ax` edited; plot saved to `.png` file if `save_to` is not
None
"""
clu_names = adata.obs[obs_col].unique().astype(str)
# use existing scanpy colors, if applicable
if obs_col == "leiden" and "leiden_colors" in adata.uns.keys():
colors = [
adata.uns["leiden_colors"][x]
for x in adata.obs.leiden.unique().astype(int)
]
elif obs_col == "louvain" and "louvain_colors" in adata.uns.keys():
colors = [
adata.uns["louvain_colors"][x]
for x in adata.obs.louvain.unique().astype(int)
]
# otherwise, get new color mapping from obs_col using self.cmap
else:
colors = self.cmap(np.linspace(0, 1, len(clu_names)))
# draw points in embedding first
sns.scatterplot(
x=adata.obsm[use_rep][:, 0],
y=adata.obsm[use_rep][:, 1],
ax=self.ax,
s=pt_size,
alpha=0.1,
color="gray",
legend=False,
edgecolor="none",
)
# draw MST edges if desired, otherwise just draw centroids
if not draw_edges:
self.ax.scatter(
x=adata.uns["{}_centroids".format(use_rep)][:, 0],
y=adata.uns["{}_centroids".format(use_rep)][:, 1],
s=ctr_size,
c=colors,
edgecolor="none",
)
else:
pos = dict(zip(clu_names, adata.uns["{}_centroids".format(use_rep)][:, :2]))
nx.draw_networkx(
adata.uns["{}_centroid_MST".format(use_rep)],
pos=pos,
ax=self.ax,
with_labels=False,
width=2,
node_size=ctr_size,
node_color=colors,
)
# highlight edges if desired
if highlight_edges:
nx.draw_networkx_edges(
adata.uns["{}_centroid_MST".format(use_rep)],
pos=pos,
ax=self.ax,
edgelist=highlight_edges,
width=5,
edge_color="red",
)
if save_to is None:
return
else:
plt.savefig(fname=save_to, transparent=True, bbox_inches="tight", dpi=1000)
def distance_stats(pre, post, downsample=False, verbose=True):
"""
Tests for correlation between Euclidean cell-cell distances before and after
transformation by a function or DR algorithm.
Parameters
----------
pre : np.array
vector of unique distances (pdist()) or distance matrix of shape (n_cells,
m_cells), i.e. (cdist()) before transformation/projection
post : np.array
vector of unique distances (pdist()) or distance matrix of shape (n_cells,
m_cells), i.e. (cdist()) after transformation/projection
downsample : int, optional (default=False)
number of distances to downsample to. maximum of 50M (~10k cells, if
symmetrical) is recommended for performance.
verbose : bool, optional (default=True)
print progress statements to console
Returns
-------
pre : np.array
vector of normalized unique distances (pdist()) or distance matrix of shape
(n_cells, m_cells), before transformation/projection
post : np.array
vector of normalized unique distances (pdist()) or distance matrix of shape
(n_cells, m_cells), after transformation/projection
corr_stats : list
output of `pearsonr()` function correlating the two normalized unique distance
vectors
EMD : float
output of `wasserstein_1d()` function calculating the Earth Mover's Distance
between the two normalized unique distance vectors
1) performs Pearson correlation of distance distributions
2) normalizes unique distances using min-max standardization for each dataset
3) calculates Wasserstein or Earth-Mover's Distance for normalized distance
distributions between datasets
"""
# make sure the number of cells in each matrix is the same
assert (
pre.shape == post.shape
), 'Matrices contain different number of distances.\n{} in "pre"\n{} in "post"\n'.format(
pre.shape[0], post.shape[0]
)
# if distance matrix (mA x mB, result of cdist), flatten to unique cell-cell distances
if pre.ndim == 2:
if verbose:
print("Flattening pre-transformation distance matrix into 1D array...")
# if symmetric, only keep unique values (above diagonal)
if np.allclose(pre, pre.T, rtol=1e-05, atol=1e-08):
pre = pre[np.triu_indices(n=pre.shape[0], k=1)]
# otherwise, flatten all distances
else:
pre = pre.flatten()
# if distance matrix (mA x mB, result of cdist), flatten to unique cell-cell distances
if post.ndim == 2:
if verbose:
print("Flattening post-transformation distance matrix into 1D array...")
# if symmetric, only keep unique values (above diagonal)
if np.allclose(post, post.T, rtol=1e-05, atol=1e-08):
post = post[np.triu_indices(n=post.shape[0], k=1)]
# otherwise, flatten all distances
else:
post = post.flatten()
# if dataset is large, randomly downsample to reasonable number of distances for calculation
if downsample:
assert downsample < len(
pre
), "Must provide downsample value smaller than total number of cell-cell distances provided in pre and post"
if verbose:
print("Downsampling to {} total cell-cell distances...".format(downsample))
idx = np.random.choice(np.arange(len(pre)), downsample, replace=False)
pre = pre[idx]
post = post[idx]
# calculate correlation coefficient using Pearson correlation
if verbose:
print("Correlating distances")
corr_stats = pearsonr(x=pre, y=post)
# min-max normalization for fair comparison of probability distributions
if verbose:
print("Normalizing unique distances")
pre -= pre.min()
pre /= pre.ptp()
post -= post.min()
post /= post.ptp()
# calculate EMD for the distance matrices
# by default, downsample to 50M distances to speed processing time,
# since this function often breaks with larger distributions
if verbose:
print("Calculating Earth-Mover's Distance between distributions")
if len(pre) > 50000000:
idx = np.random.choice(np.arange(len(pre)), 50000000, replace=False)
pre_EMD = pre[idx]
post_EMD = post[idx]
EMD = wasserstein_1d(pre_EMD, post_EMD)
else:
EMD = wasserstein_1d(pre, post)
return pre, post, corr_stats, EMD
def knn_preservation(pre, post):
"""
Tests for k-nearest neighbor preservation (%) before and after transformation by a
function or DR algorithm.
Parameters
----------
pre : np.array
knn graph of shape (n_cells, n_cells) before transformation/projection
post : np.array
knn graph of shape (n_cells, n_cells) after transformation/projection
Returns
-------
knn_pres : float
knn preservation expressed as a percentage out of 100 %
"""
# make sure the number of cells in each matrix is the same
assert (
pre.shape == post.shape
), 'Matrices contain different number of cells.\n{} in "pre"\n{} in "post"\n'.format(
pre.shape[0], post.shape[0]
)
return np.round(
(np.isclose(pre, post, rtol=1e-05, atol=1e-08).sum() / (pre.shape[0] ** 2))
* 100,
4,
)
def structure_preservation_sc(
adata,
latent,
native="X",
metric="euclidean",
k=30,
downsample=False,
verbose=True,
force_recalc=False,
):
"""
Wrapper function for full structural preservation workflow applied to `scanpy`
AnnData object
Parameters
----------
adata : anndata.AnnData
AnnData object with latent space to test in `.obsm` slot, and native
(reference) space in `.X` or `.obsm`
latent : str
`adata.obsm` key that contains low-dimensional latent space for testing
native : str, optional (default="X")
`adata.obsm` key or `.X` containing high-dimensional native space, which
should be direct input to dimension reduction that generated latent `.obsm`
for fair comparison. default "X", which uses `adata.X`.
metric : str {"chebyshev","cityblock","euclidean","minkowski","mahalanobis",
"seuclidean"}, optional (default="euclidean")
distance metric to use
k : int, optional (default=30)
number of nearest neighbors to test preservation
downsample : int, optional (default=False)
number of distances to downsample to. maximum of 50M (~10k cells, if
symmetrical) is recommended for performance.
verbose : bool, optional (default=True)
print progress statements to console
force_recalc : bool, optional (default=False)
if True, recalculate all distances and neighbor graphs, regardless of their
presence in `adata`
Returns
-------
corr_stats : list
output of `pearsonr()` function correlating the two normalized unique distance
vectors
EMD : float
output of `wasserstein_1d()` function calculating the Earth Mover's Distance
between the two normalized unique distance vectors
knn_pres : float
knn preservation expressed as a percentage out of 100 %
"""
# 0) determine native space according to argument
if native == "X":
native_space = adata.X.copy()
else:
native_space = adata.obsm[native].copy()
# 1) calculate unique cell-cell distances
if (
"{}_distances".format(native) not in adata.uns.keys() or force_recalc
): # check for existence in AnnData to prevent re-work
if verbose:
print("Calculating unique distances for native space, {}".format(native))
adata.uns["{}_distances".format(native)] = cdist(
native_space, native_space, metric=metric
)
if (
"{}_distances".format(latent) not in adata.uns.keys() or force_recalc
): # check for existence in AnnData to prevent re-work
if verbose:
print("Calculating unique distances for latent space, {}".format(latent))
adata.uns["{}_distances".format(latent)] = cdist(
adata.obsm[latent], adata.obsm[latent], metric=metric
)
# 2) get correlation and EMD values, and return normalized distance vectors for plotting distributions
(
adata.uns["{}_norm_distances".format(native)],
adata.uns["{}_norm_distances".format(latent)],
corr_stats,
EMD,
) = distance_stats(
pre=adata.uns["{}_distances".format(native)].copy(),
post=adata.uns["{}_distances".format(latent)].copy(),
verbose=verbose,
downsample=downsample,
)
# 3) determine neighbors
if (
"{}_neighbors".format(native) not in adata.uns.keys() or force_recalc
): # check for existence in AnnData to prevent re-work
if verbose:
print(
"{}-nearest neighbor calculation for native space, {}".format(k, native)
)
knn_graph(
adata.uns["{}_distances".format(native)],
k=k,
adata=adata,
save_rep="{}_knn".format(native),
)
if (
"{}_neighbors".format(latent) not in adata.uns.keys() or force_recalc
): # check for existence in AnnData to prevent re-work
if verbose:
print(
"{}-nearest neighbor calculation for latent space, {}".format(k, latent)
)
knn_graph(
adata.uns["{}_distances".format(latent)],
k=k,
adata=adata,
save_rep="{}_knn".format(latent),
)
# 4) calculate neighbor preservation
if verbose:
print("Determining nearest neighbor preservation")
if (
adata.uns["{}_knn".format(native)]["k"]
!= adata.uns["{}_knn".format(latent)]["k"]
):
warnings.warn(
'Warning: Nearest-neighbor graphs constructed with different k values. k={} in "{}_neighbors", while k={} in "{}_neighbors". Consider re-generating neighbors graphs by setting force_recalc=True.'.format(
adata.uns["{}_knn".format(native)]["k"],
native,
adata.uns["{}_knn".format(latent)]["k"],
latent,
)
)
knn_pres = knn_preservation(
pre=adata.uns["{}_knn".format(native)]["graph"],
post=adata.uns["{}_knn".format(latent)]["graph"],
)
if verbose:
print("Done!")
return corr_stats, EMD, knn_pres
class SP_plot:
"""
Class defining pretty plots for structural evaluation of dimension-reduced
embeddings such as PCA, t-SNE, and UMAP
Attributes
----------
.figsize : tuple of float
the size of the figure object on which data will be plotted
.fig : matplotlib.figure
the figure object on which data will be plotted
.ax : matplotlib.axes.ax
the axes within `self.fig`
.palette : sns.cubehelix_palette()
color palette to use for coloring `seaborn` plots
.cmap : matplotlib.pyplot.cmap
color map to use for plotting; default="cubehelix" from `seaborn`
.pre : np.array
flattened vector of normalized, unique cell-cell distances
"pre-transformation". upper triangle of cell-cell distance matrix, flattened
to vector of shape ((n_cells^2)/2)-n_cells.
.post : np.array
flattened vector of normalized, unique cell-cell distances
"post-transformation". upper triangle of cell-cell distance matrix, flattened
to vector of shape ((n_cells^2)/2)-n_cells.
.labels : list of str
name of pre- and post-transformation spaces for legend (plot_cell_distances,
plot_distributions, plot_cumulative_distributions) or axis labels
(plot_distance_correlation, joint_plot_distance_correlation) as list of two
strings. False to exclude labels.
Methods
-------
.plot_cell_distances()
plots all unique cell-cell distances before and after some transformation
.plot_distributions()
plots probability distributions for all unique cell-cell distances before and
after some transformation
.plot_cumulative_distributions()
plots cumulative probability distributions for all unique cell-cell distances
before and after some transformation
.plot_distance_correlation()
plots correlation of all unique cell-cell distances before and after some
transformation
.joint_plot_distance_correlation()
plots correlation of all unique cell-cell distances before and after some
transformation. includes marginal plots of each distribution.
"""
def __init__(
self, pre_norm, post_norm, figsize=(4, 4), labels=["Native", "Latent"]
):
"""
Initializes SP plot class
Parameters
----------
pre_norm : np.array
flattened vector of normalized, unique cell-cell distances
"pre-transformation". upper triangle of cell-cell distance matrix, flattened
to vector of shape ((n_cells^2)/2)-n_cells.
post_norm : np.array
flattened vector of normalized, unique cell-cell distances
"post-transformation". upper triangle of cell-cell distance matrix, flattened
to vector of shape ((n_cells^2)/2)-n_cells.
figsize : tuple of float, optional (default=(4,4))
the size of the figure object on which data will be plotted
labels : list of str, optional (default=["Native","Latent"])
name of pre- and post-transformation spaces for legend (plot_cell_distances,
plot_distributions, plot_cumulative_distributions) or axis labels
(plot_distance_correlation, joint_plot_distance_correlation) as list of two
strings. False to exclude labels.
Returns
-------
Initializes `self.fig` and `self.ax` according to input specs
"""
self.figsize = figsize
self.fig, self.ax = plt.subplots(1, figsize=self.figsize)
self.palette = sns.cubehelix_palette()
self.cmap = sns.cubehelix_palette(as_cmap=True)
self.pre = pre_norm
self.post = post_norm
self.labels = labels
plt.tick_params(labelbottom=False, labelleft=False)
sns.despine()
plt.tight_layout()
def plot_cell_distances(self, legend=True, save_to=None):
"""
Plots all unique cell-cell distances before and after some transformation
Parameters
----------
legend : bool, optional (default=True)
display legend on plot
save_to : str, optional (default=None)
path to `.png` file to save output. do not save if None
Returns
-------
`self.fig`, `self.ax` edited; plot saved to `.png` file if `save_to` is not
None
"""
plt.plot(self.pre, alpha=0.7, label=self.labels[0], color=self.palette[-1])
plt.plot(self.post, alpha=0.7, label=self.labels[1], color=self.palette[2])
if legend:
plt.legend(loc="best", fontsize="xx-large")
else:
plt.legend()
self.ax.legend().remove()
if save_to is None:
return
else:
plt.savefig(fname=save_to, transparent=True, bbox_inches="tight", dpi=1000)
def plot_distributions(self, legend=True, save_to=None):
"""
Plots probability distributions for all unique cell-cell distances before and
after some transformation
Parameters
----------
legend : bool, optional (default=True)
display legend on plot
save_to : str, optional (default=None)
path to `.png` file to save output. do not save if None
Returns
-------
`self.fig`, `self.ax` edited; plot saved to `.png` file if `save_to` is not
None
"""
sns.distplot(
self.pre, hist=False, kde=True, label=self.labels[0], color=self.palette[-1]
)
sns.distplot(
self.post, hist=False, kde=True, label=self.labels[1], color=self.palette[2]
)
if legend:
plt.legend(loc="best", fontsize="xx-large")
else:
plt.legend()
self.ax.legend().remove()
if save_to is None:
return
else:
plt.savefig(fname=save_to, transparent=True, bbox_inches="tight", dpi=1000)
def plot_cumulative_distributions(self, legend=True, save_to=None):
"""
Plots cumulative probability distributions for all unique cell-cell distances
before and after some transformation
Parameters
----------
legend : bool, optional (default=True)
display legend on plot
save_to : str, optional (default=None)
path to `.png` file to save output. do not save if None
Returns
-------
`self.fig`, `self.ax` edited; plot saved to `.png` file if `save_to` is not
None
"""
num_bins = int(len(self.pre) / 100)
pre_counts, pre_bin_edges = np.histogram(self.pre, bins=num_bins)
pre_cdf = np.cumsum(pre_counts)
post_counts, post_bin_edges = np.histogram(self.post, bins=num_bins)
post_cdf = np.cumsum(post_counts)
plt.plot(
pre_bin_edges[1:],
pre_cdf / pre_cdf[-1],
label=self.labels[0],
color=self.palette[-1],
)
plt.plot(
post_bin_edges[1:],
post_cdf / post_cdf[-1],
label=self.labels[1],
color=self.palette[2],
)
if legend:
plt.legend(loc="lower right", fontsize="xx-large")
else:
plt.legend()
self.ax.legend().remove()
if save_to is None:
return
else:
plt.savefig(fname=save_to, transparent=True, bbox_inches="tight", dpi=1000)
def plot_distance_correlation(self, save_to=None):
"""
Plots correlation of all unique cell-cell distances before and after some
transformation