Пример #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)
Пример #2
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)
    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)
Пример #4
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)
Пример #5
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)))
    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)))
Пример #7
0
    def test_agents_do_not_go_to_inferior_locations(self):
        """100 gridcells - 99 with attractiveness 2000, 1 with attractiveness 1, no capacity restrictions
        10,000 households
        We set the coefficient value for attracitiveness to 1.
        """
        storage = StorageFactory().get_storage('dict_storage')

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

        # create gridcells
        storage.write_table(table_name='gridcells',
                            table_data={
                                'grid_id': arange(100) + 1,
                                'attractiveness': array(99 * [2000] + [1])
                            })
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name='gridcells')

        # create coefficients and specification
        coefficients = Coefficients(names=("attractcoef", ), values=(1, ))
        specification = EquationSpecification(
            variables=("gridcell.attractiveness", ),
            coefficients=("attractcoef", ))

        # 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,
                 debuglevel=1)

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

        # nobody should choose gridcell 100
        self.assertEqual(result, 0, "Error: %s is not equal to 0" % (result, ))
        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")
Пример #9
0
    def _create_simple_gridcell_set(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='gridcells',
                            table_data={
                                'residential_units':
                                array([2, 1, 12, 20]),
                                'commercial_sqft':
                                array([3, 1, 0, 4]),
                                'industrial_sqft':
                                array([0, 10, 0, 4]),
                                'governmental_sqft':
                                array([0, 0, 0, 0]),
                                'commercial_improvement_value':
                                array([1, 2, 3, 389]),
                                'industrial_improvement_value':
                                array([1, 2, 3, 78]),
                                'residential_improvement_value':
                                array([1, 2, 3, 3]),
                                'development_type_id':
                                array([1, 3, 5, 6]),
                                'grid_id':
                                array([1, 2, 3, 4])
                            })

        return GridcellDataset(in_storage=storage, in_table_name='gridcells')
        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")
Пример #11
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)
Пример #12
0
    def xtest_gracefully_handle_empty_choice_sets(self):
        storage = StorageFactory().get_storage('dict_storage')

        #create households
        storage.write_table(table_name='households',
                            table_data={
                                'household_id': arange(10000) + 1,
                                'grid_id': array(100 * range(100)) + 1
                            })
        households = HouseholdDataset(in_storage=storage,
                                      in_table_name='households')

        # create gridcells
        storage.write_table(table_name='gridcells',
                            table_data={
                                'grid_id': arange(100) + 1,
                                'residential_units': array(100 * [100])
                            })
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name='gridcells')

        # create coefficients and specification
        coefficients = Coefficients(names=("dummy", ), values=(0, ))
        specification = EquationSpecification(
            variables=("gridcell.residential_units", ),
            coefficients=("dummy", ))

        # run the model
        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,
                 debuglevel=1)

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

        # nobody should choose gridcell 100
        self.assertEqual(ma.allclose(result.sum(), 0, rtol=0), True,
                         "Error: %s is not equal to 0" % (result.sum(), ))
Пример #13
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)
        def run_model_2():
            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 = 8)
            hlcm.run(specification, coefficients, agent_set=households, debuglevel=1)

            # get results
            gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                resources=Resources({"household":households}))
            result_more_attractive = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1)
            result_less_attractive = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1))
            return array([result_more_attractive.sum(), result_less_attractive.sum()])
    def test_agents_do_not_go_to_inferior_locations(self):
        """100 gridcells - 99 with attractiveness 2000, 1 with attractiveness 1, no capacity restrictions
        10,000 households
        We set the coefficient value for attracitiveness to 1.
        """
        storage = StorageFactory().get_storage('dict_storage')

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

        # create gridcells
        storage.write_table(table_name='gridcells',
            table_data = {
                'grid_id': arange(100)+1,
                'attractiveness':array(99*[2000]+[1])
                }
            )
        gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells')

        # create coefficients and specification
        coefficients = Coefficients(names=("attractcoef", ), values=(1,))
        specification = EquationSpecification(variables=("gridcell.attractiveness", ), coefficients=("attractcoef", ))

        # 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, debuglevel=1)

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

        # nobody should choose gridcell 100
        self.assertEqual(result, 0, "Error: %s is not equal to 0" % (result,))
Пример #16
0
        def run_model_2():
            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=8)
            hlcm.run(specification,
                     coefficients,
                     agent_set=households,
                     debuglevel=1)

            # get results
            gridcells.compute_variables(
                ["urbansim.gridcell.number_of_households"],
                resources=Resources({"household": households}))
            result_more_attractive = gridcells.get_attribute_by_id(
                "number_of_households",
                arange(ngcs_attr) + 1)
            result_less_attractive = gridcells.get_attribute_by_id(
                "number_of_households", arange(ngcs_attr + 1, ngcs + 1))
            return array(
                [result_more_attractive.sum(),
                 result_less_attractive.sum()])
Пример #17
0
    def run(self, base_directory, urbansim_cache_directory, years):
        """ run the simulation
                base_directory: directory contains all years folder of lccm.
                urbansim_cache_directory: directory contains all years folder of urbansim cache.
                years: lists of year to run."""
        model = LandCoverChangeModel(self.possible_lcts,
                                     submodel_string=self.lct_attribute,
                                     choice_attribute_name=self.lct_attribute,
                                     debuglevel=4)
        coefficients = Coefficients()
        storage = StorageFactory().get_storage('tab_storage',
                                               storage_location=os.path.join(
                                                   self.package_path, 'data'))
        coefficients.load(in_storage=storage,
                          in_table_name="land_cover_change_model_coefficients")
        specification = EquationSpecification(in_storage=storage)
        specification.load(
            in_table_name="land_cover_change_model_specification")
        specification.set_variable_prefix("biocomplexity.land_cover.")
        constants = Constants()
        simulation_state = SimulationState()
        simulation_state.set_cache_directory(urbansim_cache_directory)
        attribute_cache = AttributeCache()
        index = arange(100000)
        for year in years:
            simulation_state.set_current_time(year)
            #land_cover_path = os.path.join(base_directory, str(year))
            land_cover_path = base_directory
            land_covers = LandCoverDataset(
                in_storage=StorageFactory().get_storage(
                    'flt_storage', storage_location=land_cover_path),
                out_storage=StorageFactory().get_storage(
                    'flt_storage', storage_location=land_cover_path),
                debuglevel=4)
            land_covers.subset_by_index(index)
            #land_covers.load_dataset()
            gridcells = GridcellDataset(in_storage=attribute_cache,
                                        debuglevel=4)

            agents_index = None
            model.run(specification,
                      coefficients,
                      land_covers,
                      data_objects={
                          "gridcell": gridcells,
                          "constants": constants,
                          "flush_variables": True
                      },
                      chunk_specification={'nchunks': 1})
            land_covers.flush_dataset()
            del gridcells
            del land_covers
