MegaTileExtractor class, which groups individual tile positions from a Whole Slide Image into mega tiles — fixed- size grids of rows × cols tiles. It builds on the tile-level tissue filtering from TileExtractor and adds a second filtering stage: only mega tiles where the fraction of tissue-positive tiles falls within a configurable [min_valid_tiles_ratio, max_valid_tiles_ratio] band are kept.
The class follows the same scikit-learn–style lifecycle as TileExtractor:
- Configure — instantiate with a
MegaTileSpec, optional tissue mask, and threshold. - Fit — call
fitwith an openWSIReader. - Extract — call
extract(or the shortcutfit_extract). Results are stored inmegatile_regions_and also returned. - Export — call
to_csvorto_jsonto persist the results.
MegaTileExtractor
- Tile-level filtering — a binary tissue mask and
mask_thresholddetermine which individual tile positions contain sufficient tissue. - Mega-tile-level filtering — only mega tiles where the fraction of
valid (tissue-positive) tiles falls within
[min_valid_tiles_ratio, max_valid_tiles_ratio]are retained.
TileExtractor:
Or via method chaining:
Configuration attributes (set at init):
-
megatile_specMegaTileSpec - Mega-tile grid shape, per-tile spec, layout, stride, and valid-ratio bounds. -
masknp.ndarray | None - Binary tissue mask. -
mask_providerTissueMaskProvider | None - Per-slide mask generator. -
mask_thresholdfloat | None - Per-tile tissue fraction threshold. -
save_maskbool - Whether to store per-tile masks. -
max_megatilesint | None - Optional cap on returned mega tiles. Fitted attributes (populated byfit/extract):
source_WSIReader - Bound WSI reader.slide_name_str - Stem of the slide filename.megatile_regions_List[MegaTileRegion] - Extracted mega tiles.
fit
mask_provider is set, its generate method is called to create a per-slide tissue mask.
An open WSI reader.
MegaTileExtractor-selffor method chaining.
extract
- Reference-level setup — as in
TileExtractor. - Mask scaling — resize tissue mask to the reference level.
- Tile-spec scaling — convert tile dimensions to reference-level pixels.
- Per-tile tissue evaluation — build an integral image and compute the tissue fraction for every candidate tile position.
- Mega-tile grouping — slide a
rows × colswindow (in tile units) across the tile grid, collect per-tile validity, and filter by[min_valid_tiles_ratio, max_valid_tiles_ratio].
megatile_regions_ and returned.
An open WSI reader. When given, the extractor is fitted automatically. Otherwise
fit must have been called.List[MegaTileRegion]- Mega-tile regions that pass filtering.
RuntimeError- If no source is provided andfithas not been called.
fit_extract
fit + extract in one call.
An open WSI reader.
List[MegaTileRegion]- Extracted mega-tile regions.
to_csv
megatile_index, megatile_top, etc.) allow grouping.
Destination directory (created if needed).
Override the stored regions. Defaults to
megatile_regions_.Path- Path of the written CSV file.
RuntimeError- If no regions are available.
to_json
tiles array with per-tile metadata.
Destination directory (created if needed).
Override the stored regions.
JSON pretty-printing indent.
Path- Path of the written JSON file.
RuntimeError- If no regions are available.

