Exemplo n.º 1
0
    def _update_param(self, param, new_values):
        """
        Update the current parameter values with those specified by
        the adjustment. The values that need to be updated are chosen
        by finding all value items with label values matching the
        label values specified in the adjustment. If the value is
        set to None, then that value object will be removed.

        Note: _update_param used to raise a ParameterUpdateException if one of the new
            values did not match at least one of the current value objects. However,
            this was dropped to better support the case where the parameters are being
            extended along some label to fill the parameter space. An exception could
            be raised if a new value object contains a label that is not used in the
            current value objects for the parameter. However, it seems like it could be
            expensive to check this case, especially when a project is extending parameters.
            For now, no exceptions are raised by this method.

        """
        curr_values = self._data[param]["value"]
        if param in self._search_trees:
            curr_tree = self._search_trees[param]
        else:
            curr_tree = Tree(vos=curr_values, label_grid=self.label_grid)
        new_tree = Tree(vos=new_values, label_grid=self.label_grid)
        self._data[param]["value"] = curr_tree.update(new_tree)
        self._search_trees[param] = curr_tree
Exemplo n.º 2
0
def assert_rebalanced(tree):
    exp_tree = Tree(copy.deepcopy(tree.vos), tree.label_grid)

    tree.init()
    exp_tree.init()

    np.testing.assert_equal(tree.tree, exp_tree.tree)
Exemplo n.º 3
0
def test_update_simple(vos, label_grid):
    tree = Tree(vos, label_grid)
    new_vos = [{"d0": 1, "d1": "hello", "value": 2}]
    new_tree = Tree(new_vos, label_grid)
    tree.update(new_tree)
    assert tree.vos == [
        {
            "d0": 1,
            "d1": "hello",
            "value": 2
        },
        {
            "d0": 1,
            "d1": "world",
            "value": 1
        },
        {
            "d0": 2,
            "d1": "hello",
            "value": 1
        },
        {
            "d0": 3,
            "d1": "world",
            "value": 1
        },
    ]
    assert_rebalanced(tree)
Exemplo n.º 4
0
def test_error_on_extra_label(vos, label_grid):
    tree = Tree(vos, label_grid)
    new_vos = [{"d2": 2, "d0": 1, "d1": "hello", "value": 2}]
    new_tree = Tree(new_vos, label_grid)
    with pytest.raises(ParamToolsError):
        tree.update(new_tree)
    assert_rebalanced(tree)
Exemplo n.º 5
0
def test_delete_all(vos, label_grid):
    tree = Tree(vos, label_grid)
    new_vos = [{"value": None}]
    new_tree = Tree(new_vos, label_grid)
    tree.update(new_tree)
    assert tree.vos == []
    assert_rebalanced(tree)
Exemplo n.º 6
0
def test_build(vos, label_grid):
    tree = Tree(vos, label_grid)
    tree.init()
    exp_tree = {
        "d0": {
            1: {0, 1},
            2: {2},
            3: {3}
        },
        "d1": {
            "hello": {0, 2},
            "world": {1, 3}
        },
    }
    np.testing.assert_equal(tree.tree, exp_tree)
Exemplo n.º 7
0
def test_delete_some(vos, label_grid):
    tree = Tree(vos, label_grid)
    new_vos = [{"d1": "hello", "value": None}]
    new_tree = Tree(new_vos, label_grid)
    tree.update(new_tree)
    assert tree.vos == [
        {
            "d0": 1,
            "d1": "world",
            "value": 1
        },
        {
            "d0": 3,
            "d1": "world",
            "value": 1
        },
    ]
Exemplo n.º 8
0
def test_select(vos, label_grid):
    tree = Tree(vos, label_grid)

    res = tree.select({}, eq_func)
    assert res == vos

    res = tree.select({"d1": "world"}, eq_func)
    assert res == [
        {
            "d0": 1,
            "d1": "world",
            "value": 1
        },
        {
            "d0": 3,
            "d1": "world",
            "value": 1
        },
    ]

    res = tree.select({"d0": 1, "d1": "world"}, eq_func)
    assert res == [{"d0": 1, "d1": "world", "value": 1}]

    with pytest.raises(KeyError):
        tree.select({"d2": 1}, eq_func, exact_match=True)
Exemplo n.º 9
0
def select(
    value_objects: List[ValueObject],
    exact_match: bool,
    cmp_func: CmpFunc,
    labels: dict,
    tree: Tree = None,
) -> List[ValueObject]:
    """
    Query a parameter along some labels. If exact_match is True,
    all values in `labels` must be equal to the corresponding label
    in the parameter's "value" dictionary.

    Ignores state.

    Returns: [{"value": val, "label0": ..., }]
    """
    if not labels:
        return value_objects
    if tree is None:
        tree = Tree(vos=value_objects, label_grid=None)
    return tree.select(labels, cmp_func, exact_match)
Exemplo n.º 10
0
def test_select(vos, label_grid):
    tree = Tree(vos, label_grid)

    res = tree.select({}, eq_func)
    assert res == vos

    res = tree.select({"d1": "world"}, eq_func)
    assert res == [
        {
            "d0": 1,
            "d1": "world",
            "value": 1
        },
        {
            "d0": 3,
            "d1": "world",
            "value": 1
        },
    ]

    res = tree.select({"d0": 1, "d1": "world"}, eq_func)
    assert res == [{"d0": 1, "d1": "world", "value": 1}]

    assert tree.select({"d2": 1}, eq_func, strict=True) == []
Exemplo n.º 11
0
def test_init(vos, label_grid):
    assert Tree(vos, label_grid)