%matplotlib inline

Graph Convolutional Network Structure

The structure of Grah Convolutional Network (GCN) can be fine-tuned. It includes number of layers, layer sizes and other hyperparameters. Here we use the Nanostring CosMx data as an example to show how to fine-tune the hyperparameters of GCN structure.

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
Downloading dataset `cosmx_nsclc_he` from `https://figshare.com/ndownloader/files/41409093` as `None.tsv`

Train deep Graph network

# image-free segmentation
bg_dp = br.BrGraph(df_spots_seg, df_spots_unseg)
br.graphs.BuildWindowGraphs(bg_dp, n_cells_perClass = 15, window_width = 100.0, window_height = 100.0, n_neighbors = 10)
br.graphs.CreateData(bg_dp, batch_size = 16, training_ratio = 0.8)
node_gcnq_hidden_dims = [512, 256, 128, 128, 64, 32, 16] # GCN structure
node_mlp_hidden_dims = [16, 32, 32] # MLP as last layers before node clf
br.train.Training(
    bg_dp,
    node_gcnq_hidden_dims = node_gcnq_hidden_dims,
    node_mlp_hidden_dims = node_mlp_hidden_dims,
    edge_rbf_start = 0,
    edge_rbf_stop = 128,
    edge_rbf_n_kernels = 64,
    node_epoches = 50, 
)
Training node classifier:  98%|█████████████████████████████████████████████████████████████████████████████████████████████████  | 49/50 [02:07<00:02,  2.23s/it]
../../../_images/a1ec4dbb95ee2e8de6421ccfc83a8cd5bf0465bec89f36bebe45c4687648fefd.png
Training node classifier: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [02:15<00:00,  2.70s/it]
Training edge classifier:  98%|█████████████████████████████████████████████████████████████████████████████████████████████████  | 49/50 [01:36<00:01,  1.81s/it]
../../../_images/7c41300ea230bfe8d9b0a920fbc83c042cf56e70800f6b054a7ff1101c99e282.png
Training edge classifier: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [01:40<00:00,  2.02s/it]

Train shallow Graph network

# image-free segmentation
bg_sh = br.BrGraph(df_spots_seg, df_spots_unseg)
br.graphs.BuildWindowGraphs(bg_sh, n_cells_perClass = 15, window_width = 100.0, window_height = 100.0, n_neighbors = 10)
br.graphs.CreateData(bg_sh, batch_size = 16, training_ratio = 0.8)
node_gcnq_hidden_dims = [128, 16] # GCN structure
node_mlp_hidden_dims = [16, 32, 32] # MLP as last layers before node clf
br.train.Training(
    bg_sh,
    node_gcnq_hidden_dims = node_gcnq_hidden_dims,
    node_mlp_hidden_dims = node_mlp_hidden_dims,
    edge_rbf_start = 0,
    edge_rbf_stop = 128,
    edge_rbf_n_kernels = 64,
    node_epoches = 50, 
)
Training node classifier:  98%|█████████████████████████████████████████████████████████████████████████████████████████████████  | 49/50 [00:38<00:00,  1.64it/s]
../../../_images/485c6a156d4d7f9e1190550b1df36bd8f60a3577dfe201bbbad3f5bcbbb2be94.png
Training node classifier: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:41<00:00,  1.21it/s]
Training edge classifier:  98%|█████████████████████████████████████████████████████████████████████████████████████████████████  | 49/50 [00:50<00:00,  1.04it/s]
../../../_images/da13b458a1d0eec09d1c1273e20900fd82b9c26e9cb24c60c652327ddad8ba9d.png
Training edge classifier: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:54<00:00,  1.09s/it]

Visualizing deep model

# randomly select a cell
random_cells = cells = random.sample(bg_dp.segmented.index.values.tolist(), 1)[:3] # 3 random cells
for random_cell in random_cells:
    _,_,_ = br.pl.Plot_Classification(
        bg_dp, 
        cell_name = random_cell,
        n_neighbors = 10, 
        zoomout_scale = 4,
    )
../../../_images/f881332acb1aad31b13794c59c273cfcceffc96acdfcb8bc9714df0691b23f6b.png

Visualize shallow models

for random_cell in random_cells:
    _,_,_ = br.pl.Plot_Classification(
        bg_sh, 
        cell_name = random_cell,
        n_neighbors = 10, 
        zoomout_scale = 4,
    )
../../../_images/c369e5f951a761872d19ed233c358822e7324077dcd11fb314115fb40c92f4fe.png