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

testing overhaul, etc #279

Merged
merged 4 commits into from
Oct 17, 2022
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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ignore_errors = True
omit =
*/tests/*
*__init__.py
*_version.py

[html]
directory = coverage_html_report
6 changes: 2 additions & 4 deletions spopt/BaseClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@


class BaseSpOptSolver(ABC):
"""Base class for all spatial optimization model solvers.

"""
"""Base class for all spatial optimization model solvers."""

@abstractmethod
def solve(self):
Expand Down Expand Up @@ -56,4 +54,4 @@ class BaseSpOptHeuristicSolver(BaseSpOptSolver):
@abstractmethod
def solve(self):
"""Solve the optimization model."""
pass
pass
28 changes: 18 additions & 10 deletions spopt/locate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def client_facility_array(self) -> None:
self.cli2fac[fac_site].append(i)
else:
raise AttributeError(
"The attribute `fac2cli` is not set. See `facility_client_array` method to set the attribute"
"The attribute `fac2cli` is not set. "
"See `facility_client_array` method to set the attribute"
)


Expand Down Expand Up @@ -311,7 +312,7 @@ def add_set_covering_constraint(
)
else:
raise AttributeError(
"before setting constraints must set facility variable"
"Before setting coverage constraints facility variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -364,7 +365,8 @@ def add_backup_covering_constraint(
)
else:
raise AttributeError(
"before setting constraints must set facility variable"
"Before setting backup coverage constraints "
"facility variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -393,7 +395,7 @@ def add_facility_constraint(
model += pulp.lpSum(fac_vars) == p_facilities
else:
raise AttributeError(
"before setting constraints must set facility variable"
"Before setting facility constraint facility variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -424,7 +426,8 @@ def add_predefined_facility_constraint(
fac_vars[ind].fixValue()
else:
raise AttributeError(
"before predefined facility must set facility variable"
"Before setting predefined facility constraints "
"facility variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -463,7 +466,8 @@ def add_maximal_coverage_constraint(
)
else:
raise AttributeError(
"before setting constraints must set facility and demand variable"
"Before setting maximal coverage constraints facility "
"and demand variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -496,7 +500,8 @@ def add_assignment_constraint(
model += pulp.lpSum([cli_assgn_vars[i][j] for j in range_facility]) == 1
else:
raise AttributeError(
"before setting constraints must set client assignment variable"
"Before setting assignment constraints "
"client assignment variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -531,7 +536,8 @@ def add_opening_constraint(
model += fac_vars[j] - cli_assgn_vars[i][j] >= 0
else:
raise AttributeError(
"before setting constraints must set client assignment variable"
"Before setting opening constraints "
"client assignment variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -579,7 +585,8 @@ def add_minimized_maximum_constraint(
)
else:
raise AttributeError(
"before setting constraints must set weight and client assignment variables"
"Before setting minimized maximum constraints weight "
"and client assignment variables must be set."
)

@staticmethod
Expand Down Expand Up @@ -621,5 +628,6 @@ def add_p_dispersion_interfacility_constraint(
)
else:
raise AttributeError(
"before setting constraints must set dispersion objective value and facility assignment variables"
"Before setting interfacility distance constraints dispersion "
"objective value and facility assignment variables must be set."
)
51 changes: 24 additions & 27 deletions spopt/locate/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,25 @@ def from_geodataframe(
dem_type_geom = dem.geom_type.unique()
fac_type_geom = fac.geom_type.unique()

_msg = (
" geodataframe contains mixed type geometries or is not a point. Be "
"sure deriving centroid from geometries doesn't affect the results."
)
if len(dem_type_geom) > 1 or not "Point" in dem_type_geom:
warnings.warn(
"Demand geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Demand{_msg}", UserWarning)
dem = dem.centroid

if len(fac_type_geom) > 1 or not "Point" in fac_type_geom:
warnings.warn(
"Facility geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Facility{_msg}", UserWarning)
fac = fac.centroid

dem_data = np.array([dem.x.to_numpy(), dem.y.to_numpy()]).T
fac_data = np.array([fac.x.to_numpy(), fac.y.to_numpy()]).T

if gdf_demand.crs != gdf_fac.crs:
raise ValueError(
f"geodataframes crs are different: gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
"Geodataframes crs are different: "
f"gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
)

distances = cdist(dem_data, fac_data, distance_metric)
Expand Down Expand Up @@ -554,26 +553,25 @@ def from_geodataframe(
dem_type_geom = dem.geom_type.unique()
fac_type_geom = fac.geom_type.unique()

_msg = (
" geodataframe contains mixed type geometries or is not a point. Be "
"sure deriving centroid from geometries doesn't affect the results."
)
if len(dem_type_geom) > 1 or not "Point" in dem_type_geom:
warnings.warn(
"Demand geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Demand{_msg}", UserWarning)
dem = dem.centroid

if len(fac_type_geom) > 1 or not "Point" in fac_type_geom:
warnings.warn(
"Facility geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Facility{_msg}", UserWarning)
fac = fac.centroid

dem_data = np.array([dem.x.to_numpy(), dem.y.to_numpy()]).T
fac_data = np.array([fac.x.to_numpy(), fac.y.to_numpy()]).T

if gdf_demand.crs != gdf_fac.crs:
raise ValueError(
f"geodataframes crs are different: gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
"Geodataframes crs are different: "
f"gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
)

distances = cdist(dem_data, fac_data, distance_metric)
Expand Down Expand Up @@ -898,26 +896,25 @@ def from_geodataframe(
dem_type_geom = dem.geom_type.unique()
fac_type_geom = fac.geom_type.unique()

_msg = (
" geodataframe contains mixed type geometries or is not a point. Be "
"sure deriving centroid from geometries doesn't affect the results."
)
if len(dem_type_geom) > 1 or not "Point" in dem_type_geom:
warnings.warn(
"Demand geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Demand{_msg}", UserWarning)
dem = dem.centroid

if len(fac_type_geom) > 1 or not "Point" in fac_type_geom:
warnings.warn(
"Facility geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Facility{_msg}", UserWarning)
fac = fac.centroid

dem_data = np.array([dem.x.to_numpy(), dem.y.to_numpy()]).T
fac_data = np.array([fac.x.to_numpy(), fac.y.to_numpy()]).T

if gdf_demand.crs != gdf_fac.crs:
raise ValueError(
f"geodataframes crs are different: gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
"Geodataframes crs are different: "
f"gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
)

distances = cdist(dem_data, fac_data, distance_metric)
Expand Down
21 changes: 10 additions & 11 deletions spopt/locate/p_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PCenter(LocateSolver, BaseOutputMixin):
cli2fac: np.array
2-d MxN, where m is number of clients and n is number of facilities. Each row represent a client and has an array containing facility index meaning that the client is covered by the facility ith.
aij: np.array
Cost matrix 2-d array
Cost matrix 2-d array

"""

Expand Down Expand Up @@ -244,26 +244,25 @@ def from_geodataframe(
dem_type_geom = dem.geom_type.unique()
fac_type_geom = fac.geom_type.unique()

_msg = (
" geodataframe contains mixed type geometries or is not a point. Be "
"sure deriving centroid from geometries doesn't affect the results."
)
if len(dem_type_geom) > 1 or not "Point" in dem_type_geom:
warnings.warn(
"Demand geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Demand{_msg}", UserWarning)
dem = dem.centroid

if len(fac_type_geom) > 1 or not "Point" in fac_type_geom:
warnings.warn(
"Facility geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Facility{_msg}", UserWarning)
fac = fac.centroid

dem_data = np.array([dem.x.to_numpy(), dem.y.to_numpy()]).T
fac_data = np.array([fac.x.to_numpy(), fac.y.to_numpy()]).T

if gdf_demand.crs != gdf_fac.crs:
raise ValueError(
f"geodataframes crs are different: gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
"Geodataframes crs are different: "
f"gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
)

distances = cdist(dem_data, fac_data, distance_metric)
Expand Down Expand Up @@ -301,7 +300,7 @@ def solve(self, solver: pulp.LpSolver, results: bool = True):
----------
solver: pulp.LpSolver
solver supported by pulp package

results: bool
if True it will create metainfo - which facilities cover which demand and vice-versa, and the uncovered demand - about the model results

Expand Down
8 changes: 6 additions & 2 deletions spopt/locate/p_dispersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,12 @@ def from_geodataframe(

if len(fac_type_geom) > 1 or not "Point" in fac_type_geom:
warnings.warn(
"Facility geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
(
"Facility geodataframe contains mixed type geometries "
"or is not a point. Be sure deriving centroid from "
"geometries doesn't affect the results."
),
UserWarning,
)
fac = fac.centroid

Expand Down
21 changes: 10 additions & 11 deletions spopt/locate/p_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PMedian(LocateSolver, BaseOutputMixin, MeanDistanceMixin):
cli2fac: np.array
2-d MxN, where m is number of clients and n is number of facilities. Each row represent a client and has an array containing facility index meaning that the client is covered by the facility ith.
aij: np.array
Cost matrix 2-d array
Cost matrix 2-d array

"""

Expand Down Expand Up @@ -280,26 +280,25 @@ def from_geodataframe(
dem_type_geom = dem.geom_type.unique()
fac_type_geom = fac.geom_type.unique()

_msg = (
" geodataframe contains mixed type geometries or is not a point. Be "
"sure deriving centroid from geometries doesn't affect the results."
)
if len(dem_type_geom) > 1 or not "Point" in dem_type_geom:
warnings.warn(
"Demand geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Demand{_msg}", UserWarning)
dem = dem.centroid

if len(fac_type_geom) > 1 or not "Point" in fac_type_geom:
warnings.warn(
"Facility geodataframe contains mixed type geometries or is not a point. Be sure deriving centroid from geometries doesn't affect the results.",
Warning,
)
warnings.warn(f"Facility{_msg}", UserWarning)
fac = fac.centroid

dem_data = np.array([dem.x.to_numpy(), dem.y.to_numpy()]).T
fac_data = np.array([fac.x.to_numpy(), fac.y.to_numpy()]).T

if gdf_demand.crs != gdf_fac.crs:
raise ValueError(
f"geodataframes crs are different: gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
"Geodataframes crs are different: "
f"gdf_demand-{gdf_demand.crs}, gdf_fac-{gdf_fac.crs}"
)

distances = cdist(dem_data, fac_data, distance_metric)
Expand Down Expand Up @@ -329,7 +328,7 @@ def facility_client_array(self) -> None:

self.fac2cli.append(array_cli)

def solve(self, solver: pulp.LpSolver, results: bool=True):
def solve(self, solver: pulp.LpSolver, results: bool = True):
"""
Solve the PMedian model

Expand Down
10 changes: 5 additions & 5 deletions spopt/region/azp.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class AZP_orig:
labels_ : numpy.ndarray
Each element is a region label specifying to which region the
corresponding area was assigned to by the last run of a fit-method.

"""

def __init__(self, allow_move_strategy=None, random_state=None):
Expand All @@ -175,7 +175,7 @@ def __init__(self, allow_move_strategy=None, random_state=None):
instance can be passed as argument. Default is ``None``.
random_state : None, int, str, bytes, or bytearray
Random seed. Default is ``None``.

"""

self.n_regions = None
Expand Down Expand Up @@ -225,7 +225,7 @@ def fit_from_scipy_sparse_matrix(
objective_func : region.objective_function.ObjectiveFunction
The objective function to use. Default is
``ObjectiveFunctionPairwise()``.

"""

if attr.ndim == 1:
Expand Down Expand Up @@ -375,7 +375,7 @@ def fit_from_geodataframe(
``fit_from_scipy_sparse_matrix``.
contiguity : str
Defines the contiguity relationship between areas.
Default is ``'rook'``. Possible contiguity definitions are:
Default is ``'rook'``. Possible contiguity definitions are:

* "rook" - Rook contiguity.
* "queen" - Queen contiguity.
Expand Down Expand Up @@ -750,7 +750,7 @@ def fit_from_networkx(
Refer to the corresponding argument in
``AZP.fit_from_networkx``.
Default is ``ObjectiveFunctionPairwise()``.

"""

adj = nx.to_scipy_sparse_matrix(graph)
Expand Down
Loading