Skip to main content

``hypothesis`` strategies for geometries.

Project description

hypothesis_geometry

Github Actions Codecov License PyPI

In what follows python is an alias for python3.10 or pypy3.10 or any later version (python3.11, pypy3.11 and so on).

Installation

Prerequisites

Install the latest pip & setuptools packages versions

python -m pip install --upgrade pip setuptools

User

Download and install the latest stable version from PyPI repository

python -m pip install --upgrade hypothesis_geometry

Developer

Download the latest version from GitHub repository

git clone https://github.com/lycantropos/hypothesis_geometry.git
cd hypothesis_geometry

Install

python -m pip install -e '.'

Usage

With setup

>>> import math
>>> from fractions import Fraction
>>> from ground.context import Context
>>> from hypothesis import strategies
>>> from hypothesis_geometry import planar
>>> coordinate_type = Fraction
>>> context = Context(coordinate_factory=coordinate_type, sqrt=math.sqrt)
>>> Contour = context.contour_cls
>>> Empty = context.empty_cls
>>> Mix = context.mix_cls
>>> Multipoint = context.multipoint_cls
>>> Multipolygon = context.multipolygon_cls
>>> Multisegment = context.multisegment_cls
>>> Point = context.point_cls
>>> Polygon = context.polygon_cls
>>> Segment = context.segment_cls
>>> min_coordinate, max_coordinate = -100, 100
>>> coordinates = strategies.fractions(min_coordinate, max_coordinate)
>>> import warnings
>>> from hypothesis.errors import NonInteractiveExampleWarning
>>> # ignore hypothesis warnings caused by `example` method call
... warnings.filterwarnings('ignore', category=NonInteractiveExampleWarning)

let's take a look at what can be generated and how.

Empty geometries

>>> empty_geometries = planar.empty_geometries(context=context)
>>> empty = empty_geometries.example()
>>> isinstance(empty, Empty)
True

Points

>>> points = planar.points(coordinates, context=context)
>>> point = points.example()
>>> isinstance(point, Point)
True
>>> (
...     isinstance(point.x, coordinate_type)
...     and isinstance(point.y, coordinate_type)
... )
True
>>> (
...     min_coordinate <= point.x <= max_coordinate
...     and min_coordinate <= point.y <= max_coordinate
... )
True

Multipoints

>>> min_size, max_size = 5, 10
>>> multipoints = planar.multipoints(
...     coordinates, context=context, min_size=min_size, max_size=max_size
... )
>>> multipoint = multipoints.example()
>>> isinstance(multipoint, Multipoint)
True
>>> min_size <= len(multipoint.points) <= max_size
True
>>> all(
...     isinstance(point.x, coordinate_type)
...     and isinstance(point.y, coordinate_type)
...     for point in multipoint.points
... )
True
>>> all(
...     min_coordinate <= point.x <= max_coordinate
...     and min_coordinate <= point.y <= max_coordinate
...     for point in multipoint.points
... )
True

Segments

>>> segments = planar.segments(coordinates, context=context)
>>> segment = segments.example()
>>> isinstance(segment, Segment)
True
>>> (
...     isinstance(segment.start.x, coordinate_type)
...     and isinstance(segment.start.y, coordinate_type)
...     and isinstance(segment.end.x, coordinate_type)
...     and isinstance(segment.end.y, coordinate_type)
... )
True
>>> (
...     min_coordinate <= segment.start.x <= max_coordinate
...     and min_coordinate <= segment.start.y <= max_coordinate
...     and min_coordinate <= segment.end.x <= max_coordinate
...     and min_coordinate <= segment.end.y <= max_coordinate
... )
True

Multisegments

>>> min_size, max_size = 5, 10
>>> multisegments = planar.multisegments(
...     coordinates, context=context, min_size=min_size, max_size=max_size
... )
>>> multisegment = multisegments.example()
>>> isinstance(multisegment, Multisegment)
True
>>> min_size <= len(multisegment.segments) <= max_size
True
>>> all(
...     isinstance(segment.start.x, coordinate_type)
...     and isinstance(segment.start.y, coordinate_type)
...     and isinstance(segment.end.x, coordinate_type)
...     and isinstance(segment.end.y, coordinate_type)
...     for segment in multisegment.segments
... )
True
>>> all(
...     min_coordinate <= segment.start.x <= max_coordinate
...     and min_coordinate <= segment.start.y <= max_coordinate
...     and min_coordinate <= segment.end.x <= max_coordinate
...     and min_coordinate <= segment.end.y <= max_coordinate
...     for segment in multisegment.segments
... )
True

