thor.pp.WholeSlideImage

class thor.pp.WholeSlideImage(image_path, name=None, color_image=True, nuclei_seg_path=None, nuclei_seg_format=None, nuclei_remove_outlier=True, context_size='mean', external_cell_features_csv_path=None, save_dir=None)[source]

Bases: object

Whole slide image class.

New: Now support external cell features as csv file.
Parameters:
  • image_path (str) – Path to the whole slide image.

  • name (str, optional) – Name of the image.

  • color_image (bool) – Whether the image is a color image. Default is True.

  • nuclei_seg_path (str, optional) – Path to the nuclei segmentation result file.

  • nuclei_seg_format (str, optional) – Format of the nuclei segmentation result file. Can be ‘cellpose’, ‘mask_array_npz’ or ‘cellprofiler’.

  • nuclei_remove_outlier (bool, optional) – Whether to remove outlier cells. Default is True.

  • external_cell_features_csv_path (str, optional) – Pre-extracted cell features from the image. If provided, the cell features will be loaded from the csv file. Note, the externally provided cell features need to be paired with the nuclei segmentation results. The indices of the nuclei should be the segmentation labels.

  • save_dir (str, optional) – Path to save the results.

Examples

>>> from thor.pp import WholeSlideImage
>>> wsi = WholeSlideImage(
...     image_path="path/to/image.tif",
...     name="wsi_1",
...     color_image=True,)
>>> wsi.process(method="stardist", tile_size=(500, 500), min_size=10, flow_threshold=0, prob_thresh=0.1)

Methods

get_bbox

Get the bounding box of the image according to the mapped spot positions.

load_external_cell_features

Load external cell features and combine them with default features.

nuclei_segmentation

Wrapper function for nuclei segmentation thor.preprocessing.nuclei_seg.nuclei_segmentation_from_image_array().

process

Process the staining image to get the cell features and nuclei segmentation results.

split

Split the image into tiles.

static get_bbox(self, spot_adata_path)[source]

Get the bounding box of the image according to the mapped spot positions.

Parameters:

spot_adata_path (str) – Path to the spot adata object.

Returns:

  • LB (tuple) – Lower bound of the image (lower left corner).

  • UB (tuple) – Upper bound of the image (upper right corner).

load_external_cell_features(exclusive=False)[source]

Load external cell features and combine them with default features.

Parameters:

exclusive (bool, default: False) – If True, only use external features (must include ‘x’ and ‘y’ coordinates in the first two columns). If False, join external features with the default cell features.

Raises:

ValueError – If exclusive=True and the first two columns are not named ‘x’ and ‘y’.

Notes

The external CSV file is expected to have cell IDs as the index (first column). When exclusive=True, the first two columns must be ‘x’ and ‘y’. The result is saved to self.cell_features_csv_path, either replacing or extending the existing features.

nuclei_segmentation(**kwargs)[source]

Wrapper function for nuclei segmentation thor.preprocessing.nuclei_seg.nuclei_segmentation_from_image_array().

process(**kwargs)[source]

Process the staining image to get the cell features and nuclei segmentation results.

Parameters:

**kwargs – Keyword arguments for the nuclei_segmentation function.

split(split=(2, 2), output_path=None)[source]

Split the image into tiles.

Parameters:
  • split (tuple) – Number of tiles to split the image. Default is (2, 2).

  • output_path (str) – Path to save the tiles. Default is None.

Returns:

The tiles will be saved to the provided path in .png format.

Return type:

None