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

Refactor Scenarios to improve legibility and workflow management. #315

Merged
merged 20 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,19 @@ my_network.apply_project_card(...) # returns
my_network.write_roadway_network(...) # returns

## Scenario Building
my_scenario = scenario_from_network(roadway_network, transit_network)
my_scenario.add_projects(directory, keyword)
my_scenario.write_networks(directory, format)
my_scenario = Scenario.create_scenario(
base_scenario=my_base_scenario,
card_search_dir=project_card_directory,
tags = ["baseline-2050"]
)
my_scenario.apply_all_projects()
my_scenario.write("my_project/baseline", "baseline-2050")
my_scenario.summarize(outfile="scenario_summary_baseline.txt")

my_scenario.add_projects_from_files(list_of_build_project_card_files)
my_scenario.queued_projects
my_scenario.apply_all_projects()
my_scenario.write("my_project/build", "baseline")

```
## Attribution
Expand Down
17 changes: 10 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re


def define_env(env):
"""
This is the hook for defining variables, macros and filters
Expand All @@ -11,13 +12,15 @@ def define_env(env):
"""

@env.macro
def include_file(filename: str, downshift_h1 = True, start_line: int = 0, end_line: int = None):
def include_file(
filename: str, downshift_h1=True, start_line: int = 0, end_line: int = None
):
"""
Include a file, optionally indicating start_line and end_line.

args:
filename: file to include, relative to the top directory of the documentation project.
downshift_h1: If true, will downshift headings by 1 if h1 heading found. Defaults to True.
downshift_h1: If true, will downshift headings by 1 if h1 heading found. Defaults to True.
start_line (Optional): if included, will start including the file from this line
(indexed to 0)
end_line (Optional): if included, will stop including at this line (indexed to 0)
Expand All @@ -39,10 +42,10 @@ def include_file(filename: str, downshift_h1 = True, start_line: int = 0, end_li
print(f"???before downshifting! {full_filename}")
if md_heading_re[1].search(content) and downshift_h1:
print("!!!downshifting!")
content = re.sub(md_heading_re[5],r'#\1\2',content)
content = re.sub(md_heading_re[4],r'#\1\2',content)
content = re.sub(md_heading_re[3],r'#\1\2',content)
content = re.sub(md_heading_re[2],r'#\1\2',content)
content = re.sub(md_heading_re[1],r'#\1\2',content)
content = re.sub(md_heading_re[5], r"#\1\2", content)
content = re.sub(md_heading_re[4], r"#\1\2", content)
content = re.sub(md_heading_re[3], r"#\1\2", content)
content = re.sub(md_heading_re[2], r"#\1\2", content)
content = re.sub(md_heading_re[1], r"#\1\2", content)

return content
73 changes: 19 additions & 54 deletions network_wrangler/projectcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import os
import yaml
import json
from typing import Any, Collection, Mapping

from typing import List, Collection, Mapping, Any
from pathlib import Path
from jsonschema import validate
from jsonschema.exceptions import ValidationError
from jsonschema.exceptions import SchemaError
Expand All @@ -21,6 +21,8 @@ class ProjectCard(object):
valid: Boolean indicating if data conforms to project card data schema
"""

FILE_TYPES = ["wr", "wrangler", "yml", "yaml"]

TRANSIT_CATEGORIES = ["Transit Service Property Change", "Add Transit"]

# categories that may affect transit, but only as a secondary
Expand Down Expand Up @@ -110,7 +112,7 @@ def read_wrangler_card(path_to_card: str) -> dict:
_yaml, _pycode = cardfile.read().split(delim)
WranglerLogger.debug("_yaml: {}\n_pycode: {}".format(_yaml, _pycode))

attribute_dictionary = yaml.safe_load(_yaml)
attribute_dictionary = yaml.safe_load(_yaml.lower())
attribute_dictionary["file"] = path_to_card
attribute_dictionary["pycode"] = _pycode.lstrip("\n")

Expand All @@ -129,7 +131,7 @@ def read_yml(path_to_card: str) -> dict:
WranglerLogger.debug("Reading YAML-Style Project Card")

with open(path_to_card, "r") as cardfile:
attribute_dictionary = yaml.safe_load(cardfile)
attribute_dictionary = yaml.safe_load(cardfile.read().lower())
e-lo marked this conversation as resolved.
Show resolved Hide resolved
attribute_dictionary["file"] = path_to_card

return attribute_dictionary
Expand Down Expand Up @@ -196,6 +198,19 @@ def validate_project_card_schema(
except yaml.YAMLError as exc:
WranglerLogger.error(exc.message)

def has_any_tags(self, tags: Collection[str]) -> bool:
"""Returns true if ProjectCard has at lest one tag in tags list.

args:
tags: list of tags to search for
"""
if tags and set(tags).isdisjoint(self.tags):
WranglerLogger.debug(
f"Project card tags: {self.tags} don't match search tags: {tags}"
)
return False
return True

@staticmethod
def build_selection_query(
selection: Mapping[str, Any],
Expand Down Expand Up @@ -270,53 +285,3 @@ def build_selection_query(
sel_query = sel_query + ")"

return sel_query

def roadway_attribute_change(self, card: dict):
"""
Probably delete.
Reads a Roadway Attribute Change card.

args:
card: the project card stored in a dictionary
"""
WranglerLogger.info(card.get("Category"))

def new_roadway(self, card: dict):
"""
Probably delete.
Reads a New Roadway card.

args:
card: the project card stored in a dictionary
"""
WranglerLogger.info(card.get("Category"))

def transit_attribute_change(self, card: dict):
"""
Probably delete.
Reads a Transit Service Attribute Change card.

args:
card: the project card stored in a dictionary
"""
WranglerLogger.info(card.get("Category"))

def new_transit_right_of_way(self, card: dict):
"""
Probably delete.
Reads a New Transit Dedicated Right of Way card.

args:
card: the project card stored in a dictionary
"""
WranglerLogger.info(card.get("Category"))

def parallel_managed_lanes(self, card: dict):
"""
Probably delete.
Reads a Parallel Managed lanes card.

args:
card: the project card stored in a dictionary
"""
WranglerLogger.info(card.get("Category"))
58 changes: 39 additions & 19 deletions network_wrangler/roadwaynetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RoadwayNetwork(object):
Representation of a Roadway Network.

Typical usage example:

```py
net = RoadwayNetwork.read(
link_file=MY_LINK_FILE,
Expand Down Expand Up @@ -1695,8 +1695,10 @@ def update_node_geometry(self, updated_nodes: List = None) -> gpd.GeoDataFrame:
updated_nodes_df = copy.deepcopy(self.nodes_df)
updated_nodes = self.nodes_df.index.values.tolist()

if len(updated_nodes_df)<25:
WranglerLogger.debug(f"Original Nodes:\n{updated_nodes_df[['X','Y','geometry']]}")
if len(updated_nodes_df) < 25:
WranglerLogger.debug(
f"Original Nodes:\n{updated_nodes_df[['X','Y','geometry']]}"
)

updated_nodes_df["geometry"] = updated_nodes_df.apply(
lambda x: point_from_xy(
Expand All @@ -1708,14 +1710,18 @@ def update_node_geometry(self, updated_nodes: List = None) -> gpd.GeoDataFrame:
axis=1,
)
WranglerLogger.debug(f"{len(self.nodes_df)} nodes in network before update")
if len(updated_nodes_df)<25:
WranglerLogger.debug(f"Updated Nodes:\n{updated_nodes_df[['X','Y','geometry']]}")
if len(updated_nodes_df) < 25:
WranglerLogger.debug(
f"Updated Nodes:\n{updated_nodes_df[['X','Y','geometry']]}"
)
self.nodes_df.update(
updated_nodes_df[[RoadwayNetwork.UNIQUE_NODE_KEY, "geometry"]]
)
WranglerLogger.debug(f"{len(self.nodes_df)} nodes in network after update")
if len(self.nodes_df)<25:
WranglerLogger.debug(f"Updated self.nodes_df:\n{self.nodes_df[['X','Y','geometry']]}")
if len(self.nodes_df) < 25:
WranglerLogger.debug(
f"Updated self.nodes_df:\n{self.nodes_df[['X','Y','geometry']]}"
)

self._update_node_geometry_in_links_shapes(updated_nodes_df)

Expand All @@ -1729,10 +1735,16 @@ def nodes_in_links(
links_df: Links which to return node list for
"""
if len(links_df) < 25:
WranglerLogger.debug(f"Links:\n{links_df[RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE]}")
nodes_list = list(set(
pd.concat([links_df[c] for c in RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE]).tolist()
))
WranglerLogger.debug(
f"Links:\n{links_df[RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE]}"
)
nodes_list = list(
set(
pd.concat(
[links_df[c] for c in RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE]
).tolist()
)
)
if len(nodes_list) < 25:
WranglerLogger.debug(f"_node_list:\n{nodes_list}")
return nodes_list
Expand All @@ -1748,20 +1760,26 @@ def links_with_nodes(
node_id_list (list): List of nodes to find links for. Nodes should be identified
by the foreign key - the one that is referenced in LINK_FOREIGN_KEY.
"""
#If nodes are equal to all the nodes in the links, return all the links
# If nodes are equal to all the nodes in the links, return all the links
_nodes_in_links = RoadwayNetwork.nodes_in_links(links_df)
WranglerLogger.debug(f"# Nodes: {len(node_id_list)}\nNodes in links:{len(_nodes_in_links)}")
if len( set(node_id_list) - set(_nodes_in_links) ) == 0:
return links_df
WranglerLogger.debug(
f"# Nodes: {len(node_id_list)}\nNodes in links:{len(_nodes_in_links)}"
)
if len(set(node_id_list) - set(_nodes_in_links)) == 0:
return links_df

WranglerLogger.debug(f"Finding links assocated with {len(node_id_list)} nodes.")
if len(node_id_list) < 25:
WranglerLogger.debug(f"node_id_list: {node_id_list}")

_selected_links_df = links_df[
links_df.isin({c:node_id_list for c in RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE})
links_df.isin(
{c: node_id_list for c in RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE}
)
]
WranglerLogger.debug(f"Temp Selected {len(_selected_links_df)} associated with {len(node_id_list)} nodes.")
WranglerLogger.debug(
f"Temp Selected {len(_selected_links_df)} associated with {len(node_id_list)} nodes."
)
"""
_query_parts = [
f"{prop} == {str(n)}"
Expand All @@ -1772,7 +1790,9 @@ def links_with_nodes(
_query = " or ".join(_query_parts)
_selected_links_df = links_df.query(_query, engine="python")
"""
WranglerLogger.debug(f"Selected {len(_selected_links_df)} associated with {len(node_id_list)} nodes.")
WranglerLogger.debug(
f"Selected {len(_selected_links_df)} associated with {len(node_id_list)} nodes."
)

return _selected_links_df

Expand Down Expand Up @@ -2451,7 +2471,7 @@ def delete_nodes(self, del_nodes: dict, ignore_missing: bool = True) -> None:
f"Node deletion failed because being used in following links:\n{_links_with_nodes[RoadwayNetwork.LINK_FOREIGN_KEY_TO_NODE]}"
)
raise ValueError

# Check if node is in network
if RoadwayNetwork.UNIQUE_NODE_KEY in del_nodes:
_del_node_ids = pd.Series(del_nodes[RoadwayNetwork.UNIQUE_NODE_KEY])
Expand Down
Loading