Skip to content

Instantly share code, notes, and snippets.

@grst
Created August 2, 2023 13:31
Show Gist options
  • Save grst/c06d0f5b66ca972553b8e311dd778609 to your computer and use it in GitHub Desktop.
Save grst/c06d0f5b66ca972553b8e311dd778609 to your computer and use it in GitHub Desktop.
Get bounding box for squidpy spatial plot to be equivalent to the scanpy.pl.spatial
def get_bbox(adata, library_id, *, library_key="sample", img_key="hires", margin=50):
adata = adata[adata.obs[library_key] == library_id, :]
coords = adata.obsm["spatial"]
scale_factor = adata.uns["spatial"][library_id]["scalefactors"][f"tissue_{img_key}_scalef"]
return tuple(
np.array(
[
np.min(coords[:, 0]) - margin,
np.min(coords[:, 1]) - margin,
np.max(coords[:, 0]) + margin,
np.max(coords[:, 1]) + margin,
]
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment