Exemplo n.º 1
0
    def test_land_price_model(self):
        storage = StorageFactory().get_storage('dict_storage')

        gridcell_set_table_name = 'gridcell_set'
        storage.write_table(
            table_name=gridcell_set_table_name,
            table_data={
                "percent_residential_within_walking_distance":array([30, 0, 90, 100]),
                "gridcell_year_built":array([2002, 1968, 1880, 1921]),
                "fraction_residential_land":array([0.5, 0.1, 0.3, 0.9]),
                "residential_land_value":array([0, 0, 0, 0]),
                "nonresidential_land_value":array([0, 0, 0, 0]),
                "development_type_id":array(  [1, 1,  1, 1]),
                "grid_id": array([1,2,3,4])
                }
            )
        gridcell_set = GridcellDataset(in_storage=storage, in_table_name=gridcell_set_table_name)

        specification = EquationSpecification(variables=(
            "percent_residential_within_walking_distance",
            "gridcell_year_built", "constant"),
            coefficients=("PRWWD", "YB", "constant"))
        coefficients = Coefficients(names=("constant", "PRWWD", "YB"), values=(10.0, -0.0025, 0.0001))
        lp = LandPriceModel(filter=None, debuglevel=3)
        lp.run(specification, coefficients, gridcell_set)
        result1 = gridcell_set.get_attribute("residential_land_value")
        result2 = gridcell_set.get_attribute("nonresidential_land_value")
        self.assertEqual(ma.allclose(result1, array([12482.124,  2681.723,  6367.914, 18708.617]), rtol=1e-3), True)
        self.assertEqual(ma.allclose(result2, array([12482.124,  24135.510, 14858.466,  2078.735]), rtol=1e-3), True)
Exemplo n.º 2
0
    def test_scaling_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

        storage = StorageFactory().get_storage('dict_storage')

        jobs_table_name = 'building_types'
        storage.write_table(
            table_name=jobs_table_name,
            table_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 = JobDataset(in_storage=storage, in_table_name=jobs_table_name)

        gridcells_table_name = 'gridcells'
        storage.write_table(table_name=gridcells_table_name,
                            table_data={"grid_id": arange(3) + 1})
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name=gridcells_table_name)

        # run model
        model = ScalingJobsModel(debuglevel=4)
        model.run(gridcells, jobs, agents_index=arange(10001, 11750))
        # get results
        gridcells.compute_variables([
            "urbansim.gridcell.number_of_jobs_of_sector_15",
            "urbansim.gridcell.number_of_jobs_of_sector_1"
        ],
                                    resources=Resources({"job": jobs}))
        # sector 1 jobs should be exactly the same
        result1 = gridcells.get_attribute("number_of_jobs_of_sector_1")
        self.assertEqual(
            ma.allclose(result1, array([1000, 1000, 1000]), rtol=0), True)
        # the distribution of sector 15 jobs should be the same with higher means
        result2 = gridcells.get_attribute("number_of_jobs_of_sector_15")
        #            logger.log_status(result2)
        self.assertEqual(
            ma.allclose(result2, array([5000, 2500, 1250]), rtol=0.05), True)
Exemplo n.º 3
0
    def test_assignment_of_development_types_in_RAM(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='gridcells',
                            table_data={
                                'residential_units': array([2, 1, 12, 20]),
                                'industrial_sqft': array([0, 10, 0, 4]),
                                'commercial_sqft': array([3, 1, 0, 4]),
                                'governmental_sqft': array([0, 0, 0, 0]),
                                'development_type_id': array([-1, -1, -1, -1]),
                                'grid_id': array([1, 2, 3, 4]),
                            })
        gridcell_set = GridcellDataset(in_storage=storage,
                                       in_table_name='gridcells')

        dev_type_data = {
            'development_type_id': array([1, 2, 3]),
            'min_units': array([2, 0, 11]),
            'max_units': array([10, 20, 20]),
            'min_sqft': array([0, 11, 0]),
            'max_sqft': array([10, 15, 10])
        }
        dev_set = self._create_simple_development_types_set(dev_type_data)

        EventsCoordinator()._set_development_types_for_sqft_and_units(
            gridcell_set, dev_set)
        self.assertEqual(
            ma.allclose(gridcell_set.get_attribute("development_type_id"),
                        array([1, 2, 3, 3])), True)
    def test_assignment_of_development_types_in_RAM(self):
        storage = StorageFactory().get_storage('dict_storage')
        
        storage.write_table(
            table_name='gridcells',
            table_data={
                'residential_units':array([2, 1, 12, 20]), 
                'industrial_sqft':array([0, 10, 0, 4]), 
                'commercial_sqft':array([3, 1,  0, 4]), 
                'governmental_sqft':array([0, 0,  0, 0]), 
                'development_type_id':array([-1, -1, -1, -1]), 
                'grid_id':array([1, 2, 3, 4]),
                }
            )
        gridcell_set = GridcellDataset(in_storage=storage, in_table_name='gridcells')
        
        dev_type_data = {
            'development_type_id':array([1,2,3]), 
            'min_units':array([2,  0,  11]), 
            'max_units':array([10, 20, 20]), 
            'min_sqft':array([0,  11, 0]), 
            'max_sqft':array( [10, 15, 10])
            }
        dev_set = self._create_simple_development_types_set(dev_type_data)

        EventsCoordinator()._set_development_types_for_sqft_and_units(gridcell_set, dev_set)
        self.assertEqual(ma.allclose(gridcell_set.get_attribute("development_type_id"), array([1,2,3,3])), True)
