def correct_alternative_filters_sample(residential_units, households, tenure): """ Creates modified versions of the alternatives and choosers Orca tables (residential units and households), so that the parameters that will be given to the hlcm_simulate() method are already filtered with the alternative filters defined in the hlcm_owner and hlcm_renter yaml files. Parameters ---------- residential_units: Orca table households: Orca table tenure: str, 'rent' or 'own' Returns ------- None. New tables of residential units and households by tenure segment are registered in Orca, with broadcasts linking them to each other and to the 'buildings' table. """ units = residential_units.to_frame() units_tenure = units[units.tenure == tenure] units_name = tenure + '_units' orca.add_table(units_name, units_tenure, cache=True, cache_scope='step') hh = households.to_frame() hh_tenure = hh[hh.tenure == tenure] hh_name = tenure + '_hh' orca.add_table(hh_name, hh_tenure, cache=True, cache_scope='step') orca.broadcast('buildings', units_name, cast_index=True, onto_on='building_id') orca.broadcast(units_name, hh_name, cast_index=True, onto_on='unit_id')
def tours(non_mandatory_tours, mandatory_tours, tdd_alts): non_mandatory_df = non_mandatory_tours.local mandatory_df = mandatory_tours.local # don't expect indexes to overlap assert len(non_mandatory_df.index.intersection(mandatory_df.index)) == 0 # expect non-overlapping indexes (so the tripids dont change) assert len( np.intersect1d(non_mandatory_df.index, mandatory_df.index, assume_unique=True)) == 0 tours = pd.concat( [non_mandatory_tours.to_frame(), mandatory_tours.to_frame()], ignore_index=False) # go ahead here and add the start, end, and duration here for future use chosen_tours = tdd_alts.to_frame().loc[tours.tour_departure_and_duration] chosen_tours.index = tours.index df = pd.concat([tours, chosen_tours], axis=1) assert df.index.name == 'tour_id' # replace table function with dataframe orca.add_table('tours', df) return df
def summarize(pets_merged, history, iter_var): pets_row = pets_merged.to_frame()[['pet_name', 'age', 'pet_activity_names']] pets_row['timestamp'] = iter_var df = history.to_frame().append(pets_row, ignore_index=True) orca.add_table(history.name, df)
def scheduled_development_events(buildings, scheduled_development_events, year): sched_dev = scheduled_development_events.to_frame() print sched_dev sched_dev = sched_dev[sched_dev.year_built == year] sched_dev["residential_sqft"] = sched_dev.sqft_per_unit * sched_dev.residential_units sched_dev["job_spaces"] = sched_dev.non_residential_sqft / 400 sched_dev = sched_dev.rename( columns={"development_type_id": "building_type_id", "nonres_rent_per_sqft": "unit_price_non_residential"} ) sched_dev["tax_exempt"] = 0 sched_dev["land_area"] = 0 sched_dev["bldg_sq_ft"] = sched_dev["residential_sqft"] + sched_dev["non_residential_sqft"] sched_dev["unit_price_residential"] = ( sched_dev["res_price_per_sqft"] * sched_dev["residential_sqft"] / sched_dev.residential_units ) if len(sched_dev) > 0: max_bid = buildings.index.values.max() idx = np.arange(max_bid + 1, max_bid + len(sched_dev) + 1) sched_dev["building_id"] = idx sched_dev = sched_dev.set_index("building_id") from urbansim.developer.developer import Developer merge = Developer(pd.DataFrame({})).merge b = buildings.to_frame(buildings.local_columns) all_buildings = merge(b, sched_dev[b.columns]) orca.add_table("buildings", all_buildings)
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 _remove_developed_buildings(old_buildings, new_buildings, unplace_agents): redev_buildings = old_buildings.parcel_id.isin(new_buildings.parcel_id) l = len(old_buildings) drop_buildings = old_buildings[redev_buildings] if "dropped_buildings" in orca.orca._TABLES: prev_drops = orca.get_table("dropped_buildings").to_frame() orca.add_table("dropped_buildings", pd.concat([drop_buildings, prev_drops])) else: orca.add_table("dropped_buildings", drop_buildings) old_buildings = old_buildings[np.logical_not(redev_buildings)] l2 = len(old_buildings) if l2-l > 0: print "Dropped {} buildings because they were redeveloped".\ format(l2-l) for tbl in unplace_agents: agents = orca.get_table(tbl).local displaced_agents = agents.building_id.isin(drop_buildings.index) print "Unplaced {} before: {}".format(tbl, len(agents.query( "building_id == -1"))) agents.building_id[displaced_agents] = -1 print "Unplaced {} after: {}".format(tbl, len(agents.query( "building_id == -1"))) return old_buildings
def network_aggregations_small(netsmall): """ This will be turned into a network aggregation template. """ nodessmall = networks.from_yaml(netsmall, 'network_aggregations_small.yaml') nodessmall = nodessmall.fillna(0) # new variables print('compute additional aggregation variables') nodessmall['pop_jobs_ratio_10000'] = (nodessmall['pop_10000'] / (nodessmall['jobs_10000'])).fillna(0) nodessmall['pop_jobs_ratio_25000'] = (nodessmall['pop_25000'] / (nodessmall['jobs_25000'])).fillna(0) # fill inf and nan with median nodessmall['pop_jobs_ratio_10000'] = nodessmall[ 'pop_jobs_ratio_10000'].replace([np.inf, -np.inf], np.nan).fillna( nodessmall['pop_jobs_ratio_10000'].median) nodessmall['pop_jobs_ratio_25000'] = nodessmall[ 'pop_jobs_ratio_25000'].replace([np.inf, -np.inf], np.nan).fillna( nodessmall['pop_jobs_ratio_25000'].median) # end of addition print(nodessmall.describe()) orca.add_table('nodessmall', nodessmall)
def load_tables(h5, year, tables=None): """ Loads tables for the desired year and registers them with orca. Parameters: ----------- h5: str full path to the h5 file containing the results. year: int or str Year to grab tables for. Provide 'base' for the base year. tables: list of str, default None List of tables to load. If None, all tables in that year will be loaded. """ with pd.HDFStore(h5, mode='r') as store: # grab all the table names in the current year if tables is None: tables = [ t.split('/')[-1] for t in store.keys() if t.startswith('/{}'.format(year)) ] elif not isinstance(tables, list): tables = [tables] # read in the table and register it with orca for t in tables: df = df = store['{}/{}'.format(year, t)] orca.add_table(t, df)
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'])
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
def simple_transition(tbl, rate, location_fname): """ Run a simple growth rate transition model on the table passed in Parameters ---------- tbl : DataFrameWrapper Table to be transitioned rate : float Growth rate location_fname : str The field name in the resulting dataframe to set to -1 (to unplace new agents) Returns ------- Nothing """ transition = GrowthRateTransition(rate) df = tbl.to_frame(tbl.local_columns) print "%d agents before transition" % len(df.index) df, added, copied, removed = transition.transition(df, None) print "%d agents after transition" % len(df.index) df.loc[added, location_fname] = -1 orca.add_table(tbl.name, df)
def neighborhood_vars_first(store): # Since this is the first time running neighborhood_vars, take from preproc # to save time nodes = store['neighborhood_vars_preproc'] print nodes.describe() orca.add_table("nodes", nodes)
def subsidized_residential_feasibility(parcels, settings, add_extra_columns_func, parcel_sales_price_sqft_func, parcel_is_allowed_func, parcels_geography): kwargs = settings['feasibility'].copy() kwargs["only_built"] = False kwargs["forms_to_test"] = ["residential"] # step 1 utils.run_feasibility(parcels, parcel_sales_price_sqft_func, parcel_is_allowed_func, **kwargs) feasibility = orca.get_table("feasibility").to_frame() # get rid of the multiindex that comes back from feasibility feasibility = feasibility.stack(level=0).reset_index(level=1, drop=True) # join to parcels_geography for filtering feasibility = feasibility.join(parcels_geography.to_frame()) # add the multiindex back feasibility.columns = pd.MultiIndex.from_tuples([ ("residential", col) for col in feasibility.columns ]) feasibility = policy_modifications_of_profit(feasibility, parcels) orca.add_table("feasibility", feasibility) df = orca.get_table("feasibility").to_frame() df = df.stack(level=0).reset_index(level=1, drop=True) df.to_csv("runs/run{}_feasibility_{}.csv".format( orca.get_injectable("run_number"), orca.get_injectable("year")))
def slr_remove_dev(buildings, destroy_parcels, year, parcels, households, jobs): slr_demolish = buildings.local[buildings.parcel_id.isin (destroy_parcels.index)] orca.add_table("slr_demolish", slr_demolish) print "Demolishing %d buildings" % len(slr_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) buildings = utils._remove_developed_buildings( buildings.to_frame(buildings.local_columns), slr_demolish, unplace_agents=["households", "jobs"]) households = orca.get_table("households") households = households.to_frame() hh_unplaced_slr = households[households["building_id"] == -1] hh_unplaced_slr = hh_unplaced_slr[~hh_unplaced_slr.index.isin (hh_unplaced.index)] orca.add_injectable("hh_unplaced_slr", hh_unplaced_slr) jobs = orca.get_table("jobs") jobs = jobs.to_frame() jobs_unplaced_slr = jobs[jobs["building_id"] == -1] jobs_unplaced_slr = jobs_unplaced_slr[~jobs_unplaced_slr.index.isin (jobs_unplaced.index)] orca.add_injectable("jobs_unplaced_slr", jobs_unplaced_slr) orca.add_table("buildings", buildings) buildings = orca.get_table("buildings") print "Demolished %d buildings" % (l1 - len(buildings))
def slr_remove_dev(buildings, destroy_parcels, year, parcels, households, jobs): slr_demolish = buildings.local[buildings.parcel_id.isin( destroy_parcels.index)] orca.add_table("slr_demolish", slr_demolish) print "Demolishing %d buildings" % len(slr_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) buildings = utils._remove_developed_buildings( buildings.to_frame(buildings.local_columns), slr_demolish, unplace_agents=["households", "jobs"]) households = orca.get_table("households") households = households.to_frame() hh_unplaced_slr = households[households["building_id"] == -1] hh_unplaced_slr = hh_unplaced_slr[~hh_unplaced_slr.index.isin(hh_unplaced. index)] orca.add_injectable("hh_unplaced_slr", hh_unplaced_slr) jobs = orca.get_table("jobs") jobs = jobs.to_frame() jobs_unplaced_slr = jobs[jobs["building_id"] == -1] jobs_unplaced_slr = jobs_unplaced_slr[~jobs_unplaced_slr.index. isin(jobs_unplaced.index)] orca.add_injectable("jobs_unplaced_slr", jobs_unplaced_slr) orca.add_table("buildings", buildings) buildings = orca.get_table("buildings") print "Demolished %d buildings" % (l1 - len(buildings))
def price_vars(net): nodes2 = networks.from_yaml(net["walk"], "price_vars.yaml") nodes2 = nodes2.fillna(0) print nodes2.describe() nodes = orca.get_table('nodes') nodes = nodes.to_frame().join(nodes2) orca.add_table("nodes", nodes)
def regional_vars_first(store): # Since this is the first time running regional_vars, take from preproc # to save time nodes = store['regional_vars_preproc'] print nodes.describe() orca.add_table("tmnodes", nodes)
def _remove_developed_buildings(old_buildings, new_buildings, unplace_agents): redev_buildings = old_buildings.parcel_id.isin(new_buildings.parcel_id) l = len(old_buildings) drop_buildings = old_buildings[redev_buildings] old_buildings = old_buildings[np.logical_not(redev_buildings)] l2 = len(old_buildings) if l2-l > 0: print "Dropped {} buildings because they were redeveloped".\ format(l2-l) for tbl in unplace_agents: agents = orca.get_table(tbl) cols = agents.local_columns if "building_id" not in cols: # if it's a unit-level model, need to add building_id # explicitly cols += ["building_id"] agents = agents.to_frame(cols) displaced_agents = agents.building_id.isin(drop_buildings.index) print "Unplaced {} before: {}".format(tbl, len(agents.query( "building_id == -1"))) agents.building_id[displaced_agents] = -1 print "Unplaced {} after: {}".format(tbl, len(agents.query( "building_id == -1"))) orca.add_table(tbl, agents) return old_buildings
def _remove_developed_buildings(old_buildings, new_buildings, unplace_agents): redev_buildings = old_buildings.parcel_id.isin(new_buildings.parcel_id) l = len(old_buildings) drop_buildings = old_buildings[redev_buildings] old_buildings = old_buildings[np.logical_not(redev_buildings)] l2 = len(old_buildings) if l2 - l > 0: print "Dropped {} buildings because they were redeveloped".\ format(l2-l) for tbl in unplace_agents: agents = orca.get_table(tbl) cols = agents.local_columns if "building_id" not in cols: # if it's a unit-level model, need to add building_id # explicitly cols += ["building_id"] agents = agents.to_frame(cols) displaced_agents = agents.building_id.isin(drop_buildings.index) print "Unplaced {} before: {}".format( tbl, len(agents.query("building_id == -1"))) agents.building_id[displaced_agents] = -1 print "Unplaced {} after: {}".format( tbl, len(agents.query("building_id == -1"))) orca.add_table(tbl, agents) return old_buildings
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")
def build_networks(settings , store, parcels, intersections): edges, nodes = store['edges'], store['nodes'] net = pdna.Network(nodes["x"], nodes["y"], edges["from"], edges["to"], edges[["weight"]]) max_distance = settings['build_networks']['max_distance'] net.precompute(max_distance) #SETUP POI COMPONENTS on_ramp_nodes = nodes[nodes.on_ramp] net.init_pois(num_categories=4, max_dist=max_distance, max_pois=1) net.set_pois('onramps', on_ramp_nodes.x, on_ramp_nodes.y) parks = store.parks net.set_pois('parks', parks.x, parks.y) schools = store.schools net.set_pois('schools', schools.x, schools.y) transit = store.transit net.set_pois('transit', transit.x, transit.y) orca.add_injectable("net", net) p = parcels.to_frame(parcels.local_columns) i = intersections.to_frame(intersections.local_columns) p['node_id'] = net.get_node_ids(p['x'], p['y']) i['node_id'] = net.get_node_ids(i['x'], i['y']) #p.to_csv('data/parcels.csv') orca.add_table("parcels", p) orca.add_table("intersections", i)
def subsidized_residential_feasibility( parcels, settings, add_extra_columns_func, parcel_sales_price_sqft_func, parcel_is_allowed_func, parcels_geography): kwargs = settings['feasibility'].copy() kwargs["only_built"] = False kwargs["forms_to_test"] = ["residential"] # step 1 utils.run_feasibility(parcels, parcel_sales_price_sqft_func, parcel_is_allowed_func, **kwargs) feasibility = orca.get_table("feasibility").to_frame() # get rid of the multiindex that comes back from feasibility feasibility = feasibility.stack(level=0).reset_index(level=1, drop=True) # join to parcels_geography for filtering feasibility = feasibility.join(parcels_geography.to_frame()) # add the multiindex back feasibility.columns = pd.MultiIndex.from_tuples( [("residential", col) for col in feasibility.columns]) feasibility = policy_modifications_of_profit(feasibility, parcels) orca.add_table("feasibility", feasibility)
def groupby_person(my_person_geo): df = my_person_geo.to_frame() df5 = df.groupby(by=geo_id)['persons'].count().to_frame() #df5[my_col[-1]] = df5.index df5.columns = my_col print df5.head() orca.add_table(file_name, df5)
def scheduled_development_events(scheduled_development_events, buildings, parcels): year = get_year() sched_dev = scheduled_development_events.to_frame() sched_dev = sched_dev.groupby('siteID').apply(lambda x: x.iloc[np.random.randint(0, len(x))]) phasein = pd.read_csv('data/schdev.csv') sched_dev = sched_dev.merge(phasein,left_on = 'siteID',right_on = 'siteID') sched_dev = sched_dev.loc[sched_dev['Units'] > 0] sched_dev = sched_dev[sched_dev.Year==year] #TODO: The simple division here is not consistent with other job_spaces calculations sched_dev['job_spaces'] = sched_dev.non_residential_sqft/400 if len(sched_dev) > 0: max_bid = buildings.index.values.max() idx = np.arange(max_bid + 1,max_bid+len(sched_dev)+1) sched_dev['building_id'] = idx sched_dev['building_type_id'] = np.where(sched_dev['Htype'] == 'MF', 21, 19) sched_dev = sched_dev.set_index('building_id') sched_dev['new_bldg'] = True sched_dev['year_built'] = year sched_dev['new_units'] = sched_dev['Units'] sched_dev['sch_dev'] = True sched_dev['residential_units'] = sched_dev['Units'] b = buildings.to_frame(buildings.local_columns) if 'residential_price' in b.columns: sched_dev['residential_price'] = 0 if 'non_residential_price' in b.columns: sched_dev['non_residential_price'] = 0 all_buildings = developer.Developer.merge(b,sched_dev[b.columns]) all_buildings = all_buildings.fillna(0) orca.add_table("buildings", all_buildings)
def workplace_location_sample(asim_persons_merged, workplace_location_sample_spec, workplace_location_settings, skim_dict, destination_size_terms, chunk_size, trace_hh_id): """ build a table of workers * all zones in order to select a sample of alternative work locations. PERID, dest_TAZ, rand, pick_count 23750, 14, 0.565502716034, 4 23750, 16, 0.711135838871, 6 ... 23751, 12, 0.408038878552, 1 23751, 14, 0.972732479292, 2 """ trace_label = 'workplace_location_sample' choosers = asim_persons_merged.to_frame() alternatives = destination_size_terms.to_frame() constants = asim_utils.get_model_constants(workplace_location_settings) sample_size = workplace_location_settings["SAMPLE_SIZE"] alt_col_name = workplace_location_settings["ALT_COL_NAME"] print("Running workplace_location_sample with %d persons" % len(choosers)) # 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) # FIXME - MEMORY HACK - only include columns actually used in spec chooser_columns = workplace_location_settings['SIMULATE_CHOOSER_COLUMNS'] choosers = choosers[chooser_columns] choices = interaction_sample( choosers, alternatives, sample_size=sample_size, alt_col_name=alt_col_name, spec=workplace_location_sample_spec, skims=skims, locals_d=locals_d, chunk_size=chunk_size, trace_label=trace_label) orca.add_table('workplace_location_sample', choices)
def test_all_cols_orca(df): """ Confirm that all_cols() works with Orca input. """ orca.add_table('df', df) cols = utils.all_cols('df') assert sorted(cols) == sorted(['id', 'val1', 'val2'])
def test_get_df_str(df): """ Confirm that get_df() works with str input. """ orca.add_table('df', df) df_out = utils.get_df('df') pd.testing.assert_frame_equal(df, df_out)
def neighborhood_vars(net): nodes = networks.from_yaml(net["walk"], "neighborhood_vars.yaml") nodes = nodes.replace(-np.inf, np.nan) nodes = nodes.replace(np.inf, np.nan) nodes = nodes.fillna(0) print nodes.describe() orca.add_table("nodes", nodes)
def initialize_new_units(buildings, residential_units): """ This data maintenance step initializes units for buildings that have been newly created, conforming to the data requirements of the 'residential_units' table. Data expectations ----------------- - 'buildings' table has the following columns: - index that serves as its identifier - 'residential_units' (int, count of units in building) - 'residential_units' table has the following columns: - index named 'unit_id' that serves as its identifier - 'building_id' corresponding to the index of the 'buildings' table Results ------- - extends the 'residential_units' table, following the same schema as the 'initialize_residential_units' model step """ # Verify initial data characteristics ''' ot.assert_orca_spec(OrcaSpec( '', TableSpec( 'buildings', ColumnSpec('building_id', primary_key=True), ColumnSpec('residential_units', min=0)), TableSpec( 'residential_units', ColumnSpec('unit_id', primary_key=True), ColumnSpec('building_id', foreign_key='buildings.building_id')))) ''' old_units = residential_units.to_frame(residential_units.local_columns) bldgs = buildings.to_frame(['residential_units', 'deed_restricted_units', 'building_id']) # Filter for residential buildings not currently represented in # the units table new_bldgs = bldgs[~bldgs.building_id.isin(old_units.building_id)] new_bldgs = new_bldgs[new_bldgs.residential_units > 0] if len(new_bldgs) == 0: return # Create new units, merge them, and update the table new_units = _create_empty_units(new_bldgs) all_units = dev.merge(old_units, new_units) all_units.index.name = 'unit_id' print "Creating %d residential units for %d new buildings" % \ (len(new_units), len(new_bldgs)) orca.add_table('residential_units', all_units) # Verify final data characteristics '''
def create_simple_trips(tours, households, persons, trace_hh_id): """ Create a simple trip table """ logger.info("Running simple trips table creation with %d tours" % len(tours.index)) tours_df = tours.to_frame() # we now have a tour_id column tours_df.reset_index(inplace=True) tours_df['household_id'] = reindex(persons.household_id, tours_df.person_id) tours_df['TAZ'] = reindex(households.TAZ, tours_df.household_id) # create inbound and outbound records trips = pd.concat([tours_df, tours_df], ignore_index=True) # first half are outbound, second half are inbound trips['INBOUND'] = np.repeat([False, True], len(trips.index) / 2) # TRIPID for outbound trips = 1, inbound_trips = 2 trips['trip_num'] = np.repeat([1, 2], len(trips.index) / 2) # set key fields from tour fields: 'TAZ','destination','start','end' trips['OTAZ'] = trips.TAZ trips['OTAZ'][trips.INBOUND] = trips.destination[trips.INBOUND] trips['DTAZ'] = trips.destination trips['DTAZ'][trips.INBOUND] = trips.TAZ[trips.INBOUND] trips['start_trip'] = trips.start trips['start_trip'][trips.INBOUND] = trips.end[trips.INBOUND] trips['end_trip'] = trips.end trips['end_trip'][trips.INBOUND] = trips.start[trips.INBOUND] # create a stable (predictable) index based on tour_id and trip_num possible_trips_count = 2 trips['trip_id'] = (trips.tour_id * possible_trips_count) + (trips.trip_num - 1) trips.set_index('trip_id', inplace=True, verify_integrity=True) trip_columns = [ 'tour_id', 'INBOUND', 'trip_num', 'OTAZ', 'DTAZ', 'start_trip', 'end_trip' ] trips = trips[trip_columns] orca.add_table("trips", trips) tracing.register_traceable_table('trips', trips) pipeline.get_rn_generator().add_channel(trips, 'trips') if trace_hh_id: tracing.trace_df(trips, label="trips", warn_if_empty=True)
def full_transition(agents, agent_controls, totals_column, year, location_fname, linked_tables=None, accounting_column=None, set_year_built=False): """ Run a transition model based on control totals specified in the usual UrbanSim way Parameters ---------- agents : DataFrameWrapper Table to be transitioned agent_controls : DataFrameWrapper Table of control totals totals_column : str String indicating the agent_controls column to use for totals. year : int The year, which will index into the controls location_fname : str The field name in the resulting dataframe to set to -1 (to unplace new agents) linked_tables : dict, optional Sets the tables linked to new or removed agents to be updated with dict of {'table_name':(DataFrameWrapper, 'link_id')} accounting_column : str, optional Name of column with accounting totals/quantities to apply toward the control. If not provided then row counts will be used for accounting. set_year_built: boolean Indicates whether to update 'year_built' columns with current simulation year Returns ------- Nothing """ ct = agent_controls.to_frame() agnt = agents.local print("Total agents before transition: {}".format(len(agnt))) tran = transition.TabularTotalsTransition(ct, totals_column, accounting_column) updated, added, copied, removed = tran.transition(agnt, year) updated.loc[added, location_fname] = -1 if set_year_built: updated.loc[added, 'year_built'] = year updated_links = {} if linked_tables: for table_name, (table, col) in linked_tables.iteritems(): print('updating linked table {}'.format(table_name)) updated_links[table_name] = \ update_linked_table(table, col, added, copied, removed) orca.add_table(table_name, updated_links[table_name]) print("Total agents after transition: {}".format(len(updated))) orca.add_table(agents.name, updated[agents.local_columns]) return updated, added, copied, removed
def network_aggregations_beam(netbeam): """ This will be turned into a network aggregation template. """ nodesbeam = networks.from_yaml(netbeam, 'network_aggregations_beam.yaml') nodesbeam = nodesbeam.fillna(0) print(nodesbeam.describe()) orca.add_table('nodesbeam', nodesbeam)
def network_aggregations_small(netsmall): """ This will be turned into a network aggregation template. """ nodessmall = networks.from_yaml( netsmall, 'network_aggregations_small.yaml') nodessmall = nodessmall.fillna(0) print(nodessmall.describe()) orca.add_table('nodessmall', nodessmall)
def orca_session(): """ Set up a clean Orca session, with a data table. """ orca.clear_all() df = pd.DataFrame({'a': [0.1, 1.33, 2.4]}, index=[1, 2, 3]) orca.add_table('tab', df)
def households_transition(households, annual_household_control_totals, year): ct = annual_household_control_totals.to_frame() tran = transition.TabularTotalsTransition(ct, 'total_number_of_households') model = transition.TransitionModel(tran) hh = households.to_frame(households.local_columns + ['activity_id']) new, added_hh_idx, empty_dict = \ model.transition(hh, year,) new.loc[added_hh_idx, "building_id"] = -1 orca.add_table("households", new)
def test_validation_multiindex_unique(orca_session): """ Table validation should pass with a MultiIndex whose combinations are unique. """ d = {'id': [1, 1, 1], 'sub_id': [1, 2, 3], 'value': [4, 4, 4]} orca.add_table('tab', pd.DataFrame(d).set_index(['id', 'sub_id'])) validate_table('tab')
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'])
def scheduled_development_events(buildings, development_projects, demolish_events, summary, year, parcels, mapping, years_per_iter, parcels_geography, building_sqft_per_job, vmt_fee_categories, static_parcels): # first demolish demolish = demolish_events.to_frame().\ query("%d <= year_built < %d" % (year, year + years_per_iter)) print("Demolishing/building %d buildings" % len(demolish)) l1 = len(buildings) buildings = utils._remove_developed_buildings( buildings.to_frame(buildings.local_columns), demolish, unplace_agents=["households", "jobs"]) orca.add_injectable( 'static_parcels', np.append(static_parcels, demolish.loc[demolish.action == 'build', 'parcel_id'])) orca.add_table("buildings", buildings) buildings = orca.get_table("buildings") print("Demolished %d buildings" % (l1 - len(buildings))) print(" (this number is smaller when parcel has no existing buildings)") # then build dps = development_projects.to_frame().\ query("%d <= year_built < %d" % (year, year + years_per_iter)) if len(dps) == 0: return new_buildings = utils.scheduled_development_events( buildings, dps, remove_developed_buildings=False, unplace_agents=['households', 'jobs']) new_buildings["form"] = new_buildings.building_type.map( mapping['building_type_map']).str.lower() new_buildings["job_spaces"] = new_buildings.non_residential_sqft / \ new_buildings.building_type.fillna("OF").map(building_sqft_per_job) new_buildings["job_spaces"] = new_buildings.job_spaces.\ fillna(0).astype('int') new_buildings["geom_id"] = parcel_id_to_geom_id(new_buildings.parcel_id) new_buildings["SDEM"] = True new_buildings["subsidized"] = False new_buildings["zone_id"] = misc.reindex(parcels.zone_id, new_buildings.parcel_id) new_buildings["vmt_res_cat"] = misc.reindex(vmt_fee_categories.res_cat, new_buildings.zone_id) del new_buildings["zone_id"] new_buildings["pda"] = parcels_geography.pda_id.loc[ new_buildings.parcel_id].values new_buildings["juris_trich"] = parcels_geography.juris_trich.loc[ new_buildings.parcel_id].values summary.add_parcel_output(new_buildings)
def regional_vars(net): nodes = networks.from_yaml(net["drive"], "regional_vars.yaml") nodes = nodes.fillna(0) nodes2 = pd.read_csv('data/regional_poi_distances.csv', index_col="tmnode_id") nodes = pd.concat([nodes, nodes2], axis=1) print nodes.describe() orca.add_table("tmnodes", nodes)
def simple_transition(tbl, rate, location_fname): transition = GrowthRateTransition(rate) df = tbl.to_frame(tbl.local_columns) print "%d agents before transition" % len(df.index) df, added, copied, removed = transition.transition(df, None) print "%d agents after transition" % len(df.index) df.loc[added, location_fname] = -1 orca.add_table(tbl.name, df)
def add_lag_tables(lag, year, base_year, filename, table_names): store = pd.HDFStore(filename, mode="r") prefix = max(year-lag, base_year) if prefix == base_year: prefix = "base" key_template = '{}/{{}}'.format(prefix) for table in table_names: orca.add_table("{}_lag1".format(table), store[key_template.format(table)], cache=True) store.close()
def initialize_new_units(buildings, residential_units): """ This data maintenance step initializes units for buildings that have been newly created, conforming to the data requirements of the 'residential_units' table. Data expectations ----------------- - 'buildings' table has the following columns: - index that serves as its identifier - 'residential_units' (int, count of units in building) - 'residential_units' table has the following columns: - index named 'unit_id' that serves as its identifier - 'building_id' corresponding to the index of the 'buildings' table Results ------- - extends the 'residential_units' table, following the same schema as the 'initialize_residential_units' model step """ # Verify initial data characteristics ''' ot.assert_orca_spec(OrcaSpec( '', TableSpec( 'buildings', ColumnSpec('building_id', primary_key=True), ColumnSpec('residential_units', min=0)), TableSpec( 'residential_units', ColumnSpec('unit_id', primary_key=True), ColumnSpec('building_id', foreign_key='buildings.building_id')))) ''' old_units = residential_units.to_frame(residential_units.local_columns) bldgs = buildings.to_frame(['residential_units', 'deed_restricted_units']) # Filter for residential buildings not currently represented in # the units table new_bldgs = bldgs[~bldgs.index.isin(old_units.building_id)] new_bldgs = new_bldgs[new_bldgs.residential_units > 0] # Create new units, merge them, and update the table new_units = _create_empty_units(new_bldgs) all_units = dev.merge(old_units, new_units) all_units.index.name = 'unit_id' print "Creating %d residential units for %d new buildings" % \ (len(new_units), len(new_bldgs)) orca.add_table('residential_units', all_units) # Verify final data characteristics '''
def compute_indicators(settings, iter_var): # loop over indicators and datasets from settings and store into file for ind, value in settings['indicators'].iteritems(): for ds in value['dataset']: ds_tablename = '%s_%s_%s' % (ds, ind, str(iter_var)) df = orca.get_table(ds)[ind] #print 'ds is %s and ind is %s' % (ds, ind) #print orca.get_table(ds)[ind].to_frame().head() orca.add_table(ds_tablename, df) ind_table_list.append(ds_tablename) orca.clear_cache()
def neighborhood_vars(net): nodes = networks.from_yaml(net["walk"], "neighborhood_vars.yaml") nodes = nodes.replace(-np.inf, np.nan) nodes = nodes.replace(np.inf, np.nan) nodes = nodes.fillna(0) # nodes2 = pd.read_csv('data/local_poi_distances.csv', index_col="node_id") # nodes = pd.concat([nodes, nodes2], axis=1) print nodes.describe() orca.add_table("nodes", nodes)
def build_networks(parcels): st = pd.HDFStore(os.path.join(misc.data_dir(), "osm_sandag.h5"), "r") nodes, edges = st.nodes, st.edges net = pdna.Network(nodes["x"], nodes["y"], edges["from"], edges["to"], edges[["weight"]]) net.precompute(3000) orca.add_injectable("net", net) p = parcels.to_frame(parcels.local_columns) p['node_id'] = net.get_node_ids(p['x'], p['y']) orca.add_table("parcels", p)
def wplcm_simulate(persons, households, jobs): # can only send in jobs that have a valid building_id, so remove unlocated jobs for now jobs_df = jobs.to_frame() jobs_df = jobs_df[jobs_df.building_id>0] jobs_df.index.name = 'job_id' orca.add_table('located_jobs', jobs_df) located_jobs = orca.get_table('located_jobs') res = utils.lcm_simulate("wplcmcoef.yaml", persons, located_jobs, None, "job_id", "number_of_jobs", "vacant_jobs", cast=True) orca.clear_cache()
def get_person_geo(): # join person data with hourshold data df4 = orca.merge_tables(target='my_household', tables=['my_person', 'my_household']) # geographic info to dictionary faz_dict = dict(zip(df3['census_2010_block_group_id'], df3['faz_id'])) zone_dict = dict(zip(df3['census_2010_block_group_id'], df3['zone_id'])) city_dict = dict(zip(df3['census_2010_block_group_id'], df3['city_id'])) # map geo info to person table df4['faz_id'] = df4['census_2010_block_group_id'].map(faz_dict) df4['zone_id'] = df4['census_2010_block_group_id'].map(zone_dict) df4['city_id'] = df4['census_2010_block_group_id'].map(city_dict) orca.add_table('my_person_geo', df4)
def inputs(input_jobs, input_sectors, input_groups, input_group_def): orca.clear_all() orca.add_table('jobs', input_jobs) orca.add_table('employment_sectors', input_sectors) orca.add_table('employment_sector_groups', input_groups) orca.add_table('employment_sector_group_definitions', input_group_def) import psrc_urbansim.vars.variables_jobs
def scheduled_development_events(buildings, development_projects, demolish_events, summary, year, parcels, settings, years_per_iter, parcels_geography, building_sqft_per_job, vmt_fee_categories): # first demolish demolish = demolish_events.to_frame().\ query("%d <= year_built < %d" % (year, year + years_per_iter)) print "Demolishing/building %d buildings" % len(demolish) l1 = len(buildings) buildings = utils._remove_developed_buildings( buildings.to_frame(buildings.local_columns), demolish, unplace_agents=["households", "jobs"]) orca.add_table("buildings", buildings) buildings = orca.get_table("buildings") print "Demolished %d buildings" % (l1 - len(buildings)) print " (this number is smaller when parcel has no existing buildings)" # then build dps = development_projects.to_frame().\ query("%d <= year_built < %d" % (year, year + years_per_iter)) if len(dps) == 0: return new_buildings = utils.scheduled_development_events( buildings, dps, remove_developed_buildings=False, unplace_agents=['households', 'jobs']) new_buildings["form"] = new_buildings.building_type.map( settings['building_type_map']).str.lower() new_buildings["job_spaces"] = new_buildings.non_residential_sqft / \ new_buildings.building_type.fillna("OF").map(building_sqft_per_job) new_buildings["job_spaces"] = new_buildings.job_spaces.\ fillna(0).astype('int') new_buildings["geom_id"] = parcel_id_to_geom_id(new_buildings.parcel_id) new_buildings["SDEM"] = True new_buildings["subsidized"] = False new_buildings["zone_id"] = misc.reindex( parcels.zone_id, new_buildings.parcel_id) new_buildings["vmt_res_cat"] = misc.reindex( vmt_fee_categories.res_cat, new_buildings.zone_id) del new_buildings["zone_id"] new_buildings["pda"] = parcels_geography.pda_id.loc[ new_buildings.parcel_id].values summary.add_parcel_output(new_buildings)
def add_buildings(buildings, new_buildings, remove_developed_buildings=True): old_buildings = buildings.to_frame(buildings.local_columns) new_buildings = new_buildings[buildings.local_columns] if remove_developed_buildings: unplace_agents = ["households", "jobs"] old_buildings = \ _remove_developed_buildings(old_buildings, new_buildings, unplace_agents) all_buildings = dev.merge(old_buildings, new_buildings) orca.add_table("buildings", all_buildings)
def scheduled_development_events(buildings, scheduled_development_events): year = get_year() sched_dev = scheduled_development_events.to_frame() sched_dev = sched_dev[sched_dev.year_built==year] sched_dev['residential_sqft'] = sched_dev.sqft_per_unit*sched_dev.residential_units sched_dev['job_spaces'] = sched_dev.non_residential_sqft/400 if len(sched_dev) > 0: max_bid = buildings.index.values.max() idx = np.arange(max_bid + 1,max_bid+len(sched_dev)+1) sched_dev['building_id'] = idx sched_dev = sched_dev.set_index('building_id') from urbansim.developer.developer import Developer merge = Developer(pd.DataFrame({})).merge b = buildings.to_frame(buildings.local_columns) all_buildings = merge(b,sched_dev[b.columns]) orca.add_table("buildings", all_buildings)
def inputs(input_pcl, input_bld, input_jobs, input_gcl, input_settings): #orca.clear_all() orca.add_table('parcels', input_pcl) orca.add_table('buildings', input_bld) orca.add_table('jobs', input_jobs) orca.add_table('gridcells', input_gcl) orca.add_injectable('settings', input_settings) import psrc_urbansim.vars.variables_jobs import psrc_urbansim.vars.variables_parcels
def run_feasibility(parcels, parcel_price_callback, parcel_use_allowed_callback, residential_to_yearly=True): """ Execute development feasibility on all parcels Parameters ---------- parcels : DataFrame Wrapper The data frame wrapper for the parcel data parcel_price_callback : function A callback which takes each use of the pro forma and returns a series with index as parcel_id and value as yearly_rent parcel_use_allowed_callback : function A callback which takes each form of the pro forma and returns a series with index as parcel_id and value and boolean whether the form is allowed on the parcel residential_to_yearly : boolean (default true) Whether to use the cap rate to convert the residential price from total sales price per sqft to rent per sqft Returns ------- Adds a table called feasibility to the sim object (returns nothing) """ pf = sqftproforma.SqFtProForma() df = parcels.to_frame() # add prices for each use for use in pf.config.uses: df[use] = parcel_price_callback(use) # convert from cost to yearly rent if residential_to_yearly: df["residential"] *= pf.config.cap_rate print("Describe of the yearly rent by use") print(df[pf.config.uses].describe()) d = {} for form in pf.config.forms: print("Computing feasibility for form %s" % form) d[form] = pf.lookup(form, df[parcel_use_allowed_callback(form)]) far_predictions = pd.concat(d.values(), keys=d.keys(), axis=1) orca.add_table("feasibility", far_predictions)
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)