示例#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)
示例#2
0
def test_initializing_example_background_knowledge_3():
    """Test initializing with example data modes and extra parameters."""
    train, _ = load_toy_cancer()
    _bk = Background(
        modes=train.modes,
        line_search=True,
        recursion=True,
        node_size=3,
        max_tree_depth=4,
        number_of_clauses=8,
        number_of_cycles=10,
        ok_if_unknown=["smokes/1", "friends/2"],
        bridgers=["friends/2"],
    )
    assert _bk.modes == train.modes

    _capture = str(_bk)
    assert "setParam: nodeSize=3." in _capture
    assert "setParam: maxTreeDepth=4." in _capture
    assert "setParam: numOfCycles=10." in _capture
    assert "setParam: numOfClauses=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
    assert "okIfUnknown: smokes/1." in _capture
    assert "okIfUnknown: friends/2." in _capture
    assert "bridger: friends/2." in _capture
示例#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 = BoostedRDN(background=_bk, target="cancer", n_estimators=test_input)
    _dn.fit(example_data.train)
    assert len(_dn.estimators_) == test_input
示例#4
0
def test_predict_example_data(test_input):
    """Test learn and predict."""
    train, test = load_toy_cancer()
    _bk = Background(modes=train.modes)
    _dn = BoostedRDN(background=_bk, target="cancer", n_estimators=test_input)
    _dn.fit(train)
    assert_array_equal(_dn.predict(test), np.array([1.0, 1.0, 1.0, 0.0, 0.0]))
示例#5
0
def test_initialize_bad_n_estimators(test_input):
    """Test bad values for n_estimators"""
    _dn = BoostedRDN(target="cancer",
                     background=Background(),
                     n_estimators=test_input)
    with pytest.raises(ValueError):
        _dn.fit(example_data.train)
示例#6
0
def test_learn_example_dataset_1(test_input):
    """Learn from the example database."""
    train, _ = load_toy_cancer()
    _bk = Background(modes=train.modes)
    _dn = BoostedRDN(background=_bk, target="cancer", n_estimators=test_input)
    _dn.fit(train)
    assert len(_dn.estimators_) == test_input
示例#7
0
def test_iff_variables(std, pro):
    """Variable representation cannot be equal."""
    if std == pro:
        with pytest.raises(ValueError):
            _bk = Background(
                use_std_logic_variables=std,
                use_prolog_variables=pro,
            )
    else:
        _bk = Background(
            use_std_logic_variables=std,
            use_prolog_variables=pro,
        )

        assert _bk.use_std_logic_variables == std
        assert _bk.use_prolog_variables == pro
示例#8
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
示例#9
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 = BoostedRDN(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]))
示例#10
0
def test_initialize_bad_neg_pos_ratio(test_input):
    """Tests bad values for neg_pos_ratio"""
    _dn = BoostedRDN(target="cancer",
                     background=Background(),
                     neg_pos_ratio=test_input)
    train, _ = load_toy_cancer()
    with pytest.raises(ValueError):
        _dn.fit(train)
示例#11
0
def test_cannot_read_outside_length_of_dotfiles():
    """Test that invalid tree indexes raise errors."""
    train, _ = load_toy_cancer()
    bkg = Background(modes=train.modes)
    clf = BoostedRDN(target="cancer", background=bkg)
    clf.fit(train)
    for test_input in [-10, -5, -1, 10]:
        with pytest.raises(IndexError):
            _ = export_digraph(clf, tree_index=test_input)
示例#12
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 = BoostedRDN(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,
    )
示例#13
0
def test_predict_proba_test_data():
    """Assert arrays are almost equal on output of predict_proba()"""
    train, test = load_toy_cancer()
    _bk = Background(modes=train.modes)
    _dn = BoostedRDN(background=_bk, target="cancer", n_estimators=5)
    _dn.fit(train)
    assert_array_almost_equal(
        _dn.predict_proba(test),
        np.array([0.74, 0.74, 0.74, 0.25, 0.25]),
        decimal=2,
    )
def test_feature_importances_toy_cancer():
    """Test getting the feature importances from the Toy-Cancer set."""
    train, _ = load_toy_cancer()
    bkg = Background(modes=train.modes)
    rdn = BoostedRDN(
        target="cancer",
        background=bkg,
        n_estimators=10,
    )
    rdn.fit(train)
    _features = rdn.feature_importances_
    assert _features.most_common(1)[0] == ("smokes", 10)
