Esempio n. 1
0
    def prepare_for_estimate(self,
                             specification_dict=None,
                             specification_storage=None,
                             specification_table=None,
                             events_for_estimation_storage=None,
                             events_for_estimation_table=None,
                             agents_filter='',
                             compute_variables=[],
                             data_objects={}):

        specification = get_specification_for_estimation(
            specification_dict, specification_storage, specification_table)
        projects = None
        # create agents for estimation
        if events_for_estimation_storage is not None:
            projects = Dataset(in_storage=events_for_estimation_storage,
                               in_table_name=events_for_estimation_table,
                               id_name=[],
                               dataset_name='development_project')
            if compute_variables:
                projects.compute_variables(compute_variables,
                                           resources=Resources(data_objects))
                # needs to be a primary attribute because of the join method below
                #projects.add_primary_attribute(estimation_set.get_attribute(location_id_variable),
                #                               VariableName(location_id_variable).get_alias())

            if agents_filter:
                values = projects.compute_variables(
                    agents_filter, resources=Resources(data_objects))
                index = where(values > 0)[0]
                projects.subset_by_index(index,
                                         flush_attributes_if_not_loaded=False)

        return (specification, projects)
    def prepare_for_estimate(self, specification_dict = None, 
                             specification_storage=None,
                             specification_table=None,
                             events_for_estimation_storage=None,
                             events_for_estimation_table=None, 
                             agents_filter='',
                             compute_variables=[],
                             data_objects={}):

        specification = get_specification_for_estimation(specification_dict,
                                                          specification_storage,
                                                          specification_table)
        projects = None
        # create agents for estimation
        if events_for_estimation_storage is not None:
            projects = Dataset(in_storage = events_for_estimation_storage,
                               in_table_name= events_for_estimation_table,
                               id_name=[], dataset_name='development_project'
                               )
            if compute_variables:
                projects.compute_variables(compute_variables, resources=Resources(data_objects))
                # needs to be a primary attribute because of the join method below
                #projects.add_primary_attribute(estimation_set.get_attribute(location_id_variable), 
                #                               VariableName(location_id_variable).get_alias())
            
            if agents_filter:
                values = projects.compute_variables(agents_filter, resources=Resources(data_objects))
                index = where(values > 0)[0]
                projects.subset_by_index(index, flush_attributes_if_not_loaded=False)
                
        return (specification, projects)
Esempio n. 3
0
    def prepare_for_estimate(self,
                             specification_dict=None,
                             specification_storage=None,
                             specification_table=None,
                             events_for_estimation_storage=None,
                             events_for_estimation_table=None,
                             urbansim_constant=None,
                             base_year=0,
                             categories=None):

        from opus_core.model import get_specification_for_estimation
        specification = get_specification_for_estimation(
            specification_dict, specification_storage, specification_table)
        projects = None
        # create agents for estimation
        if events_for_estimation_storage is not None:
            event_set = DevelopmentEventDataset(
                in_storage=events_for_estimation_storage,
                in_table_name=events_for_estimation_table)
            event_set.remove_non_recent_data(base_year,
                                             urbansim_constant['recent_years'])
            projects = DevelopmentProjectCreator(
            ).create_projects_from_history(event_set, self.project_type,
                                           self.units, categories)
        return (specification, projects)
Esempio n. 4
0
 def prepare_for_estimate(self, 
                          specification_dict = None, 
                          specification_storage=None, 
                          specification_table=None):
     from opus_core.model import get_specification_for_estimation
     return get_specification_for_estimation(specification_dict, 
                                             specification_storage, 
                                             specification_table)
 def prepare_for_estimate(self, dataset=None, dataset_filter=None, filter_threshold=0, **kwargs):
     spec = get_specification_for_estimation(**kwargs)
     if (dataset is not None) and (dataset_filter is not None):
         filter_values = dataset.compute_variables([dataset_filter], dataset_pool=self.dataset_pool)
         index = where(filter_values > filter_threshold)[0]
     else:
         index = None
     return (spec, index)
 def prepare_for_estimate(self, 
                          specification_dict = None, 
                          specification_storage=None, 
                          specification_table=None):
     from opus_core.model import get_specification_for_estimation
     return get_specification_for_estimation(specification_dict, 
                                             specification_storage, 
                                             specification_table)
