Skip to content

Commit

Permalink
get categories from alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
i-am-sijia committed Dec 12, 2023
1 parent 51f9a24 commit e1e5d41
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 70 deletions.
10 changes: 8 additions & 2 deletions activitysim/abm/models/atwork_subtour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
def add_null_results(state, trace_label, tours):
logger.info("Skipping %s: add_null_results", trace_label)
cat_type = pd.api.types.CategoricalDtype(
["", "no_subtours", "eat", "business1", "maint", "business2", "eat_business"],
[""],
ordered=False,
)
choices = choices.astype(cat_type)
Expand Down Expand Up @@ -109,7 +109,7 @@ def atwork_subtour_frequency(
# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
cat_type = pd.api.types.CategoricalDtype(
["", "no_subtours", "eat", "business1", "maint", "business2", "eat_business"],
alternatives.index.tolist()+[""],
ordered=False,
)
choices = choices.astype(cat_type)
Expand All @@ -133,6 +133,12 @@ def atwork_subtour_frequency(

subtours = process_atwork_subtours(state, work_tours, alternatives)

# convert purpose to pandas categoricals
purpose_type = pd.api.types.CategoricalDtype(
alternatives.columns.tolist()+["atwork"], ordered=False
)
subtours["tour_type"] = subtours["tour_type"].astype(purpose_type)

tours = state.extend_table("tours", subtours)

state.tracing.register_traceable_table("tours", subtours)
Expand Down
2 changes: 1 addition & 1 deletion activitysim/abm/models/joint_tour_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def joint_tour_composition(
# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
cat_type = pd.api.types.CategoricalDtype(
["", "adults", "children", "mixed"], ordered=False
model_spec.columns.tolist()+[""], ordered=False
)
choices = choices.astype(cat_type)

Expand Down
30 changes: 7 additions & 23 deletions activitysim/abm/models/joint_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,7 @@ def joint_tour_frequency(
# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
cat_type = pd.api.types.CategoricalDtype(
[
"0_tours",
"1_Shop",
"1_Main",
"1_Eat",
"1_Visit",
"1_Disc",
"2_SS",
"2_SM",
"2_SE",
"2_SV",
"2_SD",
"2_MM",
"2_ME",
"2_MV",
"2_MD",
"2_EE",
"2_EV",
"2_ED",
"2_VV",
"2_VD",
"2_DD",
],
model_spec.columns.tolist(),
ordered=False,
)
choices = choices.astype(cat_type)
Expand All @@ -147,6 +125,12 @@ def joint_tour_frequency(

joint_tours = process_joint_tours(state, choices, alternatives, temp_point_persons)

# convert purpose to pandas categoricals
purpose_type = pd.api.types.CategoricalDtype(
alternatives.columns.tolist(), ordered=False
)
joint_tours["tour_type"] = joint_tours["tour_type"].astype(purpose_type)

tours = state.extend_table("tours", joint_tours)

state.tracing.register_traceable_table("tours", joint_tours)
Expand Down
8 changes: 7 additions & 1 deletion activitysim/abm/models/mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def mandatory_tour_frequency(
# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
cat_type = pd.api.types.CategoricalDtype(
["", "work1", "work2", "school1", "school2", "work_and_school"], ordered=False
model_spec.columns.tolist()+[""], ordered=False
)
choices = choices.astype(cat_type)

Expand All @@ -142,6 +142,12 @@ def mandatory_tour_frequency(
state, persons=choosers, mandatory_tour_frequency_alts=alternatives
)

# convert purpose to pandas categoricals
purpose_type = pd.api.types.CategoricalDtype(
alternatives.columns.tolist()+["univ","home","escort"], ordered=False
)
mandatory_tours["tour_type"] = mandatory_tours["tour_type"].astype(purpose_type)

tours = state.extend_table("tours", mandatory_tours)
state.tracing.register_traceable_table("tours", mandatory_tours)
state.get_rn_generator().add_channel("tours", mandatory_tours)
Expand Down
6 changes: 6 additions & 0 deletions activitysim/abm/models/non_mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ def non_mandatory_tour_frequency(
)
assert len(non_mandatory_tours) == extended_tour_counts.sum().sum()

# convert purpose to pandas categoricals
purpose_type = pd.api.types.CategoricalDtype(
alternatives.columns.tolist(), ordered=False
)
non_mandatory_tours["tour_type"] = non_mandatory_tours["tour_type"].astype(purpose_type)

if estimator:
# make sure they created the right tours
survey_tours = estimation.manager.get_survey_table("tours").sort_index()
Expand Down
2 changes: 1 addition & 1 deletion activitysim/abm/models/telecommute_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def telecommute_frequency(

choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
telecommute_frequency_cat = pd.api.types.CategoricalDtype(
["", "No_Telecommute", "1_day_week", "2_3_days_week", "4_days_week"],
model_spec.columns.tolist()+[""],
ordered=False,
)
choices = choices.astype(telecommute_frequency_cat)
Expand Down
4 changes: 2 additions & 2 deletions activitysim/abm/models/tour_mode_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def create_logsum_trips(
pandas.DataFrame
Table of trips: 2 per tour, with O/D and purpose inherited from tour
"""
stop_frequency_alts = state.get_injectable("stop_frequency_alts")
stop_freq_cat_type = pd.api.types.CategoricalDtype(
["", "work1", "work2", "school1", "school2", "work_and_school"], ordered=False
stop_frequency_alts.index.tolist()+[""], ordered=False
)
stop_frequency_alts = state.get_injectable("stop_frequency_alts")
stop_freq = "0out_0in" # no intermediate stops
tours["stop_frequency"] = stop_freq
tours["stop_frequency"] = tours["stop_frequency"].astype(stop_freq_cat_type)
Expand Down
13 changes: 13 additions & 0 deletions activitysim/abm/models/trip_purpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ def choose_intermediate_trip_purpose(
state.tracing.trace_df(rands, "%s.rands" % trace_label, columns=[None, "rand"])

choices = choices.map(pd.Series(purpose_cols))
# expand the purpose categorical
for p in purpose_cols:
if not p in trips.primary_purpose.cat.categories:
trips.primary_purpose = trips.primary_purpose.cat.add_categories(
[p]
)
choices = choices.astype(trips["primary_purpose"].dtype)
return choices

Expand Down Expand Up @@ -208,6 +214,13 @@ def run_trip_purpose(state: workflow.State, trips_df, estimator, trace_label):

result_list = []

# add home to purpose categorical
# check if parking_name is in the purpose category
if not "home" in trips_df.primary_purpose.cat.categories:
trips_df.primary_purpose = trips_df.primary_purpose.cat.add_categories(
["home"]
)

# - last trip of outbound tour gets primary_purpose
last_trip = trips_df.trip_num == trips_df.trip_count
purpose = trips_df.primary_purpose[last_trip & trips_df.outbound]
Expand Down
40 changes: 0 additions & 40 deletions activitysim/abm/models/util/tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,6 @@ def create_tours(tour_counts, tour_category, parent_col="person_id"):
# reformat with the columns given below
tours = tour_counts.stack().reset_index()
tours.columns = [parent_col, "tour_type", "tour_type_count"]
cat_tour_type = pd.api.types.CategoricalDtype(
[
"work",
"school",
"univ",
"escort",
"eatout",
"shopping",
"social",
"othmaint",
"othdiscr",
"eat",
"business",
"maint",
"atwork",
"home",
],
ordered=False,
)
tours["tour_type"] = tours["tour_type"].astype(cat_tour_type)

"""
<parent_col> tour_type tour_type_count
Expand Down Expand Up @@ -724,26 +704,6 @@ def create_joint_tours(
tours_purp.columns = [parent_col, "tour_id_temp", "tour_type"]
tours_purp["tour_id_temp"] = range(1, 1 + len(tours_purp))
tours_purp["tour_type"] = tours_purp["tour_type"].map(tour_type_dict)
cat_tour_type = pd.api.types.CategoricalDtype(
[
"work",
"school",
"univ",
"escort",
"eatout",
"shopping",
"social",
"othmaint",
"othdiscr",
"eat",
"business",
"maint",
"atwork",
"home",
],
ordered=False,
)
tours_purp["tour_type"] = tours_purp["tour_type"].astype(cat_tour_type)

"""
<parent_col> tour_id_temp tour_type
Expand Down
10 changes: 10 additions & 0 deletions activitysim/core/workflow/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,16 @@ def extend_table(self, table_name, df, axis=0):
new_df_columns = [c for c in df.columns if c not in table_df.columns]
df = df[new_df_columns]
missing_df_str_columns = []

# union categoricals
for c in table_df.columns.intersection(df.columns):
if isinstance(table_df[c].dtype,pd.api.types.CategoricalDtype):
if isinstance(df[c].dtype,pd.api.types.CategoricalDtype):
from pandas.api.types import union_categoricals

uc = union_categoricals([table_df[c], df[c]])
table_df[c]=pd.Categorical(table_df[c],categories=uc.categories)
df[c]=pd.Categorical(df[c],categories=uc.categories)

# preserve existing column order
df = pd.concat([table_df, df], sort=False, axis=axis)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ AccesibilityAtDestination if transit,pracc,"pracc.where(~tour_mode_is_transit, _
AccesibilityAtDestination if non_motorized,pracc,"pracc.where(~tour_mode_is_non_motorized, reindex(accessibility.nmRetail, df.destination))"
,destination_area_type,"reindex(land_use.area_type, df.destination)"
,is_mixed,df.composition=='mixed'
,is_adults_tour,df.composition=='adults'
,is_social,primary_purpose=='social'
,is_outbound_school_escort,"(df.school_esc_outbound.isin(['ride_share', 'pure_escort']))"
,is_inbound_school_escort,"(df.school_esc_inbound.isin(['ride_share', 'pure_escort']))"

0 comments on commit e1e5d41

Please sign in to comment.