Skip to content

generate_tile_mask

Converts categorical tile labels to a slide image mask. This mask can be used for feature extraction and spatial analysis.

Args: slide_urlpath (str): url/path to slide image (virtual slide formats compatible with openslide, .svs, .tif, .scn, ...) tiles_urlpath (str): url/path to valid SlideTiles table label_cols (List[str]): list of label columns in the input_slide_tiles table to generate the mask with output_urlpath (str): output url/path prefix storage_options (dict): storage options to pass to reading functions output_storage_options (dict): storage options to pass to writing functions

Returns:

Name Type Description
dict

output properties

Source code in src/luna/pathology/cli/generate_tile_mask.py
@timed
@save_metadata
def cli(
    slide_urlpath: str = "???",
    tiles_urlpath: str = "",
    label_cols: List[str] = "???",  # type: ignore
    output_urlpath: str = ".",
    storage_options: dict = {},
    output_storage_options: dict = {},
):
    """Converts categorical tile labels to a slide image mask. This mask can be used for feature extraction and spatial analysis.

     Args:
        slide_urlpath (str): url/path to slide image (virtual slide formats compatible with openslide, .svs, .tif, .scn, ...)
        tiles_urlpath (str): url/path to valid SlideTiles table
        label_cols (List[str]): list of label columns in the input_slide_tiles table to generate the mask with
        output_urlpath (str): output url/path prefix
        storage_options (dict): storage options to pass to reading functions
        output_storage_options (dict): storage options to pass to writing functions

    Returns:
        dict: output properties

    """
    config = get_config(vars())

    logger.info("Reading SlideTiles")
    with open(config["tiles_urlpath"], "rb", **config["storage_options"]) as of:
        tiles_df = pd.read_parquet(of).reset_index().set_index("address")

    with open(config["slide_urlpath"], **config["storage_options"]) as of:
        slide = tiffslide.TiffSlide(of)
        slide_width = slide.dimensions[0]
        slide_height = slide.dimensions[1]

    mask_arr, mask_values = convert_tiles_to_mask(
        tiles_df,
        slide_width,
        slide_height,
        config["label_cols"],
        config["output_urlpath"],
        config["output_storage_options"],
    )

    fs, output_path = fsspec.core.url_to_fs(config["output_urlpath"])

    slide_mask = Path(output_path) / "tile_mask.tif"
    properties = {
        "slide_mask": fs.unstrip_protocol(str(slide_mask)),
        "mask_values": mask_values,
        "mask_size": mask_arr.shape,
    }
    logger.info(properties)
    return properties