Exemplo n.º 5
0
    def test_residential_land_share_model(self):
        storage = StorageFactory().get_storage('dict_storage')

        gridcell_set_table_name = 'gridcell_set'
        storage.write_table(table_name=gridcell_set_table_name,
                            table_data={
                                "residential_units": array([1000, 0, 10, 500]),
                                "development_type_id": array([8, 17, 4, 8]),
                                "grid_id": array([1, 2, 3, 4])
                            })

        gridcell_set = GridcellDataset(in_storage=storage,
                                       in_table_name=gridcell_set_table_name)

        specification = EquationSpecification(
            variables=("urbansim.gridcell.devtype_8",
                       "gridcell.residential_units", "constant"),
            coefficients=("DEV8", "units", "constant"))
        coefficients = Coefficients(names=("constant", "DEV8", "units"),
                                    values=(-0.4, 1.6, 0.01))
        ResidentialLandShareModel(debuglevel=3).run(specification,
                                                    coefficients, gridcell_set)
        result = gridcell_set.get_attribute("fraction_residential_land")
        self.assertEqual(
            ma.allclose(result,
                        array([0.9999863, 0.4013123, 0.4255575, 0.9979747]),
                        rtol=1e-3), True)
Exemplo n.º 6
0
    def test_unplaced_agents_decrease_available_space(self):
        """Using the household location choice model, create a set of available spaces and
        2000 unplaced agents (along with 5000 placed agents). Run the model, and check that
        the unplaced agents were placed, and the number of available spaces has decreased"""
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='households',
                            table_data={
                                'grid_id': array(2000 * [0] + 5000 * [1]),
                                'household_id': arange(7000) + 1
                            })

        storage.write_table(table_name='gridcells',
                            table_data={
                                'residential_units': array(50 * [10000]),
                                'grid_id': arange(50) + 1
                            })

        households = HouseholdDataset(in_storage=storage,
                                      in_table_name='households')
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name='gridcells')

        coefficients = Coefficients(names=("dummy", ), values=(0.1, ))
        specification = EquationSpecification(
            variables=("gridcell.residential_units", ),
            coefficients=("dummy", ))
        """need to specify to the household location choice model exactly which households are moving,
        because by default it assumes all current households want to move, but in this test,
        the 5000 households already in gridcell #1 shouldn't move.
        here, we specify that only the unplaced households should be moved."""
        agents_index = where(households.get_attribute("grid_id") == 0)[0]

        hlcm = HouseholdLocationChoiceModelCreator().get_model(
            location_set=gridcells,
            choices="opus_core.random_choices_from_index",
            sample_size_locations=30)
        hlcm.run(specification,
                 coefficients,
                 agent_set=households,
                 agents_index=agents_index,
                 debuglevel=1)

        gridcells.compute_variables(
            ["urbansim.gridcell.vacant_residential_units"],
            resources=Resources({"household": households}))
        vacancies = gridcells.get_attribute("vacant_residential_units")
        """since there were 5000 households already in gridcell #1, and gridcell #1 has
        10000 residential units, there should be no more than 5000 vacant residential units
        in gridcell #1 after running this model"""
        self.assertEqual(vacancies[0] <= 5000, True,
                         "Error: %d" % (vacancies[0], ))
        """there should be exactly 430000 vacant residential units after the model run,
        because there were originally 50 gridcells with 10000 residential units each,
        and a total of 7000 units are occupied after the run"""
        self.assertEqual(
            sum(vacancies) == 50 * 10000 - 7000, True,
            "Error: %d" % (sum(vacancies)))
Exemplo n.º 7
0
    def test_scaling_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
        
        storage = StorageFactory().get_storage('dict_storage')

        jobs_table_name = 'jobs'        
        storage.write_table(
            table_name=jobs_table_name,
            table_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 = JobDataset(in_storage=storage, in_table_name=jobs_table_name)
        
        gridcells_table_name = 'gridcells'        
        storage.write_table(
            table_name=gridcells_table_name,
            table_data={"grid_id":arange(3)+1}
            )
        gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)
        
        # run model
        model = ScalingAgentsModel(submodel_string='sector_id', debuglevel=4)
        model.run(gridcells, jobs, agents_index = arange(10000, 11750))
        # get results
        gridcells.compute_variables(["urbansim.gridcell.number_of_jobs_of_sector_15", "urbansim.gridcell.number_of_jobs_of_sector_1"], 
            resources = Resources({"job":jobs}))
        self.assertEqual((jobs['grid_id']>0).all(), True)
        # sector 1 jobs should be exactly the same
        result1 = gridcells.get_attribute("number_of_jobs_of_sector_1")
        self.assertEqual(ma.allclose(result1, array([1000, 1000, 1000]), rtol=0), True)
        # the distribution of sector 15 jobs should be the same with higher means 
        result2 = gridcells.get_attribute("number_of_jobs_of_sector_15")
#            logger.log_status(result2)
        self.assertEqual(ma.allclose(result2, array([5000, 2500, 1250]), rtol=0.05), True)
