Skip to content

Commit

Permalink
test for exclusion mask (#104)
Browse files Browse the repository at this point in the history
* test for exclusion mask

* add edge cases for exclusion testing

* fix edge case dict key

* sorted geometries for exclusion -- DF index varied across OS

* try sorted geometries apalachicola standard -- DF index varied across OS

* reup apalachicola edge cases -- #106

* add none clause
  • Loading branch information
jGaboardi authored Nov 24, 2024
1 parent ba343d0 commit 2a5d6eb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
45 changes: 12 additions & 33 deletions sgeop/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

####################################################
# see:
# - gh#106
# - gh#102
# - gh#77
# - gh#75
# - gh#74
Expand All @@ -34,38 +36,8 @@
"douala_809": [],
"liege_1656": [921],
"slc_4881": [1144, 1146],
"apalachicola": [
468,
469,
470,
471,
472,
473,
474,
475,
476,
477,
478,
479,
480,
481,
482,
494,
495,
496,
497,
498,
499,
500,
501,
502,
503,
504,
505,
506,
507,
508,
],
"apalachicola_standard": [324],
"apalachicola_exclusion_mask": [],
}
####################################################

Expand Down Expand Up @@ -105,6 +77,12 @@ def geom_test(
geoms1 = collection1.geometry.normalize()
geoms2 = collection2.geometry.normalize()

if aoi and aoi.startswith("apalachicola"):
# Varied index order across OSs.
# See [https://github.com/uscuni/sgeop/pull/104#issuecomment-2495572388]
geoms1 = geoms1.sort_values().reset_index(drop=True)
geoms2 = geoms2.sort_values().reset_index(drop=True)

try:
assert shapely.equals_exact(geoms1, geoms2, tolerance=tolerance).all()
except AssertionError:
Expand All @@ -120,7 +98,8 @@ def geom_test(
"n_coords": {
"g1": shapely.get_coordinates(g1).shape[0],
"g2": shapely.get_coordinates(g2).shape[0],
}
},
"length": {"g1": g1.length, "g2": g2.length},
}
if unexpected_bad:
raise AssertionError(
Expand Down
Binary file not shown.
51 changes: 42 additions & 9 deletions sgeop/tests/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import geopandas
import numpy
import pytest
import shapely
from pandas.testing import assert_series_equal

import sgeop
Expand All @@ -13,28 +14,60 @@
ci_artifacts = pathlib.Path("ci_artifacts")


def test_simplify_network_small():
@pytest.mark.parametrize(
"scenario,tol,known_length",
[
("standard", 1.5, 64566.0),
("exclusion_mask", 1.05, 65765.0),
],
)
def test_simplify_network_small(scenario, tol, known_length):
ac = "apalachicola"
known = geopandas.read_parquet(test_data / f"{ac}_simplified.parquet")
known_length = 64566.0

observed = sgeop.simplify_network(
geopandas.read_parquet(test_data / f"{ac}_original.parquet")
)
original = geopandas.read_parquet(test_data / f"{ac}_original.parquet")

known = geopandas.read_parquet(test_data / f"{ac}_simplified_{scenario}.parquet")

if scenario == "exclusion_mask":
exclusion_mask = [
shapely.Polygon(
(
(-9461361.807208396, 3469029.2708674935),
(-9461009.046874022, 3469029.2708674935),
(-9461009.046874022, 3469240.1785251377),
(-9461361.807208396, 3469240.1785251377),
(-9461361.807208396, 3469029.2708674935),
)
),
shapely.Polygon(
(
(-9461429.266819818, 3469157.7482423405),
(-9461361.807208396, 3469157.7482423405),
(-9461361.807208396, 3469240.1785251377),
(-9461429.266819818, 3469240.1785251377),
(-9461429.266819818, 3469157.7482423405),
)
),
]
exclusion_mask = geopandas.GeoSeries(exclusion_mask, crs=original.crs)
else:
exclusion_mask = None

observed = sgeop.simplify_network(original, exclusion_mask=exclusion_mask)
observed_length = observed.geometry.length.sum()

# storing GH artifacts
artifact_dir = ci_artifacts / ac
artifact_dir.mkdir(parents=True, exist_ok=True)
observed.to_parquet(artifact_dir / "simplified.parquet")
observed.to_parquet(artifact_dir / f"simplified_{scenario}.parquet")

assert pytest.approx(observed_length, rel=0.00001) == known_length
assert pytest.approx(observed_length, rel=0.0001) == known_length
assert observed.index.dtype == numpy.dtype("int64")

assert observed.shape == known.shape
assert_series_equal(known._status, observed._status)

pytest.geom_test(known, observed, tolerance=1.5, aoi=ac)
pytest.geom_test(known, observed, tolerance=tol, aoi=f"{ac}_{scenario}")


@pytest.mark.parametrize(
Expand Down

0 comments on commit 2a5d6eb

Please sign in to comment.