Пример #18
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_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)
Пример #20
0
 def compute_computed_variables(self, base_directory, urbansim_cache_directory, years):
     
     for year in [years[0]]:
         land_cover_path = os.path.join(base_directory, str(year))
         #print land_cover_path
         land_covers = LandCoverDataset(in_storage=StorageFactory().get_storage('flt_storage', storage_location=land_cover_path),
                                    out_storage=StorageFactory().get_storage('flt_storage', storage_location=land_cover_path))
         land_covers.load_dataset()
         #print land_covers.get_attribute("devgrid_id")
     
         gridcell_path = os.path.join(urbansim_cache_directory)
         gridcells = GridcellDataset(in_storage=StorageFactory().get_storage('flt_storage', storage_location=gridcell_path))
         gridcells.load_dataset()
         #print gridcells.summary()
     
         ## BUG: dataset_pool is not defined
         land_covers.compute_variables(self.land_cover_urbansim_output_variables, 
                                       dataset_pool=dataset_pool)
         land_covers.write_dataset(attributes='*')
         #land_covers.flush_dataset()
         del gridcells
         del land_covers
Пример #21
0
def run_ALCM(niter):
    nhhs = 100
    ngcs = 10
    ngcs_attr = ngcs/2
    ngcs_noattr = ngcs - ngcs_attr
    hh_grid_ids = array(nhhs*[-1])
    
    storage = StorageFactory().get_storage('dict_storage')

    households_table_name = 'households'        
    storage.write_table(
        table_name = households_table_name,
        table_data = {
            'household_id': arange(nhhs)+1, 
            'grid_id': hh_grid_ids
            }
        )
        
    gridcells_table_name = 'gridcells'        
    storage.write_table(
        table_name = gridcells_table_name,
        table_data = {
            'grid_id': arange(ngcs)+1, 
            'cost':array(ngcs_attr*[100]+ngcs_noattr*[1000])
            }
        )

    households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name)
    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', ))
    logger.be_quiet()
    result = zeros((niter,ngcs))
    for iter in range(niter):
        hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, 
                choices = 'opus_core.random_choices_from_index', 
                sampler=None,
                #sample_size_locations = 30
                )
        hlcm.run(specification, coefficients, agent_set=households, debuglevel=1,
                  chunk_specification={'nchunks':1})
        
        # get results
        gridcells.compute_variables(['urbansim.gridcell.number_of_households'],
            resources=Resources({'household':households}))
        result_more_attractive = gridcells.get_attribute_by_id('number_of_households', arange(ngcs_attr)+1)
        result_less_attractive = gridcells.get_attribute_by_id('number_of_households', arange(ngcs_attr+1, ngcs+1))
        households.set_values_of_one_attribute(attribute='grid_id', values=hh_grid_ids)
        gridcells.delete_one_attribute('number_of_households')
        result[iter,:] = concatenate((result_more_attractive, result_less_attractive))
        #print result #, result_more_attractive.sum(), result_less_attractive.sum()
    return result
Пример #22
0
 def compute_computed_variables(self, base_directory, urbansim_cache_directory, years):
     
     for year in [years[0]]:
         land_cover_path = os.path.join(base_directory, str(year))
         #print land_cover_path
         land_covers = LandCoverDataset(in_storage=StorageFactory().get_storage('flt_storage', storage_location=land_cover_path),
                                    out_storage=StorageFactory().get_storage('flt_storage', storage_location=land_cover_path))
         land_covers.load_dataset()
         #print land_covers.get_attribute("devgrid_id")
     
         gridcell_path = os.path.join(urbansim_cache_directory)
         gridcells = GridcellDataset(in_storage=StorageFactory().get_storage('flt_storage', storage_location=gridcell_path))
         gridcells.load_dataset()
         #print gridcells.summary()
     
         ## BUG: dataset_pool is not defined
         land_covers.compute_variables(self.land_cover_urbansim_output_variables, 
                                       dataset_pool=dataset_pool)
         land_covers.write_dataset(attributes='*')
         #land_covers.flush_dataset()
         del gridcells
         del land_covers
    def xtest_gracefully_handle_empty_choice_sets(self):
        storage = StorageFactory().get_storage('dict_storage')

        #create households
        storage.write_table(table_name='households',
            table_data = {
                'household_id': arange(10000)+1,
                'grid_id': array(100*range(100))+1
                }
            )
        households = HouseholdDataset(in_storage=storage, in_table_name='households')

        # create gridcells
        storage.write_table(table_name='gridcells',
            table_data = {
                'grid_id': arange(100)+1,
                'residential_units':array(100*[100])
                }
            )
        gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells')

        # create coefficients and specification
        coefficients = Coefficients(names=("dummy",), values=(0,))
        specification = EquationSpecification(variables=("gridcell.residential_units",), coefficients=("dummy",))

        # run the model
        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, debuglevel=1)

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

        # nobody should choose gridcell 100
        self.assertEqual(ma.allclose(result.sum(), 0 , rtol=0),
                         True, "Error: %s is not equal to 0" % (result.sum(),))
