跳轉至

pyHisto.utils (legacy)

To be deprecated

See Legacy modules. Ingestion is via tanoa OME-Zarr and histo.open_slide; the legacy CZI readers return BGR.

log

log(msg)

Print a message prefixed with a wall-clock timestamp -- lets you see how long each phase of a long-running script actually took from its console/log output, without relying on tqdm timers (which reset per bar and don't compose across phases within one process). flush=True since stdout is fully block-buffered (not line-buffered) whenever it's piped rather than a TTY (e.g. through | tee), so without an explicit flush these can sit invisible in the process's internal buffer for a long time.

ensure_zarr_root_group

ensure_zarr_root_group(path, zarr_format=3)

Idempotently ensure path is a valid zarr group, creating it if it doesn't exist yet. Used to consolidate a sample's raw/psr/mask/collagen arrays as named children of one shared zarr store (e.g. data.zarr/raw, data.zarr/psr, ...) instead of separate top-level .zarr directories -- zarr.create_group() itself raises if the group already exists (no exist_ok flag), so callers that may run this more than once (e.g. once per pipeline stage writing into the same store) need this check-first wrapper rather than calling create_group() directly.

is_valid_file_or_directory

is_valid_file_or_directory(path)

argparse type= validator: check that a given path exists (file or directory).

tiled_resize

tiled_resize(image, target_size=None, out_tile_size=2048, anti_aliasing=True, verbose=False)

create_circular_kernel

create_circular_kernel(radius)

get_tissue_mask

get_tissue_mask(image, subsample=-1, background_offset=0.1, verbose=True, closing=None, fill_holes=None, dilate=None, erode=None)

get_tissue_mask_geojson

get_tissue_mask_geojson(gdf, image_shape, scaling_factor=1.0)

get_tissue_mask_geojson_from_file

get_tissue_mask_geojson_from_file(path, image_shape, scaling_factor=1.0)

mask_to_geojson

mask_to_geojson(mask, out_path=None, scaling_factor=1.0, min_area=0, verbose=True)

Convert a binary/label mask into vector polygon ROIs (inverse of get_tissue_mask_geojson): raster mask -> shapely polygons -> GeoDataFrame, optionally written out as a GeoJSON file loadable in QuPath/Fiji.

For a whole-slide-scale binary mask, features.shapes() can yield a very large number of small disconnected regions (e.g. fragmented collagen fibres), so polygons are scaled and area-filtered incrementally as they are generated (skipping min_area rejects before they ever become GeoDataFrame rows) rather than building the full unfiltered list first -- this bounds peak memory to roughly the surviving polygons, not every raw region. The source mask is passed to rasterio as uint8 (its natural dtype for a 0/1 mask), not upcast to int32, to avoid needlessly quadrupling memory for a full-resolution array.

Args: mask: 2D array. Non-zero pixels are treated as foreground and vectorized into one polygon per 4-connected region. out_path: If given, write the resulting polygons to this path as GeoJSON. scaling_factor: Scale applied to polygon coordinates before returning/ writing, e.g. to map a mask computed on a downsampled image back to full-resolution image coordinates. min_area: Discard polygons with area (in scaled coordinate units) below this threshold, e.g. to drop single-pixel noise. verbose: Show a live tqdm progress readout (polygon count/rate) while extracting -- there is no known total up front (it depends on how fragmented the mask is), so this reports counts, not a percentage.

Returns: geopandas.GeoDataFrame with one row per connected foreground region.

get_tissue_mask_multiotsu

get_tissue_mask_multiotsu(image, subsample=-1, closing=None, fill_holes=5, dilate=None, erode=None, otsu_classes=3, otsu_level=1, verbose=True)