How to use the calliope.utils.relative_path function in calliope

To help you get started, we’ve selected a few calliope examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github calliope-project / calliope / calliope / core / preprocess_model.py View on Github external
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
        )

        config_model_dict = utils.AttrDict.from_yaml(config.model)

        # Interpret timeseries_data_path as relative to `path`  (i.e the currently
        # open model config file)
        config_model_dict.model.timeseries_data_path = utils.relative_path(
            config.model, config_model_dict.model.timeseries_data_path
        )

    # Check whether the model config attempts to override any of the
    # base technology groups
    if 'tech_groups' in config_model_dict:
        overridden_groups = (
            set(default_tech_groups) &
github calliope-project / calliope / calliope / core.py View on Github external
# we have no filename so we just use current date/time
            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()
github calliope-project / calliope / calliope / core / preprocess_model.py View on Github external
----------
    config_run : AttrDict
    config_model : AttrDict

    """
    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
        )
github calliope-project / calliope / calliope / core / preprocess_model.py View on Github external
# 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
        )

        config_model_dict = utils.AttrDict.from_yaml(config.model)

        # Interpret timeseries_data_path as relative to `path`  (i.e the currently
        # open model config file)
        config_model_dict.model.timeseries_data_path = utils.relative_path(
            config.model, config_model_dict.model.timeseries_data_path
        )

    # Check whether the model config attempts to override any of the
    # base technology groups
    if 'tech_groups' in config_model_dict:
        overridden_groups = (
            set(default_tech_groups) &
            set(config_model_dict.tech_groups.keys())
        )
        if overridden_groups:
            raise exceptions.ModelError(
                'Trying to re-define base '
                'technology groups: {}'.format(overridden_groups)
            )
github calliope-project / calliope / calliope / core.py View on Github external
config_run_path the path to the run configuration file

    If ``adjust_data_path`` is given, the data_path setting is adjusted
    using the given path, else, it is forced to an absolute path.

    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()):