Skip to content

Commit

Permalink
Merge branch 'main' into noqa_colon_top_level
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis authored Nov 19, 2023
2 parents 9b86f6d + 3816631 commit c8d3c89
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 138 deletions.
12 changes: 6 additions & 6 deletions libpysal/cg/kdtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ def sparse_distance_matrix(self, other, max_distance, p=2):
* 0.5
)
max_distance = sphere.arcdist2linear(max_distance, self.radius) + FLOAT_EPS * 3
D = temp_KDTree.sparse_distance_matrix(self, other, max_distance)
D = D.tocoo()
# print D.data
a2l = lambda x: sphere.linear2arcdist(x, self.radius)
# print map(a2l,D.data)
return scipy.sparse.coo_matrix((list(map(a2l, D.data)), (D.row, D.col))).todok()
d = temp_KDTree.sparse_distance_matrix(self, other, max_distance)
d = d.tocoo()
# print d.data
a2l = lambda x: sphere.linear2arcdist(x, self.radius) # noqa: E731
# print map(a2l,d.data)
return scipy.sparse.coo_matrix((list(map(a2l, d.data)), (d.row, d.col))).todok()
10 changes: 5 additions & 5 deletions libpysal/cg/locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ def __init__(self, polygons):
for polygon in polygons:
x = polygon.bounding_box.left
y = polygon.bounding_box.lower
X = polygon.bounding_box.right
Y = polygon.bounding_box.upper
self._rtree.insert(polygon, Rect(x, y, X, Y))
_x = polygon.bounding_box.right
_y = polygon.bounding_box.upper
self._rtree.insert(polygon, Rect(x, y, _x, _y))

def inside(self, query_rectangle):
"""
Expand Down Expand Up @@ -549,15 +549,15 @@ def inside(self, query_rectangle):
]
)
ip = []
GPPI = get_polygon_point_intersect
gppi = get_polygon_point_intersect
for poly in res:
lower = poly.bounding_box.lower
right = poly.bounding_box.right
upper = poly.bounding_box.upper
left = poly.bounding_box.left
p1 = Point((left, lower))
p2 = Point((right, upper))
if GPPI(qp, p1) and GPPI(qp, p2):
if gppi(qp, p1) and gppi(qp, p2):
ip.append(poly)
return ip

Expand Down
3 changes: 3 additions & 0 deletions libpysal/cg/polygonQuadTreeStructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Quad Tree Structure Class for PySAL_core/cg/shapes.Ring, PySAL_core/cg/shapes.Polygon
This structure could speed up the determining of point in polygon process
"""

# ruff: noqa: N999

__author__ = "Hu Shao"

import math
Expand Down
80 changes: 41 additions & 39 deletions libpysal/cg/segmentLocator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ruff: noqa: A001, A002, B028, N801, N802
# ruff: noqa: A002, N802, N806, N999

import math
import random
Expand Down Expand Up @@ -30,7 +30,7 @@ def nearest(self, pt):

class SegmentLocator:
def __init__(self, segments, nbins=500):
warnings.warn("SegmentLocator " + dep_msg, FutureWarning)
warnings.warn("SegmentLocator " + dep_msg, FutureWarning, stacklevel=2)
self.data = segments
if hasattr(segments, "bounding_box"):
bbox = segments.bounding_box
Expand All @@ -52,9 +52,11 @@ def nearest(self, pt):
return possibles[numpy.argmin(distances)]


