예제 #1
0
def mandatory_scheduling(set_random_seed, mandatory_tours_merged, tdd_alts,
                         tdd_school_spec, tdd_work_spec,
                         mandatory_scheduling_settings, chunk_size,
                         trace_hh_id):
    """
    This model predicts the departure time and duration of each activity for
    mandatory tours
    """

    tours = mandatory_tours_merged.to_frame()
    alts = tdd_alts.to_frame()

    if trace_hh_id:
        # register non_mandatory_tours so we can slice utilities
        tracing.register_tours(tours, trace_hh_id)

    constants = get_model_constants(mandatory_scheduling_settings)

    school_spec = tdd_school_spec.to_frame()
    school_tours = tours[tours.tour_type == "school"]

    logger.info("Running mandatory_scheduling school_tours with %d tours" %
                len(school_tours))

    school_choices = vectorize_tour_scheduling(
        school_tours,
        alts,
        school_spec,
        constants,
        chunk_size,
        trace_label='mandatory_scheduling.school')

    work_spec = tdd_work_spec.to_frame()
    work_tours = tours[tours.tour_type == "work"]

    logger.info("Running %d work tour scheduling choices" % len(work_tours))

    work_choices = vectorize_tour_scheduling(
        work_tours,
        alts,
        work_spec,
        constants,
        chunk_size,
        trace_label='mandatory_scheduling.work')

    choices = pd.concat([school_choices, work_choices])

    tracing.print_summary('mandatory_scheduling tour_departure_and_duration',
                          choices,
                          describe=True)

    orca.add_column("mandatory_tours", "tour_departure_and_duration", choices)

    if trace_hh_id:
        tracing.trace_df(orca.get_table('mandatory_tours').to_frame(),
                         label="mandatory_tours",
                         slicer='person_id',
                         index_label='tour',
                         columns=None,
                         warn_if_empty=True)
def workplace_location_simulate(set_random_seed,
                                persons_merged,
                                workplace_location_spec,
                                skims,
                                destination_size_terms):

    # for now I'm going to generate a workplace location for everyone -
    # presumably it will not get used in downstream models for everyone -
    # it should depend on CDAP and mandatory tour generation as to whethrer
    # it gets used
    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    choices, _ = asim.interaction_simulate(choosers,
                                           alternatives,
                                           workplace_location_spec,
                                           skims=skims,
                                           locals_d=locals_d,
                                           sample_size=50)

    choices = choices.reindex(persons_merged.index)

    print "Describe of choices:\n", choices.describe()
    orca.add_column("persons", "workplace_taz", choices)

    add_dependent_columns("persons", "persons_workplace")
예제 #3
0
파일: mode.py 프로젝트: amos5/activitysim
def mode_choice_simulate(tours_merged,
                         mode_choice_spec,
                         mode_choice_settings,
                         skims):

    tours = tours_merged.to_frame()
    tours = tours[tours.tour_type == "work"]

    mode_choice_spec = mode_choice_spec.head(33)

    # the skims will be available under the name "skims" for any @ expressions
    in_skims = askim.Skims3D(skims.set_keys("TAZ", "workplace_taz"),
                             "in_period", -1)
    out_skims = askim.Skims3D(skims.set_keys("workplace_taz", "TAZ"),
                              "out_period", -1)
    locals_d = {
        "in_skims": in_skims,
        "out_skims": out_skims
    }
    locals_d.update(mode_choice_settings['CONSTANTS'])

    # FIXME lots of other segments here - for now running the mode choice for
    # FIXME work on all kinds of tour types
    # FIXME note that in particular the spec above only has work tours in it

    choices, _ = asim.simple_simulate(tours,
                                      get_segment_and_unstack(mode_choice_spec,
                                                              'work'),
                                      skims=[in_skims, out_skims],
                                      locals_d=locals_d)

    print "Choices:\n", choices.value_counts()
    orca.add_column("tours", "mode", choices)
예제 #4
0
def slr_inundate(scenario, parcels, slr_progression_C, slr_progression_R,
                 slr_progression_B, slr_parcel_inundation,
                 slr_parcel_inundation_mf, slr_parcel_inundation_mp,
                 slr_progression_d_b, slr_parcel_inundation_d_b,
                 slr_parcel_inundation_d_bb, slr_parcel_inundation_d_bp, year,
                 hazards):

    if scenario not in hazards["slr_scenarios"]["enable_in"]:
        return

    # horizon
    if scenario in hazards["slr_scenarios"]["rtff_prog"]:
        slr_progression = slr_progression_R.to_frame()
    elif scenario in hazards["slr_scenarios"]["cag_prog"]:
        slr_progression = slr_progression_C.to_frame()
    elif scenario in hazards["slr_scenarios"]["bttf_prog"]:
        slr_progression = slr_progression_B.to_frame()
    # draft blueprint
    elif scenario in hazards["slr_scenarios"]["d_b_prog"]:
        slr_progression = slr_progression_d_b.to_frame()
    orca.add_table("slr_progression", slr_progression)

    inundation_yr = slr_progression.query('year==@year')['inundated'].item()
    print("Inundation in model year is %d inches" % inundation_yr)

    # horizon
    if scenario in hazards["slr_scenarios"]["horizon_scenarios"]:
        if scenario in hazards["slr_scenarios"]["mitigation_full"]:
            slr_parcel_inundation = slr_parcel_inundation_mf.to_frame()
            orca.add_injectable("slr_mitigation", 'full mitigation')
        elif scenario in hazards["slr_scenarios"]["mitigation_partial"]:
            slr_parcel_inundation = slr_parcel_inundation_mp.to_frame()
            orca.add_injectable("slr_mitigation", 'partial mitigation')
        else:
            slr_parcel_inundation = slr_parcel_inundation.to_frame()
            orca.add_injectable("slr_mitigation", 'none')
    # draft blueprint
    if scenario in hazards["slr_scenarios"]["draft_blueprint_scenarios"]:
        if scenario in hazards["slr_scenarios"]["d_bb_mitigation"]:
            slr_parcel_inundation = slr_parcel_inundation_d_bb.to_frame()
            orca.add_injectable("slr_mitigation",
                                'draft blueprint basic mitigation')
        elif scenario in hazards["slr_scenarios"]["d_bp_mitigation"]:
            slr_parcel_inundation = slr_parcel_inundation_d_bp.to_frame()
            orca.add_injectable("slr_mitigation",
                                'draft blueprint plus mitigation')
        else:
            slr_parcel_inundation = slr_parcel_inundation_d_b.to_frame()
            orca.add_injectable("slr_mitigation", 'none')

    destroy_parcels = slr_parcel_inundation.\
        query('inundation<=@inundation_yr').astype('bool')
    orca.add_table('destroy_parcels', destroy_parcels)
    print("Number of parcels destroyed: %d" % len(destroy_parcels))

    slr_nodev = pd.Series(False, parcels.index)
    destroy = pd.Series(destroy_parcels['inundation'])
    slr_nodev.update(destroy)
    orca.add_column('parcels', 'slr_nodev', slr_nodev)
    parcels = orca.get_table("parcels")
예제 #5
0
def pet_activity_simulate(set_random_seed, pets_merged, pet_spec):

    # choosers: the choice model will be applied to each row of the choosers table (a pandas.DataFrame)
    choosers = pets_merged.to_frame()

    # spec: table of variable specifications and coefficient values of alternatives (a pandas.DataFrame table
    spec = pet_spec

    # locals whose values will be accessible to the execution context  when the expressions in spec are applied to choosers
    locals_d=None

    choices, model_design = asim.simple_simulate(choosers, spec)

    print "\n### model_design - results of the expressions for each row in choosers"
    print model_design

    print "\n### choices - choices expressed as zero-based index into set of alternatives"
    print choices

    # convert choice indexes to spec column names
    activity_names = spec.columns.values
    choice_names = choices.apply(lambda x : activity_names[x])

    print "\n### choice_names - choices expressed as names of columns of alternatives in spec"
    print choice_names

    # store the results so they are available to subsequent models, etc
    orca.add_column("pets", "pet_activity", choices)
    orca.add_column("pets", "pet_activity_names", choice_names)
예제 #6
0
def reinject_decorated_tables():
    """
    reinject the decorated tables (and columns)
    """

    logger.info("reinject_decorated_tables")

    # need to clear any non-decorated tables that were added during the previous run
    _ORCA._TABLES.clear()
    _ORCA._COLUMNS.clear()
    _ORCA._TABLE_CACHE.clear()
    _ORCA._COLUMN_CACHE.clear()

    for name, func in _DECORATED_TABLES.items():
        logger.debug("reinject decorated table %s" % name)
        orca.add_table(name, func)

    for column_key, args in _DECORATED_COLUMNS.items():
        table_name, column_name = column_key
        logger.debug("reinject decorated column %s.%s" % (table_name, column_name))
        orca.add_column(table_name, column_name, args['func'], cache=args['cache'])

    for name, args in _DECORATED_INJECTABLES.items():
        logger.debug("reinject decorated injectable %s" % name)
        orca.add_injectable(name, args['func'], cache=args['cache'])
예제 #7
0
def mandatory_scheduling(set_random_seed,
                         mandatory_tours_merged,
                         tdd_alts,
                         tdd_school_spec,
                         tdd_work_spec):

    tours = mandatory_tours_merged.to_frame()
    alts = tdd_alts.to_frame()

    school_spec = tdd_school_spec.to_frame()
    school_tours = tours[tours.tour_type == "school"]

    print "Running %d school tour scheduling choices" % len(school_tours)

    school_choices = vectorize_tour_scheduling(school_tours, alts, school_spec)

    work_spec = tdd_work_spec.to_frame()
    work_tours = tours[tours.tour_type == "work"]

    print "Running %d work tour scheduling choices" % len(work_tours)

    work_choices = vectorize_tour_scheduling(work_tours, alts, work_spec)

    choices = pd.concat([school_choices, work_choices])

    print "Choices:\n", choices.describe()

    orca.add_column(
        "mandatory_tours", "tour_departure_and_duration", choices)
