Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
files: "spopt\/|notebooks\/"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.11.13"
rev: "v0.12.2"
hooks:
- id: ruff-check
- id: ruff-format
Expand Down
12 changes: 6 additions & 6 deletions spopt/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
GPD_GE_10 = Version(geopandas.__version__) >= Version("1.0")


def dirpath() -> pathlib.Path:
"""Path to test data directory"""
return pathlib.Path(__file__).absolute().parent / "data"
def locate_dirpath() -> pathlib.Path:
"""Path to locate test data directory"""
return pathlib.Path(__file__).absolute().parent / "test_locate" / "data"


@pytest.fixture
def load_test_data():
def load_locate_test_data():
"""Load test data for the ``locate`` module."""

def _load_test_data(_file: str) -> dict | pandas.DataFrame:
if _file.endswith(".pkl"):
with open(dirpath() / _file, "rb") as f:
with open(locate_dirpath() / _file, "rb") as f:
test_data = pickle.load(f)
elif _file.endswith(".csv"):
test_data = pandas.read_csv(dirpath() / _file)
test_data = pandas.read_csv(locate_dirpath() / _file)
else:
raise FileNotFoundError(f"`{_file}` does not exist.")

Expand Down
1 change: 0 additions & 1 deletion spopt/tests/data/model_output.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ def test_c_p_median_with_predefined_facilities_infeasible(

class TestRealWorldLocate:
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
time_table = load_test_data("example_subject_student_school_journeys.csv")
def setup_method(self, load_locate_test_data) -> None:
time_table = load_locate_test_data(
"example_subject_student_school_journeys.csv"
)

self.cost_matrix = (
time_table.pivot_table(
Expand All @@ -94,8 +96,8 @@ def setup_method(self, load_test_data) -> None:
.values
)

self.demand_points = load_test_data("example_subject_students.csv")
self.facility_points = load_test_data("example_subject_schools.csv")
self.demand_points = load_locate_test_data("example_subject_students.csv")
self.facility_points = load_locate_test_data("example_subject_schools.csv")

self.p_facility = 10
self.demand = numpy.ones(len(self.demand_points))
Expand Down
File renamed without changes.
32 changes: 18 additions & 14 deletions spopt/tests/test_lscp.py → spopt/tests/test_locate/test_lscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_lscp_from_cost_matrix_no_results(self):
with pytest.raises(AttributeError):
result.fac2cli # noqa: B018

def test_lscp_facility_client_array_from_cost_matrix(self, load_test_data):
lscp_objective = load_test_data("lscp_fac2cli.pkl")
def test_lscp_facility_client_array_from_cost_matrix(self, load_locate_test_data):
lscp_objective = load_locate_test_data("lscp_fac2cli.pkl")

lscp = LSCP.from_cost_matrix(self.cost_matrix, 8)
lscp = lscp.solve(pulp.PULP_CBC_CMD(msg=False))
Expand All @@ -43,8 +43,8 @@ def test_lscp_facility_client_array_from_cost_matrix(self, load_test_data):
numpy.array(lscp_objective, dtype=object),
)

def test_lscp_client_facility_array_from_cost_matrix(self, load_test_data):
lscp_objective = load_test_data("lscp_cli2fac.pkl")
def test_lscp_client_facility_array_from_cost_matrix(self, load_locate_test_data):
lscp_objective = load_locate_test_data("lscp_cli2fac.pkl")

lscp = LSCP.from_cost_matrix(self.cost_matrix, 8)
lscp = lscp.solve(pulp.PULP_CBC_CMD(msg=False))
Expand All @@ -61,8 +61,8 @@ def test_lscp_from_geodataframe(self):
result = lscp.solve(pulp.PULP_CBC_CMD(msg=False))
assert isinstance(result, LSCP)

def test_lscp_facility_client_array_from_geodataframe(self, load_test_data):
lscp_objective = load_test_data("lscp_geodataframe_fac2cli.pkl")
def test_lscp_facility_client_array_from_geodataframe(self, load_locate_test_data):
lscp_objective = load_locate_test_data("lscp_geodataframe_fac2cli.pkl")

lscp = LSCP.from_geodataframe(
self.clients_snapped,
Expand All @@ -78,8 +78,8 @@ def test_lscp_facility_client_array_from_geodataframe(self, load_test_data):
numpy.array(lscp_objective, dtype=object),
)

def test_lscp_client_facility_array_from_geodataframe(self, load_test_data):
lscp_objective = load_test_data("lscp_geodataframe_cli2fac.pkl")
def test_lscp_client_facility_array_from_geodataframe(self, load_locate_test_data):
lscp_objective = load_locate_test_data("lscp_geodataframe_cli2fac.pkl")

lscp = LSCP.from_geodataframe(
self.clients_snapped,
Expand All @@ -96,9 +96,11 @@ def test_lscp_client_facility_array_from_geodataframe(self, load_test_data):
)

def test_lscp_preselected_facility_client_array_from_geodataframe(
self, load_test_data
self, load_locate_test_data
):
lscp_objective = load_test_data("lscp_preselected_loc_geodataframe_fac2cli.pkl")
lscp_objective = load_locate_test_data(
"lscp_preselected_loc_geodataframe_fac2cli.pkl"
)

fac_snapped = self.facilities_snapped.copy()

Expand All @@ -122,8 +124,8 @@ def test_lscp_preselected_facility_client_array_from_geodataframe(

class TestRealWorldLSCP:
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
network_distance = load_test_data(
def setup_method(self, load_locate_test_data) -> None:
network_distance = load_locate_test_data(
"SF_network_distance_candidateStore_16_censusTract_205_new.csv"
)

Expand All @@ -133,8 +135,10 @@ def setup_method(self, load_test_data) -> None:

self.cost_matrix = ntw_dist_piv.to_numpy()

demand_points = load_test_data("SF_demand_205_centroid_uniform_weight.csv")
facility_points = load_test_data("SF_store_site_16_longlat.csv")
demand_points = load_locate_test_data(
"SF_demand_205_centroid_uniform_weight.csv"
)
facility_points = load_locate_test_data("SF_store_site_16_longlat.csv")

self.facility_points_gdf = (
geopandas.GeoDataFrame(
Expand Down
30 changes: 16 additions & 14 deletions spopt/tests/test_lscpb.py → spopt/tests/test_locate/test_lscpb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_lscpb_from_cost_matrix_no_results(self):
with pytest.raises(AttributeError):
result.backup_perc # noqa: B018

def test_lscpb_facility_client_array_from_cost_matrix(self, load_test_data):
lscpb_objective = load_test_data("lscpb_fac2cli.pkl")
def test_lscpb_facility_client_array_from_cost_matrix(self, load_locate_test_data):
lscpb_objective = load_locate_test_data("lscpb_fac2cli.pkl")

lscpb = LSCPB.from_cost_matrix(
self.cost_matrix, 8, pulp.PULP_CBC_CMD(msg=False)
Expand All @@ -56,8 +56,8 @@ def test_lscpb_facility_client_array_from_cost_matrix(self, load_test_data):
numpy.array(lscpb_objective, dtype=object),
)

def test_lscpb_client_facility_array_from_cost_matrix(self, load_test_data):
lscpb_objective = load_test_data("lscpb_cli2fac.pkl")
def test_lscpb_client_facility_array_from_cost_matrix(self, load_locate_test_data):
lscpb_objective = load_locate_test_data("lscpb_cli2fac.pkl")

lscpb = LSCPB.from_cost_matrix(
self.cost_matrix, 8, pulp.PULP_CBC_CMD(msg=False)
Expand All @@ -82,8 +82,8 @@ def test_lscpb_from_geodataframe(self):

assert isinstance(result, LSCPB)

def test_lscpb_facility_client_array_from_geodataframe(self, load_test_data):
lscpb_objective = load_test_data("lscpb_geodataframe_fac2cli.pkl")
def test_lscpb_facility_client_array_from_geodataframe(self, load_locate_test_data):
lscpb_objective = load_locate_test_data("lscpb_geodataframe_fac2cli.pkl")

lscpb = LSCPB.from_geodataframe(
self.clients_snapped,
Expand All @@ -100,8 +100,8 @@ def test_lscpb_facility_client_array_from_geodataframe(self, load_test_data):
numpy.array(lscpb_objective, dtype=object),
)

def test_lscpb_client_facility_array_from_geodataframe(self, load_test_data):
lscpb_objective = load_test_data("lscpb_geodataframe_cli2fac.pkl")
def test_lscpb_client_facility_array_from_geodataframe(self, load_locate_test_data):
lscpb_objective = load_locate_test_data("lscpb_geodataframe_cli2fac.pkl")

lscpb = LSCPB.from_geodataframe(
self.clients_snapped,
Expand All @@ -119,9 +119,9 @@ def test_lscpb_client_facility_array_from_geodataframe(self, load_test_data):
)

def test_lscpb_preselected_facility_client_array_from_geodataframe(
self, load_test_data
self, load_locate_test_data
):
lscpb_objective = load_test_data(
lscpb_objective = load_locate_test_data(
"lscpb_preselected_loc_geodataframe_fac2cli.pkl"
)

Expand All @@ -147,8 +147,8 @@ def test_lscpb_preselected_facility_client_array_from_geodataframe(

class TestRealWorldLocate:
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
network_distance = load_test_data(
def setup_method(self, load_locate_test_data) -> None:
network_distance = load_locate_test_data(
"SF_network_distance_candidateStore_16_censusTract_205_new.csv"
)

Expand All @@ -158,8 +158,10 @@ def setup_method(self, load_test_data) -> None:

self.cost_matrix = ntw_dist_piv.to_numpy()

demand_points = load_test_data("SF_demand_205_centroid_uniform_weight.csv")
facility_points = load_test_data("SF_store_site_16_longlat.csv")
demand_points = load_locate_test_data(
"SF_demand_205_centroid_uniform_weight.csv"
)
facility_points = load_locate_test_data("SF_store_site_16_longlat.csv")

self.facility_points_gdf = (
geopandas.GeoDataFrame(
Expand Down
32 changes: 18 additions & 14 deletions spopt/tests/test_mclp.py → spopt/tests/test_locate/test_mclp.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def test_mclp_from_cost_matrix_no_results(self):
with pytest.raises(AttributeError):
result.perc_cov # noqa: B018

def test_mclp_facility_client_array_from_cost_matrix(self, load_test_data):
mclp_objective = load_test_data("mclp_fac2cli.pkl")
def test_mclp_facility_client_array_from_cost_matrix(self, load_locate_test_data):
mclp_objective = load_locate_test_data("mclp_fac2cli.pkl")

mclp = MCLP.from_cost_matrix(
self.cost_matrix,
Expand All @@ -64,8 +64,8 @@ def test_mclp_facility_client_array_from_cost_matrix(self, load_test_data):
numpy.array(mclp_objective, dtype=object),
)

def test_mclp_client_facility_array_from_cost_matrix(self, load_test_data):
mclp_objective = load_test_data("mclp_cli2fac.pkl")
def test_mclp_client_facility_array_from_cost_matrix(self, load_locate_test_data):
mclp_objective = load_locate_test_data("mclp_cli2fac.pkl")

mclp = MCLP.from_cost_matrix(
self.cost_matrix,
Expand Down Expand Up @@ -93,8 +93,8 @@ def test_mclp_from_geodataframe(self):
result = mclp.solve(pulp.PULP_CBC_CMD(msg=False))
assert isinstance(result, MCLP)

def test_mclp_facility_client_array_from_geodataframe(self, load_test_data):
mclp_objective = load_test_data("mclp_geodataframe_fac2cli.pkl")
def test_mclp_facility_client_array_from_geodataframe(self, load_locate_test_data):
mclp_objective = load_locate_test_data("mclp_geodataframe_fac2cli.pkl")

mclp = MCLP.from_geodataframe(
self.clients_snapped,
Expand All @@ -113,9 +113,11 @@ def test_mclp_facility_client_array_from_geodataframe(self, load_test_data):
)

def test_mclp_preselected_facility_client_array_from_geodataframe(
self, load_test_data
self, load_locate_test_data
):
mclp_objective = load_test_data("mclp_preselected_loc_geodataframe_fac2cli.pkl")
mclp_objective = load_locate_test_data(
"mclp_preselected_loc_geodataframe_fac2cli.pkl"
)

fac_snapped = self.facilities_snapped.copy()

Expand All @@ -138,8 +140,8 @@ def test_mclp_preselected_facility_client_array_from_geodataframe(
numpy.array(mclp_objective, dtype=object),
)

def test_mclp_client_facility_array_from_geodataframe(self, load_test_data):
mclp_objective = load_test_data("mclp_geodataframe_cli2fac.pkl")
def test_mclp_client_facility_array_from_geodataframe(self, load_locate_test_data):
mclp_objective = load_locate_test_data("mclp_geodataframe_cli2fac.pkl")

mclp = MCLP.from_geodataframe(
self.clients_snapped,
Expand All @@ -160,8 +162,8 @@ def test_mclp_client_facility_array_from_geodataframe(self, load_test_data):

class TestRealWorldLocate:
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
network_distance = load_test_data(
def setup_method(self, load_locate_test_data) -> None:
network_distance = load_locate_test_data(
"SF_network_distance_candidateStore_16_censusTract_205_new.csv"
)

Expand All @@ -171,8 +173,10 @@ def setup_method(self, load_test_data) -> None:

self.cost_matrix = ntw_dist_piv.to_numpy()

demand_points = load_test_data("SF_demand_205_centroid_uniform_weight.csv")
facility_points = load_test_data("SF_store_site_16_longlat.csv")
demand_points = load_locate_test_data(
"SF_demand_205_centroid_uniform_weight.csv"
)
facility_points = load_locate_test_data("SF_store_site_16_longlat.csv")

self.facility_points_gdf = (
geopandas.GeoDataFrame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ def test_p_center_from_cost_matrix_no_results(self):
with pytest.raises(AttributeError):
result.fac2cli # noqa: B018

def test_pcenter_facility_client_array_from_cost_matrix(self, load_test_data):
pcenter_objective = load_test_data("pcenter_fac2cli.pkl")
def test_pcenter_facility_client_array_from_cost_matrix(
self, load_locate_test_data
):
pcenter_objective = load_locate_test_data("pcenter_fac2cli.pkl")

pcenter = PCenter.from_cost_matrix(self.cost_matrix, p_facilities=4)
pcenter = pcenter.solve(pulp.PULP_CBC_CMD(msg=False))
Expand All @@ -54,8 +56,10 @@ def test_p_center_from_geodataframe(self):
result = p_center.solve(pulp.PULP_CBC_CMD(msg=False))
assert isinstance(result, PCenter)

def test_pcenter_facility_client_array_from_geodataframe(self, load_test_data):
pcenter_objective = load_test_data("pcenter_geodataframe_fac2cli.pkl")
def test_pcenter_facility_client_array_from_geodataframe(
self, load_locate_test_data
):
pcenter_objective = load_locate_test_data("pcenter_geodataframe_fac2cli.pkl")

pcenter = PCenter.from_geodataframe(
self.clients_snapped,
Expand All @@ -71,8 +75,10 @@ def test_pcenter_facility_client_array_from_geodataframe(self, load_test_data):
numpy.array(pcenter_objective, dtype=object),
)

def test_pcenter_client_facility_array_from_geodataframe(self, load_test_data):
pcenter_objective = load_test_data("pcenter_geodataframe_cli2fac.pkl")
def test_pcenter_client_facility_array_from_geodataframe(
self, load_locate_test_data
):
pcenter_objective = load_locate_test_data("pcenter_geodataframe_cli2fac.pkl")

pcenter = PCenter.from_geodataframe(
self.clients_snapped,
Expand Down Expand Up @@ -117,8 +123,8 @@ def test_pcenter_preselected_facility_client_array_from_geodataframe(self):

class TestRealWorldLocate:
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
network_distance = load_test_data(
def setup_method(self, load_locate_test_data) -> None:
network_distance = load_locate_test_data(
"SF_network_distance_candidateStore_16_censusTract_205_new.csv"
)

Expand All @@ -128,8 +134,10 @@ def setup_method(self, load_test_data) -> None:

self.cost_matrix = ntw_dist_piv.to_numpy()

demand_points = load_test_data("SF_demand_205_centroid_uniform_weight.csv")
facility_points = load_test_data("SF_store_site_16_longlat.csv")
demand_points = load_locate_test_data(
"SF_demand_205_centroid_uniform_weight.csv"
)
facility_points = load_locate_test_data("SF_store_site_16_longlat.csv")

self.facility_points_gdf = (
geopandas.GeoDataFrame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def test_p_dispersion_preselected_facility_array_from_geodataframe(self):

class TestRealWorldLocate:
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
network_distance = load_test_data(
def setup_method(self, load_locate_test_data) -> None:
network_distance = load_locate_test_data(
"SF_network_distance_candidateStore_16_censusTract_205_new.csv"
)

Expand All @@ -80,7 +80,7 @@ def setup_method(self, load_test_data) -> None:

self.cost_matrix = ntw_dist_piv.to_numpy()

facility_points = load_test_data("SF_store_site_16_longlat.csv")
facility_points = load_locate_test_data("SF_store_site_16_longlat.csv")

self.facility_points_gdf = (
geopandas.GeoDataFrame(
Expand Down
Loading