Skip to content

Commit c49f7d2

Browse files
authored
COMPAT: compatibility with numpy (#147)
* COMPAT: compatibility with numpy * pin fiona
1 parent 8c20108 commit c49f7d2

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

ci/envs/310-minimal.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
- pandas=1.4
1111
- scipy=1.10
1212
- shapely=2
13+
- fiona<1.10
1314
# tests
1415
- scikit-learn=1.2
1516
- statsmodels

pointpats/centrography.py

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def mean_center(points):
170170
return points.mean(axis=0)
171171

172172

173+
174+
173175
def weighted_mean_center(points, weights):
174176
"""
175177
Find weighted mean center of a marked point pattern.

pointpats/pointpattern.py

+36-32
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
Planar Point Pattern Class
33
44
"""
5+
56
import numpy as np
67
import sys
78
from libpysal.cg import KDTree
89
from .centrography import hull
9-
from .window import as_window, poly_from_bbox
10+
from .window import as_window, poly_from_bbox
1011
from .util import cached_property
1112
import pandas as pd
1213
from matplotlib import pyplot as plt
1314
from matplotlib.collections import PatchCollection
1415
from matplotlib.patches import Polygon
1516

1617
__author__ = "Serge Rey [email protected]"
17-
__all__ = ['PointPattern']
18+
__all__ = ["PointPattern"]
1819

1920
if sys.version_info[0] > 2:
2021
xrange = range
@@ -63,8 +64,8 @@ class PointPattern(object):
6364
7638.200000000001
6465
6566
"""
66-
def __init__(self, points, window=None, names=None, coord_names=None):
6767

68+
def __init__(self, points, window=None, names=None, coord_names=None):
6869
# first two series in df are x, y unless coor_names and names are
6970
# specified
7071

@@ -75,12 +76,12 @@ def __init__(self, points, window=None, names=None, coord_names=None):
7576
if names is not None:
7677
coord_names = names[:2]
7778
else:
78-
coord_names = ['x', 'y']
79+
coord_names = ["x", "y"]
7980
if names is None:
8081
col_names = coord_names
8182
if p > 2:
8283
for m in range(2, p):
83-
col_names.append("mark_{}".format(m-2))
84+
col_names.append("mark_{}".format(m - 2))
8485
coord_names = coord_names[:2]
8586
else:
8687
col_names = names
@@ -143,19 +144,19 @@ def get_window(self):
143144
144145
:class:`.window.Window`
145146
"""
146-
if not hasattr(self, '_window') or self._window is None:
147+
if not hasattr(self, "_window") or self._window is None:
147148
# use bbox as window
148149
self.set_window(as_window(poly_from_bbox(self.mbb)))
149150
return self._window
150151

151152
window = property(get_window, set_window)
152153

153154
def summary(self):
154-
'''
155+
"""
155156
Description of the point pattern.
156-
'''
157+
"""
157158

158-
print('Point Pattern')
159+
print("Point Pattern")
159160
print("{} points".format(self.n))
160161
print("Bounding rectangle [({},{}), ({},{})]".format(*self.mbb))
161162
print("Area of window: {}".format(self.window.area))
@@ -165,13 +166,12 @@ def summary(self):
165166
def add_marks(self, marks, mark_names=None):
166167
if mark_names is None:
167168
nm = range(len(marks))
168-
mark_names = ["mark_{}".format(self._n_marks+1+j) for j in nm]
169+
mark_names = ["mark_{}".format(self._n_marks + 1 + j) for j in nm]
169170
for name, mark in zip(mark_names, marks):
170171
self.df[name] = mark
171172
self._n_marks += 1
172173

173-
def plot(self, window=False, title="Point Pattern", hull=False,
174-
get_ax=False):
174+
def plot(self, window=False, title="Point Pattern", hull=False, get_ax=False):
175175
"""
176176
Plot function for a point pattern.
177177
@@ -187,15 +187,15 @@ def plot(self, window=False, title="Point Pattern", hull=False,
187187
pattern. If not, don't plot convex hull.
188188
get_ax : boolean
189189
If get_ax is True, return the current plot ax.
190-
190+
191191
Returns
192192
-------
193193
ax : matplotlib.axes._subplots.AxesSubplot
194194
Current plot ax. Only return it when get_ax is True.
195195
196196
"""
197197
fig, ax = plt.subplots()
198-
plt.plot(self.df[self._x], self.df[self._y], '.')
198+
plt.plot(self.df[self._x], self.df[self._y], ".")
199199
# plt.scatter(self.df[self._x], self.df[self._y])
200200
plt.title(title)
201201
if window:
@@ -234,7 +234,7 @@ def _mbb_area(self):
234234
Area of minimum bounding box
235235
"""
236236

237-
return np.product(self.mbb[[2, 3]]-self.mbb[[0, 1]])
237+
return np.prod(self.mbb[[2, 3]] - self.mbb[[0, 1]])
238238

239239
mbb_area = cached_property(_mbb_area)
240240

@@ -253,15 +253,15 @@ def _rot(self):
253253
One-quarter the smallest side of the mbb.
254254
"""
255255
w, s, e, n = self.mbb
256-
return 0.25 * min(e-w, n-s)
256+
return 0.25 * min(e - w, n - s)
257257

258258
rot = cached_property(_rot)
259259

260260
def _lambda_mbb(self):
261261
"""
262262
Intensity based on minimum bounding box
263263
"""
264-
return self.n * 1. / self.mbb_area
264+
return self.n * 1.0 / self.mbb_area
265265

266266
lambda_mbb = cached_property(_lambda_mbb)
267267

@@ -304,24 +304,25 @@ def _hull_area(self):
304304
Area of convex hull
305305
"""
306306
h = self.hull
307-
if not np.alltrue(h[0] == h[-1]):
307+
if not np.all(h[0] == h[-1]):
308308
# not in closed cartographic form
309309
h = np.vstack((h, h[0]))
310310
s = h[:-1, 0] * h[1:, 1] - h[1:, 0] * h[:-1, 1]
311-
return s.sum() / 2.
311+
return s.sum() / 2.0
312312

313313
hull_area = cached_property(_hull_area)
314314

315315
def _lambda_hull(self):
316316
"""
317317
Intensity based on convex hull
318318
"""
319-
return self.n * 1. / self.hull_area
319+
return self.n * 1.0 / self.hull_area
320320

321321
lambda_hull = cached_property(_lambda_hull)
322322

323323
def _build_tree(self):
324324
return KDTree(self.points)
325+
325326
tree = cached_property(_build_tree)
326327

327328
def knn(self, k=1):
@@ -343,8 +344,8 @@ def knn(self, k=1):
343344
nearest neighbor
344345
"""
345346
if k < 1:
346-
raise ValueError('k must be at least 1')
347-
nn = self.tree.query(self.tree.data, k=k+1)
347+
raise ValueError("k must be at least 1")
348+
nn = self.tree.query(self.tree.data, k=k + 1)
348349
return nn[1][:, 1:], nn[0][:, 1:]
349350

350351
def _nn_sum(self):
@@ -419,7 +420,7 @@ def knn_other(self, other, k=1):
419420
nearest neighbor
420421
"""
421422
if k < 1:
422-
raise ValueError('k must be at least 1')
423+
raise ValueError("k must be at least 1")
423424
try:
424425
nn = self.tree.query(np.asarray(other.points), k=k)
425426
except:
@@ -447,10 +448,10 @@ def explode(self, mark):
447448
pps = [self.df[self.df[mark] == v] for v in uv]
448449
names = self.df.columns.values.tolist()
449450
cnames = self.coord_names
450-
return[PointPattern(pp, names=names, coord_names=cnames) for pp in pps]
451+
return [PointPattern(pp, names=names, coord_names=cnames) for pp in pps]
451452

452453
def unique(self):
453-
""" Remove duplicate points in the point pattern.
454+
"""Remove duplicate points in the point pattern.
454455
455456
Two points in a point pattern are deemed to be identical if their
456457
coordinates are the same, and their marks are the same (if any)
@@ -475,8 +476,9 @@ def unique(self):
475476
coord_names = self.coord_names
476477
window = self.set_window
477478
unique_df = self.df.drop_duplicates()
478-
return PointPattern(unique_df, names=names, coord_names=coord_names,
479-
window=window)
479+
return PointPattern(
480+
unique_df, names=names, coord_names=coord_names, window=window
481+
)
480482

481483
def superimpose(self, point_pattern):
482484
"""Returns a superimposed point pattern.
@@ -511,14 +513,16 @@ def superimpose(self, point_pattern):
511513
names_pp2 = point_pattern.df.columns.values.tolist()
512514
cnames_pp2 = point_pattern.coord_names
513515
if names_pp1 != names_pp2 or cnames_pp1 != cnames_pp2:
514-
raise TypeError('Both point patterns should have similar\
515-
attributes and spatial coordinates ')
516+
raise TypeError(
517+
"Both point patterns should have similar\
518+
attributes and spatial coordinates "
519+
)
516520
pp = pd.concat((self.df, point_pattern.df))
517521
pp = pp.drop_duplicates()
518522
return PointPattern(pp, names=names_pp1, coord_names=cnames_pp1)
519523

520524
def flip_coordinates(self):
521-
""" Flips the coordinates of a point pattern.
525+
"""Flips the coordinates of a point pattern.
522526
523527
Doesn't change the structure of data frame. This function swaps
524528
`_x` and `_y` variables, which are used to represent coordinates.
@@ -527,5 +531,5 @@ def flip_coordinates(self):
527531

528532
# Pandas facade
529533
def _facade(self):
530-
self.head = self.df.head
531-
self.tail = self.df.tail
534+
self.head = self.df.head
535+
self.tail = self.df.tail

0 commit comments

Comments
 (0)