예제 #8
0
def slr_inundate(scenario, parcels, slr_progression_C, slr_progression_R,
                 slr_progression_B, year, slr_parcel_inundation, settings):

    if scenario not in settings["slr_scenarios"]["enable_in"]:
        return

    if scenario in settings["slr_scenarios"]["rtff"]:
        slr_progression = slr_progression_R.to_frame()
    elif scenario in settings["slr_scenarios"]["cag"]:
        slr_progression = slr_progression_C.to_frame()
    elif scenario in settings["slr_scenarios"]["bttf"]:
        slr_progression = slr_progression_B.to_frame()

    orca.add_table("slr_progression", slr_progression)
    inundation_yr = slr_progression.query('year==@year')['inundated'].item()
    print "Inundation in model year is %d inches" % inundation_yr
    slr_parcel_inundation = slr_parcel_inundation.to_frame()
    destroy_parcels = slr_parcel_inundation.\
        query('inundation<=@inundation_yr').astype('bool')
    orca.add_table('destroy_parcels', destroy_parcels)
    print "Number of parcels destroyed: %d" % len(destroy_parcels)

    slr_nodev = pd.Series(False, parcels.index)
    destroy = pd.Series(destroy_parcels['inundation'])
    slr_nodev.update(destroy)
    orca.add_column('parcels', 'slr_nodev', slr_nodev)
    parcels = orca.get_table("parcels")
def mandatory_tour_frequency(set_random_seed,
                             persons_merged,
                             mandatory_tour_frequency_spec):
    """
    This model predicts the frequency of making mandatory trips (see the
    alternatives above) - these trips include work and school in some combination.
    """

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'Mandatory']
    logger.info("%d persons run for mandatory tour model" % len(choosers))

    # trace.print_summary('mandatory_tour_frequency choosers',
    #                     choosers.workplace_taz, describe=True)

    choices, _ = asim.simple_simulate(choosers, mandatory_tour_frequency_spec)

    # convert indexes to alternative names
    choices = pd.Series(
        mandatory_tour_frequency_spec.columns[choices.values],
        index=choices.index).reindex(persons_merged.local.index)

    tracing.print_summary('mandatory_tour_frequency', choices, value_counts=True)

    orca.add_column("persons", "mandatory_tour_frequency", choices)
예제 #10
0
def mandatory_scheduling(set_random_seed,
                         mandatory_tours_merged,
                         tdd_alts,
                         tdd_school_spec,
                         tdd_work_spec,
                         chunk_size):
    """
    This model predicts the departure time and duration of each activity for
    mandatory tours
    """

    tours = mandatory_tours_merged.to_frame()
    alts = tdd_alts.to_frame()

    school_spec = tdd_school_spec.to_frame()
    school_tours = tours[tours.tour_type == "school"]

    logger.info("Running %d school tour scheduling choices" % len(school_tours))

    school_choices = vectorize_tour_scheduling(school_tours, alts, school_spec, chunk_size)

    work_spec = tdd_work_spec.to_frame()
    work_tours = tours[tours.tour_type == "work"]

    logger.info("Running %d work tour scheduling choices" % len(work_tours))

    work_choices = vectorize_tour_scheduling(work_tours, alts, work_spec, chunk_size)

    choices = pd.concat([school_choices, work_choices])

    tracing.print_summary('mandatory_scheduling tour_departure_and_duration',
                          choices, describe=True)

    orca.add_column(
        "mandatory_tours", "tour_departure_and_duration", choices)
def non_mandatory_tour_frequency(set_random_seed,
                                 persons_merged,
                                 non_mandatory_tour_frequency_alts,
                                 non_mandatory_tour_frequency_spec):

    choosers = persons_merged.to_frame()

    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity.isin(['Mandatory',
                                                     'NonMandatory'])]
    print "%d persons run for non-mandatory tour model" % len(choosers)

    choices_list = []
    # segment by person type and pick the right spec for each person type
    for name, segment in choosers.groupby('ptype_cat'):

        print "Running segment '%s' of size %d" % (name, len(segment))

        choices, _ = asim.interaction_simulate(
            segment,
            non_mandatory_tour_frequency_alts.to_frame(),
            # notice that we pick the column for the
            # segment for each segment we run
            non_mandatory_tour_frequency_spec[[name]])

        choices_list.append(choices)

    choices = pd.concat(choices_list)

    print "Choices:\n", choices.value_counts()

    orca.add_column("persons", "non_mandatory_tour_frequency", choices)
예제 #12
0
파일: cdap.py 프로젝트: CSHong9/activitysim
def cdap_simulate(set_random_seed, persons_merged,
                  cdap_1_person_spec, cdap_2_person_spec, cdap_3_person_spec,
                  cdap_final_rules, cdap_all_people, hh_chunk_size):
    """
    CDAP stands for Coordinated Daily Activity Pattern, which is a choice of
    high-level activity pattern for each person, in a coordinated way with other
    members of a person's household.

    Because Python requires vectorization of computation, there are some specialized
    routines in the cdap directory of activitysim for this purpose.  This module
    simply applies those utilities using the simulation framework.
    """

    choices = cdap.run_cdap(persons_merged.to_frame(),
                            "household_id",
                            "ptype",
                            cdap_1_person_spec,
                            cdap_2_person_spec,
                            cdap_3_person_spec,
                            cdap_final_rules,
                            cdap_all_people,
                            hh_chunk_size)

    choices = choices.reindex(persons_merged.index)

    tracing.print_summary('cdap_activity', choices, value_counts=True)

    orca.add_column("persons", "cdap_activity", choices)
예제 #13
0
def workplace_location_simulate(set_random_seed, persons_merged,
                                workplace_location_spec, skims,
                                destination_size_terms):

    # for now I'm going to generate a workplace location for everyone -
    # presumably it will not get used in downstream models for everyone -
    # it should depend on CDAP and mandatory tour generation as to whethrer
    # it gets used
    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    choices, _ = asim.interaction_simulate(choosers,
                                           alternatives,
                                           workplace_location_spec,
                                           skims=skims,
                                           locals_d=locals_d,
                                           sample_size=50)

    choices = choices.reindex(persons_merged.index)

    print "Describe of choices:\n", choices.describe()
    orca.add_column("persons", "workplace_taz", choices)

    add_dependent_columns("persons", "persons_workplace")
예제 #14
0
def setup_orca(dfa, dfb, dfa_col, dfb_col, dfa_factor, dfb_factor):
    orca.add_injectable('a_factor', dfa_factor)

    @orca.injectable()
    def b_factor():
        return dfb_factor

    orca.add_table('dfa', dfa)

    @orca.table('dfb')
    def dfb_table():
        return dfb

    orca.add_column('dfa', 'acol', dfa_col)
    orca.add_column('dfb', 'bcol', dfb_col)

    @orca.column('dfa')
    def extra_acol(a_factor):
        return dfa_col * a_factor

    @orca.column('dfb')
    def extra_bcol(b_factor):
        return dfb_col * b_factor

    orca.broadcast('dfb', 'dfa', cast_on='a_id', onto_index=True)

    @orca.step()
    def test_step(dfa, dfb):
        pass
예제 #15
0
def mandatory_scheduling(set_random_seed, mandatory_tours_merged, tdd_alts,
                         tdd_school_spec, tdd_work_spec):

    tours = mandatory_tours_merged.to_frame()
    alts = tdd_alts.to_frame()

    school_spec = tdd_school_spec.to_frame()
    school_tours = tours[tours.tour_type == "school"]

    print "Running %d school tour scheduling choices" % len(school_tours)

    school_choices = vectorize_tour_scheduling(school_tours, alts, school_spec)

    work_spec = tdd_work_spec.to_frame()
    work_tours = tours[tours.tour_type == "work"]

    print "Running %d work tour scheduling choices" % len(work_tours)

    work_choices = vectorize_tour_scheduling(work_tours, alts, work_spec)

    choices = pd.concat([school_choices, work_choices])

    print "Choices:\n", choices.describe()

    orca.add_column("mandatory_tours", "tour_departure_and_duration", choices)
예제 #16
0
def auto_ownership_simulate(households_merged, auto_ownership_spec,
                            auto_ownership_settings, trace_hh_id):
    """
    Auto ownership is a standard model which predicts how many cars a household
    with given characteristics owns
    """

    logger.info("Running auto_ownership_simulate with %d households" %
                len(households_merged))

    nest_spec = config.get_logit_model_settings(auto_ownership_settings)
    constants = config.get_model_constants(auto_ownership_settings)

    choices = asim.simple_simulate(choosers=households_merged.to_frame(),
                                   spec=auto_ownership_spec,
                                   nest_spec=nest_spec,
                                   locals_d=constants,
                                   trace_label=trace_hh_id
                                   and 'auto_ownership',
                                   trace_choice_name='auto_ownership')

    tracing.print_summary('auto_ownership', choices, value_counts=True)

    orca.add_column('households', 'auto_ownership', choices)

    pipeline.add_dependent_columns('households', 'households_autoown')

    if trace_hh_id:
        trace_columns = ['auto_ownership'
                         ] + orca.get_table('households_autoown').columns
        tracing.trace_df(orca.get_table('households').to_frame(),
                         label='auto_ownership',
                         columns=trace_columns,
                         warn_if_empty=True)
def non_mandatory_tour_frequency(set_random_seed, persons_merged,
                                 non_mandatory_tour_frequency_alts,
                                 non_mandatory_tour_frequency_spec):

    choosers = persons_merged.to_frame()

    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity.isin(
        ['Mandatory', 'NonMandatory'])]
    print "%d persons run for non-mandatory tour model" % len(choosers)

    choices_list = []
    # segment by person type and pick the right spec for each person type
    for name, segment in choosers.groupby('ptype_cat'):

        print "Running segment '%s' of size %d" % (name, len(segment))

        choices, _ = asim.interaction_simulate(
            segment,
            non_mandatory_tour_frequency_alts.to_frame(),
            # notice that we pick the column for the
            # segment for each segment we run
            non_mandatory_tour_frequency_spec[[name]],
            sample_size=50)

        choices_list.append(choices)

    choices = pd.concat(choices_list)

    print "Choices:\n", choices.value_counts()

    orca.add_column("persons", "non_mandatory_tour_frequency", choices)

    add_dependent_columns("persons", "persons_nmtf")
