def prepare_for_estimate(self, agents_for_estimation_storage, agents_for_estimation_table, agent_set, **kwargs): 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()) hhs_est = HouseholdDataset(in_storage=agents_for_estimation_storage, in_table_name='households_for_estimation') self.dataset_pool.replace_dataset('household', hhs_est) self.dataset_pool.replace_dataset(estimation_set.get_dataset_name(), estimation_set) spec, index = ChoiceModel.prepare_for_estimate(self, estimation_set, **kwargs) return (spec, index, estimation_set)
def prepare_for_estimate(self, estimation_storage, agents_for_estimation_table, agent_set, households_for_estimation_table=None, **kwargs): estimation_set = Dataset(in_storage = estimation_storage, in_table_name=agents_for_estimation_table, id_name=agent_set.get_id_name(), dataset_name=agent_set.get_dataset_name()) if households_for_estimation_table is not None: hhs = HouseholdDataset(in_storage=estimation_storage, in_table_name='households_for_estimation') self.dataset_pool.replace_dataset('household', hhs) self.dataset_pool.replace_dataset(estimation_set.get_dataset_name(), estimation_set) spec, index = HierarchicalChoiceModel.prepare_for_estimate(self, estimation_set, **kwargs) return (spec, index, estimation_set)
def prepare_for_estimate_hh(self, estimation_storage, agents_for_estimation_table, agent_set, persons_for_estimation_table=None, **kwargs): estimation_set = Dataset(in_storage = estimation_storage, in_table_name=agents_for_estimation_table, id_name=agent_set.get_id_name(), dataset_name=agent_set.get_dataset_name()) if persons_for_estimation_table is not None: pers = PersonDataset(in_storage=estimation_storage, in_table_name=persons_for_estimation_table) self.dataset_pool.replace_dataset('person', pers) self.dataset_pool.replace_dataset(estimation_set.get_dataset_name(), estimation_set) spec, index = ChoiceModel.prepare_for_estimate(self, estimation_set, **kwargs) return (spec, index, estimation_set)
def prepare_for_run(self, dataset=None, control_totals=None, weight_attribute=None, excel_path=None, excel_sheet_number=1, excel_data_info={}, esri_storage_location=None, dataset_name=None, current_year=0, datasets_and_weights_in_years=None, dataset_pool=None): """ If dataset is not given, it is loaded from the esri storage. In such a case, dataset_name should be the name of the shape file (without postfix). If control_totals is not given, it is loaded from an excel table. datasets_and_weights_in_years is a dictionary with years as keys and tuples of dataset_name and weight attribute as values. If its given and it has a value for the current year, its value overwrites the arguments 'dataset_name' and 'weight_attribute'. The method returns a tuple wit three elements: dataset (Dataset object), control_totals (Dataset object), weight_attribute (string) Those are going to be used in the run method. """ if control_totals is None: logger.log_status("Getting data from Excel for AllocationModel") excel_doc = ExcelDocument() excel_doc.open(excel_path) excel_doc.set_sheet(excel_sheet_number) control_total_data = excel_doc.get_dict_table_from_column_names_and_ranges(excel_data_info) excel_doc.close() excel_doc.quit() del excel_doc dict_storage = StorageFactory().get_storage('dict_storage') dict_storage.write_table(table_name = 'control_totals', table_data = control_total_data) control_totals = Dataset(in_storage = dict_storage, in_table_name = 'control_totals', id_name='year') if datasets_and_weights_in_years is not None and current_year in datasets_and_weights_in_years.keys(): dataset_name, weight_attribute = datasets_and_weights_in_years[current_year] dataset = None if dataset is None: logger.log_status("Getting data from esri_storage for AllocationModel") esri_storage = StorageFactory().get_storage('esri_storage', storage_location=esri_storage_location) #Was: dataset = Dataset(in_storage=esri_storage, in_table_name=dataset_name, id_name=dataset_name+'_id', dataset_name=dataset_name) dataset = DatasetFactory().search_for_dataset(dataset_name, dataset_pool.get_package_order(), arguments={'in_storage': esri_storage, 'id_name':dataset_name+'_id'} ) dataset_pool.add_datasets_if_not_included({dataset_name:dataset, control_totals.get_dataset_name():control_totals}) return (dataset, control_totals, weight_attribute)
#households.plot_histogram("income", bins = 10) #households.r_histogram("income") #households.r_scatter("persons", "income") households.correlation_coefficient("persons", "income") households.correlation_matrix(["persons", "income"]) households.summary() households.add_primary_attribute(data=[4,6,9,2,4,8,2,1,3,2], name="location") households.get_attribute_names() households.modify_attribute(name="location", data=[0,0], index=[0,1]) households.get_attribute("location") households.get_data_element_by_id(5).location #households.write_dataset(out_storage=storage, out_table_name="households_output") households.get_dataset_name() # Working with models from opus_core.choice_model import ChoiceModel choicemodel = ChoiceModel(choice_set=[1,2,3], utilities = "opus_core.linear_utilities", probabilities = "opus_core.mnl_probabilities", choices = "opus_core.random_choices") from numpy import array from opus_core.equation_specification import EquationSpecification specification = EquationSpecification( coefficients = array([ "beta01", "beta12", "beta03", "beta13" ]), variables = array([ "constant","household.persons", "constant", "household.persons"
def prepare_for_run(self, dataset=None, control_totals=None, weight_attribute=None, excel_path=None, excel_sheet_number=1, excel_data_info={}, esri_storage_location=None, dataset_name=None, current_year=0, datasets_and_weights_in_years=None, dataset_pool=None): """ If dataset is not given, it is loaded from the esri storage. In such a case, dataset_name should be the name of the shape file (without postfix). If control_totals is not given, it is loaded from an excel table. datasets_and_weights_in_years is a dictionary with years as keys and tuples of dataset_name and weight attribute as values. If its given and it has a value for the current year, its value overwrites the arguments 'dataset_name' and 'weight_attribute'. The method returns a tuple wit three elements: dataset (Dataset object), control_totals (Dataset object), weight_attribute (string) Those are going to be used in the run method. """ if control_totals is None: logger.log_status("Getting data from Excel for AllocationModel") excel_doc = ExcelDocument() excel_doc.open(excel_path) excel_doc.set_sheet(excel_sheet_number) control_total_data = excel_doc.get_dict_table_from_column_names_and_ranges( excel_data_info) excel_doc.close() excel_doc.quit() del excel_doc dict_storage = StorageFactory().get_storage('dict_storage') dict_storage.write_table(table_name='control_totals', table_data=control_total_data) control_totals = Dataset(in_storage=dict_storage, in_table_name='control_totals', id_name='year') if datasets_and_weights_in_years is not None and current_year in datasets_and_weights_in_years.keys( ): dataset_name, weight_attribute = datasets_and_weights_in_years[ current_year] dataset = None if dataset is None: logger.log_status( "Getting data from esri_storage for AllocationModel") esri_storage = StorageFactory().get_storage( 'esri_storage', storage_location=esri_storage_location) #Was: dataset = Dataset(in_storage=esri_storage, in_table_name=dataset_name, id_name=dataset_name+'_id', dataset_name=dataset_name) dataset = DatasetFactory().search_for_dataset( dataset_name, dataset_pool.get_package_order(), arguments={ 'in_storage': esri_storage, 'id_name': dataset_name + '_id' }) dataset_pool.add_datasets_if_not_included({ dataset_name: dataset, control_totals.get_dataset_name(): control_totals }) return (dataset, control_totals, weight_attribute)
#households.plot_histogram("income", bins = 10) #households.r_histogram("income") #households.r_scatter("persons", "income") households.correlation_coefficient("persons", "income") households.correlation_matrix(["persons", "income"]) households.summary() households.add_primary_attribute(data=[4,6,9,2,4,8,2,1,3,2], name="location") households.get_attribute_names() households.modify_attribute(name="location", data=[0,0], index=[0,1]) households.get_attribute("location") households.get_data_element_by_id(5).location #households.write_dataset(out_storage=storage, out_table_name="households_output") households.get_dataset_name() # Working with models from opus_core.choice_model import ChoiceModel choicemodel = ChoiceModel(choice_set=[1,2,3], utilities = "opus_core.linear_utilities", probabilities = "opus_core.mnl_probabilities", choices = "opus_core.random_choices") from numpy import array from opus_core.equation_specification import EquationSpecification specification = EquationSpecification( coefficients = array([ "beta01", "beta12", "beta03", "beta13" ]), variables = array([ "constant","household.persons", "constant", "household.persons"