%matplotlib inline

Image-free segmentation vs. Image-dependent segmentation

In Bering model, image embeddings are learned from CNN models and used to capture cell boundary information from staining images, such as DAPI and membrane staining. Here we use the CosMx NSCLC data as an example to compare the performance of image-free and image-dependent segmentation.

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
# load data
df_spots_all = br.datasets.cosmx_nsclc_he()
df_spots_seg = df_spots_all[df_spots_all['labels'] != 'background'] # foreground nodes
df_spots_unseg = df_spots_all[df_spots_all['labels'] == 'background'] # background nodes

img = tiff.imread('/data/aronow/Kang/spatial/Bering/demo/bm2_cosmx_nsclc/image.tif')
channels = ['Nuclei', 'PanCK', 'Membrane']

image-free segmentation

bg = br.BrGraph(df_spots_seg, df_spots_unseg, image = None, channels = None)
br.graphs.BuildWindowGraphs(bg, n_cells_perClass = 4, window_width = 100.0, window_height = 100.0, n_neighbors = 10)
br.graphs.CreateData(bg, batch_size = 16, training_ratio = 0.8)
br.train.Training(bg)
Training node classifier:  98%|█████████▊| 49/50 [00:20<00:00,  2.97it/s]
../../../_images/a9c01c048a7302380a6434224aa1ed9fb997b9f74053aa7282a9f0855acc870a.png
Training node classifier: 100%|██████████| 50/50 [00:22<00:00,  2.24it/s]
Training edge classifier:  98%|█████████▊| 49/50 [00:23<00:00,  2.09it/s]
../../../_images/7c613694aad5d292d0cd7a8e640507db1150c88811efbca24149862a2da3c1bd.png
Training edge classifier: 100%|██████████| 50/50 [00:25<00:00,  1.97it/s]
# plot cell segmentation
random_cell = cells = random.sample(bg.segmented.index.values.tolist(), 1)[0]
br.pl.Plot_Segmentation(
    bg, 
    cell_name = random_cell,
    n_neighbors = 10, 
    zoomout_scale = 4,
    use_image = True,
    pos_thresh = 0.6,
    resolution = 0.05,
    num_edges_perSpot = 100,
    min_prob_nodeclf = 0.3,
    n_iters = 20,
)
../../../_images/d7adda957a2217972d300e2e9cae741735394eece3f8849235f5fa8d55cf238a.png ../../../_images/d7adda957a2217972d300e2e9cae741735394eece3f8849235f5fa8d55cf238a.png

image-dependent segmentation

bg = br.BrGraph(df_spots_seg, df_spots_unseg, image = img, channels = channels)
br.graphs.BuildWindowGraphs(bg, n_cells_perClass = 4, window_width = 100.0, window_height = 100.0, n_neighbors = 10)
br.graphs.CreateData(bg, batch_size = 16, training_ratio = 0.8)
br.train.Training(bg)
Training node classifier:  98%|█████████▊| 49/50 [00:20<00:00,  2.88it/s]
../../../_images/bc1c6074263d2fb48f71586565d92da6b8519b89ba72ea2e78cb405360079eb2.png
Training node classifier: 100%|██████████| 50/50 [00:22<00:00,  2.27it/s]
Training edge classifier:  98%|█████████▊| 49/50 [27:46<00:31, 31.75s/it]
../../../_images/6b5c072ecb4037145d77ba2111c9be5e6000ee5dd9b4ceefd5212808f03eba22.png
Training edge classifier: 100%|██████████| 50/50 [28:32<00:00, 34.24s/it]
# plot cell segmentation
br.pl.Plot_Segmentation(
    bg, 
    cell_name = random_cell,
    n_neighbors = 10, 
    zoomout_scale = 4,
    use_image = True,
    pos_thresh = 0.6,
    resolution = 0.05,
    num_edges_perSpot = 100,
    min_prob_nodeclf = 0.3,
    n_iters = 20,
)
../../../_images/d68574b723e57138d352d91f43fb2d29802be6da1f39a278890127ba76581cd2.png ../../../_images/d68574b723e57138d352d91f43fb2d29802be6da1f39a278890127ba76581cd2.png