def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') building_types_table_name = 'building_types' storage.write_table( table_name=building_types_table_name, table_data={ 'building_type_id':array([0,2]), 'name': array(['foo', 'commercial']) } ) buildings_table_name = 'buildings' storage.write_table( table_name=buildings_table_name, table_data={ 'building_id':array([1,2,3]), 'building_type_id': array([2,0,2]) } ) building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name) buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name) buildings.compute_variables(self.variable_name, resources=Resources({'building_type':building_types})) values = buildings.get_attribute(self.variable_name) should_be = array([1,0,1]) self.assert_(ma.allequal(values, should_be), 'Error in ' + self.variable_name)
def do_unplace_jobs_with_non_existing_buildings(self, jobs, in_storage): buildings = BuildingDataset(in_storage=in_storage) building_ids = jobs.get_attribute(buildings.get_id_name()[0]) valid_building_ids_idx = where(building_ids > 0)[0] index = buildings.try_get_id_index( building_ids[valid_building_ids_idx]) logger.log_status( "%s jobs have non-existing locations and are unplaced from buildings (parcel_id and zone_id are not affected)." % where(index < 0)[0].size) jobs.modify_attribute(name="building_id", data=-1, index=valid_building_ids_idx[index < 0])
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') building_types_table_name = 'building_types' storage.write_table(table_name=building_types_table_name, table_data={ 'building_type_id': array([1, 2]), 'name': array(['residential', 'commercial']), 'units': array(['residential_units', 'commercial_sqft']) }) buildings_table_name = 'buildings' storage.write_table( table_name=buildings_table_name, table_data={ 'building_id': arange(7) + 1, 'building_type_id': array([1, 2, 1, 2, 1, 1, 2]), 'sqft': array([100, 350, 1000, 0, 430, 95, 750]), 'residential_units': array([300, 0, 100, 0, 1300, 600, 10]) }, ) building_types = BuildingTypeDataset( in_storage=storage, in_table_name=building_types_table_name) buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name, resources=Resources({ 'building_categories': { 'residential': array([200, 500, 1200]), 'commercial': array([200, 500]) } })) variable_names = map( lambda type: '%s_%s' % (self.variable_name_prefix, type), ['commercial', 'residential']) buildings.compute_variables(variable_names, resources=Resources( {'building_type': building_types})) should_be_residential = array([2, 0, 1, 0, 4, 3, 0]) should_be_commercial = array([0, 2, 0, 1, 0, 0, 3]) values_commercial = buildings.get_attribute(variable_names[0]) values_residential = buildings.get_attribute(variable_names[1]) self.assert_(ma.allequal(values_commercial, should_be_commercial), 'Error in ' + variable_names[0]) self.assert_(ma.allequal(values_residential, should_be_residential), 'Error in ' + variable_names[1])
def setUp( self ): """here, we simulate 50 residential units and 5000 commercial, industrial, and governmental sqft added to each of the gridcells in previous years. """ ### TODO: do not redefine these constants. self.comc = 1 self.indc = 3 self.govc = 2 self.sfhc = 4 self.mfhc = 5 storage = StorageFactory().get_storage('dict_storage') gridcells_table_name = 'gridcells' # create 100 gridcells, each with 200 residential units and space for 100 commercial jobs, # 100 industrial jobs, and residential, industrial, and commercial value at $500,000 each storage.write_table( table_name=gridcells_table_name, table_data={ "grid_id": arange( 1, 100+1 ), "commercial_sqft_per_job":array( 100*[100] ), "industrial_sqft_per_job":array( 100*[100] ), "single_family_improvement_value":array( 100*[500000] ), "commercial_improvement_value":array( 100*[500000] ), "industrial_improvement_value":array( 100*[500000] ) } ) self.gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name) buildings_table_name = 'buildings' # 2000 buildings (1000 with 20 residential units each, 500 with 20 commercial job and 500 with 20 industrial job each) storage.write_table( table_name=buildings_table_name, table_data={ "building_id":arange( 1, 2000+1 ), # 2000 buildings "grid_id":array( 20*range( 1, 100+1 ), dtype=int32 ), # spread evenly across 100 gridcells "building_type_id":array(1000*[self.sfhc] + 500*[self.comc] + 500*[self.indc], dtype=int8), "sqft": array(1000*[0] + 500*[2000] + 500*[2000], dtype=int32), "residential_units": array(1000*[20] + 500* [0] + 500* [0], dtype=int32), "improvement_value": array(1000*[50] + 500* [50] + 500* [50], dtype=float32), "year_built": array(1000*[1940] + 500* [1940] + 500* [1940], dtype=int32) } ) self.buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name) households_table_name = 'households' # create 10000 households, 100 in each of the 100 gridcells. # there will initially be 100 vacant residential units in each gridcell then. storage.write_table( table_name=households_table_name, table_data={ "household_id":arange( 1, 10000+1 ), "grid_id":array( 100*range( 1, 100+1 ), dtype=int32 ) } ) self.households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name) building_types_table_name = 'building_types' storage.write_table( table_name=building_types_table_name, table_data={ "building_type_id":array([self.govc,self.comc,self.indc, self.sfhc, self.mfhc], dtype=int8), "name": array(["governmental", "commercial", "industrial", "single_family","multiple_family"]), "units": array(["governmental_sqft", "commercial_sqft", "industrial_sqft", "residential_units", "residential_units"]), "is_residential": array([0,0,0,1,1], dtype='?') } ) self.building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name) job_building_types_table_name = 'job_building_types' storage.write_table( table_name=job_building_types_table_name, table_data={ "id":array([self.govc,self.comc,self.indc, self.sfhc, self.mfhc], dtype=int8), "name": array(["governmental", "commercial", "industrial", "single_family","multiple_family"]) } ) self.job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name=job_building_types_table_name) jobs_table_name = 'jobs' # create 2500 commercial jobs and distribute them equally across the 100 gridcells, # 25 commercial buildings/gridcell storage.write_table( table_name=jobs_table_name, table_data={ "job_id":arange( 1, 2500+1 ), "grid_id":array( 25*range( 1, 100+1 ), dtype=int32 ), "sector_id":array( 2500*[1], dtype=int32 ), "building_type":array(2500*[self.comc], dtype=int8) } ) self.jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name) self.dataset_pool = DatasetPool() self.dataset_pool.add_datasets_if_not_included({ "household":self.households, "job":self.jobs, "building":self.buildings, "building_type": self.building_types, "job_building_type": self.job_building_types}) self.building_categories = {'commercial': array([1000,5000]), 'industrial': array([500,800,1000])}
def test_unrolling(self): from urbansim.datasets.gridcell_dataset import GridcellDataset from urbansim.datasets.building_dataset import BuildingDataset from urbansim.datasets.building_type_dataset import BuildingTypeDataset from opus_core.datasets.dataset_pool import DatasetPool from numpy import arange storage = StorageFactory().get_storage('dict_storage') gridcells_table_name = 'gridcells' storage.write_table( table_name = gridcells_table_name, table_data = { 'grid_id':array([1,2,3]), 'commercial_sqft':array([50,50,50]), 'industrial_sqft':array([100,100,100]), 'governmental_sqft':array([0,0,0]), 'residential_units':array([10,0,0]), 'commercial_improvement_value':array([0,0,0]), 'industrial_improvement_value':array([0,0,0]), 'governmental_improvement_value':array([0,0,0]), 'residential_improvement_value':array([0,0,0]), }, ) building_table_name = 'buildings' storage.write_table( table_name = building_table_name, table_data = { 'building_id': arange(6)+1, 'year_built':array([1999,1999,1998,1998,1998,1999]), 'grid_id':array([1,3,2,3,1,1]), 'sqft':array([10,20,30,40,0,20]), 'residential_units':array([0,0,0,0,5,0]), 'improvement_value':array([0,0,0,0,0,0]), 'building_type_id': array([1,2,1,2,3,1]) }, ) building_types_table_name = 'building_types' storage.write_table( table_name = building_types_table_name, table_data = { 'building_type_id':array([1,2,3,4]), 'name': array(['industrial', 'commercial', 'residential', 'governmental']) } ) building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name) gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name) buildings = BuildingDataset(in_storage=storage, in_table_name=building_table_name) dataset_pool = DatasetPool() dataset_pool._add_dataset(building_types.get_dataset_name(), building_types) roller = RollbackGridcellsFromBuildings() roller.unroll_gridcells_for_one_year(gridcells, buildings, 2000, dataset_pool) self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'), array([50,50,50]))) self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'), array([100,100,100]))) self.assert_(ma.allequal(gridcells.get_attribute('residential_units'), array([10, 0, 0]))) roller.unroll_gridcells_for_one_year(gridcells, buildings, 1999, dataset_pool) self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'), array([50,50,30])), 'Unexpected results: expected %s; received %s' % (array([50,50,30]), gridcells.get_attribute('commercial_sqft'))) self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'), array([70,100,100]))) roller.unroll_gridcells_for_one_year(gridcells, buildings, 1998, dataset_pool) self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'), array([50,50,0]))) self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'), array([70,70,100]))) self.assert_(ma.allequal(gridcells.get_attribute('residential_units'), array([5, 0, 0])))
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