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

Fixes behaviour for graphs with no centroids #577

Merged
merged 1 commit into from
Oct 12, 2024
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
22 changes: 13 additions & 9 deletions aequilibrae/paths/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def default_types(self, tp: str):
else:
raise ValueError("It must be either a int or a float")

def prepare_graph(self, centroids: Optional[np.ndarray]) -> None:
def prepare_graph(self, centroids: Optional[np.ndarray] = None) -> None:
"""
Prepares the graph for a computation for a certain set of centroids

Expand Down Expand Up @@ -341,18 +341,22 @@ def set_graph(self, cost_field) -> None:
:Arguments:
**cost_field** (:obj:`str`): Field name. Must be numeric
"""
if cost_field in self.graph.columns:
self.cost_field = cost_field
if cost_field not in self.graph.columns:
raise ValueError("cost_field not available in the graph:" + str(self.graph.columns))

self.cost_field = cost_field

# We only have a compact graph if we have added centroids, as that's used for skimming and assignment
if not self.compact_graph.empty:
self.compact_cost = np.zeros(self.compact_graph.id.max() + 2, self.__float_type)
df = self.__graph_groupby.sum(numeric_only=True)[[cost_field]].reset_index()
self.compact_cost[df.index.values] = df[cost_field].values
if self.graph[cost_field].dtype == self.__float_type:
self.cost = np.array(self.graph[cost_field].values, copy=True)
else:
self.cost = np.array(self.graph[cost_field].values, dtype=self.__float_type)
self.logger.warning("Cost field with wrong type. Converting to float64")

if self.graph[cost_field].dtype == self.__float_type:
self.cost = np.array(self.graph[cost_field].values, copy=True)
else:
raise ValueError("cost_field not available in the graph:" + str(self.graph.columns))
self.cost = np.array(self.graph[cost_field].values, dtype=self.__float_type)
self.logger.warning("Cost field with wrong type. Converting to float64")

self.__build_derived_properties()

Expand Down
5 changes: 5 additions & 0 deletions tests/aequilibrae/paths/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def test_prepare_graph(self):
graph = self.project.network.graphs["c"]
graph.prepare_graph(np.arange(5) + 1)

def test_prepare_graph_no_centroids(self):
graph = self.project.network.graphs["c"]
graph.prepare_graph()
graph.set_graph("distance")

def test_set_graph(self):
self.graph.set_graph(cost_field="distance")
self.graph.set_blocked_centroid_flows(block_centroid_flows=True)
Expand Down