Exemplo n.º 8
0
    def test_correct_land_value(self):
        #TODO: need to remove this when fixed LP correction only working when year >= 2002
        from opus_core.simulation_state import SimulationState
        SimulationState().set_current_time(2002)
        
        storage = StorageFactory().get_storage('dict_storage')

        gridcell_set_table_name = 'gridcell_set'        
        storage.write_table(
            table_name=gridcell_set_table_name,
            table_data={
                "percent_residential_within_walking_distance":array([30, 0, 90, 100]),
                "gridcell_year_built":array([2002, 1968, 1880, 1921]),
                "fraction_residential_land":array([0.5, 0.1, 0.3, 0.9]),
                "residential_land_value":array([0, 0, 0, 0]),                          
                "residential_land_value_lag1":array([15000, 0, 7500, 0]),
                "nonresidential_land_value":array([0, 0, 0, 0]),  
                "nonresidential_land_value_lag1":array([15000, 0, 17500, 0]),                                                                    
                "development_type_id":array(  [2, 1, 1, 1]),
                "development_type_id_lag2":array(  [1, 1, 1, 1]),
                "grid_id": array([1,2,3,4])
                }
            )

        gridcell_set = GridcellDataset(in_storage=storage, in_table_name=gridcell_set_table_name)

        specification = EquationSpecification(variables=(
            "percent_residential_within_walking_distance", 
            "gridcell_year_built", "constant"), 
            coefficients=("PRWWD", "YB", "constant"))
        coefficients = Coefficients(names=("constant", "PRWWD", "YB"), values=(10.0, -0.0025, 0.0001))
        lp = LandPriceModel(filter=None, debuglevel=3)
        lp.run(specification, coefficients, gridcell_set)
        correctmodel = CorrectLandValue()
        correctmodel.run(gridcell_set)
        result1 = gridcell_set.get_attribute("residential_land_value")
        result2 = gridcell_set.get_attribute("nonresidential_land_value")
        self.assertEqual(ma.allclose(result1, array([15000.0,  2681.723,  6367.914, 18708.617]), rtol=1e-3), True)
        self.assertEqual(ma.allclose(result2, array([15000.0,  24135.510, 14858.466,  2078.735]), rtol=1e-3), True)
    def test_unplaced_agents_decrease_available_space(self):
        """Using the household location choice model, create a set of available spaces and
        2000 unplaced agents (along with 5000 placed agents). Run the model, and check that
        the unplaced agents were placed, and the number of available spaces has decreased"""
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='households',
            table_data = {
                'grid_id': array(2000*[0] + 5000*[1]),
                'household_id': arange(7000)+1
                }
            )

        storage.write_table(table_name='gridcells',
            table_data= {
                'residential_units':array(50*[10000]),
                'grid_id':  arange(50)+1
                }
            )

        households = HouseholdDataset(in_storage=storage, in_table_name='households')
        gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells')

        coefficients = Coefficients(names=("dummy",), values=(0.1,))
        specification = EquationSpecification(variables=("gridcell.residential_units",), coefficients=("dummy",))

        """need to specify to the household location choice model exactly which households are moving,
        because by default it assumes all current households want to move, but in this test,
        the 5000 households already in gridcell #1 shouldn't move.
        here, we specify that only the unplaced households should be moved."""
        agents_index = where(households.get_attribute("grid_id") == 0)[0]

        hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells,
               choices = "opus_core.random_choices_from_index", sample_size_locations = 30)
        hlcm.run(specification, coefficients, agent_set=households, agents_index=agents_index, debuglevel=1)

        gridcells.compute_variables(["urbansim.gridcell.vacant_residential_units"],
                                    resources=Resources({"household":households}))
        vacancies = gridcells.get_attribute("vacant_residential_units")

        """since there were 5000 households already in gridcell #1, and gridcell #1 has
        10000 residential units, there should be no more than 5000 vacant residential units
        in gridcell #1 after running this model"""
        self.assertEqual(vacancies[0] <= 5000,
                         True, "Error: %d" % (vacancies[0],))
        """there should be exactly 430000 vacant residential units after the model run,
        because there were originally 50 gridcells with 10000 residential units each,
        and a total of 7000 units are occupied after the run"""
        self.assertEqual(sum(vacancies) == 50 * 10000 - 7000,
                         True, "Error: %d" % (sum(vacancies)))
        def run_model2():
            storage = StorageFactory().get_storage('dict_storage')

            storage.write_table(table_name = 'households', table_data = household_data)
            households = HouseholdDataset(in_storage=storage, in_table_name='households')

            storage.write_table(table_name = 'gridcells', table_data = gridcell_data)
            gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells')

            hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False,
                    choices = "opus_core.random_choices_from_index", sample_size_locations = 30)
            hlcm.run(specification, coefficients, agent_set = households,
                     run_config=Resources({"demand_string":"gridcell.housing_demand"}))
            return gridcells.get_attribute("housing_demand")
Exemplo n.º 11
0
    def test_infinite_values(self):
        storage = StorageFactory().get_storage('dict_storage')

        gridcell_set_table_name = 'gridcell_set'
        storage.write_table(
            table_name=gridcell_set_table_name,
            table_data={
                "residential_land_value":array([100, 1e+50, 800, 0], dtype=float32),
                "nonresidential_land_value":array([1e+100, 0, 20, 0], dtype=float32),
                "grid_id": array([1,2,3,4])
                }
            )
        gridcell_set = GridcellDataset(in_storage=storage, in_table_name=gridcell_set_table_name)

        lp = LandPriceModel()

        logger.enable_hidden_error_and_warning_words()
        lp.post_check(gridcell_set)
        logger.disable_hidden_error_and_warning_words()

        result1 = gridcell_set.get_attribute("residential_land_value")
        result2 = gridcell_set.get_attribute("nonresidential_land_value")
        self.assertEqual(ma.allclose(result1, array([100,  1e+38,  800, 0]), rtol=1e-3), True)
        self.assertEqual(ma.allclose(result2, array([1e+38,  0, 20,  0]), rtol=1e-3), True)
        def run_model1():
            storage = StorageFactory().get_storage('dict_storage')

            storage.write_table(table_name = 'households', table_data = household_data)
            households = HouseholdDataset(in_storage=storage, in_table_name='households')

            storage.write_table(table_name = 'gridcells', table_data = gridcell_data)
            gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells')

            hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False,
                    choices = "opus_core.random_choices_from_index", sample_size_locations = 30)
            hlcm.run(specification, coefficients, agent_set = households)

            gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                                        resources=Resources({"household":households}))
            return gridcells.get_attribute("number_of_households")
Exemplo n.º 13
0
    def test_do_nothing_if_no_agents(self):
        storage = StorageFactory().get_storage('dict_storage')

        households_table_name = 'households'
        storage.write_table(table_name=households_table_name,
                            table_data={
                                "household_id": arange(10000) + 1,
                                "grid_id": array(10000 * [-1])
                            })
        households = HouseholdDataset(in_storage=storage,
                                      in_table_name=households_table_name)

        gridcells_table_name = 'gridcells'
        storage.write_table(table_name=gridcells_table_name,
                            table_data={
                                "grid_id": arange(100) + 1,
                                "cost": array(50 * [100] + 50 * [1000])
                            })
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name=gridcells_table_name)

        # create coefficients and specification
        coefficients = Coefficients(names=("costcoef", ), values=(-0.001, ))
        specification = EquationSpecification(variables=("gridcell.cost", ),
                                              coefficients=("costcoef", ))

        # run the model
        hlcm = HouseholdLocationChoiceModelCreator().get_model(
            location_set=gridcells,
            compute_capacity_flag=False,
            choices="opus_core.random_choices_from_index",
            sample_size_locations=30)
        hlcm.run(specification,
                 coefficients,
                 agent_set=households,
                 agents_index=array([], dtype='int32'),
                 debuglevel=1)

        # get results
        gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                                    resources=Resources(
                                        {"household": households}))
        result = gridcells.get_attribute("number_of_households")

        # check the individual gridcells
        self.assertEqual(ma.allclose(result, zeros((100, )), rtol=0), True)
    def test_do_nothing_if_no_agents(self):
        storage = StorageFactory().get_storage('dict_storage')

        households_table_name = 'households'
        storage.write_table(
            table_name = households_table_name,
            table_data = {
                "household_id": arange(10000)+1,
                "grid_id": array(10000*[-1])
                }
            )
        households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name)

        gridcells_table_name = 'gridcells'
        storage.write_table(
            table_name = gridcells_table_name,
            table_data = {
                "grid_id": arange(100)+1,
                "cost":array(50*[100]+50*[1000])
                }
            )
        gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)

        # create coefficients and specification
        coefficients = Coefficients(names=("costcoef", ), values=(-0.001,))
        specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", ))

        # run the model
        hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False,
                choices = "opus_core.random_choices_from_index", sample_size_locations = 30)
        hlcm.run(specification, coefficients, agent_set = households, agents_index=array([], dtype='int32'), debuglevel=1)

        # get results
        gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
            resources=Resources({"household":households}))
        result = gridcells.get_attribute("number_of_households")

        # check the individual gridcells
        self.assertEqual(ma.allclose(result, zeros((100,)) , rtol=0), True)
    def test_residential_land_share_model(self):
        storage = StorageFactory().get_storage('dict_storage')

        gridcell_set_table_name = 'gridcell_set'
        storage.write_table(
            table_name=gridcell_set_table_name,
            table_data={
                "residential_units":array([1000, 0, 10, 500]),
                "development_type_id":array([8, 17, 4, 8]),
                "grid_id": array([1,2,3,4])
                }
            )

        gridcell_set = GridcellDataset(in_storage=storage, in_table_name=gridcell_set_table_name)

        specification = EquationSpecification(variables=(
            "urbansim.gridcell.devtype_8",
            "gridcell.residential_units", "constant"),
            coefficients=("DEV8", "units", "constant"))
        coefficients = Coefficients(names=("constant", "DEV8", "units"), values=(-0.4, 1.6, 0.01))
        ResidentialLandShareModel(debuglevel=3).run(specification, coefficients, gridcell_set)
        result = gridcell_set.get_attribute("fraction_residential_land")
        self.assertEqual(ma.allclose(result, array([0.9999863,  0.4013123, 0.4255575, 0.9979747]), rtol=1e-3), True)
    def test_housing_price_model(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(
            table_name='gridcells',
            table_data={
                'percent_residential_within_walking_distance':array([30, 0, 90, 100]),
                'gridcell_year_built':array([2002, 1968, 1880, 1921]),
                'housing_price':array([0, 0, 0, 0]).astype(float32),
                'development_type_id':array([1, 1,  1, 1]),
                'grid_id':array([1,2,3,4])
                }
            )

        gridcell_set = GridcellDataset(in_storage=storage, in_table_name='gridcells')

        specification = EquationSpecification(
            variables = (
                'percent_residential_within_walking_distance',
                'gridcell_year_built',
                'constant'
                ),
            coefficients=(
                'PRWWD',
                'YB',
                'constant'
                )
            )

        coefficients = Coefficients(names=("constant", "PRWWD", "YB"), values=(10.0, -0.25, 0.1))

        lp = HousingPriceModel(debuglevel=3)
        lp.filter_attribute = None
        lp.run(specification, coefficients, gridcell_set)
        result = gridcell_set.get_attribute("housing_price")

        self.assertEqual(ma.allclose(result, array([202.7,  206.8,  175.5, 177.1]), rtol=1e-3), True)
Exemplo n.º 17
0
    def test_housing_price_model(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='gridcells',
                            table_data={
                                'percent_residential_within_walking_distance':
                                array([30, 0, 90, 100]),
                                'gridcell_year_built':
                                array([2002, 1968, 1880, 1921]),
                                'housing_price':
                                array([0, 0, 0, 0]).astype(float32),
                                'development_type_id':
                                array([1, 1, 1, 1]),
                                'grid_id':
                                array([1, 2, 3, 4])
                            })

        gridcell_set = GridcellDataset(in_storage=storage,
                                       in_table_name='gridcells')

        specification = EquationSpecification(
            variables=('percent_residential_within_walking_distance',
                       'gridcell_year_built', 'constant'),
            coefficients=('PRWWD', 'YB', 'constant'))

        coefficients = Coefficients(names=("constant", "PRWWD", "YB"),
                                    values=(10.0, -0.25, 0.1))

        lp = HousingPriceModel(debuglevel=3)
        lp.filter_attribute = None
        lp.run(specification, coefficients, gridcell_set)
        result = gridcell_set.get_attribute("housing_price")

        self.assertEqual(
            ma.allclose(result, array([202.7, 206.8, 175.5, 177.1]),
                        rtol=1e-3), True)
Exemplo n.º 18
0
        def run_model2():
            storage = StorageFactory().get_storage('dict_storage')

            storage.write_table(table_name='households',
                                table_data=household_data)
            households = HouseholdDataset(in_storage=storage,
                                          in_table_name='households')

            storage.write_table(table_name='gridcells',
                                table_data=gridcell_data)
            gridcells = GridcellDataset(in_storage=storage,
                                        in_table_name='gridcells')

            hlcm = HouseholdLocationChoiceModelCreator().get_model(
                location_set=gridcells,
                compute_capacity_flag=False,
                choices="opus_core.random_choices_from_index",
                sample_size_locations=30)
            hlcm.run(specification,
                     coefficients,
                     agent_set=households,
                     run_config=Resources(
                         {"demand_string": "gridcell.housing_demand"}))
            return gridcells.get_attribute("housing_demand")
Exemplo n.º 19
0
        def run_model1():
            storage = StorageFactory().get_storage('dict_storage')

            storage.write_table(table_name='households',
                                table_data=household_data)
            households = HouseholdDataset(in_storage=storage,
                                          in_table_name='households')

            storage.write_table(table_name='gridcells',
                                table_data=gridcell_data)
            gridcells = GridcellDataset(in_storage=storage,
                                        in_table_name='gridcells')

            hlcm = HouseholdLocationChoiceModelCreator().get_model(
                location_set=gridcells,
                compute_capacity_flag=False,
                choices="opus_core.random_choices_from_index",
                sample_size_locations=30)
            hlcm.run(specification, coefficients, agent_set=households)

            gridcells.compute_variables(
                ["urbansim.gridcell.number_of_households"],
                resources=Resources({"household": households}))
            return gridcells.get_attribute("number_of_households")
Exemplo n.º 20
0
specification = EquationSpecification(variables=("gridcell.cost", ), 
                                   coefficients=("costcoef", ))

hlcm = HouseholdLocationChoiceModelCreator().get_model(
    location_set = locations,
    sampler=None,
    utilities="opus_core.linear_utilities",
    probabilities="opus_core.mnl_probabilities",
    choices="opus_core.random_choices_from_index", 
    compute_capacity_flag=False)

agents.get_attribute("location")
results = hlcm.run(specification, coefficients, agents)
agents.get_attribute("location")
hlcm.upc_sequence.plot_choice_histograms( 
                    capacity=locations.get_attribute("vacant_units"))
hlcm.upc_sequence.show_plots()

coef, results = hlcm.estimate(specification, agents)

results = hlcm.run(specification, coef, agents)
hlcm.upc_sequence.plot_choice_histograms( 
                    capacity=locations.get_attribute("vacant_units"))
hlcm.upc_sequence.show_plots()

hlcm2 = HouseholdLocationChoiceModelCreator().get_model(
    location_set = locations,
    sampler=None,
    utilities="opus_core.linear_utilities",
    probabilities="opus_core.mnl_probabilities",
    choices="urbansim.lottery_choices", 
    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)
from urbansim.datasets.gridcell_dataset import GridcellDataset
from numpy import array
from opus_core.misc import unique

consumption_grid_id = consumption.get_attribute("grid_id")
years = consumption.get_attribute("billyear")
distinct_years = unique(years)

import os
from numpy import where, zeros, arange
cache_directory = "D:/urbansim_cache/water_demand"
for year in arange(1991, 2001):
    print year
    flt_storage = StorageCreator().build_storage(type="flt", location=os.path.join(cache_directory, str(year)))
    gridcells = GridcellDataset(in_storage=flt_storage)
    grid_id_idx = array(map(lambda x: gridcells.try_id_mapping(x, -1), consumption_grid_id))
    year_idx = where(years==year)[0]
    grid_id_idx_for_year = grid_id_idx[year_idx]
    for attr in gridcells.get_known_attribute_names():
        if attr not in consumption.get_known_attribute_names():
            ftype = gridcells.get_attribute(attr).type()
            consumption.add_attribute(name=attr, data=zeros(consumption.size(), dtype=ftype))
        consumption.modify_attribute(name=attr,
                                     data=gridcells.get_attribute_by_index(attr, grid_id_idx_for_year),
                                     index=year_idx)

# store consumption dataset
out_storage = StorageCreator().build_storage(type="flt", location=cache_directory)
consumption.write_dataset(attributes = consumption.get_known_attribute_names(),
                          out_storage=out_storage, out_table_name=consumption_type.lower())
Exemplo n.º 23
0
    def test_unrolling(self):
        from urbansim.datasets.gridcell_dataset import GridcellDataset
        from urbansim.datasets.development_event_dataset import DevelopmentEventDataset

        storage = StorageFactory().get_storage('dict_storage')

        gridcells_table_name = 'gridcells'
        storage.write_table(
            table_name=gridcells_table_name,
            table_data={
                'grid_id': array([1, 2, 3]),
                'development_type_id': array([3, 3, 3]),
                'commercial_sqft': array([50, 50, 50]),
                'industrial_sqft': array([100, 100, 100]),
                # Rest of this data is not used by unit tests, but is required for unrolling
                'governmental_sqft': array([0, 0, 0]),
                'residential_units': array([0, 0, 0]),
                'commercial_improvement_value': array([0, 0, 0]),
                'industrial_improvement_value': array([0, 0, 0]),
                'governmental_improvement_value': array([0, 0, 0]),
                'residential_improvement_value': array([0, 0, 0]),
            },
        )

        dev_event_history_table_name = 'dev_event_history'
        storage.write_table(
            table_name=dev_event_history_table_name,
            table_data={
                'scheduled_year': array([1999, 1999, 1998, 1998]),
                'grid_id': array([1, 3, 2, 3]),
                'starting_development_type_id': array([3, 3, 2, 1]),
                'commercial_sqft': array([10, 20, 30, 40]),
                'commercial_sqft_change_type': array(['A', 'A', 'A', 'A']),
                'industrial_sqft': array([20, 200, 99, 50]),
                'industrial_sqft_change_type': array(['A', 'D', 'R', 'A']),
                # Rest of this data is not used by unit tests, but is required for unrolling
                'governmental_sqft': array([0, 0, 0, 0]),
                'residential_units': array([0, 0, 0, 0]),
                'commercial_improvement_value': array([0, 0, 0, 0]),
                'industrial_improvement_value': array([0, 0, 0, 0]),
                'governmental_improvement_value': array([0, 0, 0, 0]),
                'residential_improvement_value': array([0, 0, 0, 0]),
            },
        )

        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name=gridcells_table_name)
        dev_event_history = DevelopmentEventDataset(
            in_storage=storage, in_table_name=dev_event_history_table_name)

        roller = RollbackGridcells()

        roller.unroll_gridcells_for_one_year(gridcells, dev_event_history,
                                             2000)
        self.assert_(
            ma.allequal(gridcells.get_attribute('commercial_sqft'),
                        array([50, 50, 50])))
        self.assert_(
            ma.allequal(gridcells.get_attribute('industrial_sqft'),
                        array([100, 100, 100])))
        self.assert_(
            ma.allequal(gridcells.get_attribute('development_type_id'),
                        array([3, 3, 3])))

        roller.unroll_gridcells_for_one_year(gridcells, dev_event_history,
                                             1999)
        self.assert_(
            ma.allequal(gridcells.get_attribute('commercial_sqft'),
                        array([40, 50, 30])),
            'Unexpected results for 1999: expected %s; received %s' %
            (array([40, 50, 30]), gridcells.get_attribute('commercial_sqft')))
        self.assert_(
            ma.allequal(gridcells.get_attribute('industrial_sqft'),
                        array([80, 100, 300])))
        self.assert_(
            ma.allequal(gridcells.get_attribute('development_type_id'),
                        array([3, 3, 3])))

        roller.unroll_gridcells_for_one_year(gridcells, dev_event_history,
                                             1998)
        self.assert_(
            ma.allequal(gridcells.get_attribute('commercial_sqft'),
                        array([40, 20, 0])))
        self.assert_(
            ma.allequal(gridcells.get_attribute('industrial_sqft'),
                        array([80, 99, 250])))
        self.assert_(
            ma.allequal(gridcells.get_attribute('development_type_id'),
                        array([3, 2, 1])))
    def test_agents_placed_in_appropriate_types(self):
        """Create 1000 unplaced industrial jobs and 1 commercial job. Allocate 50 commercial
        gridcells with enough space for 10 commercial jobs per gridcell. After running the
        EmploymentLocationChoiceModel, the 1 commercial job should be placed,
        but the 100 industrial jobs should remain unplaced
        """
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='job_building_types',
            table_data = {
                'id':array([2,1]),
                'name': array(['commercial', 'industrial'])
                }
            )
        job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name='job_building_types')

        storage.write_table(table_name='jobs',
            table_data = {
                'job_id': arange(1001)+1,
                'grid_id': array([0]*1001),
                'building_type': array([1]*1000 + [2])
                }
            )
        jobs = JobDataset(in_storage=storage, in_table_name='jobs')

        storage.write_table(table_name='gridcells',
            table_data = {
                'grid_id': arange(50)+1,
                'commercial_sqft': array([1000]*50),
                'commercial_sqft_per_job': array([100]*50)
                }
            )
        gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells')

        coefficients = Coefficients(names=("dummy",), values=(0.1,))
        specification = EquationSpecification(variables=("gridcell.commercial_sqft",), coefficients=("dummy",))

        compute_resources = Resources({"job":jobs, "job_building_type": job_building_types})
        agents_index = where(jobs.get_attribute("grid_id") == 0)
        unplace_jobs = DatasetSubset(jobs, agents_index)
        agents_index = where(unplace_jobs.get_attribute("building_type") == 2)[0]
        gridcells.compute_variables(["urbansim.gridcell.number_of_commercial_jobs"],
                                    resources=compute_resources)
        commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs")

        gridcells.compute_variables(["urbansim.gridcell.number_of_industrial_jobs"],
                                    resources=compute_resources)
        industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs")
        model_group = ModelGroup(job_building_types, "name")
        elcm = EmploymentLocationChoiceModel(ModelGroupMember(model_group,"commercial"), location_set=gridcells,
               agents_grouping_attribute = "job.building_type",
               choices = "opus_core.random_choices_from_index", sample_size_locations = 30)
        elcm.run(specification, coefficients, agent_set = jobs, agents_index=agents_index, debuglevel=1)

        gridcells.compute_variables(["urbansim.gridcell.number_of_commercial_jobs"],
                                    resources=compute_resources)
        commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs")

        gridcells.compute_variables(["urbansim.gridcell.number_of_industrial_jobs"],
                                    resources=compute_resources)
        industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs")

        self.assertEqual(commercial_jobs.sum() == 1,
                         True, "Error, there should only be a total of 1 commercial job")
        self.assertEqual(industrial_jobs.sum() == 0,
                         True, "Error, there should be no industrial jobs because there's no space for them")
