This Jupyter Notebook performs a detailed analysis of cell segmentation and classification data from an Ovarian Whole Slide Image (WSI). The core function is to categorize regions (patches) of the WSI as Tumor or Stroma and then quantify the associated lymphocyte infiltration, resulting in the calculation of Tumor Infiltrating Lymphocytes (TIL) and Stromal Infiltrating Lymphocytes (SIL).
The script processes a WSI and its corresponding cell classification results through the following steps:
-
Input Data Loading: Loads the WSI (
.svs) usingopenslideand the cell classification data (coordinates, types) from a JSON file. -
Patch-wise Analysis: Iterates over the WSI in non-overlapping
$1024 \times 1024$ pixel patches. -
Validation: Applies image-based checks (
valid_patchfunction) to automatically skip patches that are mostly background, out-of-focus, or artifact-heavy. -
Patch Classification: For each valid patch, it counts neoplastic, connective, and inflammatory (lymphocyte) cells.
- A patch is classified as Tumor if the neoplastic cell count exceeds a minimum threshold (
TUMOR_COUNT_THRESHOLD) and is greater than the connective cell count. - A patch is classified as Stroma if the connective cell count exceeds a minimum threshold (
STROMA_COUNT_THRESHOLD) and is greater than the neoplastic cell count.
- A patch is classified as Tumor if the neoplastic cell count exceeds a minimum threshold (
-
Lymphocyte Quantification: It calculates:
- TIL: The total count of lymphocytes found within all patches classified as Tumor.
- SIL: The total count of lymphocytes found within all patches classified as Stroma.
-
Visualization: Generates a composite image for easy interpretation. This image displays:
- The original WSI thumbnail.
- The WSI thumbnail overlaid with visualization, where Tumor patches are highlighted with red rectangles and Stroma patches with blue rectangles.
- Titles on the composite image summarize the final TIL and SIL counts.
The script is configured to use the following file paths:
| Variable | Description | Example Path/Value |
|---|---|---|
wsi_path |
Path to the Whole Slide Image file. | /path/to/.../wsi.svs |
json_path |
Path to the JSON file containing cell segmentation and type data. | /path/to/.../cells.json |
font_path |
Path to a TrueType font file for image annotations. | ARIAL.TTF |
Label |
A simple string label for the WSI (used in the output title). | 'CCC' |
The main deliverable is a high-level summary visualization:
- File:
wsi_summary_visualization.png - Location: Saved to the directory specified by
output_dir. - Content: A merged image showing the WSI visualization with colored patch boundaries and a title containing the final TIL and SIL metrics.
The script also prints the final TIL and SIL counts to the console.