CSGO#

class models.CSGO(yolo_path=None, unet_path=None, gpu=False, save=False, output_dir=None, zoom=40, mpp=0.25)#

Define high-level attributes for CSGO (Cell Segmentation with Globally Optimized boundaries).

Parameters#

yolo_pathstr

Path of the HD-Yolo model pretrained weight. HD-Yolo predicts nuclei

unet_pathstr

Path of the U-Net model pretrained weight. U-Net predicts membrane

gpubool, default False

Whether to use GPU to evaluate the models

savebool, default False

Whether to save CSGO outputs (see more detailed under Notes)

output_dirstr

The path to save CSGO outputs

zoomint, default 40

The first data point needed to convert resolution and mpp

mppint, default 0.25

The second data point needed to convert resolution and mpp

Notes#

If save is enabled, 1) the cell segmentation result, where each pixel is assigned to a cell, and 2) an image showing the process CSGO will be written to output_dir.

Standard imaging equipment places 40x zoomed images at MPP = 0.25

convert_resolution_to_mpp(img_resolution=40)#

Converts the solution (e.g. 20x, 40x) to microns per pixel (MPP).

Parameters#

img_resolutionint

The resolution of the incoming patch

Returns#

new_mppint

Converted MPP based on the previously defined zoom&mpp during class init.

Example#

If 40x corresponds to 0.25 MPP (defined during init), then 20x corresponds to 0.5 MPP

find_missed_nuclei(ndi_distance, nucleus_label, cell_size)#

Given transformed distance from ndi, find the local minima and mark them as new nucleus.

Parameters#

ndi_distanceint

topology map of membrane and nucleus computed from ndi.distance_transform_edt()

nucleus_labelndarray

nucleus prediction results from HD-Staining

cell_size:

the average diameter of the cell. Used to 1) calculate minimum separation between two nucleus, and 2) the size of the artifical nucleus.

Returns#

nucleus_label: nucleus labels found by both HD-Staining and minimum local distances.

membrane_detection(patch, patch_mpp, model_mpp=0.5)#

Performs membrane prediction by first initialize the U-net, load the model weights, and evaluate the model.

Parameters#

patchndarray

The H&E stained image.

patch_mppfloat

The MPP of the image. This is used to calculate the model input size automatically.

model_mppfloat

The MPP of the model. The resolution of the images used to train the U-Net model

Returns#

membrane_maskndarray

Membrane predictions

predict_membrane_from_patch(img, patch_mpp=0.25, model_mpp=0.5)#

Helper function to perform membrane segmentation after resizing the original image

Parameters#

imgndarray

The H&E stained image.

patch_mppfloat

The MPP of the image. This is used to calculate the model input size automatically.

model_mppfloat

The MPP of the model. The resolution of the images used to train the U-Net model

Returns#

outputndarray

Membrane predictions

segment(img_path, cell_size=50, img_resolution=40)#

Performs segmentation given an image path.

Parameters#

img_pathstr

The path of the image

cell_sizeint, optional

The average diameter of the cell. Used to 1) calculate minimum separation between two nucleus, and 2) the size of the artifical nucleus, by default 50

img_resolutionint, optional

The resolution of the incoming patch, by default 40

Returns#

res_cell_segndarray

An numpy.ndarray with the original image shape. Each pixel is assigned to a cell number. No pixel will be assigned as background.

Notes#

If save is enabled, 1) the cell segmentation result, where each pixel is assigned to a cell, and 2) an image showing the process CSGO will be written to output_dir.

to_device(x, device)#

Move objects to device.

Parameters#

x

PyTorch componenets that can be directly called to load to device

devicetorch.device

The PyTorch device destination

unet_init()#

Initializes the UNet model and load the model weights

watershed(nuclei_mask, membrane_mask, cell_size)#

Performs energy-based watershed given nuclei and membrane masks

Parameters#

nuclei_maskndarray

Basin of the watershed from the segmentation result of HD-yolo. Each pixel is assigned either a nuclei group number (1 to len(nuclei)), or background (0)

membrane_maskndarray

Boundary of the watershed from the segmentation result of U-Net, generated from self.membrane_detection()

cell_sizeint

The average diameter of the cell. Used to 1) calculate minimum separation between two nucleus, and 2) the size of the artifical nucleus.

Returns#

watershed_resndarray

Result of watershed. Each pixel is assigned to a cell number. No pixel will be assigned as background.