Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: reimplement mrr on top of shapely #133

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ci/envs/310-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependencies:
- pandas
- matplotlib
- libpysal
- py-opencv
# tests
- scikit-learn
- shapely
Expand All @@ -19,5 +18,4 @@ dependencies:
- codecov
- pip
- pip:
- opencv-contrib-python
- KDEpy
2 changes: 0 additions & 2 deletions ci/envs/311-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dependencies:
- libpysal
- mapclassify
- folium
- py-opencv
# tests
- shapely
- pyproj
Expand All @@ -20,7 +19,6 @@ dependencies:
- pip
- pip:
- --pre --index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple --extra-index-url https://pypi.org/simple
- opencv-contrib-python
- git+https://github.com/geopandas/geopandas.git
- git+https://github.com/pysal/libpysal.git
- scipy
Expand Down
2 changes: 0 additions & 2 deletions ci/envs/311-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependencies:
- pandas
- matplotlib
- libpysal
- py-opencv
# tests
- scikit-learn
- shapely
Expand All @@ -19,7 +18,6 @@ dependencies:
- codecov
- pip
- pip:
- opencv-contrib-python
- KDEpy
# for docs build action (this env only)
- nbsphinx
Expand Down
4 changes: 0 additions & 4 deletions ci/envs/38-minimal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ dependencies:
- pandas=1.3
- matplotlib=3.4
- libpysal=4.5
- py-opencv=4.6
# tests
- scikit-learn==1.2
- shapely
- geopandas
- pytest
- pytest-cov
- codecov
- pip
- pip:
- opencv-contrib-python==4.6.0.66
2 changes: 0 additions & 2 deletions ci/envs/39-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependencies:
- pandas
- matplotlib
- libpysal
- py-opencv
# tests
- scikit-learn
- shapely
Expand All @@ -19,5 +18,4 @@ dependencies:
- codecov
- pip
- pip:
- opencv-contrib-python
- KDEpy
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def setup(app):
'libpysal': ('https://pysal.org/libpysal/', None),
'pandas': ('https://pandas.pydata.org/pandas-docs/stable/', None),
'matplotlib':("https://matplotlib.org/", None),
'opencv-contrib-python':("https://docs.opencv.org/3.4/index.html", None),
'KDEpy':("https://kdepy.readthedocs.io/en/latest/", None),
'statsmodels':("https://www.statsmodels.org/stable/", None),
}
Expand Down
58 changes: 39 additions & 19 deletions pointpats/centrography.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import numpy as np
import warnings
import copy
import math

from math import pi as PI
from scipy.spatial import ConvexHull
from libpysal.cg import get_angle_between, Ray, is_clockwise
from scipy.spatial import distance as dist
from scipy.optimize import minimize
import shapely

not_clockwise = lambda x: not is_clockwise(x)

Expand Down Expand Up @@ -81,8 +83,7 @@
Compute the minimum rotated rectangle for an input point set.

This is the smallest enclosing rectangle (possibly rotated)
for the input point set. It is computed using OpenCV2, so
if that is not available, then this function will fail.
for the input point set. It is computed using Shapely.

Parameters
----------
Expand All @@ -92,7 +93,6 @@
return_angle : bool
whether to return the angle (in degrees) of the angle between
the horizontal axis of the rectanle and the first side (i.e. length).
Computed directly from cv2.minAreaRect.

Returns
-------
Expand All @@ -102,15 +102,21 @@

"""
points = np.asarray(points)
try:
from cv2 import minAreaRect, boxPoints
except (ImportError, ModuleNotFoundError):
raise ModuleNotFoundError("OpenCV2 is required to use this function. Please install it with `pip install opencv-contrib-python`")
((x, y), (w, h), angle) = rot_rect = minAreaRect(points.astype(np.float32))
out_points = boxPoints(rot_rect)
out_points = shapely.get_coordinates(
shapely.minimum_rotated_rectangle(shapely.multipoints(points))
)[:-1]
if return_angle:
return (out_points, angle)
return out_points
angle = (

Check warning on line 109 in pointpats/centrography.py

View check run for this annotation

Codecov / codecov/patch

pointpats/centrography.py#L109

Added line #L109 was not covered by tests
math.degrees(
math.atan2(
out_points[1][1] - out_points[0][1],
out_points[1][0] - out_points[0][0],
)
)
% 90
)
return (out_points[::-1], angle)

Check warning on line 118 in pointpats/centrography.py

View check run for this annotation

Codecov / codecov/patch

pointpats/centrography.py#L118

Added line #L118 was not covered by tests
return out_points[::-1]


def mbr(points):
Expand Down Expand Up @@ -378,8 +384,22 @@
removed = []
i = 0
while True:
angles = [_angle(_prec(p, points), p, _succ(p, points),) for p in points]
circles = [_circle(_prec(p, points), p, _succ(p, points),) for p in points]
angles = [

Check warning on line 387 in pointpats/centrography.py

View check run for this annotation

Codecov / codecov/patch

pointpats/centrography.py#L387

Added line #L387 was not covered by tests
_angle(
_prec(p, points),
p,
_succ(p, points),
)
for p in points
]
circles = [

Check warning on line 395 in pointpats/centrography.py

View check run for this annotation

Codecov / codecov/patch

pointpats/centrography.py#L395

Added line #L395 was not covered by tests
_circle(
_prec(p, points),
p,
_succ(p, points),
)
for p in points
]
radii = [c[0] for c in circles]
lexord = np.lexsort((radii, angles)) # confusing as hell defaults...
lexmax = lexord[-1]
Expand Down Expand Up @@ -506,14 +526,14 @@
else:
D = 2 * (px * (qy - ry) + qx * (ry - py) + rx * (py - qy))
center_x = (
(px ** 2 + py ** 2) * (qy - ry)
+ (qx ** 2 + qy ** 2) * (ry - py)
+ (rx ** 2 + ry ** 2) * (py - qy)
(px**2 + py**2) * (qy - ry)
+ (qx**2 + qy**2) * (ry - py)
+ (rx**2 + ry**2) * (py - qy)
) / float(D)
center_y = (
(px ** 2 + py ** 2) * (rx - qx)
+ (qx ** 2 + qy ** 2) * (px - rx)
+ (rx ** 2 + ry ** 2) * (qx - px)
(px**2 + py**2) * (rx - qx)
+ (qx**2 + qy**2) * (px - rx)
+ (rx**2 + ry**2) * (qx - px)
) / float(D)
radius = _euclidean_distance(center_x, center_y, px, py)
return radius, center_x, center_y
6 changes: 6 additions & 0 deletions pointpats/tests/test_centrography.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# TODO: skyum, dtot, weighted_mean_center, manhattan_median
import unittest
import numpy as np
import shapely
import pytest

from ..centrography import *

Expand All @@ -26,6 +28,10 @@ def setUp(self):
]
)

@pytest.mark.skipif(
jGaboardi marked this conversation as resolved.
Show resolved Hide resolved
shapely.geos_version < (3, 12, 0),
reason="Requires GEOS 3.12.0 to use correct algorithm"
)
def test_centrography_mar(self):
mrr = minimum_rotated_rectangle(self.points)
known = np.array(
Expand Down