本文整理汇总了Python中opus_core.session_configuration.SessionConfiguration.get_dataset_pool方法的典型用法代码示例。如果您正苦于以下问题:Python SessionConfiguration.get_dataset_pool方法的具体用法?Python SessionConfiguration.get_dataset_pool怎么用?Python SessionConfiguration.get_dataset_pool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opus_core.session_configuration.SessionConfiguration
的用法示例。
在下文中一共展示了SessionConfiguration.get_dataset_pool方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_tripgen_travel_model_input_files
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def test_create_tripgen_travel_model_input_files(self):
in_storage = StorageFactory().get_storage(
'sql_storage',
storage_location = self.database)
sc = SessionConfiguration(new_instance=True,
package_order = ['urbansim', 'psrc'],
in_storage=in_storage)
dataset_pool = sc.get_dataset_pool()
TravelModelInputFileWriter().run(self.tempdir_path, 2000, dataset_pool)
logger.log_status('tazdata path: ', self.tempdir_path)
# expected values - data format: {zone:{column_value:value}}
expected_tazdata = {1: [[1,1], [1,2]],
2: [[2,2]],
3: [],
4: [[2,2]]
}
# get real data from file
real_tazdata = {1:[],2:[], 3:[], 4:[]}
# income groups 1 to 4
for i in [1,2,3,4]:
tazdata_file = open(os.path.join(self.tempdir_path, 'tripgen', 'inputtg', 'tazdata.mf9%s' % i), 'r')
for a_line in tazdata_file.readlines():
if a_line[0].isspace():
numbers = a_line.split()
zone_id = int(numbers[0])
job_zone_id = int(numbers[1])
real_tazdata[i].append([zone_id, job_zone_id])
for group in expected_tazdata.keys():
self.assertEqual(real_tazdata[group], expected_tazdata[group],
"income group %d, columns did not match up."%group)
示例2: run
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def run(self, year, cache_directory=None):
"""The class is initialized with the appropriate configuration info from the
travel_model_configuration part of this config, and then copies the specified
UrbanSim data into files for daysim to read.
The variables/expressions to export are defined in the node travel_model_configuration/urbansim_to_tm_variable_mapping
of the configuration file.
"""
if cache_directory is None:
cache_directory = self.config['cache_directory']
simulation_state = SimulationState()
simulation_state.set_cache_directory(cache_directory)
simulation_state.set_current_time(year)
attribute_cache = AttributeCache()
sc = SessionConfiguration(new_instance=True,
package_order=self.config['dataset_pool_configuration'].package_order,
in_storage=attribute_cache)
dataset_pool = sc.get_dataset_pool()
tm_config = self.config['travel_model_configuration']
data_to_export = tm_config['urbansim_to_tm_variable_mapping']
table_names = data_to_export.keys()
variable_names = {}
datasets = {}
filenames = {}
in_table_names = {}
for table_name in table_names:
filter = data_to_export[table_name].get('__filter__', None)
if filter is not None:
del data_to_export[table_name]['__filter__']
out_table_name = data_to_export[table_name].get('__out_table_name__', None)
if out_table_name is not None:
del data_to_export[table_name]['__out_table_name__']
else:
out_table_name = table_name
variables_to_export = map(lambda alias: "%s = %s" % (alias, data_to_export[table_name][alias]), data_to_export[table_name].keys())
dataset_name = None
for var in variables_to_export:
var_name = VariableName(var)
if dataset_name is None:
dataset_name = var_name.get_dataset_name()
ds = dataset_pool.get_dataset(dataset_name)
datasets[dataset_name] = ds
filenames[dataset_name] = out_table_name
in_table_names[dataset_name] = table_name
if dataset_name not in variable_names.keys():
variable_names[dataset_name] = []
variable_names[dataset_name].append(var_name.get_alias())
ds.compute_variables([var_name], dataset_pool=dataset_pool)
if filter is not None:
filter_idx = where(ds.compute_variables(["__filter__ = %s" % filter], dataset_pool=dataset_pool)>0)[0]
ds = DatasetSubset(ds, index = filter_idx)
datasets[dataset_name] = ds
return self._call_input_file_writer(year, datasets, in_table_names, filenames, variable_names, dataset_pool)
示例3: setup_environment
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def setup_environment(cache_directory, year, package_order, additional_datasets={}):
gc.collect()
ss = SimulationState(new_instance=True)
ss.set_cache_directory(cache_directory)
ss.set_current_time(year)
ac = AttributeCache()
storage = ac.get_flt_storage_for_year(year)
sc = SessionConfiguration(new_instance=True,
package_order=package_order,
in_storage=ac)
logger.log_status("Setup environment for year %s. Use cache directory %s." % (year, storage.get_storage_location()))
dp = sc.get_dataset_pool()
for name, ds in additional_datasets.iteritems():
dp.replace_dataset(name, ds)
return dp
示例4: test_create_tripgen_travel_model_input_file
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def test_create_tripgen_travel_model_input_file(self):
in_storage = StorageFactory().get_storage(
'sql_storage',
storage_location = self.database)
sc = SessionConfiguration(new_instance=True,
package_order = ['urbansim', 'psrc'],
in_storage=in_storage)
dataset_pool = sc.get_dataset_pool()
#zone_set = dataset_pool.get_dataset('zone')
#hh_set = dataset_pool.get_dataset('household')
#job_set = dataset_pool.get_dataset('job')
#taz_col_set = dataset_pool.get_dataset('constant_taz_column')
TravelModelInputFileWriter().run(self.tempdir_path, 2000, dataset_pool)
logger.log_status('tazdata path: ', self.tempdir_path)
# expected values - data format: {zone:{column_value:value}}
expected_tazdata = {1:{101: 19.9,
102: 2., 103: 0., 104:1., 105:0.,
106: 3., 107:11., 109:1.,
110:0., 111:0., 112:0., 113:0., 114:0.,
115:0., 116:0., 117:0., 118:0., 119:0.,
120:2., 121:42., 122:0., 123:0., 124:11.},
2:{101: 29.9,
102: 0., 103: 2., 104:1., 105:3.,
106: 1., 107:3., 109:0.,
110:0., 111:0., 112:0., 113:3., 114:0.,
115:0., 116:0., 117:0., 118:1., 119:1.,
120:0., 121:241., 122:0., 123:0., 124:3.}}
# get real data from file
real_tazdata = {1:{},2:{}}
tazdata_file = open(os.path.join(self.tempdir_path, 'tripgen', 'inputtg', 'tazdata.ma2'), 'r')
for a_line in tazdata_file.readlines():
if a_line[0].isspace():
numbers = a_line.replace(':', ' ').split() # data line format: 1 101: 15.5
zone_id = int(numbers[0])
column_var = int(numbers[1])
value = float(numbers[2])
if value != -1:
real_tazdata[zone_id][column_var] = value
for zone in expected_tazdata.keys():
for col_var in expected_tazdata[zone].keys():
self.assertAlmostEqual(real_tazdata[zone][col_var], expected_tazdata[zone][col_var], 3,\
"zone %d, column variable %d did not match up."%(zone, col_var))
示例5: run
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def run(self, year):
"""This is the main entry point. The class is initialized with the appropriate configuration info from the
travel_model_configuration part of this config, and then copies the specified
UrbanSim data into files for emme/2 to read.
If households and jobs do not have a primary attribute zone_id, the entry 'locations_to_disaggregate'
in the travel_model_configuration should be a list of dataset names over which the zone_id
will be dissaggregated, ordered from higher to lower aggregation level, e.g. ['parcel', 'building']
"""
cache_directory = self.config['cache_directory']
simulation_state = SimulationState()
simulation_state.set_cache_directory(cache_directory)
simulation_state.set_current_time(year)
attribute_cache = AttributeCache()
sc = SessionConfiguration(new_instance=True,
package_order=self.config['dataset_pool_configuration'].package_order,
in_storage=attribute_cache)
dataset_pool = sc.get_dataset_pool()
hh_set = dataset_pool.get_dataset('household')
zone_set = dataset_pool.get_dataset('zone')
job_set = dataset_pool.get_dataset('job')
locations_to_disaggregate = self.config['travel_model_configuration']['locations_to_disaggregate']
len_locations_to_disaggregate = len(locations_to_disaggregate)
if len_locations_to_disaggregate > 0:
primary_location = locations_to_disaggregate[0]
if len_locations_to_disaggregate > 1:
intermediates_string = ", intermediates=["
for i in range(1, len_locations_to_disaggregate):
intermediates_string = "%s%s, " % (intermediates_string, locations_to_disaggregate[i])
intermediates_string = "%s]" % intermediates_string
else:
intermediates_string = ""
hh_set.compute_variables(['%s = household.disaggregate(%s.%s %s)' % (zone_set.get_id_name()[0],
primary_location, zone_set.get_id_name()[0],
intermediates_string)],
dataset_pool=dataset_pool)
job_set.compute_variables(['%s = job.disaggregate(%s.%s %s)' % (zone_set.get_id_name()[0],
primary_location, zone_set.get_id_name()[0],
intermediates_string)],
dataset_pool=dataset_pool)
return self._call_input_file_writer(year, dataset_pool)
示例6: run
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def run(self, data, upc_sequence, resources=None):
self.mnl_probabilities=upc_sequence.probability_class
self.bhhh_estimation = bhhh_mnl_estimation()
modified_upc_sequence = UPCFactory().get_model(
utilities=None, probabilities="opus_core.mnl_probabilities", choices=None)
modified_upc_sequence.utility_class = upc_sequence.utility_class
N, neqs, V = data.shape
max_iter = resources.get("max_iterations", 100) # default
sc = SessionConfiguration()
dataset_pool = sc.get_dataset_pool()
sample_rate = dataset_pool.get_dataset("sample_rate")
CLOSE = sc["CLOSE"]
info_filename = sc["info_file"]
info_filename = os.path.join('.', info_filename)
info_file = open(info_filename, "a")
constraint_dict = {1:'constrained', 0:'unconstrained'}
swing_cases_fix = 0 #set swing alternatives to constrained (1) or unconstrained (0)
prob_correlation = None
choice_set = resources['_model_'].choice_set
J = choice_set.size()
alt_id = choice_set.get_id_attribute()
movers = choice_set.get_attribute('movers')
resources.check_obligatory_keys(["capacity_string"])
supply = choice_set.get_attribute(resources["capacity_string"])
index = resources.get("index", None)
if index is None: # no sampling case, alternative set is the full choice_set
index = arange(J)
if index.ndim <= 1:
index = repeat(index[newaxis,:], N, axis=0)
if resources.get('aggregate_to_dataset', None):
aggregate_dataset = dataset_pool.get_dataset(resources.get('aggregate_to_dataset'))
choice_set_aggregate_id = choice_set.get_attribute(aggregate_dataset.get_id_name()[0])
index = aggregate_dataset.get_id_index(choice_set_aggregate_id[index].ravel()).reshape(index.shape)
supply = aggregate_dataset.get_attribute(resources["capacity_string"])
J = aggregate_dataset.size()
movers = aggregate_dataset.get_attribute("movers")
demand_history = movers[:, newaxis]
resources.merge({"index":index})
pi = ones(index.shape, dtype=float32) #initialize pi
#average_omega = ones(J,dtype=float32) #initialize average_omega
logger.start_block('Outer Loop')
for i in range(max_iter):
logger.log_status('Outer Loop Iteration %s' % i)
result = self.bhhh_estimation.run(data, modified_upc_sequence, resources)
del self.bhhh_estimation; collect()
self.bhhh_estimation = bhhh_mnl_estimation()
probability = modified_upc_sequence.get_probabilities()
if data.shape[2] == V: #insert a placeholder for ln(pi) in data
data = concatenate((data,ones((N,neqs,1),dtype=float32)), axis=2)
coef_names = resources.get("coefficient_names")
coef_names = concatenate( (coef_names, array(["ln_pi"])) )
resources.merge({"coefficient_names":coef_names})
else:
beta_ln_pi = result['estimators'][where(coef_names == 'ln_pi')][0]
logger.log_status("mu = 1/%s = %s" % (beta_ln_pi, 1/beta_ln_pi))
prob_hat = safe_array_divide(probability, pi ** beta_ln_pi)
#prob_hat = safe_array_divide(probability, pi)
prob_hat_sum = prob_hat.sum(axis=1, dtype=float32)
if not ma.allclose(prob_hat_sum, 1.0):
logger.log_status("probability doesn't sum up to 1, with minimum %s, and maximum %s" %
(prob_hat_sum.min(), prob_hat_sum.max()))
probability = normalize(prob_hat)
demand = self.mnl_probabilities.get_demand(index, probability, J) * 1 / sample_rate
demand_history = concatenate((demand_history,
demand[:, newaxis]),
axis=1)
sdratio = safe_array_divide(supply, demand, return_value_if_denominator_is_zero=2.0)
sdratio_matrix = sdratio[index]
## debug info
from numpy import histogram
from opus_core.misc import unique
cc = histogram(index.ravel(), unique(index.ravel()))[0]
logger.log_status( "=================================================================")
logger.log_status( "Probability min: %s, max: %s" % (probability.min(), probability.max()) )
logger.log_status( "Demand min: %s, max: %s" % (demand.min(), demand.max()) )
logger.log_status( "sdratio min: %s, max: %s" % (sdratio.min(), sdratio.max()) )
logger.log_status( "demand[sdratio==sdratio.min()]=%s" % demand[sdratio==sdratio.min()] )
logger.log_status( "demand[sdratio==sdratio.max()]=%s" % demand[sdratio==sdratio.max()] )
logger.log_status( "Counts of unique submarkets in alternatives min: %s, max: %s" % (cc.min(), cc.max()) )
logger.log_status( "=================================================================")
#.........这里部分代码省略.........
示例7: run
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def run(self, dataset1, dataset2, index1=None, index2=None, sample_size=10, weight=None,
include_chosen_choice=None, with_replacement=True, resources=None, dataset_pool=None):
"""
this function samples number of sample_size (scalar value) alternatives from dataset2
for agent set specified by dataset1.
If index1 is not None, only samples alterantives for agents with indices in index1;
if index2 is not None, only samples alternatives from indices in index2.
sample_size specifies number of alternatives to be sampled for each agent.
weight, to be used as sampling weight, is either an attribute name of dataset2, or a 1d
array of the same length as index2 or 2d array of shape (index1.size, index2.size).
Also refer to document of interaction_dataset"""
if dataset_pool is None:
sc = SessionConfiguration()
try:
dataset_pool=sc.get_dataset_pool()
except:
dataset_pool = DatasetPool(sc.package_order)
local_resources = Resources(resources)
local_resources.merge_if_not_None(
{"dataset1": dataset1, "dataset2": dataset2,
"index1":index1, "index2": index2,
"sample_size": sample_size, "weight": weight,
"with_replacement": with_replacement,
"include_chosen_choice": include_chosen_choice})
local_resources.check_obligatory_keys(['dataset1', 'dataset2', 'sample_size'])
agent = local_resources["dataset1"]
choice = local_resources["dataset2"]
index1 = local_resources.get("index1", None)
if index1 is None:
index1 = arange(agent.size())
index2 = local_resources.get("index2", None)
if index2 is None:
index2 = arange(choice.size())
if index1.size == 0 or index2.size == 0:
err_msg = "either choice size or agent size is zero, return None"
logger.log_warning(err_msg)
return (None, None)
agent_category_definition = local_resources.get("agent_category_definition", [])
choice_category_definition = local_resources.get("choice_category_definition", [])
agent_filter_attribute = local_resources.get("agent_filter_attribute", None)
category_inflating_factor = local_resources.get("category_inflating_factor", 10)
frequency, unique_agent_category_id, unique_choice_category_id, agent_category_id, choice_category_id = \
get_category_and_frequency(agent, agent_category_definition,
choice, choice_category_definition,
agent_filter_attribute, category_inflating_factor,
dataset_pool=dataset_pool)
include_chosen_choice = local_resources.get("include_chosen_choice", False)
chosen_choice_id = agent.get_attribute(choice.get_id_name()[0])[index1]
chosen_choice_index = choice.try_get_id_index(chosen_choice_id, return_value_if_not_found=-1)
chosen_choice_index_to_index2 = lookup(chosen_choice_index, index2, index_if_not_found=UNPLACED_ID)
J = local_resources["sample_size"]
if include_chosen_choice:
J = J - 1
local_resources.merge_with_defaults({'with_replacement': with_replacement})
with_replacement = local_resources.get("with_replacement")
sampled_index = empty((index1.size, J), dtype="int32")
sampling_prob = empty((index1.size, J), dtype="float64")
_digitize, _where, _normalize = digitize, where, normalize
_ncumsum, _rand, _searchsorted = ncumsum, rand, searchsorted #speed hack
for i in range(unique_agent_category_id.size):
category_id = unique_agent_category_id[i]
agents_in_this_category = _where(agent_category_id[index1] == category_id)[0]
num_agents = agents_in_this_category.size
if num_agents == 0: continue
#import pdb; pdb.set_trace()
## divide frequency by the mean frequency to avoid overflow
weights = frequency[i, _digitize(choice_category_id[index2], unique_choice_category_id)-1] / frequency[i, :].mean()
prob = _normalize(weights)
index = _searchsorted(_ncumsum(prob), _rand(num_agents * J)).reshape(-1, J)
if not with_replacement:
raise NotImplementedError, "Sample without replacement is not implemented for this sampler yet."
# nz = nonzero(prob)[0].size
# if J < nz:
# ## number of non zero weight less than alternatives, sample with replacement
# logger.log_warning("There are %s non zero weights and are less than the number of alternatives proposed %s. " % (nz, J) +
# "Sample with replacement instead.")
# continue
# i=0; max_iterations=200
# while True:
# index = sort(index, axis=1)
# where_repeats = nonzero( logical_not(diff(index, axis=1)) )
# num_repeats = where_repeats[0].size
# if num_repeats == 0: break
# index[where_repeats] = _searchsorted(_rand(num_repeats), prob)
# i += 1
#.........这里部分代码省略.........
示例8: run
# 需要导入模块: from opus_core.session_configuration import SessionConfiguration [as 别名]
# 或者: from opus_core.session_configuration.SessionConfiguration import get_dataset_pool [as 别名]
def run(self, dataset1, dataset2, index1=None, index2=None, sample_size=10, weight=None,
include_chosen_choice=False, with_replacement=False, resources=None, dataset_pool=None):
"""this function samples number of sample_size (scalar value) alternatives from dataset2
for agent set specified by dataset1.
If index1 is not None, only samples alterantives for agents with indices in index1;
if index2 is not None, only samples alternatives from indices in index2.
sample_size specifies number of alternatives to be sampled for each agent.
weight, to be used as sampling weight, is either an attribute name of dataset2, or a 1d
array of the same length as index2 or 2d array of shape (index1.size, index2.size).
Also refer to document of interaction_dataset"""
if dataset_pool is None:
try:
sc = SessionConfiguration()
dataset_pool=sc.get_dataset_pool()
except:
dataset_pool = DatasetPool()
local_resources = Resources(resources)
local_resources.merge_if_not_None(
{"dataset1": dataset1, "dataset2": dataset2,
"index1":index1, "index2": index2,
"sample_size": sample_size, "weight": weight,
"with_replacement": with_replacement,
"include_chosen_choice": include_chosen_choice})
local_resources.check_obligatory_keys(['dataset1', 'dataset2', 'sample_size'])
agent = local_resources["dataset1"]
index1 = local_resources.get("index1", None)
if index1 is None:
index1 = arange(agent.size())
choice = local_resources["dataset2"]
index2 = local_resources.get("index2", None)
if index2 is None:
index2 = arange(choice.size())
if index1.size == 0 or index2.size == 0:
err_msg = "either choice size or agent size is zero, return None"
logger.log_warning(err_msg)
return None
include_chosen_choice = local_resources.get("include_chosen_choice", False)
J = local_resources["sample_size"]
if include_chosen_choice:
J = J - 1
with_replacement = local_resources.get("with_replacement")
weight = local_resources.get("weight", None)
if isinstance(weight, str):
if weight in choice.get_known_attribute_names():
weight=choice.get_attribute(weight)
rank_of_weight = 1
elif VariableName(weight).get_dataset_name() == choice.get_dataset_name():
weight=choice.compute_variables(weight, dataset_pool=dataset_pool)
rank_of_weight = 1
else:
## weights can be an interaction variable
interaction_dataset = InteractionDataset(local_resources)
weight=interaction_dataset.compute_variables(weight, dataset_pool=dataset_pool)
rank_of_weight = 2
elif isinstance(weight, ndarray):
rank_of_weight = weight.ndim
elif not weight: ## weight is None or empty string
weight = ones(index2.size)
rank_of_weight = 1
else:
err_msg = "unkown weight type"
logger.log_error(err_msg)
raise TypeError, err_msg
if (weight.size <> index2.size) and (weight.shape[rank_of_weight-1] <> index2.size):
if weight.shape[rank_of_weight-1] == choice.size():
if rank_of_weight == 1:
weight = take(weight, index2)
if rank_of_weight == 2:
weight = take(weight, index2, axis=1)
else:
err_msg = "weight array size doesn't match to size of dataset2 or its index"
logger.log_error(err_msg)
raise ValueError, err_msg
prob = normalize(weight)
#chosen_choice = ones(index1.size) * UNPLACED_ID
chosen_choice_id = agent.get_attribute(choice.get_id_name()[0])[index1]
#index_of_placed_agent = where(greater(chosen_choice_id, UNPLACED_ID))[0]
chosen_choice_index = choice.try_get_id_index(chosen_choice_id, return_value_if_not_found=UNPLACED_ID)
chosen_choice_index_to_index2 = lookup(chosen_choice_index, index2, index_if_not_found=UNPLACED_ID)
if rank_of_weight == 1: # if weight_array is 1d, then each agent shares the same weight for choices
replace = with_replacement # sampling with no replacement
if nonzerocounts(weight) < J:
logger.log_warning("weight array dosen't have enough non-zero counts, use sample with replacement")
replace = True
sampled_index = prob2dsample( index2, sample_size=(index1.size, J),
prob_array=prob, exclude_index=chosen_choice_index_to_index2,
replace=replace, return_index=True )
#.........这里部分代码省略.........