示例#15
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
示例#16
0
def test_serialize_BoostedRDN(tmpdir):
    """Test that inference is possible after loading from json"""
    output_json = tmpdir.join("ToyCancerRDN.json")
    train, test = load_toy_cancer()
    bkg = Background(modes=train.modes)
    rdn = BoostedRDN(background=bkg, target="cancer", n_estimators=5)
    rdn.fit(train)
    rdn.to_json(output_json)

    # New BoostedRDN instance, loading from file, and running.
    rdn2 = BoostedRDN()
    rdn2.from_json(output_json)

    _predictions = rdn2.predict(test)
    assert len(rdn2.estimators_) == 5
    assert_array_equal(_predictions, np.array([1.0, 1.0, 1.0, 0.0, 0.0]))
示例#17
0
def test_serialize_BoostedRDNRegressor(tmpdir):
    """Test serializing and inference with a regressor object."""
    output_json = tmpdir.join("BostonHousingRDN.json")

    train = Database.from_files(
        pos="datasets/Boston/train/pos.pl",
        neg="datasets/Boston/train/neg.pl",
        facts="datasets/Boston/train/facts.pl",
    )

    bkg = Background(modes=[
        "crim(+id,#varsrim).",
        "zn(+id,#varzn).",
        "indus(+id,#varindus).",
        "chas(+id,#varchas).",
        "nox(+id,#varnox).",
        "rm(+id,#varrm).",
        "age(+id,#varage).",
        "dis(+id,#vardis).",
        "rad(+id,#varrad).",
        "tax(+id,#vartax).",
        "ptratio(+id,#varptrat).",
        "b(+id,#varb).",
        "lstat(+id,#varlstat).",
        "medv(+id).",
    ])

    rdn = BoostedRDNRegressor(background=bkg, target="medv", n_estimators=5)
    rdn.fit(train)
    rdn.to_json(output_json)

    # New BoostedRDN instance, loading from file, and running.
    rdn2 = BoostedRDNRegressor()
    rdn2.from_json(output_json)

    test = Database.from_files(
        pos="datasets/Boston/test/pos.pl",
        neg="datasets/Boston/test/neg.pl",
        facts="datasets/Boston/test/facts.pl",
    )

    _predictions = rdn2.predict(test)
    assert len(_predictions) == 13
示例#18
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
示例#19
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)
示例#20
0
def test_initialize_bad_bridgers(test_input):
    """Initialize bridgers with input that should raise an error."""
    with pytest.raises(ValueError):
        _ = Background(bridgers=test_input)
示例#21
0
def test_initialize_bad_ok_if_unknown_variables(test_input):
    """Initialize ok_if_unknown with input that should raise error."""
    with pytest.raises(ValueError):
        _ = Background(ok_if_unknown=test_input)
示例#22
0
def test_initialize_bad_prolog_variables(test_input):
    """Initialize use_prolog_variables with input which raises error."""
    with pytest.raises(ValueError):
        _ = Background(use_prolog_variables=test_input)
示例#23
0
def test_initialize_bad_background_knowledge_load_all_basic_modes(test_input):
    """Incorrect load_all_basic_modes arguments."""
    with pytest.raises(ValueError):
        _ = Background(load_all_basic_modes=test_input)
示例#24
0
def test_initialize_bad_background_knowledge_number_of_clauses(test_input):
    """Incorrect number_of_cycles settings."""
    with pytest.raises(ValueError):
        _ = Background(number_of_clauses=test_input)
示例#25
0
def test_initialize_bad_background_knowledge_max_tree_depth(test_input):
    """Incorrect max_tree_depth settings."""
    with pytest.raises(ValueError):
        _ = Background(max_tree_depth=test_input)
示例#26
0
def test_initialize_bad_background_knowledge_line_search(test_input):
    """Incorrect line_search settings"""
    with pytest.raises(ValueError):
        _ = Background(line_search=test_input)
示例#27
0
def test_initialize_bad_background_knowledge_recursion(test_input):
    """Incorrect recursion settings."""
    with pytest.raises(ValueError):
        _ = Background(recursion=test_input)
示例#28
0
def test_initialize_bad_background_knowledge_modes(test_input):
    """Incorrect modes settings"""
    with pytest.raises(ValueError):
        _ = Background(modes=test_input)
示例#29
0
def test_string_conversion_no_modes():
    """Test initializing when no modes are provided."""

    _bk = Background()
    _capture = str(_bk)
    assert "smokes(+person)." not in _capture