From 27895e2c249dccfdd12969519802fc067b7cf54a Mon Sep 17 00:00:00 2001 From: Jeff Newman Date: Mon, 4 Mar 2024 11:05:11 -0600 Subject: [PATCH 1/2] veh type categorical --- activitysim/abm/models/vehicle_allocation.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/activitysim/abm/models/vehicle_allocation.py b/activitysim/abm/models/vehicle_allocation.py index e4129e106..9fb672726 100644 --- a/activitysim/abm/models/vehicle_allocation.py +++ b/activitysim/abm/models/vehicle_allocation.py @@ -196,6 +196,19 @@ def vehicle_allocation( choosers = pd.merge(choosers, vehicles_wide, how="left", on="household_id") choosers.set_index("tour_id", inplace=True) + ## get categorical dtype for vehicle_type and use it to create new dtype for + ## vehicle_occup_* and selected_vehicle columns + veh_type_dtype = vehicles["vehicle_type"].dtype + if isinstance(veh_type_dtype, pd.CategoricalDtype): + veh_categories = list(veh_type_dtype.categories) + if "" not in veh_categories: + veh_categories.insert(0, "") + if "non_hh_veh" not in veh_categories: + veh_categories.append("non_hh_veh") + veh_choice_dtype = pd.CategoricalDtype(veh_categories, ordered=False) + else: + veh_choice_dtype = "category" + # ----- setup skim keys skims = get_skim_dict(network_los, choosers) locals_dict.update(skims) @@ -254,7 +267,7 @@ def vehicle_allocation( # creating a column for choice of each occupancy level tours_veh_occup_col = f"vehicle_occup_{occup}" tours[tours_veh_occup_col] = choices["choice"] - tours[tours_veh_occup_col] = tours[tours_veh_occup_col].astype("category") + tours[tours_veh_occup_col] = tours[tours_veh_occup_col].astype(veh_choice_dtype) tours_veh_occup_cols.append(tours_veh_occup_col) if estimator: From fc980396749ce59ba6505d247a0f4e527e03e3eb Mon Sep 17 00:00:00 2001 From: Jeff Newman Date: Mon, 4 Mar 2024 21:59:41 -0600 Subject: [PATCH 2/2] remove empty string as vehicle type, allow normal missing cat value --- activitysim/abm/models/vehicle_allocation.py | 2 -- activitysim/abm/models/vehicle_type_choice.py | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/activitysim/abm/models/vehicle_allocation.py b/activitysim/abm/models/vehicle_allocation.py index 9fb672726..a341493ff 100644 --- a/activitysim/abm/models/vehicle_allocation.py +++ b/activitysim/abm/models/vehicle_allocation.py @@ -201,8 +201,6 @@ def vehicle_allocation( veh_type_dtype = vehicles["vehicle_type"].dtype if isinstance(veh_type_dtype, pd.CategoricalDtype): veh_categories = list(veh_type_dtype.categories) - if "" not in veh_categories: - veh_categories.insert(0, "") if "non_hh_veh" not in veh_categories: veh_categories.append("non_hh_veh") veh_choice_dtype = pd.CategoricalDtype(veh_categories, ordered=False) diff --git a/activitysim/abm/models/vehicle_type_choice.py b/activitysim/abm/models/vehicle_type_choice.py index 8271ac6e8..813652459 100644 --- a/activitysim/abm/models/vehicle_type_choice.py +++ b/activitysim/abm/models/vehicle_type_choice.py @@ -393,7 +393,7 @@ def iterate_vehicle_type_choice( sorted(alts_cats_dict["fuel_type"]), ordered=False ) vehicle_type_cat = pd.api.types.CategoricalDtype( - [""] + sorted(set(alts_wide["vehicle_type"])), ordered=False + sorted(set(alts_wide["vehicle_type"])), ordered=False ) alts_wide["body_type"] = alts_wide["body_type"].astype(body_type_cat) @@ -403,7 +403,7 @@ def iterate_vehicle_type_choice( alts_wide = alts_long = None alts = model_spec.columns vehicle_type_cat = pd.api.types.CategoricalDtype( - [""] + sorted(set(alts)), ordered=False + sorted(set(alts)), ordered=False ) # alts preprocessor