예제 #18
0
파일: test_server.py 프로젝트: UDST/orca
def setup_orca(dfa, dfb, dfa_col, dfb_col, dfa_factor, dfb_factor):
    orca.add_injectable('a_factor', dfa_factor)

    @orca.injectable()
    def b_factor():
        return dfb_factor

    orca.add_table('dfa', dfa)

    @orca.table('dfb')
    def dfb_table():
        return dfb

    orca.add_column('dfa', 'acol', dfa_col)
    orca.add_column('dfb', 'bcol', dfb_col)

    @orca.column('dfa')
    def extra_acol(a_factor):
        return dfa_col * a_factor

    @orca.column('dfb')
    def extra_bcol(b_factor):
        return dfb_col * b_factor

    orca.broadcast('dfb', 'dfa', cast_on='a_id', onto_index=True)

    @orca.step()
    def test_step(dfa, dfb):
        pass
예제 #19
0
파일: mode.py 프로젝트: CSHong9/activitysim
def tour_mode_choice_simulate(tours_merged,
                              tour_mode_choice_spec,
                              tour_mode_choice_settings,
                              skims, stacked_skims,
                              omx_file):

    tours = tours_merged.to_frame()

    choices_list = []

    print "Tour types:\n", tours.tour_type.value_counts()

    for tour_type, segment in tours.groupby('tour_type'):

        # FIXME - log
        logger.info("running tour_type '%s'" % tour_type)

        # FIXME - hack to run subset of tour types
        # TOUR_TYPE_SUBSET = []
        # # TOUR_TYPE_SUBSET = ['eatout']
        # if TOUR_TYPE_SUBSET and tour_type not in TOUR_TYPE_SUBSET:
        #     print "skipping tour_type %s" % tour_type
        #     continue

        tour_type_tours = tours[tours.tour_type == tour_type]
        orig_key = 'TAZ'
        dest_key = 'destination'

        # FIXME - check that destination is not null (patch_mandatory_tour_destination not run?)

        # FIXME - log
        # print "dest_taz counts:\n", tour_type_tours[dest_key].value_counts()

        choices = _mode_choice_simulate(
            tour_type_tours,
            skims, stacked_skims,
            orig_key=orig_key,
            dest_key=dest_key,
            spec=get_segment_and_unstack(tour_mode_choice_spec, tour_type),
            additional_constants=tour_mode_choice_settings['CONSTANTS'],
            omx=omx_file)

        tracing.print_summary('tour_mode_choice_simulate %s' % tour_type,
                              choices, value_counts=True)

        choices_list.append(choices)

        # FIXME - force garbage collection
        mem = asim.memory_info()
        logger.info('memory_info tour_type %s, %s' % (tour_type, mem))

    choices = pd.concat(choices_list)

    tracing.print_summary('tour_mode_choice_simulate all tour type', choices, value_counts=True)

    orca.add_column("tours", "mode", choices)

    # FIXME - this forces garbage collection
    asim.memory_info()
예제 #20
0
def test_all_cols_extras(df):
    """
    Confirm that all_cols() includes columns not part of the Orca core table.
    
    """
    orca.add_table('df', df)
    orca.add_column('df', 'newcol', pd.Series())
    cols = utils.all_cols('df')
    assert sorted(cols) == sorted(['id', 'val1', 'val2', 'newcol'])
예제 #21
0
파일: mode.py 프로젝트: CSHong9/activitysim
def trip_mode_choice_simulate(tours_merged,
                              trip_mode_choice_spec,
                              trip_mode_choice_settings,
                              skims,
                              stacked_skims,
                              omx_file):

    # FIXME running the trips model on tours
    trips = tours_merged.to_frame()
    stack = askim.SkimStack(skims)

    choices_list = []

    # FIXME - log
    print "Trip types:\n", trips.tour_type.value_counts()

    for tour_type, segment in trips.groupby('tour_type'):

        logger.info("running tour_type '%s'" % tour_type)

        tour_type_tours = trips[trips.tour_type == tour_type]
        orig_key = 'TAZ'
        dest_key = 'destination'

        # FIXME - check that destination is not null (patch_mandatory_tour_destination not run?)

        # FIXME - log
        # print "dest_taz counts:\n", tour_type_tours[dest_key].value_counts()

        choices = _mode_choice_simulate(
            tour_type_tours,
            skims, stacked_skims,
            orig_key=orig_key,
            dest_key=dest_key,
            spec=get_segment_and_unstack(trip_mode_choice_spec, tour_type),
            additional_constants=trip_mode_choice_settings['CONSTANTS'],
            omx=omx_file)

        tracing.print_summary('trip_mode_choice_simulate %s' % tour_type,
                              choices, value_counts=True)

        choices_list.append(choices)

        # FIXME - force garbage collection
        mem = asim.memory_info()
        logger.info('memory_info tour_type %s, %s' % (tour_type, mem))

    choices = pd.concat(choices_list)

    tracing.print_summary('trip_mode_choice_simulate all tour type', choices, value_counts=True)

    orca.add_column("trips", "mode", choices)

    # FIXME - this forces garbage collection
    asim.memory_info()
예제 #22
0
def auto_ownership_simulate(households):
    """
    Generate auto ownership choices for the synthetic pop households. The categories are:
    - 0: no vehicle
    - 1: one vehicle
    - 2: two vehicles
    - 3: three or more vehicles
    """

    # income bin dummies
    income_bins = pd.cut(
        orca.get_table('households').to_frame().income,
        bins=[0, 20000, 40000, 60000, 80000, 100000, 120000, np.inf],
        labels=['2', '4', '6', '8', '10', '12', '12p'],
        include_lowest=True)

    income_bin_dummies = pd.get_dummies(income_bins, prefix='income')

    for i in income_bin_dummies.columns:
        orca.add_column('households', i, income_bin_dummies[i])

    # load UrbanAccess transit accessibility variables
    parcels = orca.get_table('parcels').to_frame()
    am_acc = pd.read_csv('./data/access_indicators_ampeak.csv',
                         dtype={'block_id': str})
    am_acc.block_id = am_acc.block_id.str.zfill(15)
    parcels_with_acc = parcels.merge(
        am_acc, how='left', on='block_id').reindex(
            index=parcels.index)  # reorder to align with parcels table

    for acc_col in set(parcels_with_acc.columns) - set(parcels):
        # fill NA with median value
        orca.add_column(
            'parcels', acc_col, parcels_with_acc[acc_col].fillna(
                parcels_with_acc[acc_col].median()))

    @orca.table(cache=False)
    def hh_merged():
        df = orca.merge_tables(target='households',
                               tables=[
                                   'households', 'units', 'buildings',
                                   'parcels', 'nodessmall', 'nodeswalk'
                               ])
        return df

    m = mm.get_step('auto_ownership')

    # remove filters, specify out table, out column

    m.filters = None
    m.out_table = 'households'
    m.out_column = 'cars_alt'

    m.run()
예제 #23
0
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec):

    choices, _ = asim.simple_simulate(
        households_merged.to_frame(), auto_ownership_spec)

    print "Choices:\n", choices.value_counts()

    orca.add_column("households", "auto_ownership", choices)

    add_dependent_columns("households", "households_autoown")
예제 #24
0
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec):

    choices, _ = asim.simple_simulate(households_merged.to_frame(),
                                      auto_ownership_spec)

    print "Choices:\n", choices.value_counts()

    orca.add_column("households", "auto_ownership", choices)

    add_dependent_columns("households", "households_autoown")
예제 #25
0
    def decorator(func):
        name = func.__name__

        logger.debug("inject column %s.%s" % (table_name, name))

        column_key = (table_name, name)

        assert not _DECORATED_COLUMNS.get(column_key, False)
        _DECORATED_COLUMNS[column_key] = {'func': func, 'cache': cache}

        orca.add_column(table_name, name, func, cache=cache)

        return func
예제 #26
0
def patch_mandatory_tour_destination(mandatory_tours_merged):

    """
    Patch destination column of mandatory tours with school or workplace taz
    to conform to non-mandatory tours naming so that computed columns in the tours
    table can use destination for any tour type.
    """

    mandatory_tours_merged['destination'] = \
        np.where(mandatory_tours_merged['tour_type'] == 'school',
                 mandatory_tours_merged['school_taz'],
                 mandatory_tours_merged['workplace_taz'])

    orca.add_column("mandatory_tours", "destination", mandatory_tours_merged.destination)
예제 #27
0
def cdap_simulate(persons_merged, cdap_settings, cdap_indiv_spec,
                  cdap_interaction_coefficients,
                  cdap_fixed_relative_proportions, chunk_size, trace_hh_id):
    """
    CDAP stands for Coordinated Daily Activity Pattern, which is a choice of
    high-level activity pattern for each person, in a coordinated way with other
    members of a person's household.

    Because Python requires vectorization of computation, there are some specialized
    routines in the cdap directory of activitysim for this purpose.  This module
    simply applies those utilities using the simulation framework.
    """

    persons_df = persons_merged.to_frame()

    constants = config.get_model_constants(cdap_settings)

    logger.info("Running cdap_simulate with %d persons" %
                len(persons_df.index))

    choices = run_cdap(
        persons=persons_df,
        cdap_indiv_spec=cdap_indiv_spec,
        cdap_interaction_coefficients=cdap_interaction_coefficients,
        cdap_fixed_relative_proportions=cdap_fixed_relative_proportions,
        locals_d=constants,
        chunk_size=chunk_size,
        trace_hh_id=trace_hh_id,
        trace_label='cdap')

    tracing.print_summary('cdap_activity',
                          choices.cdap_activity,
                          value_counts=True)

    print pd.crosstab(persons_df.ptype, choices.cdap_activity, margins=True)

    choices = choices.reindex(persons_merged.index)
    orca.add_column("persons", "cdap_activity", choices.cdap_activity)
    orca.add_column("persons", "cdap_rank", choices.cdap_rank)

    pipeline.add_dependent_columns("persons", "persons_cdap")
    pipeline.add_dependent_columns("households", "households_cdap")

    if trace_hh_id:

        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         label="cdap",
                         columns=['ptype', 'cdap_rank', 'cdap_activity'],
                         warn_if_empty=True)
예제 #28
0
def cdap_simulate(set_random_seed, persons_merged,
                  cdap_1_person_spec, cdap_2_person_spec, cdap_3_person_spec,
                  cdap_final_rules, cdap_all_people):

    choices = cdap.run_cdap(persons_merged.to_frame(),
                            "household_id",
                            "ptype",
                            cdap_1_person_spec,
                            cdap_2_person_spec,
                            cdap_3_person_spec,
                            cdap_final_rules,
                            cdap_all_people)

    print "Choices:\n", choices.value_counts()
    orca.add_column("persons", "cdap_activity", choices)
