Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Oct 23, 2024
1 parent 95958a2 commit 2c77aa3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
6 changes: 4 additions & 2 deletions grid2demand/_grid2demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ def net2grid(self, *,

print(" : Creating grid...")
# generate zone based on zone_id in node.csv
if self.node_dict:
if self.node_dict and hasattr(self, "node_dict_activity_nodes"):
node_dict = {**self.node_dict, **self.node_dict_activity_nodes}
elif self.node_dict:
node_dict = self.node_dict
elif hasattr(self, "node_dict_activity_nodes"):
node_dict = self.node_dict_activity_nodes
Expand Down Expand Up @@ -703,7 +705,7 @@ def save_results_to_csv(self, output_dir: str = "",
agent_time_period: str = "0700-0800",
zone_od_dist_table: bool = False,
zone_od_dist_matrix: bool = False,
overwrite_file: bool = True) -> None:
overwrite_file: bool = False) -> None:
"""save results to csv files
Args:
Expand Down
15 changes: 8 additions & 7 deletions grid2demand/func_lib/save_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ def save_node(self, overwrite_file: bool = True) -> None:

# if activity type is used, merge node_dict and node_dict_activity_nodes
if hasattr(self, "node_dict_activity_nodes"):
node_dict = {**self.node_dict, **self.node_dict_activity_nodes}
node_df = pd.concat([pd.DataFrame(self.node_dict_activity_nodes.values()),
pd.DataFrame(self.node_dict.values())])

else:
node_dict = self.node_dict
node_df = pd.DataFrame(self.node_dict.values())

node_df = pd.DataFrame(node_dict.values())
node_df.rename(columns={"id": "node_id"}, inplace=True)

# fill zone_id with None if node_id is not in node_dict_activity_nodes
node_df["zone_id"] = node_df["zone_id"].where(
node_df["node_id"].isin(self.node_dict_activity_nodes.keys()),
None) if hasattr(self, "node_dict_activity_nodes") else node_df["zone_id"]
# # fill zone_id with None if node_id is not in node_dict_activity_nodes
# node_df["zone_id"] = node_df["zone_id"].where(
# node_df["node_id"].isin(self.node_dict_activity_nodes.keys()),
# None) if hasattr(self, "node_dict_activity_nodes") else node_df["zone_id"]

node_df.to_csv(path_output, index=False)
print(f" : Successfully saved updated node to node.csv to {self.output_dir}")
Expand Down
2 changes: 1 addition & 1 deletion tutorial/demand_from_grid_use_zone_id_in_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
net.run_gravity_model()

# Step 7: Output demand, agent, zone, zone_od_dist_table, zone_od_dist_matrix files
net.save_results_to_csv(zone=True, node=True, poi=True, agent=True, overwrite_file=False)
net.save_results_to_csv(zone=False, node=True, poi=False, agent=False, overwrite_file=False)

0 comments on commit 2c77aa3

Please sign in to comment.