Contours

>>> min_size, max_size = 5, 10
>>> contours = planar.contours(
...     coordinates, context=context, min_size=min_size, max_size=max_size
... )
>>> contour = contours.example()
>>> isinstance(contour, Contour)
True
>>> min_size <= len(contour.vertices) <= max_size
True
>>> all(
...     isinstance(vertex.x, coordinate_type)
...     and isinstance(vertex.y, coordinate_type)
...     for vertex in contour.vertices
... )
True
>>> all(
...     min_coordinate <= vertex.x <= max_coordinate
...     and min_coordinate <= vertex.y <= max_coordinate
...     for vertex in contour.vertices
... )
True

also planar.concave_contours & planar.convex_contours options are available.

Multicontours

>>> min_size, max_size = 5, 10
>>> min_contour_size, max_contour_size = 4, 8
>>> multicontours = planar.multicontours(
...     coordinates,
...     context=context,
...     min_size=min_size,
...     max_size=max_size,
...     min_contour_size=min_contour_size,
...     max_contour_size=max_contour_size,
... )
>>> multicontour = multicontours.example()
>>> isinstance(multicontour, list)
True
>>> all(isinstance(contour, Contour) for contour in multicontour)
True
>>> min_size <= len(multicontour) <= max_size
True
>>> all(
...     min_contour_size <= len(contour.vertices) <= max_contour_size
...     for contour in multicontour
... )
True
>>> all(
...     isinstance(vertex.x, coordinate_type)
...     and isinstance(vertex.y, coordinate_type)
...     for contour in multicontour
...     for vertex in contour.vertices
... )
True
>>> all(
...     min_coordinate <= vertex.x <= max_coordinate
...     and min_coordinate <= vertex.y <= max_coordinate
...     for contour in multicontour
...     for vertex in contour.vertices
... )
True

Polygons

>>> min_size, max_size = 5, 10
>>> min_holes_size, max_holes_size = 1, 3
>>> min_hole_size, max_hole_size = 4, 8
>>> polygons = planar.polygons(
...     coordinates,
...     context=context,
...     min_size=min_size,
...     max_size=max_size,
...     min_holes_size=min_holes_size,
...     max_holes_size=max_holes_size,
...     min_hole_size=min_hole_size,
...     max_hole_size=max_hole_size,
... )
>>> polygon = polygons.example()
>>> isinstance(polygon, Polygon)
True
>>> min_size <= len(polygon.border.vertices) <= max_size
True
>>> min_holes_size <= len(polygon.holes) <= max_holes_size
True
>>> all(
...     min_hole_size <= len(hole.vertices) <= max_hole_size
...     for hole in polygon.holes
... )
True
>>> polygon_contours = [polygon.border, *polygon.holes]
>>> all(
...     isinstance(vertex.x, coordinate_type)
...     and isinstance(vertex.y, coordinate_type)
...     for contour in polygon_contours
...     for vertex in contour.vertices
... )
True
>>> all(
...     min_coordinate <= vertex.x <= max_coordinate
...     and min_coordinate <= vertex.y <= max_coordinate
...     for contour in polygon_contours
...     for vertex in contour.vertices
... )
True

Multipolygons

