Created
March 7, 2021 05:28
-
-
Save ivirshup/c9a119d50c63b9232c846e8570d042e0 to your computer and use it in GitHub Desktop.
tsne_nearest_neighbor_method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scanpy as sc | |
import numpy as np | |
from matplotlib import pyplot as plt | |
from openTSNE import TSNE | |
from itertools import product | |
pbmc = sc.datasets.pbmc3k_processed() | |
RANDOM_STATE = 1291321 | |
keys = [] | |
for metric, knn_method in product(["euclidean", "cosine"], ["annoy", "pynndescent"]): | |
key = f"tsne_{metric}_{knn_method}" | |
keys.append(key) | |
pbmc.obsm[key] = np.asarray( | |
TSNE( | |
n_jobs=-1, | |
metric=metric, | |
negative_gradient_method="bh", | |
neighbors=knn_method, | |
random_state=RANDOM_STATE, | |
).fit(pbmc.X) | |
) | |
with plt.rc_context({"figure.dpi": 150, "figure.figsize": (5, 5)}): | |
fig, axs = plt.subplots(ncols=2, nrows=2) | |
for key, ax in zip(keys, axs.flat): | |
sc.pl.embedding( | |
pbmc, | |
basis=key, | |
size=20, | |
color="louvain", | |
ax=ax, | |
show=False, | |
legend_loc=None, | |
title=key, | |
) | |
plt.tight_layout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment