Skip to content

Commit

Permalink
1 source for toy fac & dem error/warning data
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Apr 28, 2024
1 parent 15f4f2b commit 9707567
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 98 deletions.
40 changes: 39 additions & 1 deletion spopt/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ def _network_instance(
return _network_instance


_warns_geo_crs = pytest.warns(UserWarning, match="Geometry is in a geographic CRS")


@pytest.fixture
def loc_warns_geo_crs() -> _pytest.recwarn.WarningsChecker:
"""`locate` warning"""
return pytest.warns(UserWarning, match="Geometry is in a geographic CRS")
return _warns_geo_crs


@pytest.fixture
Expand Down Expand Up @@ -164,3 +167,38 @@ def loc_raises_infeasible() -> _pytest.python_api.RaisesContext:
def loc_raises_fac_constr() -> _pytest.python_api.RaisesContext:
"""`locate` error"""
return pytest.raises(AttributeError, match="Before setting facility constraint")


@pytest.fixture
def toy_fac_data() -> geopandas.GeoDataFrame:
"""Toy facility data used in ``locate`` error & warning tests."""
pol1 = shapely.Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = shapely.Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = shapely.Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}

return geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")


@pytest.fixture
def toy_dem_data() -> (
tuple[geopandas.GeoDataFrame, geopandas.GeoDataFrame, geopandas.GeoDataFrame]
):
"""Toy demand data used in ``locate`` error & warning tests."""

point = shapely.Point(10, 10)
point_dict = {"weight": 4, "geometry": [point]}

gdf_dem = geopandas.GeoDataFrame(point_dict, crs="EPSG:4326")

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message="Conversion of an array with ndim > 0"
)
gdf_dem_crs = gdf_dem.to_crs("EPSG:3857")

gdf_dem_buffered = gdf_dem.copy()
with _warns_geo_crs:
gdf_dem_buffered["geometry"] = gdf_dem.buffer(2)

return gdf_dem, gdf_dem_crs, gdf_dem_buffered
4 changes: 1 addition & 3 deletions spopt/tests/test_c_p_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import pytest

from spopt.locate import PMedian
from spopt.locate.base import (
SpecificationError,
)
from spopt.locate.base import SpecificationError


class TestSyntheticLocate:
Expand Down
23 changes: 6 additions & 17 deletions spopt/tests/test_lscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy
import pulp
import pytest
from shapely import Point, Polygon

from spopt.locate import LSCP
from spopt.locate.base import FacilityModelBuilder
Expand Down Expand Up @@ -199,23 +198,13 @@ def test_infeasibility_lscp_from_geodataframe(self, loc_raises_infeasible):

class TestErrorsWarnings:
@pytest.fixture(autouse=True)
def setup_method(self, loc_warns_geo_crs) -> None:
pol1 = Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}
def setup_method(self, toy_fac_data, toy_dem_data) -> None:
self.gdf_fac = toy_fac_data

point = Point(10, 10)
point_dict = {"weight": 4, "geometry": [point]}

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")
self.gdf_dem = geopandas.GeoDataFrame(point_dict, crs="EPSG:4326")

self.gdf_dem_crs = self.gdf_dem.to_crs("EPSG:3857")

self.gdf_dem_buffered = self.gdf_dem.copy()
with loc_warns_geo_crs:
self.gdf_dem_buffered["geometry"] = self.gdf_dem.buffer(2)
gdf_dem, gdf_dem_crs, gdf_dem_buffered = toy_dem_data
self.gdf_dem = gdf_dem
self.gdf_dem_crs = gdf_dem_crs
self.gdf_dem_buffered = gdf_dem_buffered

def test_attribute_error_add_set_covering_constraint(self):
with pytest.raises(AttributeError, match="Before setting coverage constraints"):
Expand Down
23 changes: 6 additions & 17 deletions spopt/tests/test_lscpb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy
import pulp
import pytest
from shapely import Point, Polygon

from spopt.locate import LSCPB
from spopt.locate.base import FacilityModelBuilder
Expand Down Expand Up @@ -240,23 +239,13 @@ def test_infeasibility_lscpb_from_geodataframe(self, loc_raises_infeasible):

class TestErrorsWarnings:
@pytest.fixture(autouse=True)
def setup_method(self, loc_warns_geo_crs) -> None:
pol1 = Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}
def setup_method(self, toy_fac_data, toy_dem_data) -> None:
self.gdf_fac = toy_fac_data

point = Point(10, 10)
point_dict = {"weight": 4, "geometry": [point]}

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")
self.gdf_dem = geopandas.GeoDataFrame(point_dict, crs="EPSG:4326")

self.gdf_dem_crs = self.gdf_dem.to_crs("EPSG:3857")

self.gdf_dem_buffered = self.gdf_dem.copy()
with loc_warns_geo_crs:
self.gdf_dem_buffered["geometry"] = self.gdf_dem.buffer(2)
gdf_dem, gdf_dem_crs, gdf_dem_buffered = toy_dem_data
self.gdf_dem = gdf_dem
self.gdf_dem_crs = gdf_dem_crs
self.gdf_dem_buffered = gdf_dem_buffered

