Skip to main content

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
    

Release files for hypothesis-geometry 9.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hypothesis-geometry 9.0.0
File Size Uploaded
hypothesis_geometry-9.0.0.tar.gz 35.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hypothesis-geometry 9.0.0
File Interpreter ABI Platform
hypothesis_geometry-9.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 65.8 kB

Release files / hypothesis_geometry-9.0.0.tar.gz

Download URL hypothesis_geometry-9.0.0.tar.gz
Size 35.6 kB
Tags Source
SHA-256 checksum
How to use checksums
80ebebf813edf4021e8dcef2df164696983ae4aeb2f103a64d0c6260cfb8614d
BLAKE2b-256 checksum
How to use checksums
873b1415ce7d24791e552c14988b9b375dc9426a760fa0f092fc83e756956c70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / hypothesis_geometry-9.0.0-py3-none-any.whl

Download URL hypothesis_geometry-9.0.0-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a68e02f2adcea68058d7be34ea9ca60fe51d0ba07449c0beffd8d9edd442da52
BLAKE2b-256 checksum
How to use checksums
687fcb8e587241a259834da7cfe13be59d249310eb65f715a6c6dfa915fadbf3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

9.0.0 This release

2 release files

8.0.0

2 release files

7.3.0

2 release files

7.2.0

2 release files

7.1.0

2 release files

7.0.0

2 release files

6.1.0

2 release files

6.0.0

2 release files

5.0.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.17.2

2 release files

0.16.0

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page