From c91635e0307f275ed1e529b3ff8955b02860e108 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Tue, 9 Jul 2024 17:21:11 +0200 Subject: [PATCH] TST: update for changes in GEOS (#638) * TST: update for changes in GEOS * docstrings * partial revert * try fixing the dev failure * rm tests --- momepy/functional/_dimension.py | 26 ++++---- momepy/functional/_distribution.py | 68 ++++++++++---------- momepy/functional/_shape.py | 42 ++++++------ momepy/functional/tests/conftest.py | 4 +- momepy/functional/tests/test_distribution.py | 8 +-- momepy/functional/tests/test_diversity.py | 18 ------ momepy/functional/tests/test_shape.py | 4 +- momepy/tests/test_distribution.py | 2 +- 8 files changed, 77 insertions(+), 95 deletions(-) diff --git a/momepy/functional/_dimension.py b/momepy/functional/_dimension.py index a9c72b49..cc35f0da 100644 --- a/momepy/functional/_dimension.py +++ b/momepy/functional/_dimension.py @@ -342,11 +342,11 @@ def weighted_character( >>> elongation = momepy.elongation(buildings) >>> elongation.head() - 0 0.908235 - 1 0.581317 - 2 0.726515 - 3 0.838843 - 4 0.727297 + 0 0.908244 + 1 0.581318 + 2 0.726527 + 3 0.838840 + 4 0.727294 Name: elongation, dtype: float64 Define spatial graph: @@ -360,16 +360,16 @@ def weighted_character( >>> momepy.weighted_character(elongation, buildings.area, knn5) focal - 0 0.808188 - 1 0.817300 - 2 0.627588 - 3 0.794766 - 4 0.806400 - ... - 139 0.780764 + 0 0.808190 + 1 0.817309 + 2 0.627589 + 3 0.794769 + 4 0.806403 + ... + 139 0.780744 140 0.875046 141 0.753670 - 142 0.440009 + 142 0.440000 143 0.901127 Name: sum, Length: 144, dtype: float64 """ diff --git a/momepy/functional/_distribution.py b/momepy/functional/_distribution.py index a28a183b..645ddb9e 100644 --- a/momepy/functional/_distribution.py +++ b/momepy/functional/_distribution.py @@ -47,15 +47,15 @@ def orientation(geometry: GeoDataFrame | GeoSeries) -> Series: >>> path = momepy.datasets.get_path("bubenec") >>> buildings = geopandas.read_file(path, layer="buildings") >>> momepy.orientation(buildings) - 0 41.050525 + 0 41.051468 1 20.829200 - 2 20.967704 - 3 20.811290 - 4 20.411651 - ... - 139 21.110547 + 2 20.969610 + 3 20.811525 + 4 20.412895 + ... + 139 21.107057 140 13.669062 - 141 13.671225 + 141 13.671217 142 21.463914 143 13.754569 Name: orientation, Length: 144, dtype: float64 @@ -185,17 +185,17 @@ def alignment(orientation: Series, graph: Graph) -> Series: Alignment of orienation within triangulated neighbors: >>> momepy.alignment(orientation, delaunay) - 0 14.638740 - 1 0.217086 - 2 0.204713 - 3 0.151303 - 4 0.353203 - ... - 139 1.970563 - 140 0.127318 - 141 0.161911 - 142 4.351755 - 143 0.084888 + 0 14.639585 + 1 0.217417 + 2 0.205626 + 3 0.151730 + 4 0.352692 + ... + 139 1.970642 + 140 0.127322 + 141 0.161906 + 142 4.350890 + 143 0.084884 Name: alignment, Length: 144, dtype: float64 """ orientation_expanded = orientation.loc[graph._adjacency.index.get_level_values(1)] @@ -584,15 +584,15 @@ def street_alignment( Compute street alignment. >>> momepy.street_alignment(blg_orient, str_orient, buildings["street_index"]) - 0 0.289796 + 0 0.290739 1 4.542071 - 2 0.107651 - 3 0.425138 - 4 0.824776 + 2 0.105745 + 3 0.424903 + 4 0.823533 ... - 139 1.783366 + 139 1.779876 140 0.109254 - 141 0.466462 + 141 0.466453 142 1.223387 143 0.455081 Name: street_alignment, Length: 144, dtype: float64 @@ -644,17 +644,17 @@ def cell_alignment( Compute alignment: >>> momepy.cell_alignment(blg_orient, tess_orient) - 0 0.853584 - 1 20.829171 - 2 5.550296 - 3 4.052898 - 4 0.160534 - ... - 139 0.193781 - 140 17.920319 + 0 0.854788 + 1 20.829200 + 2 5.552120 + 3 4.052674 + 4 0.159289 + ... + 139 0.189750 + 140 17.920139 141 0.393708 - 142 0.023874 - 143 0.252116 + 142 0.024618 + 143 0.252122 Name: orientation, Length: 144, dtype: float64 """ diff --git a/momepy/functional/_shape.py b/momepy/functional/_shape.py index a4de9649..a9acd067 100644 --- a/momepy/functional/_shape.py +++ b/momepy/functional/_shape.py @@ -379,16 +379,16 @@ def rectangularity(geometry: GeoDataFrame | GeoSeries) -> Series: >>> path = momepy.datasets.get_path("bubenec") >>> buildings = geopandas.read_file(path, layer="buildings") >>> momepy.rectangularity(buildings) - 0 0.694257 - 1 0.702243 - 2 0.901606 - 3 0.821808 - 4 0.912861 - ... - 139 0.997022 + 0 0.694268 + 1 0.702242 + 2 0.901582 + 3 0.821797 + 4 0.912858 + ... + 139 0.996876 140 0.820865 141 0.659281 - 142 0.971584 + 142 0.971600 143 0.999400 Length: 144, dtype: float64 """ @@ -612,14 +612,14 @@ def equivalent_rectangular_index(geometry: GeoDataFrame | GeoSeries) -> Series: >>> momepy.equivalent_rectangular_index(buildings) 0 0.787923 1 0.443137 - 2 0.954253 + 2 0.954252 3 0.851658 4 0.957543 - ... - 139 1.000055 + ... + 139 1.000050 140 0.907837 141 0.813269 - 142 0.995923 + 142 0.995926 143 0.999999 Length: 144, dtype: float64 """ @@ -656,17 +656,17 @@ def elongation(geometry: GeoDataFrame | GeoSeries) -> Series: >>> path = momepy.datasets.get_path("bubenec") >>> buildings = geopandas.read_file(path, layer="buildings") >>> momepy.elongation(buildings) - 0 0.908235 - 1 0.581317 - 2 0.726515 - 3 0.838843 - 4 0.727297 - ... - 139 0.607978 + 0 0.908244 + 1 0.581318 + 2 0.726527 + 3 0.838840 + 4 0.727294 + ... + 139 0.608004 140 0.979998 141 0.747326 - 142 0.564071 - 143 0.987954 + 142 0.564060 + 143 0.987953 Name: elongation, Length: 144, dtype: float64 """ bbox = shapely.minimum_rotated_rectangle(geometry.geometry.array) diff --git a/momepy/functional/tests/conftest.py b/momepy/functional/tests/conftest.py index 55e126b9..d2949dfb 100644 --- a/momepy/functional/tests/conftest.py +++ b/momepy/functional/tests/conftest.py @@ -4,11 +4,11 @@ from pandas.testing import assert_index_equal -def assert_result(result, expected, geometry, **kwargs): +def assert_result(result, expected, geometry, rel=None, abs=None, **kwargs): # noqa: A002 """Check the expected values and types of the result. Note: ''count'' refers to the number of non-NAs in the result.""" for key, value in expected.items(): - assert getattr(result, key)() == pytest.approx(value) + assert getattr(result, key)() == pytest.approx(value, rel=rel, abs=abs) assert isinstance(result, pd.Series) assert_index_equal(result.index, geometry.index, **kwargs) diff --git a/momepy/functional/tests/test_distribution.py b/momepy/functional/tests/test_distribution.py index 4c141a13..2f0344f1 100644 --- a/momepy/functional/tests/test_distribution.py +++ b/momepy/functional/tests/test_distribution.py @@ -26,7 +26,7 @@ def test_orientation(self): "max": 42.329365250279125, } r = mm.orientation(self.df_buildings) - assert_result(r, expected, self.df_buildings) + assert_result(r, expected, self.df_buildings, rel=1e-4) expected = { "mean": 21.176405050561755, @@ -56,7 +56,7 @@ def test_alignment(self): "max": 21.32311946014944, } r = mm.alignment(orientation, self.graph) - assert_result(r, expected, self.df_buildings, check_names=False) + assert_result(r, expected, self.df_buildings, check_names=False, rel=1e-4) def test_neighbor_distance(self): expected = { @@ -120,7 +120,7 @@ def test_street_alignment(self): "max": 20.357934749623894, } r = mm.street_alignment(building_orientation, street_orientation, street_index) - assert_result(r, expected, self.df_buildings) + assert_result(r, expected, self.df_buildings, rel=1e-3) def test_cell_alignment(self): df_buildings = self.df_buildings.reset_index() @@ -136,7 +136,7 @@ def test_cell_alignment(self): "max": 33.201625570390746, "min": 1.722848278973288e-05, } - assert_result(align, align_expected, df_buildings) + assert_result(align, align_expected, df_buildings, abs=1e-2) assert_series_equal(align, align2, check_names=False) diff --git a/momepy/functional/tests/test_diversity.py b/momepy/functional/tests/test_diversity.py index 8d53b939..1f882fa0 100644 --- a/momepy/functional/tests/test_diversity.py +++ b/momepy/functional/tests/test_diversity.py @@ -397,14 +397,6 @@ def test_unique(self): un_nan_drop, un_nan_drop_expected, self.df_tessellation, check_names=False ) - # to count nas you have to explicitly process them npw - self.df_tessellation.loc[self.df_tessellation["cat"].isna(), "cat"] = "np.nan" - un_nan = self.diversity_graph.describe( - self.df_tessellation["cat"], statistics=["nunique"] - )["nunique"] - un_nan_expected = {"count": 144, "mean": 8.13888888888889, "min": 8, "max": 9} - assert_result(un_nan, un_nan_expected, self.df_tessellation, check_names=False) - @pytest.mark.skipif( not PD_210, reason="aggregation is different in previous pandas versions" ) @@ -1092,16 +1084,6 @@ def test_unique(self): ).series assert_series_equal(un_new, un_old, check_dtype=False, check_names=False) - # to keep NAs you ahve to explicitly process them now - self.df_tessellation.loc[self.df_tessellation["cat"].isna(), "cat"] = "np.nan" - un_new = self.graph_diversity.describe( - self.df_tessellation["cat"], statistics=["nunique"] - )["nunique"] - un_old = mm.Unique( - self.df_tessellation, "cat", self.sw, "uID", dropna=False - ).series - assert_series_equal(un_new, un_old, check_dtype=False, check_names=False) - def test_unweighted_percentile(self): sw = mm.sw_high(k=3, gdf=self.df_tessellation, ids="uID") graph = ( diff --git a/momepy/functional/tests/test_shape.py b/momepy/functional/tests/test_shape.py index 85d1d97b..67ee05ff 100644 --- a/momepy/functional/tests/test_shape.py +++ b/momepy/functional/tests/test_shape.py @@ -100,7 +100,7 @@ def test_rectangularity(self): "max": 0.9994000284783504, } r = mm.rectangularity(self.df_buildings) - assert_result(r, expected, self.df_buildings) + assert_result(r, expected, self.df_buildings, rel=1e-3) def test_shape_index(self): expected = { @@ -205,7 +205,7 @@ def test_elongation(self): "max": 0.9975111632520667, } r = mm.elongation(self.df_buildings) - assert_result(r, expected, self.df_buildings) + assert_result(r, expected, self.df_buildings, rel=1e-3) @pytest.mark.skipif(not GPD_013, reason="get_coordinates() not available") def test_centroid_corner_distance(self): diff --git a/momepy/tests/test_distribution.py b/momepy/tests/test_distribution.py index 79a27f40..d0f1ae81 100644 --- a/momepy/tests/test_distribution.py +++ b/momepy/tests/test_distribution.py @@ -127,7 +127,7 @@ def test_Alignment(self): if Version(shapely.geos_version_string) < Version("3.12") else 18.29842494 ) - assert self.df_buildings["align_sw"][0] == pytest.approx(test_value) + assert self.df_buildings["align_sw"][0] == pytest.approx(test_value, rel=1e-3) sw_drop = Queen.from_dataframe(self.df_tessellation[2:], ids="uID") assert ( mm.Alignment(self.df_buildings, sw_drop, "uID", self.df_buildings["orient"])