>>> min_size, max_size = 2, 5
>>> min_border_size, max_border_size = 5, 10
>>> min_holes_size, max_holes_size = 1, 3
>>> min_hole_size, max_hole_size = 4, 8
>>> multipolygons = planar.multipolygons(
...     coordinates,
...     context=context,
...     min_size=min_size,
...     max_size=max_size,
...     min_border_size=min_border_size,
...     max_border_size=max_border_size,
...     min_holes_size=min_holes_size,
...     max_holes_size=max_holes_size,
...     min_hole_size=min_hole_size,
...     max_hole_size=max_hole_size,
... )
>>> multipolygon = multipolygons.example()
>>> isinstance(multipolygon, Multipolygon)
True
>>> min_size <= len(multipolygon.polygons) <= max_size
True
>>> all(
...     min_border_size <= len(polygon.border.vertices) <= max_border_size
...     and min_holes_size <= len(polygon.holes) <= max_holes_size
...     and all(
...         min_hole_size <= len(hole.vertices) <= max_hole_size
...         for hole in polygon.holes
...     )
...     for polygon in multipolygon.polygons
... )
True
>>> all(
...     all(
...         isinstance(vertex.x, coordinate_type)
...         and isinstance(vertex.y, coordinate_type)
...         for vertex in polygon.border.vertices
...     )
...     and all(
...         isinstance(vertex.x, coordinate_type)
...         and isinstance(vertex.y, coordinate_type)
...         for hole in polygon.holes
...         for vertex in hole.vertices
...     )
...     for polygon in multipolygon.polygons
... )
True
>>> all(
...     all(
...         min_coordinate <= vertex.x <= max_coordinate
...         and min_coordinate <= vertex.y <= max_coordinate
...         for vertex in polygon.border.vertices
...     )
...     and all(
...         min_coordinate <= vertex.x <= max_coordinate
...         and min_coordinate <= vertex.y <= max_coordinate
...         for hole in polygon.holes
...         for vertex in hole.vertices
...     )
...     for polygon in multipolygon.polygons
... )
True

Mixes

>>> min_points_size, max_points_size = 2, 3
>>> min_segments_size, max_segments_size = 1, 4
>>> min_polygons_size, max_polygons_size = 0, 5
>>> min_polygon_border_size, max_polygon_border_size = 5, 10
>>> min_polygon_holes_size, max_polygon_holes_size = 1, 4
>>> min_polygon_hole_size, max_polygon_hole_size = 3, 5
>>> mixes = planar.mixes(
...     coordinates,
...     context=context,
...     min_points_size=min_points_size,
...     max_points_size=max_points_size,
...     min_segments_size=min_segments_size,
...     max_segments_size=max_segments_size,
...     min_polygons_size=min_polygons_size,
...     max_polygons_size=max_polygons_size,
...     min_polygon_border_size=min_polygon_border_size,
...     max_polygon_border_size=max_polygon_border_size,
...     min_polygon_holes_size=min_polygon_holes_size,
...     max_polygon_holes_size=max_polygon_holes_size,
...     min_polygon_hole_size=min_polygon_hole_size,
...     max_polygon_hole_size=max_polygon_hole_size,
... )
>>> mix = mixes.example()
>>> isinstance(mix, Mix)
True
>>> isinstance(mix.discrete, (Empty, Multipoint))
True
>>> points = [] if isinstance(mix.discrete, Empty) else mix.discrete.points
>>> min_points_size <= len(points) <= max_points_size
True
>>> all(
...     isinstance(point.x, coordinate_type)
...     and isinstance(point.y, coordinate_type)
...     for point in points
... )
True
>>> all(
...     min_coordinate <= point.x <= max_coordinate
...     and min_coordinate <= point.y <= max_coordinate
...     for point in points
... )
True
>>> isinstance(mix.linear, (Empty, Segment, Contour, Multisegment))
True
>>> segments = (
...     []
...     if isinstance(mix.linear, Empty)
...     else (
...         [mix.linear]
...         if isinstance(mix.linear, Segment)
...         else (
...             mix.linear.segments
...             if isinstance(mix.linear, Multisegment)
...             else context.contour_edges(mix.linear)
...         )
...     )
... )
>>> min_segments_size <= len(segments) <= max_segments_size
True
>>> all(
...     isinstance(segment.start.x, coordinate_type)
...     and isinstance(segment.start.y, coordinate_type)
...     and isinstance(segment.end.x, coordinate_type)
...     and isinstance(segment.end.y, coordinate_type)
...     for segment in segments
... )
True
>>> all(
...     min_coordinate <= segment.start.x <= max_coordinate
...     and min_coordinate <= segment.start.y <= max_coordinate
...     and min_coordinate <= segment.end.x <= max_coordinate
...     and min_coordinate <= segment.end.y <= max_coordinate
...     for segment in segments
... )
True
>>> isinstance(mix.shaped, (Empty, Polygon, Multipolygon))
True
>>> polygons = (
...     []
...     if isinstance(mix.shaped, Empty)
...     else (
...         [mix.shaped]
...         if isinstance(mix.shaped, Polygon)
...         else mix.shaped.polygons
...     )
... )
>>> min_polygons_size <= len(polygons) <= max_polygons_size
True
>>> all(
...     min_polygon_border_size
...     <= len(polygon.border.vertices)
...     <= max_polygon_border_size
...     and (
...         min_polygon_holes_size
...         <= len(polygon.holes)
...         <= max_polygon_holes_size
...     )
...     and all(
...         min_polygon_hole_size
...         <= len(hole.vertices)
...         <= max_polygon_hole_size
...         for hole in polygon.holes
...     )
...     for polygon in polygons
... )
True
>>> all(
...     all(
...         isinstance(vertex.x, coordinate_type)
...         and isinstance(vertex.y, coordinate_type)
...         for vertex in polygon.border.vertices
...     )
...     and all(
...         isinstance(vertex.x, coordinate_type)
...         and isinstance(vertex.y, coordinate_type)
...         for hole in polygon.holes
...         for vertex in hole.vertices
...     )
...     for polygon in polygons
... )
True
>>> all(
...     all(
...         min_coordinate <= vertex.x <= max_coordinate
...         and min_coordinate <= vertex.y <= max_coordinate
...         for vertex in polygon.border.vertices
...     )
...     and all(
...         min_coordinate <= vertex.x <= max_coordinate
...         and min_coordinate <= vertex.y <= max_coordinate
...         for hole in polygon.holes
...         for vertex in hole.vertices
...     )
...     for polygon in polygons
... )
True