Exemplo n.º 25
0
specification = EquationSpecification(variables=("gridcell.cost", ),
                                      coefficients=("costcoef", ))

hlcm = HouseholdLocationChoiceModelCreator().get_model(
    location_set=locations,
    sampler=None,
    utilities="opus_core.linear_utilities",
    probabilities="opus_core.mnl_probabilities",
    choices="opus_core.random_choices_from_index",
    compute_capacity_flag=False)

agents.get_attribute("location")
results = hlcm.run(specification, coefficients, agents)
agents.get_attribute("location")
hlcm.upc_sequence.plot_choice_histograms(
    capacity=locations.get_attribute("vacant_units"))
hlcm.upc_sequence.show_plots()

coef, results = hlcm.estimate(specification, agents)

results = hlcm.run(specification, coef, agents)
hlcm.upc_sequence.plot_choice_histograms(
    capacity=locations.get_attribute("vacant_units"))
hlcm.upc_sequence.show_plots()

hlcm2 = HouseholdLocationChoiceModelCreator().get_model(
    location_set=locations,
    sampler=None,
    utilities="opus_core.linear_utilities",
    probabilities="opus_core.mnl_probabilities",
    choices="urbansim.lottery_choices",
    def test_unrolling(self):
        from urbansim.datasets.gridcell_dataset import GridcellDataset
        from urbansim.datasets.building_dataset import BuildingDataset
        from urbansim.datasets.building_type_dataset import BuildingTypeDataset
        from opus_core.datasets.dataset_pool import DatasetPool
        from numpy import arange
        
        storage = StorageFactory().get_storage('dict_storage')

        gridcells_table_name = 'gridcells'        
        storage.write_table(
            table_name = gridcells_table_name,
            table_data = {
                'grid_id':array([1,2,3]),
                'commercial_sqft':array([50,50,50]),
                'industrial_sqft':array([100,100,100]),
                'governmental_sqft':array([0,0,0]),
                'residential_units':array([10,0,0]),
                'commercial_improvement_value':array([0,0,0]),
                'industrial_improvement_value':array([0,0,0]),
                'governmental_improvement_value':array([0,0,0]),
                'residential_improvement_value':array([0,0,0]),
                },
            )

        building_table_name = 'buildings'        
        storage.write_table(
            table_name = building_table_name,
            table_data = {
                'building_id': arange(6)+1, 
                'year_built':array([1999,1999,1998,1998,1998,1999]),
                'grid_id':array([1,3,2,3,1,1]),
                'sqft':array([10,20,30,40,0,20]),
                'residential_units':array([0,0,0,0,5,0]),
                'improvement_value':array([0,0,0,0,0,0]),
                'building_type_id': array([1,2,1,2,3,1])
                },
            )
        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]), 
                    'name': array(['industrial', 'commercial', 'residential', 'governmental'])
                    }
            )

        building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name)
        gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)
        buildings = BuildingDataset(in_storage=storage, in_table_name=building_table_name)
        dataset_pool = DatasetPool()
        dataset_pool._add_dataset(building_types.get_dataset_name(), building_types)
        
        roller = RollbackGridcellsFromBuildings()
        
        roller.unroll_gridcells_for_one_year(gridcells, buildings, 2000, dataset_pool)
        self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'),
                              array([50,50,50])))
        self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'),
                              array([100,100,100])))
        self.assert_(ma.allequal(gridcells.get_attribute('residential_units'),
                              array([10, 0, 0])))
        
        roller.unroll_gridcells_for_one_year(gridcells, buildings, 1999, dataset_pool)
        self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'),
                              array([50,50,30])),
                     'Unexpected results: expected %s; received %s' % 
                     (array([50,50,30]), gridcells.get_attribute('commercial_sqft')))
        self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'),
                              array([70,100,100])))
        
        roller.unroll_gridcells_for_one_year(gridcells, buildings, 1998, dataset_pool)
        self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'),
                              array([50,50,0])))
        self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'),
                              array([70,70,100])))
        self.assert_(ma.allequal(gridcells.get_attribute('residential_units'),
                              array([5, 0, 0])))