예제 #29
0
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec):
    """
    Auto ownership is a standard model which predicts how many cars a household
    with given characteristics owns
    """

    choices, _ = asim.simple_simulate(
        households_merged.to_frame(), auto_ownership_spec)

    tracing.print_summary('auto_ownership', choices, value_counts=True)

    orca.add_column("households", "auto_ownership", choices)

    add_dependent_columns("households", "households_autoown")
예제 #30
0
def destination_choice(set_random_seed,
                       non_mandatory_tours_merged,
                       skims,
                       destination_choice_spec,
                       destination_size_terms):

    # choosers are tours - in a sense tours are choosing their destination
    choosers = non_mandatory_tours_merged.to_frame()
    alternatives = destination_size_terms.to_frame()
    spec = destination_choice_spec.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    choices_list = []
    # segment by trip type and pick the right spec for each person type
    for name, segment in choosers.groupby('tour_type'):

        # FIXME - there are two options here escort with kids and without
        if name == "escort":
            # FIXME just run one of the other models for now
            name = "shopping"

        # the segment is now available to switch between size terms
        locals_d['segment'] = name

        print spec.columns
        print name

        print "Running segment '%s' of size %d" % (name, len(segment))

        choices, _ = asim.interaction_simulate(segment,
                                               alternatives,
                                               spec[[name]],
                                               skims=skims,
                                               locals_d=locals_d)

        choices_list.append(choices)

    choices = pd.concat(choices_list)

    print "Choices:\n", choices.describe()
    # every trip now has a destination which is the index from the
    # alternatives table - in this case it's the destination taz
    orca.add_column("non_mandatory_tours", "destination", choices)
예제 #31
0
def non_mandatory_scheduling(set_random_seed, non_mandatory_tours_merged,
                             tdd_alts, tdd_non_mandatory_spec):

    tours = non_mandatory_tours_merged.to_frame()

    print "Running %d non-mandatory tour scheduling choices" % len(tours)

    spec = tdd_non_mandatory_spec.to_frame()
    alts = tdd_alts.to_frame()

    choices = vectorize_tour_scheduling(tours, alts, spec)

    print "Choices:\n", choices.describe()

    orca.add_column("non_mandatory_tours", "tour_departure_and_duration",
                    choices)
예제 #32
0
def workplace_location_simulate(set_random_seed,
                                persons_merged,
                                workplace_location_spec,
                                skims,
                                destination_size_terms,
                                chunk_size):

    """
    The workplace location model predicts the zones in which various people will
    work.
    """

    # for now I'm going to generate a workplace location for everyone -
    # presumably it will not get used in downstream models for everyone -
    # it should depend on CDAP and mandatory tour generation as to whethrer
    # it gets used
    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    # FIXME - HACK - only include columns actually used in spec (which we pathologically know)
    choosers = choosers[["income_segment", "TAZ", "mode_choice_logsums"]]

    choices = asim.interaction_simulate(choosers,
                                        alternatives,
                                        workplace_location_spec,
                                        skims=skims,
                                        locals_d=locals_d,
                                        sample_size=50,
                                        chunk_size=chunk_size)

    # FIXME - no need to reindex?
    choices = choices.reindex(persons_merged.index)

    logger.info("%s workplace_taz choices min: %s max: %s" %
                (len(choices.index), choices.min(), choices.max()))

    tracing.print_summary('workplace_taz', choices, describe=True)

    orca.add_column("persons", "workplace_taz", choices)

    add_dependent_columns("persons", "persons_workplace")
예제 #33
0
def mandatory_tour_frequency(persons_merged,
                             mandatory_tour_frequency_spec,
                             mandatory_tour_frequency_settings,
                             trace_hh_id):
    """
    This model predicts the frequency of making mandatory trips (see the
    alternatives above) - these trips include work and school in some combination.
    """

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'M']
    logger.info("Running mandatory_tour_frequency with %d persons" % len(choosers))

    nest_spec = config.get_logit_model_settings(mandatory_tour_frequency_settings)
    constants = config.get_model_constants(mandatory_tour_frequency_settings)

    choices = asim.simple_simulate(
        choosers,
        spec=mandatory_tour_frequency_spec,
        nest_spec=nest_spec,
        locals_d=constants,
        trace_label=trace_hh_id and 'mandatory_tour_frequency',
        trace_choice_name='mandatory_tour_frequency')

    # convert indexes to alternative names
    choices = pd.Series(
        mandatory_tour_frequency_spec.columns[choices.values],
        index=choices.index).reindex(persons_merged.local.index)

    tracing.print_summary('mandatory_tour_frequency', choices, value_counts=True)

    orca.add_column("persons", "mandatory_tour_frequency", choices)
    pipeline.add_dependent_columns("persons", "persons_mtf")

    create_mandatory_tours_table()

    # FIXME - test prng repeatability
    r = pipeline.get_rn_generator().random_for_df(choices)
    orca.add_column("persons", "mtf_rand", [item for sublist in r for item in sublist])

    if trace_hh_id:
        trace_columns = ['mandatory_tour_frequency']
        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         label="mandatory_tour_frequency",
                         columns=trace_columns,
                         warn_if_empty=True)
예제 #34
0
def school_location_simulate(set_random_seed,
                             persons_merged,
                             school_location_spec,
                             skims,
                             destination_size_terms):

    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()
    spec = school_location_spec.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    choices_list = []
    for school_type in ['university', 'highschool', 'gradeschool']:

        locals_d['segment'] = school_type

        choosers_segment = choosers[choosers["is_" + school_type]]

        choices, _ = asim.interaction_simulate(choosers_segment,
                                               alternatives,
                                               spec[[school_type]],
                                               skims=skims,
                                               locals_d=locals_d,
                                               sample_size=50)
        choices_list.append(choices)

    choices = pd.concat(choices_list)

    # this fillna is necessary to avoid a downstream crash and might be a bit
    # wrong logically.  The issue here is that there is a small but non-zero
    # chance to choose a school trip even if not of the school type (because
    # of -999 rather than outright removal of alternative availability). -
    # this fills in the location for those uncommon circumstances,
    # so at least it runs
    choices = choices.reindex(persons_merged.index).fillna(-1)

    print "Describe of choices:\n", choices.describe()

    orca.add_column("persons", "school_taz", choices)

    add_dependent_columns("persons", "persons_school")
예제 #35
0
파일: models.py 프로젝트: psrc/urbansim2
def governmental_jobs_scaling(jobs, buildings, year):
    orca.add_column('buildings', 'existing', np.zeros(len(buildings),
                    dtype="int32"))
    alloc = AgentAllocationModel('existing', 'number_of_governmental_jobs',
                                 as_delta=False)
    jobs_to_place = jobs.local[np.logical_and(np.in1d(jobs.sector_id,
                                              [18, 19]), jobs.building_id < 0)]
    print "Locating %s governmental jobs" % len(jobs_to_place)
    loc_ids, loc_allo = alloc.locate_agents(orca.get_table
                                            ("buildings").to_frame
                                            (buildings.local_columns +
                                             ['number_of_governmental_jobs',
                                              'existing']), jobs_to_place,
                                            year=year)
    jobs.local.loc[loc_ids.index, buildings.index.name] = loc_ids
    print "Number of unplaced governmental jobs: %s" % np.logical_or(np.isnan(loc_ids), loc_ids < 0).sum()
    orca.add_table(jobs.name, jobs.local)
예제 #36
0
def mode_choice_simulate(tours_merged, mode_choice_spec, mode_choice_settings,
                         skims, omx_file):

    tours = tours_merged.to_frame()

    print mode_choice_spec.eatout

    # FIXME this only runs eatout
    choices = _mode_choice_simulate(tours[tours.tour_type == "eatout"],
                                    skims,
                                    get_segment_and_unstack(
                                        mode_choice_spec, 'eatout'),
                                    mode_choice_settings['CONSTANTS'],
                                    omx=omx_file)

    print "Choices:\n", choices.value_counts()
    orca.add_column("tours", "mode", choices)
예제 #37
0
def non_mandatory_scheduling(set_random_seed,
                             non_mandatory_tours_merged,
                             tdd_alts,
                             tdd_non_mandatory_spec):

    tours = non_mandatory_tours_merged.to_frame()

    print "Running %d non-mandatory tour scheduling choices" % len(tours)

    spec = tdd_non_mandatory_spec.to_frame()
    alts = tdd_alts.to_frame()

    choices = vectorize_tour_scheduling(tours, alts, spec)

    print "Choices:\n", choices.describe()

    orca.add_column(
        "non_mandatory_tours", "tour_departure_and_duration", choices)
예제 #38
0
def school_location_simulate(set_random_seed, persons_merged,
                             school_location_spec, skims,
                             destination_size_terms):

    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()
    spec = school_location_spec.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    choices_list = []
    for school_type in ['university', 'highschool', 'gradeschool']:

        locals_d['segment'] = school_type

        choosers_segment = choosers[choosers["is_" + school_type]]

        choices, _ = asim.interaction_simulate(choosers_segment,
                                               alternatives,
                                               spec[[school_type]],
                                               skims=skims,
                                               locals_d=locals_d,
                                               sample_size=50)
        choices_list.append(choices)

    choices = pd.concat(choices_list)

    # this fillna is necessary to avoid a downstream crash and might be a bit
    # wrong logically.  The issue here is that there is a small but non-zero
    # chance to choose a school trip even if not of the school type (because
    # of -999 rather than outright removal of alternative availability). -
    # this fills in the location for those uncommon circumstances,
    # so at least it runs
    choices = choices.reindex(persons_merged.index).fillna(-1)

    print "Describe of choices:\n", choices.describe()

    orca.add_column("persons", "school_taz", choices)

    add_dependent_columns("persons", "persons_school")
예제 #39
0
def make_reindex_injectable(from_table,
                            to_table,
                            col_name,
                            cache=True,
                            cache_scope='iteration'):
    """
    This creates a PK-PK reindex injectable.

    """
    def reindex_template():
        from_wrap = orca.get_table(from_table)
        to_wrap = orca.get_table(to_table)
        return from_wrap[col_name].reindex(to_wrap.index).fillna(0)

    orca.add_column(to_table,
                    col_name,
                    reindex_template,
                    cache=cache,
                    cache_scope=cache_scope)
