def test_optimal_tree3(): records = parse_data(r"test_optimal_tree3.txt") tree = optimal_tree(records, ["fever","cough","headache"],2) assert "influenza" == tree.positive_child.positive_child.data assert "meningitis" == tree.positive_child.negative_child.data assert "cold" == tree.negative_child.positive_child.data assert "healthy" == tree.negative_child.negative_child.data
def test_optimal_tree(capsys): symptoms = [ 'congestion', 'cough', 'fatigue', 'fever', 'headache', 'irritability', 'muscle_ache', 'nausea', 'rigidity', 'sore_throat' ] for fn, successes in { r'data/tiny_data.txt': [1 / 6, 2 / 6, 4 / 6, 5 / 6, 1, 1, 1, 1, 1, 1, 1], r'data/small_data.txt': [ 10 / 60, 20 / 60, 36 / 60, 49 / 60, 52 / 60, 53 / 60, 55 / 60, 56 / 60, 56 / 60, 56 / 60, 56 / 60 ], r'data/medium_data.txt': [ 100 / 600, 195 / 600, 346 / 600, 445 / 600, 491 / 600, 517 / 600, 534 / 600, 540 / 600, 543 / 600, 544 / 600, 545 / 600 ], r'data/big_data.txt': [1000 / 6000, 1951 / 6000, 3359 / 6000, 4403 / 6000], }.items(): records = ex11.parse_data(fn) for i in range(len(symptoms) + 1): if fn.endswith('big_data.txt') and i > 3: continue tree = ex11.optimal_tree(records, symptoms, i) if CHECK_OPTIMAL_TREE: actual = repr_tree(tree) # open(fn + '.4expected' + str(i), 'w').write(actual) expected1 = open(fn + '.expected' + str(i)).read() expected2 = open(fn + '.2expected' + str(i)).read() expected3 = open(fn + '.3expected' + str(i)).read() expected4 = open(fn + '.4expected' + str(i)).read() assert actual in (expected1, expected2, expected3, expected4), \ "Maybe your answer is correct, check it carefully. " \ "If it's OK, remove this assert." cur_diagnoser = ex11.Diagnoser(tree) # print(diagnoser.calculate_success_rate(records), end=', ') actual = cur_diagnoser.calculate_success_rate(records) expected = successes[i] assert actual == expected, \ f"tree: {repr_tree(cur_diagnoser.root)}\n" \ f"records from: {fn}\n" \ f"num of symptoms: {i}\n" \ f"expected: {expected}\n" \ f"actual: {actual}" # if fn.endswith('big_data.txt') and i == 3: # with open('data/big_diagnoser.dmp', 'wb') as f: # pickle.dump(diagnoser, f) # if fn.endswith('medium_data.txt') and i == 6: # with open('data/medium_diagnoser.dmp', 'wb') as f: # pickle.dump(diagnoser, f) # print() out, err = capsys.readouterr() assert not out and not err, f"Don't print. out: '{out}', err: '{err}'"
# test 5 expected = [[True, False, True], [False, True, False]] result = diagnoser.paths_to_illness("influenza") check(5, expected, result, "influenza", list_check) # test 6 result = build_tree(records, ["cough", "sneezing", "fever"]) check(6, root, result, "", tree_check) if not tree_check(result, root): print("expected: ") print_tree(root) print("result: ") print_tree(result) # test 7 result = optimal_tree(records, ["cough", "sneezing", "fever"], 2) new_leaf1 = Node("influenza", None, None) new_leaf2 = Node("cold", None, None) new_fever1 = Node("fever", new_leaf1, new_leaf2) new_leaf3 = Node("dead", None, None) new_leaf4 = Node("healthy", None, None) new_fever2 = Node("fever", new_leaf3, new_leaf4) new_root = Node("cough", new_fever1, new_fever2) if not tree_check(result, new_root): print("expected: ") print_tree(new_root) print("result: ") print_tree(result)
def test_optimal_tree2(): records = parse_data(r"test_optimal_tree2.txt") tree = optimal_tree(records,["cough", "fever", "headache"],1) assert "fever" == tree.data or "headache" == tree.data
def test_optimal_tree1(): records = parse_data(r"test_optimal_tree1.txt") tree = optimal_tree(records, ["cough", "fever", "headache"], 2) assert "cough" == tree.data or "fever" == tree.data assert "cough" == tree.positive_child.data or "fever" == tree.positive_child.data