Skip to content

Commit

Permalink
first part of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis committed Nov 15, 2024
1 parent 9a73edb commit 1298e5d
Showing 1 changed file with 101 additions and 64 deletions.
165 changes: 101 additions & 64 deletions pointpats/tests/test_centrography.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
# TODO: skyum, dtot, weighted_mean_center, manhattan_median
import unittest
import numpy as np
import shapely
import pytest
import geopandas as gpd

from ..centrography import *
from pointpats import centrography

from libpysal.common import RTOL

points = np.array(
[
[66.22, 32.54],
[22.52, 22.39],
[31.01, 81.21],
[9.47, 31.02],
[30.78, 60.10],
[75.21, 58.93],
[79.26, 7.68],
[8.23, 39.93],
[98.73, 77.17],
[89.78, 42.53],
[65.19, 92.08],
[54.46, 8.48],
]
)
geoms = gpd.GeoSeries.from_xy(*points.T)
sequence = points.tolist()

class TestCentrography(unittest.TestCase):
def setUp(self):
self.points = np.array(
[
[66.22, 32.54],
[22.52, 22.39],
[31.01, 81.21],
[9.47, 31.02],
[30.78, 60.10],
[75.21, 58.93],
[79.26, 7.68],
[8.23, 39.93],
[98.73, 77.17],
[89.78, 42.53],
[65.19, 92.08],
[54.46, 8.48],
]
)
dispatch_types = pytest.mark.parametrize("points", [points, geoms, sequence], ids=["ndarray", "geoseries", "list"])

@pytest.mark.skipif(
shapely.geos_version < (3, 12, 0),
reason="Requires GEOS 3.12.0 to use correct algorithm"
@pytest.mark.skipif(
shapely.geos_version < (3, 12, 0),
reason="Requires GEOS 3.12.0 to use correct algorithm"
)
@dispatch_types
def test_centrography_mar(points):
mrr = centrography.minimum_rotated_rectangle(points)
known = np.array(
[
[36.40165, 104.61744],
[4.087286, 30.417522],
[75.59908, -0.726158],
[107.913445, 73.47376251220703],
]
)
def test_centrography_mar(self):
mrr = minimum_rotated_rectangle(self.points)
known = np.array(
[
[36.40165, 104.61744],
[4.087286, 30.417522],
[75.59908, -0.726158],
[107.913445, 73.47376251220703],
]
)
if isinstance(points, gpd.GeoSeries):
assert shapely.Polygon(known).normalize().equals_exact(mrr.normalize(), 1e-5)
else:
for i in range(5):
success = np.allclose(mrr, np.roll(known, i, axis=0))
if success:
Expand All @@ -53,45 +57,78 @@ def test_centrography_mar(self):
f"\ncomputed {mrr}\nknown: {known}"
)

def test_centrography_mbr(self):
min_x, min_y, max_x, max_y = minimum_bounding_rectangle(self.points)
@dispatch_types
def test_centrography_mbr(points):
res = centrography.minimum_bounding_rectangle(points)
if isinstance(points, gpd.GeoSeries):
assert shapely.box(
8.2300000000000004,
7.6799999999999997,
98.730000000000004,
92.079999999999998
).normalize().equals_exact(res.normalize(), 1e-5)
else:
min_x, min_y, max_x, max_y = res
np.testing.assert_allclose(min_x, 8.2300000000000004, RTOL)
np.testing.assert_allclose(min_y, 7.6799999999999997, RTOL)
np.testing.assert_allclose(max_x, 98.730000000000004, RTOL)
np.testing.assert_allclose(max_y, 92.079999999999998, RTOL)

def test_centrography_hull(self):
hull_array = hull(self.points)
res = np.array(
[
[31.01, 81.21],
[8.23, 39.93],
[9.47, 31.02],
[22.52, 22.39],
[54.46, 8.48],
[79.26, 7.68],
[89.78, 42.53],
[98.73, 77.17],
[65.19, 92.08],
]
)
np.testing.assert_array_equal(hull_array, res)
@dispatch_types
def test_centrography_hull(points):
hull = centrography.hull(points)
exp = np.array(
[
[31.01, 81.21],
[8.23, 39.93],
[9.47, 31.02],
[22.52, 22.39],
[54.46, 8.48],
[79.26, 7.68],
[89.78, 42.53],
[98.73, 77.17],
[65.19, 92.08],
]
)
if isinstance(points, gpd.GeoSeries):
assert shapely.Polygon(exp).normalize().equals_exact(hull.normalize(), 1e-5)
else:
np.testing.assert_array_equal(hull, exp)

def test_centrography_mean_center(self):
res = np.array([52.57166667, 46.17166667])
np.testing.assert_array_almost_equal(mean_center(self.points), res)
@dispatch_types
def test_centrography_mean_center(points):
exp = np.array([52.57166667, 46.17166667])
res = centrography.mean_center(points)
if isinstance(points, gpd.GeoSeries):
exp = shapely.Point(exp)
assert exp.equals_exact(res, 1e-5)
else:
np.testing.assert_array_almost_equal(res, exp)

def test_centrography_std_distance(self):
std = std_distance(self.points)
np.testing.assert_allclose(std, 40.149806489086714, RTOL)
@dispatch_types
def test_centrography_std_distance(points):
std = centrography.std_distance(points)
np.testing.assert_allclose(std, 40.149806489086714, RTOL)

def test_centrography_ellipse(self):
res = ellipse(self.points)
@dispatch_types
def test_centrography_ellipse(points):
res = centrography.ellipse(points)
if isinstance(points, gpd.GeoSeries):
assert shapely.Point(52.571666666666836, 46.17166666666682).equals_exact(res.centroid, 1e-5)
np.testing.assert_allclose(res.area, 5313.537950951353, RTOL)
assert shapely.Polygon([(75.35555380165113, -7.417558286073575),
(-2.5919130566989, 27.519856670878436),
(29.787779531682332, 99.76089161940709),
(107.73524639003224, 64.82347666245569),
(75.35555380165113, -7.417558286073575),
]).equals_exact(res.oriented_envelope, 1e-5)
else:
np.testing.assert_allclose(res[0], 39.623867886462982, RTOL)
np.testing.assert_allclose(res[1], 42.753818949026815, RTOL)
np.testing.assert_allclose(res[2], 1.1039268428650906, RTOL)

def test_centrography_euclidean_median(self):
euclidean = euclidean_median(self.points)
res = np.array([54.16770671, 44.4242589])
np.testing.assert_array_almost_equal(euclidean, res, decimal=3)
@dispatch_types
def test_centrography_euclidean_median(points):
euclidean = centrography.euclidean_median(points)
res = np.array([54.16770671, 44.4242589])
np.testing.assert_array_almost_equal(euclidean, res, decimal=3)

0 comments on commit 1298e5d

Please sign in to comment.