def test_write_to_location_1(tmpdir): """ Test that predicates are written to files in a target location with add_pos syntax. """ _db = Database() _db.add_pos("a(b).") _db.add_neg("a(c).") _db.add_fact("d(b,c).") _db.write(filename="train", location=pathlib.Path(tmpdir)) assert tmpdir.join("train_pos.txt").read() == "a(b).\n" assert tmpdir.join("train_neg.txt").read() == "a(c).\n" assert tmpdir.join("train_facts.txt").read() == "d(b,c).\n"
def test_initialize_database_1(): """ Test initializing a Database with defualt settings. """ _db = Database() assert _db.pos == [] assert _db.neg == [] assert _db.facts == []
def test_initialize_database_2(): """ Test initializing a Database with a specific target. """ _db = Database() assert _db.pos == [] assert _db.neg == [] assert _db.facts == []
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
def test_initialize_from_files_lazy_paths(): """Test initializing from pathlib.Path filenames with lazy loading.""" _db = Database.from_files( pos=pathlib.Path("datasets/ToyFather/train/pos.pl"), neg=pathlib.Path("datasets/ToyFather/train/neg.pl"), facts=pathlib.Path("datasets/ToyFather/train/facts.pl"), lazy_load=True, ) assert _db.pos == pathlib.Path("datasets/ToyFather/train/pos.pl") assert _db.neg == pathlib.Path("datasets/ToyFather/train/neg.pl") assert _db.facts == pathlib.Path("datasets/ToyFather/train/facts.pl")
def test_initialize_from_files_lazy_strings(): """Test initializing from string filename with lazy loading.""" _db = Database.from_files( pos="datasets/ToyFather/train/pos.pl", neg="datasets/ToyFather/train/neg.pl", facts="datasets/ToyFather/train/facts.pl", lazy_load=True, ) assert _db.pos == "datasets/ToyFather/train/pos.pl" assert _db.neg == "datasets/ToyFather/train/neg.pl" assert _db.facts == "datasets/ToyFather/train/facts.pl"
def test_initialize_mix(): """Test initializing from a mix of lazy and lists.""" _pos = "datasets/ToyFather/train/pos.pl" _neg = "datasets/ToyFather/train/neg.pl" _facts = pathlib.Path("datasets/ToyFather/train/facts.pl") _db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=True) _db.neg = ["father(harrypotter,ronweasley)."] assert isinstance(_db.pos, str) assert isinstance(_db.neg, list) assert isinstance(_db.facts, pathlib.Path)
def test_boston_predict_after_load(test_input): """Load a 0.5.2 BostonHousing json file and predict.""" clf = BoostedRDNRegressor() clf.from_json("srlearn/tests/regression_tests/json/boston_{0}.json".format( test_input)) test = Database.from_files( pos="datasets/Boston/test/pos.pl", neg="datasets/Boston/test/neg.pl", facts="datasets/Boston/test/facts.pl", ) _predictions = clf.predict(test) assert len(_predictions) == 13
def test_lazy_write(tmpdir): """Test writing to a location after a lazy load.""" _pos = "datasets/ToyFather/train/pos.pl" _neg = "datasets/ToyFather/train/neg.pl" _facts = "datasets/ToyFather/train/facts.pl" _db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=True) assert isinstance(_db.pos, str) assert isinstance(_db.neg, str) assert isinstance(_db.facts, str) _db.write(filename="train", location=pathlib.Path(tmpdir)) assert tmpdir.join("train_pos.txt").read() == open(_pos).read() assert tmpdir.join("train_neg.txt").read() == open(_neg).read() assert tmpdir.join("train_facts.txt").read() == open(_facts).read()
def test_initialize_from_files_not_lazy(): """Test initializing from files without lazy loading.""" _pos = "datasets/ToyFather/train/pos.pl" _neg = "datasets/ToyFather/train/neg.pl" _facts = "datasets/ToyFather/train/facts.pl" _db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=False) assert isinstance(_db.pos, list) assert isinstance(_db.neg, list) assert isinstance(_db.facts, list) assert _db.pos == open(_pos).read().splitlines() assert _db.neg == open(_neg).read().splitlines() assert _db.facts == open(_facts).read().splitlines()
def test_mixed_write(tmpdir): """Test writing to a location with a mix of lazy, non-lazy, and lists.""" _pos = "datasets/ToyFather/train/pos.pl" _neg = "datasets/ToyFather/train/neg.pl" _facts = pathlib.Path("datasets/ToyFather/train/facts.pl") _db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=True) _db.neg = ["father(harrypotter,ronweasley)."] assert isinstance(_db.pos, str) assert isinstance(_db.neg, list) assert isinstance(_db.facts, pathlib.Path) _db.write(filename="test", location=pathlib.Path(tmpdir)) assert tmpdir.join("test_pos.txt").read() == open(_pos).read() assert tmpdir.join( "test_neg.txt").read() == "father(harrypotter,ronweasley).\n" assert tmpdir.join("test_facts.txt").read() == open(_facts).read()