Skip to content

Instantly share code, notes, and snippets.

@tulerpetontidae
Created June 4, 2024 08:20
Show Gist options
  • Save tulerpetontidae/c993419dde0e36057749ec3f48e2f949 to your computer and use it in GitHub Desktop.
Save tulerpetontidae/c993419dde0e36057749ec3f48e2f949 to your computer and use it in GitHub Desktop.
function to plot outline over a region in visium data
# install alpha_shapes from https://github.com/panosz/alpha_shapes
# adata.obsm['region_inside'] contains a (n_visium_spots, k_regions) binary array,
# where 1 indicates visium spot beplonging to a given regions
# adata.obsm['spatial'] contains position of each of n_visium_spots
from alpha_shapes import Alpha_Shaper, plot_alpha_shape
from tqdm import tqdm
def plot_visium_region_outline(adata):
alpha_scaling=1
outline_list = []
for i in tqdm(range(adata.obsm['region_inside'].shape[1]-1)):
try:
nodule_points = adata.obsm['spatial'][adata.obsm['region_inside'][:,i] == 1]
if len(nodule_points) != 0:
#double
new_coord_hex = []
for j in range(nodule_points.shape[0]):
new_coord_hex.append(np.array(hexagon_points(nodule_points[j], 198/2+16)))
new_coord_hex = filter_near_duplicates(np.array([x for s in new_coord_hex for x in s]))
shaper = Alpha_Shaper(new_coord_hex)
try:
alpha = 12
alpha_shape = shaper.get_shape(alpha=alpha)
try:
outline_list.append(alpha_shape.boundary.coords.xy)
except NotImplementedError:
alpha = 5
alpha_shape = shaper.get_shape(alpha=alpha)
outline_list.append(alpha_shape.boundary.coords.xy)
except AttributeError:
alpha = 2
alpha_shape = shaper.get_shape(alpha=alpha)
outline_list.append(alpha_shape.boundary.coords.xy)
# outline_list.append(outwords_points[hull].T)
# plt.plot(*outwords_points[hull].T, c='k')
# plt.scatter(*nodule_points.T, c='grey')
else:
print('empty ' + str(i))
except IndexError:
print(i)
if ax is None:
fig, ax = plt.subplots(figsize=(8,8))
for i in range(len(outline_list)):
ax.fill(*outline_list[i], fill=False, edgecolor='black', lw=1, zorder=3)
ax.set_aspect('equal')
ax.spines[['right', 'top', 'left', 'bottom']].set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment