def test_merge_diff_type_sub_configs():
    """Test that configs with nested diff type are not merged"""
    a = {"a": 1, "b": {"c": 2, "t": 1, "d": {"t": 2, "e": 3}}}
    b = {"b": {"a": 3, "t": 1, "d": {"t": 2, "f": 4}}}

    m = resolve_config.merge_configs(a, b, differentiators=["t"])
    assert m == {
        "a": 1,
        "b": {
            "a": 3,
            "c": 2,
            "t": 1,
            "d": {
                "t": 2,
                "e": 3,
                "f": 4
            }
        }
    }

    c = {"b": {"a": 3, "t": 1, "d": {"t": 3, "f": 4}}}

    m = resolve_config.merge_configs(a, c, differentiators=["t"])
    assert m == {"a": 1, "b": {"a": 3, "c": 2, "t": 1, "d": {"t": 3, "f": 4}}}

    d = {"b": {"a": 3, "t": 2, "d": {"t": 2, "f": 4}}}

    m = resolve_config.merge_configs(a, d, differentiators=["t"])
    assert m == {"a": 1, "b": {"a": 3, "t": 2, "d": {"t": 2, "f": 4}}}
def test_merge_sub_configs_sub_extend_two():
    """Ensure updating to third level from second level is fine"""
    a = {'a': 1, 'b': {'c': 2}}
    b = {'d': {'e': 3}}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 1, 'b': {'c': 2}, 'd': {'e': 3}}

    a = {'a': 1, 'b': {'c': 2, 'd': 3}}
    b = {'b': {'e': {'f': 4}}}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 1, 'b': {'c': 2, 'd': 3, 'e': {'f': 4}}}
def test_merge_sub_configs_sub_extend_two():
    """Ensure updating to third level from second level is fine"""
    a = {"a": 1, "b": {"c": 2}}
    b = {"d": {"e": 3}}

    m = resolve_config.merge_configs(a, b)

    assert m == {"a": 1, "b": {"c": 2}, "d": {"e": 3}}

    a = {"a": 1, "b": {"c": 2, "d": 3}}
    b = {"b": {"e": {"f": 4}}}

    m = resolve_config.merge_configs(a, b)

    assert m == {"a": 1, "b": {"c": 2, "d": 3, "e": {"f": 4}}}
def test_merge_sub_configs_sub_update_two():
    """Ensure updating on second level is fine"""
    a = {"a": 1, "b": {"c": 2}}
    b = {"b": {"c": 3}}

    m = resolve_config.merge_configs(a, b)

    assert m == {"a": 1, "b": {"c": 3}}

    a = {"a": 1, "b": {"c": 2, "d": 3}}
    b = {"b": {"c": 4}}

    m = resolve_config.merge_configs(a, b)

    assert m == {"a": 1, "b": {"c": 4, "d": 3}}
def test_merge_sub_configs_sub_update_two():
    """Ensure updating on second level is fine"""
    a = {'a': 1, 'b': {'c': 2}}
    b = {'b': {'c': 3}}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 1, 'b': {'c': 3}}

    a = {'a': 1, 'b': {'c': 2, 'd': 3}}
    b = {'b': {'c': 4}}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 1, 'b': {'c': 4, 'd': 3}}
Exemple #6
0
def consolidate_config(name, version, config):
    """Merge together given configuration with db configuration matching
    for experiment (``name``, ``version``)
    """
    db_config = fetch_config_from_db(name, version)

    # Do not merge spaces, the new definition overrides it.
    if "space" in config:
        db_config.pop("space", None)

    log.debug("Merging user and db configs:")
    log.debug("    config from user:\n%s", pprint.pformat(config))
    log.debug("    config from DB:\n%s", pprint.pformat(db_config))

    new_config = config
    config = resolve_config.merge_configs(db_config, config)

    config.setdefault("metadata", {})
    resolve_config.update_metadata(config["metadata"])

    merge_algorithm_config(config, new_config)
    # TODO: Remove for v0.4
    merge_producer_config(config, new_config)

    config.setdefault("name", name)
    config.setdefault("version", version)

    log.debug("    Merged config:\n%s", pprint.pformat(config))

    return config
def test_merge_diff_type_configs():
    """Test that configs with diff type are not merged"""
    a = {"a": 1, "b": {"c": 2, "t": 1}}
    b = {"a": 1, "b": {"c": 3, "d": 4, "t": 2}}
    c = {"b": {"c": {"d": 4}, "e": 5, "t": 3}}

    m = resolve_config.merge_configs(a, b, differentiators=["t"])

    assert m == {"a": 1, "b": {"c": 3, "d": 4, "t": 2}}

    m = resolve_config.merge_configs(b, c, differentiators=["t"])
    assert m == {"a": 1, "b": {"c": {"d": 4}, "e": 5, "t": 3}}

    assert resolve_config.merge_configs(b, c, differentiators=[
        "t"
    ]) == resolve_config.merge_configs(a, b, c, differentiators=["t"])