예제 #40
0
def destination_choice(set_random_seed, non_mandatory_tours_merged, skims,
                       destination_choice_spec, destination_size_terms):

    # choosers are tours - in a sense tours are choosing their destination
    choosers = non_mandatory_tours_merged.to_frame()
    alternatives = destination_size_terms.to_frame()
    spec = destination_choice_spec.to_frame()

    # set the keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    skims.set_keys("TAZ", "TAZ_r")
    # the skims will be available under the name "skims" for any @ expressions
    locals_d = {"skims": skims}

    choices_list = []
    # segment by trip type and pick the right spec for each person type
    for name, segment in choosers.groupby('tour_type'):

        # FIXME - there are two options here escort with kids and without
        if name == "escort":
            # FIXME just run one of the other models for now
            name = "shopping"

        # the segment is now available to switch between size terms
        locals_d['segment'] = name

        print "Running segment '%s' of size %d" % (name, len(segment))

        choices, _ = asim.interaction_simulate(segment,
                                               alternatives,
                                               spec[[name]],
                                               skims=skims,
                                               locals_d=locals_d,
                                               sample_size=50)

        choices_list.append(choices)

    choices = pd.concat(choices_list)

    print "Choices:\n", choices.describe()
    # every trip now has a destination which is the index from the
    # alternatives table - in this case it's the destination taz
    orca.add_column("non_mandatory_tours", "destination", choices)
def mandatory_tour_frequency(set_random_seed, persons_merged,
                             mandatory_tour_frequency_spec):

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'Mandatory']
    print "%d persons run for mandatory tour model" % len(choosers)

    print choosers.workplace_taz.describe()

    choices, _ = asim.simple_simulate(choosers, mandatory_tour_frequency_spec)

    # convert indexes to alternative names
    choices = pd.Series(mandatory_tour_frequency_spec.columns[choices.values],
                        index=choices.index).reindex(
                            persons_merged.local.index)

    print "Choices:\n", choices.value_counts()
    orca.add_column("persons", "mandatory_tour_frequency", choices)
예제 #42
0
def mode_choice_simulate(tours_merged,
                         mode_choice_spec,
                         mode_choice_settings,
                         skims, omx_file):

    tours = tours_merged.to_frame()

    print mode_choice_spec.eatout

    # FIXME this only runs eatout
    choices = _mode_choice_simulate(
        tours[tours.tour_type == "eatout"],
        skims,
        get_segment_and_unstack(mode_choice_spec, 'eatout'),
        mode_choice_settings['CONSTANTS'],
        omx=omx_file)

    print "Choices:\n", choices.value_counts()
    orca.add_column("tours", "mode", choices)
def mandatory_tour_frequency(set_random_seed,
                             persons_merged,
                             mandatory_tour_frequency_spec):

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'Mandatory']
    print "%d persons run for mandatory tour model" % len(choosers)

    print choosers.workplace_taz.describe()

    choices, _ = asim.simple_simulate(choosers, mandatory_tour_frequency_spec)

    # convert indexes to alternative names
    choices = pd.Series(
        mandatory_tour_frequency_spec.columns[choices.values],
        index=choices.index).reindex(persons_merged.local.index)

    print "Choices:\n", choices.value_counts()
    orca.add_column("persons", "mandatory_tour_frequency", choices)
예제 #44
0
def non_mandatory_scheduling(set_random_seed, non_mandatory_tours_merged,
                             tdd_alts, tdd_non_mandatory_spec,
                             non_mandatory_scheduling_settings, chunk_size,
                             trace_hh_id):
    """
    This model predicts the departure time and duration of each activity for
    non-mandatory tours
    """

    tours = non_mandatory_tours_merged.to_frame()

    logger.info("Running non_mandatory_scheduling with %d tours" % len(tours))

    constants = get_model_constants(non_mandatory_scheduling_settings)

    spec = tdd_non_mandatory_spec.to_frame()
    alts = tdd_alts.to_frame()

    choices = vectorize_tour_scheduling(tours,
                                        alts,
                                        spec,
                                        constants,
                                        chunk_size,
                                        trace_label='non_mandatory_scheduling')

    tracing.print_summary(
        'non_mandatory_scheduling tour_departure_and_duration',
        choices,
        describe=True)

    orca.add_column("non_mandatory_tours", "tour_departure_and_duration",
                    choices)

    if trace_hh_id:
        tracing.trace_df(orca.get_table('non_mandatory_tours').to_frame(),
                         label="non_mandatory_tours",
                         slicer='person_id',
                         index_label='tour_id',
                         columns=None,
                         warn_if_empty=True)
예제 #45
0
def make_series_broadcast_injectable(from_series,
                                     to_table,
                                     col_name,
                                     fkey,
                                     fill_with=None,
                                     cache=True,
                                     cache_scope='iteration'):
    """
    Broadcasts an injected series to table.

    """
    def s_broadcast_template():
        b = broadcast(orca.get_injectable(from_series),
                      orca.get_table(to_table)[fkey])
        if fill_with is not None:
            b.fillna(fill_with, inplace=True)
        return b

    orca.add_column(to_table,
                    col_name,
                    s_broadcast_template,
                    cache=cache,
                    cache_scope=cache_scope)
예제 #46
0
def slr_inundate(scenario, parcels, slr_progression_C, slr_progression_R,
                 slr_progression_B, year, slr_parcel_inundation):
    if scenario == '1':
        slr_progression = slr_progression_C.to_frame()
    elif scenario == '2':
        slr_progression = slr_progression_R.to_frame()
    elif scenario == '5':
        slr_progression = slr_progression_B.to_frame()
    else:
        slr_progression = slr_progression_C.to_frame()  # placeholder
    inundation_yr = slr_progression.query('year==@year')['inundated'].item()
    print "Inundation in model year is %d inches" % inundation_yr
    slr_parcel_inundation = slr_parcel_inundation.to_frame()
    destroy_parcels = slr_parcel_inundation.\
        query('inundation<=@inundation_yr').astype('bool')
    orca.add_table('destroy_parcels', destroy_parcels)
    print "Number of parcels destroyed: %d" % len(destroy_parcels)

    slr_nodev = pd.Series(False, parcels.index)
    destroy = pd.Series(destroy_parcels['inundation'])
    slr_nodev.update(destroy)
    orca.add_column('parcels', 'slr_nodev', slr_nodev)
    parcels = orca.get_table("parcels")
예제 #47
0
def cdap_simulate(
    set_random_seed,
    persons_merged,
    cdap_1_person_spec,
    cdap_2_person_spec,
    cdap_3_person_spec,
    cdap_final_rules,
    cdap_all_people,
):

    choices = cdap.run_cdap(
        persons_merged.to_frame(),
        "household_id",
        "ptype",
        cdap_1_person_spec,
        cdap_2_person_spec,
        cdap_3_person_spec,
        cdap_final_rules,
        cdap_all_people,
    )

    print "Choices:\n", choices.value_counts()
    orca.add_column("persons", "cdap_activity", choices)
예제 #48
0
def slr_inundate(scenario, parcels, slr_progression_C, slr_progression_R,
                 slr_progression_B, year, slr_parcel_inundation):
    if scenario == '1':
        slr_progression = slr_progression_C.to_frame()
    elif scenario == '2':
        slr_progression = slr_progression_R.to_frame()
    elif scenario == '5':
        slr_progression = slr_progression_B.to_frame()
    else:
        slr_progression = slr_progression_C.to_frame()  # placeholder
    inundation_yr = slr_progression.query('year==@year')['inundated'].item()
    print "Inundation in model year is %d inches" % inundation_yr
    slr_parcel_inundation = slr_parcel_inundation.to_frame()
    destroy_parcels = slr_parcel_inundation.\
        query('inundation<=@inundation_yr').astype('bool')
    orca.add_table('destroy_parcels', destroy_parcels)
    print "Number of parcels destroyed: %d" % len(destroy_parcels)

    slr_nodev = pd.Series(False, parcels.index)
    destroy = pd.Series(destroy_parcels['inundation'])
    slr_nodev.update(destroy)
    orca.add_column('parcels', 'slr_nodev', slr_nodev)
    parcels = orca.get_table("parcels")
예제 #49
0
def patch_mandatory_tour_destination(mandatory_tours_merged, trace_hh_id):

    """
    Patch destination column of mandatory tours with school or workplace taz
    to conform to non-mandatory tours naming so that computed columns in the tours
    table can use destination for any tour type.
    """

    logger.info("Running patch_mandatory_tour_destination")

    mandatory_tours_merged['destination'] = \
        np.where(mandatory_tours_merged['tour_type'] == 'school',
                 mandatory_tours_merged['school_taz'],
                 mandatory_tours_merged['workplace_taz'])

    orca.add_column("mandatory_tours", "destination", mandatory_tours_merged.destination)

    if trace_hh_id:
        tracing.trace_df(orca.get_table('mandatory_tours').to_frame(),
                         label="mandatory_tours.destination",
                         slicer='person_id',
                         index_label='tour',
                         columns=None,
                         warn_if_empty=True)
예제 #50
0
def make_broadcast_injectable(from_table,
                              to_table,
                              col_name,
                              fkey,
                              cache=True,
                              cache_scope='iteration'):
    """
    This creates a broadcast column function/injectable and registers it with orca.

    Parameters:
    -----------
    from_table: str
        The table name to brodacast from (the right).
    to_table: str
        The table name to broadcast to (the left).
    col_name: str
        Name of the column to broadcast.
    fkey: str
        Name of column on the to table that serves as the foreign key.
    cache: bool, optional, default True
        Whether or not the broadcast is cached.
    cache_scope: str, optional, default `iteration`
        Cache scope for the broadcast.

    """
    def broadcast_template():

        return broadcast(
            orca.get_table(from_table)[col_name],
            orca.get_table(to_table)[fkey])

    orca.add_column(to_table,
                    col_name,
                    broadcast_template,
                    cache=cache,
                    cache_scope=cache_scope)
