def test_multiple_supply_arcgis(self): demand_col = "Population" demand_id_col = "GEOID10" supply_id_col = "ORIG_ID" d = arcgis.GeoAccessor.from_featureclass( os.path.join(self.dir_name, "../test_data/demand_point.shp")) s = arcgis.GeoAccessor.from_featureclass( os.path.join(self.dir_name, "../test_data/facility_service_areas.shp")) s2 = arcgis.GeoAccessor.from_featureclass( os.path.join(self.dir_name, "../test_data/facility2_service_areas.shp")) coverage = Coverage.from_spatially_enabled_dataframes( d, s, demand_id_col, supply_id_col, demand_col=demand_col) coverage2 = Coverage.from_spatially_enabled_dataframes( d, s2, demand_id_col, supply_id_col, demand_name=coverage.demand_name, demand_col=demand_col) problem = Problem.lscp([coverage, coverage2]) problem.solve(pulp.GLPK()) selected_locations = problem.selected_supply(coverage) selected_locations2 = problem.selected_supply(coverage2) covered_demand = d.query( f"{demand_id_col} in ({[f'{i}' for i in problem.selected_demand(coverage)]})" ) coverage = math.ceil( (covered_demand[demand_col].sum() / d[demand_col].sum()) * 100) assert (len(selected_locations) >= 5) assert (len(selected_locations2) >= 17) assert (coverage == 100)
def test_init_invalid_demand_col(self, binary_coverage_dataframe): with pytest.raises(TypeError) as e: c = Coverage(binary_coverage_dataframe, demand_col=[], coverage_type="partial") assert (e.value.args[0] == "Expected 'str' type for demand_col, got '<class 'list'>'")
def test_from_coverage_dataframe_invalid_supply_id_col2( self, demand_points_dataframe, facility_service_areas_dataframe): with pytest.raises(ValueError) as e: c = Coverage.from_geodataframes(demand_points_dataframe, facility_service_areas_dataframe, "GEOID10", "test") assert (e.value.args[0] == f"'test' not in dataframe")
def test_from_coverage_sedf_invalid_demand_id_col2( self, demand_points_sedf, facility_service_areas_sedf): with pytest.raises(ValueError) as e: c = Coverage.from_spatially_enabled_dataframes( demand_points_sedf, facility_service_areas_sedf, "test", "ORIG_ID") assert (e.value.args[0] == f"'test' not in dataframe")
def test_init_invalid_demand_col3(self, binary_coverage_dataframe): with pytest.raises(ValueError) as e: c = Coverage(binary_coverage_dataframe, demand_col=None, coverage_type="partial") assert (e.value.args[0] == "'demand_col' is required when generating partial coverage")
def test_init_invalid_demand(self): with pytest.raises(TypeError) as e: c = Coverage(None, "Population") assert ( e.value.args[0] == "Expected 'Dataframe' type for dataframe, got '<class 'NoneType'>'" )
def test_from_coverage_sedf_invalid_supply_df(self, demand_points_sedf): with pytest.raises(TypeError) as e: c = Coverage.from_spatially_enabled_dataframes( demand_points_sedf, None, "GEOID10", "ORIG_ID") assert ( e.value.args[0] == "Expected 'Dataframe' type for supply_df, got '<class 'NoneType'>'" )
def test_from_coverage_dataframe_demand_col( self, demand_points_dataframe, facility_service_areas_dataframe): c = Coverage.from_geodataframes(demand_points_dataframe, facility_service_areas_dataframe, "GEOID10", "ORIG_ID", demand_col="Population") assert (isinstance(c, Coverage)) assert c.demand_col == "Population"
def test_from_coverage_dataframe_supply_name( self, demand_points_dataframe, facility_service_areas_dataframe): c = Coverage.from_geodataframes(demand_points_dataframe, facility_service_areas_dataframe, "GEOID10", "ORIG_ID", supply_name="test") assert (isinstance(c, Coverage)) assert c.supply_name == "test"
def test_from_coverage_dataframe_invalid_coverage_type( self, demand_points_dataframe, facility_service_areas_dataframe): with pytest.raises(ValueError) as e: c = Coverage.from_geodataframes(demand_points_dataframe, facility_service_areas_dataframe, "GEOID10", "ORIG_ID", coverage_type="test") assert (e.value.args[0] == "Invalid coverage type 'test'")
def test_from_coverage_sedf_invalid_supply_id_col( self, demand_points_sedf, facility_service_areas_sedf): with pytest.raises(TypeError) as e: c = Coverage.from_spatially_enabled_dataframes( demand_points_sedf, facility_service_areas_sedf, "GEOID10", None) assert ( e.value.args[0] == "Expected 'str' type for demand_id_col, got '<class 'NoneType'>'")
def test_from_coverage_dataframe_invalid_demand_id_col( self, demand_points_dataframe, facility_service_areas_dataframe): with pytest.raises(TypeError) as e: c = Coverage.from_geodataframes(demand_points_dataframe, facility_service_areas_dataframe, None, "ORIG_ID") assert ( e.value.args[0] == "Expected 'str' type for demand_id_col, got '<class 'NoneType'>'")
def test_from_coverage_sedf_invalid_demand_df(self, facility_service_areas_sedf): with pytest.raises(TypeError) as e: c = Coverage.from_spatially_enabled_dataframes( None, facility_service_areas_sedf, "GEOID10", "ORIG_ID") assert ( e.value.args[0] == "Expected 'Dataframe' type for demand_df, got '<class 'NoneType'>'" )
def test_from_coverage_dataframe_partial(self, demand_polygon_dataframe, facility_service_areas_dataframe): c = Coverage.from_geodataframes(demand_polygon_dataframe, facility_service_areas_dataframe, "GEOID10", "ORIG_ID", coverage_type="partial", demand_col="Population") assert (isinstance(c, Coverage)) assert (c.coverage_type == "partial")
def test_from_coverage_sedf_supply_name(self, demand_points_sedf, facility_service_areas_sedf): c = Coverage.from_spatially_enabled_dataframes( demand_points_sedf, facility_service_areas_sedf, "GEOID10", "ORIG_ID", supply_name="test") assert (isinstance(c, Coverage)) assert c.supply_name == "test"
def test_from_coverage_sedf_demand_col(self, demand_points_sedf, facility_service_areas_sedf): c = Coverage.from_spatially_enabled_dataframes( demand_points_sedf, facility_service_areas_sedf, "GEOID10", "ORIG_ID", demand_col="Population") assert (isinstance(c, Coverage)) assert c.demand_col == "Population"
def test_from_coverage_dataframe_demand_col_required( self, demand_points_dataframe, facility_service_areas_dataframe): with pytest.raises(ValueError) as e: c = Coverage.from_geodataframes(demand_points_dataframe, facility_service_areas_dataframe, "GEOID10", "ORIG_ID", coverage_type="partial") assert (e.value.args[0] == "demand_col is required when generating partial coverage")
def test_multiple_supply(self): demand_id_col = "GEOID10" supply_id_col = "ORIG_ID" demand_col = "Population" d = geopandas.read_file( os.path.join(self.dir_name, "../test_data/demand_point.shp")) s = geopandas.read_file( os.path.join(self.dir_name, "../test_data/facility_service_areas.shp")) s2 = geopandas.read_file( os.path.join(self.dir_name, "../test_data/facility2_service_areas.shp")) coverage = Coverage.from_geodataframes(d, s, demand_id_col, supply_id_col, demand_col=demand_col) coverage2 = Coverage.from_geodataframes( d, s2, demand_id_col, supply_id_col, demand_col=demand_col, demand_name=coverage.demand_name) problem = Problem.mclp([coverage, coverage2], max_supply={ coverage: 5, coverage2: 10 }) problem.solve(pulp.GLPK()) selected_locations = problem.selected_supply(coverage) selected_locations2 = problem.selected_supply(coverage2) covered_demand = d.query( f"{demand_id_col} in ({[f'{i}' for i in problem.selected_demand(coverage)]})" ) result = math.ceil( (covered_demand[demand_col].sum() / d[demand_col].sum()) * 100) assert (len(selected_locations) == 5) assert (len(selected_locations2) == 10) assert result == 96
def test_single_supply_arcgis(self): demand_id_col = "GEOID10" supply_id_col = "ORIG_ID" d = arcgis.GeoAccessor.from_featureclass( os.path.join(self.dir_name, "../test_data/demand_point.shp")) s = arcgis.GeoAccessor.from_featureclass( os.path.join(self.dir_name, "../test_data/facility_service_areas.shp")) coverage = Coverage.from_spatially_enabled_dataframes( d, s, demand_id_col, supply_id_col) problem = Problem.lscp(coverage) with pytest.raises((InfeasibleException, UndefinedException)) as e: problem.solve(pulp.GLPK())
def test_single_supply(self): demand_id_col = "GEOID10" supply_id_col = "ORIG_ID" d = geopandas.read_file( os.path.join(self.dir_name, "../test_data/demand_point.shp")) s = geopandas.read_file( os.path.join(self.dir_name, "../test_data/facility_service_areas.shp")) coverage = Coverage.from_geodataframes(d, s, demand_id_col, supply_id_col) problem = Problem.lscp(coverage) with pytest.raises((InfeasibleException, UndefinedException)) as e: problem.solve(pulp.GLPK())
def test_single_supply(self): demand_id_col = "GEOID10" supply_id_col = "ORIG_ID" demand_col = "Population" d = geopandas.read_file( os.path.join(self.dir_name, "../test_data/demand_point.shp")) s = geopandas.read_file( os.path.join(self.dir_name, "../test_data/facility_service_areas.shp")) coverage = Coverage.from_geodataframes(d, s, demand_id_col, supply_id_col, demand_col=demand_col) problem = Problem.mclp(coverage, max_supply={coverage: 5}) problem.solve(pulp.GLPK()) covered_demand = d.query( f"{demand_id_col} in ({[f'{i}' for i in problem.selected_demand(coverage)]})" ) result = math.ceil( (covered_demand[demand_col].sum() / d[demand_col].sum()) * 100) assert result == 53
def test_init_invalid_coverage_type2(self, binary_coverage_dataframe): with pytest.raises(ValueError) as e: c = Coverage(binary_coverage_dataframe, coverage_type="test") assert (e.value.args[0] == "Invalid coverage type 'test'")
def test_demand_name_property(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe, "Population", demand_name="test") assert (c.demand_name == "test")
def test_init(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe, "Population") assert (isinstance(c, Coverage))
def test_init_invalid_coverage_type(self, binary_coverage_dataframe): with pytest.raises(TypeError) as e: c = Coverage(binary_coverage_dataframe, coverage_type=[]) assert (e.value.args[0] == "Expected 'str' type for coverage_type, got '<class 'list'>'")
def test_demand_col_property2(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe) assert (c.demand_col is None)
def test_demand_col_property(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe, "Population") assert (c.demand_col == "Population")
def test_coverage_type_property2(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe, "Population", coverage_type="partial") assert (c.coverage_type == "partial")
def test_supply_name_property(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe, "Population", supply_name="test") assert (c.supply_name == "test")
def test_supply_name_property_default(self, binary_coverage_dataframe): c = Coverage(binary_coverage_dataframe, "Population") assert (isinstance(c.supply_name, str))