Exemplo n.º 27
0
    def test_agents_placed_in_appropriate_types(self):
        """Create 1000 unplaced industrial jobs and 1 commercial job. Allocate 50 commercial
        gridcells with enough space for 10 commercial jobs per gridcell. After running the
        EmploymentLocationChoiceModel, the 1 commercial job should be placed,
        but the 100 industrial jobs should remain unplaced
        """
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='job_building_types',
                            table_data={
                                'id': array([2, 1]),
                                'name': array(['commercial', 'industrial'])
                            })
        job_building_types = JobBuildingTypeDataset(
            in_storage=storage, in_table_name='job_building_types')

        storage.write_table(table_name='jobs',
                            table_data={
                                'job_id': arange(1001) + 1,
                                'grid_id': array([0] * 1001),
                                'building_type': array([1] * 1000 + [2])
                            })
        jobs = JobDataset(in_storage=storage, in_table_name='jobs')

        storage.write_table(table_name='gridcells',
                            table_data={
                                'grid_id': arange(50) + 1,
                                'commercial_sqft': array([1000] * 50),
                                'commercial_sqft_per_job': array([100] * 50)
                            })
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name='gridcells')

        coefficients = Coefficients(names=("dummy", ), values=(0.1, ))
        specification = EquationSpecification(
            variables=("gridcell.commercial_sqft", ), coefficients=("dummy", ))

        compute_resources = Resources({
            "job": jobs,
            "job_building_type": job_building_types
        })
        agents_index = where(jobs.get_attribute("grid_id") == 0)
        unplace_jobs = DatasetSubset(jobs, agents_index)
        agents_index = where(
            unplace_jobs.get_attribute("building_type") == 2)[0]
        gridcells.compute_variables(
            ["urbansim.gridcell.number_of_commercial_jobs"],
            resources=compute_resources)
        commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs")

        gridcells.compute_variables(
            ["urbansim.gridcell.number_of_industrial_jobs"],
            resources=compute_resources)
        industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs")
        model_group = ModelGroup(job_building_types, "name")
        elcm = EmploymentLocationChoiceModel(
            ModelGroupMember(model_group, "commercial"),
            location_set=gridcells,
            agents_grouping_attribute="job.building_type",
            choices="opus_core.random_choices_from_index",
            sample_size_locations=30)
        elcm.run(specification,
                 coefficients,
                 agent_set=jobs,
                 agents_index=agents_index,
                 debuglevel=1)

        gridcells.compute_variables(
            ["urbansim.gridcell.number_of_commercial_jobs"],
            resources=compute_resources)
        commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs")

        gridcells.compute_variables(
            ["urbansim.gridcell.number_of_industrial_jobs"],
            resources=compute_resources)
        industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs")

        self.assertEqual(
            commercial_jobs.sum() == 1, True,
            "Error, there should only be a total of 1 commercial job")
        self.assertEqual(
            industrial_jobs.sum() == 0, True,
            "Error, there should be no industrial jobs because there's no space for them"
        )
    def test_unrolling(self):
        from urbansim.datasets.gridcell_dataset import GridcellDataset
        from urbansim.datasets.development_event_dataset import DevelopmentEventDataset
        
        storage = StorageFactory().get_storage('dict_storage')

        gridcells_table_name = 'gridcells'        
        storage.write_table(
            table_name = gridcells_table_name,
            table_data = {
                'grid_id':array([1,2,3]),
                'development_type_id':array([3,3,3]),
                'commercial_sqft':array([50,50,50]),
                'industrial_sqft':array([100,100,100]),
                # Rest of this data is not used by unit tests, but is required for unrolling
                'governmental_sqft':array([0,0,0]),
                'residential_units':array([0,0,0]),
                'commercial_improvement_value':array([0,0,0]),
                'industrial_improvement_value':array([0,0,0]),
                'governmental_improvement_value':array([0,0,0]),
                'residential_improvement_value':array([0,0,0]),
                },
            )

        dev_event_history_table_name = 'dev_event_history'        
        storage.write_table(
            table_name = dev_event_history_table_name,
            table_data = {
                'scheduled_year':array([1999,1999,1998,1998]),
                'grid_id':array([1,3,2,3]),
                'starting_development_type_id':array([3,3,2,1]),
                'commercial_sqft':array([10,20,30,40]),
                'commercial_sqft_change_type':array(['A','A','A','A']),
                'industrial_sqft':array([20,200,99,50]),
                'industrial_sqft_change_type':array(['A','D','R','A']),
                # Rest of this data is not used by unit tests, but is required for unrolling
                'governmental_sqft':array([0,0,0,0]),
                'residential_units':array([0,0,0,0]),
                'commercial_improvement_value':array([0,0,0,0]),
                'industrial_improvement_value':array([0,0,0,0]),
                'governmental_improvement_value':array([0,0,0,0]),
                'residential_improvement_value':array([0,0,0,0]),
                },
            )

        gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)
        dev_event_history = DevelopmentEventDataset(in_storage=storage, in_table_name=dev_event_history_table_name)

        roller = RollbackGridcells()
        
        roller.unroll_gridcells_for_one_year(gridcells, dev_event_history, 2000)
        self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'),
                              array([50,50,50])))
        self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'),
                              array([100,100,100])))
        self.assert_(ma.allequal(gridcells.get_attribute('development_type_id'),
                              array([3,3,3])))
        
        roller.unroll_gridcells_for_one_year(gridcells, dev_event_history, 1999)
        self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'),
                              array([40,50,30])),
                     'Unexpected results for 1999: expected %s; received %s' % 
                     (array([40,50,30]), gridcells.get_attribute('commercial_sqft')))
        self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'),
                              array([80,100,300])))
        self.assert_(ma.allequal(gridcells.get_attribute('development_type_id'),
                              array([3,3,3])))
        
        roller.unroll_gridcells_for_one_year(gridcells, dev_event_history, 1998)
        self.assert_(ma.allequal(gridcells.get_attribute('commercial_sqft'),
                              array([40,20,0])))
        self.assert_(ma.allequal(gridcells.get_attribute('industrial_sqft'),
                              array([80,99,250])))
        self.assert_(ma.allequal(gridcells.get_attribute('development_type_id'),
                              array([3,2,1])))