class Polyline_Shapefile_SegmentLocator:
class Polyline_Shapefile_SegmentLocator: # noqa: N801
def __init__(self, shpfile, nbins=500):
warnings.warn("Polyline_Shapefile_SegmentLocator " + dep_msg, FutureWarning)
warnings.warn(
"Polyline_Shapefile_SegmentLocator " + dep_msg, FutureWarning, stacklevel=2
)
self.data = shpfile
bbox = Rectangle(*shpfile.bbox)
res = max((bbox.right - bbox.left), (bbox.upper - bbox.lower)) / float(nbins)
Expand Down Expand Up @@ -102,7 +104,7 @@ def __init__(self, bounds, resolution):
TODO: complete this doctest
>>> g = SegmentGrid(Rectangle(0, 0, 10, 10), 1)
"""
warnings.warn("SegmentGrid " + dep_msg, FutureWarning)
warnings.warn("SegmentGrid " + dep_msg, FutureWarning, stacklevel=2)
if resolution == 0:
raise Exception("Cannot create grid with resolution 0")
self.res = resolution
Expand Down Expand Up @@ -203,20 +205,20 @@ def add(self, segment, id):
+ str(segment)
)
i, j = self.bin_loc(segment.p1, id)
I_, J_ = self.bin_loc(segment.p2, id)
i_, j_ = self.bin_loc(segment.p2, id)
self.endMask[i, j] = True
self.endMask[I_, J_] = True
self.endMask[i_, j_] = True

res = self.res
line = segment.line
tiny = res / 1000.0
for i in range(1 + min(i, I_), max(i, I_)): # noqa: B020
for i in range(1 + min(i, i_), max(i, i_)): # noqa: B020
# print 'i',i
x = self.x_range[0] + (i * res)
y = line.y(x)
self.bin_loc((x - tiny, y), id)
self.bin_loc((x + tiny, y), id)
for j in range(1 + min(j, J_), max(j, J_)): # noqa: B020
for j in range(1 + min(j, j_), max(j, j_)): # noqa: B020
# print 'j',j
y = self.y_range[0] + (j * res)
x = line.x(y)
Expand Down Expand Up @@ -280,25 +282,25 @@ def nearest(self, pt):
#### Filter indicies by bounds of the grid.
### filters must be applied one at a time
### I havn't figure out a way to group these
filter = rows >= 0
rows = rows[filter]
cols = cols[filter] # i >= 0
filter = rows < self.i_range
rows = rows[filter]
cols = cols[filter] # i < i_range
filter = cols >= 0
rows = rows[filter]
cols = cols[filter] # j >= 0
filter = cols < self.j_range
rows = rows[filter]
cols = cols[filter] # j < j_range
filter_ = rows >= 0
rows = rows[filter_]
cols = cols[filter_] # i >= 0
filter_ = rows < self.i_range
rows = rows[filter_]
cols = cols[filter_] # i < i_range
filter_ = cols >= 0
rows = rows[filter_]
cols = cols[filter_] # j >= 0
filter_ = cols < self.j_range
rows = rows[filter_]
cols = cols[filter_] # j < j_range
if DEBUG:
maskCopy = self.mask.copy().astype(float)
maskCopy += self.endMask.astype(float)
maskCopy[rows, cols] += 1
maskCopy[row, col] += 3
mask_copy = self.mask.copy().astype(float)
mask_copy += self.endMask.astype(float)
mask_copy[rows, cols] += 1
mask_copy[row, col] += 3
i = pylab.matshow(
maskCopy,
mask_copy,
origin="lower",
extent=self.x_range + self.y_range,
fignum=1,
Expand All @@ -320,8 +322,8 @@ def nearest(self, pt):
### The old way...
### previously I was using kd.query_ball_point on,
### but the performance was terrible.
I_ = self.kd2.query_ball_point(grid_loc, radius)
for i in I_:
i_ = self.kd2.query_ball_point(grid_loc, radius)
for i in i_:
t = tuple(self.kd.data[i])
possibles.update(self.hash[t])
return list(possibles)
Expand All @@ -341,16 +343,16 @@ def random_points(n):


def combo_check(bins, segments, qpoints):
G = SegmentLocator(segments, bins)
G2 = BruteSegmentLocator(segs)
g = SegmentLocator(segments, bins)
g2 = BruteSegmentLocator(segs)
for pt in qpoints:
a = G.nearest(pt)
b = G2.nearest(pt)
a = g.nearest(pt)
b = g2.nearest(pt)
if a != b:
print(a, b, a == b)
global DEBUG
DEBUG = True
a = G.nearest(pt)
a = g.nearest(pt)
print(a)
a = segments[a]
b = segments[b]
Expand All @@ -363,11 +365,11 @@ def combo_check(bins, segments, qpoints):

def brute_check(segments, qpoints): # noqa: ARG001
t0 = time.time()
G2 = BruteSegmentLocator(segs)
g2 = BruteSegmentLocator(segs)
t1 = time.time()
print("Created Brute in %0.4f seconds" % (t1 - t0))
t2 = time.time()
q = list(map(G2.nearest, qpoints))
q = list(map(g2.nearest, qpoints))
t3 = time.time()
print("Brute Found %d matches in %0.4f seconds" % (len(qpoints), t3 - t2))
print("Total Brute Time:", t3 - t0)
Expand All @@ -377,19 +379,19 @@ def brute_check(segments, qpoints): # noqa: ARG001

def grid_check(bins, segments, qpoints, visualize=False):
t0 = time.time()
G = SegmentLocator(segments, bins)
g = SegmentLocator(segments, bins)
t1 = time.time()
G.grid.kd # noqa: B018
g.grid.kd # noqa: B018
t2 = time.time()
print("Created Grid in %0.4f seconds" % (t1 - t0))
print("Created KDTree in %0.4f seconds" % (t2 - t1))
if visualize:
pylab.matshow(
G.grid.mask, origin="lower", extent=G.grid.x_range + G.grid.y_range
g.grid.mask, origin="lower", extent=g.grid.x_range + g.grid.y_range
)

t2 = time.time()
list(map(G.nearest, qpoints))
list(map(g.nearest, qpoints))
t3 = time.time()
print("Grid Found %d matches in %0.4f seconds" % (len(qpoints), t3 - t2))
print("Total Grid Time:", t3 - t0)
Expand Down
Loading

0 comments on commit c8d3c89

Please sign in to comment.