def non_mandatory_scheduling(set_random_seed,
                             non_mandatory_tours_merged,
                             tdd_alts,
                             tdd_non_mandatory_spec,
                             chunk_size):
    """
    This model predicts the departure time and duration of each activity for
    non-mandatory tours
    """

    tours = non_mandatory_tours_merged.to_frame()

    logger.info("Running %d non-mandatory tour scheduling choices" % len(tours))

    spec = tdd_non_mandatory_spec.to_frame()
    alts = tdd_alts.to_frame()

    choices = vectorize_tour_scheduling(tours, alts, spec, chunk_size)

    tracing.print_summary('non_mandatory_scheduling tour_departure_and_duration',
                          choices, describe=True)

    orca.add_column(
        "non_mandatory_tours", "tour_departure_and_duration", choices)
예제 #52
0
def destination_choice(set_random_seed,
                       non_mandatory_tours_merged,
                       skim_dict,
                       destination_choice_spec,
                       destination_choice_settings,
                       destination_size_terms,
                       chunk_size,
                       trace_hh_id):

    """
    Given the tour generation from the above, each tour needs to have a
    destination, so in this case tours are the choosers (with the associated
    person that's making the tour)
    """

    # choosers are tours - in a sense tours are choosing their destination
    choosers = non_mandatory_tours_merged.to_frame()
    alternatives = destination_size_terms.to_frame()
    spec = destination_choice_spec.to_frame()

    constants = get_model_constants(destination_choice_settings)

    if trace_hh_id:
        # register non_mandatory_tours so we can slice utilities
        tracing.register_tours(choosers, trace_hh_id)

    # create wrapper with keys for this lookup - in this case there is a TAZ in the choosers
    # and a TAZ in the alternatives which get merged during interaction
    # the skims will be available under the name "skims" for any @ expressions
    skims = skim_dict.wrap("TAZ", "TAZ_r")

    locals_d = {
        'skims': skims
    }
    if constants is not None:
        locals_d.update(constants)

    logger.info("Running destination_choice  with %d non_mandatory_tours" % len(choosers.index))

    choices_list = []
    # segment by trip type and pick the right spec for each person type
    for name, segment in choosers.groupby('tour_type'):

        # FIXME - there are two options here escort with kids and without
        kludge_name = name
        if name == "escort":
            logging.error("destination_choice escort not implemented - running shopping instead")
            kludge_name = "shopping"

        # the segment is now available to switch between size terms
        locals_d['segment'] = kludge_name

        # FIXME - no point in considering impossible alternatives
        alternatives_segment = alternatives[alternatives[kludge_name] > 0]

        logger.info("Running segment '%s' of %d tours %d alternatives" %
                    (name, len(segment), len(alternatives_segment)))

        # name index so tracing knows how to slice
        segment.index.name = 'tour_id'

        choices = asim.interaction_simulate(segment,
                                            alternatives_segment,
                                            spec[[kludge_name]],
                                            skims=skims,
                                            locals_d=locals_d,
                                            sample_size=50,
                                            chunk_size=chunk_size,
                                            trace_label='destination.%s' % name)

        choices_list.append(choices)

    choices = pd.concat(choices_list)

    # FIXME - can there be null destinations?
    if choices.isnull().any():
        logger.error("destination_choice had %s null destinations" % choices.isnull().sum())
        assert choices.isnull().sum() == 0

    tracing.print_summary('destination', choices, describe=True)

    # every trip now has a destination which is the index from the
    # alternatives table - in this case it's the destination taz
    orca.add_column("non_mandatory_tours", "destination", choices)

    if trace_hh_id:
        tracing.trace_df(orca.get_table('non_mandatory_tours').to_frame(),
                         label="destination",
                         slicer='person_id',
                         index_label='tour',
                         columns=None,
                         warn_if_empty=True)
예제 #53
0
def add_dependent_columns(base_dfname, new_dfname):
    tbl = orca.get_table(new_dfname)
    for col in tbl.columns:
        # logger.debug("Adding dependent column %s" % col)
        orca.add_column(base_dfname, col, tbl[col])
예제 #54
0
def compute_accessibility(settings, accessibility_spec, accessibility_settings,
                          skims, omx_file, land_use, trace_od):
    """
    Compute accessibility for each zone in land use file using expressions from accessibility_spec

    The actual results depend on the expressions in accessibility_spec, but this is initially
    intended to permit implementation of the mtc accessibility calculation as implemented by
    Accessibility.job

    Compute measures of accessibility used by the automobile ownership model.
    The accessibility measure first multiplies an employment variable by a mode-specific decay
    function.  The product reflects the difficulty of accessing the activities the farther
    (in terms of round-trip travel time) the jobs are from the location in question. The products
    to each destination zone are next summed over each origin zone, and the logarithm of the
    product mutes large differences.  The decay function on the walk accessibility measure is
    steeper than automobile or transit.  The minimum accessibility is zero.
    """

    logger.info("Running compute_accessibility")

    constants = get_model_constants(accessibility_settings)
    land_use_columns = accessibility_settings.get('land_use_columns', [])

    land_use_df = land_use.to_frame()

    zone_count = len(land_use_df.index)

    # create OD dataframe
    od_df = pd.DataFrame(
        data={
            'orig': np.repeat(np.asanyarray(land_use_df.index), zone_count),
            'dest': np.tile(np.asanyarray(land_use_df.index), zone_count)
        })

    if trace_od:
        trace_orig, trace_dest = trace_od
        trace_od_rows = (od_df.orig == trace_orig) & (od_df.dest == trace_dest)
    else:
        trace_od_rows = None

    # merge land_use_columns into od_df
    land_use_df = land_use_df[land_use_columns]
    od_df = pd.merge(od_df, land_use_df, left_on='dest',
                     right_index=True).sort_index()

    locals_d = {
        'log': np.log,
        'exp': np.exp,
        'skim_od': AccessibilitySkims(skims, omx_file, zone_count),
        'skim_do': AccessibilitySkims(skims,
                                      omx_file,
                                      zone_count,
                                      transpose=True)
    }
    if constants is not None:
        locals_d.update(constants)

    results, trace_results = asim_eval.assign_variables(
        accessibility_spec, od_df, locals_d, trace_rows=trace_od_rows)
    accessibility_df = pd.DataFrame(index=land_use.index)
    for column in results.columns:
        data = np.asanyarray(results[column])
        data.shape = (zone_count, zone_count)
        accessibility_df[column] = np.log(np.sum(data, axis=1) + 1)

        orca.add_column("accessibility", column, accessibility_df[column])

    if trace_od:

        if not trace_od_rows.any():
            logger.warn("trace_od not found origin = %s, dest = %s" %
                        (trace_orig, trace_dest))
        else:

            # add OD columns to trace results
            df = pd.concat([od_df[trace_od_rows], trace_results], axis=1)

            # dump the trace results table (with _temp variables) to aid debugging
            # note that this is not the same as the orca-injected accessibility table
            # FIXME - should we name this differently and also dump the updated accessibility table?
            tracing.trace_df(df,
                             label='accessibility',
                             index_label='skim_offset',
                             slicer='NONE',
                             warn_if_empty=True)

        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         "persons_merged",
                         warn_if_empty=True)
def earthquake_demolish(parcels, parcels_tract, tracts_earthquake, buildings,
                        households, jobs, residential_units, year, earthquake):
    if year == 2035 and earthquake:
        # assign each parcel to a census tract
        # using the lookup table created with "parcel_tract_assignment.ipynb"
        census_tract = pd.Series(parcels_tract['census_tract'],
                                 parcels_tract.index)
        print "Number of parcels with census tracts is: %d" % len(census_tract)
        orca.add_column('parcels', 'tract', census_tract)

        # group parcels by their census tract
        parcels_tract['parcel_id'] = parcels_tract.index
        parcels_tract = parcels_tract.to_frame(columns=['parcel_id',
                                                        'census_tract'])
        parcels_tract = parcels_tract[['census_tract', 'parcel_id']]
        tract_parcels_grp = []
        tracts = []
        parcels_tract = sorted(parcels_tract.values, key=itemgetter(0))
        for tract, parcels in itertools.groupby(parcels_tract,
                                                key=itemgetter(0)):
            tract_parcels_grp.append(list(parcels))
            tracts.append(tract)
        print "Number of census tract groups is: %d" % len(tract_parcels_grp)

        # for the parcels in each tract, destroy X% of parcels in that tract
        tracts_earthquake = tracts_earthquake.to_frame()
        tracts_earthquake = tracts_earthquake.sort_values(by=['tract_ba'])
        tracts_earthquake = tracts_earthquake.reset_index(drop=True)

        buildings = buildings.to_frame()
        eq_buildings = []
        existing_buildings = []
        new_buildings = []
        fire_buildings = []

        for i in range(len(tracts)):
            grp = [x[1] for x in tract_parcels_grp[i]]
            buildings_i = buildings[buildings['parcel_id'].isin(grp)]

            # existing buildings
            # select the buildings with highest fragility co-efficient
            # (and random no.) based on census tract pct to be destroyed
            existing_pct = tracts_earthquake['prop_eq'][i]
            build_frag = buildings_i['eq_destroy'].sort_values(ascending=False)
            top_build_frag = build_frag[: int(round(
                len(build_frag) * existing_pct))]
            # add to a list of buildings to destroy
            buildings_top = top_build_frag.index
            existing_buildings.extend(buildings_top)
            eq_buildings.extend(buildings_top)

            # new buildings
            # translate MMI to a probability
            # in-model is also nice if probabilities associated with
            # new buildings change
            buildings_new = buildings_i[buildings_i['year_built'] > 2015]
            if len(buildings_new) > 0:
                mmi = int(round(tracts_earthquake['shaking'][i]))
                if mmi < 6:
                    new_pct = 0
                elif mmi == 7:
                    new_pct = .002
                elif mmi == 8:
                    new_pct = .01
                elif mmi == 9:
                    new_pct = .05
                # randomly select buildings to be destroyed based on
                # percentages
                new_no = int(round(len(buildings_new)*new_pct))
                buildings_new_rand = np.random.choice(buildings_new.index,
                                                      new_no, replace=False)
                # add to a list of buildings to destroy
                if len(buildings_new_rand) > 0:
                    new_buildings.extend(buildings_new_rand)
                    eq_buildings.extend(buildings_new_rand)

            # fire buildings
            # select buildings to be destroyed by fire by looking only at
            # remaining buildings
            fire_pct = tracts_earthquake['prop_fire'][i]
            buildings_i_remain = buildings_i[~buildings_i.index.isin
                                             (buildings_top)]
            if len(buildings_new) > 0:
                buildings_i_remain = buildings_i_remain[~buildings_i_remain.
                                                        index.isin
                                                        (buildings_new_rand)]
            # select buildings to be destroyed based on random number
            # and census tract pct
            fire_buildings_rand = buildings_i_remain['fire_destroy']. \
                sort_values(ascending=False)
            top_fire_buildings = fire_buildings_rand[: int(round(
                len(fire_buildings_rand) * fire_pct))]
            # add to a list of buildings to destroy
            buildings_fire = top_fire_buildings.index
            fire_buildings.extend(buildings_fire)
            eq_buildings.extend(buildings_fire)

        print "Total number of buildings being destroyed is: %d" \
            % len(eq_buildings)

        orca.add_injectable("eq_buildings", eq_buildings)
        orca.add_injectable("existing_buildings", existing_buildings)
        orca.add_injectable("new_buildings", new_buildings)
        orca.add_injectable("fire_buildings", fire_buildings)

        # remove buildings, unplace agents
        buildings = orca.get_table('buildings')
        eq_demolish = buildings.local[buildings.index.isin
                                      (eq_buildings)]
        orca.add_table("eq_demolish", eq_demolish)
        print "Demolishing %d buildings" % len(eq_demolish)

        households = households.to_frame()
        hh_unplaced = households[households["building_id"] == -1]
        jobs = jobs.to_frame()
        jobs_unplaced = jobs[jobs["building_id"] == -1]

        l1 = len(buildings)
        # currently destroying more buildings than it is being
        # passed- why?
        buildings = utils._remove_developed_buildings(
            buildings.to_frame(buildings.local_columns),
            eq_demolish,
            unplace_agents=["households", "jobs"])

        households = orca.get_table("households")
        households = households.to_frame()
        hh_unplaced_eq = households[households["building_id"] == -1]
        hh_unplaced_eq = hh_unplaced_eq[~hh_unplaced_eq.index.isin
                                        (hh_unplaced.index)]
        orca.add_injectable("hh_unplaced_eq", hh_unplaced_eq)
        jobs = orca.get_table("jobs")
        jobs = jobs.to_frame()
        jobs_unplaced_eq = jobs[jobs["building_id"] == -1]
        jobs_unplaced_eq = jobs_unplaced_eq[~jobs_unplaced_eq.index.isin
                                            (jobs_unplaced.index)]
        orca.add_injectable("jobs_unplaced_eq", jobs_unplaced_eq)

        orca.add_table("buildings", buildings)
        buildings = orca.get_table("buildings")
        print "Demolished %d buildings" % (l1 - len(buildings))