Esempio n. 7
0
    def prepare_for_estimate(self,
                             specification_dict=None,
                             specification_storage=None,
                             specification_table=None,
                             agent_set=None,
                             agents_for_estimation_storage=None,
                             agents_for_estimation_table=None,
                             join_datasets=False,
                             index_to_unplace=None,
                             portion_to_unplace=1.0,
                             agent_filter=None,
                             data_objects={}):
        from opus_core.model import get_specification_for_estimation
        specification = get_specification_for_estimation(
            specification_dict, specification_storage, specification_table)
        if (agent_set is not None) and (index_to_unplace is not None):
            if self.location_id_string is not None:
                agent_set.compute_variables(self.location_id_string,
                                            resources=Resources(data_objects))
            if portion_to_unplace < 1:
                unplace_size = int(portion_to_unplace * index_to_unplace.size)
                end_index_to_unplace = sample_noreplace(
                    index_to_unplace, unplace_size)
            else:
                end_index_to_unplace = index_to_unplace
            logger.log_status("Unplace " + str(end_index_to_unplace.size) +
                              " agents.")
            agent_set.modify_attribute(self.choice_set.get_id_name()[0],
                                       -1 * ones(end_index_to_unplace.size),
                                       end_index_to_unplace)
        # create agents for estimation
        if agents_for_estimation_storage is not None:
            estimation_set = Dataset(in_storage=agents_for_estimation_storage,
                                     in_table_name=agents_for_estimation_table,
                                     id_name=agent_set.get_id_name(),
                                     dataset_name=agent_set.get_dataset_name())
            if agent_filter is not None:
                estimation_set.compute_variables(
                    agent_filter, resources=Resources(data_objects))
                index = where(
                    estimation_set.get_attribute(agent_filter) > 0)[0]
                estimation_set.subset_by_index(
                    index, flush_attributes_if_not_loaded=False)

            if join_datasets:
                agent_set.join_by_rows(estimation_set,
                                       require_all_attributes=False,
                                       change_ids_if_not_unique=True)
                index = arange(agent_set.size() - estimation_set.size(),
                               agent_set.size())
            else:
                index = agent_set.get_id_index(
                    estimation_set.get_id_attribute())
        else:
            index = arange(agent_set.size())
        return (specification, index)
def prepare_for_estimate(specification_dict=None,
                         specification_storage=None,
                         specification_table=None,
                         agent_set=None,
                         household_set=None,
                         agents_for_estimation_storage=None,
                         agents_for_estimation_table=None,
                         households_for_estimation_table=None,
                         join_datasets=False,
                         filter=None,
                         data_objects=None):
    specification = get_specification_for_estimation(specification_dict,
                                                     specification_storage,
                                                     specification_table)
    if agents_for_estimation_storage is not None:
        estimation_set = Dataset(in_storage=agents_for_estimation_storage,
                                 in_table_name=agents_for_estimation_table,
                                 id_name=agent_set.get_id_name(),
                                 dataset_name=agent_set.get_dataset_name())
        hh_estimation_set = None
        if households_for_estimation_table is not None:
            hh_estimation_set = Dataset(
                in_storage=agents_for_estimation_storage,
                in_table_name=households_for_estimation_table,
                id_name=household_set.get_id_name(),
                dataset_name=household_set.get_dataset_name())

        filter_index = arange(estimation_set.size())
        if filter:
            estimation_set.compute_variables(filter,
                                             resources=Resources(data_objects))
            filter_index = where(estimation_set.get_attribute(filter) > 0)[0]
            #estimation_set.subset_by_index(index, flush_attributes_if_not_loaded=False)

        if join_datasets:
            if hh_estimation_set is not None:
                household_set.join_by_rows(hh_estimation_set,
                                           require_all_attributes=False,
                                           change_ids_if_not_unique=True)

            agent_set.join_by_rows(estimation_set,
                                   require_all_attributes=False,
                                   change_ids_if_not_unique=True)
            index = arange(agent_set.size() - estimation_set.size(),
                           agent_set.size())[filter_index]
        else:
            index = agent_set.get_id_index(
                estimation_set.get_id_attribute()[filter_index])
    else:
        if agent_set is not None:
            index = arange(agent_set.size())
        else:
            index = None

    return (specification, index)
Esempio n. 9
0
 def prepare_for_estimate(self, specification_dict = None, specification_storage=None,
                           specification_table=None, dataset=None,
                           filter_variable="urbansim.gridcell.total_land_value",
                           threshold=0):
     from opus_core.model import get_specification_for_estimation
     specification = get_specification_for_estimation(specification_dict,
                                                       specification_storage,
                                                       specification_table)
     index = None
     if dataset is not None:
         dataset.compute_variables(filter_variable)
         index = where(dataset.get_attribute(filter_variable) >= threshold)[0]
     return (specification, index)
Esempio n. 10
0
 def prepare_for_estimate(self, estimation_storage=None, estimation_table=None, dataset_filter=None, 
                         filter_threshold=0, **kwargs):
     spec = get_specification_for_estimation(**kwargs)
     if estimation_storage is not None and estimation_table is not None:
         dataset = Dataset(in_storage = estimation_storage, in_table_name=estimation_table,
                                  id_name='proposal_id', 
                                  dataset_name='development_project_proposal')
     if (dataset is not None) and (dataset_filter is not None):
         filter_values = dataset.compute_variables([dataset_filter], dataset_pool=self.dataset_pool)
         index = where(filter_values > filter_threshold)[0]
     else:
         index = None
     return (dataset, spec, index)
Esempio n. 11
0
 def prepare_for_estimate(self,
                          dataset=None,
                          dataset_filter=None,
                          filter_threshold=0,
                          **kwargs):
     spec = get_specification_for_estimation(**kwargs)
     if (dataset is not None) and (dataset_filter is not None):
         filter_values = dataset.compute_variables(
             [dataset_filter], dataset_pool=self.dataset_pool)
         index = where(filter_values > filter_threshold)[0]
     else:
         index = None
     return (spec, index)
 def prepare_for_estimate(self, specification_dict = None, specification_storage=None,
                           specification_table=None, dataset=None,
                           filter_variable="urbansim.gridcell.fraction_residential_land"):
     from opus_core.model import get_specification_for_estimation
     specification = get_specification_for_estimation(specification_dict,
                                                       specification_storage,
                                                       specification_table)
     index = None
     if dataset is not None:
         dataset.compute_variables(filter_variable)
         tmp = where(dataset.get_attribute(filter_variable) > 0, 1, 0)
         tmp = logical_and(tmp, where(dataset.get_attribute(filter_variable) < 1, 1, 0))
         index = where(tmp)[0]
     return (specification, index)
def prepare_for_estimate(specification_dict = None, 
                         specification_storage=None, 
                         specification_table=None,
                         agent_set=None, 
                         household_set=None,
                         agents_for_estimation_storage=None,
                         agents_for_estimation_table=None,
                         households_for_estimation_table=None,
                         join_datasets=False,
                         filter=None,
                         data_objects=None):
    specification = get_specification_for_estimation(specification_dict, 
                                                     specification_storage, 
                                                     specification_table)
    if agents_for_estimation_storage is not None:                 
        estimation_set = Dataset(in_storage = agents_for_estimation_storage, 
                                 in_table_name=agents_for_estimation_table,
                                 id_name=agent_set.get_id_name(), 
                                 dataset_name=agent_set.get_dataset_name())
        hh_estimation_set = None
        if households_for_estimation_table is not None:
            hh_estimation_set = Dataset(in_storage = agents_for_estimation_storage, 
                                     in_table_name=households_for_estimation_table,
                                     id_name=household_set.get_id_name(), 
                                     dataset_name=household_set.get_dataset_name())
        
        filter_index = arange(estimation_set.size())
        if filter:
            estimation_set.compute_variables(filter, resources=Resources(data_objects))
            filter_index = where(estimation_set.get_attribute(filter) > 0)[0]
            #estimation_set.subset_by_index(index, flush_attributes_if_not_loaded=False)
        
        if join_datasets:
            if hh_estimation_set is not None:
                household_set.join_by_rows(hh_estimation_set, require_all_attributes=False,
                                           change_ids_if_not_unique=True)
                
            agent_set.join_by_rows(estimation_set, require_all_attributes=False,
                                   change_ids_if_not_unique=True)
            index = arange(agent_set.size() - estimation_set.size(), agent_set.size())[filter_index]
        else:
            index = agent_set.get_id_index(estimation_set.get_id_attribute()[filter_index])
    else:
        if agent_set is not None:
            index = arange(agent_set.size())
        else:
            index = None
            
    return (specification, index)
 def prepare_for_estimate(self,
                          specification_dict=None,
                          specification_storage=None,
                          specification_table=None,
                          dataset=None,
                          filter_variable="housing_price",
                          threshold=0):
     from opus_core.model import get_specification_for_estimation
     specification = get_specification_for_estimation(
         specification_dict, specification_storage, specification_table)
     index = None
     if dataset is not None:
         dataset.compute_variables(filter_variable)
         index = where(
             dataset.get_attribute(filter_variable) >= threshold)[0]
     return (specification, index)
