本文整理汇总了Python中opus_core.datasets.dataset.DatasetSubset.size方法的典型用法代码示例。如果您正苦于以下问题:Python DatasetSubset.size方法的具体用法?Python DatasetSubset.size怎么用?Python DatasetSubset.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opus_core.datasets.dataset.DatasetSubset
的用法示例。
在下文中一共展示了DatasetSubset.size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
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)
large_area_ids = control_totals.get_attribute("large_area_id")
jobs_large_area_ids = job_set.compute_variables("washtenaw.job.large_area_id")
unique_large_areas = unique(large_area_ids)
is_year = control_totals.get_attribute("year")==year
all_jobs_index = arange(job_set.size())
sectors = unique(control_totals.get_attribute("sector_id")[is_year])
self._compute_sector_variables(sectors, job_set)
for area in unique_large_areas:
idx = where(logical_and(is_year, large_area_ids == area))[0]
self.control_totals_for_this_year = DatasetSubset(control_totals, idx)
jobs_index = where(jobs_large_area_ids == area)[0]
jobs_for_this_area = DatasetSubset(job_set, jobs_index)
logger.log_status("ETM for area %s (currently %s jobs)" % (area, jobs_for_this_area.size()))
last_remove_idx = self.remove_jobs.size
self._do_run_for_this_year(jobs_for_this_area)
add_jobs_size = self.new_jobs[self.location_id_name].size-self.new_jobs["large_area_id"].size
remove_jobs_size = self.remove_jobs.size-last_remove_idx
logger.log_status("add %s, remove %s, total %s" % (add_jobs_size, remove_jobs_size,
jobs_for_this_area.size()+add_jobs_size-remove_jobs_size))
self.new_jobs["large_area_id"] = concatenate((self.new_jobs["large_area_id"],
array(add_jobs_size*[area], dtype="int32")))
# transform indices of removing jobs into indices of the whole dataset
self.remove_jobs[last_remove_idx:self.remove_jobs.size] = all_jobs_index[jobs_index[self.remove_jobs[last_remove_idx:self.remove_jobs.size]]]
self._update_job_set(job_set)
idx_new_jobs = arange(job_set.size()-self.new_jobs["large_area_id"].size, job_set.size())
jobs_large_area_ids = job_set.compute_variables("washtenaw.job.large_area_id")
jobs_large_area_ids[idx_new_jobs] = self.new_jobs["large_area_id"]
job_set.delete_one_attribute("large_area_id")
job_set.add_attribute(jobs_large_area_ids, "large_area_id", metadata=AttributeType.PRIMARY)
# return an index of new jobs
return arange(job_set.size()-self.new_jobs["large_area_id"].size, job_set.size())
示例2: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
def run(self, year, household_set, control_totals, characteristics, resources=None):
# self.person_set = person_set
self._do_initialize_for_run(household_set)
control_totals.get_attribute("total_number_of_households") # to make sure they are loaded
self.characteristics = characteristics
self.all_categories = self.characteristics.get_attribute("characteristic")
self.all_categories = array(map(lambda x: x.lower(), self.all_categories))
self.scaled_characteristic_names = get_distinct_names(self.all_categories).tolist()
self.marginal_characteristic_names = copy(control_totals.get_id_name())
index_year = self.marginal_characteristic_names.index("year")
self.marginal_characteristic_names.remove("year")
self.marginal_characteristic_names.remove(self.subarea_id_name)
region_ids = control_totals.get_attribute(self.subarea_id_name)
households_region_ids = household_set.compute_one_variable_with_unknown_package(variable_name="%s" % (self.subarea_id_name), dataset_pool=self.dataset_pool)
unique_regions = unique(region_ids)
is_year = control_totals.get_attribute("year")==year
all_households_index = arange(household_set.size())
for area in unique_regions:
idx = where(logical_and(is_year, region_ids == area))[0]
self.control_totals_for_this_year = DatasetSubset(control_totals, idx)
households_index = where(households_region_ids == area)[0]
if households_index.size == 0:
continue
households_for_this_area = DatasetSubset(household_set, households_index)
logger.log_status("HTM for area %s (currently %s households)" % (area, households_for_this_area.size()))
last_remove_idx = self.remove_households.size
last_new_hhs_idx = self.mapping_existing_hhs_to_new_hhs.size
self._do_run_for_this_year(households_for_this_area)
add_hhs_size = self.new_households[self.location_id_name].size-self.new_households[self.subarea_id_name].size+self.mapping_existing_hhs_to_new_hhs.size-last_new_hhs_idx
remove_hhs_size = self.remove_households.size-last_remove_idx
logger.log_status("add %s, remove %s, total %s" % (add_hhs_size, remove_hhs_size,
households_for_this_area.size()+add_hhs_size-remove_hhs_size
))
self.new_households[self.subarea_id_name] = concatenate((self.new_households[self.subarea_id_name],
array((self.new_households[self.location_id_name].size-self.new_households[self.subarea_id_name].size)*[area], dtype="int32")))
# transform indices of removing households into indices of the whole dataset
self.remove_households[last_remove_idx:self.remove_households.size] = all_households_index[households_index[self.remove_households[last_remove_idx:self.remove_households.size]]]
# do the same for households to be duplicated
self.mapping_existing_hhs_to_new_hhs[last_new_hhs_idx:self.mapping_existing_hhs_to_new_hhs.size] = all_households_index[households_index[self.mapping_existing_hhs_to_new_hhs[last_new_hhs_idx:self.mapping_existing_hhs_to_new_hhs.size]]]
self._update_household_set(household_set)
idx_new_households = arange(household_set.size()-self.new_households[self.subarea_id_name].size, household_set.size())
#household_region_ids = household_set.compute_variables("urbansim_parcel.household.%s" % self.subarea_id_name)
#household_region_ids[idx_new_households] = self.new_households[self.subarea_id_name]
region_ids = household_set.get_attribute(self.subarea_id_name).copy()
household_set.delete_one_attribute(self.subarea_id_name)
household_set.add_attribute(region_ids, self.subarea_id_name, metadata=AttributeType.PRIMARY)
# return an index of new households
return idx_new_households
示例3: _do_run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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)
示例4: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
def run(self, chunk_specification, dataset, dataset_index=None, result_array_type=float32, **kwargs):
""" 'chunk_specification' - determines number of chunks to use when computing over
the dataset set.
'dataset' - an object of class Dataset that is to be chunked.
'dataset_index' - index of individuals in dataset to be chunked.
'result_array_type' - type of the resulting array. Can be any numerical type of numpy array.
**kwargs - keyword arguments.
The method chunks dataset_index in the desired number of chunks (minimum is 1) and for each chunk it calls the method
'run_chunk'. The order of the individuals entering the chunking is determined by the method 'get_agents_order'.
"""
if dataset_index==None:
dataset_index=arange(dataset.size())
if not isinstance(dataset_index,ndarray):
dataset_index=array(dataset_index)
logger.log_status("Total number of individuals: %s" % dataset_index.size)
result_array = zeros(dataset_index.size, dtype=result_array_type)
if dataset_index.size <= 0:
logger.log_status("Nothing to be done.")
return result_array
all_indexed_individuals = DatasetSubset(dataset, dataset_index)
ordered_agent_indices = self.get_agents_order(all_indexed_individuals)# set order of individuals in chunks
# TODO: Remove next six lines after we inherit chunk specification as a text string.
if (chunk_specification is None):
chunk_specification = {'nchunks':1}
chunker = ChunkSpecification(chunk_specification)
self.number_of_chunks = chunker.nchunks(dataset_index)
chunksize = int(ceil(all_indexed_individuals.size()/float(self.number_of_chunks)))
for ichunk in range(self.number_of_chunks):
logger.start_block("%s chunk %d out of %d."
% (self.model_short_name, (ichunk+1), self.number_of_chunks))
self.index_of_current_chunk = ichunk
try:
chunk_agent_indices = ordered_agent_indices[arange((ichunk*chunksize),
min((ichunk+1)*chunksize,
all_indexed_individuals.size()))]
logger.log_status("Number of agents in this chunk: %s" % chunk_agent_indices.size)
result_array[chunk_agent_indices] = self.run_chunk(dataset_index[chunk_agent_indices],
dataset, **kwargs).astype(result_array_type)
finally:
logger.end_block()
return result_array
示例5: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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
示例6: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
def run(self, n=500,
realestate_dataset_name = 'building',
current_year=None,
**kwargs):
target_vacancy = self.dataset_pool.get_dataset('target_vacancy')
if current_year is None:
year = SimulationState().get_current_time()
else:
year = current_year
self.current_year = year
this_year_index = where(target_vacancy['year']==year)[0]
target_vacancy_for_this_year = DatasetSubset(target_vacancy, this_year_index)
if target_vacancy_for_this_year.size() == 0:
raise IOError, 'No target vacancy defined for year %s.' % year
self.all_btypes_size = target_vacancy_for_this_year.size()
return DevelopmentProjectProposalSamplingModelWithMinimum.run(self, n=n, realestate_dataset_name=realestate_dataset_name,
current_year=current_year, **kwargs)
示例7: _do_run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
def _do_run(self, location_set, agent_set, agents_index, resources=None):
location_id_name = location_set.get_id_name()[0]
asubset = DatasetSubset(agent_set, agents_index)
if asubset.size() <= 0:
return array([], dtype='int32')
#unplace agents
agent_set.modify_attribute(location_id_name,
resize(array([-1]), asubset.size()), agents_index)
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)
if self.consider_capacity:
location_set.compute_variables([self.capacity_attribute],
dataset_pool=self.dataset_pool)
weights = location_subset[self.capacity_attribute]
if self.number_of_agents_attribute is not None:
location_set.compute_variables([self.number_of_agents_attribute],
dataset_pool=self.dataset_pool)
weights = clip(weights - location_subset[self.number_of_agents_attribute],
0, location_subset[self.capacity_attribute])
else:
weights = ones(location_subset.size())
if weights.sum() <=0:
logger.log_status("Locations' capacity sums to zero. Nothing to be done.")
return array([])
distr = weights/float(weights.sum())
random_sample = probsample_replace(location_subset.get_id_attribute(), size=asubset.size(),
prob_array=distr)
agent_set.modify_attribute(location_id_name, random_sample, agents_index)
return agent_set.get_attribute_by_index(location_id_name, agents_index)
示例8: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
def run(self, agent_set, **kwargs):
large_areas = agent_set.get_attribute(self.large_area_id_name)
valid_large_area = where(large_areas > 0)[0]
if valid_large_area.size > 0:
unique_large_areas = unique(large_areas[valid_large_area])
cond_array = zeros(agent_set.size(), dtype="bool8")
cond_array[valid_large_area] = True
result = array([], dtype="int32")
for area in unique_large_areas:
new_index = where(logical_and(cond_array, large_areas == area))[0]
agent_subset = DatasetSubset(agent_set, new_index)
logger.log_status("ARM for area %s (%s agents)" % (area, agent_subset.size()))
this_result = AgentRelocationModel.run(self, agent_subset, **kwargs)
result = concatenate((result, new_index[this_result]))
no_large_area = where(large_areas <= 0)[0]
result = concatenate((result, no_large_area))
return result
示例9: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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})
#.........这里部分代码省略.........
示例10: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [as 别名]
def run(self, in_storage, out_storage=None, business_dsname="business", zone_dsname=None):
dataset_pool = DatasetPool(storage=in_storage, package_order=['psrc_parcel', 'urbansim_parcel', 'urbansim', 'opus_core'] )
seed(1)
allbusinesses = dataset_pool.get_dataset(business_dsname)
parcels = dataset_pool.get_dataset('parcel')
buildings = dataset_pool.get_dataset('building')
parcels.compute_variables(["urbansim_parcel.parcel.residential_units", "number_of_buildings = parcel.number_of_agents(building)",
"non_residential_sqft = (parcel.aggregate(building.non_residential_sqft)).astype(int32)",
"number_of_res_buildings = parcel.aggregate(urbansim_parcel.building.is_residential)",
"number_of_nonres_buildings = parcel.aggregate(urbansim_parcel.building.is_non_residential)",
"number_of_mixed_use_buildings = parcel.aggregate(urbansim_parcel.building.is_generic_building_type_6)"
],
dataset_pool=dataset_pool)
restypes = [12, 4, 19, 11, 34, 10, 33]
reslutypes = [13,14,15,24]
is_valid_business = ones(allbusinesses.size(), dtype='bool8')
parcels_not_matched = logical_and(in1d(allbusinesses["parcel_id"], parcels.get_id_attribute(), invert=True), allbusinesses["parcel_id"] > 0)
if(parcels_not_matched.sum() > 0):
is_valid_business[where(parcels_not_matched)] = False
logger.log_warning(message="No parcel exists for %s businesses (%s jobs)" % (parcels_not_matched.sum(),
allbusinesses[self.number_of_jobs_attr][where(parcels_not_matched)].sum()))
zero_parcel = allbusinesses["parcel_id"]<=0
if zero_parcel.sum() > 0:
is_valid_business[where(zero_parcel)] = False
logger.log_warning(message="%s businesses (%s jobs) located on zero parcel_id" % (zero_parcel.sum(),
allbusinesses[self.number_of_jobs_attr][where(zero_parcel)].sum()))
zero_size = logical_and(is_valid_business, allbusinesses[self.number_of_jobs_attr].round() == 0)
if(sum(zero_size) > 0):
is_valid_business[where(zero_size)] = False
logger.log_warning(message="%s businesses are of size 0." % sum(zero_size))
businesses = DatasetSubset(allbusinesses, index=where(is_valid_business)[0])
parcels.add_attribute(name="number_of_workplaces", data=parcels.sum_dataset_over_ids(businesses, constant=1))
has_single_res_buildings = logical_and(parcels["number_of_buildings"] == 1, parcels["number_of_res_buildings"] == 1) # 1 (1 residential)
parcels.add_attribute(data=has_single_res_buildings.astype("int32"), name="buildings_code")
has_mult_res_buildings = logical_and(parcels["number_of_buildings"] > 1, parcels["number_of_nonres_buildings"] == 0) # 2 (mult residential)
parcels.modify_attribute("buildings_code", data=2*ones(has_mult_res_buildings.sum()), index=where(has_mult_res_buildings))
has_single_nonres_buildings = logical_and(logical_and(parcels["number_of_buildings"] == 1, parcels["number_of_nonres_buildings"] == 1), parcels["number_of_mixed_use_buildings"] == 0) # 3 (1 non-res)
parcels.modify_attribute("buildings_code", data=3*ones(has_single_nonres_buildings.sum()), index=where(has_single_nonres_buildings))
has_mult_nonres_buildings = logical_and(logical_and(parcels["number_of_buildings"] > 1, parcels["number_of_res_buildings"] == 0), parcels["number_of_mixed_use_buildings"] == 0) # 4 (mult non-res)
parcels.modify_attribute("buildings_code", data=4*ones(has_mult_nonres_buildings.sum()), index=where(has_mult_nonres_buildings))
has_single_mixed_buildings = logical_and(parcels["number_of_buildings"] == 1, parcels["number_of_mixed_use_buildings"] == 1) # 5 (1 mixed-use)
parcels.modify_attribute("buildings_code", data=5*ones(has_single_mixed_buildings.sum()), index=where(has_single_mixed_buildings))
has_mult_mixed_buildings = logical_and(parcels["number_of_buildings"] > 1,
logical_or(logical_and(parcels["number_of_res_buildings"] > 0, parcels["number_of_nonres_buildings"] > 0),
logical_or(parcels["number_of_mixed_use_buildings"] > 1,
logical_and(parcels["number_of_res_buildings"] == 0,
parcels["number_of_mixed_use_buildings"] > 0)))) # 6
parcels.modify_attribute("buildings_code", data=6*ones(has_mult_mixed_buildings.sum()), index=where(has_mult_mixed_buildings))
has_no_building_res_lutype = logical_and(parcels["number_of_buildings"] == 0, in1d(parcels["land_use_type_id"], reslutypes)) # 7 (vacant with res LU type)
parcels.modify_attribute("buildings_code", data=7*ones(has_no_building_res_lutype.sum()), index=where(has_no_building_res_lutype))
has_no_building_nonres_lutype = logical_and(parcels["number_of_buildings"] == 0, in1d(parcels["land_use_type_id"], reslutypes)==0) # 8 (vacant with non-res LU type)
parcels.modify_attribute("buildings_code", data=8*ones(has_no_building_nonres_lutype.sum()), index=where(has_no_building_nonres_lutype))
business_sizes = businesses[self.number_of_jobs_attr].round().astype("int32")
business_location = {}
business_location1wrkpl = zeros(businesses.size(), dtype="int32")
business_location1wrkplres = zeros(businesses.size(), dtype="int32")
business_ids = businesses.get_id_attribute()
# sample one building for cases when sampling is required.
for ibusid in range(businesses.size()):
idx = where(buildings['parcel_id'] == businesses['parcel_id'][ibusid])[0]
bldgids = buildings['building_id'][idx]
business_location[business_ids[ibusid]] = bldgids
if bldgids.size == 1:
business_location1wrkpl[ibusid] = bldgids[0]
elif bldgids.size > 1:
business_location1wrkpl[ibusid] = bldgids[sample_noreplace(arange(bldgids.size), 1)]
if buildings['residential_units'][idx].sum() > 0:
# Residential buildings are sampled with probabilities proportional to residential units
business_location1wrkplres[ibusid] = bldgids[probsample_noreplace(arange(bldgids.size), 1, prob_array=buildings['residential_units'][idx])]
else:
business_location1wrkplres[ibusid] = business_location1wrkpl[ibusid]
home_based = zeros(business_sizes.sum(), dtype="bool8")
job_building_id = zeros(business_sizes.sum(), dtype="int32")
job_array_labels = business_ids.repeat(business_sizes)
job_assignment_case = zeros(business_sizes.sum(), dtype="int32")
processed_bindicator = zeros(businesses.size(), dtype="bool8")
business_codes = parcels.get_attribute_by_id("buildings_code", businesses["parcel_id"])
business_nworkplaces = parcels.get_attribute_by_id("number_of_workplaces", businesses["parcel_id"])
logger.log_status("Total number of jobs: %s" % home_based.size)
# 1. 1-2 worker business in 1 residential building
idx_sngl_wrk_1bld_fit = where(logical_and(business_sizes < 3, business_codes == 1))[0]
jidx = in1d(job_array_labels, business_ids[idx_sngl_wrk_1bld_fit])
home_based[jidx] = True
job_building_id[jidx] = business_location1wrkpl[idx_sngl_wrk_1bld_fit].repeat(business_sizes[idx_sngl_wrk_1bld_fit])
job_assignment_case[jidx] = 1
processed_bindicator[idx_sngl_wrk_1bld_fit] = True
logger.log_status("1. %s jobs (%s businesses) set as home-based due to 1-2 worker x 1 residential building fit." % (
business_sizes[idx_sngl_wrk_1bld_fit].sum(), idx_sngl_wrk_1bld_fit.size))
# 2. 1-2 worker business in multiple residential buildings
idx_sngl_wrk_multbld_fit = where(logical_and(logical_and(processed_bindicator==0, business_sizes < 3), business_codes == 2))[0]
jidx = in1d(job_array_labels, business_ids[idx_sngl_wrk_multbld_fit])
home_based[jidx] = True
#.........这里部分代码省略.........
示例11: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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
示例12: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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
示例13: DevelopmentProjectTransitionModel
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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):
#.........这里部分代码省略.........
示例14: run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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():
#.........这里部分代码省略.........
示例15: prepare_for_run
# 需要导入模块: from opus_core.datasets.dataset import DatasetSubset [as 别名]
# 或者: from opus_core.datasets.dataset.DatasetSubset import size [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')
if 'units_proposed' not in existing_proposal_set_parent.get_known_attribute_names():
## compute 'units_proposed' and add it as a primary attribute (as it may be missing when loaded from the base_year_data)
units_proposed = existing_proposal_set_parent.compute_variables(proposed_units_variable, dataset_pool)
existing_proposal_set_parent.add_attribute(units_proposed, "units_proposed", AttributeType.PRIMARY)
#load proposals whose status_id are not of id_tentative or id_not_available
available_idx = where(in1d(existing_proposal_set_parent.get_attribute("status_id"),
array([DevelopmentProjectProposalDataset.id_active,
DevelopmentProjectProposalDataset.id_proposed,
DevelopmentProjectProposalDataset.id_planned,
DevelopmentProjectProposalDataset.id_with_velocity])))[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 = SimulationState().get_flush_datasets()
SimulationState().set_flush_datasets(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 and existing_proposal_set.size()>0:
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