AnnotationRenderer#

class AnnotationRenderer(score_prop=None, mapper=None, where=None, score_fn=<function AnnotationRenderer.<lambda>>, max_scale=8, zoomed_out_strat=10000, thickness=-1, edge_thickness=1, secondary_cmap=None, blur_radius=0, score_prop_edge=None)[source]#

Renderer containing information and methods to render annotations from an AnnotationStore to a tile.

Parameters:
  • score_prop (str) – A key that is present in the properties of annotations to be rendered that will be used to color rendered annotations.

  • mapper (str, Dict or List) – A dictionary or colormap used to color annotations according to the value of properties[score_prop] of an annotation. Should be either a matplotlib colormap, a string which is a name of a matplotlib colormap, a dict of possible property {value: color} pairs, or a list of categorical property values (in which case a dict will be created with a random color generated for each category)

  • where (str or Callable) – a callable or predicate which will be passed on to AnnotationStore.query() when fetching annotations to be rendered (see AnnotationStore for more details)

  • score_fn (Callable) – an optional callable which will be called on the value of the property that will be used to generate the color before giving it to colormap. Use it for example to normalise property values if they do not fall into the range [0,1], as matplotlib colormap expects values in this range. i.e roughly speaking annotation_color=mapper(score_fn(ann.properties[score_prop]))

  • max_scale (int) – downsample level above which Polygon geometries on crowded tiles will be rendered as a bounding box instead

  • zoomed_out_strat (int, str) – strategy to use when rendering zoomed out tiles at a level above max_scale. Can be one of ‘decimate’, ‘scale’, or a number which defines the minimum area an abject has to cover to be rendered while zoomed out above max_scale.

  • thickness (int) – line thickness of rendered contours. -1 will render filled contours.

  • edge_thickness (int) – line thickness of rendered edges.

  • secondary_cmap (dict [str, str, cmap])) – a dictionary of the form {“type”: some_type, “score_prop”: a property name, “mapper”: a matplotlib cmap object}. For annotations of the specified type, the given secondary colormap will override the primary colormap.

  • blur_radius (int) – radius of gaussian blur to apply to rendered annotations.

Methods

get_color

Get the color for an annotation.

render_annotations

Render annotations within given bounds.

render_by_type

Render annotation appropriately to its geometry type.

render_line

Render a line annotation onto a tile using cv2.

render_multipoly

render a multipolygon annotation onto a tile using cv2

render_poly

Render a polygon annotation onto a tile using cv2.

render_pt

Render a point annotation onto a tile using cv2.

to_tile_coords

Return coords relative to top left of tile, as array suitable for cv2.

get_color(annotation, edge=False)[source]#

Get the color for an annotation. :param annotation: Annotation to get color for. :type annotation: Annotation

Returns:

A color tuple (rgba).

Return type:

tuple

Parameters:

annotation (Annotation) –

render_annotations(store, bounds, scale, res=1, border=0)[source]#

Render annotations within given bounds.

This gets annotations as bounding boxes or geometries according to zoom level, and renders them. Large collections of small annotation geometries are decimated if appropriate.

Parameters:
  • rgb (np.ndarray) – The image to render the annotation on.

  • bound_geom (Polygon) – A polygon representing the bounding box of the tile.

  • scale (float) – The scale at which we are rendering the tile.

  • store (AnnotationStore) –

  • bounds (Tuple[float, float, float, float]) –

  • res (int) –

  • border (int) –

Returns:

The tile with the annotations rendered.

Return type:

np.ndarray

render_by_type(tile, annotation, top_left, scale)[source]#

Render annotation appropriately to its geometry type.

Parameters:
  • tile (np.ndarray) – The rgb(a) tile image to render the annotation on.

  • annotation (Annotation) – The annotation to render.

  • top_left (Tuple[int, int]) – The top left coordinate of the tile.

  • scale (float) – The scale at which we are rendering the tile.

render_line(tile, annotation, top_left, scale)[source]#

Render a line annotation onto a tile using cv2. :param tile: The rgb(a) tile image to render onto. :type tile: ndarray :param annotation: The annotation to render. :type annotation: Annotation :param top_left: The top left corner of the tile in wsi. :type top_left: tuple :param scale: The zoom scale at which we are rendering. :type scale: float

Parameters:
render_multipoly(tile, annotation, top_left, scale)[source]#

render a multipolygon annotation onto a tile using cv2

render_poly(tile, annotation, top_left, scale)[source]#

Render a polygon annotation onto a tile using cv2. :param tile: The rgb(a) tile image to render onto. :type tile: ndarray :param annotation: The annotation to render. :type annotation: Annotation :param top_left: The top left corner of the tile in wsi. :type top_left: tuple :param scale: The zoom scale at which we are rendering. :type scale: float

Parameters:
render_pt(tile, annotation, top_left, scale)[source]#

Render a point annotation onto a tile using cv2. :param tile: The rgb(a) tile image to render onto. :type tile: ndarray :param annotation: The annotation to render. :type annotation: Annotation :param top_left: The top left corner of the tile in wsi. :type top_left: tuple :param scale: The zoom scale at which we are rendering. :type scale: float

Parameters:
static to_tile_coords(coords, top_left, scale)[source]#

Return coords relative to top left of tile, as array suitable for cv2. :param coords: List of coordinates in the form [x, y]. :type coords: List :param top_left: The top left corner of the tile in wsi. :type top_left: tuple :param scale: The zoom scale at which we are rendering. :type scale: float

Returns:

Array of coordinates in tile space in the form [x, y].

Return type:

np.array

Parameters: