# -*- coding: utf-8 -*-
"""
Read in the 'BreastCancer.dne' Bayes net, and enter data on 
a particular case to classify it as malignant or not.

@author: Sophie
"""
import sys, os
sys.path.append(os.path.abspath('../src'))
import neticapy

env = neticapy.Environ()
res, mesg = env.init_netica()
print(mesg)
if res < 0:
    raise SystemExit

net = env.new_file_stream("Data Files/BreastCancer.dne").read_net(neticapy.ReadingOption.NO_VISUAL_INFO)

Malignancy = net.get_node_named("Malignancy")
ClumpThickness = net.get_node_named("ClumpThickness")
UniformityOfCellSize = net.get_node_named("UniformityOfCellSize");
UniformityOfCellShape = net.get_node_named("UniformityOfCellShape");
MarginalAdhesion = net.get_node_named("MarginalAdhesion");
SingleEpithelialCellSize = net.get_node_named("SingleEpithelialCellSize");
BareNuclei = net.get_node_named("BareNuclei");
BlandChromatin = net.get_node_named("BlandChromatin");
NormalNucleoli = net.get_node_named("NormalNucleoli");
Mitoses = net.get_node_named("Mitoses");

nodes = net.nodes

""" Clear any CPT data """

for i in range(nodes.length):
    nodes.nth_node(i).delete_tables()

""" Train net based on database of Wisconsin breast cancer study """

net.revise_CPTs_by_case_file(env.new_file_stream("Data Files/BreastCancer.cas"), nodes, 1)

""" Now perform a classification for a particular case:  10,3,3,2,2,10,4,1,2 """

ClumpThickness.enter_value(10.0)
UniformityOfCellSize.enter_value(3.0)
UniformityOfCellShape.enter_value(3.0)
MarginalAdhesion.enter_value(2.0)
SingleEpithelialCellSize.enter_value(2.0)
BareNuclei.enter_value(10.0)
BlandChromatin.enter_value(4.0)
NormalNucleoli.enter_value(1.0)
Mitoses.enter_value(2.0)

net.compile_()

print("\nThe probability of this cell being malignant is %g\n\n" % Malignancy.get_beliefs()[0])

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