本文整理汇总了Python中opus_core.datasets.dataset.DatasetSubset.get_attribute方法的典型用法代码示例。如果您正苦于以下问题:Python DatasetSubset.get_attribute方法的具体用法?Python DatasetSubset.get_attribute怎么用?Python DatasetSubset.get_attribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opus_core.datasets.dataset.DatasetSubset
的用法示例。
在下文中一共展示了DatasetSubset.get_attribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run( self, vacancy_table, history_table, year, location_set, dataset_pool=None, resources=None ):
self.dataset_pool=dataset_pool
building_types = self.dataset_pool.get_dataset('building_type')
target_vacancy_this_year = DatasetSubset(vacancy_table, index=where(vacancy_table.get_attribute("year")==year)[0])
building_type_ids = target_vacancy_this_year.get_attribute('building_type_id')
building_type_idx = building_types.get_id_index(building_type_ids)
self.used_building_types = DatasetSubset(building_types, index=building_type_idx)
project_types = self.used_building_types.get_attribute('building_type_name')
is_residential = self.used_building_types.get_attribute('is_residential')
unit_names = where(is_residential, 'residential_units', 'non_residential_sqft')
specific_unit_names = where(is_residential, 'residential_units', '_sqft')
rates = target_vacancy_this_year.get_attribute('target_total_vacancy')
self.project_units = {}
self.project_specific_units = {}
target_rates = {}
for i in range(self.used_building_types.size()):
self.project_units[project_types[i]] = unit_names[i]
if is_residential[i]:
self.project_specific_units[project_types[i]] = specific_unit_names[i]
else:
self.project_specific_units[project_types[i]] = "%s%s" % (project_types[i], specific_unit_names[i])
target_rates[building_type_ids[i]] = rates[i]
self._compute_vacancy_and_total_units_variables(location_set, project_types, resources)
self.pre_check( location_set, target_vacancy_this_year, project_types)
projects = None
for project_type_id, target_vacancy_rate in target_rates.iteritems():
# determine current-year vacancy rates
project_type = building_types.get_attribute_by_id('building_type_name', project_type_id)
vacant_units_sum = location_set.get_attribute(self.variable_for_vacancy[project_type]).sum()
units_sum = float( location_set.get_attribute(self.variable_for_total_units[project_type]).sum() )
should_develop_units = int(round(max( 0, ( target_vacancy_rate * units_sum - vacant_units_sum ) /
( 1 - target_vacancy_rate ) )))
logger.log_status(project_type + ": vacant units: %d, should be vacant: %f, sum units: %d"
% (vacant_units_sum, target_vacancy_rate * units_sum, units_sum))
if not should_develop_units:
logger.log_note(("Will not build any " + project_type + " units, because the current vacancy of %d units\n"
+ "is more than the %d units desired for the vacancy rate of %f.")
% (vacant_units_sum,
target_vacancy_rate * units_sum,
target_vacancy_rate))
#create projects
if should_develop_units > 0:
this_project = self._create_projects(should_develop_units, project_type, project_type_id, history_table,
location_set, units_sum, resources)
if projects is None:
projects = this_project
else:
projects.join_by_rows(this_project, change_ids_if_not_unique=True)
return projects
示例2: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run( self, model_configuration, vacancy_table, history_table, year,
location_set, resources=None):
large_area_ids = vacancy_table.get_attribute("large_area_id")
locations_large_area_ids = location_set.compute_variables("washtenaw.%s.large_area_id" % location_set.get_dataset_name())
unique_large_areas = unique(large_area_ids)
self._compute_vacancy_variables(location_set,
model_configuration['development_project_types'],
resources)
projects = {}
for area in unique_large_areas:
location_index = where(locations_large_area_ids == area)[0]
locations_for_this_area = DatasetSubset(location_set, location_index)
logger.log_status("DPLCM for area %s", area)
target_residential_vacancy_rate, target_non_residential_vacancy_rate = self._get_target_vacancy_rates(vacancy_table, year, area)
for project_type in model_configuration['development_project_types']:
# determine current-year vacancy rates
vacant_units_sum = locations_for_this_area.get_attribute(self.variable_for_vacancy[project_type]).sum()
units_sum = float( locations_for_this_area.get_attribute(self.units_variable[project_type]).sum() )
vacant_rate = self.safe_divide(vacant_units_sum, units_sum)
if model_configuration['development_project_types'][project_type]['residential']:
target_vacancy_rate = target_residential_vacancy_rate
else:
target_vacancy_rate = target_non_residential_vacancy_rate
should_develop_units = int(round(max( 0, ( target_vacancy_rate * units_sum - vacant_units_sum ) /
( 1 - target_vacancy_rate ) )))
logger.log_status(project_type + ": vacant units: %d, should be vacant: %f, sum units: %d, will develop: %d"
% (vacant_units_sum, target_vacancy_rate * units_sum, units_sum, should_develop_units))
#create projects
if should_develop_units > 0:
project_dataset = self._create_projects(should_develop_units, project_type, history_table,
locations_for_this_area, units_sum,
model_configuration['development_project_types'],
resources)
project_dataset.add_attribute(array(project_dataset.size()*[area]), "large_area_id",
metadata=AttributeType.PRIMARY)
if (project_type not in projects.keys()) or (projects[project_type] is None):
projects[project_type] = project_dataset
else:
projects[project_type].join_by_rows(project_dataset, change_ids_if_not_unique=True)
for project_type in model_configuration['development_project_types']:
if project_type not in projects.keys():
projects[project_type] = None
if projects[project_type] is None:
size = 0
else:
projects[project_type].add_submodel_categories()
size = projects[project_type].size()
logger.log_status("%s %s projects to be built" % (size, project_type))
return projects
开发者ID:christianurich,项目名称:VIBe2UrbanSim,代码行数:53,代码来源:regional_development_project_transition_model.py
示例3: choose_agents_to_move_from_overfilled_locations
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def choose_agents_to_move_from_overfilled_locations(self, capacity,
agent_set, agents_index, agents_locations):
"""Agents with the smallest number of units should move again.
"""
if capacity is None:
return array([], dtype='int32')
index_valid_agents_locations = where(agents_locations > 0)[0]
valid_agents_locations = agents_locations[index_valid_agents_locations].astype("int32")
unique_locations = unique(valid_agents_locations).astype("int32")
index_consider_capacity = self.choice_set.get_id_index(unique_locations)
capacity_of_affected_locations = capacity[index_consider_capacity]
overfilled = where(capacity_of_affected_locations < 0)[0]
movers = array([], dtype='int32')
indexed_individuals = DatasetSubset(agent_set, agents_index[index_valid_agents_locations])
ordered_agent_indices = self.get_agents_order(indexed_individuals)
sizes = indexed_individuals.get_attribute(self.units_full_name)[ordered_agent_indices]
choice_ids = self.choice_set.get_id_attribute()
for loc in overfilled:
agents_to_move = where(valid_agents_locations == choice_ids[index_consider_capacity[loc]])[0]
if agents_to_move.size > 0:
n = int(-1*capacity_of_affected_locations[loc])
this_sizes = sizes[agents_to_move]
csum = this_sizes[arange(this_sizes.size-1,-1,-1)].cumsum() # ordered increasingly
csum = csum[arange(csum.size-1, -1,-1)] # ordered back decreasingly
w = where(csum < n)[0]
if w.size < agents_to_move.size: #add one more agent in order the cumsum be larger than n
w = concatenate((array([agents_to_move.size-w.size-1]), w))
idx = ordered_agent_indices[agents_to_move[w]]
movers = concatenate((movers, idx))
return movers
示例4: estimate_mu
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def estimate_mu(self):
iout = -1
self.values_from_mr = {}
for quantity in self.observed_data.get_quantity_objects():
dataset_name = quantity.get_dataset_name()
variable = quantity.get_variable_name()
iout += 1
dimension_reduced = False
quantity_ids = quantity.get_dataset().get_id_attribute()
for i in range(self.number_of_runs):
ds = self._compute_variable_for_one_run(i, variable, dataset_name, self.get_calibration_year(), quantity)
if isinstance(ds, InteractionDataset):
ds = ds.get_flatten_dataset()
if i == 0: # first run
self.mu[iout] = zeros((self.y[iout].size, self.number_of_runs), dtype=float32)
ids = ds.get_id_attribute()
else:
if ds.size() > ids.shape[0]:
ds = DatasetSubset(ds, ds.get_id_index(ids))
dimension_reduced = True
scale = self.get_scales(ds, i+1, variable)
matching_index = ds.get_id_index(quantity_ids)
values = scale[matching_index] * ds.get_attribute(variable)[matching_index]
self.mu[iout][:,i] = try_transformation(values, quantity.get_transformation())
self.values_from_mr[variable.get_expression()] = self.mu[iout]
if dimension_reduced:
self.y[iout] = self.y[iout][quantity.get_dataset().get_id_index(ids)]
示例5: _do_run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def _do_run(self, location_set, agent_set, agents_index, data_objects=None, resources=None):
location_id_name = location_set.get_id_name()[0]
jobsubset = DatasetSubset(agent_set, agents_index)
if jobsubset.size() <= 0:
return array([], dtype='int32')
#unplace jobs
agent_set.set_values_of_one_attribute(location_id_name,
resize(array([-1.0]), jobsubset.size()), agents_index)
sector_ids = jobsubset.get_attribute("sector_id")
sectors = unique(sector_ids)
counts = ndimage_sum(ones((jobsubset.size(),)), labels=sector_ids.astype('int32'), index=sectors.astype('int32'))
if sectors.size <=1 :
counts = array([counts])
variables = map(lambda x: "number_of_jobs_of_sector_"+str(int(x)), sectors)
compute_variables = map(lambda var: self.variable_package + "." +
location_set.get_dataset_name()+ "." + var, variables)
if data_objects is not None:
self.dataset_pool.add_datasets_if_not_included(data_objects)
self.dataset_pool.add_datasets_if_not_included({agent_set.get_dataset_name():agent_set})
location_set.compute_variables(compute_variables, dataset_pool=self.dataset_pool)
if self.filter is None:
location_index = arange(location_set.size())
else:
filter_values = location_set.compute_variables([self.filter], dataset_pool=self.dataset_pool)
location_index = where(filter_values > 0)[0]
if location_index.size <= 0:
logger.log_status("No locations available. Nothing to be done.")
return array([])
location_subset = DatasetSubset(location_set, location_index)
i=0
for sector in sectors:
distr = location_subset.get_attribute(variables[i])
if ma.allclose(distr.sum(), 0):
uniform_prob = 1.0/distr.size
distr = resize(array([uniform_prob], dtype='float64'), distr.size)
logger.log_warning("Probabilities in scaling model for sector " + str(sector) + " sum to 0.0. Substituting uniform distribution!")
# random_sample = sample(location_set.get_attribute("grid_id"), k=int(counts[i]), \
# probabilities = distr)
distr = distr/float(distr.sum())
random_sample = probsample_replace(location_subset.get_id_attribute(), size=int(counts[i]),
prob_array=distr)
idx = where(sector_ids == sector)[0]
#modify job locations
agent_set.set_values_of_one_attribute(location_id_name, random_sample, agents_index[idx])
i+=1
return agent_set.get_attribute_by_index(location_id_name, agents_index)
示例6: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
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
示例7: prepare_for_run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def prepare_for_run(self, dataset_pool,
create_proposal_set=True,
parcel_filter_for_new_development=None,
parcel_filter_for_redevelopment=None,
template_filter=None,
spec_replace_module_variable_pair=None,
proposed_units_variable="urbansim_parcel.development_project_proposal.units_proposed",
**kwargs):
"""create development project proposal dataset from parcels and development templates.
spec_replace_module_variable_pair is a tuple with two elements: module name, variable within the module
that contans a dictionary of model variables to be replaced in the specification.
"""
specification, coefficients, dummy = RegressionModel.prepare_for_run(self, **kwargs)
try:
existing_proposal_set_parent = dataset_pool.get_dataset('development_project_proposal')
#load proposals whose status_id are not of id_tentative or id_not_available
available_idx = where(logical_and(existing_proposal_set_parent.get_attribute("status_id") != DevelopmentProjectProposalDataset.id_tentative,
existing_proposal_set_parent.get_attribute("status_id") != DevelopmentProjectProposalDataset.id_not_available))[0]
existing_proposal_set = DatasetSubset(existing_proposal_set_parent, available_idx)
# Code updated by Hanyi Li, MAG 6/8/2010
# Replacing the cached 'development_project_proposal' dataset with
# the filtered dataset 'existing_proposal_set'
dataset_pool.replace_dataset(existing_proposal_set_parent.get_dataset_name(), existing_proposal_set)
except:
existing_proposal_set = None
parcels = dataset_pool.get_dataset('parcel')
templates = dataset_pool.get_dataset('development_template')
# It is important that during this method no variable flushing happens, since
# we create datasets of the same name for different purposes (new development and redevelopment)
# and flushing would mix them up
flush_variables_current = SessionConfiguration().get('flush_variables', False)
SessionConfiguration().put_data({'flush_variables': False})
# Code added by Jesse Ayers, MAG, 9/14/2009
# Getting an index of parcels that have actively developing projects (those on a velocity function)
# and making sure that new proposals are not generated for them
if existing_proposal_set:
parcels_with_proposals = existing_proposal_set.get_attribute('parcel_id')
parcels_with_proposals_idx = parcels.get_id_index(parcels_with_proposals)
if parcel_filter_for_new_development is not None:
if parcel_filter_for_new_development[parcel_filter_for_new_development.find('=')+1] == '=':
filter = 'flter = numpy.logical_and(parcel.number_of_agents(development_project_proposal) == 0, %s)' % parcel_filter_for_new_development
else:
parcel_filter_for_new_development = parcel_filter_for_new_development[parcel_filter_for_new_development.find('=')+1:].lstrip()
filter = 'flter = numpy.logical_and(parcel.number_of_agents(development_project_proposal) == 0, %s)' % parcel_filter_for_new_development
index1 = where(parcels.compute_variables(filter))[0]
else:
if parcel_filter_for_new_development is not None:
index1 = where(parcels.compute_variables(parcel_filter_for_new_development))[0]
else:
index1 = None
if template_filter is not None:
try:
index2 = where(templates.compute_variables(template_filter))[0]
except Exception, e:
logger.log_warning( "template_filter is set to %s, but there is an error when computing it: %s"
% (template_filter, e) )
index2 = None
开发者ID:christianurich,项目名称:VIBe2UrbanSim,代码行数:64,代码来源:development_project_proposal_regression_model.py
示例8: test_agents_placed_in_appropriate_types
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
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")
示例9: EmploymentTransitionModel
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
class EmploymentTransitionModel(Model):
"""Creates and removes jobs from job_set."""
model_name = "Employment Transition Model"
location_id_name_default = "grid_id"
variable_package_default = "urbansim"
def __init__(self, location_id_name=None, variable_package=None, dataset_pool=None, debuglevel=0):
self.debug = DebugPrinter(debuglevel)
self.location_id_name = self.location_id_name_default
self.variable_package = self.variable_package_default
if location_id_name is not None:
self.location_id_name = location_id_name
if variable_package is not None:
self.variable_package = variable_package
self.dataset_pool = self.create_dataset_pool(dataset_pool, ["urbansim", "opus_core"])
def run(self, year, job_set, control_totals, job_building_types, data_objects=None, resources=None):
self._do_initialize_for_run(job_set, job_building_types, data_objects)
idx = where(control_totals.get_attribute("year")==year)[0]
self.control_totals_for_this_year = DatasetSubset(control_totals, idx)
self._do_run_for_this_year(job_set)
return self._update_job_set(job_set)
def _do_initialize_for_run(self, job_set, job_building_types, data_objects=None):
self.max_id = job_set.get_id_attribute().max()
self.job_size = job_set.size()
self.job_id_name = job_set.get_id_name()[0]
self.new_jobs = {
self.location_id_name:array([], dtype=job_set.get_data_type(self.location_id_name, int32)),
"sector_id":array([], dtype=job_set.get_data_type("sector_id", int32)),
self.job_id_name:array([], dtype=job_set.get_data_type(self.job_id_name, int32)),
"building_type":array([], dtype=job_set.get_data_type("building_type", int8))
}
self.remove_jobs = array([], dtype=int32)
if data_objects is not None:
self.dataset_pool.add_datasets_if_not_included(data_objects)
self.dataset_pool.add_datasets_if_not_included({job_building_types.get_dataset_name():job_building_types})
self.available_building_types = job_building_types.get_id_attribute()
def _compute_sector_variables(self, sectors, job_set):
compute_resources = Resources({"debug":self.debug})
job_set.compute_variables(
map(lambda x: "%s.%s.is_in_employment_sector_%s_home_based"
% (self.variable_package, job_set.get_dataset_name(), x),
sectors) +
map(lambda x: "%s.%s.is_in_employment_sector_%s_non_home_based"
% (self.variable_package, job_set.get_dataset_name(), x),
sectors) + ["is_non_home_based_job", "is_home_based_job"],
dataset_pool = self.dataset_pool,
resources = compute_resources)
def _do_run_for_this_year(self, job_set):
building_type = job_set.get_attribute("building_type")
sectors = unique(self.control_totals_for_this_year.get_attribute("sector_id"))
self._compute_sector_variables(sectors, job_set)
for sector in sectors:
isector = where(self.control_totals_for_this_year.get_attribute("sector_id") == sector)[0]
total_hb_jobs = self.control_totals_for_this_year.get_attribute("total_home_based_employment")[isector]
total_nhb_jobs = self.control_totals_for_this_year.get_attribute("total_non_home_based_employment")[isector]
is_in_sector_hb = job_set.get_attribute("is_in_employment_sector_%s_home_based" % sector)
is_in_sector_nhb = job_set.get_attribute("is_in_employment_sector_%s_non_home_based" % sector)
diff_hb = int(total_hb_jobs - is_in_sector_hb.astype(int8).sum())
diff_nhb = int(total_nhb_jobs - is_in_sector_nhb.astype(int8).sum())
if diff_hb < 0: # home based jobs to be removed
w = where(is_in_sector_hb == 1)[0]
sample_array, non_placed, size_non_placed = \
get_array_without_non_placed_agents(job_set, w, -1*diff_hb,
self.location_id_name)
self.remove_jobs = concatenate((self.remove_jobs, non_placed,
sample_noreplace(sample_array, max(0,abs(diff_hb)-size_non_placed))))
if diff_nhb < 0: # non home based jobs to be removed
w = where(is_in_sector_nhb == 1)[0]
sample_array, non_placed, size_non_placed = \
get_array_without_non_placed_agents(job_set, w, -1*diff_nhb,
self.location_id_name)
self.remove_jobs = concatenate((self.remove_jobs, non_placed,
sample_noreplace(sample_array, max(0,abs(diff_nhb)-size_non_placed))))
if diff_hb > 0: # home based jobs to be created
self.new_jobs[self.location_id_name] = concatenate((self.new_jobs[self.location_id_name],
zeros((diff_hb,), dtype=self.new_jobs[self.location_id_name].dtype.type)))
self.new_jobs["sector_id"] = concatenate((self.new_jobs["sector_id"],
(resize(array([sector], dtype=self.new_jobs["sector_id"].dtype.type), diff_hb))))
if 1 in is_in_sector_hb:
building_type_distribution = array(ndimage_sum(is_in_sector_hb,
labels=building_type,
index=self.available_building_types))
elif 1 in job_set.get_attribute("is_home_based_job"): # take the building type distribution from the whole region
building_type_distribution = array(ndimage_sum(
job_set.get_attribute("is_home_based_job"),
labels=building_type,
index=self.available_building_types))
else: # there are no home-based jobs in the region, take uniform distribution
building_type_distribution = ones(self.available_building_types.size)
building_type_distribution = building_type_distribution/building_type_distribution.sum()
sampled_building_types = probsample_replace(
self.available_building_types, diff_hb, building_type_distribution/
float(building_type_distribution.sum()))
self.new_jobs["building_type"] = concatenate((self.new_jobs["building_type"],
#.........这里部分代码省略.........
示例10: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run(self, n=500, run_config=None, current_year=None, debuglevel=0):
"""
n - sample n proposals at a time, evaluate them one by one
"""
self.demolished_buildings = array([], dtype='int32') #id of buildings to be demolished
if current_year is None:
current_year = SimulationState().get_current_time()
if not self.positive_proposals:
logger.log_status("Proposal Set size <= 0, no proposals to consider, skipping DPPSM.")
return (self.proposal_set, self.demolished_buildings)
self.proposal_component_set.compute_variables([
'urbansim_parcel.development_project_proposal_component.units_proposed',
'urbansim_parcel.development_project_proposal_component.is_residential'],
dataset_pool=self.dataset_pool)
self.proposal_set.compute_variables([
'urbansim_parcel.development_project_proposal.number_of_components',
'zone_id=development_project_proposal.disaggregate(parcel.zone_id)',
#'occurence_frequency = development_project_proposal.disaggregate(development_template.sample_size)'
],
dataset_pool=self.dataset_pool)
buildings = self.dataset_pool.get_dataset("building")
buildings.compute_variables([
"occupied_units_for_jobs = urbansim_parcel.building.number_of_non_home_based_jobs",
"units_for_jobs = urbansim_parcel.building.total_non_home_based_job_space",
"occupied_residential_units = urbansim_parcel.building.number_of_households",
# "urbansim_parcel.building.existing_units",
"urbansim_parcel.building.is_residential"
],
dataset_pool=self.dataset_pool)
## define unit_name by whether a building is residential or not (with is_residential attribute)
## if it is non-residential (0), count units by number of job spaces (units_for_jobs)
## if it is residential (1), count units by residenital units
self.unit_name = array(["units_for_jobs", "residential_units"])
target_vacancy = self.dataset_pool.get_dataset('target_vacancy')
target_vacancy.compute_variables(['is_residential = target_vacancy.disaggregate(building_type.is_residential)'],
dataset_pool=self.dataset_pool)
# This try-except block checks to see if the object has a subarea_id_name,
# if it does, it calculates the vacancy rates by subarea_id_name
try:
# Check for subarea_id_name in target_vacancies dataset
# if it is present, vacancy rates are specified by subarea_id_name
# if it is not, vacancy rates are specified region wide
target_vacancy.load_dataset()
if self.subarea_id_name in target_vacancy.get_attribute_names():
current_target_vacancy_this_year = DatasetSubset(target_vacancy, index=where(target_vacancy.get_attribute("year")==current_year)[0])
current_target_vacancy = DatasetSubset(current_target_vacancy_this_year, index=where(current_target_vacancy_this_year.get_attribute(self.subarea_id_name)==self.area_id)[0])
else:
current_target_vacancy = DatasetSubset(target_vacancy, index=where(target_vacancy.get_attribute("year")==current_year)[0])
except AttributeError:
# vacancy rates are specified region wide:
current_target_vacancy = DatasetSubset(target_vacancy, index=where(target_vacancy.get_attribute("year")==current_year)[0])
if current_target_vacancy.size() == 0:
raise IOError, 'No target vacancy defined for year %s.' % current_year
self.existing_units = {} #total existing units by land_use type
self.occupied_units = {} #total occupied units by land_use type
self.proposed_units = {} #total proposed units by land_use type
self.demolished_units = {} #total (to be) demolished units by land_use type
components_building_type_ids = self.proposal_component_set.get_attribute("building_type_id").astype("int32")
proposal_ids = self.proposal_set.get_id_attribute()
proposal_ids_in_component_set = self.proposal_component_set.get_attribute("proposal_id")
all_units_proposed = self.proposal_component_set.get_attribute("units_proposed")
number_of_components_in_proposals = self.proposal_set.get_attribute("number_of_components")
self.accepting_proposals = zeros(current_target_vacancy.get_attribute("building_type_id").max()+1, dtype='bool8') #whether accepting new proposals, for each building type
self.accepted_proposals = [] # index of accepted proposals
self.target_vacancies = {}
tv_building_types = current_target_vacancy.get_attribute("building_type_id")
tv_rate = current_target_vacancy.get_attribute("target_vacancy_rate")
for itype in range(tv_building_types.size):
self.target_vacancies[tv_building_types[itype]] = tv_rate[itype]
self.check_vacancy_rates(current_target_vacancy) #initialize self.accepting_proposal based on current vacancy rate
sqft_per_job = self.dataset_pool.get_dataset("building_sqft_per_job")
zones_of_proposals = self.proposal_set.get_attribute("zone_id")
self.building_sqft_per_job_table = sqft_per_job.get_building_sqft_as_table(zones_of_proposals.max(),
tv_building_types.max())
# consider only those proposals that have all components of accepted type and sum of proposed units > 0
is_accepted_type = self.accepting_proposals[components_building_type_ids]
sum_is_accepted_type_over_proposals = array(ndimage.sum(is_accepted_type, labels = proposal_ids_in_component_set,
index = proposal_ids))
sum_of_units_proposed = array(ndimage.sum(all_units_proposed, labels = proposal_ids_in_component_set,
index = proposal_ids))
is_proposal_eligible = logical_and(sum_is_accepted_type_over_proposals == number_of_components_in_proposals,
sum_of_units_proposed > 0)
is_proposal_eligible = logical_and(is_proposal_eligible,
self.proposal_set.get_attribute("start_year")==current_year )
## handle planned proposals: all proposals with status_id == is_planned
## and start_year == current_year are accepted
planned_proposal_indexes = where(logical_and(
self.proposal_set.get_attribute("status_id") == self.proposal_set.id_planned,
self.proposal_set.get_attribute("start_year") == current_year )
)[0]
#.........这里部分代码省略.........
开发者ID:christianurich,项目名称:VIBe2UrbanSim,代码行数:103,代码来源:development_project_proposal_sampling_model.py
示例11: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run(self, realestate_dataset,
living_units_dataset,
year=None,
occupied_spaces_variable="occupied_units",
total_spaces_variable="total_units",
target_attribute_name='target_vacancy_rate',
sample_from_dataset = None,
living_units_from_dataset = None,
sample_filter="",
reset_attribute_value={},
year_built = 'year_built',
dataset_pool=None,
append_to_realestate_dataset = False,
table_name = "development_projects",
dataset_name = "development_project",
id_name = 'development_project_id',
**kwargs):
"""
sample_filter attribute/variable indicates which records in the dataset are eligible in the sampling for removal or cloning
append_to_realestate_dataset - whether to append the new dataset to realestate_dataset
"""
if self.target_vancy_dataset is None:
raise RuntimeError, "target_vacancy_rate dataset is unspecified."
if not sample_from_dataset or not living_units_from_dataset:
logger.log_note('No development projects or no living units of development projects to sample from. Development projects are taken from building dataset and thus living units from living_units dataset.')
sample_from_dataset = realestate_dataset
living_units_from_dataset = living_units_dataset
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.target_vancy_dataset.get_attribute('year')==year)[0]
target_vacancy_for_this_year = DatasetSubset(self.target_vancy_dataset, this_year_index)
column_names = list(set( self.target_vancy_dataset.get_known_attribute_names() ) - set( [ target_attribute_name, occupied_spaces_variable, total_spaces_variable, 'year', '_hidden_id_'] ))
column_names.sort(reverse=True)
column_values = dict([ (name, target_vacancy_for_this_year.get_attribute(name)) for name in column_names + [target_attribute_name]])
independent_variables = list(set([re.sub('_max$', '', re.sub('_min$', '', col)) for col in column_names]))
sample_dataset_known_attributes = sample_from_dataset.get_known_attribute_names()
for attribute in independent_variables:
if attribute not in sample_dataset_known_attributes:
sample_from_dataset.compute_one_variable_with_unknown_package(attribute, dataset_pool=dataset_pool)
sample_dataset_known_attributes = sample_from_dataset.get_known_attribute_names() #update after compute
if sample_filter:
short_name = VariableName(sample_filter).get_alias()
if short_name not in sample_dataset_known_attributes:
filter_indicator = sample_from_dataset.compute_variables(sample_filter, dataset_pool=dataset_pool)
else:
filter_indicator = sample_from_dataset.get_attribute(short_name)
else:
filter_indicator = 1
sampled_index = array([], dtype=int32)
#log header
if PrettyTable is not None:
status_log = PrettyTable()
status_log.set_field_names(column_names + ["actual", "target", "expected", "difference", "action"])
else:
logger.log_status("\t".join(column_names + ["actual", "target", "expected", "difference", "action"]))
error_log = ''
for index in range(target_vacancy_for_this_year.size()):
sample_indicator = ones( sample_from_dataset.size(), dtype='bool' )
criterion = {} # for logging
for attribute in independent_variables:
if attribute in sample_dataset_known_attributes:
sample_attribute = sample_from_dataset.get_attribute(attribute)
else:
raise ValueError, "attribute %s used in target vacancy dataset can not be found in dataset %s" % (attribute, realestate_dataset.get_dataset_name())
if attribute + '_min' in column_names:
amin = target_vacancy_for_this_year.get_attribute(attribute+'_min')[index]
criterion.update({attribute + '_min':amin})
if amin != -1:
sample_indicator *= sample_attribute >= amin
if attribute + '_max' in column_names:
amax = target_vacancy_for_this_year.get_attribute(attribute+'_max')[index]
criterion.update({attribute + '_max':amax})
if amax != -1:
sample_indicator *= sample_attribute <= amax
if attribute in column_names:
aval = column_values[attribute][index]
criterion.update({attribute:aval})
if aval == -1:
continue
elif aval == -2: ##treat -2 in control totals column as complement set, i.e. all other values not already specified in this column
sample_indicator *= logical_not(ismember(sample_attribute, column_values[attribute]))
else:
sample_indicator *= sample_attribute == aval
this_total_spaces_variable, this_occupied_spaces_variable = total_spaces_variable, occupied_spaces_variable
## total/occupied_spaces_variable can be specified either as a universal name for all realestate
## or in targe_vacancy_rate dataset for each vacancy category
if occupied_spaces_variable in target_vacancy_for_this_year.get_known_attribute_names():
#.........这里部分代码省略.........
示例12: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run(self, year=None,
target_attribute_name='number_of_households',
sample_filter="",
reset_dataset_attribute_value={},
dataset_pool=None, **kwargs):
""" sample_filter attribute/variable indicates which records in the dataset are eligible in the sampling for removal or cloning
"""
#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.control_totals.get_attribute('year')==year)[0]
control_totals_for_this_year = DatasetSubset(self.control_totals, this_year_index)
column_names = list(set( self.control_totals.get_known_attribute_names() ) - set( [ target_attribute_name, 'year', '_hidden_id_'] ))
column_names.sort(reverse=True)
column_values = dict([ (name, control_totals_for_this_year.get_attribute(name)) for name in column_names + [target_attribute_name]])
independent_variables = list(set([re.sub('_max$', '', re.sub('_min$', '', col)) for col in column_names]))
dataset_known_attributes = self.dataset.get_known_attribute_names()
for variable in independent_variables:
if variable not in dataset_known_attributes:
self.dataset.compute_one_variable_with_unknown_package(variable, dataset_pool=dataset_pool)
dataset_known_attributes = self.dataset.get_known_attribute_names() #update after compute
if sample_filter:
short_name = VariableName(sample_filter).get_alias()
if short_name not in dataset_known_attributes:
filter_indicator = self.dataset.compute_variables(sample_filter, dataset_pool=dataset_pool)
else:
filter_indicator = self.dataset.get_attribute(short_name)
else:
filter_indicator = 1
to_be_cloned = array([], dtype=int32)
to_be_removed = array([], dtype=int32)
#log header
if PrettyTable is not None:
status_log = PrettyTable()
status_log.set_field_names(column_names + ["actual", "target", "difference", "action"])
else:
logger.log_status("\t".join(column_names + ["actual", "target", "difference", "action"]))
error_log = ''
for index in range(control_totals_for_this_year.size()):
lucky_index = None
indicator = ones( self.dataset.size(), dtype='bool' )
criterion = {}
for attribute in independent_variables:
if attribute in dataset_known_attributes:
dataset_attribute = self.dataset.get_attribute(attribute)
else:
raise ValueError, "attribute %s used in control total dataset can not be found in dataset %s" % (attribute, self.dataset.get_dataset_name())
if attribute + '_min' in column_names:
amin = column_values[attribute + '_min'][index]
criterion.update({attribute + '_min':amin})
if amin != -1:
indicator *= dataset_attribute >= amin
if attribute + '_max' in column_names:
amax = column_values[attribute+'_max'][index]
criterion.update({attribute + '_max':amax})
if amax != -1:
indicator *= dataset_attribute <= amax
if attribute in column_names:
aval = column_values[attribute][index]
criterion.update({attribute:aval})
if aval == -1:
continue
elif aval == -2: ##treat -2 in control totals column as complement set, i.e. all other values not already specified in this column
complement_values = setdiff1d( dataset_attribute, column_values[attribute] )
has_one_of_the_complement_value = zeros(dataset_attribute.size, dtype='bool')
for value in complement_values:
has_one_of_the_complement_value += dataset_attribute == value
indicator *= has_one_of_the_complement_value
else:
indicator *= dataset_attribute == aval
target_num = column_values[target_attribute_name][index]
## if accounting attribute is None, count number of agents with indicator = True
if self.dataset_accounting_attribute is None:
actual_num = indicator.sum()
action_num = 0
diff = target_num - actual_num
if actual_num != target_num:
legit_index = where(logical_and(indicator, filter_indicator))[0]
if legit_index.size > 0:
if actual_num < target_num:
lucky_index = sample_replace(legit_index, target_num - actual_num)
to_be_cloned = concatenate((to_be_cloned, lucky_index))
elif actual_num > target_num:
lucky_index = sample_noreplace(legit_index, actual_num-target_num)
to_be_removed = concatenate((to_be_removed, lucky_index))
action_num = lucky_index.size
else:
error_log += "There is nothing to sample from %s and no action will happen for" % self.dataset.get_dataset_name() + \
','.join([col+"="+str(criterion[col]) for col in column_names]) + '\n'
else:
## sum accounting attribute for agents with indicator = True;
## assume dataset_accouting_attribute is a primary attribute
accounting = self.dataset.get_attribute(self.dataset_accounting_attribute) * indicator
actual_num = accounting.sum()
#.........这里部分代码省略.........
示例13: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run(self, dataset, outcome_attribute, weight_attribute,
control_totals, current_year, control_total_attribute=None,
year_attribute='year', capacity_attribute=None, add_quantity=False, dataset_pool=None):
"""'dataset' is a Dataset for which a quantity 'outcome_attribute' is created. The total amount of the quantity is
given by the attribute 'control_total_attribute' of the 'control_totals' Dataset. If it is not given, it is assumed
to have the same name as 'outcome_attribute'. The 'weight_attribute' of 'dataset' determines the allocation weights.
The 'control_totals' Dataset contains an attribute 'year' (or alternatively, an attribute given by the 'year_attribute' argument)
and optionally other attributes that must be known to the 'dataset' (such as a geography). For each row of the control_totals dataset
for which year matches the 'current_year', the total amount is distributed among the corresponding members of 'dataset' according to weights.
If a 'capacity_attribute' is given (attribute of 'dataset'), the algorithm removes any allocations that exceeds the capacity and
redistributes it among remaining members. The resulting values are appended to 'dataset' as 'outcome_attribute' (as primary attribute).
If add_quantity is True and the 'outcome_attribute' exists in dataset, the resulting values are added to the current values of
'outcome_attribute'.
"""
ct_attr = control_totals.get_known_attribute_names()
if year_attribute not in ct_attr:
raise StandardError, "Year attribute '%s' must be a known attribute of the control totals dataset." % year_attribute
ct_attr.remove(year_attribute)
if control_total_attribute is None:
control_total_attribute = outcome_attribute
if control_total_attribute not in ct_attr:
raise StandardError, "Attribute '%s' must be a known attribute of the control totals dataset." % control_total_attribute
ct_attr.remove(control_total_attribute)
if control_totals._is_hidden_id():
ct_attr.remove(control_totals.id_name()[0])
# compute weights and other attributes necessary for allocation
attrs_to_compute = [weight_attribute] + ct_attr
if capacity_attribute is not None:
attrs_to_compute.append(capacity_attribute)
for attr in attrs_to_compute:
try:
dataset.compute_variables(attr, dataset_pool=dataset_pool)
except:
dataset.compute_one_variable_with_unknown_package(attr, dataset_pool=dataset_pool)
# create subset of control totals for the current year
year_index = where(control_totals.get_attribute(year_attribute) == current_year)[0]
if year_index.size <= 0:
logger.log_warning("No control total for year %s" % current_year)
return None
control_totals_for_this_year = DatasetSubset(control_totals, year_index)
# check capacity
if capacity_attribute is not None:
if dataset.get_attribute(capacity_attribute).sum() < control_totals_for_this_year.get_attribute(control_total_attribute).sum():
logger.log_warning("Capacity (%s) is smaller than the amount to allocate (%s)." % (dataset.get_attribute(capacity_attribute).sum(),
control_totals_for_this_year.get_attribute(control_total_attribute).sum()))
C = dataset.get_attribute(capacity_attribute).astype('int32')
all_weights = dataset.get_attribute(weight_attribute)
outcome = zeros(dataset.size(), dtype='int32')
for ct_row in range(control_totals_for_this_year.size()):
is_considered = ones(dataset.size(), dtype='bool8')
for characteristics in ct_attr:
is_considered = logical_and(is_considered, dataset.get_attribute(characteristics) == control_totals_for_this_year.get_attribute(characteristics)[ct_row])
T = control_totals_for_this_year.get_attribute(control_total_attribute)[ct_row]
it = 1
while True:
is_considered_idx = where(is_considered)[0]
weights = all_weights[is_considered_idx]
weights_sum = float(weights.sum())
outcome[is_considered_idx] = round_(outcome[is_considered_idx] + T * (weights/weights_sum)).astype('int32')
if capacity_attribute is None:
break
diff = outcome[is_considered_idx] - C[is_considered_idx]
outcome[is_considered_idx] = clip(outcome[is_considered_idx], 0, C[is_considered_idx])
if it == 1 and C[is_considered_idx].sum() < T:
logger.log_warning("Control total %s cannot be met due to a capacity restriction of %s" % (T, C[is_considered_idx].sum()))
T = where(diff < 0, 0, diff).sum()
if T <= 0:
break
is_considered = logical_and(is_considered, outcome < C)
it += 1
if add_quantity and (outcome_attribute in dataset.get_known_attribute_names()):
dataset.modify_attribute(name=outcome_attribute, data=outcome+dataset.get_attribute(outcome_attribute))
logger.log_status('New values added to the attribute %s of dataset %s.' % (outcome_attribute, dataset.get_dataset_name()))
else:
dataset.add_primary_attribute(name=outcome_attribute, data=outcome)
logger.log_status('New values stored into attribute %s of dataset %s.' % (outcome_attribute, dataset.get_dataset_name()))
dataset.flush_attribute(outcome_attribute)
return outcome
示例14: DevelopmentProjectTransitionModel
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
class DevelopmentProjectTransitionModel( Model ):
"""
Creates development projects. Each development project is for a single type
of development, e.g. 'industrial' or 'commercial'. This model creates
enough development projects to match the desired vacancy rates, as defined in the target_vacancies
table. It does not place any projects in locations; that is the job of the development project
location choice models. The distribution of project sizes (amount of space, value of space) is
determined by sampling from the projects in the development_event_history table.
"""
model_name = "Development Project Transition Model"
def __init__( self, debuglevel=0 ):
self.debug = DebugPrinter( debuglevel )
def pre_check( self, location_set, vacancy_table, types ):
for ptype in types:
self.check_for_space( location_set.get_attribute(self.variable_for_total_units[ptype]))
self.check_target_vacancy_is_not_100_percent( vacancy_table.get_attribute( "target_total_vacancy"))
def check_for_space( self, values ):
"""Check that this array of values sums to something > 0."""
self.do_check( "x > 0", array( [values.sum()] ) )
def check_target_vacancy_is_not_100_percent( self, value ):
"""Check that the target vacancy rate is not 100% (ratio == 1), because it doesn't make sense,
and it also causes a divide by 0 error."""
self.do_check( "x < 1", value )
def run( self, vacancy_table, history_table, year, location_set, dataset_pool=None, resources=None ):
self.dataset_pool=dataset_pool
building_types = self.dataset_pool.get_dataset('building_type')
target_vacancy_this_year = DatasetSubset(vacancy_table, index=where(vacancy_table.get_attribute("year")==year)[0])
building_type_ids = target_vacancy_this_year.get_attribute('building_type_id')
building_type_idx = building_types.get_id_index(building_type_ids)
self.used_building_types = DatasetSubset(building_types, index=building_type_idx)
project_types = self.used_building_types.get_attribute('building_type_name')
is_residential = self.used_building_types.get_attribute('is_residential')
unit_names = where(is_residential, 'residential_units', 'non_residential_sqft')
specific_unit_names = where(is_residential, 'residential_units', '_sqft')
rates = target_vacancy_this_year.get_attribute('target_total_vacancy')
self.project_units = {}
self.project_specific_units = {}
target_rates = {}
for i in range(self.used_building_types.size()):
self.project_units[project_types[i]] = unit_names[i]
if is_residential[i]:
self.project_specific_units[project_types[i]] = specific_unit_names[i]
else:
self.project_specific_units[project_types[i]] = "%s%s" % (project_types[i], specific_unit_names[i])
target_rates[building_type_ids[i]] = rates[i]
self._compute_vacancy_and_total_units_variables(location_set, project_types, resources)
self.pre_check( location_set, target_vacancy_this_year, project_types)
projects = None
for project_type_id, target_vacancy_rate in target_rates.iteritems():
# determine current-year vacancy rates
project_type = building_types.get_attribute_by_id('building_type_name', project_type_id)
vacant_units_sum = location_set.get_attribute(self.variable_for_vacancy[project_type]).sum()
units_sum = float( location_set.get_attribute(self.variable_for_total_units[project_type]).sum() )
should_develop_units = int(round(max( 0, ( target_vacancy_rate * units_sum - vacant_units_sum ) /
( 1 - target_vacancy_rate ) )))
logger.log_status(project_type + ": vacant units: %d, should be vacant: %f, sum units: %d"
% (vacant_units_sum, target_vacancy_rate * units_sum, units_sum))
if not should_develop_units:
logger.log_note(("Will not build any " + project_type + " units, because the current vacancy of %d units\n"
+ "is more than the %d units desired for the vacancy rate of %f.")
% (vacant_units_sum,
target_vacancy_rate * units_sum,
target_vacancy_rate))
#create projects
if should_develop_units > 0:
this_project = self._create_projects(should_develop_units, project_type, project_type_id, history_table,
location_set, units_sum, resources)
if projects is None:
projects = this_project
else:
projects.join_by_rows(this_project, change_ids_if_not_unique=True)
return projects
def _compute_vacancy_and_total_units_variables(self, location_set, project_types, resources=None):
compute_resources = Resources(resources)
compute_resources.merge({"debug":self.debug})
self.variable_for_vacancy = {}
self.variable_for_total_units = {}
for ptype in project_types:
self.variable_for_vacancy[ptype] = compute_resources.get(
"%s_vacant_variable" % ptype,
"urbansim_zone.%s.vacant_%s" % (location_set.get_dataset_name(),
self.project_specific_units[ptype]))
self.variable_for_total_units[ptype] = compute_resources.get(
"%s_total_units_variable" % ptype,
"%s.aggregate(urbansim_zone.building.total_%s)" % (location_set.get_dataset_name(),
self.project_specific_units[ptype]))
location_set.compute_variables([self.variable_for_vacancy[ptype], self.variable_for_total_units[ptype]],
dataset_pool=self.dataset_pool, resources = compute_resources)
def _create_projects(self, should_develop_units, project_type, project_type_id, history_table, location_set, units_sum, resources=None):
#.........这里部分代码省略.........
示例15: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import get_attribute [as 别名]
def run(
self,
realestate_dataset,
year=None,
occupied_spaces_variable="occupied_units",
total_spaces_variable="total_units",
target_attribute_name="target_vacancy_rate",
sample_from_dataset=None,
sample_filter="",
reset_attribute_value={},
year_built="year_built",
dataset_pool=None,
append_to_realestate_dataset=False,
table_name="development_projects",
dataset_name="development_project",
id_name="development_project_id",
**kwargs
):
"""
sample_filter attribute/variable indicates which records in the dataset are eligible in the sampling for removal or cloning
append_to_realestate_dataset - whether to append the new dataset to realestate_dataset
"""
if self.target_vancy_dataset is None:
raise RuntimeError, "target_vacancy_rate dataset is unspecified."
if not sample_from_dataset:
sample_from_dataset = realestate_dataset
# 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.target_vancy_dataset.get_attribute("year") == year)[0]
target_vacancy_for_this_year = DatasetSubset(self.target_vancy_dataset, this_year_index)
column_names = list(
set(self.target_vancy_dataset.get_known_attribute_names())
- set([target_attribute_name, occupied_spaces_variable, total_spaces_variable, "year", "_hidden_id_"])
)
column_names.sort(reverse=True)
column_values = dict(
[
(name, target_vacancy_for_this_year.get_attribute(name))
for name in column_names + [target_attribute_name]
]
)
independent_variables = list(set([re.sub("_max$", "", re.sub("_min$", "", col)) for col in column_names]))
dataset_known_attributes = realestate_dataset.get_known_attribute_names()
sample_dataset_known_attributes = sample_from_dataset.get_known_attribute_names()
for variable in independent_variables:
if variable not in dataset_known_attributes:
realestate_dataset.compute_one_variable_with_unknown_package(variable, dataset_pool=dataset_pool)
if variable not in sample_dataset_known_attributes:
sample_from_dataset.compute_one_variable_with_unknown_package(variable, dataset_pool=dataset_pool)
dataset_known_attributes = realestate_dataset.get_known_attribute_names() # update after compute
if sample_filter:
short_name = VariableName(sample_filter).get_alias()
if short_name not in dataset_known_attributes:
filter_indicator = sample_from_dataset.compute_variables(sample_filter, dataset_pool=dataset_pool)
else:
filter_indicator = sample_from_dataset.get_attribute(short_name)
else:
filter_indicator = 1
sampled_index = array([], dtype=int32)
# log header
if PrettyTable is not None:
status_log = PrettyTable()
status_log.set_field_names(column_names + ["actual", "target", "expected", "difference", "action"])
else:
logger.log_status("\t".join(column_names + ["actual", "target", "expected", "difference", "action"]))
error_log = ""
for index in range(target_vacancy_for_this_year.size()):
this_sampled_index = array([], dtype=int32)
indicator = ones(realestate_dataset.size(), dtype="bool")
sample_indicator = ones(sample_from_dataset.size(), dtype="bool")
criterion = {} # for logging
for attribute in independent_variables:
if attribute in dataset_known_attributes:
dataset_attribute = realestate_dataset.get_attribute(attribute)
sample_attribute = sample_from_dataset.get_attribute(attribute)
else:
raise ValueError, "attribute %s used in target vacancy dataset can not be found in dataset %s" % (
attribute,
realestate_dataset.get_dataset_name(),
)
if attribute + "_min" in column_names:
amin = target_vacancy_for_this_year.get_attribute(attribute + "_min")[index]
criterion.update({attribute + "_min": amin})
if amin != -1:
indicator *= dataset_attribute >= amin
sample_indicator *= sample_attribute >= amin
if attribute + "_max" in column_names:
amax = target_vacancy_for_this_year.get_attribute(attribute + "_max")[index]
criterion.update({attribute + "_max": amax})
#.........这里部分代码省略.........