# -*- coding: utf-8 -*-
"""
Created on Sat Jan 13 18:06:54 2018

@author: Sophie
"""
import neticapy as netica

env = netica.Environ()	

net = netica.Net(None, "Data Files/ChestClinic.dne", "NO_VISUAL_INFO")

test_nodes = netica.NodeList(0, net)
unobsv_nodes = netica.NodeList(0, net)

VisitAsia = net.get_node_named("VisitAsia")
Tuberculosis = net.get_node_named("Tuberculosis")
Cancer = net.get_node_named("Cancer")
Smoking = net.get_node_named("Smoking")
TbOrCa = net.get_node_named("TbOrCa")
XRay = net.get_node_named("XRay")
Dyspnea = net.get_node_named("Dyspnea")
Bronchitis = net.get_node_named("Bronchitis")

# The observed nodes are the factors known during diagnosis:
test_nodes.add(Cancer, None)

# The unobserved nodes are the factors not known during diagnosis:
unobsv_nodes.add(Bronchitis, None)
unobsv_nodes.add(Tuberculosis, None)
unobsv_nodes.add(TbOrCa, None)

net.retract_findings()      # IMPORTANT: Otherwise any findings will be part of tests !!

net.compile_net()    

tester = netica.Tester(test_nodes, unobsv_nodes, -1)

casefile = netica.Stream("Data Files/ChestClinic.cas")

caseset = netica.Caseset("ChestClinicCases")

caseset.add_cases_from_file(casefile, 1)
tester.test_with_cases(caseset)

def print_confusion_matrix(tester, node):
    numstates = range(node.num_states)
    print("\nConfusion matrix for {}:\n".format(node.name))
    for i in numstates:
        print(node.get_state_name(i).ljust(15), end='')
    print("Actual".ljust(15))
    for a in numstates:
        for p in numstates:
            print(repr(tester.get_confusion_matrix(node, p, a)).ljust(15), end='')
        print(node.get_state_name(a).ljust(15))

print_confusion_matrix(tester, Cancer)

print("\nError rate for {} = {}%\n".format(Cancer.name, 
      (tester.error_rate(Cancer) * 100.0)))

print("Logarithmic loss for {} = {}\n".format(Cancer.name, 
      tester.log_loss(Cancer)))

res, mesg = env.close_netica()
print(mesg)