Esempio n. 15
0
 def prepare_for_estimate(self, dataset=None, dataset_for_estimation_storage=None, dataset_for_estimation_table=None, join_datasets=False, **kwargs):
     from opus_core.model import get_specification_for_estimation
     from opus_core.datasets.dataset import Dataset
     spec = get_specification_for_estimation(**kwargs)
     if (dataset_for_estimation_storage is not None) and (dataset_for_estimation_table is not None):
         estimation_set = Dataset(in_storage = dataset_for_estimation_storage,
                                   in_table_name=dataset_for_estimation_table,
                                   id_name=dataset.get_id_name(), dataset_name=dataset.get_dataset_name())
         if join_datasets:
             dataset.join_by_rows(estimation_set, require_all_attributes=False,
                                 change_ids_if_not_unique=True)
             index = arange(dataset.size()-estimation_set.size(),dataset.size())
         else:
             index = dataset.get_id_index(estimation_set.get_id_attribute())
     else:
             index = None
     return (spec, index)
    def prepare_for_estimate(self,specification_dict = None,
                                  specification_storage=None,
                                  specification_table=None,
                                  events_for_estimation_storage=None,
                                  events_for_estimation_table=None):

        from opus_core.model import get_specification_for_estimation
        specification = get_specification_for_estimation(specification_dict,
                                                          specification_storage,
                                                          specification_table)
        development = None
        # create agents for estimation
        if events_for_estimation_storage is not None:
            event_set = DevelopmentEventDataset(in_storage = events_for_estimation_storage,
                                            in_table_name= events_for_estimation_table)
            development = create_landuse_developments_from_history(event_set)
        return (specification, development)
    def __init__(self, config, model=None, model_group=None, specification=None, scenario_name=None):
        self.factory = VariableFactory()

        lib = config.get_expression_library()
        self.factory.set_expression_library(lib)

        self.model = model
        self.model_group = model_group
        
        if model is not None:
            if specification is None:
                specification_dict = config.get_estimation_specification(model, model_group)
                spec = get_specification_for_estimation(specification_dict)
            else:
                spec = specification
            model_prefix = ''
            if model_group is not None:
                model_prefix = '%s_' % model_group
            self.model_name = '%s%s' % (model_prefix, model)
            
            self.var_list = spec.get_distinct_long_variable_names().tolist()
            
            #check other model nodes, such as agents_filter, submodel_string or filter
            config_node_path = "model_manager/models/model[@name='%s']" % self.model
            model_node = config._find_node(config_node_path)
            controller = config._convert_model_to_dict(model_node)
            addvars = []
            addvars.append(controller.get('init', {}).get('arguments', {}).get('filter', None))
            addvars.append(controller.get('init', {}).get('arguments', {}).get('submodel_string', None))
            addvars.append(controller.get('init', {}).get('arguments', {}).get('choice_attribute_name', None))
            addvars.append(controller.get('prepare_for_run', {}).get('arguments', {}).get('agent_filter', None))
            for var in addvars:
                if isinstance(var, str):
                    self.var_list.append(eval(var)) # eval because these entries are in double quotes, e.g. "'attribute'"
                    
            self.var_tree = []
            l = []
            for var in self.var_list:
                l.append((var, []))
            self.var_tree.append((self.model_name, l))
        else:
            # this is meant to be for all models but is not working yet
            #self.config = config.get_run_configuration(scenario_name)
            self.var_list = []
            self.var_tree = []
    def prepare_for_estimate(self, specification_dict = None, 
                             specification_storage=None, 
                             specification_table=None,
                             agent_set=None, 
                             agents_for_estimation_storage=None,
                             agents_for_estimation_table=None,
                             filter_for_estimation_set=None,
                             data_objects=None):
        specification = get_specification_for_estimation(specification_dict, 
                                                         specification_storage, 
                                                         specification_table)
        if self.filter is not None:
            agents_index = where( self.proposal_set.compute_variables(self.filter) )[0]        
        
        id_attribute_name = ['parcel_id', 'template_id', 'is_redevelopment']
        if agents_for_estimation_storage is not None:
            estimation_set = Dataset(in_storage = agents_for_estimation_storage, 
                                     in_table_name=agents_for_estimation_table,
                                     id_name=id_attribute_name, 
                                     dataset_name=agent_set.get_dataset_name())
            
            filter_index = arange(estimation_set.size())
            if filter_for_estimation_set:
                filter_index = where(estimation_set.compute_variables(filter_for_estimation_set, resources=Resources(data_objects)))[0]
                estimation_set.subset_by_index(filter_index, flush_attributes_if_not_loaded=False)
                
            id_attributes = None
            for attr_name in id_attribute_name:
                attr_value = agent_set.get_attribute_as_column(attr_name)
                if id_attributes == None:
                    id_attributes = attr_value
                else:
                    id_attributes = concatenate((id_attributes, attr_value), axis=1)
                    
            id_index = estimation_set.try_get_id_index(id_attributes, return_value_if_not_found=-1)

            status_id = 2 * ones(agent_set.size(), dtype="int8")
            status_id[where(id_index != -1)] = 1
            name = self.choice_attribute_name.get_alias()
            if name in agent_set.get_known_attribute_names():
                agent_set.set_values_of_one_attribute(name, status_id[where(id_index != -1)], where(id_index!=-1)[0])
            else:
                agent_set.add_primary_attribute(status_id, name)
            
        return (specification, agents_index)
