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

Testing data type op #4

Draft
wants to merge 10 commits into
base: generic-whale-cs
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions activitysim/abm/models/atwork_subtour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
simulate,
tracing,
workflow,
asim_enum,
)

logger = logging.getLogger(__name__)
Expand All @@ -41,7 +42,7 @@ def atwork_subtour_frequency(
trace_label = "atwork_subtour_frequency"
model_settings_file_name = "atwork_subtour_frequency.yaml"
trace_hh_id = state.settings.trace_hh_id
work_tours = tours[tours.tour_type == "work"]
work_tours = tours[tours.tour_type == asim_enum.TourPurpose.work]

# - if no work_tours
if len(work_tours) == 0:
Expand Down Expand Up @@ -115,7 +116,7 @@ def atwork_subtour_frequency(
state.add_table("tours", tours)

# - create atwork_subtours based on atwork_subtour_frequency choice names
work_tours = tours[tours.tour_type == "work"]
work_tours = tours[tours.tour_type == asim_enum.TourPurpose.work]
assert not work_tours.atwork_subtour_frequency.isnull().any()

subtours = process_atwork_subtours(state, work_tours, alternatives)
Expand Down
9 changes: 6 additions & 3 deletions activitysim/abm/models/joint_tour_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
simulate,
tracing,
workflow,
asim_enum,
)

logger = logging.getLogger(__name__)


def add_null_results(state, trace_label, tours):
logger.info("Skipping %s: add_null_results" % trace_label)
tours["composition"] = ""
tours["composition"] = asim_enum.TourComposition.na
state.add_table("tours", tours)


Expand All @@ -38,7 +39,7 @@ def joint_tour_composition(
trace_label = "joint_tour_composition"
model_settings_file_name = "joint_tour_composition.yaml"

joint_tours = tours[tours.tour_category == "joint"]
joint_tours = tours[tours.tour_category == asim_enum.TourCategory.joint]

# - if no joint tours
if joint_tours.shape[0] == 0:
Expand All @@ -63,6 +64,7 @@ def joint_tour_composition(
locals_dict = {
"persons": persons,
"hh_time_window_overlap": lambda *x: hh_time_window_overlap(state, *x),
"asim_enum": asim_enum,
}

expressions.assign_columns(
Expand Down Expand Up @@ -106,6 +108,7 @@ def joint_tour_composition(

# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
choices = choices.map(asim_enum.TourComposition.__dict__)

if estimator:
estimator.write_choices(choices)
Expand All @@ -117,7 +120,7 @@ def joint_tour_composition(
joint_tours["composition"] = choices

# reindex since we ran model on a subset of households
tours["composition"] = choices.reindex(tours.index).fillna("").astype(str)
tours["composition"] = choices.reindex(tours.index).fillna(asim_enum.TourComposition.na)
state.add_table("tours", tours)

tracing.print_summary(
Expand Down
12 changes: 10 additions & 2 deletions activitysim/abm/models/joint_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
simulate,
tracing,
workflow,
asim_enum,
)

logger = logging.getLogger(__name__)
Expand All @@ -40,6 +41,9 @@ def joint_tour_frequency(
alternatives = simulate.read_model_alts(
state, "joint_tour_frequency_alternatives.csv", set_index="alt"
)
# add "j" in front of joint tour alternatives
alternatives.index = "j" + alternatives.index.to_series()
alternatives.index = alternatives.index.to_series().map(asim_enum.JointTourFrequency.__dict__)

# - only interested in households with more than one cdap travel_active person and
# - at least one non-preschooler
Expand All @@ -60,6 +64,7 @@ def joint_tour_frequency(
locals_dict = {
"persons": persons,
"hh_time_window_overlap": lambda *x: hh_time_window_overlap(state, *x),
"asim_enum": asim_enum,
}

expressions.assign_columns(
Expand Down Expand Up @@ -98,6 +103,9 @@ def joint_tour_frequency(

# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
# add "j" in front of joint tour alternatives
choices = "j" + choices
choices = choices.map(asim_enum.JointTourFrequency.__dict__)

if estimator:
estimator.write_choices(choices)
Expand Down Expand Up @@ -130,7 +138,7 @@ def joint_tour_frequency(
# we expect there to be an alt with no tours - which we can use to backfill non-travelers
no_tours_alt = (alternatives.sum(axis=1) == 0).index[0]
households["joint_tour_frequency"] = (
choices.reindex(households.index).fillna(no_tours_alt).astype(str)
choices.reindex(households.index).fillna(asim_enum.JointTourFrequency.na)
)

households["num_hh_joint_tours"] = (
Expand All @@ -156,7 +164,7 @@ def joint_tour_frequency(

if estimator:
survey_tours = estimation.manager.get_survey_table("tours")
survey_tours = survey_tours[survey_tours.tour_category == "joint"]
survey_tours = survey_tours[survey_tours.tour_category == asim_enum.TourCategory.joint]

print(f"len(survey_tours) {len(survey_tours)}")
print(f"len(joint_tours) {len(joint_tours)}")
Expand Down
13 changes: 7 additions & 6 deletions activitysim/abm/models/joint_tour_participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
simulate,
tracing,
workflow,
asim_enum,
)
from activitysim.core.util import assign_in_place, reindex

Expand Down Expand Up @@ -49,8 +50,8 @@ def joint_tour_participation_candidates(joint_tours, persons_merged):

# - filter out ineligible candidates (adults for children-only tours, and vice-versa)
eligible = ~(
((candidates.composition == "adults") & ~candidates.adult)
| ((candidates.composition == "children") & candidates.adult)
((candidates.composition == asim_enum.TourComposition.adults) & ~candidates.adult)
| ((candidates.composition == asim_enum.TourComposition.children) & candidates.adult)
)
candidates = candidates[eligible]

Expand Down Expand Up @@ -79,8 +80,8 @@ def get_tour_satisfaction(candidates, participate):
candidates = candidates[participate]

# if this happens, we would need to filter them out!
assert not ((candidates.composition == "adults") & ~candidates.adult).any()
assert not ((candidates.composition == "children") & candidates.adult).any()
assert not ((candidates.composition == asim_enum.TourComposition.adults) & ~candidates.adult).any()
assert not ((candidates.composition == asim_enum.TourComposition.children) & candidates.adult).any()

# FIXME tour satisfaction - hack
# annotate_households_cdap.csv says there has to be at least one non-preschooler in household
Expand All @@ -104,8 +105,8 @@ def get_tour_satisfaction(candidates, participate):
# (x.composition == 'children') & (x.participants > 1) & (x.preschoolers < x.participants) | \
# (x.composition == 'mixed') & (x.adults > 0) & (x.participants > x.adults)

satisfaction = (x.composition != "mixed") & (x.participants > 1) | (
x.composition == "mixed"
satisfaction = (x.composition != asim_enum.TourComposition.mixed) & (x.participants > 1) | (
x.composition == asim_enum.TourComposition.mixed
) & (x.adults > 0) & (x.participants > x.adults)

satisfaction = satisfaction.reindex(tour_ids).fillna(False).astype(bool)
Expand Down
10 changes: 5 additions & 5 deletions activitysim/abm/models/mandatory_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from activitysim.abm.models.util.tour_scheduling import run_tour_scheduling
from activitysim.core import timetable as tt
from activitysim.core import tracing, workflow
from activitysim.core import tracing, workflow, asim_enum
from activitysim.core.util import assign_in_place, reindex

logger = logging.getLogger(__name__)
Expand All @@ -30,7 +30,7 @@ def mandatory_tour_scheduling(
model_name = "mandatory_tour_scheduling"
trace_label = model_name

mandatory_tours = tours[tours.tour_category == "mandatory"]
mandatory_tours = tours[tours.tour_category == asim_enum.TourCategory.mandatory]

# - if no mandatory_tours
if mandatory_tours.shape[0] == 0:
Expand All @@ -46,11 +46,11 @@ def mandatory_tour_scheduling(
# we conflate them by segmenting tour processing to align with primary_purpose
tour_segment_col = "mandatory_tour_seg"
assert tour_segment_col not in mandatory_tours
is_university_tour = (mandatory_tours.tour_type == "school") & reindex(
is_university_tour = (mandatory_tours.tour_type == asim_enum.TourPurpose.school) & reindex(
persons_merged.is_university, mandatory_tours.person_id
)
mandatory_tours[tour_segment_col] = mandatory_tours.tour_type.where(
~is_university_tour, "univ"
~is_university_tour, asim_enum.TourPurpose.univ.value
)

choices = run_tour_scheduling(
Expand All @@ -66,7 +66,7 @@ def mandatory_tour_scheduling(
state.add_table("tours", tours)

# updated df for tracing
mandatory_tours = tours[tours.tour_category == "mandatory"]
mandatory_tours = tours[tours.tour_category == asim_enum.TourCategory.mandatory]

state.tracing.dump_df(
DUMP,
Expand Down
9 changes: 7 additions & 2 deletions activitysim/abm/models/mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
simulate,
tracing,
workflow,
asim_enum,
)

logger = logging.getLogger(__name__)
Expand All @@ -23,7 +24,7 @@ def add_null_results(state, trace_label, mandatory_tour_frequency_settings):
logger.info("Skipping %s: add_null_results", trace_label)

persons = state.get_dataframe("persons")
persons["mandatory_tour_frequency"] = ""
persons["mandatory_tour_frequency"] = asim_enum.MandatoryTourFrequency.na.value

tours = pd.DataFrame()
tours["tour_category"] = None
Expand Down Expand Up @@ -110,6 +111,7 @@ def mandatory_tour_frequency(

# convert indexes to alternative names
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
choices = choices.map(asim_enum.MandatoryTourFrequency._member_map_)

if estimator:
estimator.write_choices(choices)
Expand All @@ -128,6 +130,8 @@ def mandatory_tour_frequency(
alternatives = simulate.read_model_alts(
state, "mandatory_tour_frequency_alternatives.csv", set_index="alt"
)
# change the alt index to asim_enum
alternatives.index = alternatives.index.to_series().map(asim_enum.MandatoryTourFrequency._member_map_)
choosers["mandatory_tour_frequency"] = choices.reindex(choosers.index)

mandatory_tours = process_mandatory_tours(
Expand All @@ -143,13 +147,14 @@ def mandatory_tour_frequency(

# need to reindex as we only handled persons with cdap_activity == 'M'
persons["mandatory_tour_frequency"] = (
choices.reindex(persons.index).fillna("").astype(str)
choices.reindex(persons.index).fillna(asim_enum.MandatoryTourFrequency.na.value)
)

expressions.assign_columns(
state,
df=persons,
model_settings=model_settings.get("annotate_persons"),
locals_dict={"asim_enum":asim_enum},
trace_label=tracing.extend_trace_label(trace_label, "annotate_persons"),
)

Expand Down
3 changes: 2 additions & 1 deletion activitysim/abm/models/non_mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
simulate,
tracing,
workflow,
asim_enum,
)
from activitysim.core.interaction_simulate import interaction_simulate

Expand Down Expand Up @@ -177,7 +178,7 @@ def non_mandatory_tour_frequency(
# - preprocessor
preprocessor_settings = model_settings.get("preprocessor", None)
if preprocessor_settings:
locals_dict = {"person_max_window": lambda x: person_max_window(state, x)}
locals_dict = {"person_max_window": lambda x: person_max_window(state, x), "asim_enum":asim_enum}

expressions.assign_columns(
state,
Expand Down
4 changes: 2 additions & 2 deletions activitysim/abm/models/util/canonical_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import pandas as pd

from activitysim.core import simulate, workflow
from activitysim.core import simulate, workflow, asim_enum

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -383,7 +383,7 @@ def set_tour_index(
assert tour_num_col in tours.columns

# create string tour_id corresonding to keys in possible_tours (e.g. 'work1', 'j_shopping2')
tours["tour_id"] = tours.tour_type + tours[tour_num_col].map(str)
tours["tour_id"] = tours.tour_type.apply(lambda x: asim_enum.TourPurpose._value2member_map_[x].name) + tours[tour_num_col].map(str)

if parent_tour_num_col:
# we need to distinguish between subtours of different work tours
Expand Down
4 changes: 2 additions & 2 deletions activitysim/abm/models/util/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pandas as pd

from activitysim.core import config, expressions, simulate, workflow
from activitysim.core import config, expressions, simulate, workflow, asim_enum
from activitysim.core.estimation import Estimator

"""
Expand Down Expand Up @@ -110,7 +110,7 @@ def run_tour_mode_choice_simulate(

spec = state.filesystem.read_model_spec(file_name=model_settings["SPEC"])
coefficients = state.filesystem.get_segment_coefficients(
model_settings, tour_purpose
model_settings, asim_enum.TourPurpose._value2member_map_[tour_purpose].name
)

spec = simulate.eval_coefficients(state, spec, coefficients, estimator)
Expand Down
Loading