%matplotlib inline

Train Node Classification Model without Graph

Node classification model can be realised with neural networks with Fully-Connected Layers (FCNs) without using graph models. However, in our manuscript, we demonstrated that the graph model based on Neighborhood Gene Component has the advantage of message passing, so that the predicted labels of neighboring transcripts are similar. Here we use 10x Xenium DCIS data [Janesick et al., 2022] as an example to demonstrate the advantage of graph models in the node (transcript) classification.

Import packages & data

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

import Bering as br

load data

df_spots_all = br.datasets.xenium_dcis_janesick()
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

Create Bering object and training data

# image-dependent segmentation
bg = br.BrGraph(df_spots_seg, df_spots_unseg)
br.graphs.BuildWindowGraphs(bg, n_cells_perClass = 10, window_width = 15.0, window_height = 15.0, n_neighbors = 30)
br.graphs.CreateData(bg, batch_size = 16, training_ratio = 0.8)

Training (without Graph)

br.train.Training(bg, baseline = True)
Training node classifier:  98%|█████████▊| 49/50 [00:38<00:00,  1.65it/s]
../../../_images/1c33ecbae518da9e1fb9b2c1a82ec3cb8707f1d90b65527a34a566223a88ca22.png
Training node classifier: 100%|██████████| 50/50 [00:40<00:00,  1.22it/s]
Training edge classifier:  98%|█████████▊| 49/50 [01:08<00:01,  1.22s/it]
../../../_images/59b8f5ca6b9c4264e7c13c291daab71141615a58ca458bb9511f5696d5b2eefc.png
Training edge classifier: 100%|██████████| 50/50 [01:11<00:00,  1.42s/it]

Training (with graph)

# image-dependent segmentation
bg_graph = br.BrGraph(df_spots_seg, df_spots_unseg)
br.graphs.BuildWindowGraphs(bg_graph, n_cells_perClass = 10, window_width = 15.0, window_height = 15.0, n_neighbors = 30)
br.graphs.CreateData(bg_graph, batch_size = 16, training_ratio = 0.8)
br.train.Training(bg_graph, baseline = False)
Training node classifier:  98%|█████████▊| 49/50 [01:28<00:01,  1.44s/it]
../../../_images/7739583bc6cb03d421a76f3463dc6eb11896c0a68bfbdfeb196a9f492dd4f133.png
Training node classifier: 100%|██████████| 50/50 [01:35<00:00,  1.90s/it]
Training edge classifier:  98%|█████████▊| 49/50 [01:36<00:01,  1.74s/it]
../../../_images/3ae7dc94193028cfbbd20e9f2946169974ef9b86d760c6b8b9a43d8c4cc76155.png
Training edge classifier: 100%|██████████| 50/50 [01:40<00:00,  2.01s/it]

Comparison of two predictions

# randomly select a cell
random.seed(42)
random_cell = cells = random.sample(bg.segmented.index.values.tolist(), 1)[0]
# plot node classification
_,_,_ = br.pl.Plot_Classification(
    bg, 
    cell_name = random_cell,
    n_neighbors = 30, 
    zoomout_scale = 8,
)
../../../_images/46c982f1ffa8e5a0be80b5a9c8fc749cbe54f7f8f17efa4c932d80ef21723ecc.png
# plot node classification
_,_,_ = br.pl.Plot_Classification(
    bg_graph, 
    cell_name = random_cell,
    n_neighbors = 30, 
    zoomout_scale = 8,
)
../../../_images/069859465d500523f213d72b72bb7db49a6b5683f9c78429f1f216f0975bbce8.png