From 4a8fba32741a8a9b866efce5b5f3233c5e1a3d32 Mon Sep 17 00:00:00 2001 From: Sijia Wang Date: Thu, 5 Aug 2021 17:22:28 -0400 Subject: [PATCH] fix bug in add_counts --- lasso/roadway.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lasso/roadway.py b/lasso/roadway.py index f8568ec..38514be 100644 --- a/lasso/roadway.py +++ b/lasso/roadway.py @@ -575,6 +575,10 @@ def add_variable_using_shst_reference( ) var_shst_df = pd.read_csv(var_shst_csvdata) + # there are aadt = 0 in the counts, drop them + var_shst_df = var_shst_df[var_shst_df[shst_csv_variable] > 0].copy() + # count station to shared street match - there are many-to-one matches, keep just one match + var_shst_df.drop_duplicates(subset = ["shstReferenceId"], inplace = True) if "shstReferenceId" not in var_shst_df.columns: msg = "'shstReferenceId' required but not found in {}".format(var_shst_data) @@ -598,7 +602,7 @@ def add_variable_using_shst_reference( join_gdf[shst_csv_variable].fillna(0, inplace=True) if network_variable in self.links_df.columns and not overwrite: - join_gdf.loc[join_gdf[network_variable] > 0, network_variable] = join_gdf[ + join_gdf.loc[join_gdf[network_variable] == 0, network_variable] = join_gdf[ shst_csv_variable ].astype(network_var_type) else: @@ -608,6 +612,28 @@ def add_variable_using_shst_reference( self.links_df[network_variable] = join_gdf[network_variable] + # MN and WI counts are vehicles using the segment in both directions, no directional counts + # we will make sure both direction has the same daily AADT + dir_link_count_df = self.links_df[ + (self.links_df[network_variable] > 0) & + (self.links_df["drive_access"] == 1) + ][["A", "B", network_variable]].copy() + reverse_dir_link_count_df = dir_link_count_df.rename(columns = {"A":"B", "B":"A"}).copy() + + link_count_df = pd.concat( + [dir_link_count_df, reverse_dir_link_count_df], + sort = False, + ignore_index = True + ) + link_count_df.drop_duplicates(subset = ["A", "B"], inplace = True) + + self.links_df = pd.merge( + self.links_df.drop(network_variable, axis = 1), + link_count_df[["A", "B", network_variable]], + how = "left", + on = ["A", "B"] + ) + self.links_df[network_variable].fillna(0, inplace = True) WranglerLogger.info( "Added variable: {} using Shared Streets Reference".format(network_variable) ) @@ -722,6 +748,7 @@ def add_counts( self.links_df["count_NT"] = self.links_df[network_variable] / 4 self.links_df["count_daily"] = self.links_df[network_variable] + # add COUNTYEAR self.links_df["count_year"] = 2017 WranglerLogger.info(