def umap_feature_plot(
data,
feature,
hide_ticks = True,
legend_loc = 'top right',
title = 'UMAP plot',
point_size = 20,
random_state = 42,
palette = 'flare',
n_neighbors = 15,
min_dist = 0.5,
spread = 1.0,
alpha = 1.0,
init_pos = 'spectral',
n_epochs = 500,
negative_sample_rate = 5.0):
"""
Function to display UMAP reductions using seaborn
Parameters:
data : pandas.DataFrame
Data use for dim reduction
labels : pandas.Series
Labels for coloring the plot
hide_ticks : boolean
Should axis ticks be hidden? Default is True
legend_loc : str
Position of the legend
"""
embedding = umap.UMAP(
random_state = random_state,
min_dist = min_dist,
spread = spread,
n_neighbors = n_neighbors,
learning_rate = alpha,
negative_sample_rate = negative_sample_rate,
init = init_pos,
metric = 'euclidean',
n_epochs = n_epochs
)
embedding = pd.DataFrame(embedding.fit_transform(data), columns = ['UMAP1','UMAP2'])
labels = data[feature]
plot = sns.scatterplot(
x = 'UMAP1',
y = 'UMAP2',
data = embedding,
hue = labels.tolist(),
alpha = .8,
linewidth = 0.1,
s = point_size,
palette = palette,
edgecolor = 'grey'
)
plot.set_title(title)
if(hide_ticks):
plot.axes.xaxis.set_ticks([])
plot.axes.yaxis.set_ticks([])
plot.tick_params(left=False,bottom=False)
plot.legend(loc = legend_loc, fontsize = 8)
out_file = "figures/" + title + ".png"
plt.savefig(out_file, dpi = 600, transparent = False, facecolor = plot.get_facecolor())
def single_cell_umap(data, labels, hide_ticks = True, legend_loc = 'top right', title = 'UMAP plot', random_state = 42,point_size = 10, palette = 'deep'):
"""
Function to display UMAP reductions using seaborn
Parameters:
data : pandas.DataFrame
Data use for dim reduction
labels : pandas.Series
Labels for coloring the plot
hide_ticks : boolean
Should axis ticks be hidden? Default is True
legend_loc : str
Position of the legend
"""
embedding = umap.UMAP(random_state = random_state)
embedding = pd.DataFrame(embedding.fit_transform(data), columns = ['UMAP1','UMAP2'])
plot = sns.scatterplot(
x = 'UMAP1',
y = 'UMAP2',
data = embedding,
hue = labels.tolist(),
alpha = .8,
linewidth = 0,
s = point_size,
palette = palette
)
plot.set_title(title)
if(hide_ticks):
plot.axes.xaxis.set_ticks([])
plot.axes.yaxis.set_ticks([])
plot.tick_params(left=False,bottom=False)
plot.legend(loc = legend_loc, fontsize = 8)
out_file = "figures/" + title + ".png"
plt.savefig(out_file, dpi = 600, transparent = False, facecolor = plot.get_facecolor())
def clr(data):
"""
Compute clr normalization using the gmean function from scipy
data : data to transform
"""
return np.log(data+1) - np.log(gmean(data)+1)