Example #1
0
def test_write_background_to_file_2(tmpdir):
    """Test writing Background object to a file with extra parameters."""
    _bk = Background(modes=example_data.train.modes,
                     node_size=1,
                     max_tree_depth=5)
    _bk.write(filename="train", location=pathlib.Path(tmpdir))
    assert tmpdir.join("train_bk.txt").read() == str(_bk)
Example #2
0
def test_initialize_bad_n_estimators(test_input):
    """Test bad values for n_estimators"""
    _dn = RDN(target="cancer",
              background=Background(),
              n_estimators=test_input)
    with pytest.raises(ValueError):
        _dn.fit(example_data.train)
Example #3
0
def test_learn_example_dataset_1(test_input):
    """Learn from the example database."""
    _bk = Background(modes=example_data.train.modes,
                     use_std_logic_variables=True)
    _dn = RDN(background=_bk, target="cancer", n_estimators=test_input)
    _dn.fit(example_data.train)
    assert len(_dn.estimators_) == test_input
Example #4
0
def test_predict_example_data(test_input):
    """Test learn and predict."""
    _bk = Background(modes=example_data.train.modes,
                     use_std_logic_variables=True)
    _dn = RDN(background=_bk, target="cancer", n_estimators=test_input)
    _dn.fit(example_data.train)
    assert_array_equal(_dn.predict(example_data.test),
                       np.array([1.0, 1.0, 1.0, 0.0, 0.0]))
Example #5
0
def test_initialize_background_knowledge_1():
    """
    Test initializing a Background object with default settings.
    """
    _bk = Background()
    assert _bk.modes is None
    assert not _bk.line_search
    assert not _bk.recursion
Example #6
0
def test_string_conversion_no_modes():
    """Test initializing when no modes are provided."""

    # TODO: This should check for exact string matches,
    #  re-evaluate when the parameters are stabilized.

    _bk = Background()
    _capture = str(_bk)
    assert "smokes(+Person)." not in _capture
Example #7
0
def test_predict_proba_test_data():
    """Assert arrays are almost equal on output of predict_proba()"""
    _bk = Background(modes=example_data.train.modes,
                     use_std_logic_variables=True)
    _dn = RDN(background=_bk, target="cancer", n_estimators=5)
    _dn.fit(example_data.train)
    assert_array_almost_equal(
        _dn.predict_proba(example_data.test),
        np.array([0.74, 0.74, 0.74, 0.25, 0.25]),
        decimal=2,
    )
Example #8
0
def test_initialize_example_background_knowledge_1():
    """Test initializing with example_data modes"""
    _bk = Background(modes=example_data.train.modes)
    assert _bk.modes == example_data.train.modes
    assert not _bk.line_search
    assert not _bk.recursion

    _capture = str(_bk)
    assert "setParam: nodeSize=2." in _capture
    assert "setParam: maxTreeDepth=3." in _capture
    assert "setParam: numberOfCycles=100." in _capture
    assert "setParam: numberOfClauses=100." in _capture
    assert "friends(+Person,-Person)." in _capture
    assert "friends(-Person,+Person)." in _capture
    assert "smokes(+Person)." in _capture
    assert "cancer(+Person)." in _capture
Example #9
0
def test_initializing_example_background_knowledge_2():
    """Test initializing with example_data modes and extra parameters."""
    _bk = Background(
        modes=example_data.train.modes,
        line_search=True,
        recursion=True,
        node_size=3,
        max_tree_depth=4,
        number_of_clauses=8,
        number_of_cycles=10,
    )
    assert _bk.modes == example_data.train.modes

    _capture = str(_bk)
    assert "setParam: nodeSize=3." in _capture
    assert "setParam: maxTreeDepth=4." in _capture
    assert "setParam: numberOfCycles=10." in _capture
    assert "setParam: numberOfClauses=8." in _capture
    assert "setParam: lineSearch=true." in _capture
    assert "setParam: recursion=true." in _capture
    assert "friends(+Person,-Person)." in _capture
    assert "friends(-Person,+Person)." in _capture
    assert "smokes(+Person)." in _capture
    assert "cancer(+Person)." in _capture
Example #10
0
def test_initialize_bad_background_knowledge_recursion(test_input):
    """Incorrect recursion settings."""
    with pytest.raises(ValueError):
        _bk = Background(recursion=test_input)
Example #11
0
def test_initialize_bad_background_knowledge_max_tree_depth(test_input):
    """Incorrect max_tree_depth settings."""
    with pytest.raises(ValueError):
        _bk = Background(max_tree_depth=test_input)
Example #12
0
def test_initialize_bad_background_knowledge_line_search(test_input):
    """Incorrect line_search settings"""
    with pytest.raises(ValueError):
        _bk = Background(line_search=test_input)
Example #13
0
def test_initialize_bad_background_knowledge_number_of_clauses(test_input):
    """Incorrect number_of_cycles settings."""
    with pytest.raises(ValueError):
        _bk = Background(number_of_clauses=test_input)
Example #14
0
def test_initialize_bad_background_knowledge_modes(test_input):
    """Incorrect modes settings"""
    with pytest.raises(ValueError):
        _bk = Background(modes=test_input)
Example #15
0
def test_initialize_bad_background_knowledge_load_all_basic_modes(test_input):
    """Incorrect load_all_basic_modes arguments."""
    with pytest.raises(ValueError):
        _bk = Background(load_all_basic_modes=test_input)
Example #16
0
def test_initialize_bad_prolog_variables(test_input):
    """Initialize use_prolog_variables with input which raises error."""
    with pytest.raises(ValueError):
        _bk = Background(use_prolog_variables=test_input)
Example #17
0
def test_write_background_to_file_1(tmpdir):
    """Test writing Background object to a file with default parameters."""
    _bk = Background()
    _bk.write(filename="train", location=pathlib.Path(tmpdir))
    assert tmpdir.join("train_bk.txt").read() == str(_bk)