def __init__(self): in_storage = StorageFactory().get_storage( 'sql_storage', hostname=settings.get_db_host_name(), username=settings.get_db_user_name(), password=settings.get_db_password(), database_name=settings.db) zones = ZoneDataset(in_storage=in_storage) print "Read and Write ZoneDataset." out_storage = StorageFactory().build_storage_for_dataset( type='flt_storage', storage_location=settings.dir) ReadWriteADataset(zones, out_storage=out_storage, out_table_name=settings.zonesubdir)
def get_zone_based_accessibility_into_cache(self, year): """ Imports accessibility results from MATSim into UrbanSim cache (zones table) """ logger.log_status('Importing zone-based accessibility indicators from MATSim ...') zone_data_set = ZoneDataset(in_storage=self.in_storage, in_table_name=self.zone_table_name) existing_zone_data_set = ZoneDataset( in_storage=self.cache_storage, in_table_name=self.zone_table_name ) existing_zone_data_set.join(zone_data_set, zone_data_set.get_non_id_primary_attribute_names(), metadata=AttributeType.PRIMARY) logger.log_status('Writing zone data to cache ...') flt_dir_for_next_year = os.path.join(self.cache_directory, str(year+1)) out_storage = StorageFactory().get_storage('flt_storage', storage_location = flt_dir_for_next_year) existing_zone_data_set.write_dataset(attributes=existing_zone_data_set.get_known_attribute_names(), out_storage=out_storage, out_table_name=self.zone_table_name) logger.log_status('Finished importing zone-based accessibility indicators to zone dataset.')
def test_getting_several_emme2_data_into_travel_data_set(self): if self._has_travel_model: num_zones = 30 zone_storage = StorageFactory().get_storage('dict_storage') zone_table_name = 'zone' zone_storage.write_table( table_name=zone_table_name, table_data={'zone_id': array(range(num_zones)) + 1}, ) zone_set = ZoneDataset(in_storage=zone_storage, in_table_name=zone_table_name) matrix_attribute_map = { 'au1tim': 'single_vehicle_to_work_travel_time', 'biketm': 'bike_to_work_travel_time' } tm_output = TravelModelOutput() travel_data_set = tm_output.get_travel_data_set( zone_set, matrix_attribute_map, self.real_bank_path) self.assertEqual( travel_data_set.get_attribute( 'single_vehicle_to_work_travel_time').size, num_zones * num_zones) self.assertEqual( travel_data_set.get_attribute('bike_to_work_travel_time').size, num_zones * num_zones) from numpy import ma self.assertEqual( False, ma.allclose( travel_data_set.get_attribute( 'single_vehicle_to_work_travel_time'), travel_data_set.get_attribute('bike_to_work_travel_time'))) else: logger.log_warning('Test skipped. TRAVELMODELROOT environment ' 'variable not found.')
coef, result = hlcm_psrc.estimate(specification, agents_psrc, agents_index=idx, estimate_config=Resources( {"estimation_size_agents": 0.01}), debuglevel=4) result = hlcm_psrc.run(specification, coef, agents_psrc, agents_index=sample(range(agents_psrc.size()), 500), debuglevel=4) storage = StorageFactory().get_storage('sql_storage', storage_location=db) zones_psrc = ZoneDataset(in_storage=storage) hlcm_psrc_zones = HouseholdLocationChoiceModelCreator().get_model( location_set=zones_psrc, sampler="opus_core.samplers.weighted_sampler", sample_size_locations=10, choices="opus_core.random_choices_from_index", compute_capacity_flag=False, estimate_config=Resources({"weights_for_estimation_string": None})) specification = EquationSpecification( variables=("urbansim.zone.number_of_jobs", "urbansim.household.income"), coefficients=("NOJ", "INC")) agents_psrc.compute_variables(["urbansim.household.zone_id"], resources=Resources({"gridcell": locations_psrc}))
def modify_zone_based_accessibility_into_cache(self, year): """ Modifies accessibility results from MATSim in UrbanSim cache (zones table) """ logger.log_status('Importing zone-based accessibility indicators from MATSim ...') existing_zone_data_set = ZoneDataset( in_storage=self.cache_storage, in_table_name=self.zone_table_name ) size = existing_zone_data_set.size() counter = 0 for id in range(0,size): # get row index of id index = existing_zone_data_set.get_id_index(id) faz_id = existing_zone_data_set.get_attribute_by_index(self.faz_id, index) # get original accessibility values car_accessibility = existing_zone_data_set.get_attribute_by_index('car_accessibility', index) walk_accessibility = existing_zone_data_set.get_attribute_by_index('walk_accessibility', index) bike_accessibility = existing_zone_data_set.get_attribute_by_index('bike_accessibility', index) freespeed_accessibility = existing_zone_data_set.get_attribute_by_index('freespeed_accessibility', index) # decrease accessibility if int(faz_id) == int(self.faz_number_decrease_acc): counter = counter + 1 # modify and store car accessibility car_accessibility = car_accessibility / self.factor existing_zone_data_set.modify_attribute('car_accessibility', car_accessibility, index ) # modify and store walk accessibility walk_accessibility = walk_accessibility / self.factor existing_zone_data_set.modify_attribute('walk_accessibility', walk_accessibility, index ) # modify and store bike accessibility bike_accessibility = bike_accessibility / self.factor existing_zone_data_set.modify_attribute('bike_accessibility', bike_accessibility, index ) # modify and store free accessibility freespeed_accessibility = freespeed_accessibility / self.factor existing_zone_data_set.modify_attribute('freespeed_accessibility', freespeed_accessibility, index ) # increase accessibility elif (int(faz_id) == int(self.faz_number_increase_acc_1)) or (int(faz_id) == int(self.faz_number_increase_acc_2)) or (int(faz_id) == int(self.faz_number_increase_acc_3)) or (int(faz_id) == int(self.faz_number_increase_acc_4)): counter = counter + 1 # modify and store car accessibility car_accessibility = car_accessibility * self.factor existing_zone_data_set.modify_attribute('car_accessibility', car_accessibility, index ) # modify and store walk accessibility walk_accessibility = walk_accessibility * self.factor existing_zone_data_set.modify_attribute('walk_accessibility', walk_accessibility, index ) # modify and store bike accessibility bike_accessibility = bike_accessibility * self.factor existing_zone_data_set.modify_attribute('bike_accessibility', bike_accessibility, index ) # modify and store free accessibility freespeed_accessibility = freespeed_accessibility * self.factor existing_zone_data_set.modify_attribute('freespeed_accessibility', freespeed_accessibility, index ) logger.log_status('Writing modified zone data to cache ...') flt_dir_for_next_year = os.path.join(self.cache_directory, str(year+1)) out_storage = StorageFactory().get_storage('flt_storage', storage_location = flt_dir_for_next_year) existing_zone_data_set.write_dataset(attributes=existing_zone_data_set.get_known_attribute_names(), out_storage=out_storage, out_table_name=self.zone_table_name) logger.log_status('Modified %d zones' %counter) logger.log_status('Finished modifying zone-based accessibility indicators in zone dataset.')
def get_resources(self, data_dictionary, dataset): """Create resources for computing a variable. """ resources = Resources() for key in data_dictionary.keys(): if key in self.datasets: data = data_dictionary[key] storage = StorageFactory().get_storage('dict_storage') if self.id_names[key] not in data_dictionary[key].keys( ) and not isinstance(self.id_names[key], list): data[self.id_names[key]] = arange( 1, len(data_dictionary[key][data_dictionary[key].keys() [0]]) + 1) # add id array id_name = self.id_names[key] storage.write_table(table_name='data', table_data=data) if key == "gridcell": gc = GridcellDataset(in_storage=storage, in_table_name='data') # add relative_x and relative_y gc.get_id_attribute() n = int(ceil(sqrt(gc.size()))) if "relative_x" not in data.keys(): x = (indices((n, n)) + 1)[1].ravel() gc.add_attribute(x[0:gc.size()], "relative_x", metadata=1) if "relative_y" not in data.keys(): y = (indices((n, n)) + 1)[0].ravel() gc.add_attribute(y[0:gc.size()], "relative_y", metadata=1) resources.merge({key: gc}) elif key == "household": resources.merge({ key: HouseholdDataset(in_storage=storage, in_table_name='data') }) elif key == "development_project": resources.merge({ key: DevelopmentProjectDataset(in_storage=storage, in_table_name='data') }) elif key == "development_event": resources.merge({ key: DevelopmentEventDataset(in_storage=storage, in_table_name='data') }) elif key == "neighborhood": resources.merge({ key: NeighborhoodDataset(in_storage=storage, in_table_name='data') }) elif key == "job": resources.merge({ key: JobDataset(in_storage=storage, in_table_name='data') }) elif key == "zone": resources.merge({ key: ZoneDataset(in_storage=storage, in_table_name='data') }) elif key == "travel_data": resources.merge({ key: TravelDataDataset(in_storage=storage, in_table_name='data') }) elif key == "faz": resources.merge({ key: FazDataset(in_storage=storage, in_table_name='data') }) elif key == "fazdistrict": resources.merge({ key: FazdistrictDataset(in_storage=storage, in_table_name='data') }) elif key == "race": resources.merge({ key: RaceDataset(in_storage=storage, in_table_name='data') }) elif key == "county": resources.merge({ key: CountyDataset(in_storage=storage, in_table_name='data') }) elif key == "large_area": resources.merge({ key: LargeAreaDataset(in_storage=storage, in_table_name='data') }) elif key == "development_group": resources.merge({ key: DevelopmentGroupDataset(in_storage=storage, in_table_name='data') }) elif key == "employment_sector_group": resources.merge({ key: EmploymentSectorGroupDataset(in_storage=storage, in_table_name='data') }) elif key == "plan_type_group": resources.merge({ key: PlanTypeGroupDataset(in_storage=storage, in_table_name='data') }) elif key == "building": resources.merge({ key: BuildingDataset(in_storage=storage, in_table_name='data') }) else: resources.merge({key: data_dictionary[key]}) if dataset in self.interactions: if dataset == "household_x_gridcell": resources.merge({ "dataset": HouseholdXGridcellDataset(dataset1=resources["household"], dataset2=resources["gridcell"]) }) if dataset == "job_x_gridcell": resources.merge({ "dataset": JobXGridcellDataset(dataset1=resources["job"], dataset2=resources["gridcell"]) }) if dataset == "household_x_zone": resources.merge({ "dataset": HouseholdXZoneDataset(dataset1=resources["household"], dataset2=resources["zone"]) }) if dataset == "household_x_neighborhood": resources.merge({ "dataset": HouseholdXNeighborhoodDataset( dataset1=resources["household"], dataset2=resources["neighborhood"]) }) if dataset == "development_project_x_gridcell": resources.merge({ "dataset": DevelopmentProjectXGridcellDataset( dataset1=resources["development_project"], dataset2=resources["gridcell"]) }) else: resources.merge({"dataset": resources[dataset]}) resources.merge({"check_variables": '*', "debug": 4}) return resources
def modify_zone_based_accessibility_into_cache(self, year): """ Modifies accessibility results from MATSim in UrbanSim cache (zones table) """ logger.log_status('Importing zone-based accessibility indicators from MATSim ...') existing_zone_data_set = ZoneDataset( in_storage=self.cache_storage, in_table_name=self.zone_table_name ) for zone_id in range(1318,2042): # get row index of zone_id index = existing_zone_data_set.get_id_index(zone_id) # get and modify car accessibility car_accessibility = existing_zone_data_set.get_attribute_by_index('car_accessibility', index) car_accessibility = car_accessibility / self.factor existing_zone_data_set.modify_attribute('car_accessibility', car_accessibility, index ) # get and modify walk accessibility walk_accessibility = existing_zone_data_set.get_attribute_by_index('walk_accessibility', index) walk_accessibility = walk_accessibility / self.factor existing_zone_data_set.modify_attribute('walk_accessibility', walk_accessibility, index ) # get and modify bike accessibility #bike_accessibility = existing_zone_data_set.get_attribute_by_index('bike_accessibility', index) #bike_accessibility = bike_accessibility / self.factor #existing_zone_data_set.modify_attribute('bike_accessibility', bike_accessibility, index ) # get and modify free accessibility #freespeed_accessibility = existing_zone_data_set.get_attribute_by_index('freespeed_accessibility', index) #freespeed_accessibility = freespeed_accessibility / self.factor #existing_zone_data_set.modify_attribute('freespeed_accessibility', freespeed_accessibility, index ) logger.log_status('Writing modified zone data to cache ...') flt_dir_for_next_year = os.path.join(self.cache_directory, str(year+1)) out_storage = StorageFactory().get_storage('flt_storage', storage_location = flt_dir_for_next_year) existing_zone_data_set.write_dataset(attributes=existing_zone_data_set.get_known_attribute_names(), out_storage=out_storage, out_table_name=self.zone_table_name) logger.log_status('Finished modifying zone-based accessibility indicators in zone dataset.')