Note

This page was generated from 1_auxseg_livewire.ipynb. Interactive online version: Colab badge. Some tutorial content may look better in light mode.

1.Auxiliary segmentation (AuxSeg): an interactive tool for ROI segmentation#

AuxSeg is designed to select the regions of interest (e.g., an entire tissue or a tumor core) in any arbitrary geometrical shape based on simple mouse clicks (instead of a pure-manual boundary drawing).

AuxSeg is implemented with three modes:

(1) Livewire mode (a simplified magic wand tool)

This allows the user to compute a ‘best’ path, with a minimum color gradient cost (see technicals), from a seed point to the next seed point. When these seed points land on the boundaries of a tissue in sequence, the livewire will snap on the tissue boundaries in the image.

Related mouse and keyboard operation:

  1. Plot a seed point: Left mouse button single click;

  2. Draw a line: Mouse movement. In the livewire mode, the predicted path will be pre-showed on the image.

(2) Straight-line mode

This allows the user to draw the straight line between two seed points, when the livewire does not give the accurate prediction along some fuzzy edges, or the user just wants a straight regular border.

Related mouse and keyboard operation:

  1. Switch to straight-line mode: Keyboard ‘s’.

(3) Withdraw mode

This allows the user to cancel the last operation.

Related mouse and keyboard operation:

  1. Withdraw: Keyboard ‘z’.

Demo video#

[12]:
import io
import base64
from IPython.display import HTML
video_path = "./screenshot/auxseg_livewire.mp4"
video = io.open(video_path, 'r+b').read()
encoded = base64.b64encode(video)
HTML(
    data=
    '''
    <video width="350" height="460" controls>
    <source src="data:video/mp4;base64,{0}" type="video/mp4" />
    </video>
    '''.format(encoded.decode('ascii'))
)
[12]:

Example: Tissue Segmentation#

One typical application scenario of AuxSeg is to segment the entire tissue and remove background noises* from a spatial transcriptomic data.

Such noises are usually heterogenetic, being derived from the diffused RNAs at the different edges of the tissue, preserving their local information of the adjacent tissue regions on either total expression level or RNA profiling signals. It challenges the performance of the global and automated segmentation algorithms.

Therefore, AuxSeg provides practical assistance to outline the tissue boundaries manually, while not completely manually, with the assistance of certain automatic pathfinding algorithm.

The performance of AuxSeg is demonstrated in our tutorial video:https://www.dropbox.com/s/oy87fpazivg85iw/livewire_tissue_seg.mp4?dl=0

AuxSeg is in beta version. Any comments or suggestions are welcome.

  • The noise here refers to the free RNAs that are, after tissue permeabilization, diffused beyond the boundaries of the original tissue, captured, sequenced, and determined as false positive expressed by the out-of-boundary RNA probes.

[ ]:
# import packages
import spateo as st
import numpy as np
import matplotlib.pyplot as plt


# raw binning data, which contains spot/bins from the whole chip
# bin50_h5ad: https://www.dropbox.com/s/vtapwsccpi885l2/mousebrain_bin50_raw.h5ad?dl=0
fname_bin50 = "mousebrain_bin50_raw.h5ad"
adata_bin50 = st.sample_data.mousebrain(fname_bin50)


# rescale spatial coordinates to generate UMI image
coor = np.column_stack((adata_bin50.obsm['spatial'], adata_bin50.X.sum(axis=1).A)).astype('int')
coor[:, 0] = ((coor[:, 0] - 25) / 50).astype('int')
coor[:, 1] = ((coor[:, 1] - 25) / 50).astype('int')
img = np.zeros(shape=[max(coor[:, 0]) + 1, max(coor[:, 1]) + 1], dtype='int')
for line in coor:
    img[line[0], line[1]] = 20 if line[2] > 2000 else int(line[2] / 100)


# perform interactive tissue cut (interactive process, see tutorial video)
tissue_seg = st.pp.auxseg.LiveWireSegmentation(img, smooth_image=False, threshold_gradient_image=False)
plt.scatter(coor[:, 1], coor[:, 0], c=coor[:, 2], s=30)
tissue_seg.connect()
plt.show()


# subset adata to tissue area only
tissue_area = [x.tolist() in tissue_seg.rst.tolist() for x in adata_bin50.obsm['spatial'].astype('int')]
adata_bin50 = adata_bin50[tissue_area,: ]
adata_bin50