Esempio n. 19
0
 def prepare_for_estimate(
         self,
         specification_dict=None,
         specification_storage=None,
         specification_table=None,
         dataset=None,
         filter_variable="urbansim.gridcell.fraction_residential_land"):
     from opus_core.model import get_specification_for_estimation
     specification = get_specification_for_estimation(
         specification_dict, specification_storage, specification_table)
     index = None
     if dataset is not None:
         dataset.compute_variables(filter_variable)
         tmp = where(dataset.get_attribute(filter_variable) > 0, 1, 0)
         tmp = logical_and(
             tmp, where(dataset.get_attribute(filter_variable) < 1, 1, 0))
         index = where(tmp)[0]
     return (specification, index)
    def prepare_for_estimate(self,
                             specification_dict=None,
                             specification_storage=None,
                             specification_table=None,
                             events_for_estimation_storage=None,
                             events_for_estimation_table=None):

        from opus_core.model import get_specification_for_estimation
        specification = get_specification_for_estimation(
            specification_dict, specification_storage, specification_table)
        development = None
        # create agents for estimation
        if events_for_estimation_storage is not None:
            event_set = DevelopmentEventDataset(
                in_storage=events_for_estimation_storage,
                in_table_name=events_for_estimation_table)
            development = create_landuse_developments_from_history(event_set)
        return (specification, development)
    def prepare_for_estimate(self, specification_dict = None, specification_storage=None,
                              specification_table=None,
                              events_for_estimation_storage=None,
                              events_for_estimation_table=None, urbansim_constant=None, base_year=0,
                              categories=None):

        from opus_core.model import get_specification_for_estimation
        specification = get_specification_for_estimation(specification_dict,
                                                          specification_storage,
                                                          specification_table)
        projects = None
        # create agents for estimation
        if events_for_estimation_storage is not None:
            event_set = DevelopmentEventDataset(in_storage = events_for_estimation_storage,
                                            in_table_name= events_for_estimation_table)
            event_set.remove_non_recent_data(base_year, urbansim_constant['recent_years'])
            projects = DevelopmentProjectCreator().create_projects_from_history(
                                               event_set, self.project_type,
                                               self.units, categories)
        return (specification, projects)
    def prepare_for_estimate(self, specification_dict = None, specification_storage=None,
                              specification_table=None, agent_set=None,
                              agents_for_estimation_storage=None,
                              agents_for_estimation_table=None, join_datasets=False,
                              index_to_unplace=None, portion_to_unplace=1.0,
                              agent_filter=None,
                              data_objects={}):
        from opus_core.model import get_specification_for_estimation
        specification = get_specification_for_estimation(specification_dict,
                                                          specification_storage,
                                                          specification_table)
        if (agent_set is not None) and (index_to_unplace is not None):
            if self.location_id_string is not None:
                agent_set.compute_variables(self.location_id_string, resources=Resources(data_objects))
            if portion_to_unplace < 1:
                unplace_size = int(portion_to_unplace*index_to_unplace.size)
                end_index_to_unplace = sample_noreplace(index_to_unplace, unplace_size)
            else:
                end_index_to_unplace = index_to_unplace
            logger.log_status("Unplace " + str(end_index_to_unplace.size) + " agents.")
            agent_set.modify_attribute(self.choice_set.get_id_name()[0],
                                        -1*ones(end_index_to_unplace.size), end_index_to_unplace)
        # create agents for estimation
        if agents_for_estimation_storage is not None:
            estimation_set = Dataset(in_storage = agents_for_estimation_storage,
                                      in_table_name=agents_for_estimation_table,
                                      id_name=agent_set.get_id_name(), dataset_name=agent_set.get_dataset_name())
            if agent_filter is not None:
                estimation_set.compute_variables(agent_filter, resources=Resources(data_objects))
                index = where(estimation_set.get_attribute(agent_filter) > 0)[0]
                estimation_set.subset_by_index(index, flush_attributes_if_not_loaded=False)

            if join_datasets:
                agent_set.join_by_rows(estimation_set, require_all_attributes=False,
                                    change_ids_if_not_unique=True)
                index = arange(agent_set.size()-estimation_set.size(),agent_set.size())
            else:
                index = agent_set.get_id_index(estimation_set.get_id_attribute())
        else:
            index = arange(agent_set.size())
        return (specification, index)
    def prepare_for_estimate(self,
                             add_member_prefix=True,
                             specification_dict=None,
                             specification_storage=None,
                             specification_table=None,
                             building_set=None,
                             buildings_for_estimation_storage=None,
                             buildings_for_estimation_table=None,
                             constants=None,
                             base_year=0,
                             building_categories=None,
                             location_id_variable=None,
                             join_datasets=False,
                             data_objects=None,
                             **kwargs):
        #        buildings = None

        if (building_set is not None):
            if location_id_variable is not None:
                building_set.compute_variables(
                    location_id_variable, resources=Resources(data_objects))

        # create agents for estimation
        if buildings_for_estimation_storage is not None:
            estimation_set = Dataset(
                in_storage=buildings_for_estimation_storage,
                in_table_name=buildings_for_estimation_table,
                id_name=building_set.get_id_name(),
                dataset_name=building_set.get_dataset_name())
            if location_id_variable:
                estimation_set.compute_variables(
                    location_id_variable, resources=Resources(data_objects))
                # needs to be a primary attribute because of the join method below
                estimation_set.add_primary_attribute(
                    estimation_set.get_attribute(location_id_variable),
                    VariableName(location_id_variable).alias())

            years = estimation_set.get_attribute("scheduled_year")
            recent_years = constants['recent_years']
            indicator = zeros(estimation_set.size())
            for year in range(base_year - recent_years, base_year + 1):
                indicator = logical_or(indicator, years == year)
            idx = where(logical_not(indicator))[0]
            estimation_set.remove_elements(idx)

            #if filter:
            #estimation_set.compute_variables(filter, resources=Resources(data_objects))
            #index = where(estimation_set.get_attribute(filter) > 0)[0]
            #estimation_set.subset_by_index(index, flush_attributes_if_not_loaded=False)

            if join_datasets:
                building_set.join_by_rows(estimation_set,
                                          require_all_attributes=False,
                                          change_ids_if_not_unique=True)
                index = arange(building_set.size() - estimation_set.size(),
                               agent_set.size())
            else:
                index = building_set.get_id_index(
                    estimation_set.get_id_attribute())
        else:
            if building_set is not None:
                index = arange(building_set.size())
            else:
                index = None

        if add_member_prefix:
            specification_table = self.group_member.add_member_prefix_to_table_names(
                [specification_table])

        from opus_core.model import get_specification_for_estimation
        #from urbansim.functions import compute_supply_and_add_to_location_set
        specification = get_specification_for_estimation(
            specification_dict, specification_storage, specification_table)

        #specification, dummy = AgentLocationChoiceModelMember.prepare_for_estimate(self, add_member_prefix,
        #specification_dict, specification_storage,
        #specification_table,
        #location_id_variable=location_id_variable,
        #data_objects=data_objects, **kwargs)
        return (specification, index)
    def prepare_for_estimate(self, specification_dict = None, specification_storage=None,
                              specification_table=None, agent_set=None, 
                              agents_for_estimation_storage=None,
                              agents_for_estimation_table=None, join_datasets=False,
                              index_to_unplace=None, portion_to_unplace=1.0,
                              compute_lambda=False, grouping_location_set=None,
                              movers_variable=None, movers_index=None,
                              filter=None, location_id_variable=None,
                              data_objects={}):
        """Put 'location_id_variable' always in, if the location id is to be computed on the estimation set,
        i.e. if it is not a primary attribute of the estimation set. Set 'index_to_unplace' to None, if 'compute_lambda' is True.
        In such a case, the annual supply is estimated without unplacing agents. 'grouping_location_set', 'movers_variable' and
        'movers_index' must be given, if 'compute_lambda' is True.
        """
        from opus_core.model import get_specification_for_estimation
        from urbansim.functions import compute_supply_and_add_to_location_set
        specification = get_specification_for_estimation(specification_dict,
                                                          specification_storage,
                                                          specification_table)
        if (agent_set is not None) and (index_to_unplace is not None):
            if self.location_id_string is not None:
                agent_set.compute_variables(self.location_id_string, resources=Resources(data_objects))
            if portion_to_unplace < 1:
                unplace_size = int(portion_to_unplace*index_to_unplace.size)
                end_index_to_unplace = sample_noreplace(index_to_unplace, unplace_size)
            else:
                end_index_to_unplace = index_to_unplace
            logger.log_status("Unplace " + str(end_index_to_unplace.size) + " agents.")
            agent_set.modify_attribute(self.choice_set.get_id_name()[0],
                                        resize(array([-1]), end_index_to_unplace.size), end_index_to_unplace)
        if compute_lambda:
            movers = zeros(agent_set.size(), dtype="bool8")
            if movers_index is not None:
                movers[movers_index] = 1
            agent_set.add_primary_attribute(movers, "potential_movers")
            self.estimate_config["weights_for_estimation_string"] = self.estimate_config["weights_for_estimation_string"]+"_from_lambda"
            compute_supply_and_add_to_location_set(self.choice_set, grouping_location_set,
                                                   self.run_config["number_of_units_string"],
                                                   self.run_config["capacity_string"],
                                                   movers_variable,
                                                   self.estimate_config["weights_for_estimation_string"],
                                                   resources=Resources(data_objects))

        # create agents for estimation
        if (agents_for_estimation_storage is not None) and (agents_for_estimation_table is not None):
            estimation_set = Dataset(in_storage = agents_for_estimation_storage,
                                      in_table_name=agents_for_estimation_table,
                                      id_name=agent_set.get_id_name(), dataset_name=agent_set.get_dataset_name())
            if location_id_variable is not None:
                estimation_set.compute_variables(location_id_variable, resources=Resources(data_objects))
                # needs to be a primary attribute because of the join method below
                estimation_set.add_primary_attribute(estimation_set.get_attribute(location_id_variable), VariableName(location_id_variable).get_alias())
            if filter:
                values = estimation_set.compute_variables(filter, resources=Resources(data_objects))
                index = where(values > 0)[0]
                estimation_set.subset_by_index(index, flush_attributes_if_not_loaded=False)

            if join_datasets:
                agent_set.join_by_rows(estimation_set, require_all_attributes=False,
                                    change_ids_if_not_unique=True)
                index = arange(agent_set.size()-estimation_set.size(),agent_set.size())
            else:
                index = agent_set.get_id_index(estimation_set.get_id_attribute())
        else:
            if agent_set is not None:
                if filter is not None:
                    values = agent_set.compute_variables(filter, resources=Resources(data_objects))
                    index = where(values > 0)[0]
                else:
                    index = arange(agent_set.size())
            else:
                index = None
        return (specification, index)
    def prepare_for_estimate(self,
                             specification_dict=None,
                             specification_storage=None,
                             specification_table=None,
                             agent_set=None,
                             agents_for_estimation_storage=None,
                             agents_for_estimation_table=None,
                             join_datasets=False,
                             index_to_unplace=None,
                             portion_to_unplace=1.0,
                             compute_lambda=False,
                             grouping_location_set=None,
                             movers_variable=None,
                             movers_index=None,
                             filter=None,
                             location_id_variable=None,
                             data_objects={}):
        """Put 'location_id_variable' always in, if the location id is to be computed on the estimation set,
        i.e. if it is not a primary attribute of the estimation set. Set 'index_to_unplace' to None, if 'compute_lambda' is True.
        In such a case, the annual supply is estimated without unplacing agents. 'grouping_location_set', 'movers_variable' and
        'movers_index' must be given, if 'compute_lambda' is True.
        """
        from opus_core.model import get_specification_for_estimation
        from urbansim.functions import compute_supply_and_add_to_location_set
        specification = get_specification_for_estimation(
            specification_dict, specification_storage, specification_table)
        if (agent_set is not None) and (index_to_unplace is not None):
            if self.location_id_string is not None:
                agent_set.compute_variables(self.location_id_string,
                                            resources=Resources(data_objects))
            if portion_to_unplace < 1:
                unplace_size = int(portion_to_unplace * index_to_unplace.size)
                end_index_to_unplace = sample_noreplace(
                    index_to_unplace, unplace_size)
            else:
                end_index_to_unplace = index_to_unplace
            logger.log_status("Unplace " + str(end_index_to_unplace.size) +
                              " agents.")
            agent_set.modify_attribute(
                self.choice_set.get_id_name()[0],
                resize(array([-1]), end_index_to_unplace.size),
                end_index_to_unplace)
        if compute_lambda:
            movers = zeros(agent_set.size(), dtype="bool8")
            if movers_index is not None:
                movers[movers_index] = 1
            agent_set.add_primary_attribute(movers, "potential_movers")
            self.estimate_config[
                "weights_for_estimation_string"] = self.estimate_config[
                    "weights_for_estimation_string"] + "_from_lambda"
            compute_supply_and_add_to_location_set(
                self.choice_set,
                grouping_location_set,
                self.run_config["number_of_units_string"],
                self.run_config["capacity_string"],
                movers_variable,
                self.estimate_config["weights_for_estimation_string"],
                resources=Resources(data_objects))

        # create agents for estimation
        if (agents_for_estimation_storage
                is not None) and (agents_for_estimation_table is not None):
            estimation_set = Dataset(in_storage=agents_for_estimation_storage,
                                     in_table_name=agents_for_estimation_table,
                                     id_name=agent_set.get_id_name(),
                                     dataset_name=agent_set.get_dataset_name())
            if location_id_variable is not None:
                estimation_set.compute_variables(
                    location_id_variable, resources=Resources(data_objects))
                # needs to be a primary attribute because of the join method below
                estimation_set.add_primary_attribute(
                    estimation_set.get_attribute(location_id_variable),
                    VariableName(location_id_variable).get_alias())
            if filter:
                values = estimation_set.compute_variables(
                    filter, resources=Resources(data_objects))
                index = where(values > 0)[0]
                estimation_set.subset_by_index(
                    index, flush_attributes_if_not_loaded=False)

            if join_datasets:
                agent_set.join_by_rows(estimation_set,
                                       require_all_attributes=False,
                                       change_ids_if_not_unique=True)
                index = arange(agent_set.size() - estimation_set.size(),
                               agent_set.size())
            else:
                index = agent_set.get_id_index(
                    estimation_set.get_id_attribute())
        else:
            if agent_set is not None:
                if filter is not None:
                    values = agent_set.compute_variables(
                        filter, resources=Resources(data_objects))
                    index = where(values > 0)[0]
                else:
                    index = arange(agent_set.size())
            else:
                index = None
        return (specification, index)