def test_error_lscpb_different_crs(
self, loc_warns_mixed_type_fac, loc_raises_diff_crs, loc_warns_geo_crs
Expand Down
24 changes: 6 additions & 18 deletions spopt/tests/test_mclp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy
import pulp
import pytest
from shapely import Point, Polygon

from spopt.locate import MCLP
from spopt.locate.base import FacilityModelBuilder
Expand Down Expand Up @@ -342,24 +341,13 @@ def test_attribute_error_percentage_mclp_facility_client_array(self):

class TestErrorsWarnings:
@pytest.fixture(autouse=True)
def setup_method(self, loc_warns_geo_crs) -> None:
pol1 = Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}
def setup_method(self, toy_fac_data, toy_dem_data) -> None:
self.gdf_fac = toy_fac_data

point = Point(10, 10)
point_dict = {"weight": 4, "geometry": [point]}

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")
self.gdf_dem = geopandas.GeoDataFrame(point_dict, crs="EPSG:4326")

self.gdf_dem_crs = self.gdf_dem.to_crs("EPSG:3857")

self.gdf_dem_buffered = self.gdf_dem.copy()

with loc_warns_geo_crs:
self.gdf_dem_buffered["geometry"] = self.gdf_dem.buffer(2)
gdf_dem, gdf_dem_crs, gdf_dem_buffered = toy_dem_data
self.gdf_dem = gdf_dem
self.gdf_dem_crs = gdf_dem_crs
self.gdf_dem_buffered = gdf_dem_buffered

def test_attribute_error_add_facility_constraint(self, loc_raises_fac_constr):
with loc_raises_fac_constr:
Expand Down
23 changes: 6 additions & 17 deletions spopt/tests/test_p_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy
import pulp
import pytest
from shapely import Point, Polygon

from spopt.locate import PCenter
from spopt.locate.base import FacilityModelBuilder
Expand Down Expand Up @@ -193,23 +192,13 @@ def test_infeasibility_pcenter_from_geodataframe(self, loc_raises_infeasible):

class TestErrorsWarnings:
@pytest.fixture(autouse=True)
def setup_method(self, loc_warns_geo_crs) -> None:
pol1 = Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}
def setup_method(self, toy_fac_data, toy_dem_data) -> None:
self.gdf_fac = toy_fac_data

point = Point(10, 10)
point_dict = {"weight": 4, "geometry": [point]}

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")
self.gdf_dem = geopandas.GeoDataFrame(point_dict, crs="EPSG:4326")

self.gdf_dem_crs = self.gdf_dem.to_crs("EPSG:3857")

self.gdf_dem_buffered = self.gdf_dem.copy()
with loc_warns_geo_crs:
self.gdf_dem_buffered["geometry"] = self.gdf_dem.buffer(2)
gdf_dem, gdf_dem_crs, gdf_dem_buffered = toy_dem_data
self.gdf_dem = gdf_dem
self.gdf_dem_crs = gdf_dem_crs
self.gdf_dem_buffered = gdf_dem_buffered

def test_attribute_error_add_minimized_maximum_constraint(self):
with pytest.raises(
Expand Down
11 changes: 3 additions & 8 deletions spopt/tests/test_p_dispersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy
import pulp
import pytest
from shapely import Polygon

from spopt.locate import PDispersion
from spopt.locate.base import FacilityModelBuilder
Expand Down Expand Up @@ -139,13 +138,9 @@ def test_infeasibility_p_dispersion_from_geodataframe(self, loc_raises_infeasibl


class TestErrorsWarnings:
def setup_method(self) -> None:
pol1 = Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")
@pytest.fixture(autouse=True)
def setup_method(self, toy_fac_data) -> None:
self.gdf_fac = toy_fac_data

def test_attribute_error_add_facility_constraint(self, loc_raises_fac_constr):
with loc_raises_fac_constr:
Expand Down
23 changes: 6 additions & 17 deletions spopt/tests/test_p_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy
import pulp
import pytest
from shapely import Point, Polygon

from spopt.locate import PMedian
from spopt.locate.base import FacilityModelBuilder
Expand Down Expand Up @@ -227,23 +226,13 @@ def test_infeasibility_pmedian_from_geodataframe(self, loc_raises_infeasible):

class TestErrorsWarnings:
@pytest.fixture(autouse=True)
def setup_method(self, loc_warns_geo_crs) -> None:
pol1 = Polygon([(0, 0), (1, 0), (1, 1)])
pol2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
pol3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
polygon_dict = {"geometry": [pol1, pol2, pol3]}
def setup_method(self, toy_fac_data, toy_dem_data) -> None:
self.gdf_fac = toy_fac_data

point = Point(10, 10)
point_dict = {"weight": 4, "geometry": [point]}

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")
self.gdf_dem = geopandas.GeoDataFrame(point_dict, crs="EPSG:4326")

self.gdf_dem_crs = self.gdf_dem.to_crs("EPSG:3857")

self.gdf_dem_buffered = self.gdf_dem.copy()
with loc_warns_geo_crs:
self.gdf_dem_buffered["geometry"] = self.gdf_dem.buffer(2)
gdf_dem, gdf_dem_crs, gdf_dem_buffered = toy_dem_data
self.gdf_dem = gdf_dem
self.gdf_dem_crs = gdf_dem_crs
self.gdf_dem_buffered = gdf_dem_buffered

def test_attribute_error_add_assignment_constraint(self):
with pytest.raises(
Expand Down

0 comments on commit 9707567

Please sign in to comment.