Пример #24
0
    def test_capacity_jobs_model(self):        
        storage = StorageFactory().get_storage('dict_storage')

        jobs_table_name = 'building_types'        
        storage.write_table(
            table_name=jobs_table_name,
            table_data={
                "job_id": arange(200)+1,
                "grid_id":array(10*[1]+20*[2]+10*[3]+160*[-1])
                }
            )
        jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name)
        capacity = array([60, 25, 200])
        gridcells_table_name = 'gridcells'        
        storage.write_table(
            table_name=gridcells_table_name,
            table_data={"grid_id":arange(3)+1,
                        "capacity": capacity}
            )
        gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)
        dataset_pool = DatasetPool(datasets_dict={'job': jobs, 'gridcell':gridcells}, 
                                   package_order=["urbansim", "opus_core"])
        current = gridcells.compute_variables(["urbansim.gridcell.number_of_jobs"], dataset_pool=dataset_pool)
        # run model
        model = CapacityLocationModel(capacity_attribute='gridcell.capacity', 
                                      number_of_agents_attribute="urbansim.gridcell.number_of_jobs",
                                      agents_filter="job.grid_id<0",
                                      dataset_pool=dataset_pool
                                      )
        model.run(gridcells, jobs)
        # get results
        result = gridcells.compute_variables(["urbansim.gridcell.number_of_jobs"], dataset_pool=dataset_pool)
        #logger.log_status((result - current)/float(result.sum()))
        #logger.log_status((capacity - current)/float(capacity.sum()))
        #logger.log_status(result)
        self.assertEqual(result[0] > result[1], True)
        self.assertEqual(result[1] < result[2], True)
        self.assertEqual(result[0] < result[2], 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)
Пример #27
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)
Пример #28
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")
Пример #29
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)
Пример #30
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")
    def __init__(self):
        db_config = DatabaseServerConfiguration(
            host_name=settings.get_db_host_name(),
            user_name=settings.get_db_user_name(),
            password=settings.get_db_password())
        db_server = DatabaseServer(db_config)
        db = db_server.get_database(settings.db)

        in_storage = StorageFactory().get_storage('sql_storage',
                                                  storage_location=db)

        gcs = GridcellDataset(in_storage=in_storage, nchunks=5)
        print "Read and Write GridcellDataset."
        out_storage = StorageFactory().build_storage_for_dataset(
            type='flt_storage', storage_location=settings.dir)
        ReadWriteADataset(gcs,
                          out_storage=out_storage,
                          out_table_name=settings.gcsubdir)
Пример #32
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)
Пример #33
0
    def test_do_nothing_if_empty_set(self):
        storage = StorageFactory().get_storage('dict_storage')

        gridcell_set_table_name = 'gridcell_set'
        storage.write_table(table_name=gridcell_set_table_name,
                            table_data={"grid_id": array([], dtype='int32')})

        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 = HousingPriceModel(debuglevel=4)
        lp.filter_attribute = None
        result = lp.run(specification, coefficients, gridcell_set)
        self.assertEqual(result.size, 0)
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())
Пример #35
0
    def get_resources(self, data_dictionary, dataset):
        """Create resources for computing a variable. """
        resources = Resources()

        for key in data_dictionary.keys():
            if key in self.datasets:
                data = data_dictionary[key]

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

                if self.id_names[key] not in data_dictionary[key].keys(
                ) and not isinstance(self.id_names[key], list):
                    data[self.id_names[key]] = arange(
                        1,
                        len(data_dictionary[key][data_dictionary[key].keys()
                                                 [0]]) + 1)  # add id array

                id_name = self.id_names[key]
                storage.write_table(table_name='data', table_data=data)

                if key == "gridcell":
                    gc = GridcellDataset(in_storage=storage,
                                         in_table_name='data')

                    # add relative_x and relative_y
                    gc.get_id_attribute()
                    n = int(ceil(sqrt(gc.size())))
                    if "relative_x" not in data.keys():
                        x = (indices((n, n)) + 1)[1].ravel()
                        gc.add_attribute(x[0:gc.size()],
                                         "relative_x",
                                         metadata=1)
                    if "relative_y" not in data.keys():
                        y = (indices((n, n)) + 1)[0].ravel()
                        gc.add_attribute(y[0:gc.size()],
                                         "relative_y",
                                         metadata=1)
                    resources.merge({key: gc})

                elif key == "household":
                    resources.merge({
                        key:
                        HouseholdDataset(in_storage=storage,
                                         in_table_name='data')
                    })
                elif key == "development_project":
                    resources.merge({
                        key:
                        DevelopmentProjectDataset(in_storage=storage,
                                                  in_table_name='data')
                    })
                elif key == "development_event":
                    resources.merge({
                        key:
                        DevelopmentEventDataset(in_storage=storage,
                                                in_table_name='data')
                    })
                elif key == "neighborhood":
                    resources.merge({
                        key:
                        NeighborhoodDataset(in_storage=storage,
                                            in_table_name='data')
                    })
                elif key == "job":
                    resources.merge({
                        key:
                        JobDataset(in_storage=storage, in_table_name='data')
                    })
                elif key == "zone":
                    resources.merge({
                        key:
                        ZoneDataset(in_storage=storage, in_table_name='data')
                    })
                elif key == "travel_data":
                    resources.merge({
                        key:
                        TravelDataDataset(in_storage=storage,
                                          in_table_name='data')
                    })
                elif key == "faz":
                    resources.merge({
                        key:
                        FazDataset(in_storage=storage, in_table_name='data')
                    })
                elif key == "fazdistrict":
                    resources.merge({
                        key:
                        FazdistrictDataset(in_storage=storage,
                                           in_table_name='data')
                    })
                elif key == "race":
                    resources.merge({
                        key:
                        RaceDataset(in_storage=storage, in_table_name='data')
                    })
                elif key == "county":
                    resources.merge({
                        key:
                        CountyDataset(in_storage=storage, in_table_name='data')
                    })
                elif key == "large_area":
                    resources.merge({
                        key:
                        LargeAreaDataset(in_storage=storage,
                                         in_table_name='data')
                    })
                elif key == "development_group":
                    resources.merge({
                        key:
                        DevelopmentGroupDataset(in_storage=storage,
                                                in_table_name='data')
                    })
                elif key == "employment_sector_group":
                    resources.merge({
                        key:
                        EmploymentSectorGroupDataset(in_storage=storage,
                                                     in_table_name='data')
                    })
                elif key == "plan_type_group":
                    resources.merge({
                        key:
                        PlanTypeGroupDataset(in_storage=storage,
                                             in_table_name='data')
                    })
                elif key == "building":
                    resources.merge({
                        key:
                        BuildingDataset(in_storage=storage,
                                        in_table_name='data')
                    })

            else:
                resources.merge({key: data_dictionary[key]})

        if dataset in self.interactions:
            if dataset == "household_x_gridcell":
                resources.merge({
                    "dataset":
                    HouseholdXGridcellDataset(dataset1=resources["household"],
                                              dataset2=resources["gridcell"])
                })
            if dataset == "job_x_gridcell":
                resources.merge({
                    "dataset":
                    JobXGridcellDataset(dataset1=resources["job"],
                                        dataset2=resources["gridcell"])
                })
            if dataset == "household_x_zone":
                resources.merge({
                    "dataset":
                    HouseholdXZoneDataset(dataset1=resources["household"],
                                          dataset2=resources["zone"])
                })
            if dataset == "household_x_neighborhood":
                resources.merge({
                    "dataset":
                    HouseholdXNeighborhoodDataset(
                        dataset1=resources["household"],
                        dataset2=resources["neighborhood"])
                })
            if dataset == "development_project_x_gridcell":
                resources.merge({
                    "dataset":
                    DevelopmentProjectXGridcellDataset(
                        dataset1=resources["development_project"],
                        dataset2=resources["gridcell"])
                })

        else:
            resources.merge({"dataset": resources[dataset]})
        resources.merge({"check_variables": '*', "debug": 4})
        return resources
    def test_stochastic_test_case(self):
        """100 gridcells - 50 with cost 100, 50 with cost 1000, no capacity restrictions
        10,000 households
        We set the coefficient value for cost -0.001. This leads to probability
        proportion 0.71 (less costly gridcells) to 0.29 (expensive gridcells)
        (derived from the logit formula)
        """
        storage = StorageFactory().get_storage('dict_storage')

        nhouseholds = 10000
        ngrids = 10

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

        # create gridcells
        storage.write_table(table_name='gridcells',
                            table_data={
                                'grid_id':
                                arange(ngrids) + 1,
                                'cost':
                                array(ngrids / 2 * [100] + ngrids / 2 * [1000])
                            })
        gridcells = GridcellDataset(in_storage=storage,
                                    in_table_name='gridcells')

        # 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", sampler=None)
        #sample_size_locations = 30)

        # check the individual gridcells
        # This is a stochastic model, so it may legitimately fail occassionally.
        success = []

        def inner_loop():
            hlcm.run(specification,
                     coefficients,
                     agent_set=households,
                     debuglevel=1,
                     chunk_specification={'nchunks': 1})

            # get results
            gridcells.compute_variables(
                ["urbansim.gridcell.number_of_households"],
                resources=Resources({"household": households}))
            result_more_attractive = gridcells.get_attribute_by_id(
                "number_of_households",
                arange(ngrids / 2) + 1)
            return result_more_attractive

        expected_results = array(ngrids / 2 *
                                 [nhouseholds * 0.71 / (ngrids / 2)])
        self.run_stochastic_test(__file__,
                                 inner_loop,
                                 expected_results,
                                 10,
                                 type="pearson",
                                 transformation=None)

        # Make sure it fails when expected distribution is different from actual.
        expected_results = array(ngrids / 2 *
                                 [nhouseholds * 0.61 / (ngrids / 2)])
        try:
            self.run_stochastic_test(__file__,
                                     inner_loop,
                                     expected_results,
                                     10,
                                     type="poisson",
                                     transformation=None)
        except AssertionError:
            pass
Пример #37
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])))
Пример #38
0
##########
# agents from householdset.tab
agents = HouseholdDataset(in_storage=StorageFactory().get_storage(
    'tab_storage', storage_location='.'),
                          in_table_name="householdset",
                          id_name="agent_id")

agents.summary()
agents.get_attribute("income")
agents.plot_histogram("income", bins=10)
agents.r_histogram("income")
agents.r_scatter("income", "persons")

# gridcells from PSRC
locations_psrc = GridcellDataset(in_storage=StorageFactory().get_storage(
    'flt_storage', storage_location="/home/hana/bandera/urbansim/data/GPSRC"),
                                 in_table_name="gc")
locations_psrc.summary()
locations_psrc.plot_histogram("distance_to_highway", bins=15)
locations_psrc.r_image("distance_to_highway")
locations_psrc.plot_map("distance_to_highway")

locations_psrc.compute_variables("urbansim.gridcell.ln_total_land_value")
locations_psrc.plot_map("ln_total_land_value")

# Models
########

#HLCM

# locations from gridcellset.tab
    def setUp( self ):
        """here, we simulate 50 residential units
        and 5000 commercial, industrial, and governmental sqft added to each of the gridcells in previous years.
        """

        ### TODO: do not redefine these constants.
        self.comc = 1
        self.indc = 3
        self.govc = 2
        self.sfhc = 4
        self.mfhc = 5

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

        gridcells_table_name = 'gridcells'
#            create 100 gridcells, each with 200 residential units and space for 100 commercial jobs,
#            100 industrial jobs, and residential, industrial, and commercial value at $500,000 each
        storage.write_table(
            table_name=gridcells_table_name,
            table_data={
                "grid_id": arange( 1, 100+1 ),
                "commercial_sqft_per_job":array( 100*[100] ),
                "industrial_sqft_per_job":array( 100*[100] ),
                "single_family_improvement_value":array( 100*[500000] ),
                "commercial_improvement_value":array( 100*[500000] ),
                "industrial_improvement_value":array( 100*[500000] )
                }
            )
        self.gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)

        buildings_table_name = 'buildings'
