本文整理汇总了Python中entities.Entity.from_yaml方法的典型用法代码示例。如果您正苦于以下问题:Python Entity.from_yaml方法的具体用法?Python Entity.from_yaml怎么用?Python Entity.from_yaml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类entities.Entity
的用法示例。
在下文中一共展示了Entity.from_yaml方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_str
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
#.........这里部分代码省略.........
input_dir = os.path.join(simulation_dir, input_dir)
config.input_directory = input_dir
if input_file is None:
input_file = input_def.get('file', '')
input_path = os.path.join(input_dir, input_file)
output_def = simulation_def['output']
if output_dir is None:
output_dir = output_def.get('path', '')
if not os.path.isabs(output_dir):
output_dir = os.path.join(simulation_dir, output_dir)
if not os.path.exists(output_dir):
print("creating directory: '%s'" % output_dir)
os.makedirs(output_dir)
config.output_directory = output_dir
minimal_output = False
if output_file is None:
output_file = output_def.get('file', '')
if output_file:
output_path = os.path.join(output_dir, output_file)
else:
# using a temporary directory instead of a temporary file
# because tempfile.* only returns file-like objects (which
# pytables does not support) or directories, not file names.
tmp_dir = tempfile.mkdtemp(prefix='liam2-', suffix='-tmp',
dir=output_dir)
output_path = os.path.join(tmp_dir, 'simulation.h5')
minimal_output = True
entities = {}
for k, v in content['entities'].iteritems():
entities[k] = Entity.from_yaml(k, v)
for entity in entities.itervalues():
entity.attach_and_resolve_links(entities)
global_context = {'__globals__': global_symbols(globals_def),
'__entities__': entities}
parsing_context = global_context.copy()
parsing_context.update((entity.name, entity.all_symbols(global_context))
for entity in entities.itervalues())
# compute the lag variable for each entity (an entity can cause fields from
# other entities to be added via links)
# dict of sets
lag_vars_by_entity = defaultdict(set)
for entity in entities.itervalues():
parsing_context['__entity__'] = entity.name
entity.parse_processes(parsing_context)
entity_lag_vars = entity.compute_lagged_fields()
for e in entity_lag_vars:
lag_vars_by_entity[e.name] |= entity_lag_vars[e]
# store that in entity.lag_fields and create entity.array_lag
for entity in entities.itervalues():
entity_lag_vars = lag_vars_by_entity[entity.name]
if entity_lag_vars:
# make sure we have an 'id' column, and that it comes first
# (makes debugging easier). 'id' is always necessary for lag
# expressions to be able to "expand" the vector of values to the
# "current" individuals.
entity_lag_vars.discard('id')
sorted_vars = ['id'] + sorted(entity_lag_vars)
field_type = dict(entity.fields.name_types)
lag_fields = [(v, field_type[v]) for v in sorted_vars]
示例2: from_yaml
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
def from_yaml(cls, fpath,
input_dir=None, input_file=None,
output_dir=None, output_file=None):
simulation_path = os.path.abspath(fpath)
simulation_dir = os.path.dirname(simulation_path)
with open(fpath) as f:
content = yaml.load(f)
expand_periodic_fields(content)
content = handle_imports(content, simulation_dir)
validate_dict(content, cls.yaml_layout)
# the goal is to get something like:
# globals_def = {'periodic': [('a': int), ...],
# 'MIG': int}
globals_def = content.get('globals', {})
for k, v in content.get('globals', {}).iteritems():
if "type" in v:
v["type"] = field_str_to_type(v["type"], "array '%s'" % k)
else:
#TODO: fields should be optional (would use all the fields
# provided in the file)
v["fields"] = fields_yaml_to_type(v["fields"])
globals_def[k] = v
simulation_def = content['simulation']
seed = simulation_def.get('random_seed')
if seed is not None:
seed = int(seed)
print("using fixed random seed: %d" % seed)
random.seed(seed)
np.random.seed(seed)
periods = simulation_def['periods']
time_scale = simulation_def.get('time_scale', 'year')
retro = simulation_def.get('retro', False)
start_period = simulation_def.get('start_period', None)
init_period = simulation_def.get('init_period', None)
if start_period is None and init_period is None:
raise Exception("Either start_period either init_period should be given.")
if start_period is not None:
if init_period is not None:
raise Exception("Start_period can't be given if init_period is.")
step = time_period[time_scale] * (1 - 2 * (retro))
init_period = addmonth(start_period, step)
config.skip_shows = simulation_def.get('skip_shows', config.skip_shows)
# TODO: check that the value is one of "raise", "skip", "warn"
config.assertions = simulation_def.get('assertions', config.assertions)
logging_def = simulation_def.get('logging', {})
config.log_level = logging_def.get('level', config.log_level)
if 'timings' in simulation_def:
warnings.warn("simulation.timings is deprecated, please use "
"simulation.logging.timings instead",
DeprecationWarning)
config.show_timings = simulation_def['timings']
config.show_timings = logging_def.get('timings', config.show_timings)
autodump = simulation_def.get('autodump', None)
if autodump is True:
autodump = 'autodump.h5'
if isinstance(autodump, basestring):
# by default autodump will dump all rows
autodump = (autodump, None)
config.autodump = autodump
autodiff = simulation_def.get('autodiff', None)
if autodiff is True:
autodiff = 'autodump.h5'
if isinstance(autodiff, basestring):
# by default autodiff will compare all rows
autodiff = (autodiff, None)
config.autodiff = autodiff
legislation = simulation_def.get('legislation', None)
final_stat = simulation_def.get('final_stat', None)
input_def = simulation_def['input']
input_directory = input_dir if input_dir is not None else input_def.get('path', '')
if not os.path.isabs(input_directory):
input_directory = os.path.join(simulation_dir, input_directory)
config.input_directory = input_directory
output_def = simulation_def['output']
output_directory = output_dir if output_dir is not None else output_def.get('path', '')
assert os.path.isabs(output_directory), "{} is not an absolute path".format(output_directory)
if not os.path.isabs(output_directory):
output_directory = os.path.join(simulation_dir, output_directory)
if not os.path.exists(output_directory):
print("creating directory: '%s'" % output_directory)
os.makedirs(output_directory)
config.output_directory = output_directory
if output_file is None:
output_file = output_def['file']
output_path = os.path.join(output_directory, output_file)
method = input_def.get('method', 'h5')
#.........这里部分代码省略.........
示例3: from_yaml
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
def from_yaml(cls, fpath,
input_dir=None, input_file=None,
output_dir=None, output_file=None):
simulation_path = os.path.abspath(fpath)
simulation_dir = os.path.dirname(simulation_path)
with open(fpath) as f:
content = yaml.load(f)
content = handle_imports(content, simulation_dir)
validate_dict(content, cls.yaml_layout)
# the goal is to get something like:
# globals_def = {'periodic': [('a': int), ...],
# 'MIG': int}
globals_def = {}
for k, v in content.get('globals', {}).iteritems():
# periodic is a special case
if k == 'periodic':
type_ = fields_yaml_to_type(v)
else:
# "fields" and "type" are synonyms
type_def = v.get('fields') or v.get('type')
if isinstance(type_def, basestring):
type_ = field_str_to_type(type_def, "array '%s'" % k)
else:
if not isinstance(type_def, list):
raise SyntaxError("invalid structure for globals")
type_ = fields_yaml_to_type(type_def)
globals_def[k] = type_
simulation_def = content['simulation']
seed = simulation_def.get('random_seed')
if seed is not None:
seed = int(seed)
print("using fixed random seed: %d" % seed)
random.seed(seed)
np.random.seed(seed)
periods = simulation_def['periods']
start_period = simulation_def['start_period']
config.skip_shows = simulation_def.get('skip_shows', False)
#TODO: check that the value is one of "raise", "skip", "warn"
config.assertions = simulation_def.get('assertions', 'raise')
config.show_timings = simulation_def.get('timings', True)
autodump = simulation_def.get('autodump', None)
if autodump is True:
autodump = 'autodump.h5'
if isinstance(autodump, basestring):
# by default autodump will dump all rows
autodump = (autodump, None)
config.autodump = autodump
autodiff = simulation_def.get('autodiff', None)
if autodiff is True:
autodiff = 'autodump.h5'
if isinstance(autodiff, basestring):
# by default autodiff will compare all rows
autodiff = (autodiff, None)
config.autodiff = autodiff
input_def = simulation_def['input']
input_directory = input_dir if input_dir is not None \
else input_def.get('path', '')
if not os.path.isabs(input_directory):
input_directory = os.path.join(simulation_dir, input_directory)
config.input_directory = input_directory
output_def = simulation_def['output']
output_directory = output_dir if output_dir is not None \
else output_def.get('path', '')
if not os.path.isabs(output_directory):
output_directory = os.path.join(simulation_dir, output_directory)
if not os.path.exists(output_directory):
print("creating directory: '%s'" % output_directory)
os.makedirs(output_directory)
config.output_directory = output_directory
if output_file is None:
output_file = output_def['file']
output_path = os.path.join(output_directory, output_file)
for k, v in content['entities'].iteritems():
entity_registry.add(Entity.from_yaml(k, v))
for entity in entity_registry.itervalues():
entity.check_links()
entity.parse_processes(globals_def)
entity.compute_lagged_fields()
init_def = [d.items()[0] for d in simulation_def.get('init', {})]
init_processes, init_entities = [], set()
for ent_name, proc_names in init_def:
if ent_name not in entity_registry:
raise Exception("Entity '%s' not found" % ent_name)
entity = entity_registry[ent_name]
init_entities.add(entity)
init_processes.extend([(entity.processes[proc_name], 1)
for proc_name in proc_names])
#.........这里部分代码省略.........
示例4: from_yaml
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
def from_yaml(cls, fpath,
input_dir=None, input_file=None,
output_dir=None, output_file=None):
simulation_path = os.path.abspath(fpath)
simulation_dir = os.path.dirname(simulation_path)
with open(fpath) as f:
content = yaml.load(f)
expand_periodic_fields(content)
content = handle_imports(content, simulation_dir)
validate_dict(content, cls.yaml_layout)
# the goal is to get something like:
# globals_def = {'periodic': [('a': int), ...],
# 'MIG': int}
globals_def = content.get('globals', {})
for k, v in content.get('globals', {}).iteritems():
if "type" in v:
v["type"] = field_str_to_type(v["type"], "array '%s'" % k)
else:
#TODO: fields should be optional (would use all the fields
# provided in the file)
v["fields"] = fields_yaml_to_type(v["fields"])
globals_def[k] = v
simulation_def = content['simulation']
seed = simulation_def.get('random_seed')
if seed is not None:
seed = int(seed)
print("using fixed random seed: %d" % seed)
random.seed(seed)
np.random.seed(seed)
periods = simulation_def['periods']
start_period = simulation_def['start_period']
config.skip_shows = simulation_def.get('skip_shows', config.skip_shows)
#TODO: check that the value is one of "raise", "skip", "warn"
config.assertions = simulation_def.get('assertions', config.assertions)
logging_def = simulation_def.get('logging', {})
config.log_level = logging_def.get('level', config.log_level)
if 'timings' in simulation_def:
warnings.warn("simulation.timings is deprecated, please use "
"simulation.logging.timings instead",
DeprecationWarning)
config.show_timings = simulation_def['timings']
config.show_timings = logging_def.get('timings', config.show_timings)
autodump = simulation_def.get('autodump', None)
if autodump is True:
autodump = 'autodump.h5'
if isinstance(autodump, basestring):
# by default autodump will dump all rows
autodump = (autodump, None)
config.autodump = autodump
autodiff = simulation_def.get('autodiff', None)
if autodiff is True:
autodiff = 'autodump.h5'
if isinstance(autodiff, basestring):
# by default autodiff will compare all rows
autodiff = (autodiff, None)
config.autodiff = autodiff
input_def = simulation_def['input']
input_directory = input_dir if input_dir is not None \
else input_def.get('path', '')
if not os.path.isabs(input_directory):
input_directory = os.path.join(simulation_dir, input_directory)
config.input_directory = input_directory
output_def = simulation_def['output']
output_directory = output_dir if output_dir is not None \
else output_def.get('path', '')
if not os.path.isabs(output_directory):
output_directory = os.path.join(simulation_dir, output_directory)
if not os.path.exists(output_directory):
print("creating directory: '%s'" % output_directory)
os.makedirs(output_directory)
config.output_directory = output_directory
if output_file is None:
output_file = output_def['file']
output_path = os.path.join(output_directory, output_file)
entities = {}
for k, v in content['entities'].iteritems():
entities[k] = Entity.from_yaml(k, v)
for entity in entities.itervalues():
entity.attach_and_resolve_links(entities)
global_context = {'__globals__': global_symbols(globals_def),
'__entities__': entities}
parsing_context = global_context.copy()
parsing_context.update((entity.name, entity.all_symbols(global_context))
for entity in entities.itervalues())
for entity in entities.itervalues():
parsing_context['__entity__'] = entity.name
entity.parse_processes(parsing_context)
#.........这里部分代码省略.........
示例5: from_yaml
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
def from_yaml(cls, main_fpath,
input_dir=None, input_file=None,
output_dir=None, output_file=None):
simulation_path = os.path.abspath(main_fpath)
simulation_dir = os.path.dirname(simulation_path)
#TODO : add an instruction in yaml_layout in so as to call an other YAML file and use it instead of a list.
with open(main_fpath) as f:
content = yaml.load(f)
list_fpath=[]
if "import" in content.keys():
list_fpath = content['import'].values()
for fname in list_fpath:
fpath = os.path.join(simulation_dir, fname)
with open(fpath) as f:
content1 = yaml.load(f)
if "globals" in content1:
if "globals" in content:
raise Exception("globals can be defined only once")
else : content['globals']=content1['globals']
if "entities" in content1:
if "entities" in content:
for name1 in content1['entities'].keys():
if name1 in content['entities'].keys():
for name2 in content1['entities'][name1].keys():
if name2 in content['entities'][name1].keys():
if name2 in ('processes','macros','links'):
for name3 in content1['entities'][name1][name2].keys():
if name3 in content['entities'][name1][name2].keys():
raise Exception("%s of %s is defined a second time in %s" %(name3,name1,fpath) )
else : content['entities'][name1][name2][name3]=content1['entities'][name1][name2][name3]
if name2 == 'fields':
raise Exception("fields of entities can be defined only once, it is not the case for %s in %s" %(name1,fpath) )
else : content['entities'][name1][name2]=content1['entities'][name1][name2]
else : content['entities'][name1]=content1['entities'][name1]
else : content['entities']=content1['entities']
if "simulation" in content1:
if "simulation" in content:
raise Exception("simualtion can be defined only once")
else : content['simulation']=content1['simulation']
#XXX: use validictory instead of my custom validator?
# http://readthedocs.org/docs/validictory/
validate_dict(content, cls.yaml_layout)
globals_def = content.get('globals', {})
periodic_globals = globals_def.get('periodic', [])
# list of one-item-dicts to list of tuples
periodic_globals = [d.items()[0] for d in periodic_globals]
globals_fields = [(name, str_to_type[typestr])
for name, typestr in periodic_globals]
simulation_def = content['simulation']
seed = simulation_def.get('random_seed')
if seed is not None:
seed = int(seed)
print "using fixed random seed: %d" % seed
random.seed(seed)
np.random.seed(seed)
periods = simulation_def['periods']
start_period = simulation_def['start_period']
config.skip_shows = simulation_def.get('skip_shows', False)
#TODO: check that the value is one of "raise", "skip", "warn"
config.assertions = simulation_def.get('assertions', 'raise')
input_def = simulation_def['input']
input_directory = input_dir if input_dir is not None \
else input_def.get('path', '')
if not os.path.isabs(input_directory):
input_directory = os.path.join(simulation_dir, input_directory)
config.input_directory = input_directory
output_def = simulation_def['output']
output_directory = output_dir if output_dir is not None \
else output_def.get('path', '')
if not os.path.isabs(output_directory):
output_directory = os.path.join(simulation_dir, output_directory)
config.output_directory = output_directory
if output_file is None:
output_file = output_def['file']
output_path = os.path.join(output_directory, output_file)
for k, v in content['entities'].iteritems():
entity_registry.add(Entity.from_yaml(k, v))
for entity in entity_registry.itervalues():
entity.check_links()
entity.parse_processes(globals_fields)
entity.compute_lagged_fields()
init_def = [d.items()[0] for d in simulation_def.get('init', {})]
init_processes, init_entities = [], set()
for ent_name, proc_names in init_def:
if ent_name not in entity_registry:
raise Exception("Entity '%s' not found" % ent_name)
#.........这里部分代码省略.........
示例6: from_yaml
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
def from_yaml(cls, main_fpath, input_dir=None, input_file=None, output_dir=None, output_file=None):
simulation_path = os.path.abspath(main_fpath)
simulation_dir = os.path.dirname(simulation_path)
# TODO : add an instruction in yaml_layout in so as to call an other YAML file and use it instead of a list.
with open(main_fpath) as f:
content = yaml.load(f)
list_fpath = []
if "import" in content.keys():
list_fpath = content["import"].values()
for fname in list_fpath:
fpath = os.path.join(simulation_dir, fname)
with open(fpath) as f:
content1 = yaml.load(f)
if "globals" in content1:
if "globals" in content:
raise Exception("globals can be defined only once")
else:
content["globals"] = content1["globals"]
if "entities" in content1:
if "entities" in content:
for name1 in content1["entities"].keys():
if name1 in content["entities"].keys():
for name2 in content1["entities"][name1].keys():
if name2 in content["entities"][name1].keys():
if name2 in ("processes", "macros", "links"):
for name3 in content1["entities"][name1][name2].keys():
if name3 in content["entities"][name1][name2].keys():
raise Exception(
"%s of %s is defined a second time in %s"
% (name3, name1, fpath)
)
else:
content["entities"][name1][name2][name3] = content1["entities"][
name1
][name2][name3]
if name2 == "fields":
raise Exception(
"fields of entities can be defined only once, it is not the case for %s in %s"
% (name1, fpath)
)
else:
content["entities"][name1][name2] = content1["entities"][name1][name2]
else:
content["entities"][name1] = content1["entities"][name1]
else:
content["entities"] = content1["entities"]
if "simulation" in content1:
if "simulation" in content:
raise Exception("simualtion can be defined only once")
else:
content["simulation"] = content1["simulation"]
validate_dict(content, cls.yaml_layout)
# the goal is to get something like:
# globals_def = {'periodic': [('a': int), ...],
# 'MIG': int}
globals_def = {}
for k, v in content.get("globals", {}).iteritems():
# periodic is a special case
if k == "periodic":
type_ = fields_yaml_to_type(v)
else:
# "fields" and "type" are synonyms
type_def = v.get("fields") or v.get("type")
if isinstance(type_def, basestring):
type_ = field_str_to_type(type_def, "array '%s'" % k)
else:
if not isinstance(type_def, list):
raise SyntaxError("invalid structure for globals")
type_ = fields_yaml_to_type(type_def)
globals_def[k] = type_
simulation_def = content["simulation"]
seed = simulation_def.get("random_seed")
if seed is not None:
seed = int(seed)
print "using fixed random seed: %d" % seed
random.seed(seed)
np.random.seed(seed)
periods = simulation_def["periods"]
start_period = simulation_def["start_period"]
config.skip_shows = simulation_def.get("skip_shows", False)
# TODO: check that the value is one of "raise", "skip", "warn"
config.assertions = simulation_def.get("assertions", "raise")
input_def = simulation_def["input"]
input_directory = input_dir if input_dir is not None else input_def.get("path", "")
if not os.path.isabs(input_directory):
input_directory = os.path.join(simulation_dir, input_directory)
config.input_directory = input_directory
output_def = simulation_def["output"]
output_directory = output_dir if output_dir is not None else output_def.get("path", "")
if not os.path.isabs(output_directory):
output_directory = os.path.join(simulation_dir, output_directory)
config.output_directory = output_directory
#.........这里部分代码省略.........
示例7: from_yaml
# 需要导入模块: from entities import Entity [as 别名]
# 或者: from entities.Entity import from_yaml [as 别名]
def from_yaml(cls, fpath,
input_dir=None, input_file=None,
output_dir=None, output_file=None):
simulation_path = os.path.abspath(fpath)
simulation_dir = os.path.dirname(simulation_path)
with open(fpath) as f:
content = yaml.load(f)
content = handle_imports(content, simulation_dir)
validate_dict(content, cls.yaml_layout)
# the goal is to get something like:
# globals_def = {'periodic': [('a': int), ...],
# 'MIG': int}
globals_def = {}
for k, v in content.get('globals', {}).iteritems():
# periodic is a special case
if k == 'periodic':
type_ = fields_yaml_to_type(v)
else:
# "fields" and "type" are synonyms
type_def = v.get('fields') or v.get('type')
if isinstance(type_def, basestring):
type_ = field_str_to_type(type_def, "array '%s'" % k)
else:
if not isinstance(type_def, list):
raise SyntaxError("invalid structure for globals")
type_ = fields_yaml_to_type(type_def)
globals_def[k] = type_
simulation_def = content['simulation']
seed = simulation_def.get('random_seed')
if seed is not None:
seed = int(seed)
print("using fixed random seed: %d" % seed)
random.seed(seed)
np.random.seed(seed)
periods = simulation_def['periods']
time_scale = simulation_def.get('time_scale', 'year')
retro = simulation_def.get('retro', False)
start_period = simulation_def.get('start_period',None)
init_period = simulation_def.get('init_period',None)
if start_period is None and init_period is None:
raise Exception("Either start_period either init_period should be given.")
if start_period is not None:
if init_period is not None:
raise Exception("Start_period can't be given if init_period is.")
step = time_period[time_scale]*(1 - 2*(retro))
init_period = addmonth(start_period, step)
config.skip_shows = simulation_def.get('skip_shows', False)
#TODO: check that the value is one of "raise", "skip", "warn"
config.assertions = simulation_def.get('assertions', 'raise')
config.show_timings = simulation_def.get('timings', True)
autodump = simulation_def.get('autodump', None)
if autodump is True:
autodump = 'autodump.h5'
if isinstance(autodump, basestring):
# by default autodump will dump all rows
autodump = (autodump, None)
config.autodump = autodump
autodiff = simulation_def.get('autodiff', None)
if autodiff is True:
autodiff = 'autodump.h5'
if isinstance(autodiff, basestring):
# by default autodiff will compare all rows
autodiff = (autodiff, None)
config.autodiff = autodiff
legislation = simulation_def.get('legislation', None)
final_stat = simulation_def.get('final_stat', None)
input_def = simulation_def['input']
input_directory = input_dir if input_dir is not None \
else input_def.get('path', '')
if not os.path.isabs(input_directory):
input_directory = os.path.join(simulation_dir, input_directory)
config.input_directory = input_directory
output_def = simulation_def['output']
output_directory = output_dir if output_dir is not None \
else output_def.get('path', '')
if not os.path.isabs(output_directory):
output_directory = os.path.join(simulation_dir, output_directory)
if not os.path.exists(output_directory):
print("creating directory: '%s'" % output_directory)
os.makedirs(output_directory)
config.output_directory = output_directory
if output_file is None:
output_file = output_def['file']
output_path = os.path.join(output_directory, output_file)
method = input_def.get('method', 'h5')
#need to be before processes because in case of legislation, we need input_table for now.
#.........这里部分代码省略.........