You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry for the slow reply on this. You see, this functionality doesn't exist in Scarf, but we did provide the function to draw in the code that accompanied the manuscript.
But to simplify things, I have created the following function that you can run out of the box. I will bake this into Scarf in the next release (lots of good things coming there).
Just copy and paste the function below and then call it like this:
plot_centroid_umap(ds)
wherein ds is the scarf DataStore
defplot_centroid_umap(
ds,
group_key: str="RNA_leiden_cluster",
layout_key: str="RNA_UMAP",
show_edges: bool=True,
padding: float=1.25,
cluster_size_multiplier: float=1.0,
edge_width_multiplier: float=100.0,
cmap: str=None,
custom_color_key=None,
save_name: str=None
):
importpandasaspdimportmatplotlib.pyplotaspltfrommatplotlib.collectionsimportLineCollectionfromscarf.plotsimport_scatter_make_colorsc_x=f"{layout_key}1"c_y=f"{layout_key}2"df=ds.cells.to_pandas_dataframe(
key="I",
columns=[c_x, c_y, group_key]
).groupby(group_key).mean()
clusts=ds.cells.fetch(group_key)
df["cluster_size"] =pd.Series(clusts).value_counts() *cluster_size_multiplier# Fix group colurs as per Scarf's `plot_layout` logiccmap=_scatter_make_colors(
df.index.astype("category"),
cmap,
custom_color_key, None, None
)[1]
colors= [cmap[x] forxindf.index]
fig, ax=plt.subplots(1, 1, figsize=(4,4))
ax.scatter(
df[c_x],
df[c_y],
s=df.cluster_size,
lw=1,
edgecolors='k',
c=colors,
)
# Give the plot a bit of padding so that the blobs are not truncatedax.set_xlim(
df[c_x].min()*padding,
df[c_x].max()*padding
)
ax.set_ylim(
df[c_y].min()*padding,
df[c_y].max()*padding
)
# Calculate the mean number of edges from the KNN graphifshow_edges:
g=ds.load_graph()
lines= []
widths= []
foriinset(clusts):
v=pd.Series(
clusts[g[clusts==i].tocoo().col]
).value_counts()
v=v/v.sum()
forj,kinv.to_dict().items():
ifj==i:
continuelines.append([
(df[c_x][i], df[c_y][i]),
(df[c_x][j], df[c_y][j])
])
widths.append((edge_width_multiplier*k))
lc=LineCollection(lines, linewidths=widths, color='k', zorder=0)
ax.add_collection(lc)
ifsave_nameisnotNone:
plt.savefig(save_name, dpi=300)
plt.show()
Pro-Tip: To copy the code, click the icon in the top-right corner when you hover over the code block.
Expected output:
The UMAP for this data looked like this:
Hope this helps, and feel free to reach out if you face an issue.
The text was updated successfully, but these errors were encountered: