def test_distribute_unplaced_jobs_model(self): # Places 1750 jobs of sector 15 # gridcell has expected about # 1 4000 sector 15 jobs 5000 sector 15 jobs # 1000 sector 1 jobs 1000 sector 1 jobs # 2 2000 sector 15 jobs 2500 sector 15 jobs # 1000 sector 1 jobs 1000 sector 1 jobs # 3 1000 sector 15 jobs 1250 sector 15 jobs # 1000 sector 1 jobs 1000 sector 1 jobs # unplaced 1750 sector 15 jobs 0 # create jobs storage = StorageFactory().get_storage("dict_storage") job_data = { "job_id": arange(11750) + 1, "sector_id": array(7000 * [15] + 3000 * [1] + 1750 * [15]), "grid_id": array(4000 * [1] + 2000 * [2] + 1000 * [3] + 1000 * [1] + 1000 * [2] + 1000 * [3] + 1750 * [-1]), } jobs_table_name = "jobs" storage.write_table(table_name=jobs_table_name, table_data=job_data) jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name) storage = StorageFactory().get_storage("dict_storage") building_types_table_name = "building_types" storage.write_table(table_name=building_types_table_name, table_data={"grid_id": arange(3) + 1}) gridcells = GridcellDataset(in_storage=storage, in_table_name=building_types_table_name) # run model model = DistributeUnplacedJobsModel(debuglevel=4) model.run(gridcells, jobs) # get results # no jobs are unplaced result1 = where(jobs.get_attribute("grid_id") < 0)[0] self.assertEqual(result1.size, 0) # the first 10000jobs kept their locations result2 = jobs.get_attribute_by_index("grid_id", arange(10000)) # logger.log_status(result2) self.assertEqual(ma.allclose(result2, job_data["grid_id"][0:10000], rtol=0), True) # run model with filter # unplace first 500 jobs of sector 15 jobs.modify_attribute(name="grid_id", data=zeros(500), index=arange(500)) # unplace first 500 jobs of sector 1 jobs.modify_attribute(name="grid_id", data=zeros(500), index=arange(7000, 7501)) # place only unplaced jobs of sector 1 model.run(gridcells, jobs, agents_filter="job.sector_id == 1") # 500 jobs of sector 15 should be unplaced result3 = where(jobs.get_attribute("grid_id") <= 0)[0] self.assertEqual(result3.size, 500) # jobs of sector 1 are placed result4 = jobs.get_attribute_by_index("grid_id", arange(7000, 7501)) self.assertEqual((result4 <= 0).sum(), 0)
def test_run_model_with_known_buildings(self): storage = self.storage storage.write_table( table_name = 'buildings', table_data = { 'building_id': array([1,2,3,4,5,6,7]), 'parcel_id': array([1,1,2,2,2,3,3]), 'is_residential': array([0,0,0,1,1,0,0]) } ) storage.write_table( table_name = 'employment_events', table_data = { 'parcel_id': array([2, 2, -1, -1, 1]), 'building_id': array([-1, -1, 6, 7, -1]), 'scheduled_year': array([2006, 2006, 2006, 2006, 2006]), 'number_of_non_home_based_jobs': array([3500, 500, -100, 0, 100]), 'number_of_home_based_jobs': array([0, 20, 0, 10, 0]), 'sector_id': array([1, 2, 15, 2, 1]), 'replace_non_home_based_jobs': array([0, 0, 0, 1, 0]) } ) # change in 2006 ############ # parcel/sector 1 2 3 # 1 +100nhb +3500nhb -- # 2 =0nhb +500nhb/+20hb +10hb # 15 -- -- -100nhb dataset_pool = DatasetPool(storage=storage, package_order=['urbansim_parcel', 'urbansim']) job_set = JobDataset(in_storage=storage) job_set.modify_attribute('building_id', array(6000*[1] + 4000*[3] + 3000*[6])) dataset_pool.add_datasets_if_not_included({'job':job_set}) model = EmploymentEventsModel(dataset_pool=dataset_pool) model.run(dataset_pool.get_dataset('employment_event'), job_set, current_year=2006) buildings = dataset_pool.get_dataset('building') jobs_in_sec_1 = buildings.compute_variables(['urbansim_parcel.building.number_of_jobs_of_sector_1'], dataset_pool=dataset_pool) jobs_in_sec_2 = buildings.compute_variables(['urbansim_parcel.building.number_of_jobs_of_sector_2'], dataset_pool=dataset_pool) jobs_in_sec_15 = buildings.compute_variables(['urbansim_parcel.building.number_of_jobs_of_sector_15'], dataset_pool=dataset_pool) self.assertEqual(jobs_in_sec_1[0:2].sum()==4100, True) # parcel 1 self.assertEqual(jobs_in_sec_1[2] == 5500, True) # parcel 2 non-residential self.assertEqual(jobs_in_sec_1[5:7].sum()==1000, True) # parcel 3 self.assertEqual(jobs_in_sec_2[0:2].sum()==1000, True) # parcel 1 self.assertEqual(jobs_in_sec_2[2] == 1500, True) # parcel 2 non-residential self.assertEqual(jobs_in_sec_2[3:5].sum() == 20, True) # parcel 2 residential self.assertEqual(jobs_in_sec_2[5]==100, True) # parcel 3, building 6 self.assertEqual(jobs_in_sec_2[6]==10, True) # parcel 3, building 7 self.assertEqual(jobs_in_sec_15[0:2].sum()==1000, True) # parcel 2 non-residential self.assertEqual(jobs_in_sec_15[2] == 1000, True) # parcel 2 residential self.assertEqual(jobs_in_sec_15[5]==900, True) # parcel 3, building 6
def test_same_distribution_after_job_subtraction(self): """Removes 1,750 sector_1 jobs, without specifying the distribution across gridcells (so it is assumed equal) Test that the distribution (in %) of sector 1 jobs across gridcells before and after the subtraction are relatively equal. """ 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) ect_set_table_name = 'ect_set' storage.write_table(table_name=ect_set_table_name, table_data=self.annual_employment_control_totals_data) ect_set = ControlTotalDataset(in_storage=storage, in_table_name=ect_set_table_name, what="employment") # unplace some jobs jobs_set.modify_attribute(name="grid_id", data=zeros(int(jobs_set.size()/2)), index=arange(int(jobs_set.size()/2))) #run model with input Datasets model = EmploymentTransitionModel() model.run(year=2000, job_set=jobs_set, control_totals=ect_set, job_building_types=self.job_building_types) results = jobs_set.size() should_be = [11250] self.assertEqual(ma.allequal(should_be, results), True, "Error") def run_model(): 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 = EmploymentTransitionModel() model.run(year=2000, job_set=jobs_set, control_totals=ect_set, job_building_types=self.job_building_types) # check that the distribution of jobs is the same before and after subtracting jobs results = self.get_count_all_sectors_and_gridcells(jobs_set) return results expected_results = array([4000.0/7000.0*5250.0, 1000, 1000, 2000.0/7000.0*5250.0, 1000, 1000, 1000.0/7000.0*5250.0, 1000, 1000]) self.run_stochastic_test(__file__, run_model, expected_results, 10) 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 = EmploymentTransitionModel() 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 expected_results = array([3500.0/7000.0*5250.0, 900, 3500.0/7000.0*5250.0, 1800, 0, 300]) self.run_stochastic_test(__file__, run_model2, expected_results, 20)
def test_distribute_unplaced_jobs_model(self): # Places 1750 jobs of sector 15 # gridcell has expected about # 1 4000 sector 15 jobs 5000 sector 15 jobs # 1000 sector 1 jobs 1000 sector 1 jobs # 2 2000 sector 15 jobs 2500 sector 15 jobs # 1000 sector 1 jobs 1000 sector 1 jobs # 3 1000 sector 15 jobs 1250 sector 15 jobs # 1000 sector 1 jobs 1000 sector 1 jobs # unplaced 1750 sector 15 jobs 0 # create jobs storage = StorageFactory().get_storage('dict_storage') job_data = { "job_id": arange(11750)+1, "sector_id": array(7000*[15]+3000*[1]+1750*[15]), "grid_id":array(4000*[1]+2000*[2]+1000*[3]+1000*[1]+1000*[2]+1000*[3]+1750*[-1]) } jobs_table_name = 'jobs' storage.write_table(table_name=jobs_table_name, table_data=job_data) jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name) storage = StorageFactory().get_storage('dict_storage') building_types_table_name = 'building_types' storage.write_table( table_name=building_types_table_name, table_data={ "grid_id":arange(3)+1 } ) gridcells = GridcellDataset(in_storage=storage, in_table_name=building_types_table_name) # run model model = DistributeUnplacedJobsModel(debuglevel=4) model.run(gridcells, jobs) # get results # no jobs are unplaced result1 = where(jobs.get_attribute("grid_id")<0)[0] self.assertEqual(result1.size, 0) # the first 10000jobs kept their locations result2 = jobs.get_attribute_by_index("grid_id", arange(10000)) # logger.log_status(result2) self.assertEqual(ma.allclose(result2, job_data["grid_id"][0:10000], rtol=0), True) # run model with filter # unplace first 500 jobs of sector 15 jobs.modify_attribute(name='grid_id', data=zeros(500), index=arange(500)) # unplace first 500 jobs of sector 1 jobs.modify_attribute(name='grid_id', data=zeros(500), index=arange(7000, 7501)) # place only unplaced jobs of sector 1 model.run(gridcells, jobs, agents_filter='job.sector_id == 1') # 500 jobs of sector 15 should be unplaced result3 = where(jobs.get_attribute("grid_id")<=0)[0] self.assertEqual(result3.size, 500) # jobs of sector 1 are placed result4 = jobs.get_attribute_by_index("grid_id", arange(7000, 7501)) self.assertEqual((result4 <= 0).sum(), 0)
def test_run_model_with_known_buildings(self): storage = self.storage storage.write_table(table_name='buildings', table_data={ 'building_id': array([1, 2, 3, 4, 5, 6, 7]), 'parcel_id': array([1, 1, 2, 2, 2, 3, 3]), 'is_residential': array([0, 0, 0, 1, 1, 0, 0]) }) storage.write_table(table_name='employment_events', table_data={ 'parcel_id': array([2, 2, -1, -1, 1]), 'building_id': array([-1, -1, 6, 7, -1]), 'scheduled_year': array([2006, 2006, 2006, 2006, 2006]), 'number_of_non_home_based_jobs': array([3500, 500, -100, 0, 100]), 'number_of_home_based_jobs': array([0, 20, 0, 10, 0]), 'sector_id': array([1, 2, 15, 2, 1]), 'replace_non_home_based_jobs': array([0, 0, 0, 1, 0]) }) # change in 2006 ############ # parcel/sector 1 2 3 # 1 +100nhb +3500nhb -- # 2 =0nhb +500nhb/+20hb +10hb # 15 -- -- -100nhb dataset_pool = DatasetPool( storage=storage, package_order=['urbansim_parcel', 'urbansim']) job_set = JobDataset(in_storage=storage) job_set.modify_attribute('building_id', array(6000 * [1] + 4000 * [3] + 3000 * [6])) dataset_pool.add_datasets_if_not_included({'job': job_set}) model = EmploymentEventsModel(dataset_pool=dataset_pool) model.run(dataset_pool.get_dataset('employment_event'), job_set, current_year=2006) buildings = dataset_pool.get_dataset('building') jobs_in_sec_1 = buildings.compute_variables( ['urbansim_parcel.building.number_of_jobs_of_sector_1'], dataset_pool=dataset_pool) jobs_in_sec_2 = buildings.compute_variables( ['urbansim_parcel.building.number_of_jobs_of_sector_2'], dataset_pool=dataset_pool) jobs_in_sec_15 = buildings.compute_variables( ['urbansim_parcel.building.number_of_jobs_of_sector_15'], dataset_pool=dataset_pool) self.assertEqual(jobs_in_sec_1[0:2].sum() == 4100, True) # parcel 1 self.assertEqual(jobs_in_sec_1[2] == 5500, True) # parcel 2 non-residential self.assertEqual(jobs_in_sec_1[5:7].sum() == 1000, True) # parcel 3 self.assertEqual(jobs_in_sec_2[0:2].sum() == 1000, True) # parcel 1 self.assertEqual(jobs_in_sec_2[2] == 1500, True) # parcel 2 non-residential self.assertEqual(jobs_in_sec_2[3:5].sum() == 20, True) # parcel 2 residential self.assertEqual(jobs_in_sec_2[5] == 100, True) # parcel 3, building 6 self.assertEqual(jobs_in_sec_2[6] == 10, True) # parcel 3, building 7 self.assertEqual(jobs_in_sec_15[0:2].sum() == 1000, True) # parcel 2 non-residential self.assertEqual(jobs_in_sec_15[2] == 1000, True) # parcel 2 residential self.assertEqual(jobs_in_sec_15[5] == 900, True) # parcel 3, building 6
def test_same_distribution_after_job_subtraction(self): """Removes 1,750 sector_1 jobs, without specifying the distribution across gridcells (so it is assumed equal) Test that the distribution (in %) of sector 1 jobs across gridcells before and after the subtraction are relatively equal. """ 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) ect_set_table_name = 'ect_set' storage.write_table( table_name=ect_set_table_name, table_data=self.annual_employment_control_totals_data) ect_set = ControlTotalDataset(in_storage=storage, in_table_name=ect_set_table_name, what="employment") # unplace some jobs jobs_set.modify_attribute(name="grid_id", data=zeros(int(jobs_set.size() / 2)), index=arange(int(jobs_set.size() / 2))) #run model with input Datasets model = EmploymentTransitionModel() model.run(year=2000, job_set=jobs_set, control_totals=ect_set, job_building_types=self.job_building_types) results = jobs_set.size() should_be = [11250] self.assertEqual(ma.allequal(should_be, results), True, "Error") def run_model(): 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 = EmploymentTransitionModel() model.run(year=2000, job_set=jobs_set, control_totals=ect_set, job_building_types=self.job_building_types) # check that the distribution of jobs is the same before and after subtracting jobs results = self.get_count_all_sectors_and_gridcells(jobs_set) return results expected_results = array([ 4000.0 / 7000.0 * 5250.0, 1000, 1000, 2000.0 / 7000.0 * 5250.0, 1000, 1000, 1000.0 / 7000.0 * 5250.0, 1000, 1000 ]) self.run_stochastic_test(__file__, run_model, expected_results, 10) 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 = EmploymentTransitionModel() 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 expected_results = array([ 3500.0 / 7000.0 * 5250.0, 900, 3500.0 / 7000.0 * 5250.0, 1800, 0, 300 ]) self.run_stochastic_test(__file__, run_model2, expected_results, 20)