#            2000 buildings (1000 with 20 residential units each, 500 with 20 commercial job and 500 with 20 industrial job each)
        storage.write_table(
            table_name=buildings_table_name,
            table_data={
                "building_id":arange( 1, 2000+1 ), # 2000 buildings
                "grid_id":array( 20*range( 1, 100+1 ), dtype=int32 ), # spread evenly across 100 gridcells
                "building_type_id":array(1000*[self.sfhc] +
                                         500*[self.comc] +
                                         500*[self.indc], dtype=int8),
                "sqft": array(1000*[0] +
                              500*[2000] +
                              500*[2000], dtype=int32),
                "residential_units": array(1000*[20] +
                                           500* [0] +
                                           500* [0], dtype=int32),
                "improvement_value": array(1000*[50] +
                                           500* [50] +
                                           500* [50], dtype=float32),
                "year_built": array(1000*[1940] +
                                    500* [1940] +
                                    500* [1940], dtype=int32)
                }
            )
        self.buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name)

        households_table_name = 'households'
#            create 10000 households, 100 in each of the 100 gridcells.
#            there will initially be 100 vacant residential units in each gridcell then.
        storage.write_table(
            table_name=households_table_name,
            table_data={
                "household_id":arange( 1, 10000+1 ),
                "grid_id":array( 100*range( 1, 100+1 ), dtype=int32 )
                }
            )
        self.households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name)

        building_types_table_name = 'building_types'
        storage.write_table(
            table_name=building_types_table_name,
            table_data={
                "building_type_id":array([self.govc,self.comc,self.indc, self.sfhc, self.mfhc], dtype=int8),
                "name": array(["governmental", "commercial", "industrial", "single_family","multiple_family"]),
                "units": array(["governmental_sqft", "commercial_sqft", "industrial_sqft", "residential_units", "residential_units"]),
                "is_residential": array([0,0,0,1,1], dtype='?')
                }
            )
        self.building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name)

        job_building_types_table_name = 'job_building_types'
        storage.write_table(
            table_name=job_building_types_table_name,
            table_data={
                "id":array([self.govc,self.comc,self.indc, self.sfhc, self.mfhc], dtype=int8),
                "name": array(["governmental", "commercial", "industrial", "single_family","multiple_family"])
                }
            )
        self.job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name=job_building_types_table_name)

        jobs_table_name = 'jobs'
#            create 2500 commercial jobs and distribute them equally across the 100 gridcells,
#            25 commercial buildings/gridcell
        storage.write_table(
            table_name=jobs_table_name,
            table_data={
                "job_id":arange( 1, 2500+1 ),
                "grid_id":array( 25*range( 1, 100+1 ), dtype=int32 ),
                "sector_id":array( 2500*[1], dtype=int32 ),
                "building_type":array(2500*[self.comc], dtype=int8)
                }
            )
        self.jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name)

        self.dataset_pool = DatasetPool()
        self.dataset_pool.add_datasets_if_not_included({
                                            "household":self.households,
                                            "job":self.jobs,
                                            "building":self.buildings,
                                            "building_type": self.building_types,
                                            "job_building_type": self.job_building_types})

        self.building_categories = {'commercial': array([1000,5000]),
                                    'industrial': array([500,800,1000])}
    def test_place_agents_to_correct_areas(self):
        """10 gridcells - 5 in area 1, 5 in area 2, with equal cost, no capacity restrictions
        100 households - 70 live in area 1, 30 live in area 2.
        We set the coefficient value for cost -0.001. 
        """
        storage = StorageFactory().get_storage('dict_storage')

        nhhs = 100
        ngcs = 10
        ngcs_attr = ngcs/2
        hh_grid_ids = array(nhhs*[-1])
        lareas = array(ngcs_attr*[1] + ngcs_attr*[2])
        hh_lareas = array(70*[1] + 30*[2])
        
        household_data = {
            'household_id': arange(nhhs)+1,
            'grid_id': hh_grid_ids,
            'faz_id': hh_lareas
            }

        gridcell_data = {
            'grid_id': arange(ngcs)+1,
            'cost':array(ngcs*[100]),
            'faz_id': lareas            
            }

        storage.write_table(table_name = 'households', table_data = household_data)
        storage.write_table(table_name = 'gridcells', table_data = gridcell_data)

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

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

        # check the individual gridcells
        def run_model():
            households = HouseholdDataset(in_storage=storage, in_table_name='households')
            hlcm = SubareaHouseholdLocationChoiceModel(location_set=gridcells, compute_capacity_flag=False,
                    choices = "opus_core.random_choices_from_index", sample_size_locations = 4, subarea_id_name="faz_id")
            hlcm.run(specification, coefficients, agent_set=households, debuglevel=1)

            # get results
            gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                resources=Resources({"household":households}))
            result_area1 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1)
            result_area2 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1))
            gridcells.delete_one_attribute("number_of_households")
            result = concatenate((result_area1, result_area2))
            return result

        expected_results = array(ngcs_attr*[nhhs*0.7/float(ngcs_attr)] + ngcs_attr*[nhhs*0.3/float(ngcs_attr)])

        self.run_stochastic_test(__file__, run_model, expected_results, 10)

        # check the exact sum 
        hlcm = SubareaHouseholdLocationChoiceModel(location_set=gridcells, compute_capacity_flag=False,
                    choices = "opus_core.random_choices_from_index", sample_size_locations = 4, subarea_id_name="faz_id")
        hlcm.run(specification, coefficients, agent_set=households, debuglevel=1)
        gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                resources=Resources({"household":households}))
        result_area1 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1).sum()
        result_area2 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1)).sum()
        results =  array([result_area1, result_area2])

        expected_results = array([70, 30])
        self.assertEqual(ma.allequal(expected_results, results), True, "Error, should_be: %s, but result: %s" % (
                                                                                             expected_results, results))
    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")
