def _get_data(self, year, dataset_name, attribute_name): current_year = SimulationState().get_current_time() SimulationState().set_current_time(year) dataset = DatasetFactory().get_dataset(dataset_name, package='urbansim', subdir='datasets', arguments={'in_storage':AttributeCache()}) dataset.compute_variables(attribute_name, resources=self.simulation.config) variable_name = VariableName(attribute_name) short_name = variable_name.get_short_name() values = dataset.get_attribute(short_name) SimulationState().set_current_time(current_year) return values
def _get_data(self, year, dataset_name, attribute_name): current_year = SimulationState().get_current_time() SimulationState().set_current_time(year) dataset = DatasetFactory().get_dataset( dataset_name, package='urbansim', subdir='datasets', arguments={'in_storage': AttributeCache()}) dataset.compute_variables(attribute_name, resources=self.simulation.config) variable_name = VariableName(attribute_name) short_name = variable_name.get_short_name() values = dataset.get_attribute(short_name) SimulationState().set_current_time(current_year) return values
raise StandardError, "cache directory argument is required (-c or --cache-directory)" if (options.start_year is None) and (options.refinements_directory is None): parser.print_usage() raise StandardError, "Either start year (argument -s or --start-year) or refinements directory (argument --refinements-directory) must be given." start_year = options.start_year end_year = options.end_year package_order = [ package.strip() for package in options.package_order.split(",") ] refinements = None refinements_storage = None if options.refinements_directory is not None: refinements_storage = StorageFactory().get_storage('flt_storage', storage_location=options.refinements_directory) refinements = DatasetFactory().search_for_dataset('refinement', package_order, arguments={'in_storage':refinements_storage}) years = refinements.get_attribute('year') if start_year is None: start_year = years.min() if end_year is None: end_year = years.max() simulation_state = SimulationState() simulation_state.set_cache_directory(options.cache_directory) simulation_state.set_current_time(start_year) attribute_cache = AttributeCache() dataset_pool = SessionConfiguration(new_instance=True, package_order=package_order, in_storage=attribute_cache).get_dataset_pool() if refinements is None: refinements = dataset_pool.get_dataset('refinement') years = refinements.get_attribute('year') if start_year is None: start_year = years.min()
class TestLagVariables(opus_unittest.OpusTestCase): def setUp(self): self.config = TestCacheConfiguration() self.simulation_state = SimulationState(new_instance=True) SessionConfiguration(self.config, new_instance=True, package_order=['urbansim', 'opus_core'], in_storage=AttributeCache()) self.base_year = self.config['base_year'] creating_baseyear_cache_configuration = self.config['creating_baseyear_cache_configuration'] self.simulation_state.set_current_time(self.base_year) cache_directory = self.simulation_state.get_cache_directory() copytree(os.path.join(creating_baseyear_cache_configuration.baseyear_cache.existing_cache_to_copy, str(self.base_year)), os.path.join(cache_directory, str(self.base_year))) cacher = CacheScenarioDatabase() cacher.prepare_data_before_baseyear(cache_directory, self.base_year, creating_baseyear_cache_configuration) self.config['cache_directory'] = cache_directory cache_storage = AttributeCache().get_flt_storage_for_year(self.base_year) cache_directory = self.simulation_state.get_cache_directory() flt_directory = os.path.join(cache_directory, str(self.base_year)) self.gridcell = DatasetFactory().get_dataset('gridcell', package='urbansim', subdir='datasets', arguments={'in_storage':StorageFactory().get_storage('flt_storage', storage_location=flt_directory)} ) def tearDown(self): self.simulation_state.remove_singleton(delete_cache=True) def test_lag_variables(self): """Test lag variables""" # A weak test that computing a lag variable on a realistic dataset does not crash. self.gridcell.compute_variables('urbansim.gridcell.n_recent_transitions_to_developed', resources=self.config) # The following tests are fragile, since they need to know exactly what values are being # subtracted, and ignore any negative amount that is truncated at zero. # If you change the "subset" dataset to a different region, you will # have to update the expected value. self.gridcell.compute_variables('urbansim.gridcell.commercial_sqft', resources=self.config) self.gridcell.compute_variables('urbansim.gridcell.commercial_sqft_lag1', resources=self.config) self.gridcell.compute_variables('urbansim.gridcell.commercial_sqft_lag2', resources=self.config) sqft = self.gridcell.get_attribute('commercial_sqft').sum() sqft_lag1 = self.gridcell.get_attribute('commercial_sqft_lag1').sum() sqft_lag2 = self.gridcell.get_attribute('commercial_sqft_lag2').sum() logger.log_status('sqft = %s' % sqft) logger.log_status('sqft_lag1 = %s' % sqft_lag1) logger.log_status('sqft_lag2 = %s' % sqft_lag2) logger.log_status('base_year = %s' % self.base_year) self.assertEqual(self.base_year, SimulationState().get_current_time()) self.assertEqual(sqft, sqft_lag1) self.assertEqual(578+2083+1103+87, sqft_lag1 - sqft_lag2) # Do lag variables produce different results for derived attributes? self.gridcell.compute_variables('urbansim.gridcell.n_recent_development_projects', resources=self.config) self.gridcell.compute_variables('urbansim.gridcell.n_recent_development_projects_lag1', resources=self.config) n_recent_projects = self.gridcell.get_attribute('n_recent_development_projects').sum() n_recent_projects_lag1 = self.gridcell.get_attribute('n_recent_development_projects_lag1').sum() self.assertEqual(n_recent_projects, 11) self.assertEqual(n_recent_projects_lag1, 15) # Do lag_variables produce different results for derived attributes without lags? self.gridcell.compute_variables('urbansim.gridcell.ln_commercial_sqft', resources=self.config) self.gridcell.compute_variables('urbansim.gridcell.ln_commercial_sqft_lag4', resources=self.config) sqft = self.gridcell.get_attribute('ln_commercial_sqft').sum() sqft_lag4 = self.gridcell.get_attribute('ln_commercial_sqft_lag4').sum() self.assertNotEqual(sqft, sqft_lag4)
class TestLagVariables(opus_unittest.OpusTestCase): def setUp(self): self.config = TestCacheConfiguration() self.simulation_state = SimulationState(new_instance=True) SessionConfiguration(self.config, new_instance=True, package_order=['urbansim', 'opus_core'], in_storage=AttributeCache()) self.base_year = self.config['base_year'] creating_baseyear_cache_configuration = self.config[ 'creating_baseyear_cache_configuration'] self.simulation_state.set_current_time(self.base_year) cache_directory = self.simulation_state.get_cache_directory() copytree( os.path.join( creating_baseyear_cache_configuration.baseyear_cache. existing_cache_to_copy, str(self.base_year)), os.path.join(cache_directory, str(self.base_year))) cacher = CacheScenarioDatabase() cacher.prepare_data_before_baseyear( cache_directory, self.base_year, creating_baseyear_cache_configuration) self.config['cache_directory'] = cache_directory cache_storage = AttributeCache().get_flt_storage_for_year( self.base_year) cache_directory = self.simulation_state.get_cache_directory() flt_directory = os.path.join(cache_directory, str(self.base_year)) self.gridcell = DatasetFactory().get_dataset( 'gridcell', package='urbansim', subdir='datasets', arguments={ 'in_storage': StorageFactory().get_storage('flt_storage', storage_location=flt_directory) }) def tearDown(self): self.simulation_state.remove_singleton(delete_cache=True) def test_lag_variables(self): """Test lag variables""" # A weak test that computing a lag variable on a realistic dataset does not crash. self.gridcell.compute_variables( 'urbansim.gridcell.n_recent_transitions_to_developed', resources=self.config) # The following tests are fragile, since they need to know exactly what values are being # subtracted, and ignore any negative amount that is truncated at zero. # If you change the "subset" dataset to a different region, you will # have to update the expected value. self.gridcell.compute_variables('urbansim.gridcell.commercial_sqft', resources=self.config) self.gridcell.compute_variables( 'urbansim.gridcell.commercial_sqft_lag1', resources=self.config) self.gridcell.compute_variables( 'urbansim.gridcell.commercial_sqft_lag2', resources=self.config) sqft = self.gridcell.get_attribute('commercial_sqft').sum() sqft_lag1 = self.gridcell.get_attribute('commercial_sqft_lag1').sum() sqft_lag2 = self.gridcell.get_attribute('commercial_sqft_lag2').sum() logger.log_status('sqft = %s' % sqft) logger.log_status('sqft_lag1 = %s' % sqft_lag1) logger.log_status('sqft_lag2 = %s' % sqft_lag2) logger.log_status('base_year = %s' % self.base_year) self.assertEqual(self.base_year, SimulationState().get_current_time()) self.assertEqual(sqft, sqft_lag1) self.assertEqual(578 + 2083 + 1103 + 87, sqft_lag1 - sqft_lag2) # Do lag variables produce different results for derived attributes? self.gridcell.compute_variables( 'urbansim.gridcell.n_recent_development_projects', resources=self.config) self.gridcell.compute_variables( 'urbansim.gridcell.n_recent_development_projects_lag1', resources=self.config) n_recent_projects = self.gridcell.get_attribute( 'n_recent_development_projects').sum() n_recent_projects_lag1 = self.gridcell.get_attribute( 'n_recent_development_projects_lag1').sum() self.assertEqual(n_recent_projects, 11) self.assertEqual(n_recent_projects_lag1, 15) # Do lag_variables produce different results for derived attributes without lags? self.gridcell.compute_variables('urbansim.gridcell.ln_commercial_sqft', resources=self.config) self.gridcell.compute_variables( 'urbansim.gridcell.ln_commercial_sqft_lag4', resources=self.config) sqft = self.gridcell.get_attribute('ln_commercial_sqft').sum() sqft_lag4 = self.gridcell.get_attribute( 'ln_commercial_sqft_lag4').sum() self.assertNotEqual(sqft, sqft_lag4)
start_year = options.start_year end_year = options.end_year package_order = [ package.strip() for package in options.package_order.split(",") ] refinements = None refinements_storage = None if options.refinements_directory is not None: refinements_storage = StorageFactory().get_storage( 'flt_storage', storage_location=options.refinements_directory) refinements = DatasetFactory().search_for_dataset( 'refinement', package_order, arguments={'in_storage': refinements_storage}) years = refinements.get_attribute('year') if start_year is None: start_year = years.min() if end_year is None: end_year = years.max() simulation_state = SimulationState() simulation_state.set_cache_directory(options.cache_directory) simulation_state.set_current_time(start_year) attribute_cache = AttributeCache() dataset_pool = SessionConfiguration( new_instance=True, package_order=package_order, in_storage=attribute_cache).get_dataset_pool() if refinements is None: refinements = dataset_pool.get_dataset('refinement') years = refinements.get_attribute('year')