Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flesh out kn-pmp testing #429

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 82 additions & 29 deletions spopt/tests/test_knearest_p_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from shapely.geometry import Point

from spopt.locate.p_median import KNearestPMedian
from spopt.locate.base import SpecificationError
import os
import pickle
import platform
Expand All @@ -26,27 +27,26 @@ def setup_method(self) -> None:
"geometry": [Point(1, 1), Point(0, 2), Point(2, 0)],
"capacity": [1, 1, 1],
}
gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326")
gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326")
self.gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326")
self.gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326")
self.k_nearest_pmedian = KNearestPMedian.from_geodataframe(
gdf_demand,
gdf_fac,
self.gdf_demand,
self.gdf_fac,
"geometry",
"geometry",
"demand",
p_facilities=2,
facility_capacity_col="capacity",
k_array=k,
)
self.solver = pulp.PULP_CBC_CMD(msg=False)

def test_knearest_p_median_from_geodataframe(self):
result = self.k_nearest_pmedian.solve(pulp.PULP_CBC_CMD(msg=False))
result = self.k_nearest_pmedian.solve(self.solver)
assert isinstance(result, KNearestPMedian)

def test_knearest_p_median_from_geodataframe_no_results(self):
result = self.k_nearest_pmedian.solve(
pulp.PULP_CBC_CMD(msg=False), results=False
)
result = self.k_nearest_pmedian.solve(self.solver, results=False)
assert isinstance(result, KNearestPMedian)

with pytest.raises(AttributeError):
Expand All @@ -57,8 +57,7 @@ def test_knearest_p_median_from_geodataframe_no_results(self):
result.mean_dist

def test_solve(self):
solver = pulp.PULP_CBC_CMD(msg=False)
self.k_nearest_pmedian.solve(solver)
self.k_nearest_pmedian.solve(self.solver)
assert self.k_nearest_pmedian.problem.status == pulp.LpStatusOptimal

fac2cli_known = [[1], [0], []]
Expand All @@ -68,14 +67,26 @@ def test_solve(self):
assert self.k_nearest_pmedian.cli2fac == cli2fac_known
assert self.k_nearest_pmedian.mean_dist == mean_dist_known

def test_error_overflow_k(self):
k = numpy.array([10, 10])
with pytest.raises(ValueError, match="The value of k should be"):
KNearestPMedian.from_geodataframe(
self.gdf_demand,
self.gdf_fac,
"geometry",
"geometry",
"demand",
p_facilities=2,
facility_capacity_col="capacity",
k_array=k,
)

def test_error_k_array_non_numpy_array(self):
gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326")
gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326")
k = [1, 1]
with pytest.raises(TypeError):
with pytest.raises(TypeError, match="k_array should be a numpy array."):
KNearestPMedian.from_geodataframe(
gdf_demand,
gdf_fac,
self.gdf_demand,
self.gdf_fac,
"geometry",
"geometry",
"demand",
Expand All @@ -85,14 +96,11 @@ def test_error_k_array_non_numpy_array(self):
)

def test_error_k_array_invalid_value(self):
gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326")
gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326")

k = numpy.array([1, 4])
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="The value of k should be no more"):
KNearestPMedian.from_geodataframe(
gdf_demand,
gdf_fac,
self.gdf_demand,
self.gdf_fac,
"geometry",
"geometry",
"demand",
Expand All @@ -101,21 +109,66 @@ def test_error_k_array_invalid_value(self):
k_array=k,
)

def test_error_geodataframe_crs_mismatch(self):
gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326")
gdf_fac = geopandas.GeoDataFrame(
self.facility_data, crs="EPSG:3857"
) # Different CRS
def test_error_no_crs_demand(self):
_gdf_demand = geopandas.GeoDataFrame(self.demand_data)
k = numpy.array([1, 1])
with pytest.raises(ValueError, match="GeoDataFrame gdf_demand "):
KNearestPMedian.from_geodataframe(
_gdf_demand,
self.gdf_fac,
"geometry",
"geometry",
"demand",
p_facilities=2,
facility_capacity_col="capacity",
k_array=k,
)

def test_error_no_crs_facility(self):
_gdf_fac = geopandas.GeoDataFrame(self.facility_data)
k = numpy.array([1, 1])
with pytest.raises(ValueError, match="GeoDataFrame gdf_facility "):
KNearestPMedian.from_geodataframe(
self.gdf_demand,
_gdf_fac,
"geometry",
"geometry",
"demand",
p_facilities=2,
facility_capacity_col="capacity",
k_array=k,
)

def test_error_geodataframe_crs_mismatch(self):
_gdf_fac = self.gdf_fac.copy().to_crs("EPSG:3857")
k = numpy.array([1, 1])
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Geodataframes crs are different"):
KNearestPMedian.from_geodataframe(
gdf_demand,
gdf_fac,
self.gdf_demand,
_gdf_fac,
"geometry",
"geometry",
"demand",
p_facilities=2,
facility_capacity_col="capacity",
k_array=k,
)

def test_error_high_capacity(self):
_gdf_demand = self.gdf_demand.copy()
_gdf_demand["demand"] = [10, 10]
k = numpy.array([1, 1])
with pytest.raises(
SpecificationError,
match="Problem is infeasible. The highest possible capacity",
):
KNearestPMedian.from_geodataframe(
_gdf_demand,
self.gdf_fac,
"geometry",
"geometry",
"demand",
p_facilities=1,
facility_capacity_col="capacity",
k_array=k,
).solve(self.solver)