cellmil.segmentation

class cellmil.segmentation.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]

Modules