From 3aceeb88ebcec5beffaf982efafec2ce69a3518f Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Sat, 28 Oct 2023 15:32:24 -0400 Subject: [PATCH 1/3] =?UTF-8?q?format=20&=20lint=20=E2=80=93=20cg/ops/test?= =?UTF-8?q?s/*.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libpysal/cg/ops/tests/test_accessors.py | 65 ++++++------------------- libpysal/cg/ops/tests/test_shapely.py | 32 ++++++------ libpysal/cg/ops/tests/test_tabular.py | 14 +++--- 3 files changed, 39 insertions(+), 72 deletions(-) diff --git a/libpysal/cg/ops/tests/test_accessors.py b/libpysal/cg/ops/tests/test_accessors.py index 4c5debd66..fdd305e52 100644 --- a/libpysal/cg/ops/tests/test_accessors.py +++ b/libpysal/cg/ops/tests/test_accessors.py @@ -1,10 +1,10 @@ import numpy as np import pytest -from ....common import ATOL, RTOL, pandas +from ....common import ATOL, RTOL from ....examples import get_path from ....io.geotable.file import read_files as rf -from ...shapes import Chain, LineSegment, Point, Polygon, Rectangle +from ...shapes import LineSegment, Rectangle from .. import _accessors as to_test @@ -52,7 +52,7 @@ def test_bbox(self): ] bboxes = to_test.bbox(self.polygons).tolist() - for ans, bbox in zip(answer, bboxes): + for ans, bbox in zip(answer, bboxes, strict=True): np.testing.assert_allclose(ans, bbox, rtol=RTOL, atol=ATOL) def test_bounding_box(self): @@ -97,9 +97,9 @@ def test_bounding_box(self): ], ] - for bbox, answer in zip(line_bboxes, line_answers): + for bbox, answer in zip(line_bboxes, line_answers, strict=True): np.testing.assert_allclose(bbox, answer, atol=ATOL, rtol=RTOL) - for bbox, answer in zip(pgon_bboxes, pgon_answers): + for bbox, answer in zip(pgon_bboxes, pgon_answers, strict=True): np.testing.assert_allclose(bbox, answer, atol=ATOL, rtol=RTOL) for rectangle in line_rects + pgon_rects: assert isinstance(rectangle, Rectangle) @@ -118,7 +118,7 @@ def test_centroid(self): (0.04759584610455384, -0.44147205133285744), ] - for ct, answer in zip(centroids, centroid_answers): + for ct, answer in zip(centroids, centroid_answers, strict=True): np.testing.assert_allclose(ct, answer, rtol=RTOL, atol=ATOL) def test_holes(self): @@ -186,8 +186,8 @@ def test_holes(self): ], ], ] - for hole, answer in zip(holes, answers): - for sub_hole, sub_answer in zip(hole, answer): + for hole, answer in zip(holes, answers, strict=True): + for sub_hole, sub_answer in zip(hole, answer, strict=True): np.testing.assert_allclose(sub_hole, sub_answer, rtol=RTOL, atol=ATOL) def test_len(self): @@ -211,7 +211,7 @@ def test_parts(self): with pytest.raises(AttributeError): to_test.parts(self.points) - line_parts = to_test.parts(self.lines) + to_test.parts(self.lines) pgon_parts = to_test.parts(self.polygons) pgon_answers = [ @@ -275,42 +275,9 @@ def test_parts(self): ] ], ] - line_answers = [ - [ - [ - (-0.009053924887015952, -0.25832280562918325), - (0.007481157395930582, -0.2589587703323735), - (0.007481157395930582, -0.2589587703323735), - ] - ], - [ - [ - (0.10923550990637088, -0.2564149115196125), - (0.12895041570526866, -0.2564149115196125), - ] - ], - [ - [ - (0.050726757212867735, -0.3130157701035449), - (0.050726757212867735, -0.356261369920482), - (0.06153815716710198, -0.3448140052630575), - (0.06153815716710198, -0.3448140052630575), - ] - ], - [ - [ - (-0.0414881247497188, -0.41286222850441445), - (-0.012233748402967204, -0.4402087107415953), - (0.027196063194828424, -0.46055958124368335), - (0.07489341593409732, -0.4586516871341126), - (0.11241533342232213, -0.43639292252245376), - (0.1391258509563127, -0.4058666167693217), - ] - ], - ] - for part, answer in zip(pgon_parts, pgon_answers): - for piece, sub_answer in zip(part, answer): + for part, answer in zip(pgon_parts, pgon_answers, strict=True): + for piece, sub_answer in zip(part, answer, strict=True): np.testing.assert_allclose(piece, sub_answer, rtol=RTOL, atol=ATOL) def test_perimeter(self): @@ -333,7 +300,7 @@ def test_segments(self): to_test.segments(self.polygons) line_segments = to_test.segments(self.lines) - flattened = [l[0] for l in line_segments] + flattened = [l_[0] for l_ in line_segments] answers = [ [ @@ -390,8 +357,8 @@ def test_segments(self): ], ] - for parts, points in zip(flattened, answers): - for piece, answer in zip(parts, points): + for parts, points in zip(flattened, answers, strict=True): + for piece, answer in zip(parts, points, strict=True): assert isinstance(piece, LineSegment) p1, p2 = piece.p1, piece.p2 np.testing.assert_allclose([p1, p2], answer) @@ -481,7 +448,7 @@ def test_vertices(self): (-0.04527237752903268, -0.413550752273984), ], ] - for part, answer in zip(line_verts, line_answers): + for part, answer in zip(line_verts, line_answers, strict=True): np.testing.assert_allclose(part, answer, atol=ATOL, rtol=RTOL) - for part, answer in zip(pgon_verts, pgon_answers): + for part, answer in zip(pgon_verts, pgon_answers, strict=True): np.testing.assert_allclose(part, answer, atol=ATOL, rtol=RTOL) diff --git a/libpysal/cg/ops/tests/test_shapely.py b/libpysal/cg/ops/tests/test_shapely.py index ab51a534d..c1203a1aa 100644 --- a/libpysal/cg/ops/tests/test_shapely.py +++ b/libpysal/cg/ops/tests/test_shapely.py @@ -1,17 +1,19 @@ +from warnings import warn + +import numpy as np import pytest -from .. import _shapely as sht -from ...shapes import Point, Chain, Polygon -# from ... import comparators as comp -# from ... import shapely as she -from ....io.geotable import read_files as rf from ....examples import get_path -import numpy as np -from warnings import warn +from ....io.geotable import read_files as rf + +# from ... import comparators as comp +# from ... import shapely as she +from ...shapes import Chain +from .. import _shapely as sht @pytest.mark.skip("Skipping shapely during reorg.") -class Test_Shapely: +class TestShapely: def setup_method(self): self.polygons = rf(get_path("Polygon.shp")) self.points = rf(get_path("Point.shp")) @@ -25,20 +27,20 @@ def setup_method(self): def compare(self, func_name, df, **kwargs): geom_list = df.geometry.tolist() - shefunc = she.__dict__[func_name] - shtfunc = sht.__dict__[func_name] + shefunc = she.__dict__[func_name] # noqa F821 + shtfunc = sht.__dict__[func_name] # noqa F821 try: she_vals = (shefunc(geom, **kwargs) for geom in geom_list) sht_vals = shtfunc(df, inplace=False, **kwargs) - sht_list = sht_vals["shape_{}".format(func_name)].tolist() - for tabular, shapely in zip(sht_list, she_vals): - if comp.is_shape(tabular) and comp.is_shape(shapely): - comp.equal(tabular, shapely) + sht_list = sht_vals[f"shape_{func_name}"].tolist() + for tabular, shapely in zip(sht_list, she_vals, strict=True): + if comp.is_shape(tabular) and comp.is_shape(shapely): # noqa F821 + comp.equal(tabular, shapely) # noqa F821 else: assert tabular == shapely except NotImplementedError as e: - warn("The shapely/PySAL bridge is not implemented: {}.".format(e)) + warn(f"The shapely/PySAL bridge is not implemented: {e}.", stacklevel=2) return True def test_to_wkb(self): diff --git a/libpysal/cg/ops/tests/test_tabular.py b/libpysal/cg/ops/tests/test_tabular.py index 9585b5ea3..f027f19d7 100644 --- a/libpysal/cg/ops/tests/test_tabular.py +++ b/libpysal/cg/ops/tests/test_tabular.py @@ -1,12 +1,10 @@ import numpy as np -from ....common import ATOL, RTOL, pandas from ....common import requires as _requires from ....examples import get_path from ....io import geotable as pdio -from ... import ops as GIS +from ... import ops as GIS # noqa N812 from ...shapes import Polygon -from .. import tabular as ta class TestTabular: @@ -35,7 +33,7 @@ def test_round_trip(self): assert isinstance(geodf, gpd.GeoDataFrame) new_df = GIS.tabular.to_df(geodf) assert isinstance(new_df, pd.DataFrame) - for new, old in zip(new_df.geometry, self.columbus.geometry): + for new, old in zip(new_df.geometry, self.columbus.geometry, strict=True): assert new == old def test_spatial_join(self): @@ -49,11 +47,11 @@ def test_dissolve(self): assert out[0].area == 2.0 assert out[1].area == 2.0 - answer_vertices0 = set([(0, 0), (0, 1), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)]) - answer_vertices1 = set([(2, 1), (2, 0), (1, 0), (1, 1), (1, 2), (2, 2), (2, 1)]) + answer_vertices0 = {(0, 0), (0, 1), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)} + answer_vertices1 = {(2, 1), (2, 0), (1, 0), (1, 1), (1, 2), (2, 2), (2, 1)} - s0 = set([tuple(map(int, t)) for t in out[0].vertices]) - s1 = set([tuple(map(int, t)) for t in out[1].vertices]) + s0 = {tuple(map(int, t)) for t in out[0].vertices} + s1 = {tuple(map(int, t)) for t in out[1].vertices} assert s0 == answer_vertices0 assert s1 == answer_vertices1 From 465ce662edb7a46cc962e148446e977a40822217 Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Mon, 30 Oct 2023 09:02:39 -0400 Subject: [PATCH 2/3] fix test_accessors.py:TestAccessors:test_parts --- libpysal/cg/ops/tests/test_accessors.py | 41 +++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/libpysal/cg/ops/tests/test_accessors.py b/libpysal/cg/ops/tests/test_accessors.py index fdd305e52..0bbc7f399 100644 --- a/libpysal/cg/ops/tests/test_accessors.py +++ b/libpysal/cg/ops/tests/test_accessors.py @@ -211,9 +211,45 @@ def test_parts(self): with pytest.raises(AttributeError): to_test.parts(self.points) - to_test.parts(self.lines) - pgon_parts = to_test.parts(self.polygons) + line_parts = to_test.parts(self.lines) + line_answers = [ + [ + [ + (-0.009053924887015952, -0.25832280562918325), + (0.007481157395930582, -0.2589587703323735), + (0.007481157395930582, -0.2589587703323735), + ] + ], + [ + [ + (0.10923550990637088, -0.2564149115196125), + (0.12895041570526866, -0.2564149115196125), + ] + ], + [ + [ + (0.050726757212867735, -0.3130157701035449), + (0.050726757212867735, -0.356261369920482), + (0.06153815716710198, -0.3448140052630575), + (0.06153815716710198, -0.3448140052630575), + ] + ], + [ + [ + (-0.0414881247497188, -0.41286222850441445), + (-0.012233748402967204, -0.4402087107415953), + (0.027196063194828424, -0.46055958124368335), + (0.07489341593409732, -0.4586516871341126), + (0.11241533342232213, -0.43639292252245376), + (0.1391258509563127, -0.4058666167693217), + ] + ], + ] + for part, answer in zip(line_parts, line_answers): + for piece, sub_answer in zip(part, answer): + np.testing.assert_allclose(piece, sub_answer, rtol=RTOL, atol=ATOL) + pgon_parts = to_test.parts(self.polygons) pgon_answers = [ [ [ @@ -275,7 +311,6 @@ def test_parts(self): ] ], ] - for part, answer in zip(pgon_parts, pgon_answers, strict=True): for piece, sub_answer in zip(part, answer, strict=True): np.testing.assert_allclose(piece, sub_answer, rtol=RTOL, atol=ATOL) From a6b9165095e17375268168055ce895b84ebb3309 Mon Sep 17 00:00:00 2001 From: James Gaboardi Date: Mon, 30 Oct 2023 09:06:08 -0400 Subject: [PATCH 3/3] missed zip=strict --- libpysal/cg/ops/tests/test_accessors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libpysal/cg/ops/tests/test_accessors.py b/libpysal/cg/ops/tests/test_accessors.py index 0bbc7f399..4317122f3 100644 --- a/libpysal/cg/ops/tests/test_accessors.py +++ b/libpysal/cg/ops/tests/test_accessors.py @@ -245,8 +245,8 @@ def test_parts(self): ] ], ] - for part, answer in zip(line_parts, line_answers): - for piece, sub_answer in zip(part, answer): + for part, answer in zip(line_parts, line_answers, strict=True): + for piece, sub_answer in zip(part, answer, strict=True): np.testing.assert_allclose(piece, sub_answer, rtol=RTOL, atol=ATOL) pgon_parts = to_test.parts(self.polygons)