Skip to content

Commit

Permalink
expand changeable attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
i-am-sijia committed Jul 1, 2021
1 parent 9ea4bb9 commit 298a3a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
9 changes: 9 additions & 0 deletions examples/settings/log_to_net.csv
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ LANES_AM,lanes_AM
LANES_MD,lanes_MD
LANES_PM,lanes_PM
LANES_NT,lanes_NT
LANES_EA,lanes_EA
LANES_EV,lanes_EV
PRICE_SOV_AM,price_sov_AM
PRICE_HOV2_AM,price_hov2_AM
PRICE_HOV3_AM,price_hov3_AM
Expand Down Expand Up @@ -66,6 +68,13 @@ ML_LANES_AM,ML_lanes_AM
ML_LANES_MD,ML_lanes_MD
ML_LANES_PM,ML_lanes_PM
ML_LANES_NT,ML_lanes_NT
ML_LANES_EA,ML_lanes_EA
ML_LANES_EV,ML_lanes_EV
MANAGED,managed
BUS_ONLY,bus_only
RAIL_ONLY,rail_only
USECLASS_EA,useclass_EA
USECLASS_AM,useclass_AM
USECLASS_MD,useclass_MD
USECLASS_PM,useclass_PM
USECLASS_EV,useclass_EV
50 changes: 26 additions & 24 deletions lasso/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def create_project(
)
base_roadway_network.split_properties_by_time_period_and_category()
elif base_roadway_network:
pass
base_roadway_network.split_properties_by_time_period_and_category()
else:
msg = "No base roadway network."
WranglerLogger.info(msg)
Expand Down Expand Up @@ -419,14 +419,17 @@ def determine_roadway_network_changes_compatability(
dbf_to_net_df = pd.read_csv(parameters.net_to_dbf_crosswalk)
dbf_to_net_dict = dict(zip(dbf_to_net_df["dbf"], dbf_to_net_df["net"]))

for c in roadway_changes.columns:
if (c not in log_to_net_df["log"].tolist()) & (c not in ["A", "B"]):
roadway_changes.rename(columns={c : c.lower()}, inplace=True)
roadway_changes.rename(columns=log_to_net_dict, inplace=True)
roadway_changes.rename(columns=dbf_to_net_dict, inplace=True)

# for links "L" that change "C",
# find locations where there isn't a base roadway link

link_changes_df = roadway_changes[
(roadway_changes.OBJECT == "L") & (roadway_changes.OPERATION == "C")
(roadway_changes["object"] == "L") & (roadway_changes["operation"] == "C")
]

link_merge_df = pd.merge(
Expand All @@ -445,9 +448,8 @@ def determine_roadway_network_changes_compatability(

# for links "N" that change "C",
# find locations where there isn't a base roadway node

node_changes_df = roadway_changes[
(roadway_changes.OBJECT == "N") & (roadway_changes.OPERATION == "C")
(roadway_changes.object == "N") & (roadway_changes["operation"] == "C")
]
node_merge_df = pd.merge(
node_changes_df[["model_node_id"]],
Expand Down Expand Up @@ -512,26 +514,26 @@ def add_highway_changes(self, limit_variables_to_existing_network=False):

## if worth it, could also add some functionality to network wrangler itself.
node_changes_df = self.roadway_changes[
self.roadway_changes.OBJECT == "N"
self.roadway_changes.object == "N"
].copy()

link_changes_df = self.roadway_changes[
self.roadway_changes.OBJECT == "L"
self.roadway_changes.object == "L"
].copy()

def _final_op(x):
if x.OPERATION_history[-1] == "D":
if "A" in x.OPERATION_history[:-1]:
if x["operation_history"][-1] == "D":
if "A" in x["operation_history"][:-1]:
return "N"
else:
return "D"
elif x.OPERATION_history[-1] == "A":
if "D" in x.OPERATION_history[:-1]:
elif x["operation_history"][-1] == "A":
if "D" in x["operation_history"][:-1]:
return "C"
else:
return "A"
else:
if "A" in x.OPERATION_history[:-1]:
if "A" in x["operation_history"][:-1]:
return "A"
else:
return "C"
Expand All @@ -540,7 +542,7 @@ def _process_deletions(link_changes_df):
""""""
WranglerLogger.debug("Processing link deletions")

cube_delete_df = link_changes_df[link_changes_df.OPERATION_final == "D"]
cube_delete_df = link_changes_df[link_changes_df["operation_final"] == "D"]
if len(cube_delete_df) > 0:
links_to_delete = cube_delete_df["model_link_id"].tolist()
delete_link_dict = {
Expand All @@ -559,7 +561,7 @@ def _process_link_additions(
):
""""""
WranglerLogger.debug("Processing link additions")
cube_add_df = link_changes_df[link_changes_df.OPERATION_final == "A"]
cube_add_df = link_changes_df[link_changes_df["operation_final"] == "A"]
if len(cube_add_df) == 0:
WranglerLogger.debug("No link additions processed")
return {}
Expand All @@ -572,9 +574,9 @@ def _process_link_additions(
]
else:
add_col = [
c for c in cube_add_df.columns if c not in ["OPERATION_final"]
c for c in cube_add_df.columns if c not in ["operation_final"]
]
# can leave out "OPERATION_final" from writing out, is there a reason to write it out?
# can leave out "operation_final" from writing out, is there a reason to write it out?

add_link_properties = cube_add_df[add_col].to_dict("records")

Expand All @@ -591,7 +593,7 @@ def _process_node_additions(node_add_df):
WranglerLogger.debug("No node additions processed")
return []

add_nodes_dict_list = node_add_df.drop(["OPERATION_final"], axis=1).to_dict(
add_nodes_dict_list = node_add_df.drop(["operation_final"], axis=1).to_dict(
"records"
)
WranglerLogger.debug("{} Nodes Added".format(len(add_nodes_dict_list)))
Expand Down Expand Up @@ -676,7 +678,7 @@ def _process_single_link_change(change_row, changeable_col):
p_time_period,
p_category,
managed_lane,
) = column_name_to_parts(c)
) = column_name_to_parts(c, self.parameters)

_d = {
"existing": base_row[c],
Expand Down Expand Up @@ -734,7 +736,7 @@ def _process_single_link_change(change_row, changeable_col):

def _process_link_changes(link_changes_df, changeable_col):
""""""
cube_change_df = link_changes_df[link_changes_df.OPERATION_final == "C"]
cube_change_df = link_changes_df[link_changes_df["operation_final"] == "C"]
if not cube_change_df.shape[0]:
WranglerLogger.info("No link changes processed")
return []
Expand Down Expand Up @@ -798,16 +800,16 @@ def _consolidate_actions(log, base, key_list):
log_df[x] = log_df[x].astype(base[x].dtype)

action_history_df = (
log_df.groupby(key_list)["OPERATION"]
log_df.groupby(key_list)["operation"]
.agg(lambda x: x.tolist())
.rename("OPERATION_history")
.rename("operation_history")
.reset_index()
)

log_df = pd.merge(log_df, action_history_df, on=key_list, how="left")
log_df.drop_duplicates(subset=key_list, keep="last", inplace=True)
log_df["OPERATION_final"] = log_df.apply(lambda x: _final_op(x), axis=1)
return log_df[changeable_col + ["OPERATION_final"]]
log_df["operation_final"] = log_df.apply(lambda x: _final_op(x), axis=1)
return log_df[changeable_col + ["operation_final"]]

delete_link_dict = None
add_link_dict = None
Expand Down Expand Up @@ -858,13 +860,13 @@ def _consolidate_actions(log, base, key_list):

# print error message for node change and node deletion
if (
len(node_changes_df[node_changes_df.OPERATION_final.isin(["C", "D"])])
len(node_changes_df[node_changes_df["operation_final"].isin(["C", "D"])])
> 0
):
msg = "NODE changes and deletions are not allowed!"
WranglerLogger.error(msg)
raise ValueError(msg)
node_add_df = node_changes_df[node_changes_df.OPERATION_final == "A"]
node_add_df = node_changes_df[node_changes_df["operation_final"] == "A"]

if add_link_dict:
add_link_dict["nodes"] = _process_node_additions(node_add_df)
Expand Down

0 comments on commit 298a3a2

Please sign in to comment.