def eq_code_buildings(buildings, year, earthquake):
    if year == 2035 and earthquake:
        # tags buildings that exist in 2035 with a fragility coefficient
        # keeping in-model adds run time, but is important given developer
        # model stochastisitcy, that will change the building stock in 2035
        # this also allows us to change the building codes when retrofitting
        # policies are applied, thus changing fragility coefficients
        buildings = buildings.to_frame()
        code = []
        fragilities = []

        for i in buildings.index:
            if (buildings['building_type'][i] == 'HS' and
               buildings['year_built'][i] <= 2015):
                a = 'SF'
                if buildings['stories'][i] == 1:
                    b = '01'
                    if buildings['year_built'][i] <= 1940:
                        c = 'G1'
                    elif (buildings['year_built'][i] >= 1941 and
                          buildings['year_built'][i] <= 1960):
                        c = 'G2'
                    elif (buildings['year_built'][i] >= 1961 and
                          buildings['year_built'][i] <= 1995):
                        c = 'G3'
                    elif (buildings['year_built'][i] >= 1996 and
                          buildings['year_built'][i] <= 2015):
                        c = 'G4'
                elif buildings['stories'][i] >= 2:
                    b = '2P'
                    if buildings['year_built'][i] <= 1920:
                        c = 'G1'
                    elif (buildings['year_built'][i] >= 1921 and
                          buildings['year_built'][i] <= 1940):
                        c = 'G2'
                    elif (buildings['year_built'][i] >= 1941 and
                          buildings['year_built'][i] <= 1960):
                        c = 'G3'
                    elif (buildings['year_built'][i] >= 1961 and
                          buildings['year_built'][i] <= 1995):
                        c = 'G4'
                    elif (buildings['year_built'][i] >= 1996 and
                          buildings['year_built'][i] <= 2015):
                        c = 'G5'
            elif ((buildings['building_type'][i] == 'HM' or
                  buildings['building_type'][i] == 'MR') and
                  buildings['year_built'][i] <= 2015):
                if (buildings['residential_units'][i] == 2 or
                   buildings['residential_units'][i] == 3 or
                   buildings['residential_units'][i] == 4):
                    a = 'DU'  # 2, 3, & 4 units
                    # are considered duplex/triplex/quadplex
                    if buildings['stories'][i] == 1:
                        b = '01'
                        if buildings['year_built'][i] <= 1940:
                            c = 'G1'
                        elif (buildings['year_built'][i] >= 1941 and
                              buildings['year_built'][i] <= 1960):
                            c = 'G2'
                        elif (buildings['year_built'][i] >= 1961 and
                              buildings['year_built'][i] <= 1995):
                            c = 'G3'
                        elif (buildings['year_built'][i] >= 1996 and
                              buildings['year_built'][i] <= 2015):
                            c = 'G4'
                    if buildings['stories'][i] >= 2:
                        b = '2P'
                        if buildings['year_built'][i] <= 1920:
                            c = 'G1'
                        elif (buildings['year_built'][i] >= 1921 and
                              buildings['year_built'][i] <= 1940):
                            c = 'G2'
                        elif (buildings['year_built'][i] >= 1941 and
                              buildings['year_built'][i] <= 1960):
                            c = 'G3'
                        elif (buildings['year_built'][i] >= 1961 and
                              buildings['year_built'][i] <= 1977):
                            c = 'G4'
                        elif (buildings['year_built'][i] >= 1978 and
                              buildings['year_built'][i] <= 1991):
                            c = 'G5'
                        elif (buildings['year_built'][i] >= 1992 and
                              buildings['year_built'][i] <= 2015):
                            c = 'G6'
                else:  # this assumes one-unit HM/MR buildings
                    # are also 5+ units (multifamily split by parcels)
                    a = 'MF'
                    if buildings['stories'][i] == 1:
                        b = '01'
                        if buildings['year_built'][i] <= 1920:
                            c = 'G1'
                        elif (buildings['year_built'][i] >= 1921 and
                              buildings['year_built'][i] <= 1940):
                            c = 'G2'
                        elif (buildings['year_built'][i] >= 1941 and
                              buildings['year_built'][i] <= 1960):
                            c = 'G3'
                        elif (buildings['year_built'][i] >= 1961 and
                              buildings['year_built'][i] <= 1995):
                            c = 'G4'
                        elif (buildings['year_built'][i] >= 1996 and
                              buildings['year_built'][i] <= 2015):
                            c = 'G5'
                    elif (buildings['stories'][i] >= 2 and
                          buildings['stories'][i] <= 5):
                        b = '25'
                        if buildings['year_built'][i] <= 1920:
                            c = 'G1'
                        elif (buildings['year_built'][i] >= 1921 and
                              buildings['year_built'][i] <= 1940):
                            c = 'G2'
                        elif (buildings['year_built'][i] >= 1941 and
                              buildings['year_built'][i] <= 1960):
                            c = 'G3'
                        elif (buildings['year_built'][i] >= 1961 and
                              buildings['year_built'][i] <= 1977):
                            c = 'G4'
                        elif (buildings['year_built'][i] >= 1978 and
                              buildings['year_built'][i] <= 1991):
                            c = 'G5'
                        elif (buildings['year_built'][i] >= 1992 and
                              buildings['year_built'][i] <= 2015):
                            c = 'G6'
                    elif buildings['stories'][i] >= 6:
                        b = '5P'
                        if buildings['year_built'][i] <= 1950:
                            c = 'G1'
                        elif (buildings['year_built'][i] >= 1951 and
                              buildings['year_built'][i] <= 1971):
                            c = 'G2'
                        elif (buildings['year_built'][i] >= 1972 and
                              buildings['year_built'][i] <= 1995):
                            c = 'G3'
                        elif (buildings['year_built'][i] >= 1996 and
                              buildings['year_built'][i] <= 2006):
                            c = 'G4'
                        elif (buildings['year_built'][i] >= 2007 and
                              buildings['year_built'][i] <= 2015):
                            c = 'G5'
            elif buildings['year_built'][i] <= 2015:
                a = 'OT'
                b = 'NN'
                if buildings['year_built'][i] <= 1933:
                    c = 'G1'
                elif (buildings['year_built'][i] >= 1934 and
                      buildings['year_built'][i] <= 1950):
                    c = 'G2'
                elif (buildings['year_built'][i] >= 1951 and
                      buildings['year_built'][i] <= 1972):
                    c = 'G3'
                elif (buildings['year_built'][i] >= 1973 and
                      buildings['year_built'][i] <= 1996):
                    c = 'G4'
                elif (buildings['year_built'][i] >= 1997 and
                      buildings['year_built'][i] <= 2006):
                    c = 'G5'
                elif (buildings['year_built'][i] >= 2007 and
                      buildings['year_built'][i] <= 2015):
                    c = 'G6'
            # new buildings built by the developer model
            elif buildings['year_built'][i] > 2015:
                a = 'NN'
                b = 'NN'
                c = 'NN'
                # alternative if retrofitted: d = 'R'
            d = 'N'
            code_i = a+b+c+d
            code.append(code_i)

            # assign a fragility coefficient based on building code
            if (code_i == 'SF01G4N' or code_i == 'SF2PG5N' or
               code_i == 'DU2PG6N' or code_i == 'MF5PG5N' or
               code_i == 'DU01G4N' or code_i == 'MF25G6N' or
               code_i == 'MF01G5N' or code_i == 'OTNNG6N'):
                fragility = 1
            elif (code_i == 'SF01G3N' or code_i == 'DU01G3N' or
                  code_i == 'DU2PG5N' or code_i == 'MF25G5N' or
                  code_i == 'MF01G4N' or code_i == 'OTNNG5N' or
                  code_i == 'MF5PG4N'):
                fragility = 1.2
            elif (code_i == 'SF2PG4N' or code_i == 'MF5PG3N' or
                  code_i == 'OTNNG4N'):
                fragility = 1.3
            elif (code_i == 'MF5PG1N' or code_i == 'OTNNG2N'):
                fragility = 1.4
            elif (code_i == 'MF01G3N' or code_i == 'MF5PG2N' or
                  code_i == 'SF01G2N' or code_i == 'DU01G2N' or
                  code_i == 'OTNNG3N'):
                fragility = 1.5
            elif (code_i == 'DU2PG3N' or code_i == 'DU2PG4N'):
                fragility = 1.75
            elif (code_i == 'SF2PG3N' or code_i == 'DU01G1N' or
                  code_i == 'DU2PG2N' or code_i == 'MF01G2N' or
                  code_i == 'OTNNG1N'):
                fragility = 2
            elif (code_i == 'SF2PG2N'):
                fragility = 2.25
            elif (code_i == 'DU2PG1N' or code_i == 'SF01G1N' or
                  code_i == 'SF2PG1N' or code_i == 'MF01G1N' or
                  code_i == 'MF25G1N'):
                fragility = 2.5
            elif (code_i == 'MF25G2N' or code_i == 'MF25G3N' or
                  code_i == 'MF25G4N'):
                fragility = 3
            elif (code_i == 'NNNNNNN'):
                fragility = 0
            fragilities.append(fragility)

        orca.add_injectable("code", code)
        orca.add_injectable("fragilities", fragilities)

        # add codes and fragilities as orca columns
        code = pd.Series(code, buildings.index)
        orca.add_column('buildings', 'earthquake_code', code)
        fragility = pd.Series(fragilities, buildings.index)
        orca.add_column('buildings', 'fragility_coef', fragility)

        # generate random number, multiply by fragilities
        buildings = orca.get_table('buildings')
        rand_eq = np.random.random(len(buildings))
        destroy_eq = pd.Series(rand_eq*fragility)
        orca.add_column('buildings', 'eq_destroy', destroy_eq)

        # generate random number for fire
        rand_fire = pd.Series(np.random.random(len(buildings)))
        orca.add_column('buildings', 'fire_destroy', rand_fire)
