Skip to content

Commit

Permalink
[Done] assert_geometries_equal (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw committed Oct 23, 2021
1 parent afddf3b commit 9d8a3b4
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 102 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Version 0.11 (unreleased)
with empty geometries (#381).
* Added ``pygeos.force_2d`` and ``pygeos.force_3d`` to change the dimensionality of
the coordinates in a geometry (#396).
* Added ``pygeos.testing.assert_geometries_equal`` (#401).

**API Changes**

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ API Reference
linear
coordinates
strtree
testing
changelog


Expand Down
14 changes: 14 additions & 0 deletions docs/testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Testing
=======

The functions in this module are not directly importable from the root ``pygeos`` module.
Instead, import them from the submodule as follows:

>>> from pygeos.testing import assert_geometries_equal

.. automodule:: pygeos.testing
:members:
:exclude-members:
:special-members:
:inherited-members:
:show-inheritance:
200 changes: 200 additions & 0 deletions pygeos/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
from functools import partial

import numpy as np

import pygeos

__all__ = ["assert_geometries_equal"]


def _equals_exact_with_ndim(x, y, tolerance):
return pygeos.equals_exact(x, y, tolerance=tolerance) & (
pygeos.get_coordinate_dimension(x) == pygeos.get_coordinate_dimension(y)
)


def _replace_nan(arr):
return np.where(np.isnan(arr), 0.0, arr)


def _assert_nan_coords_same(x, y, tolerance, err_msg, verbose):
x, y = np.broadcast_arrays(x, y)
x_coords = pygeos.get_coordinates(x, include_z=True)
y_coords = pygeos.get_coordinates(y, include_z=True)

# Check the shapes (condition is copied from numpy test_array_equal)
if x_coords.shape != y_coords.shape:
return False

# Check NaN positional equality
x_id = np.isnan(x_coords)
y_id = np.isnan(y_coords)
if not (x_id == y_id).all():
msg = build_err_msg(
[x, y],
err_msg + "\nx and y nan coordinate location mismatch:",
verbose=verbose,
)
raise AssertionError(msg)

# If this passed, replace NaN with a number to be able to use equals_exact
x_no_nan = pygeos.apply(x, _replace_nan, include_z=True)
y_no_nan = pygeos.apply(y, _replace_nan, include_z=True)

return _equals_exact_with_ndim(x_no_nan, y_no_nan, tolerance=tolerance)


def _assert_none_same(x, y, err_msg, verbose):
x_id = pygeos.is_missing(x)
y_id = pygeos.is_missing(y)

if not (x_id == y_id).all():
msg = build_err_msg(
[x, y],
err_msg + "\nx and y None location mismatch:",
verbose=verbose,
)
raise AssertionError(msg)

# If there is a scalar, then here we know the array has the same
# flag as it everywhere, so we should return the scalar flag.
if x.ndim == 0:
return bool(x_id)
elif y.ndim == 0:
return bool(y_id)
else:
return y_id


def assert_geometries_equal(
x,
y,
tolerance=1e-7,
equal_none=True,
equal_nan=True,
normalize=False,
err_msg="",
verbose=True,
):
"""Raises an AssertionError if two geometry array_like objects are not equal.
Given two array_like objects, check that the shape is equal and all elements of
these objects are equal. An exception is raised at shape mismatch or conflicting
values. In contrast to the standard usage in pygeos, no assertion is raised if
both objects have NaNs/Nones in the same positions.
Parameters
----------
x : Geometry or array_like
y : Geometry or array_like
equal_none : bool, default True
Whether to consider None elements equal to other None elements.
equal_nan : bool, default True
Whether to consider nan coordinates as equal to other nan coordinates.
normalize : bool, default False
Whether to normalize geometries prior to comparison.
err_msg : str, optional
The error message to be printed in case of failure.
verbose : bool, optional
If True, the conflicting values are appended to the error message.
"""
__tracebackhide__ = True # Hide traceback for py.test
if normalize:
x = pygeos.normalize(x)
y = pygeos.normalize(y)
x = np.array(x, copy=False)
y = np.array(y, copy=False)

is_scalar = x.ndim == 0 or y.ndim == 0

# Check the shapes (condition is copied from numpy test_array_equal)
if not (is_scalar or x.shape == y.shape):
msg = build_err_msg(
[x, y],
err_msg + f"\n(shapes {x.shape}, {y.shape} mismatch)",
verbose=verbose,
)
raise AssertionError(msg)

flagged = False
if equal_none:
flagged = _assert_none_same(x, y, err_msg, verbose)

if not np.isscalar(flagged):
x, y = x[~flagged], y[~flagged]
# Only do the comparison if actual values are left
if x.size == 0:
return
elif flagged:
# no sense doing comparison if everything is flagged.
return

is_equal = _equals_exact_with_ndim(x, y, tolerance=tolerance)
if is_scalar and not np.isscalar(is_equal):
is_equal = bool(is_equal[0])

if np.all(is_equal):
return
elif not equal_nan:
msg = build_err_msg(
[x, y],
err_msg + f"\nNot equal to tolerance {tolerance:g}",
verbose=verbose,
)
raise AssertionError(msg)

# Optionally refine failing elements if NaN should be considered equal
if not np.isscalar(is_equal):
x, y = x[~is_equal], y[~is_equal]
# Only do the NaN check if actual values are left
if x.size == 0:
return
elif is_equal:
# no sense in checking for NaN if everything is equal.
return

is_equal = _assert_nan_coords_same(x, y, tolerance, err_msg, verbose)
if not np.all(is_equal):
msg = build_err_msg(
[x, y],
err_msg + f"\nNot equal to tolerance {tolerance:g}",
verbose=verbose,
)
raise AssertionError(msg)


## BELOW A COPY FROM numpy.testing._private.utils (numpy version 1.20.2)


def build_err_msg(
arrays,
err_msg,
header="Geometries are not equal:",
verbose=True,
names=("x", "y"),
precision=8,
):
msg = ["\n" + header]
if err_msg:
if err_msg.find("\n") == -1 and len(err_msg) < 79 - len(header):
msg = [msg[0] + " " + err_msg]
else:
msg.append(err_msg)
if verbose:
for i, a in enumerate(arrays):

if isinstance(a, np.ndarray):
# precision argument is only needed if the objects are ndarrays
r_func = partial(np.array_repr, precision=precision)
else:
r_func = repr

try:
r = r_func(a)
except Exception as exc:
r = f"[repr failed for <{type(a).__name__}>: {exc}]"
if r.count("\n") > 3:
r = "\n".join(r.splitlines()[:3])
r += "..."
msg.append(f" {names[i]}: {r}")
return "\n".join(msg)
11 changes: 0 additions & 11 deletions pygeos/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import numpy as np
import pytest
from numpy.testing import assert_array_equal

import pygeos

Expand Down Expand Up @@ -83,13 +82,3 @@ def assert_decreases_refcount(obj):
pytest.skip("sys.getrefcount is not available.")
yield
assert sys.getrefcount(obj) == before - 1


def assert_geometries_equal(actual, expected):
actual = np.asarray(actual)
expected = np.broadcast_to(expected, actual.shape)
mask = pygeos.is_geometry(expected)
if np.any(mask):
assert pygeos.equals_exact(actual[mask], expected[mask]).all()
if np.any(~mask):
assert_array_equal(actual[~mask], expected[~mask])
22 changes: 13 additions & 9 deletions pygeos/tests/test_constructive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pygeos
from pygeos import Geometry, GEOSException
from pygeos.testing import assert_geometries_equal

from .common import (
all_types,
Expand Down Expand Up @@ -271,9 +272,12 @@ def test_offset_curve_join_style_invalid():
"POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 4 2, 4 4, 2 4, 2 2))"
),
),
(
pytest.param(
pygeos.Geometry("MULTILINESTRING ((0 0, 1 2), (3 3, 4 4))"),
pygeos.Geometry("MULTILINESTRING ((1 2, 0 0), (4 4, 3 3))"),
marks=pytest.mark.skipif(
pygeos.geos_version < (3, 8, 0), reason="GEOS < 3.8"
),
),
(
pygeos.Geometry(
Expand All @@ -295,7 +299,7 @@ def test_offset_curve_join_style_invalid():
],
)
def test_reverse(geom, expected):
assert pygeos.equals(pygeos.reverse(geom), expected)
assert_geometries_equal(pygeos.reverse(geom), expected)


@pytest.mark.skipif(pygeos.geos_version < (3, 7, 0), reason="GEOS < 3.7")
Expand All @@ -307,7 +311,7 @@ def test_reverse_none():
expected = pygeos.Geometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))")
result = pygeos.reverse([None, geometry])
assert result[0] is None
assert pygeos.equals(result[1], expected)
assert_geometries_equal(result[1], expected)


@pytest.mark.skipif(pygeos.geos_version < (3, 7, 0), reason="GEOS < 3.7")
Expand Down Expand Up @@ -339,7 +343,7 @@ def test_reverse_invalid_type(geom):
def test_clip_by_rect(geom, expected):
geom, expected = pygeos.Geometry(geom), pygeos.Geometry(expected)
actual = pygeos.clip_by_rect(geom, 10, 10, 20, 20)
assert pygeos.equals(actual, expected)
assert_geometries_equal(actual, expected)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -374,7 +378,7 @@ def test_clip_by_rect(geom, expected):
def test_clip_by_rect_polygon(geom, rect, expected):
geom, expected = pygeos.Geometry(geom), pygeos.Geometry(expected)
actual = pygeos.clip_by_rect(geom, *rect)
assert pygeos.equals(actual, expected)
assert_geometries_equal(actual, expected)


@pytest.mark.parametrize("geometry", all_types)
Expand Down Expand Up @@ -605,14 +609,14 @@ def test_segmentize_tolerance_nan(geometry):
)
def test_segmentize_empty(geometry):
actual = pygeos.segmentize(geometry, tolerance=5)
assert pygeos.equals(actual, geometry).all()
assert_geometries_equal(actual, geometry)


@pytest.mark.skipif(pygeos.geos_version < (3, 10, 0), reason="GEOS < 3.10")
@pytest.mark.parametrize("geometry", [point, point_z, multi_point])
def test_segmentize_no_change(geometry):
actual = pygeos.segmentize(geometry, tolerance=5)
assert pygeos.equals(actual, geometry).all()
assert_geometries_equal(actual, geometry)


@pytest.mark.skipif(pygeos.geos_version < (3, 10, 0), reason="GEOS < 3.10")
Expand Down Expand Up @@ -686,7 +690,7 @@ def test_segmentize_none():
)
def test_segmentize(geometry, tolerance, expected):
actual = pygeos.segmentize(geometry, tolerance)
assert pygeos.equals(actual, geometry).all()
assert_geometries_equal(actual, expected)