Пример #42
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 run(self, base_directory, urbansim_cache_directory, years, output_directory, temp_folder,
            coefficients_name, specification_name, convert_flt=True, convert_input=False):
        """ run the simulation
                base_directory: directory contains all years folder of lccm.
                urbansim_cache_directory: directory contains all years folder of urbansim cache.
                years: lists of year to run."""
        model = LandCoverChangeModel(self.possible_lcts, submodel_string=self.lct_attribute, 
                                     choice_attribute_name=self.lct_attribute, debuglevel=4)
        coefficients = Coefficients()
        storage = StorageFactory().get_storage('tab_storage', 
            storage_location=os.path.join(self.package_path, 'data'))
        coefficients.load(in_storage=storage, in_table_name=coefficients_name)
        specification = EquationSpecification(in_storage=storage)
        specification.load(in_table_name=specification_name)
        specification.set_variable_prefix("biocomplexity.land_cover.")
        constants = Constants()
        simulation_state = SimulationState()
        simulation_state.set_cache_directory(urbansim_cache_directory)
        attribute_cache = AttributeCache()
        SessionConfiguration(new_instance=True,
                             package_order=['biocomplexity', 'urbansim', 'opus_core'],
                             in_storage=AttributeCache())
                
        ncols = LccmConfiguration.ncols        
        
        if temp_folder is None:
            self.temp_land_cover_dir = tempfile.mkdtemp()
        else:
            self.temp_land_cover_dir = temp_folder
        
        for year in years:
            land_cover_path = self._generate_input_land_cover(year, base_directory, urbansim_cache_directory, 
                                                              years, output_directory, convert_flt, convert_input)
            #max_size = 174338406 (orig) - act. int: 19019944 (37632028 incl NoData)
            max_size = self._get_max_index(land_cover_path) # 1st instance of lc_dataset - but looks like a 'lite' version
            offset = min(LccmConfiguration.offset, max_size)
            s = 0
            t = offset
            while (s < t and t <= max_size):
                logger.log_status("Offset: ", s, t)
                index = arange(s,t)
                
                land_cover_cache_path=os.path.join(urbansim_cache_directory,str(year),'land_covers')
                self._clean_up_land_cover_cache(land_cover_cache_path)
                
                simulation_state.set_current_time(year)
                
                # 2nd instance of lc_dataset
                land_covers = LandCoverDataset(in_storage=StorageFactory().get_storage('flt_storage', storage_location=land_cover_path),
                                           out_storage=StorageFactory().get_storage('flt_storage', storage_location=land_cover_path),
                                           debuglevel=4)
                land_covers.subset_by_index(index)
#                land_covers.load_dataset()
                gridcells = GridcellDataset(in_storage=attribute_cache, debuglevel=4)

                agents_index = None
                model.run(specification, coefficients, land_covers, data_objects={"gridcell":gridcells,
                              "constants":constants, "flush_variables":True},
                              chunk_specification = {'nchunks':5}) ## chunk size set here
                land_covers.flush_dataset()
                del gridcells
                del land_covers

#                self._generate_output_flt(year, urbansim_cache_directory, output_directory, convert_flt)
                self._generate_output_flt2(year, urbansim_cache_directory, output_directory, convert_flt)
                
                if t >= max_size: break
                s = max(t-10*ncols,s)
                t = min(t+offset-10*ncols,max_size)
                
        # clean up temp storage after done simulation
        shutil.rmtree(self.temp_land_cover_dir)
    def get_resources(self, data_dictionary, dataset):
        """Create resources for computing a variable. """
        resources=Resources()
        
        for key in data_dictionary.keys():
            if key in self.datasets:
                data = data_dictionary[key]
                
                storage = StorageFactory().get_storage('dict_storage')
                
                if self.id_names[key] not in data_dictionary[key].keys() and not isinstance(self.id_names[key], list):
                    data[self.id_names[key]] = arange(1, len(data_dictionary[key][data_dictionary[key].keys()[0]])+1) # add id array
                
                id_name = self.id_names[key]
                storage.write_table(table_name = 'data', table_data = data)
                
                if key == "gridcell":
                    gc = GridcellDataset(in_storage=storage, in_table_name='data')
                    
                    # add relative_x and relative_y
                    gc.get_id_attribute()
                    n = int(ceil(sqrt(gc.size())))
                    if "relative_x" not in data.keys():
                        x = (indices((n,n))+1)[1].ravel()
                        gc.add_attribute(x[0:gc.size()], "relative_x", metadata=1)
                    if "relative_y" not in data.keys():
                        y = (indices((n,n))+1)[0].ravel()
                        gc.add_attribute(y[0:gc.size()], "relative_y", metadata=1)
                    resources.merge({key: gc})
                
                elif key == "household":
                    resources.merge({key: HouseholdDataset(in_storage=storage, in_table_name='data')})
                elif key == "development_project":
                    resources.merge({key: DevelopmentProjectDataset(in_storage=storage, in_table_name='data')})
                elif key == "development_event":
                    resources.merge({key: DevelopmentEventDataset(in_storage=storage, in_table_name='data')})   
                elif key == "neighborhood":
                    resources.merge({key: NeighborhoodDataset(in_storage=storage, in_table_name='data')})
                elif key == "job":
                    resources.merge({key: JobDataset(in_storage=storage, in_table_name='data')})                    
                elif key == "zone":
                    resources.merge({key: ZoneDataset(in_storage=storage, in_table_name='data')})
                elif key == "travel_data":
                    resources.merge({key: TravelDataDataset(in_storage=storage, in_table_name='data')})
                elif key == "faz":
                    resources.merge({key: FazDataset(in_storage=storage, in_table_name='data')})
                elif key == "fazdistrict":
                    resources.merge({key: FazdistrictDataset(in_storage=storage, in_table_name='data')})                    
                elif key == "race":
                    resources.merge({key: RaceDataset(in_storage=storage, in_table_name='data')})
                elif key == "county":
                    resources.merge({key: CountyDataset(in_storage=storage, in_table_name='data')})
                elif key == "large_area":
                    resources.merge({key: LargeAreaDataset(in_storage=storage, in_table_name='data')})
                elif key == "development_group":
                    resources.merge({key: DevelopmentGroupDataset(in_storage=storage, in_table_name='data')})
                elif key == "employment_sector_group":
                    resources.merge({key: EmploymentSectorGroupDataset(in_storage=storage, in_table_name='data')})        
                elif key == "plan_type_group":
                    resources.merge({key: PlanTypeGroupDataset(in_storage=storage, in_table_name='data')})
                elif key == "building":
                    resources.merge({key: BuildingDataset(in_storage=storage, in_table_name='data')})
                    
            else:
                resources.merge({key:data_dictionary[key]})

        if dataset in self.interactions:
            if dataset == "household_x_gridcell": 
                resources.merge({"dataset": HouseholdXGridcellDataset(dataset1=resources["household"], dataset2=resources["gridcell"])})
            if dataset == "job_x_gridcell":
                resources.merge({"dataset": JobXGridcellDataset(dataset1=resources["job"], dataset2=resources["gridcell"])})
            if dataset == "household_x_zone":
                resources.merge({"dataset": HouseholdXZoneDataset(dataset1=resources["household"], dataset2=resources["zone"])})
            if dataset == "household_x_neighborhood":
                resources.merge({"dataset": HouseholdXNeighborhoodDataset(dataset1=resources["household"], dataset2=resources["neighborhood"])})
            if dataset == "development_project_x_gridcell":
                resources.merge({"dataset": DevelopmentProjectXGridcellDataset(dataset1=resources["development_project"], dataset2=resources["gridcell"])})

        else:
            resources.merge({"dataset": resources[dataset]})
        resources.merge({"check_variables":'*', "debug":4})
        return resources