예제 #57
0
파일: misc.py 프로젝트: CSHong9/activitysim
def add_dependent_columns(base_dfname, new_dfname):
    tbl = orca.get_table(new_dfname)
    for col in tbl.columns:
        logger.info("Adding dependent column %s" % col)
        orca.add_column(base_dfname, col, tbl[col])
예제 #58
0
def add_dependent_columns(base_dfname, new_dfname):
    tbl = orca.get_table(new_dfname)
    for col in tbl.columns:
        print "Adding", col
        orca.add_column(base_dfname, col, tbl[col])
예제 #59
0
def lcm_simulate(cfg, choosers, buildings, join_tbls, out_fname,
                 supply_fname, vacant_fname,
                 enable_supply_correction=None):
    """
    Simulate the location choices for the specified choosers

    Parameters
    ----------
    cfg : string
        The name of the yaml config file from which to read the location
        choice model
    choosers : DataFrameWrapper
        A dataframe of agents doing the choosing
    buildings : DataFrameWrapper
        A dataframe of buildings which the choosers are locating in and which
        have a supply
    join_tbls : list of strings
        A list of land use dataframes to give neighborhood info around the
        buildings - will be joined to the buildings using existing broadcasts.
    out_fname : string
        The column name to write the simulated location to
    supply_fname : string
        The string in the buildings table that indicates the amount of
        available units there are for choosers, vacant or not
    vacant_fname : string
        The string in the buildings table that indicates the amount of vacant
        units there will be for choosers
    enable_supply_correction : Python dict
        Should contain keys "price_col" and "submarket_col" which are set to
        the column names in buildings which contain the column for prices and
        an identifier which segments buildings into submarkets
    """
    cfg = misc.config(cfg)

    choosers_df = to_frame(choosers, [], cfg, additional_columns=[out_fname])

    additional_columns = [supply_fname, vacant_fname]
    if enable_supply_correction is not None and \
            "submarket_col" in enable_supply_correction:
        additional_columns += [enable_supply_correction["submarket_col"]]
    if enable_supply_correction is not None and \
            "price_col" in enable_supply_correction:
        additional_columns += [enable_supply_correction["price_col"]]
    locations_df = to_frame(buildings, join_tbls, cfg,
                            additional_columns=additional_columns)

    available_units = buildings[supply_fname]
    vacant_units = buildings[vacant_fname]

    print "There are %d total available units" % available_units.sum()
    print "    and %d total choosers" % len(choosers)
    print "    but there are %d overfull buildings" % \
          len(vacant_units[vacant_units < 0])

    vacant_units = vacant_units[vacant_units > 0]

    # sometimes there are vacant units for buildings that are not in the
    # locations_df, which happens for reasons explained in the warning below
    indexes = np.repeat(vacant_units.index.values,
                        vacant_units.values.astype('int'))
    isin = pd.Series(indexes).isin(locations_df.index)
    missing = len(isin[isin == False])
    indexes = indexes[isin.values]
    units = locations_df.loc[indexes].reset_index()
    check_nas(units)

    print "    for a total of %d temporarily empty units" % vacant_units.sum()
    print "    in %d buildings total in the region" % len(vacant_units)

    if missing > 0:
        print "WARNING: %d indexes aren't found in the locations df -" % \
            missing
        print "    this is usually because of a few records that don't join "
        print "    correctly between the locations df and the aggregations tables"

    movers = choosers_df[choosers_df[out_fname] == -1]
    print "There are %d total movers for this LCM" % len(movers)

    if enable_supply_correction is not None:
        assert isinstance(enable_supply_correction, dict)
        assert "price_col" in enable_supply_correction
        price_col = enable_supply_correction["price_col"]
        assert "submarket_col" in enable_supply_correction
        submarket_col = enable_supply_correction["submarket_col"]

        lcm = yaml_to_class(cfg).from_yaml(str_or_buffer=cfg)

        if enable_supply_correction.get("warm_start", False) is True:
            raise NotImplementedError()

        multiplier_func = enable_supply_correction.get("multiplier_func", None)
        if multiplier_func is not None:
            multiplier_func = orca.get_injectable(multiplier_func)

        kwargs = enable_supply_correction.get('kwargs', {})
        new_prices, submarkets_ratios = supply_and_demand(
            lcm,
            movers,
            units,
            submarket_col,
            price_col,
            base_multiplier=None,
            multiplier_func=multiplier_func,
            **kwargs)

        # we will only get back new prices for those alternatives
        # that pass the filter - might need to specify the table in
        # order to get the complete index of possible submarkets
        submarket_table = enable_supply_correction.get("submarket_table", None)
        if submarket_table is not None:
            submarkets_ratios = submarkets_ratios.reindex(
                orca.get_table(submarket_table).index).fillna(1)
            # write final shifters to the submarket_table for use in debugging
            orca.get_table(submarket_table)["price_shifters"] = submarkets_ratios

        print "Running supply and demand"
        print "Simulated Prices"
        print buildings[price_col].describe()
        print "Submarket Price Shifters"
        print submarkets_ratios.describe()
        # we want new prices on the buildings, not on the units, so apply
        # shifters directly to buildings and ignore unit prices
        orca.add_column(buildings.name,
                        price_col+"_hedonic", buildings[price_col])
        new_prices = buildings[price_col] * \
            submarkets_ratios.loc[buildings[submarket_col]].values
        buildings.update_col_from_series(price_col, new_prices)
        print "Adjusted Prices"
        print buildings[price_col].describe()

    if len(movers) > vacant_units.sum():
        print "WARNING: Not enough locations for movers"
        print "    reducing locations to size of movers for performance gain"
        movers = movers.head(vacant_units.sum())

    new_units, _ = yaml_to_class(cfg).predict_from_cfg(movers, units, cfg)

    # new_units returns nans when there aren't enough units,
    # get rid of them and they'll stay as -1s
    new_units = new_units.dropna()

    # go from units back to buildings
    new_buildings = pd.Series(units.loc[new_units.values][out_fname].values,
                              index=new_units.index)

    choosers.update_col_from_series(out_fname, new_buildings)
    _print_number_unplaced(choosers, out_fname)

    if enable_supply_correction is not None:
        new_prices = buildings[price_col]
        if "clip_final_price_low" in enable_supply_correction:
            new_prices = new_prices.clip(lower=enable_supply_correction[
                "clip_final_price_low"])
        if "clip_final_price_high" in enable_supply_correction:
            new_prices = new_prices.clip(upper=enable_supply_correction[
                "clip_final_price_high"])
        buildings.update_col_from_series(price_col, new_prices)

    vacant_units = buildings[vacant_fname]
    print "    and there are now %d empty units" % vacant_units.sum()
    print "    and %d overfull buildings" % len(vacant_units[vacant_units < 0])
예제 #60
0
}

discrete_variables = {
    'households': ['persons', 'race_of_head', 'workers', 'children',
                   'cars', 'hispanic_head', 'tenure', 'recent_mover',
                   'income_quartile'],
    'jobs': ['sector_id'],
    # 'buildings': ['building_type_id']
}
sum_vars = ['persons', 'workers', 'children', 'cars', 'hispanic_head',
            'recent_mover', 'acres', 'land_value', 'residential_units',
            'non_residential_sqft', 'job_spaces', 'income']

geog_vars_to_dummify = orca.get_injectable('aggregate_geos').values()

orca.add_column(
    'parcels', 'sum_acres', orca.get_table('parcels').acres)  # temporary

for agent in variables_to_aggregate.keys():
    for geography_name, geography_id in geographic_levels.items():
        if geography_name != agent:

            # Define size variables
            generators.make_size_var(agent, geography_name, geography_id)

            # Define attribute variables
            variables = variables_to_aggregate[agent]
            for var in variables:
                for aggregation_function in aggregation_functions:
                    if aggregation_function == 'sum':
                        if var in sum_vars:
                            generators.make_agg_var(agent, geography_name,