def run(self, year=None, dataset_pool=None, **kwargs): """ """ if dataset_pool is None: dataset_pool = SessionConfiguration().get_dataset_pool() if year is None: year = SimulationState().get_current_time() this_year_index = where(self.scheduled_events.get_attribute('year')==year)[0] scheduled_events_for_this_year = DatasetSubset(self.scheduled_events, this_year_index) scheduled_events_for_this_year.load_dataset_if_not_loaded() column_names = list(set( self.scheduled_events.get_known_attribute_names() ) - set( [ 'year', 'action', 'attribute', 'amount', 'event_id', '_hidden_id_'] )) column_names.sort() # column_values = dict([ (name, scheduled_events_for_this_year.get_attribute(name)) for name in column_names]) for index in range(scheduled_events_for_this_year.size()): indicator = ones( self.dataset.size(), dtype='bool' ) event_attr = {} for attribute in column_names: if attribute in self.dataset.get_known_attribute_names(): dataset_attribute = self.dataset.get_attribute(attribute) else: ## this is done inside the loop because some action may delete computed attributes, such as dataset.add_elements() try: dataset_attribute = self.dataset.compute_one_variable_with_unknown_package(attribute, dataset_pool=dataset_pool) except: raise ValueError, "attribute %s used in scheduled events dataset can not be found in dataset %s" % (attribute, self.dataset.get_dataset_name()) # if attribute in column_names: aval = scheduled_events_for_this_year.get_attribute(attribute)[index] if aval == -1: continue # ignore if column value is -1 else: indicator *= dataset_attribute == aval event_attr.update({attribute:aval}) #agents in dataset satisfying all conditions are identified by indicator legit_index = where(indicator)[0] this_event = scheduled_events_for_this_year.get_data_element(index) if not hasattr(this_event, 'attribute'): action_attr_name = '' else: action_attr_name = this_event.attribute action_function = getattr(self, '_' + this_event.action.strip().lower()) action_function( amount=this_event.amount, attribute=action_attr_name, dataset=self.dataset, index=legit_index, data_dict=event_attr ) self.post_run(self.dataset, legit_index, **kwargs) return self.dataset
def run(self, year=None, dataset_pool=None, **kwargs): """ """ if dataset_pool is None: dataset_pool = SessionConfiguration().get_dataset_pool() if year is None: year = SimulationState().get_current_time() this_year_index = where( self.scheduled_events.get_attribute('year') == year)[0] scheduled_events_for_this_year = DatasetSubset(self.scheduled_events, this_year_index) scheduled_events_for_this_year.load_dataset_if_not_loaded() column_names = list( set(self.scheduled_events.get_known_attribute_names()) - set([ 'year', 'action', 'attribute', 'amount', 'event_id', '_hidden_id_' ])) column_names.sort() # column_values = dict([ (name, scheduled_events_for_this_year.get_attribute(name)) for name in column_names]) for index in range(scheduled_events_for_this_year.size()): indicator = ones(self.dataset.size(), dtype='bool') event_attr = {} for attribute in column_names: if attribute in self.dataset.get_known_attribute_names(): dataset_attribute = self.dataset.get_attribute(attribute) else: ## this is done inside the loop because some action may delete computed attributes, such as dataset.add_elements() try: dataset_attribute = self.dataset.compute_one_variable_with_unknown_package( attribute, dataset_pool=dataset_pool) except: raise ValueError, "attribute %s used in scheduled events dataset can not be found in dataset %s" % ( attribute, self.dataset.get_dataset_name()) # if attribute in column_names: aval = scheduled_events_for_this_year.get_attribute( attribute)[index] if aval == -1: continue # ignore if column value is -1 else: indicator *= dataset_attribute == aval event_attr.update({attribute: aval}) #agents in dataset satisfying all conditions are identified by indicator legit_index = where(indicator)[0] this_event = scheduled_events_for_this_year.get_data_element(index) if not hasattr(this_event, 'attribute'): action_attr_name = '' else: action_attr_name = this_event.attribute action_function = getattr(self, '_' + this_event.action.strip().lower()) action_function(amount=this_event.amount, attribute=action_attr_name, dataset=self.dataset, index=legit_index, data_dict=event_attr) self.post_run(self.dataset, legit_index, **kwargs) return self.dataset