Last active
May 15, 2020 14:45
-
-
Save ivirshup/0bf8568fd873a94c9e73ba40be2be4d4 to your computer and use it in GitHub Desktop.
Mascot UMAP
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
from PIL import image | |
import pandas as pd | |
import numpy as np | |
empty_pixel = np.array([255, 255, 255, 0]).reshape(1, 1, -1) | |
# !wget https://github.com/theislab/scanpy/raw/master/docs/_static/img/Scanpy_Logo_RGB.png | |
im = Image.open("./Scanpy_Logo_RGB.png") | |
a = np.array(im) | |
# Make dataframe of pixels | |
df = pd.DataFrame( | |
dict( | |
zip( | |
("y_coord", "x_coord"), | |
np.where(np.logical_and.reduce(~np.equal(a, empty_pixel), axis=2)) | |
) | |
) | |
) | |
# Translate pixel coordinates from cartesian to euclidean | |
df["y"] = np.abs(df["y_coord"] - df["y_coord"].max()) | |
df["x"] = df["x_coord"] | |
# Remove the text | |
sub = df[~((df["y"] < 400) & (df["x"] < 1500))] | |
# Retrieving rgba values: | |
colors = a[sub["y_coord"], sub["x_coord"], :] | |
for i, c in enumerate("rgba"): | |
sub[c] = colors[:, i] | |
import umap | |
import umap.plot | |
u = umap.UMAP() | |
u.fit(sub.drop(columns=["x_coord", "y_coord"])) | |
# But I couldn't figure out how to use the rgba info for a color map, so I'll color by: | |
sub["c"] = sub["x"] - sub["y"] | |
sub["c"] -= sub["c"].min() | |
sub["c"] /= sub["c"].max() | |
umap.plot.points(u, values=sub["c"], cmap="magma_r", width=3000, height=3000) | |
# Hexbin | |
import hvplot.pandas | |
sub["inv_c"] = 1 - sub["c"] | |
( | |
sub | |
.hvplot.hexbin("x", "y", C="inv_c", reduce_function=np.sum, cmap="magma", logz=True, xaxis=None, yaxis=None, colorbar=False, width=600, height=350) | |
.opts(gridsize=45) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment