playground_metrics.utils.geometry

Geometry helpers

Functional helpers

class playground_metrics.utils.geometry.GeometryType(value)[source]

Bases: Enum

An enumeration of all geometry types available in GEOS with their respective GEOS numeric code.

NONE = -1
POINT = 0
LINESTRING = 1
LINEARRING = 2
POLYGON = 3
MULTIPOINT = 4
MULTILINESTRING = 5
MULTIPOLYGON = 6
GEOMETRYCOLLECTION = 7
playground_metrics.utils.geometry.intersection_over_union(x, y, force_thread=False)[source]

Compute the intersection-over-union in between every possible geometry pairs from two arrays of geometries.

Parameters:
  • x (ArrayLike) – An array of geometries.

  • y (ArrayLike) – An array of geometries.

  • force_thread (bool) – Force the use of the threaded implementation. Note that this can incur a significant computational cost for small input and fail on input too small to be reliably chunked.

Returns:

An intersection-over-union matrix.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.euclidean_distance(x, y, force_thread=False)[source]

Compute the euclidean distance in between every possible centroid pairs from two arrays of geometries.

Parameters:
  • x (ArrayLike) – An array of geometries.

  • y (ArrayLike) – An array of geometries.

  • force_thread (bool) – Force the use of the threaded implementation. Note that this can incur a significant computational cost for small input and fail on input too small to be reliably chunked.

Returns:

An intersection-over-union matrix.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.point_to_box(x, width=64.0, height=64.0)[source]

Convert an array of points to an array of constant size boxes.

Parameters:
  • x (numpy.ndarray) – An array of points.

  • width (float) – The output boxes’ width.

  • height (float) – The output boxes’ width.

Returns:

An array of boxes.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.is_type(x, *geometry_types)[source]

Return whether geometries in the provided array are of one of the provided types.

Parameters:
  • x (numpy.ndarray) – A 1D [geometry] array.

  • *geometry_types (GeometryType) – A geometry type.

Returns:

A boolean array of whether geometries are of any of the provided``geometry_types``.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.as_points(x)[source]

Convert an array of geometries to an array of centroid points.

Parameters:

x (numpy.ndarray) – An array of geometries.

Returns:

An array of points.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.as_boxes(x)[source]

Convert an array of geometries to an array of bounding boxes polygons.

Parameters:

x (numpy.ndarray) – An array of points.

Returns:

An array of polygons.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.enforce_type(x, convert_fn, *geometry_types, in_place=False)[source]

Convert geometries which are not of one of the provided types with the provided conversion function.

Parameters:
  • x (numpy.ndarray) – A 1D [geometry] array.

  • convert_fn (callable) – A geometry type conversion function.

  • *geometry_types (GeometryType) – A geometry type.

  • in_place (bool) – If True convert the geometries in place.

Returns:

An array of geometries, some of which where converted.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.enforce_point(x, in_place=False)[source]

Convert non-point geometries to points.

Parameters:
  • x (numpy.ndarray) – A 1D [geometry] array.

  • in_place (bool) – If True convert the geometries in place.

Returns:

An array of points, some of which where converted.

Return type:

numpy.ndarray

playground_metrics.utils.geometry.enforce_polygon(x, in_place=False)[source]

Convert non-polygon geometries to polygons.

Parameters:
  • x (numpy.ndarray) – A 1D [geometry] array.

  • in_place (bool) – If True convert the geometries in place.

Returns:

An array of points, some of which where converted.

Return type:

numpy.ndarray

Conversion helpers

playground_metrics.utils.conversion.convert_to_bounding_box(input_array, trim_invalid_geometry=False, autocorrect_invalid_geometry=False)[source]

Convert an input array to a BoundingBox array.

Parameters:
  • input_array (ndarray, list) – A ndarray of BoundingBox optionally followed by a confidence value and/or a label where each row is: [xmin, ymin, xmax, ymax, (confidence), (label)]

  • trim_invalid_geometry (bool) – Optional, default to False. If set to True conversion will ignore invalid geometries and leave them out of output_array. This means that the function will return an array where output_array.shape[0] <= input_array.shape[0]. If set to False, an invalid geometry will raise an InvalidGeometryError.

  • autocorrect_invalid_geometry (Bool) – Optional, default to False. Doesn’t do anything, introduced to unify convert functions interfaces.

Returns:

A BoundingBox ndarray where each row contains a geometry followed by optionally confidence and a label e.g.: [BoundingBox, (confidence), (label)]

Return type:

ndarray