Caveats

  • Strategies may be slow depending on domain, so it may be necessary to add HealthCheck.filter_too_much, HealthCheck.too_slow in suppress_health_check and set deadline to None.

  • Unbounded floating point strategies for coordinates (like hypothesis.strategies.floats with unset min_value/max_value) do not play well with bounded sizes and may cause a lot of searching iterations with no success, so it is recommended to use bounded floating point coordinates with bounded sizes or unbounded coordinates with unbounded sizes.

  • decimal.Decimal coordinates are not supported, because they seem to be too hard to work with correctly (e.g. sometimes self-intersecting contours arise), so it is suggested to use float or fractions.Fraction instead.

Development

Bumping version

Prerequisites

Install bump-my-version.

Release

Choose which version number category to bump following semver specification.

Test bumping version

bump-my-version bump --dry-run --verbose $CATEGORY

where $CATEGORY is the target version number category name, possible values are patch/minor/major.

Bump version

bump-my-version bump --verbose $CATEGORY

This will set version to major.minor.patch.

Running tests

Plain

Install with dependencies

python -m pip install -e '.[tests]'

Run

pytest

Docker container

Run

  • with CPython

    docker-compose --file docker-compose.cpython.yml up
    
  • with PyPy

    docker-compose --file docker-compose.pypy.yml up
    

Bash script

Run

  • with CPython

    ./run-tests.sh
    

    or

    ./run-tests.sh cpython
    
  • with PyPy

    ./run-tests.sh pypy
    

PowerShell script

Run

  • with CPython

    .\run-tests.ps1
    

    or

    .\run-tests.ps1 cpython
    
  • with PyPy

    .\run-tests.ps1 pypy
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypothesis_geometry-9.0.0.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hypothesis_geometry-9.0.0-py3-none-any.whl (30.2 kB view details)

Uploaded Python 3

File details

Details for the file hypothesis_geometry-9.0.0.tar.gz.

File metadata

  • Download URL: hypothesis_geometry-9.0.0.tar.gz
  • Upload date:
  • Size: 35.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hypothesis_geometry-9.0.0.tar.gz
Algorithm Hash digest
SHA256 80ebebf813edf4021e8dcef2df164696983ae4aeb2f103a64d0c6260cfb8614d
MD5 f0ebc8c873cffc2f09c63fc77461045c
BLAKE2b-256 873b1415ce7d24791e552c14988b9b375dc9426a760fa0f092fc83e756956c70

See more details on using hashes here.

File details

Details for the file hypothesis_geometry-9.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hypothesis_geometry-9.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a68e02f2adcea68058d7be34ea9ca60fe51d0ba07449c0beffd8d9edd442da52
MD5 81e57ccace798f0c82bbbaab1a5a74d2
BLAKE2b-256 687fcb8e587241a259834da7cfe13be59d249310eb65f715a6c6dfa915fadbf3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page