def test_merge_configs_update_two():
    """Ensure update on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'a': 3}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 3, 'b': 2}
def test_merge_configs_update_extend_two():
    """Ensure update and extension on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"b": 3, "c": 4}

    m = resolve_config.merge_configs(a, b)

    assert m == {"a": 1, "b": 3, "c": 4}
Exemple #10
0
def test_merge_configs_update_extend_two():
    """Ensure update and extension on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'b': 3, 'c': 4}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 1, 'b': 3, 'c': 4}
Exemple #11
0
def test_merge_sub_configs_update_three():
    """Ensure updating twice to third level from second level is fine"""
    a = {'a': 1, 'b': {'c': 2}}
    b = {'b': {'c': 3}}
    c = {'b': {'c': {'d': 4}}}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {'a': 1, 'b': {'c': {'d': 4}}}

    a = {'a': 1, 'b': {'c': 2, 'd': 3}}
    b = {'b': {'c': 4}}
    c = {'b': {'c': {'e': 5}}}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {'a': 1, 'b': {'c': {'e': 5}, 'd': 3}}
Exemple #12
0
def test_merge_configs_extend_two():
    """Ensure extension on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"c": 3}

    m = resolve_config.merge_configs(a, b)

    assert m == {"a": 1, "b": 2, "c": 3}
Exemple #13
0
def test_merge_sub_configs_update_three():
    """Ensure updating twice to third level from second level is fine"""
    a = {"a": 1, "b": {"c": 2}}
    b = {"b": {"c": 3}}
    c = {"b": {"c": {"d": 4}}}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {"a": 1, "b": {"c": {"d": 4}}}

    a = {"a": 1, "b": {"c": 2, "d": 3}}
    b = {"b": {"c": 4}}
    c = {"b": {"c": {"e": 5}}}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {"a": 1, "b": {"c": {"e": 5}, "d": 3}}
Exemple #14
0
def test_merge_configs_extend_two():
    """Ensure extension on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'c': 3}

    m = resolve_config.merge_configs(a, b)

    assert m == {'a': 1, 'b': 2, 'c': 3}
Exemple #15
0
def test_merge_configs_update_extend_three():
    """Ensure two updates and extensions on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"b": 3, "c": 4}
    c = {"a": 5, "d": 6}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {"a": 5, "b": 3, "c": 4, "d": 6}
Exemple #16
0
def test_merge_matching_type_configs():
    """Test that configs with matching type are merged properly"""
    a = {"a": 1, "b": {"c": 2, "t": "match"}}
    b = {"b": {"c": 3, "d": 4, "t": "match"}}
    c = {"b": {"c": {"d": 4}, "e": 5, "t": "match"}}

    m = resolve_config.merge_configs(a, b, c, differentiators=["t"])

    assert m == {"a": 1, "b": {"c": {"d": 4}, "d": 4, "e": 5, "t": "match"}}
Exemple #17
0
def test_merge_configs_extend_three():
    """Ensure two extensions on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"c": 3}
    c = {"d": 4}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {"a": 1, "b": 2, "c": 3, "d": 4}
Exemple #18
0
def test_merge_configs_update_three():
    """Ensure two updates on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'a': 3}
    c = {'b': 4}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {'a': 3, 'b': 4}
Exemple #19
0
def test_merge_configs_update_three():
    """Ensure two updates on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"a": 3}
    c = {"b": 4}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {"a": 3, "b": 4}
Exemple #20
0
def test_merge_configs_update_extend_three():
    """Ensure two updates and extensions on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'b': 3, 'c': 4}
    c = {'a': 5, 'd': 6}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {'a': 5, 'b': 3, 'c': 4, 'd': 6}
Exemple #21
0
def test_merge_configs_extend_three():
    """Ensure two extensions on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'c': 3}
    c = {'d': 4}

    m = resolve_config.merge_configs(a, b, c)

    assert m == {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Exemple #22
0
def test_merge_configs_update_extend_four():
    """Ensure three updates and extensions on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"b": 3, "c": 4}
    c = {"a": 5, "d": 6}
    d = {"d": 7, "e": 8}

    m = resolve_config.merge_configs(a, b, c, d)

    assert m == {"a": 5, "b": 3, "c": 4, "d": 7, "e": 8}
Exemple #23
0
def test_merge_configs_update_four():
    """Ensure three updates on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"a": 3}
    c = {"b": 4}
    d = {"a": 5, "b": 6}

    m = resolve_config.merge_configs(a, b, c, d)

    assert m == {"a": 5, "b": 6}
Exemple #24
0
def test_merge_configs_update_extend_four():
    """Ensure three updates and extensions on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'b': 3, 'c': 4}
    c = {'a': 5, 'd': 6}
    d = {'d': 7, 'e': 8}

    m = resolve_config.merge_configs(a, b, c, d)

    assert m == {'a': 5, 'b': 3, 'c': 4, 'd': 7, 'e': 8}
