Skip to content

Commit

Permalink
Merge pull request #133 from martinfleis/skip
Browse files Browse the repository at this point in the history
REF: reimplement mrr on top of shapely
  • Loading branch information
knaaptime committed Mar 18, 2024
2 parents 2c0e0b2 + 339435e commit 85a77b8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 32 deletions.
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 @@ def minimum_rotated_rectangle(points, return_angle=False):
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 @@ def minimum_rotated_rectangle(points, return_angle=False):
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 @@ def minimum_rotated_rectangle(points, return_angle=False):
"""
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 = (
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)
return out_points[::-1]


def mbr(points):
Expand Down Expand Up @@ -378,8 +384,22 @@ def _skyum_lists(points):
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 = [
_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
]
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 @@ def _circle(p, q, r, dmetric=_euclidean_distance):
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(
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

0 comments on commit 85a77b8

Please sign in to comment.