Exemplo n.º 29
0
from numpy import where, zeros, arange
cache_directory = "D:/urbansim_cache/water_demand"
for year in arange(1991, 2001):
    print year
    flt_storage = StorageCreator().build_storage(type="flt",
                                                 location=os.path.join(
                                                     cache_directory,
                                                     str(year)))
    gridcells = GridcellDataset(in_storage=flt_storage)
    grid_id_idx = array(
        map(lambda x: gridcells.try_id_mapping(x, -1), consumption_grid_id))
    year_idx = where(years == year)[0]
    grid_id_idx_for_year = grid_id_idx[year_idx]
    for attr in gridcells.get_known_attribute_names():
        if attr not in consumption.get_known_attribute_names():
            ftype = gridcells.get_attribute(attr).type()
            consumption.add_attribute(name=attr,
                                      data=zeros(consumption.size(),
                                                 dtype=ftype))
        consumption.modify_attribute(name=attr,
                                     data=gridcells.get_attribute_by_index(
                                         attr, grid_id_idx_for_year),
                                     index=year_idx)

# store consumption dataset
out_storage = StorageCreator().build_storage(type="flt",
                                             location=cache_directory)
consumption.write_dataset(attributes=consumption.get_known_attribute_names(),
                          out_storage=out_storage,
                          out_table_name=consumption_type.lower())