Exemple #25
0
def test_merge_configs_extend_four():
    """Ensure three extensions on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'c': 3}
    c = {'d': 4}
    d = {'e': 5}

    m = resolve_config.merge_configs(a, b, c, d)

    assert m == {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Exemple #26
0
def test_merge_configs_extend_four():
    """Ensure three extensions on first level is fine"""
    a = {"a": 1, "b": 2}
    b = {"c": 3}
    c = {"d": 4}
    d = {"e": 5}

    m = resolve_config.merge_configs(a, b, c, d)

    assert m == {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
Exemple #27
0
def test_merge_configs_update_four():
    """Ensure three updates on first level is fine"""
    a = {'a': 1, 'b': 2}
    b = {'a': 3}
    c = {'b': 4}
    d = {'a': 5, 'b': 6}

    m = resolve_config.merge_configs(a, b, c, d)

    assert m == {'a': 5, 'b': 6}
Exemple #28
0
    def fetch_full_config(self, cmdargs, use_db=True):
        """Get dictionary of the full configuration of the experiment.

        .. seealso::

            `orion.core.io.experiment_builder` for more information on the hierarchy of
            configurations.

        Parameters
        ----------
        cmdargs:

        use_db: bool
            Use experiment configuration found in database if True. Defaults to True.

        Note
        ----
            This method builds an experiment view in the background to fetch the configuration from
            the database.

        """
        default_options = self.fetch_default_options()
        env_vars = self.fetch_env_vars()
        if use_db:
            config_from_db = self.fetch_config_from_db(cmdargs)
        else:
            config_from_db = {}
        cmdconfig = self.fetch_file_config(cmdargs)
        metadata = dict(metadata=self.fetch_metadata(cmdargs))

        exp_config = resolve_config.merge_configs(
            default_options, env_vars, copy.deepcopy(config_from_db),
            cmdconfig, cmdargs, metadata)

        if 'user' in exp_config:
            exp_config['metadata']['user'] = exp_config['user']

        # TODO: Find a better solution
        if isinstance(exp_config['algorithms'],
                      dict) and len(exp_config['algorithms']) > 1:
            for key in list(config_from_db['algorithms'].keys()):
                exp_config['algorithms'].pop(key)

        return exp_config
Exemple #29
0
def get_cmd_config(cmdargs):
    """Fetch configuration defined by commandline and local configuration file.

    Arguments of commandline have priority over options in configuration file.
    """
    cmdargs = resolve_config.fetch_config_from_cmdargs(cmdargs)
    cmd_config = resolve_config.fetch_config(cmdargs)
    cmd_config = resolve_config.merge_configs(cmd_config, cmdargs)

    cmd_config.update(cmd_config.pop("experiment", {}))
    cmd_config["user_script_config"] = cmd_config.get("worker", {}).get(
        "user_script_config", None
    )

    cmd_config["branching"] = cmd_config.pop("evc", {})

    # TODO: We should move branching specific stuff below in a centralized place for EVC stuff.
    if (
        cmd_config["branching"].get("auto_resolution", False)
        and cmdargs.get("manual_resolution", None) is None
    ):
        cmd_config["branching"]["manual_resolution"] = False

    non_monitored_arguments = cmdargs.get("non_monitored_arguments")
    if non_monitored_arguments:
        cmd_config["branching"][
            "non_monitored_arguments"
        ] = non_monitored_arguments.split(":")

    # TODO: user_args won't be defined if reading from DB only (`orion hunt -n <exp> ` alone)
    metadata = resolve_config.fetch_metadata(
        cmd_config.get("user"),
        cmd_config.get("user_args"),
        cmd_config.get("user_script_config"),
    )
    cmd_config["metadata"] = metadata
    cmd_config.pop("config", None)

    cmd_config["space"] = cmd_config["metadata"].get("priors", None)

    backward.update_db_config(cmd_config)

    return cmd_config
Exemple #30
0
def setup_storage(storage=None, debug=False):
    """Create the storage instance from a configuration.

    Parameters
    ----------
    config: dict, optional
        Configuration for the storage backend. If not defined, global configuration
        is used.
    debug: bool, optional
        If using in debug mode, the storage config is overrided with legacy:EphemeralDB.
        Defaults to False.

    """
    if storage is None:
        storage = orion.core.config.storage.to_dict()

    storage = copy.deepcopy(storage)

    if storage.get("type") == "legacy" and "database" not in storage:
        storage["database"] = orion.core.config.storage.database.to_dict()
    elif storage.get("type") is None and "database" in storage:
        storage["type"] = "legacy"

    # If using same storage type
    if storage["type"] == orion.core.config.storage.type:
        storage = resolve_config.merge_configs(
            orion.core.config.storage.to_dict(), storage)

    if debug:
        storage = {"type": "legacy", "database": {"type": "EphemeralDB"}}

    storage_type = storage.pop("type")

    log.debug("Creating %s storage client with args: %s", storage_type,
              storage)
    try:
        storage_factory.create(of_type=storage_type, **storage)
    except ValueError:
        if storage_factory.create().__class__.__name__.lower(
        ) != storage_type.lower():
            raise