From 384d135928f33b5a1b5fad504ea9c6a4612e2e4b Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Thu, 6 Jul 2023 18:05:28 +0200 Subject: [PATCH] Handle scipy and pytest deprecations (#109) * remove ckdtree * qhull deprecations * pytest deprecations --- pointpats/geometry.py | 17 ++++++++--------- pointpats/tests/test_ripley.py | 2 +- pointpats/tests/test_spacetime.py | 18 +++++++++--------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/pointpats/geometry.py b/pointpats/geometry.py index 84486d5..92eb5e3 100644 --- a/pointpats/geometry.py +++ b/pointpats/geometry.py @@ -11,7 +11,7 @@ # Utilities and dispatching # # ------------------------------------------------------------# -TREE_TYPES = (spatial.KDTree, spatial.cKDTree, Arc_KDTree) +TREE_TYPES = (spatial.KDTree, Arc_KDTree) try: from sklearn.neighbors import KDTree, BallTree @@ -42,7 +42,7 @@ def area(shape): @area.register -def _(shape: spatial.qhull.ConvexHull): +def _(shape: spatial.ConvexHull): """ If a shape is a convex hull from scipy, assure it's 2-dimensional and then use its volume. @@ -85,7 +85,7 @@ def _(shape: numpy.ndarray): @bbox.register -def _(shape: spatial.qhull.ConvexHull): +def _(shape: spatial.ConvexHull): """ For scipy.spatial.ConvexHulls, compute the bounding box from their boundary points. @@ -132,7 +132,7 @@ def _(shape: spatial.Delaunay, x: float, y: float): @contains.register -def _(shape: spatial.qhull.ConvexHull, x: float, y: float): +def _(shape: spatial.ConvexHull, x: float, y: float): """ For convex hulls, convert their exterior first into a Delaunay triangulation and then use the delaunay dispatcher. @@ -174,7 +174,7 @@ def _(shape: numpy.ndarray): @centroid.register -def _(shape: spatial.qhull.ConvexHull): +def _(shape: spatial.ConvexHull): """ Treat convex hulls as arrays of points """ @@ -277,7 +277,6 @@ def build_best_tree(coordinates, metric): Chooses from: 1. sklearn.KDTree if available and metric is simple 2. sklearn.BallTree if available and metric is complicated - 3. scipy.spatial.cKDTree if nothing else Parameters ---------- @@ -305,7 +304,7 @@ def build_best_tree(coordinates, metric): Otherwise, an error will be raised. """ coordinates = numpy.asarray(coordinates) - tree = spatial.cKDTree + tree = spatial.KDTree try: import sklearn from sklearn.neighbors import KDTree, BallTree @@ -404,7 +403,7 @@ def prepare_hull(coordinates, hull=None): - an (N,2) array of points for which the bounding box will be computed & used - a shapely polygon/multipolygon - a shapely geometry - - a scipy.spatial.qhull.ConvexHull + - a scipy.spatial.ConvexHull """ if isinstance(hull, numpy.ndarray): assert len(hull) == 4, f"bounding box provided is not shaped correctly! {hull}" @@ -423,7 +422,7 @@ def prepare_hull(coordinates, hull=None): return spatial.ConvexHull(coordinates) elif hull.startswith("alpha") or hull.startswith("α"): return alpha_shape_auto(coordinates) - elif isinstance(hull, spatial.qhull.ConvexHull): + elif isinstance(hull, spatial.ConvexHull): return hull raise ValueError( f"Hull type {hull} not in the set of valid options:" diff --git a/pointpats/tests/test_ripley.py b/pointpats/tests/test_ripley.py index f5b506a..073bb19 100644 --- a/pointpats/tests/test_ripley.py +++ b/pointpats/tests/test_ripley.py @@ -23,7 +23,7 @@ ] ) -tree = spatial.cKDTree(points) +tree = spatial.KDTree(points) chull = spatial.ConvexHull(points) ashape = alpha_shape_auto(points) diff --git a/pointpats/tests/test_spacetime.py b/pointpats/tests/test_spacetime.py index ccfbbea..94d1ffb 100644 --- a/pointpats/tests/test_spacetime.py +++ b/pointpats/tests/test_spacetime.py @@ -10,9 +10,9 @@ def setUp(self): def test_SpaceTimeEvents(self): events = SpaceTimeEvents(self.path, "T") - self.assertEquals(events.n, 188) - self.assertEquals(list(events.space[0]), [300.0, 302.0]) - self.assertEquals(list(events.t[0]), [413]) + self.assertEqual(events.n, 188) + self.assertEqual(list(events.space[0]), [300.0, 302.0]) + self.assertEqual(list(events.t[0]), [413]) class Knox_Tester(unittest.TestCase): @@ -22,7 +22,7 @@ def setUp(self): def test_knox(self): result = knox(self.events.space, self.events.t, delta=20, tau=5, permutations=1) - self.assertEquals(result["stat"], 13.0) + self.assertEqual(result["stat"], 13.0) class Mantel_Tester(unittest.TestCase): @@ -40,7 +40,7 @@ def test_mantel(self): tcon=0.0, tpow=1.0, ) - self.assertAlmostEquals(result["stat"], 0.014154, 6) + self.assertAlmostEqual(result["stat"], 0.014154, 6) class Jacquez_Tester(unittest.TestCase): @@ -49,9 +49,9 @@ def setUp(self): self.events = SpaceTimeEvents(path, "T") def test_jacquez(self): - result = jacquez(self.events.space, - self.events.t, k=3, permutations=1) - self.assertEquals(result['stat'], 12) + result = jacquez(self.events.space, self.events.t, k=3, permutations=1) + self.assertEqual(result["stat"], 12) + class ModifiedKnox_Tester(unittest.TestCase): def setUp(self): @@ -62,7 +62,7 @@ def test_modified_knox(self): result = modified_knox( self.events.space, self.events.t, delta=20, tau=5, permutations=1 ) - self.assertAlmostEquals(result["stat"], 2.810160, 6) + self.assertAlmostEqual(result["stat"], 2.810160, 6) suite = unittest.TestSuite()