def test_my_inputs(self): """Total number of commercial sqft of buildings. """ 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(['foo', 'commercial']) }, ) building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, data_dictionary = { 'zone':{ 'zone_id':array([1,2,3]), }, 'building': { 'building_type_id':array([1,2,1,2,1,1]), 'zone_id':array([2,3,1,1,2,1]), 'sqft':array([100, 350, 1000, 0, 430, 95]) }, 'building_type': building_types }, dataset = 'zone' ) should_be = array([0, 0, 350]) self.assert_(ma.allequal(values, should_be), 'Error in ' + self.variable_name)
def test_3(self): variable_name = "psrc.gridcell.travel_time_hbw_am_drive_alone_to_3" storage = StorageFactory().get_storage("dict_storage") storage.write_table( table_name="gridcells", table_data={"grid_id": array([1, 2, 3]), "zone_id": array([1, 1, 3])} ) storage.write_table( table_name="zones", table_data={ "zone_id": array([1, 2, 3]), "travel_time_hbw_am_drive_alone_to_1": array([1.1, 2.2, 3.3]), "travel_time_hbw_am_drive_alone_to_3": array([0.1, 0.2, 0.3]), }, ) dataset_pool = DatasetPool(package_order=["urbansim"], storage=storage) gridcell = dataset_pool.get_dataset("gridcell") gridcell.compute_variables(variable_name, dataset_pool=dataset_pool) values = gridcell.get_attribute(variable_name) should_be = array([0.1, 0.1, 0.3]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') persons_table_name = 'persons' storage.write_table( table_name=persons_table_name, table_data={ 'person_id':array([1, 2, 3, 4, 5]), 'household_id':array([1, 1, 3, 3, 3]), 'member_id':array([1,2,1,2,3]) }, ) persons = PersonDataset(in_storage=storage, in_table_name=persons_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, data_dictionary = { 'household':{ 'household_id':array([1,2,3]), 'grid_id':array([9, 9, 7]) }, 'person':persons }, dataset = 'person' ) should_be = array([9, 9, 7, 7, 7]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), 'Error in ' + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage("dict_storage") persons_table_name = "persons" storage.write_table( table_name=persons_table_name, table_data={ "person_id": array([1, 2, 3, 4, 5, 6]), "household_id": array([1, 1, 2, 3, 3, 3]), "member_id": array([1, 2, 1, 1, 2, 3]), "work_nonhome_based": array([1, 0, 0, 1, 0, 1]), "work_place_zone_id": array([71, -1, -1, 89, -1, -1]), }, ) persons = PersonDataset(in_storage=storage, in_table_name=persons_table_name) values = VariableTestToolbox().compute_variable( self.variable_name, data_dictionary={"household": {"household_id": array([1, 2, 3, 4])}, "person": persons}, dataset="household", ) should_be = array([1, 0, 1, 0]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), "Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') persons_table_name = 'persons' storage.write_table( table_name=persons_table_name, table_data={ 'person_id':array([1, 2, 3, 4, 5]), 'household_id':array([1, 1, 3, 3, 3]), 'member_id':array([1,2,1,2,3]), 'home_zone_id': array([3, 1, 1, 2, 3]), 'work_place_zone_id':array([1, 3, 3, 1, 2]) }, ) persons = PersonDataset(in_storage=storage, in_table_name=persons_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, data_dictionary = { 'person':persons, 'travel_data':{ 'from_zone_id':array([3,3,1,1,1,2,2,3,2]), 'to_zone_id': array([1,3,1,3,2,1,3,2,2]), 'am_single_vehicle_to_work_travel_time':array([1.1, 2.2, 3.3, 4.4, 0.5, 0.7, 8.7, 7.8, 1.0]) } }, dataset = 'person' ) should_be = array([1.1, 4.4, 4.4, 0.7, 7.8]) self.assert_(ma.allclose(values, should_be, rtol=1e-2), 'Error in ' + self.variable_name)
def test_my_input(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id": array([1,3]), "number_of_jobs": array([10, 1]) } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id":array([3,3,1,1]), "to_zone_id":array([1,3,1,3]), "am_total_transit_time_walk":array([1, 2, 3, 4]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([1.17361, 10.25]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='gridcells', table_data={ 'grid_id': array([1, 2, 3]), 'zone_id': array([1, 1, 3]), } ) storage.write_table( table_name='zones', table_data={ 'zone_id': array([1, 2, 3]), "trip_weighted_average_generalized_cost_hbw_to_work_am_drive_alone": array([4.1, 5.3, 6.2]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) gridcell = dataset_pool.get_dataset('gridcell') gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = gridcell.get_attribute(self.variable_name) should_be = array([4.1, 4.1, 6.2]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
def test_create_indicator_multiple_years(self): indicator_path = os.path.join(self.temp_cache_path, "indicators") self.assert_(not os.path.exists(indicator_path)) self.source_data.years = range(1980, 1984) indicator = Indicator(dataset_name="test", attribute="opus_core.test.attribute") maker = Maker(project_name="test", test=True) maker.create(indicator=indicator, source_data=self.source_data) for year in range(1980, 1984): storage_location = os.path.join(self.source_data.get_indicator_directory(), "_stored_data", repr(year)) self.assert_(os.path.exists(os.path.join(storage_location, "test"))) store = StorageFactory().get_storage(type="flt_storage", storage_location=storage_location) cols = store.get_column_names(table_name="test") self.assertEqual(sorted(cols), sorted(["attribute", "id"])) id_vals = [1, 2, 3, 4] attribute_vals = [5, 6, 7, 8] attribute_vals_1983 = [10, 12, 14, 16] data = store.load_table(table_name="test", column_names=cols) self.assertEqual(id_vals, list(data["id"])) if year == 1983: self.assertEqual(attribute_vals_1983, list(data["attribute"])) else: self.assertEqual(attribute_vals, list(data["attribute"]))
def test_my_input(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ 'zone_id': array([1, 3]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id":array([1,3,3,1]), "to_zone_id": array([1,1,3,3]), "am_pk_period_drive_alone_vehicle_trips":array([1, 7, 3, 4]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([8, 7]) self.assert_(ma.allequal(values, should_be), msg="Error in " + self.variable_name)
def test_my_inputs( self ): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='households', table_data={ 'household_id': array([1, 2, 3, 4]), 'age_of_head': array([12, 20, 25, 30]), } ) storage.write_table( table_name='urbansim_constants', table_data={ 'young_age': array([25]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) household = dataset_pool.get_dataset('household') household.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household.get_attribute(self.variable_name) should_be = array( [1,1,1,0] ) self.assert_(ma.allequal(values, should_be,), msg="Error in " + self.variable_name)
def test(self): storage = StorageFactory().get_storage('dict_storage') parcels_table_name = 'parcels' storage.write_table( table_name=parcels_table_name, table_data={ 'parcel_id':array([1,2,3,4]) }, ) parcels = ParcelDataset(in_storage=storage, in_table_name=parcels_table_name) sid = number_of_surveyed_households.surveyed_households_starting_id values = VariableTestToolbox().compute_variable(self.variable_name, \ data_dictionary = { 'parcel':parcels, 'household':{ 'parcel_id':array([1, 2, 3, 4, 2, 2]), 'household_id':array([sid, sid+11, sid-1, sid-5, sid+7, sid-6]), } }, dataset = 'parcel' ) should_be = array([1,1,0,0]) self.assert_(ma.allclose(values, should_be, rtol=1e-20), 'Error in ' + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id": array([1,2]), "trip_mode_bike": array([3,1]), "trip_mode_walk": array([5,6]), "trip_mode_park_ride": array([3,2]), "trip_mode_share_ride2": array([1,8]), "trip_mode_drive_alone": array([2,9]), "trip_mode_share_ride3": array([8,4]), "trip_mode_transit": array([5,5]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([8.0/27.0, 7.0/35.0]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_my_inputs( self ): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='households', table_data={ 'household_id': array([1, 2, 3, 4]), 'income': array([50, 100, 200, 300]), } ) storage.write_table( table_name='urbansim_constants', table_data={ "low_income_fraction": array([.25]), 'mid_income_fraction': array([.25]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) household = dataset_pool.get_dataset('household') household.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household.get_attribute(self.variable_name) should_be = array([0, 0, 0, 1]) self.assert_(ma.allequal(values, should_be,), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ 'zone_id': array([1, 2]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id": array([1,1,2,2]), 'to_zone_id': array([1,2,1,2]), "am_single_vehicle_to_work_travel_time":array([1.1, 2.2, 3.3, 4.4]), "am_pk_period_drive_alone_vehicle_trips":array([1.0, 2.0, 3.0, 4.0]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([(1.1*1.0 +2.2*2.0)/(3.0), (3.3*3.0 + 4.4*4.0)/(7.0)]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_with_all_zero_denominator(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id": array([1,2,3,4]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id":array([1,2,2,3,4]), "to_zone_id":array([1,2,1,2,2]), "am_single_vehicle_to_work_travel_time":array([1.1, 2.2, 3.3, 4.4, 5.5]), "am_pk_period_drive_alone_vehicle_trips":array([0, 0.0, 0.0, 0.0, 0.0]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([0.0, 0.0, 0.0, 0.0]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='gridcells', table_data={ 'grid_id': array([1,2,3,4]), 'relative_x': array([1,2,1,2]), 'relative_y': array([1,1,2,2]), 'is_development_type_residential': array([1, 1, 1, 0]) } ) storage.write_table( table_name='urbansim_constants', table_data={ "walking_distance_circle_radius": array([150]), 'cell_size': array([150]), "acres": array([105.0]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) gridcell = dataset_pool.get_dataset('gridcell') gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = gridcell.get_attribute(self.variable_name) should_be = array([5, 4, 4, 2]) self.assert_(ma.allclose( values, should_be, rtol=1e-7), msg = "Error in " + self.variable_name)
def test_safely_divide_two_attributes(self): from opus_core.datasets.dataset_pool import DatasetPool storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='tests', table_data={ 'id': array([1,2,3,4]), 'numerator': array([1,2,3,0]), 'denominator': array([2.,0.,2.,0.]), } ) dataset_pool = DatasetPool(package_order=['opus_core'], storage=storage) test = dataset_pool.get_dataset('test') variable = Variable() variable.set_dataset(test) result = variable.safely_divide_two_attributes('opus_core.test.numerator', 'opus_core.test.denominator') self.assert_(ma.allclose(array([.5, 0, 1.5, 0]), result)) result = variable.safely_divide_two_attributes('opus_core.test.numerator', 'opus_core.test.denominator', value_for_divide_by_zero=-1.0) self.assert_(ma.allclose(array([.5, -1., 1.5, -1.]), result))
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='buildings', table_data={ 'building_id': arange(50), 'residential_units': array(5*[0]+10*[20]+5*[15]+10*[50]+15*[3]+5*[45]), } ) storage.write_table( table_name='jobs', table_data={ 'job_id': array([1, 2, 3]), } ) dataset_pool = DatasetPool(package_order=['urbansim_parcel', 'urbansim'], storage=storage) job_x_building = dataset_pool.get_dataset('job_x_building', dataset_arguments={"index2": array([[13, 15, 23, 49], [5, 9, 17, 43], [17, 18, 40, 47]], dtype="int32")}) job_x_building.compute_variables(self.variable_name) values = job_x_building.get_attribute(self.variable_name) # The values are computed using formula from Ben-Akiva book (Chapter of correcting for sampling bias) should_be = array([[-11.3207881 , -11.03310603, -12.23707884, -12.13171832], [-15.01597613, -15.01597613, -14.72829406, -13.11885615], [-14.18521949, -14.18521949, -12.57578158, -15.28383178]]) + 11.03310603 self.assert_(ma.allclose(values, should_be, rtol=1e-4), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') parcels_table_name = 'parcels' storage.write_table( table_name=parcels_table_name, table_data={ 'parcel_id':array([1,2,3,4,5]), 'residential_units':array([1, 1, 3, 2, 3]), 'number_of_households':array([2, 1, 3, 0, 5]), 'county':array(['033','061','035','033','033']) }, ) parcels = ParcelDataset(in_storage=storage, in_table_name=parcels_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, \ data_dictionary = { 'parcel':parcels }, dataset = 'parcel' ) should_be = array([1, 0, 0, 0, 3]) #would be a 2-D array if it spanned more than one 'directory' self.assert_(ma.allclose(values, should_be, rtol=1e-05), 'Error in ' + self.variable_name)
def get_values(self, sector, threshold): self.variable_name = "urbansim_parcel.zone.sector_%s_employment_within_%s_minutes_travel_time_hbw_am_transit_walk" % (sector, threshold) storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id":array([1,3]), "number_of_jobs_of_sector_2":array([10, 1]), "number_of_jobs_of_sector_3":array([7, 2]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id": array([3,3,1,1]), "to_zone_id": array([1,3,1,3]), "am_total_transit_time_walk": array([1.1, 2.2, 3.3, 4.4]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) return values
def test_full_tree(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='parcels', table_data={ 'parcel_id':array([1,2,3,4]), 'is_in_city_seattle':array([1, 1, 0, 0]) }, ) storage.write_table( table_name='households', table_data={ 'household_id':array([1,2,3,4,5]), 'income':array([1000, 300000, 50000, 0, 10550]) }, ) dataset_pool = DatasetPool(package_order=['psrc', 'urbansim'], storage=storage) household_x_parcel = dataset_pool.get_dataset('household_x_parcel') household_x_parcel.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household_x_parcel.get_attribute(self.variable_name) should_be = array([[1000*1, 1000*1, 1000*0, 1000*0], [300000*1, 300000*1, 300000*0,300000*0 ], [50000*1, 50000*1, 50000*0,50000*0], [0, 0, 0, 0], [10550*1, 10550*1, 10550*0, 10550*0]]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
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([1, 2, 3, 4]), 'generic_building_type_id': array([2,3,1,1]) } ) buildings_table_name = 'buildings' storage.write_table( table_name=buildings_table_name, table_data={ 'building_id': array([1, 2, 3, 4, 5, 6]), 'building_type_id': array([2, 1, 2, 4, 3, 3]) } ) dataset_pool = DatasetPool(package_order=['urbansim_parcel', 'urbansim'], storage=storage) buildings = dataset_pool.get_dataset('building') values = buildings.compute_variables(self.variable_name, dataset_pool=dataset_pool) should_be = array([True, False, True, False, False, False]) self.assert_(ma.allequal(values, should_be), 'Error in ' + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='buildings', table_data={ 'building_id': array([1, 2, 3]), } ) storage.write_table( table_name='jobs', table_data={ 'job_id': array([1, 2, 3, 4, 5, 6]), 'sector_id': array([1, 1, 3, 2, 3, 3]), 'building_id': array([1, 1, 1, 2, 3, 3]) } ) dataset_pool = DatasetPool(package_order=['urbansim_parcel', 'urbansim'], storage=storage) buildings = dataset_pool.get_dataset('building') values = buildings.compute_variables(self.variable_name, dataset_pool=dataset_pool) should_be = array([1/3., 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") storage.write_table( table_name="gridcells", table_data={ "grid_id": arange(50), "residential_units": array(5 * [0] + 10 * [20] + 5 * [15] + 10 * [50] + 15 * [3] + 5 * [45]), }, ) storage.write_table(table_name="households", table_data={"household_id": array([1, 2, 3])}) dataset_pool = DatasetPool(package_order=["urbansim"], storage=storage) household_x_gridcell = dataset_pool.get_dataset( "household_x_gridcell", dataset_arguments={"index2": array([[13, 15, 23, 49], [5, 9, 17, 43], [17, 18, 40, 47]], dtype="int32")}, ) household_x_gridcell.compute_variables(self.variable_name) values = household_x_gridcell.get_attribute(self.variable_name) # The values are computed using formula from Ben-Akiva book (Chapter of correcting for sampling bias) should_be = ( array( [ [-11.3207881, -11.03310603, -12.23707884, -12.13171832], [-15.01597613, -15.01597613, -14.72829406, -13.11885615], [-14.18521949, -14.18521949, -12.57578158, -15.28383178], ] ) + 11.03310603 ) self.assert_(ma.allclose(values, should_be, rtol=1e-4), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') parcels_table_name = 'parcels' storage.write_table( table_name=parcels_table_name, table_data={ 'parcel_id':array([1,2,3,4,5]), 'grid_id':array([1, 1, 3, 2, 3]), }, ) parcels = ParcelDataset(in_storage=storage, in_table_name=parcels_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, \ data_dictionary = { 'gridcell':{ \ 'grid_id':array([1, 2, 3]), 'residential_units_within_walking_distance':array([100, 1000, 1500]) }, 'parcel':parcels }, dataset = 'parcel' ) should_be = array([100.0, 100.0, 1500.0, 1000.0, 1500.0]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), 'Error in ' + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') parcels_table_name = 'parcels' storage.write_table( table_name=parcels_table_name, table_data={ 'parcel_id':array([1,2,3,4,5]), 'is_in_city_seattle': array([1, 0, 1, 0, 3]), 'is_single_family_unit':array([1, 0, 0, 1, 3]), }, ) parcels = ParcelDataset(in_storage=storage, in_table_name=parcels_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, data_dictionary = { 'parcel':parcels,}, dataset = 'parcel' ) should_be = array([911, 900, 910, 901, 933]) 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') persons_table_name = 'persons' storage.write_table( table_name=persons_table_name, table_data={ 'person_id':array([1, 2, 3, 4, 5, 6]), 'household_id':array([1, 1, 2, 3, 3, 3]), 'member_id':array([1, 2, 1, 1, 2, 3]), 'work_nonhome_based':array([1, 0, 0, 1, 0, 1]), 'work_place_zone_id':array([71, -1, -1, 89, -1, -1]), }, ) persons = PersonDataset(in_storage=storage, in_table_name=persons_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, data_dictionary = { 'household':{ 'household_id':array([1, 2, 3, 4]) }, 'person':persons }, dataset = 'household' ) should_be = array([1, 0, 1, 0]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), 'Error in ' + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') persons_table_name = 'persons' storage.write_table( table_name=persons_table_name, table_data={ 'person_id':array([1, 2, 3, 4, 5, 6]), 'household_id':array([1, 1, 2, 3, 3, 3]), 'member_id':array([1, 2, 1, 1, 2, 3]), 'worker1':array([1, 0, 1, 0, 0, 1]), 'var_name_hbw_am_drive_alone_from_home_to_work':array([5.3,2, 999,0, 1.1,7]) }, ) persons = PersonDataset(in_storage=storage, in_table_name=persons_table_name) values = VariableTestToolbox().compute_variable(self.variable_name, \ data_dictionary = { 'person':persons, 'household':{ 'household_id':array([1, 2, 3]) } }, dataset = 'household' ) should_be = array([5.3, 999, 7]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), 'Error in ' + self.variable_name)
def test_with_all_zero_denominator(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id": array([1,2,3,4]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id":array([1,2,1,2,2]), "to_zone_id":array([1,2,2,3,4]), "am_single_vehicle_to_work_travel_time":array([1.1, 2.2, 3.3, 4.4, 5.5]), "am_pk_period_drive_alone_vehicle_trips":array([0.0, 0.0, 0.0, 0.0, 0.0]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([0.0, 0.0, 0.0, 0.0 ]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='test_agents', table_data={ 'id': array([1, 2, 3]), 'income': array([1, 20, 500]), }) storage.write_table(table_name='test_locations', table_data={ 'id': array([1, 2]), 'cost': array([1000, 2000]), }) dataset_pool = DatasetPool(package_order=['opus_core'], storage=storage) test_agent_x_test_location = dataset_pool.get_dataset( 'test_agent_x_test_location') values = test_agent_x_test_location.compute_variables( self.variable_name, dataset_pool=dataset_pool) should_be = array([[1000, 2000], [20000, 40000], [500000, 1000000]]) self.assert_(ma.allclose(values, should_be, rtol=1e-20), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='gridcells', table_data={ 'grid_id': array([1, 2, 3]), 'zone_id': array([1, 1, 3]), }) storage.write_table( table_name='zones', table_data={ 'zone_id': array([1, 2, 3]), "trip_weighted_average_time_hbw_to_work_am_bike": array([4.1, 5.3, 6.2]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) gridcell = dataset_pool.get_dataset('gridcell') gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = gridcell.get_attribute(self.variable_name) should_be = array([4.1, 4.1, 6.2]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
def create_landuse_developments_from_history( history_table, attribute_name='development_type_id'): """ """ id_name_default = 'landuse_development_id' history_values_starting = history_table.get_attribute('starting_' + attribute_name) history_values_ending = history_table.get_attribute('ending_' + attribute_name) development_index = where( history_values_starting <> history_values_ending)[0] #attributes = history_table.get_primary_attribute_names() attributes = ['scheduled_year', 'grid_id'] values = arange(development_index.size) storage = StorageFactory().get_storage('dict_storage') development_table_name = 'development' storage.write_table( table_name=development_table_name, table_data=transpose(values), ) development = LandUseDevelopmentDataset( in_storage=storage, in_table_name=development_table_name, names=[id_name_default]) for attr in attributes: development.add_attribute(history_table.get_attribute_by_index( attr, development_index), attr, metadata=AttributeType.PRIMARY) #load ending_development_type_id as development_type_id development.add_attribute(history_table.get_attribute_by_index( 'ending_development_type_id', development_index), attribute_name, metadata=AttributeType.PRIMARY) #project.add_submodel_categories() return development
def get_values(self, number): variable_name = "psrc.zone.employment_within_%s_minutes_travel_time_hbw_am_walk" % number storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id":array([1,3]), "number_of_jobs":array([10, 1]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id": array([3,3,1,1]), "to_zone_id": array([1,3,1,3]), "am_walk_time_in_minutes": array([1.1, 2.2, 3.3, 4.4]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(variable_name) return values
def test_with_zero_denominator(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'zone_id': array([1, 2, 3, 4]), }) storage.write_table(table_name='travel_data', table_data={ "from_zone_id": array([1, 2, 1, 2, 2]), "to_zone_id": array([1, 2, 2, 3, 4]), "single_vehicle_to_work_travel_cost": array([1.1, 2.2, 3.3, 4.4, 5.5]), "am_pk_period_drive_alone_vehicle_trips": array([10.1, 20.0, 30.0, 0.0, 0.0]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([ (1.1 * 10.1) / (10.1), (2.2 * 20.0 + 3.3 * 30) / (20.0 + 30.0), (2.2 * 20.0 + 3.3 * 30) / (20.0 + 30.0), # when denominator = 0, use prior good value (2.2 * 20.0 + 3.3 * 30) / (20.0 + 30.0) ]) # when denominator = 0, use prior good value self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'zone_id': array([1, 2]), }) storage.write_table(table_name='travel_data', table_data={ "from_zone_id": array([1, 1, 2, 2]), "to_zone_id": array([1, 2, 1, 2]), "single_vehicle_to_work_travel_cost": array([1.1, 2.2, 3.3, 4.4]), "am_pk_period_drive_alone_vehicle_trips": array([1.0, 2.0, 3.0, 4.0]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([(1.1 * 1.0 + 3.3 * 3.0) / (4.0), (2.2 * 2.0 + 4.4 * 4.0) / (6.0)]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def get_values(self, number): variable_name = "psrc.zone.travel_time_hbw_am_drive_alone_from_%d" % number storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='zones', table_data={ "zone_id":array([1,3]), } ) storage.write_table( table_name='travel_data', table_data={ "from_zone_id": array([3,3,1,1]), "to_zone_id": array([1,3,1,3]), "am_single_vehicle_to_work_travel_time": array([1.1, 2.2, 3.3, 4.4]), } ) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(variable_name) return values
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='gridcells', table_data={ 'grid_id': array([1, 2, 3, 4]), 'relative_x': array([1, 2, 1, 2]), 'relative_y': array([1, 1, 2, 2]), 'number_of_development_type_12_within_walking_distance': array([3, 5, 1, 0]) }) storage.write_table(table_name='urbansim_constants', table_data={ "walking_distance_circle_radius": array([150]), 'cell_size': array([150]), "acres": array([105.0]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) gridcell = dataset_pool.get_dataset('gridcell') gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = gridcell.get_attribute(self.variable_name) should_be = array([ 3 / 5.0 * 100.0, 5 / 5.0 * 100.0, 1 / 5.0 * 100.0, 0 / 5.0 * 100.0 ]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='parcels', table_data={ 'parcel_id': array([1, 2, 3, 4]), 'built_sf': array([50, 50, 75, 10]) }, ) storage.write_table( table_name='households', table_data={ 'household_id': array([1, 2, 3, 4, 5]), 'is_high_income': array([1, 0, 1, 0, 1]) }, ) dataset_pool = DatasetPool(package_order=['psrc', 'urbansim'], storage=storage) household_x_parcel = dataset_pool.get_dataset('household_x_parcel') household_x_parcel.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household_x_parcel.get_attribute(self.variable_name) should_be = array([[50, 50, 75, 10], [0, 0, 0, 0], [50, 50, 75, 10], [0, 0, 0, 0], [50, 50, 75, 10]]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
def test_aggregate_sum(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'my_variable': array([4, 8, 0.5, 1]), 'id': array([1, 2, 3, 4]), 'id2': array([1, 2, 1, 2]), }) storage.write_table(table_name='faz', table_data={"id2": array([1, 2])}) ds = Dataset(in_storage=storage, in_table_name='zones', id_name="id", dataset_name="myzone") ds2 = Dataset(in_storage=storage, in_table_name='faz', id_name="id2", dataset_name="myfaz") dataset_pool = DatasetPool() dataset_pool._add_dataset('myzone', ds) dataset_pool._add_dataset('myfaz', ds2) values = ds2.compute_variables( ['myfaz.aggregate(10.0*myzone.my_variable, function=sum)'], dataset_pool=dataset_pool) should_be = array([45, 90]) self.assert_(ma.allclose(values, should_be, rtol=1e-6), "Error in aggregate_sum")
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='gridcells', table_data={ 'grid_id': array([1, 2, 3]), 'households_without_children_within_walking_distance': array([5, 0, 42]), }) storage.write_table(table_name='households', table_data={ 'household_id': array([1, 2, 3, 4]), 'is_without_children': array([0, 1, 1, 0]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) household_x_gridcell = dataset_pool.get_dataset('household_x_gridcell') household_x_gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household_x_gridcell.get_attribute(self.variable_name) should_be = array([[0, 0, 0], [5, 0, 42], [5, 0, 42], [0, 0, 0]]) self.assert_(ma.allequal(values, should_be), msg="Error in " + self.variable_name)
def test_disaggregate_fully_qualified_variable(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'zone_id': array([1, 2, 3, 4]), 'id': array([1, 2, 1, 2]) }) # it would be nicer to call this table 'fazzes' but we want to use the existing test variable storage.write_table(table_name='test_locations', table_data={ 'cost': array([4, 8]), 'id': array([1, 2]) }) zone_dataset = Dataset(in_storage=storage, in_table_name='zones', id_name="zone_id", dataset_name="zone") test_dataset = Dataset(in_storage=storage, in_table_name='test_locations', id_name="id", dataset_name='test_location') dataset_pool = DatasetPool() dataset_pool._add_dataset('zone', zone_dataset) dataset_pool._add_dataset('test_location', test_dataset) values = zone_dataset.compute_variables( ['zone.disaggregate(10.0*opus_core.test_location.cost_times_3)'], dataset_pool=dataset_pool) should_be = array([120, 240, 120, 240]) self.assert_(ma.allclose(values, should_be, rtol=1e-6), "Error in test_disaggregate_fully_qualified_variable")
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='gridcells', table_data={ 'grid_id': array([1, 2, 3]), 'residential_avg_val_per_unit_within_walking_distance': array([50, 10, 20]), }) storage.write_table(table_name='households', table_data={ 'household_id': array([1, 2, 3, 4]), 'is_mid_income': array([1, 0, 1, 0]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) household_x_gridcell = dataset_pool.get_dataset('household_x_gridcell') household_x_gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household_x_gridcell.get_attribute(self.variable_name) should_be = array([[50, 10, 20], [0, 0, 0], [50, 10, 20], [0, 0, 0]]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_aggregate_all_mean(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'my_variable': array([4, 8, 10, 1]), 'id': array([1, 2, 3, 4]), }) storage.write_table(table_name='regions', table_data={ "id": array([1]), }) ds = Dataset(in_storage=storage, in_table_name='zones', id_name="id", dataset_name="myzone") ds2 = Dataset(in_storage=storage, in_table_name='regions', id_name="id", dataset_name="myregion") dataset_pool = DatasetPool() dataset_pool._add_dataset('myzone', ds) dataset_pool._add_dataset('myregion', ds2) ds2.compute_variables([ "myvar = myregion.aggregate_all(10.0*myzone.my_variable, function=mean)" ], dataset_pool=dataset_pool) values = ds2.get_attribute("myvar") should_be = array([57.5]) self.assert_(ma.allclose(values, should_be, rtol=1e-6), "Error in aggregate_all_mean")
def test_aggregate_fully_qualified_variable(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'zone_id': array([1, 2]), }) # it would be nicer to call this table 'gridcells' but we want to use the existing test variable storage.write_table(table_name='tests', table_data={ 'a_dependent_variable': array([4, 8, 0.5, 1]), 'id': array([1, 2, 3, 4]), 'zone_id': array([1, 2, 1, 2]), }) zone_dataset = Dataset(in_storage=storage, in_table_name='zones', id_name="zone_id", dataset_name='zone') test_dataset = Dataset(in_storage=storage, in_table_name='tests', id_name="id", dataset_name='tests') dataset_pool = DatasetPool() dataset_pool._add_dataset('zone', zone_dataset) dataset_pool._add_dataset('tests', test_dataset) values = zone_dataset.compute_variables( ['zone.aggregate(10.0*opus_core.tests.a_test_variable)'], dataset_pool=dataset_pool) should_be = array([450, 900]) self.assert_(ma.allclose(values, should_be, rtol=1e-6), "Error in test_aggregate_fully_qualified_variable")
def test_versioning_with_aggregate(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data={ 'my_variable': array([4, 8, 2, 1, 40, 23, 78]), 'id0': arange(7) + 1, 'id1': array([1, 3, 1, 2, 3, 2, 1]) }) storage.write_table(table_name='fazes', table_data={ 'id1': array([1, 2, 3]), 'id2': array([1, 2, 1]) }) storage.write_table(table_name='fazdistr', table_data={'id2': array([1, 2])}) ds0 = Dataset(in_storage=storage, in_table_name='households', id_name="id0", dataset_name="myhousehold") ds1 = Dataset(in_storage=storage, in_table_name='fazes', id_name="id1", dataset_name="myfaz") ds2 = Dataset(in_storage=storage, in_table_name='fazdistr', id_name="id2", dataset_name="myfazdistr") dataset_pool = DatasetPool() dataset_pool._add_dataset('myhousehold', ds0) dataset_pool._add_dataset('myfaz', ds1) dataset_pool._add_dataset('myfazdistr', ds2) ds0.modify_attribute("id1", array([1, 3, 1, 2, 3, 2, 1])) # has version 1 variable = 'my_var = myfazdistr.aggregate(10.0*myhousehold.my_variable, intermediates=[myfaz])' ds2.compute_variables([variable], dataset_pool=dataset_pool) self.assert_(ds2.get_version("my_var") == 0) ds2.compute_variables([variable], dataset_pool=dataset_pool) self.assert_( ds2.get_version("my_var") == 0) # version should stay the same, i.e. it should not recompute ds0.touch_attribute("id1") # has version 2 ds2.compute_variables([variable], dataset_pool=dataset_pool) self.assert_( ds2.get_version("my_var") == 1) # version should be 1, i.e. it should recompute when id changes ds1.touch_attribute("id2") # has version 1 ds2.compute_variables([variable], dataset_pool=dataset_pool) self.assert_( ds2.get_version("my_var") == 2) # version should be 2, i.e. it should recompute when id changes
def test_disaggregate(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'id': array([1, 2, 3, 4]), 'id2': array([1, 2, 1, 2]) }) storage.write_table(table_name='faz', table_data={ 'my_variable': array([4, 8]), 'id2': array([1, 2]) }) ds = Dataset(in_storage=storage, in_table_name='zones', id_name="id", dataset_name="myzone") ds2 = Dataset(in_storage=storage, in_table_name='faz', id_name="id2", dataset_name="myfaz") dataset_pool = DatasetPool() dataset_pool._add_dataset('myzone', ds) dataset_pool._add_dataset('myfaz', ds2) values = ds.compute_variables( ["myzone.disaggregate(10.0*myfaz.my_variable)"], dataset_pool=dataset_pool) should_be = array([40, 80, 40, 80]) self.assert_(ma.allclose(values, should_be, rtol=1e-6), "Error in disaggregate")
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='gridcells', table_data={ 'grid_id': array([1, 2, 3]), 'housing_cost': array([1000, 10000, 100000]), }) storage.write_table(table_name='households', table_data={ 'household_id': array([1, 2, 3]), 'income': array([1, 20, 500]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) household_x_gridcell = dataset_pool.get_dataset('household_x_gridcell') household_x_gridcell.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household_x_gridcell.get_attribute(self.variable_name) should_be = array([[-999, -9999, -99999], [-980, -9980, -99980], [-500, -9500, -99500]]) self.assert_(ma.allequal( values, should_be, ), msg="Error in " + self.variable_name)
def test_aggregate(self): # test aggregate with no function specified (so defaults to 'sum') storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'zone_id': array([1, 2]), }) storage.write_table(table_name='gridcells', table_data={ 'my_variable': array([4, 8, 0.5, 1]), 'grid_id': array([1, 2, 3, 4]), 'zone_id': array([1, 2, 1, 2]), }) zone_dataset = Dataset(in_storage=storage, in_table_name='zones', id_name="zone_id", dataset_name='zone') gridcell_dataset = Dataset(in_storage=storage, in_table_name='gridcells', id_name="grid_id", dataset_name='gridcell') dataset_pool = DatasetPool() dataset_pool._add_dataset('gridcell', gridcell_dataset) dataset_pool._add_dataset('zone', zone_dataset) values = zone_dataset.compute_variables( ['zone.aggregate(10.0*gridcell.my_variable)'], dataset_pool=dataset_pool) should_be = array([45, 90]) self.assert_(ma.allclose(values, should_be, rtol=1e-6), "Error in aggregate")
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='buildings', table_data={ 'building_id': array([1, 2, 3, 4, 5, 6]), 'year_built': array([1995, 2000, 2005, 0, 1800, 1799]), 'land_value': array([0, 10, 500, 20, 0, 9]), 'improvement_value': array([2, 0, 10, 10, 0, 70]) }) storage.write_table(table_name='urbansim_constants', table_data={ "absolute_min_year": array([1800]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) buildings = dataset_pool.get_dataset('building') buildings.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = buildings.get_attribute(self.variable_name) should_be = array([False, False, True, False, False, False]) self.assert_(ma.allequal(values, should_be), msg="Error in " + self.variable_name)
def test_my_input(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ "zone_id": array([1, 3]), "number_of_jobs": array([10, 1]), }) storage.write_table(table_name='travel_data', table_data={ "from_zone_id": array([3, 3, 1, 1]), "to_zone_id": array([1, 3, 1, 3]), "am_total_transit_time_walk": array([1, 2, 3, 4]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([1.17361, 10.25]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
def test_full_tree(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='parcels', table_data={ 'parcel_id': array([1, 2, 3, 4]), 'residential_units': array([1, 1, 3, 2]) }, ) storage.write_table( table_name='households', table_data={ 'household_id': array([1, 2, 3, 4, 5]), 'income': array([1000, 300000, 50000, 0, 10550]) }, ) dataset_pool = DatasetPool(package_order=['psrc', 'urbansim'], storage=storage) household_x_parcel = dataset_pool.get_dataset('household_x_parcel') household_x_parcel.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household_x_parcel.get_attribute(self.variable_name) should_be = array([[0, 0, 1000 * log(3), 1000 * log(2)], [0, 0, 300000 * log(3), 300000 * log(2)], [0, 0, 50000 * log(3), 50000 * log(2)], [0, 0, 0, 0], [0, 0, 10550 * log(3), 10550 * log(2)]]) self.assert_(ma.allclose(values, should_be, rtol=1e-3), msg="Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ 'zone_id': array([1, 2]), }) storage.write_table(table_name='travel_data', table_data={ "from_zone_id": array([1, 1, 2, 2]), "to_zone_id": array([1, 2, 1, 2]), "hbw_daily_share_ride3_person_trip_table": array([1.1, 2.2, 3.3, 4.4]), "hbnw_daily_share_ride3_person_trip_table": array([2.0, 3.0, 1.0, 0.0]), "nhb_daily_share_ride3_person_trip_table": array([12.8, 4.5, 1.2, 8.0]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) should_be = array([25.6, 17.9]) self.assert_(ma.allclose(values, should_be, rtol=1e-7), msg="Error in " + self.variable_name)
def test_interaction_set_aggregate_and_multiply_same_expr(self): # Test doing an aggregate on an interaction set component and using the result in a multiply operation # with the same expression. expr = "(test_agent.income+1)*test_location.aggregate(test_agent.income+1)" storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='test_agents', table_data={ 'id': array([1, 2, 3]), 'location_id': array([1, 2, 2]), 'income': array([1, 20, 50]) }) storage.write_table(table_name='test_locations', table_data={'location_id': array([1, 2])}) location_dataset = Dataset(in_storage=storage, in_table_name='test_locations', id_name="location_id", dataset_name="test_location") dataset_pool = DatasetPool(package_order=['opus_core'], storage=storage) dataset_pool._add_dataset('test_location', location_dataset) test_agent_x_test_location = dataset_pool.get_dataset( 'test_agent_x_test_location') result = test_agent_x_test_location.compute_variables( expr, dataset_pool=dataset_pool) # test_agent.income+1 is [2, 21, 51] # test_location.aggregate(test_agent.income+1) is [2, 72] should_be = array([[2 * 2, 2 * 72], [21 * 2, 21 * 72], [51 * 2, 51 * 72]]) self.assert_(ma.allclose(result, should_be, rtol=1e-6))
def test_input(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='fazes', table_data={ 'faz_id' :array([1,3,4]), } ) storage.write_table(table_name='households', table_data={ 'household_id' :array([1,2,3]), 'persons' :array([1,3,4]), } ) dc = DatasetFactory().get_dataset('faz_persons', package='psrc_parcel', arguments={'in_storage':storage} ) self.assertEqual(dc.get_id_name(), ['dummy_id']) self.assertTrue( "faz_id" in dc.get_known_attribute_names()) self.assertTrue( "persons" in dc.get_known_attribute_names()) self.assertEqual(dc.size(), 9) self.assertTrue(allclose(dc.get_id_attribute(), array([ 101, 103, 104, 301, 303, 304, 401, 403, 404]) ))
def test_multiply(self): expr = 'test_agent.income*test_location.cost' storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='test_agents', table_data={ 'id': array([1, 2, 3]), 'income': array([1, 20, 500]) }) storage.write_table(table_name='test_locations', table_data={ 'id': array([1, 2]), 'cost': array([1000, 2000]) }) dataset_pool = DatasetPool(package_order=['opus_core'], storage=storage) test_agent_x_test_location = dataset_pool.get_dataset( 'test_agent_x_test_location') result = test_agent_x_test_location.compute_variables( expr, dataset_pool=dataset_pool) should_be = array([[1000, 2000], [20000, 40000], [500000, 1000000]]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), msg="Error in " + expr) name = VariableName(expr) # since the expression involves both test_agent and test_location, the dataset name should be None # and the interaction set names should be (test_agent, test_location) or (test_location, test_agent) self.assertEqual(name.get_dataset_name(), None) names = name.get_interaction_set_names() self.assertEqual(len(names), 2) self.assert_('test_agent' in names) self.assert_('test_location' in names)
def get_values(self, sector, threshold): self.variable_name = "urbansim_parcel.zone.sector_%s_employment_within_%s_minutes_travel_time_hbw_am_transit_walk" % ( sector, threshold) storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='zones', table_data={ "zone_id": array([1, 3]), "number_of_jobs_of_sector_2": array([10, 1]), "number_of_jobs_of_sector_3": array([7, 2]), }) storage.write_table(table_name='travel_data', table_data={ "from_zone_id": array([3, 3, 1, 1]), "to_zone_id": array([1, 3, 1, 3]), "am_total_transit_time_walk": array([1.1, 2.2, 3.3, 4.4]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) zone = dataset_pool.get_dataset('zone') zone.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = zone.get_attribute(self.variable_name) return values
def test_interaction_set_component_expression_alias(self): expr = "squid = 3+opus_core.test_agent.income_times_2" storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='test_agents', table_data={ 'id': array([1, 2, 3]), 'income': array([1, 20, 500]) }) storage.write_table(table_name='test_locations', table_data={ 'id': array([1, 2]), 'cost': array([1000, 2000]) }) dataset_pool = DatasetPool(package_order=['opus_core'], storage=storage) test_agent_x_test_location = dataset_pool.get_dataset( 'test_agent_x_test_location') result = test_agent_x_test_location.compute_variables( expr, dataset_pool=dataset_pool) should_be = array([[5, 5], [43, 43], [1003, 1003]]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), msg="Error in " + expr) # test that the interaction set now has this as an attribute result2 = test_agent_x_test_location.get_attribute('squid') self.assert_(ma.allclose(result2, should_be, rtol=1e-6), msg="Error in " + expr) # test that the value can now also be accessed using the alias result3 = test_agent_x_test_location.compute_variables(['squid']) self.assert_(ma.allclose(result3, should_be, rtol=1e-6), msg="Error in " + expr)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data={ 'household_id': array([1, 2, 3, 4]), 'income': array([50, 100, 200, 300]), }) storage.write_table(table_name='urbansim_constants', table_data={ "low_income_fraction": array([.25]), 'mid_income_fraction': array([.25]), }) dataset_pool = DatasetPool(package_order=['urbansim'], storage=storage) household = dataset_pool.get_dataset('household') household.compute_variables(self.variable_name, dataset_pool=dataset_pool) values = household.get_attribute(self.variable_name) should_be = array([1, 1, 2, 3]) self.assert_(ma.allequal( values, should_be, ), msg="Error in " + self.variable_name)