def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') job_building_types_table_name = 'job_building_types' storage.write_table( table_name=job_building_types_table_name, table_data={ 'id':array([1,2,3,4]), 'home_based': array([1, 0, 1, 0]) } ) jobs_table_name = 'jobs' storage.write_table( table_name=jobs_table_name, table_data={ 'job_id':arange(10)+1, 'building_type': array([3,3,2,2,4,2,1,3,4,1]) } ) job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name=job_building_types_table_name) jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name) jobs.compute_variables(self.variable_name, resources=Resources({'job_building_type': job_building_types})) values = jobs.get_attribute(self.variable_name) should_be = array([0,0,1,1,1,1,0,0,1,0]) self.assert_(ma.allequal(values, should_be), 'Error in ' + self.variable_name)
def run_model2(): storage = StorageFactory().get_storage('dict_storage') jobs_set_table_name = 'jobs_set' storage.write_table( table_name = jobs_set_table_name, table_data = self.jobs_data, ) jobs_set = JobDataset(in_storage=storage, in_table_name=jobs_set_table_name) model = RegionalEmploymentTransitionModel() model.run(year=2000, job_set=jobs_set, control_totals=ect_set, job_building_types=self.job_building_types) # check that the distribution of building type is the same before and after subtracting jobs jobs_set.compute_variables(["urbansim.job.is_in_employment_sector_1_industrial", "urbansim.job.is_in_employment_sector_2_industrial", "urbansim.job.is_in_employment_sector_1_commercial", "urbansim.job.is_in_employment_sector_2_commercial", "urbansim.job.is_in_employment_sector_1_governmental", "urbansim.job.is_in_employment_sector_2_governmental"], resources = Resources({"job_building_type":self.job_building_types})) result = array([jobs_set.get_attribute("is_in_employment_sector_1_industrial").sum(), jobs_set.get_attribute("is_in_employment_sector_2_industrial").sum(), jobs_set.get_attribute("is_in_employment_sector_1_commercial").sum(), jobs_set.get_attribute("is_in_employment_sector_2_commercial").sum(), jobs_set.get_attribute("is_in_employment_sector_1_governmental").sum(), jobs_set.get_attribute("is_in_employment_sector_2_governmental").sum() ]) return result
def test_my_inputs(self): storage = StorageFactory().get_storage("dict_storage") job_building_types_table_name = "job_building_types" storage.write_table( table_name=job_building_types_table_name, table_data={"id": array([1, 2, 3, 4]), "home_based": array([0, 1, 0, 1])}, ) jobs_table_name = "jobs" storage.write_table( table_name=jobs_table_name, table_data={"job_id": arange(10) + 1, "building_type": array([3, 3, 2, 2, 4, 2, 1, 3, 4, 1])}, ) job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name=job_building_types_table_name) jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name) jobs.compute_variables(self.variable_name, resources=Resources({"job_building_type": job_building_types})) values = jobs.get_attribute(self.variable_name) should_be = array([0, 0, 1, 1, 1, 1, 0, 0, 1, 0], dtype="bool8") self.assert_(ma.allequal(values, should_be), "Error in " + self.variable_name)
def test_my_inputs(self): storage = StorageFactory().get_storage('dict_storage') job_building_types_table_name = 'job_building_types' storage.write_table(table_name=job_building_types_table_name, table_data={ 'id': array([1, 2, 3, 4]), 'home_based': array([1, 0, 1, 0]) }) jobs_table_name = 'jobs' storage.write_table(table_name=jobs_table_name, table_data={ 'job_id': arange(10) + 1, 'building_type': array([3, 3, 2, 2, 4, 2, 1, 3, 4, 1]) }) job_building_types = JobBuildingTypeDataset( in_storage=storage, in_table_name=job_building_types_table_name) jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name) jobs.compute_variables(self.variable_name, resources=Resources( {'job_building_type': job_building_types})) values = jobs.get_attribute(self.variable_name) should_be = array([0, 0, 1, 1, 1, 1, 0, 0, 1, 0]) self.assert_(ma.allequal(values, should_be), 'Error in ' + self.variable_name)
def run_model2(): storage = StorageFactory().get_storage('dict_storage') jobs_set_table_name = 'jobs_set' storage.write_table( table_name=jobs_set_table_name, table_data=self.jobs_data, ) jobs_set = JobDataset(in_storage=storage, in_table_name=jobs_set_table_name) model = RegionalEmploymentTransitionModel() model.run(year=2000, job_set=jobs_set, control_totals=ect_set, job_building_types=self.job_building_types) # check that the distribution of building type is the same before and after subtracting jobs jobs_set.compute_variables([ "urbansim.job.is_in_employment_sector_1_industrial", "urbansim.job.is_in_employment_sector_2_industrial", "urbansim.job.is_in_employment_sector_1_commercial", "urbansim.job.is_in_employment_sector_2_commercial", "urbansim.job.is_in_employment_sector_1_governmental", "urbansim.job.is_in_employment_sector_2_governmental" ], resources=Resources({ "job_building_type": self.job_building_types })) result = array([ jobs_set.get_attribute( "is_in_employment_sector_1_industrial").sum(), jobs_set.get_attribute( "is_in_employment_sector_2_industrial").sum(), jobs_set.get_attribute( "is_in_employment_sector_1_commercial").sum(), jobs_set.get_attribute( "is_in_employment_sector_2_commercial").sum(), jobs_set.get_attribute( "is_in_employment_sector_1_governmental").sum(), jobs_set.get_attribute( "is_in_employment_sector_2_governmental").sum() ]) return result
def test_erm_correct_distribution_of_jobs_relocate(self): # In addition to unplaced jobs choose 50% of jobs of sector 2 to relocate and # no job of sector 1. # gridcell has expected # 1 100 sector 1 jobs 100 sector 1 jobs # 400 sector 2 jobs about 200 sector 2 jobs # 2 100 sector 1 jobs 100 sector 1 jobs # 200 sector 2 jobs about 100 sector 2 jobs # 3 100 sector 1 jobs 100 sector 1 jobs # 100 sector 2 jobs about 50 sector 2 jobs # unplaced 10 sector 1 jobs # 10 sector 2 jobs storage = StorageFactory().get_storage("dict_storage") # create jobs job_grid_ids = array(100 * [1] + 100 * [2] + 100 * [3] + 400 * [1] + 200 * [2] + 100 * [3] + 20 * [-1]) storage.write_table( table_name="jobs", table_data={ "job_id": arange(1020) + 1, "sector_id": array(300 * [1] + 700 * [2] + 10 * [1] + 10 * [2]), "grid_id": job_grid_ids, }, ) jobs = JobDataset(in_storage=storage, in_table_name="jobs") # create gridcells storage.write_table(table_name="gridcells", table_data={"grid_id": arange(3) + 1}) gridcells = GridcellDataset(in_storage=storage, in_table_name="gridcells") # create rate set with rate 0 for jobs of sector 1 and 0.5 for jobs of sector 2 storage.write_table( table_name="rates", table_data={"sector_id": array([1, 2]), "job_relocation_probability": array([0, 0.5])} ) rates = JobRelocationRateDataset(in_storage=storage, in_table_name="rates") # run model model = EmploymentRelocationModelCreator().get_model(debuglevel=0) hrm_resources = Resources({"annual_job_relocation_rate": rates}) # get results from one run movers_indices = model.run(jobs, resources=hrm_resources) jobs.compute_variables(["urbansim.job.is_in_employment_sector_1"]) # unplace chosen jobs compute_resources = Resources({"job": jobs, "urbansim_constant": {"industrial_code": 1, "commercial_code": 2}}) jobs.set_values_of_one_attribute(attribute="grid_id", values=-1, index=movers_indices) gridcells.compute_variables( ["urbansim.gridcell.number_of_jobs_of_sector_1", "urbansim.gridcell.number_of_jobs_of_sector_2"], resources=compute_resources, ) # only 100 jobs of sector 1 (unplaced jobs) should be selected result1 = jobs.get_attribute_by_index("is_in_employment_sector_1", movers_indices).astype(int8).sum() self.assertEqual(result1 == 10, True) # number of sector 1 jobs should not change result2 = gridcells.get_attribute("number_of_jobs_of_sector_1") self.assertEqual(ma.allclose(result2, array([100, 100, 100]), rtol=0), True) def run_model(): jobs.modify_attribute(name="grid_id", data=job_grid_ids) indices = model.run(jobs, resources=hrm_resources) jobs.modify_attribute(name="grid_id", data=-1, index=indices) gridcells.compute_variables(["urbansim.gridcell.number_of_jobs_of_sector_2"], resources=compute_resources) return gridcells.get_attribute("number_of_jobs_of_sector_2") # distribution of sector 2 jobs should be the same, the mean are halfs of the original values should_be = array([200, 100, 50]) self.run_stochastic_test(__file__, run_model, should_be, 10)