Пример #45
0
def run_ALCM(niter):
    nhhs = 100
    ngcs = 10
    ngcs_attr = ngcs / 2
    ngcs_noattr = ngcs - ngcs_attr
    hh_grid_ids = array(nhhs * [-1])

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

    households_table_name = 'households'
    storage.write_table(table_name=households_table_name,
                        table_data={
                            'household_id': arange(nhhs) + 1,
                            'grid_id': hh_grid_ids
                        })

    gridcells_table_name = 'gridcells'
    storage.write_table(table_name=gridcells_table_name,
                        table_data={
                            'grid_id': arange(ngcs) + 1,
                            'cost':
                            array(ngcs_attr * [100] + ngcs_noattr * [1000])
                        })

    households = HouseholdDataset(in_storage=storage,
                                  in_table_name=households_table_name)
    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', ))
    logger.be_quiet()
    result = zeros((niter, ngcs))
    for iter in range(niter):
        hlcm = HouseholdLocationChoiceModelCreator().get_model(
            location_set=gridcells,
            compute_capacity_flag=False,
            choices='opus_core.random_choices_from_index',
            sampler=None,
            #sample_size_locations = 30
        )
        hlcm.run(specification,
                 coefficients,
                 agent_set=households,
                 debuglevel=1,
                 chunk_specification={'nchunks': 1})

        # get results
        gridcells.compute_variables(['urbansim.gridcell.number_of_households'],
                                    resources=Resources(
                                        {'household': households}))
        result_more_attractive = gridcells.get_attribute_by_id(
            'number_of_households',
            arange(ngcs_attr) + 1)
        result_less_attractive = gridcells.get_attribute_by_id(
            'number_of_households', arange(ngcs_attr + 1, ngcs + 1))
        households.set_values_of_one_attribute(attribute='grid_id',
                                               values=hh_grid_ids)
        gridcells.delete_one_attribute('number_of_households')
        result[iter, :] = concatenate(
            (result_more_attractive, result_less_attractive))
        #print result #, result_more_attractive.sum(), result_less_attractive.sum()
    return result
Пример #46
0
    def get_resources(self, data_dictionary, dataset):
        """Create resources for computing a variable. """
        resources = Resources()
        for key in data_dictionary.keys():
            if key in self.datasets:
                data = data_dictionary[key]
                if self.id_names[key] not in data_dictionary[key].keys(
                ) and not isinstance(self.id_names[key], list):

                    data[self.id_names[key]] = arange(1,\
                        len(data_dictionary[key][data_dictionary[key].keys()[0]])+1) # add id array

                if key == "land_cover":
                    land_cover_storage = StorageFactory().get_storage(
                        'dict_storage')
                    land_cover_table_name = 'land_cover'
                    land_cover_storage.write_table(
                        table_name=land_cover_table_name,
                        table_data=data,
                    )

                    lc = LandCoverDataset(
                        in_storage=land_cover_storage,
                        in_table_name=land_cover_table_name,
                    )

                    # add relative_x and relative_y
                    lc.get_id_attribute()
                    n = int(ceil(sqrt(lc.size())))

                    if "relative_x" not in data.keys():
                        x = (indices((n, n)) + 1)[1].ravel()
                        lc.add_attribute(x[0:lc.size()],
                                         "relative_x",
                                         metadata=1)
                    if "relative_y" not in data.keys():
                        y = (indices((n, n)) + 1)[0].ravel()
                        lc.add_attribute(y[0:lc.size()],
                                         "relative_y",
                                         metadata=1)

                    resources.merge({key: lc})

                if key == "gridcell":
                    gridcell_storage = StorageFactory().get_storage(
                        'dict_storage')
                    gridcell_table_name = 'gridcell'
                    gridcell_storage.write_table(
                        table_name=gridcell_table_name,
                        table_data=data,
                    )

                    gridcell_dataset = GridcellDataset(
                        in_storage=gridcell_storage,
                        in_table_name=gridcell_table_name,
                    )

                    resources.merge({key: gridcell_dataset})
            else:
                resources.merge({key: data_dictionary[key]})

        if dataset in self.interactions:
            pass
        else:
            resources.merge({"dataset": resources[dataset]})
        resources.merge({"check_variables": '*', "debug": 4})
        return resources
Пример #47
0
    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_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)
