cellmil.segmentation.cell_segmenter

Functions

get_cell_position(bbox[, patch_size])

Get cell position as a list

get_cell_position_marging(bbox[, ...])

Get the status of the cell, describing the cell position

get_edge_patch(position, row, col)

Classes

CellPostProcessor(cell_list, logger)

CellSegmenter(config)

Class for cell instance segmentation in whole slide images.

class cellmil.segmentation.cell_segmenter.CellSegmenter(config: CellSegmenterConfig)[source]

Bases: object

Class for cell instance segmentation in whole slide images.

Features reliable multiprocessing with Pool.imap and automatic fallback to sequential processing if multiprocessing fails.

__init__(config: CellSegmenterConfig) None[source]
set_wsi(wsi_path: str | pathlib.Path, patched_slide_path: str | pathlib.Path) None[source]

Set the WSI path and patched slide path for segmentation.

_setup_wsi() None[source]

Setup the whole slide image for segmentation.

_download_models() None[source]

Download model checkpoints if they don’t exist.

_load_model() None[source]

Load model and checkpoint

_load_inference_transforms()[source]

Load the inference transformations from the run_configuration

_get_wsi_dimensions() tuple[int, int][source]

Get the dimensions (width, height) of the WSI at level 0.

Returns:

WSI dimensions as (width, height)

Return type:

tuple[int, int]

_are_contour_coordinates_valid(contour_global: ndarray[Any, Any]) bool[source]

Check if contour coordinates are within the WSI bounds.

Parameters:

contour_global (np.ndarray) – Global contour coordinates, shape (N, 2) where each point is [x, y]

Returns:

True if all contour points are within WSI bounds, False otherwise

Return type:

bool

process() None[source]

Process WSI file

get_cell_predictions(predictions: dict[str, torch.Tensor], magnification: float | int = 40) list[dict[numpy.int32, dict[str, Any]]] | list[dict[int, dict[str, Any]]][source]

Get cell predictions from model output

Parameters:
  • predictions (torch.Tensor) – Model output

  • magnification (float, optional) – Magnification of the WSI. Defaults to 40.0.

Returns:

Dictionary with cell predictions

Return type:

dict[str, Any]

post_process_edge_cells(cell_list: list[dict[str, Any]]) list[Any][source]

Use the CellPostProcessor to remove multiple cells and merge due to overlap

Parameters:

cell_list (List[dict]) – List with cell-dictionaries. Required keys: * bbox * centroid * contour * type_prob * type * patch_coordinates * cell_status * offset_global

Returns:

List with integers of cells that should be kept

Return type:

List[int]

cellmil.segmentation.cell_segmenter.get_cell_position(bbox: ndarray[Any, Any], patch_size: int = 1024) list[int][source]

Get cell position as a list

Entry is 1, if cell touches the border: [top, right, down, left]

Parameters:
  • bbox (np.ndarray) – Bounding-Box of cell

  • patch_size (int, optional) – Patch-size. Defaults to 1024.

Returns:

List with 4 integers for each position

Return type:

List[int]

cellmil.segmentation.cell_segmenter.get_cell_position_marging(bbox: ndarray[Any, Any], patch_size: int = 1024, margin: int = 64) Literal[0, 1, 2, 3, 4, 5, 6, 7, 8][source]

Get the status of the cell, describing the cell position

A cell is either in the mid (0) or at one of the borders (1-8)

# Numbers are assigned clockwise, starting from top left # i.e., top left = 1, top = 2, top right = 3, right = 4, bottom right = 5 bottom = 6, bottom left = 7, left = 8 # Mid status is denoted by 0

Parameters:
  • bbox (np.ndarray) – Bounding Box of cell

  • patch_size (int, optional) – Patch-Size. Defaults to 1024.

  • margin (int, optional) – Margin-Size. Defaults to 64.

Returns:

Cell Status

Return type:

int

cellmil.segmentation.cell_segmenter.get_edge_patch(position: list[int], row: int, col: int)[source]
class cellmil.segmentation.cell_segmenter.CellPostProcessor(cell_list: list[dict[str, Any]], logger: Logger)[source]

Bases: object

__init__(cell_list: list[dict[str, Any]], logger: Logger) None[source]

Post-Processing a list of cells from one WSI

Parameters:
  • cell_list (List[dict]) – List with cell-dictionaries. Required keys: * bbox * centroid * contour * type_prob * type * patch_coordinates * cell_status * offset_global

  • logger (logging.Logger) – Logger

post_process_cells() DataFrame[source]

Main Post-Processing coordinator, entry point

Returns:

DataFrame with post-processed and cleaned cells

Return type:

pd.DataFrame

_clean_edge_cells() DataFrame[source]

Create a DataFrame that just contains all margin cells (cells inside the margin, not touching the border) and border/edge cells (touching border) with no overlapping equivalent (e.g, if patch has no neighbour)

Returns:

Cleaned DataFrame

Return type:

pd.DataFrame

_remove_overlap(cleaned_edge_cells: DataFrame) DataFrame[source]

Remove overlapping cells from provided DataFrame

Parameters:

cleaned_edge_cells (pd.DataFrame) – DataFrame that should be cleaned

Returns:

Cleaned DataFrame

Return type:

pd.DataFrame