Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
break
#
# COMBINE ALL MODEL CONFIG INTO ONE FILE AND WRITE IT TO `out_dir`
#
data_path_adj = c.get_key('parallel.data_path_adjustment', None)
o = core.get_model_config(c, self.config_file,
adjust_data_path=data_path_adj,
insert_defaults=False)
unified_config_file = os.path.join(out_dir, 'Runs', 'model.yaml')
o.to_yaml(os.path.join(unified_config_file))
c.model = 'model.yaml'
## FIXME README: no longer have data_path_adjustment option
## FIXME: README: now simply saving unified model.yaml without making changes to it
config_model = utils.AttrDict.from_yaml(c.model)
unified_config_file = os.path.join(out_dir, 'Runs', 'model.yaml')
config_model.to_yaml(os.path.join(unified_config_file))
c.model = 'model.yaml'
# Always make sure we are saving outputs from iterations!
c.set_key('output.save', True)
#
# SET UP SUBMISSION SCRIPT FOR ARRAY RUNS
#
if c.parallel.style == 'array':
# Write array submission script
submit_file = os.path.join(out_dir, self.f_submit.format('array'))
with open(submit_file, 'w') as f:
self._write_submit(f, n_iter=len(iterations))
os.chmod(submit_file, 0o755)
"""
model_run = utils.AttrDict()
if debug_comments is None:
debug_comments = utils.AttrDict()
# README CHANGED: if run_config overrides data_path, it is no longer
# interpreted as relative to the run_config file's path
# 1) Apply any initiall overrides to config_model
# 1.a) Via 'model_override', which is the path to a YAML file
if 'model_override' in config_run:
override_path = utils.relative_path(
config_run.config_run_path, config_run.model_override
)
override_dict = utils.AttrDict.from_yaml(override_path)
config_model.union(
override_dict, allow_override=True, allow_replacement=True
)
for k, v in override_dict.as_dict_flat():
debug_comments.set_key(
'config_model.{}'.format(k),
'Overridden via `model_override: {}`'.format(override_path))
# 1.b) Via 'override', which is an AttrDict
if ('override' in config_run and isinstance(config_run.override, utils.AttrDict)):
config_model.union(
config_run.override, allow_override=True, allow_replacement=True
)
for k, v in override_dict.as_dict_flat():
debug_comments.set_key(
'config_model.{}'.format(k),
"""
Generate processed ModelRun configuration from a YAML run configuration file.
Parameters
----------
run_config_path : str
Path to YAML file with run configuration.
override : AttrDict, optional
Provide any additional options or override options from
``config_run`` by passing an AttrDict of the form
``{'model_settings': 'foo.yaml'}``. Any option possible in
``run.yaml`` can be specified in the dict, inluding ``override.``
options.
"""
config_run = utils.AttrDict.from_yaml(run_config_path)
config_run.config_run_path = run_config_path
debug_comments = utils.AttrDict()
# If we have no run name we use the run config file name without extension
if 'name' not in config_run:
config_run.name = os.path.splitext(os.path.basename(run_config_path))[0]
debug_comments.set_key(
'config_run.name', 'From run configuration filename'
)
# If passed in, config_run is overridden with any additional overrides...
if run_config_override:
assert isinstance(run_config_override, utils.AttrDict)
config_run.union(
run_config_override, allow_override=True, allow_replacement=True
If ``insert_defaults`` is False, the default settings from
defaults.yaml will not be included, which is necessary when
generating model settings file for parallel runs.
"""
# Ensure 'model' key is a list
if not isinstance(cr.model, list):
cr.model = [cr.model]
# Interpret relative config paths as relative to run.yaml
cr.model = [utils.relative_path(config_run_path, i) for i in cr.model]
# Load defaults from module path
module_conf = os.path.join(os.path.dirname(__file__), 'config')
o = utils.AttrDict.from_yaml(os.path.join(module_conf, 'defaults.yaml'))
# If defaults should not be inserted, replace the loaded AttrDict
# with an empty one (a bit of a hack, but we also want the
# default_techs list so we need to load the AttrDict anyway)
if not insert_defaults:
o = utils.AttrDict()
o.techs = utils.AttrDict()
# Load all additional files, continuously checking consistency
for path in cr.model:
new_o = utils.AttrDict.from_yaml(path)
if 'techs' in list(new_o.keys()):
overlap = set(get_default_techs()) & set(new_o.techs.keys())
if overlap:
e = exceptions.ModelError
raise e('Trying to re-define a default technology in '
a run config dictionary or model config dictionary.
Parameters
----------
config : AttrDict
a run or model configuration AttrDict
is_model_config : bool, optional
if True, ``config`` is a model_config, else a run_config
"""
base_model_config_file = os.path.join(
os.path.dirname(__file__),
'..', 'config', 'model.yaml'
)
config_model = utils.AttrDict.from_yaml(base_model_config_file)
default_tech_groups = list(config_model.tech_groups.keys())
# README CHANGED: `model` is not a list any longer -
# it is now always a single file
# README CHANGED: order of arguments to relative_path reversed
# README CHANGED: data_path option removed -- need to make sure
# that for parallel runs, data_path relative to the currently
# open model config file always works
if not is_model_config:
# Interpret relative config paths as relative to run.yaml
config.model = utils.relative_path(
config.config_run_path, config.model
self.run_id = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S_%f")
# Use current working directory as config_run path
self.config_run_path = os.getcwd()
self.config_run = cr
if override:
assert isinstance(override, utils.AttrDict)
cr.union(override, allow_override=True, allow_replacement=True)
# If manually specify a run_id in debug, overwrite the generated one
if 'debug.run_id' in cr.keys_nested():
self.run_id = cr.debug.run_id
self.config_model = get_model_config(cr, self.config_run_path)
# Override config_model settings if specified in config_run
# 1) Via 'model_override', which is the path to a YAML file
if 'model_override' in cr:
override_path = utils.relative_path(self.config_run_path, cr.model_override)
override_dict = utils.AttrDict.from_yaml(override_path)
self.override_model_config(override_dict)
# 2) Via 'override', which is an AttrDict
if ('override' in cr and isinstance(cr.override, utils.AttrDict)):
self.override_model_config(cr.override)
# Initialize locations
locs = self.config_model.locations
self.config_model.locations = locations.process_locations(locs)
# As a final step, flush the option cache
self.flush_option_cache()
def get_default_techs(foo=0): # pylint: disable=unused-argument
"""
Get list of techs pre-defined in defaults.yaml.
The foo=0 parameter makes sure that lru_cache has an argument to cache,
the function must always be called as get_default_techs() with no
arguments, ensuring that the values are only read from disk once and
then cached.
"""
module_config = os.path.join(os.path.dirname(__file__), 'config')
o = utils.AttrDict.from_yaml(os.path.join(module_config, 'defaults.yaml'))
return list(o.techs.keys())