@pytest.mark.skipif(pygeos.geos_version < (3, 8, 0), reason="GEOS < 3.8")
Expand Down Expand Up @@ -728,7 +732,7 @@ def test_minimum_bounding_circle_all_types(geometry):
)
def test_minimum_bounding_circle(geometry, expected):
actual = pygeos.minimum_bounding_circle(geometry)
assert pygeos.equals(actual, expected).all()
assert_geometries_equal(actual, expected)


@pytest.mark.skipif(pygeos.geos_version < (3, 6, 0), reason="GEOS < 3.6")
Expand Down
9 changes: 5 additions & 4 deletions pygeos/tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pygeos
from pygeos.geometry import GeometryType
from pygeos.testing import assert_geometries_equal

from .common import (
empty_polygon,
Expand Down Expand Up @@ -148,8 +149,8 @@ def test_polygon_from_linearring():


def test_polygons_none():
assert pygeos.equals(pygeos.polygons(None), empty_polygon)
assert pygeos.equals(pygeos.polygons(None, holes=[linear_ring]), empty_polygon)
assert_geometries_equal(pygeos.polygons(None), empty_polygon)
assert_geometries_equal(pygeos.polygons(None, holes=[linear_ring]), empty_polygon)


def test_polygons():
Expand Down Expand Up @@ -362,7 +363,7 @@ def test_create_collection_wrong_geom_type(func, sub_geom):
)
def test_box(coords, ccw, expected):
actual = pygeos.box(*coords, ccw=ccw)
assert pygeos.equals(actual, expected)
assert_geometries_equal(actual, expected)


@pytest.mark.parametrize(
Expand All @@ -388,7 +389,7 @@ def test_box(coords, ccw, expected):
)
def test_box_array(coords, ccw, expected):
actual = pygeos.box(*coords, ccw=ccw)
assert pygeos.equals(actual, expected).all()
assert_geometries_equal(actual, expected)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 9d8a3b4

Please sign in to comment.