Esempio n. 26
0
    def __init__(self,
                 config,
                 model=None,
                 model_group=None,
                 specification=None,
                 scenario_name=None):
        self.factory = VariableFactory()

        lib = config.get_expression_library()
        self.factory.set_expression_library(lib)

        self.model = model
        self.model_group = model_group

        if model is not None:
            if specification is None:
                specification_dict = config.get_estimation_specification(
                    model, model_group)
                spec = get_specification_for_estimation(specification_dict)
            else:
                spec = specification
            model_prefix = ''
            if model_group is not None:
                model_prefix = '%s_' % model_group
            self.model_name = '%s%s' % (model_prefix, model)

            self.var_list = spec.get_distinct_long_variable_names().tolist()

            #check other model nodes, such as agents_filter, submodel_string or filter
            config_node_path = "model_manager/models/model[@name='%s']" % self.model
            model_node = config._find_node(config_node_path)
            controller = config._convert_model_to_dict(model_node)
            addvars = []
            addvars.append(
                controller.get('init', {}).get('arguments',
                                               {}).get('filter', None))
            addvars.append(
                controller.get('init', {}).get('arguments',
                                               {}).get('submodel_string',
                                                       None))
            addvars.append(
                controller.get('init', {}).get('arguments',
                                               {}).get('choice_attribute_name',
                                                       None))
            addvars.append(
                controller.get('prepare_for_run',
                               {}).get('arguments',
                                       {}).get('agent_filter', None))
            for var in addvars:
                if isinstance(var, str):
                    self.var_list.append(
                        eval(var)
                    )  # eval because these entries are in double quotes, e.g. "'attribute'"

            self.var_tree = []
            l = []
            for var in self.var_list:
                l.append((var, []))
            self.var_tree.append((self.model_name, l))
        else:
            # this is meant to be for all models but is not working yet
            #self.config = config.get_run_configuration(scenario_name)
            self.var_list = []
            self.var_tree = []
    def prepare_for_estimate(
        self,
        add_member_prefix=True,
        specification_dict=None,
        specification_storage=None,
        specification_table=None,
        building_set=None,
        buildings_for_estimation_storage=None,
        buildings_for_estimation_table=None,
        constants=None,
        base_year=0,
        building_categories=None,
        location_id_variable=None,
        join_datasets=False,
        data_objects=None,
        **kwargs
    ):
        #        buildings = None

        if building_set is not None:
            if location_id_variable is not None:
                building_set.compute_variables(location_id_variable, resources=Resources(data_objects))

        # create agents for estimation
        if buildings_for_estimation_storage is not None:
            estimation_set = Dataset(
                in_storage=buildings_for_estimation_storage,
                in_table_name=buildings_for_estimation_table,
                id_name=building_set.get_id_name(),
                dataset_name=building_set.get_dataset_name(),
            )
            if location_id_variable:
                estimation_set.compute_variables(location_id_variable, resources=Resources(data_objects))
                # needs to be a primary attribute because of the join method below
                estimation_set.add_primary_attribute(
                    estimation_set.get_attribute(location_id_variable), VariableName(location_id_variable).alias()
                )

            years = estimation_set.get_attribute("scheduled_year")
            recent_years = constants["recent_years"]
            indicator = zeros(estimation_set.size(), dtype="int32")
            for year in range(base_year - recent_years, base_year + 1):
                indicator = logical_or(indicator, years == year)
            idx = where(logical_not(indicator))[0]
            estimation_set.remove_elements(idx)

            # if filter:
            # estimation_set.compute_variables(filter, resources=Resources(data_objects))
            # index = where(estimation_set.get_attribute(filter) > 0)[0]
            # estimation_set.subset_by_index(index, flush_attributes_if_not_loaded=False)

            if join_datasets:
                building_set.join_by_rows(estimation_set, require_all_attributes=False, change_ids_if_not_unique=True)
                index = arange(building_set.size() - estimation_set.size(), building_set.size())
            else:
                index = building_set.get_id_index(estimation_set.get_id_attribute())
        else:
            if building_set is not None:
                index = arange(building_set.size())
            else:
                index = None

        if add_member_prefix:
            specification_table = self.group_member.add_member_prefix_to_table_names([specification_table])

        from opus_core.model import get_specification_for_estimation

        # from urbansim.functions import compute_supply_and_add_to_location_set
        specification = get_specification_for_estimation(specification_dict, specification_storage, specification_table)

        # specification, dummy = AgentLocationChoiceModelMember.prepare_for_estimate(self, add_member_prefix,
        # specification_dict, specification_storage,
        # specification_table,
        # location_id_variable=location_id_variable,
        # data_objects=data_objects, **kwargs)
        return (specification, index)