Raises:

ValueError – If input_array have invalid dimensions.

playground_metrics.utils.conversion.convert_to_point(input_array, trim_invalid_geometry=False, autocorrect_invalid_geometry=False)[source]

Convert an input array to a Point array.

Parameters:
  • input_array (ndarray, list) – A ndarray of Point optionally followed by a confidence value and/or a label where each row is: [x, y, (confidence), (label)]

  • trim_invalid_geometry (bool) – Optional, default to False. If set to True conversion will ignore invalid geometries and leave them out of output_array. This means that the function will return an array where output_array.shape[0] <= input_array.shape[0]. If set to False, an invalid geometry will raise an InvalidGeometryError.

  • autocorrect_invalid_geometry (Bool) – Optional, default to False. Doesn’t do anything, introduced to unify convert functions interfaces.

Returns:

A Point ndarray where each row contains a geometry followed by optionally confidence and a label e.g.: [Point, (confidence), (label)]

Return type:

ndarray

Raises:

ValueError – If input_array have invalid dimensions.

playground_metrics.utils.conversion.convert_to_polygon(input_array, trim_invalid_geometry=False, autocorrect_invalid_geometry=False)[source]

Convert an input array to a Polygon array.

Parameters:
  • input_array (ndarray, list) – A ndarray of Polygons optionally followed by a confidence value and/or a label where each row is: [[[outer_ring], [inner_rings]], (confidence), (label)]

  • trim_invalid_geometry (bool) – Optional, default to False. If set to True conversion will ignore invalid geometries and leave them out of output_array. This means that the function will return an array where output_array.shape[0] <= input_array.shape[0]. If set to False, an invalid geometry will raise an InvalidGeometryError.

  • autocorrect_invalid_geometry (Bool) – Optional, default to False. Whether to attempt correcting a faulty geometry to form a valid one. If set to True and the autocorrect attempt is unsuccessful, it falls back to the behaviour defined in trim_invalid_geometry.

Note

  • Polygon auto-correction only corrects self-crossing exterior rings, in which case it creates one Polygon out of every simple ring which might be extracted from the original Polygon exterior.

  • Polygon auto-correction will systematically fail on Polygons with at least one inner ring.

Returns:

A Polygon ndarray where each row contains a geometry followed by optionally confidence and a label e.g.: [Polygon, (confidence), (label)]

Return type:

ndarray

Raises:

ValueError – If input_array have invalid dimensions.

playground_metrics.utils.conversion.get_type_and_convert(input_array, trim_invalid_geometry=False, autocorrect_invalid_geometry=False)[source]

Automatically find the geometry type from the input array shape and convert it to a geometry array.

Parameters:
  • input_array (ndarray, list) –

    • A ndarray of detections stored as:

      • Bounding boxes for a given class where each row is a detection stored as: [x_min, y_min, x_max, y_max, confidence, label]

      • Polygons for a given class where each row is a detection stored as: [[[outer_ring], [inner_rings]], confidence, label]

      • Points for a given class where each row is a detection stored as: [x, y, confidence, label]

    • A ndarray of ground truth stored as:

      • Bounding boxes for a given class where each row is a ground truth stored as: [x_min, y_min, x_max, y_max, label]

      • Polygons for a given class where each row is a ground truth stored as: [[[outer_ring], [inner_rings]], label]

      • Points for a given class where each row is a ground truth stored as: [x, y, label]

  • trim_invalid_geometry (bool) – Optional, default to False. If set to True conversion will ignore invalid geometries and leave them out of output_array. This means that the function will return an array where output_array.shape[0] <= input_array.shape[0]. If set to False, an invalid geometry will raise an InvalidGeometryError.

  • autocorrect_invalid_geometry (Bool) – Optional, default to False. Whether to attempt correcting a faulty geometry to form a valid one. If set to True and the autocorrect attempt is unsuccessful, it falls back to the behaviour defined in trim_invalid_geometry.

Note

  • Polygon auto-correction only corrects self-crossing exterior rings, in which case it creates one Polygon out of every simple ring which might be extracted from the original Polygon exterior.

  • Polygon auto-correction will systematically fail on Polygons with at least one inner ring.

Returns:

A tuple of output containing:

  • The BaseGeometry type as a string which may be either "point", "polygon" or "bbox"

  • A geometry ndarray where each row contains a geometry followed by optionally confidence and a label e.g.: [BaseGeometry, (confidence), label]

Return type:

(str, ndarray)

Raises:

ValueError – If input_array have invalid dimensions.