Пример #49
0
    def test_agents_go_to_attractive_locations(self):
        """10 gridcells - 5 with cost 100, 5 with cost 1000, no capacity restrictions
        100 households
        We set the coefficient value for cost -0.001. This leads to probability
        proportion 0.71 (less costly gridcells) to 0.29 (expensive gridcells)
        (derived from the logit formula)
        """
        storage = StorageFactory().get_storage('dict_storage')

        nhhs = 100
        ngcs = 10
        ngcs_attr = ngcs / 2
        ngcs_noattr = ngcs - ngcs_attr
        hh_grid_ids = array(nhhs * [-1])

        household_data = {
            'household_id': arange(nhhs) + 1,
            'grid_id': hh_grid_ids
        }

        gridcell_data = {
            'grid_id': arange(ngcs) + 1,
            'cost': array(ngcs_attr * [100] + ngcs_noattr * [1000])
        }

        storage.write_table(table_name='households', table_data=household_data)
        storage.write_table(table_name='gridcells', table_data=gridcell_data)

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

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

        # check the individual gridcells
        def run_model():
            hlcm = HouseholdLocationChoiceModelCreator().get_model(
                location_set=gridcells,
                compute_capacity_flag=False,
                choices="opus_core.random_choices_from_index",
                sample_size_locations=8)
            hlcm.run(specification,
                     coefficients,
                     agent_set=households,
                     debuglevel=1)

            # get results
            gridcells.compute_variables(
                ["urbansim.gridcell.number_of_households"],
                resources=Resources({"household": households}))
            result_more_attractive = gridcells.get_attribute_by_id(
                "number_of_households",
                arange(ngcs_attr) + 1)
            result_less_attractive = gridcells.get_attribute_by_id(
                "number_of_households", arange(ngcs_attr + 1, ngcs + 1))
            households.set_values_of_one_attribute(attribute="grid_id",
                                                   values=hh_grid_ids)
            gridcells.delete_one_attribute("number_of_households")
            result = concatenate(
                (result_more_attractive, result_less_attractive))
            return result

        expected_results = array(ngcs_attr * [nhhs * 0.71 / float(ngcs_attr)] +
                                 ngcs_noattr *
                                 [nhhs * 0.29 / float(ngcs_noattr)])

        self.run_stochastic_test(__file__, run_model, expected_results, 10)

        def run_model_2():
            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=8)
            hlcm.run(specification,
                     coefficients,
                     agent_set=households,
                     debuglevel=1)

            # get results
            gridcells.compute_variables(
                ["urbansim.gridcell.number_of_households"],
                resources=Resources({"household": households}))
            result_more_attractive = gridcells.get_attribute_by_id(
                "number_of_households",
                arange(ngcs_attr) + 1)
            result_less_attractive = gridcells.get_attribute_by_id(
                "number_of_households", arange(ngcs_attr + 1, ngcs + 1))
            return array(
                [result_more_attractive.sum(),
                 result_less_attractive.sum()])

        expected_results = array([nhhs * 0.71, nhhs * 0.29])
        self.run_stochastic_test(__file__, run_model_2, expected_results, 10)
Пример #50
0
    def test_place_agents_to_correct_areas(self):
        """10 gridcells - 5 in area 1, 5 in area 2, with equal cost, no capacity restrictions
        100 households - 70 live in area 1, 30 live in area 2.
        We set the coefficient value for cost -0.001. 
        """
        storage = StorageFactory().get_storage('dict_storage')

        nhhs = 100
        ngcs = 10
        ngcs_attr = ngcs/2
        hh_grid_ids = array(nhhs*[-1])
        lareas = array(ngcs_attr*[1] + ngcs_attr*[2])
        hh_lareas = array(70*[1] + 30*[2])
        
        household_data = {
            'household_id': arange(nhhs)+1,
            'grid_id': hh_grid_ids,
            'faz_id': hh_lareas
            }

        gridcell_data = {
            'grid_id': arange(ngcs)+1,
            'cost':array(ngcs*[100]),
            'faz_id': lareas            
            }

        storage.write_table(table_name = 'households', table_data = household_data)
        storage.write_table(table_name = 'gridcells', table_data = gridcell_data)

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

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

        # check the individual gridcells
        def run_model():
            households = HouseholdDataset(in_storage=storage, in_table_name='households')
            hlcm = SubareaHouseholdLocationChoiceModel(location_set=gridcells, compute_capacity_flag=False,
                    choices = "opus_core.random_choices_from_index", sample_size_locations = 4, subarea_id_name="faz_id")
            hlcm.run(specification, coefficients, agent_set=households, debuglevel=1)

            # get results
            gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                resources=Resources({"household":households}))
            result_area1 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1)
            result_area2 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1))
            gridcells.delete_one_attribute("number_of_households")
            result = concatenate((result_area1, result_area2))
            return result

        expected_results = array(ngcs_attr*[nhhs*0.7/float(ngcs_attr)] + ngcs_attr*[nhhs*0.3/float(ngcs_attr)])

        self.run_stochastic_test(__file__, run_model, expected_results, 10)

        # check the exact sum 
        hlcm = SubareaHouseholdLocationChoiceModel(location_set=gridcells, compute_capacity_flag=False,
                    choices = "opus_core.random_choices_from_index", sample_size_locations = 4, subarea_id_name="faz_id")
        hlcm.run(specification, coefficients, agent_set=households, debuglevel=1)
        gridcells.compute_variables(["urbansim.gridcell.number_of_households"],
                resources=Resources({"household":households}))
        result_area1 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1).sum()
        result_area2 = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1)).sum()
        results =  array([result_area1, result_area2])

        expected_results = array([70, 30])
        self.assertEqual(ma.allequal(expected_results, results), True, "Error, should_be: %s, but result: %s" % (
                                                                                             expected_results, results))
    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])))
Пример #52
0
# Datasets
##########
# agents from householdset.tab
agents = HouseholdDataset(in_storage = StorageFactory().get_storage('tab_storage', storage_location='.'),
                      in_table_name = "householdset", id_name="agent_id")

agents.summary()
agents.get_attribute("income")
agents.plot_histogram("income", bins = 10)
agents.r_histogram("income")
agents.r_scatter("income", "persons")

# gridcells from PSRC
locations_psrc = GridcellDataset(in_storage = StorageFactory().get_storage('flt_storage', 
        storage_location = "/home/hana/bandera/urbansim/data/GPSRC"), 
    in_table_name = "gc")
locations_psrc.summary()
locations_psrc.plot_histogram("distance_to_highway", bins = 15)
locations_psrc.r_image("distance_to_highway")
locations_psrc.plot_map("distance_to_highway")

locations_psrc.compute_variables("urbansim.gridcell.ln_total_land_value")
locations_psrc.plot_map("ln_total_land_value")

# Models
########

#HLCM

# locations from gridcellset.tab
    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])))