def test_scaling_households_model_with_weights(self): storage = StorageFactory().get_storage('dict_storage') hhs_table_name = 'households' storage.write_table( table_name=hhs_table_name, table_data={ "household_id": arange(100)+1, "building_id":array(10*[1]+50*[2]+10*[3]+30*[-1]) } ) households = HouseholdDataset(in_storage=storage, in_table_name=hhs_table_name) buildings_table_name = 'buildings' storage.write_table( table_name=buildings_table_name, table_data={"building_id":arange(3)+1} ) buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name) # run model: Give the first building ten times as much weight as building 2. No weight for building 3. model = ScalingAgentsModel(weights="10*(building.building_id == 1) + (building.building_id == 2)", debuglevel=4) model.run(buildings, households, agents_index = arange(70, 100)) # all households are placed self.assertEqual((households['building_id']>0).all(), True) # get results buildings.compute_variables(["urbansim_parcel.building.number_of_households"], resources = Resources({"household":households})) result = buildings["number_of_households"] self.assertEqual(result.sum(), 100) res_incr = result - array([10, 50, 10]) # building increments self.assertEqual(res_incr[2], 0) # third building should have no increment # first building should get more HHs than the second building self.assertEqual(res_incr[1] < res_incr[0], True)
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 test_scaling_households_model(self): storage = StorageFactory().get_storage('dict_storage') hhs_table_name = 'households' storage.write_table( table_name=hhs_table_name, table_data={ "household_id": arange(100)+1, "building_id":array(10*[1]+60*[2]+30*[-1]) } ) households = HouseholdDataset(in_storage=storage, in_table_name=hhs_table_name) buildings_table_name = 'buildings' storage.write_table( table_name=buildings_table_name, table_data={"building_id":arange(2)+1} ) buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name) # run model model = ScalingAgentsModel(debuglevel=4) model.run(buildings, households, agents_index = arange(70, 100)) # all households are placed self.assertEqual((households['building_id']>0).all(), True) # get results buildings.compute_variables(["urbansim_parcel.building.number_of_households"], resources = Resources({"household":households})) result = buildings["number_of_households"] self.assertEqual(result.sum(), 100) res_incr = result - array([10, 60]) # building increments # second building should get many more HHs than the first building (at least twice as much) self.assertEqual(res_incr[1] > 2*res_incr[0], True)
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 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(6)+1, 'building_type_id': array([1,2,1,2,1,1]), 'sqft': array([100, 350, 1000, 0, 430, 95]), 'residential_units': array([300, 0, 100, 0, 1000, 600]) } ) 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})) should_be = array([300, 350, 100, 0, 1000, 600]) values = buildings.get_attribute(self.variable_name) self.assert_(ma.allequal(values, should_be), 'Error in ' + self.variable_name)
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 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 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]), "building_type_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)