%matplotlib inline

Visualize Cell Segmentation Performance

This tutorial shows how to apply Bering to segment individual cells in the spatial data.

We conduct the visualization based on the Bering applications in MERFISH Cortex data (Zhang et al.), and Xenium data of Ductual Carcinoma In Situ (DCIS). For the training procedure, please refer to the documentation here: xxx and xxx.

In this documentation, we load the saved model directly and focus on the visualization part.

Import packages & data

import random
import numpy as np
import pandas as pd
import tifffile as tiff
import matplotlib.pyplot as plt 

import Bering as br

Visualize segmented cells in MERFISH data

In the analyzed data from the MERFISH Cortex dataset (Zhang et al.), there were xxx cells identified by the authors using Watershed in slice xxx. After identifying real signals, we identified individual cells and found more cells with satisfying boundaries in our prediction.

load model

import pickle 
with open('models/MERFISH_cortex_model_simplified.pl', 'rb') as f:
    bg = pickle.load(f)

Visualizing segmentation in a window

In the visualization, individual cells are assigned with unique colors. Background noises were not used for segmentation and colored in gray.

# randomly select cells
random_cells = cells = random.sample(bg.segmented.index.values.tolist(), 1)[:3]

# plot node classification
for random_cell in random_cells:
    print(f'Plotting segmentation for the region around cell {random_cell}')
    br.pl.Plot_Segmentation(
        bg, 
        cell_name = random_cell,
        n_neighbors = 30, 
        zoomout_scale = 8,
        pos_thresh = 0.6,
        resolution = 0.05,
        num_edges_perSpot = 300,
        min_prob_nodeclf = 0.3,
        n_iters = 10,
    )
Plotting segmentation for the region around cell 930
../../../_images/b81e3ff40f38f576925b8a2143f6c0aa87c37f8ca988a2e94d26638e692c9cb5.png

compare number of cells between original and Bering segmentation

The segmentation results were generated by Watershed in the original paper, where we can easily observed a lot of unsegmented real signals. We compare Bering segmentation output with the original segmentation using number of segmentation cells and ratio of recalled transcripts as the metrics.

# conduct segmentation for whole slice
br.tl.node_classification(bg, bg.spots_all.copy(), n_neighbors = 30, )
br.tl.cell_segmentation(bg)

# number of cells
ncells_ori = len(bg.spots_all['segmented'].unique())
ncells_pred = len(bg.spots_all['predicted_cells'].unique())

# number of segmented transcripts
ntrans_ori = len(bg.spots_all[bg.spots_all['labels'] != 'background'])
ntrans_pred = len(bg.spots_all[bg.spots_all['predicted_node_labels'] != 'background'])

# draw plots
fig, axes = plt.subplots(1, 2, figsize = (10, 5))
axes[0].bar(['Original', 'Predicted'], [ncells_ori, ncells_pred])
axes[0].set_title('Number of Cells')
axes[1].bar(['Original', 'Predicted'], [ntrans_ori, ntrans_pred])
axes[1].set_title('Number of Segmented Transcripts')
fig.tight_layout()
../../../_images/0c56cc6da25bb7d6a6480ce4262e6aad2b8558b974df561da3508ccffccd2e0d.png

Visualize segmented cells in Breast cancer

The Xenium data of ductal carcinoma in-situ (DCIS) is a less noisy data. In the Bering application, we segmented individual cells in the tumor microenvironment.

load model

with open('models/Xenium_dcis_model_simplified.pl', 'rb') as f:
    bg = pickle.load(f)
# randomly select a cell
random_cells = cells = random.sample(bg.segmented.index.values.tolist(), 1)[:3]

# plot node classification
for random_cell in random_cells:
    print(f'plotting segmentation for the region around cell {random_cell}')
    br.pl.Plot_Segmentation(
        bg, 
        cell_name = random_cell,
        n_neighbors = 10, 
        zoomout_scale = 4,
        pos_thresh = 0.6,
        resolution = 0.10,
        num_edges_perSpot = 300,
        min_prob_nodeclf = 0.3,
        n_iters = 10,
    )
plotting segmentation for the region around cell 1202
../../../_images/14ea64d51cb1b35d4732144e515e8e58a90dd7318c5634e6bf3b429b69ff1eaf.png