def test_train(self):
        values = [[True, False, True, True,
                   0.1], [True, True, True, True, 0.2],
                  [True, False, True, True, 0.5],
                  [False, True, True, False, -0.3],
                  [True, False, True, False, -0.0],
                  [True, True, True, False, -0.2],
                  [True, False, False, True, 0.6],
                  [False, True, False, True, 0.8],
                  [True, True, False, True, 0.1],
                  [False, False, False, False, -0.8],
                  [False, True, False, False, -0.2],
                  [False, True, False, False, -0.3],
                  [False, True, False, False, -0.5]]

        nor = NoisyOR("or", ["v1", "v2", "v3", "v4"])
        v1 = CPT("v1", [])
        v2 = CPT("v2", [])
        v3 = CPT("v3", [])
        v4 = CPT("v4", [])
        y = GDT("y", ["or"])
        bn = BNet()
        bn.add(nor)
        bn.add(v1)
        bn.add(v2)
        bn.add(v3)
        bn.add(v4)
        bn.add(y)
        bn.train(["v1", "v2", "v3", "v4", "y"], values, ["or"], 1)
        print nor
        print y
        print v1
        print v2
        print v3
        print v4
 def testroot(self):
     print "\n****Checking node root****"
     self.assertTrue(self.burglary.is_root())
     self.assertFalse(self.alarm.is_root())
     self.assertFalse(self.johncalls.is_root())
     X = CPT("X", ["M"])
     self.assertFalse(X.is_root())
 def put(self, key, prob):
     """
     Set the variable of this Noisy-OR CPT if the parent variables
     are set accordingly (one to True, rest to False)
     """
     if key.count(True) != 1:
         return
     CPT.put(self, key, prob)
Пример #4
0
def Parser(file_name):
    """
    Parses the xmlbif file and builds the list of variables.
    Also builds the list condiotional probability tables.
    Also couple hash tables are built to quickly access things.
    """
    tree = ET.parse(file_name)
    root = tree.getroot()

    n_prime = 67

    Hash_Variables = HashTable(n_prime)
    Hash_CPT = HashTable(n_prime)
    Hash_Nodes = HashTable(n_prime)

    variables = []
    cp_tables = []

    if root.tag != "BIF":
        print "file must be xmlbif"
    else:
        root = root[0]
        if root.tag != "NETWORK":
            print "file must have a NETWORK tag"
        else:
            for i in xrange(1, len(root)):
                if root[i].tag == "VARIABLE":
                    domain = getDomain(root[i])
                    rv = RandomVariable(root[i][0].text, domain)
                    variables.append(rv)
                    Hash_Variables.put(rv.name, rv)
                elif root[i].tag == "DEFINITION":
                    cpt = CPT()
                    for j in xrange(0, len(root[i])):
                        if root[i][j].tag == "FOR":
                            cpt.add_for_variable(
                                Hash_Variables.get(root[i][j].text))
                        elif root[i][j].tag == "GIVEN":
                            cpt.add_given_variable(
                                Hash_Variables.get(root[i][j].text))
                        elif root[i][j].tag == "TABLE":
                            cpt.build_table()
                            split_text = root[i][j].text.split(" ")
                            for t in xrange(0, len(split_text)):
                                try:
                                    p = float(split_text[t])
                                    cpt.add_prob(p)
                                except ValueError:
                                    ad = 3
                    cp_tables.append(cpt)
                    Hash_CPT.put(cpt.name, cpt)

    return variables, cp_tables, Hash_Variables, Hash_CPT, Hash_Nodes
def Parser(file_name):
    """
    Parses the xmlbif file and builds the list of variables.
    Also builds the list conditional probability tables.
    Also couple hash tables are built to quickly access things.
    """
    tree = ET.parse(file_name)
    root = tree.getroot()

    n_prime = 67

    Hash_Variables = HashTable(n_prime)
    Hash_CPT = HashTable(n_prime)
    Hash_Nodes = HashTable(n_prime)

    variables = []
    cp_tables = []

    if root.tag != "BIF":
        print "file must be xmlbif"
    else:
        root = root[0]
        if root.tag != "NETWORK":
            print "file must have a NETWORK tag"
        else:
            for i in xrange(1, len(root)):
                if root[i].tag == "VARIABLE":
                    domain = getDomain(root[i])
                    rv = RandomVariable(root[i][0].text, domain)
                    variables.append(rv)
                    Hash_Variables.put(rv.name,rv)
                elif root[i].tag == "DEFINITION":
                    cpt = CPT()
                    for j in xrange(0, len(root[i])):
                        if root[i][j].tag == "FOR":
                            cpt.add_for_variable(Hash_Variables.get(root[i][j].text))
                        elif root[i][j].tag == "GIVEN":
                            cpt.add_given_variable(Hash_Variables.get(root[i][j].text))
                        elif root[i][j].tag == "TABLE":
                            cpt.build_table()
                            split_text = root[i][j].text.split(" ")
                            for t in xrange(0, len(split_text)):
                                try:
                                    p = float(split_text[t])
                                    cpt.add_prob(p)
                                except ValueError:
                                    ad = 3
                    cp_tables.append(cpt)
                    Hash_CPT.put(cpt.name,cpt)

    return variables, cp_tables, Hash_Variables, Hash_CPT, Hash_Nodes
Пример #6
0
    def main4(self):
        values = [
            #  A      G      F      E      C    B    D
            [True, False, True, True, 0.1, False, True],
            [True, True, True, True, 0.2, True, True],
            [True, False, True, True, 0.5, False, False],
            [False, True, True, False, -0.3, False, True],
            [True, False, True, False, -0.0, True, True],
            [True, True, True, False, -0.2, False, True],
            [True, False, False, True, 0.6, False, True],
            [False, True, False, True, 0.8, False, True],
            [True, True, False, True, 0.1, True, True],
            [False, False, False, False, -0.8, True, False],
            [False, True, False, False, -0.2, False, True],
            [False, True, False, False, -0.3, True, True],
            [False, True, False, False, -0.5, False, True]
        ]

        a = CPT("A", [])
        b = CPT("B", [])
        c = GDT("C", ["B"])  # Gaussian continuous
        d = CPT("D", ["A"])
        e = CPT("E", ["A", "B"])
        f = CPT("F", ["D", "E"])
        g = CPT("G", ["F"])
        # put them all into the BN
        bn = BNet()
        bn.add(a)
        bn.add(b)
        bn.add(c)
        bn.add(d)
        bn.add(e)
        bn.add(f)
        bn.add(g)

        # train it using EM
        bn.train(["A", "G", "F", "E", "C", "B", "D"], values, [], 9)
        #        bn.train( ["A", "G", "F", "E"], values,  ["B", "D"], 9)
        print a
        print b
        print c
        print d
        print e
        print f
        print g

        c.instance = 0.5
        a.instance = True
        test = bn.infer_naive(["E"])
        test.normalize()
        print test
Пример #7
0
    def main(self):
        values = [
            #  A      G      F      E      C
            [True, False, True, True, 0.1],
            [True, True, True, True, 0.2],
            [True, False, True, True, 0.5],
            [False, True, True, False, -0.3],
            [True, False, True, False, -0.0],
            [True, True, True, False, -0.2],
            [True, False, False, True, 0.6],
            [False, True, False, True, 0.8],
            [True, True, False, True, 0.1],
            [False, False, False, False, -0.8],
            [False, True, False, False, -0.2],
            [False, True, False, False, -0.3],
            [False, True, False, False, -0.5]
        ]
        # construct a BN with two root nodes
        a = CPT("A", [])
        b = CPT("B", [])
        c = GDT("C", ["B"])  # Gaussian continuous
        d = CPT("D", ["A"])
        e = CPT("E", ["A", "B"])
        f = CPT("F", ["D", "E"])
        g = CPT("G", ["F"])
        # put them all into the BN
        bn = BNet()
        bn.add(a)
        bn.add(b)
        bn.add(c)
        bn.add(d)
        bn.add(e)
        bn.add(f)
        bn.add(g)

        # train it using EM
        bn.train(["A", "G", "F", "E", "C"], values, ["B", "D"], 9)
        print a
        print b
        print c
        print d
        print e
        print f
        print g

        c.instance = 0.5
        a.instance = True
        test = bn.infer_naive(["E"])
        test.normalize()
        print test
Пример #8
0
    def main2(self):

        values = [
            #  A      G      F      E
            [True, False, True, True],
            [True, True, True, True],
            [True, False, True, True],
            [False, True, True, False],
            [True, False, True, False],
            [True, True, True, False],
            [True, False, False, True],
            [False, True, False, True],
            [True, True, False, True],
            [False, False, False, False],
            [False, True, False, False],
            [False, True, False, False],
            [False, True, False, False]
        ]
        # construct a BN with two root nodes
        a = CPT("A", [])
        b = CPT("B", [])
        d = CPT("D", ["A"])
        e = CPT("E", ["A", "B"])
        f = CPT("F", ["D", "E"])
        g = CPT("G", ["F"])
        # put them all into the BN
        bn = BNet()
        bn.add(a)
        bn.add(b)
        bn.add(d)
        bn.add(e)
        bn.add(f)
        bn.add(g)

        # train it using EM
        bn.train(["A", "G", "F", "E"], values, ["B", "D"], 9)
        print a
        print b
        print d
        print e
        print f
        print g

        a.instance = True
        test = bn.infer_naive(["E"])
        test.normalize()
        print test
 def get(self, status, key):
     """
     Retrieve the Noisy-OR filtered probability of a parent variable setting
     """
     pnot_true = 1.0
     for i in range(len(key)):
         if not key[i]:
             continue
         onekey = [False] * len(key)
         onekey[i] = True
         p = CPT.get(self, True, onekey)
         if p is None:
             return None
         pnot_true *= (1.0 - p)
     return 1.0 - pnot_true if status else pnot_true
 def testadvanced6(self):
     print "\n****Testing inference larger network (Train network)****"
     print "Setting A = True, inferring extra C node (not GDT) and D,E"
     C = CPT("C", ["B"])
     C.put([True], 0.95)
     C.put([False], 0.86)
     self.bn_2.add(C)
     self.A.instance = True
     test = self.bn_2.infer_naive(["C", "D", "E"])
     test.normalize()
     print test
Пример #11
0
 def computeOutgoingCPT(self, idx):
     prob = 1.0
     for i in range(0, self.no_of_neighbours):
         if i == idx:
             continue
         partner_name = self.neighbours[i].name
         current_cpt = self.incoming_cpt[partner_name]
         if current_cpt.is_unity_cpt:
             continue
         if (current_cpt.node_name !=
                 self.name) or (current_cpt.conditionals_length != 0):
             print("Kuch toh jhol he")
             break
         prob = prob * current_cpt.cpt_values[0]
     outgoing_neighbour_name = self.neighbours[idx].neighbours[0].name
     return_cpt = CPT(self.name, [], [prob])
     return return_cpt
 def testtrain1(self):
     """
     \n****Testing training the large train network,
     two unknown, one GDT****
     """
     values=[
         #  A      G      F      E      C
         [True,  False, True,  True,   0.1],
         [True,  True,  True,  True,   0.2],
         [True,  False, True,  True,   0.5],
         [False, True,  True,  False, -0.3],
         [True,  False, True,  False, -0.0],
         [True,  True,  True,  False, -0.2],
         [True,  False, False, True,   0.6],
         [False, True,  False, True,   0.8],
         [True,  True,  False, True,   0.1],
         [False, False, False, False, -0.8],
         [False, True,  False, False, -0.2],
         [False, True,  False, False, -0.3],
         [False, True,  False, False, -0.5]]
     
     a = CPT("A", [])
     b = CPT("B", [])
     c = GDT("C",  ["B"]) # Gaussian continuous
     d = CPT("D",  ["A"])
     e = CPT("E",  ["A", "B"])
     f = CPT("F",  ["D", "E"])
     g = CPT("G",  ["F"])
     bn = BNet()
     bn.add(a)
     bn.add(b)
     bn.add(c)
     bn.add(d)
     bn.add(e)
     bn.add(f)
     bn.add(g)
     
     bn.train( ["A", "G", "F", "E", "C"], values,  ["B", "D"], 9)
     c.instance = 0.5
     a.instance = True
     test = bn.infer_naive(["E"])
     test.normalize()
     print test
Пример #13
0
    def main3(self):
        values = [[-0.1, None], [-0.5, None], [-0.3, True], [0.3, None],
                  [0.4, None], [0.1, None], [-0.4, None], [-0.2, None],
                  [-0.3, None], [0.1, None], [0.4, False], [0.2, None]]
        # construct a BN with one boolean root and one continuous child
        a = CPT("A", [])
        b = GDT("B", ["A"])
        #b.setTieVariances(True)
        bn = BNet()
        bn.add(a)
        bn.add(b)
        #bn.EM_PRINT_STATUS=True
        # train it using EM
        #        bn.train(["B","A"], values, [], 5)
        bn.train(["B"], values, ["A"], 5)
        print a
        print b

        b.instance = 0.0
        test = bn.infer_naive(["A"])
        test.normalize()
        print test
Пример #14
0
from CPT import CPT
from PredictionTree import PredictionTree
from tqdm import tqdm
from time import sleep

model = CPT()
data, target = model.load_files("./data/train.csv", "./data/test.csv")
# print(data)
# print(target)
model.train(data)
predictions = model.predict(data, target, 5, 3)
print(predictions)
import time
from CPT import CPT, read_file

if __name__ == '__main__':

    t1 = time.time()

    # Prepare data from file
    datafile = './data/train.csv'
    data = read_file(datafile,
                     id_col='ID',
                     line_num_col='LINE_NB',
                     code_col='CODE',
                     require_sorting=False)

    t2 = time.time()

    # Instantiate Model
    my_cpt = CPT()
    # Train
    my_cpt.train(data, max_seq_length=10)
    my_cpt.prune(2)
    # Save
    pickle.dump(my_cpt, open('./model.pkl', 'wb'))

    t3 = time.time()

    print("Computing time: %i " % (t3 - t1))
    print("readfile time: %i " % (t2 - t1))
    print("model time: %i " % (t3 - t2))
Пример #16
0
class FactorNode:
    def __init__(self, node_name):
        self.name = node_name
        self.neighbours = []
        self.no_of_neighbours = 0
        self.neighbours_map = {}
        
        self.conditionals = []

        # interaction parameters
        self.incoming = []
        self.incoming_temp = []
        self.outgoing = []

        self.incoming_cpt = {}        

    def setCPT(self, cpt_values):
        self.cpt = CPT(self.neighbours[0].name, self.neighbours[1:], cpt_values)

    def addNeighbour(self, variable_node):
        self.neighbours.append(variable_node)
        neighbour_idx = self.no_of_neighbours
        self.neighbours_map[variable_node.name] = neighbour_idx
        self.no_of_neighbours = self.no_of_neighbours + 1

    def checkForIncomingMessages(self, idx):
        ready = True
        for i in range(0,self.no_of_neighbours):
            if i==idx:
                continue
            ready = ready & self.incoming[i]
        return ready

    def checkReadiness(self, neighbour_id):
        if self.outgoing[neighbour_id]==False:
            return self.checkForIncomingMessages(neighbour_id)
        return False

    def setIncomingMessage(self, id):
        idx = self.neighbours_map[id]
        self.incoming_temp[idx] = True

    def updateIncomingFlags(self):
        for i in range(0, self.no_of_neighbours):
            self.incoming[i] = self.incoming_temp[i]

    def computeOutgoingCPT(self, idx):
        cpt_list = []
        for i in range(0,self.no_of_neighbours):
            if i==idx:
                continue
            partner_name = self.neighbours[i].name            
            current_cpt = self.incoming_cpt[partner_name]
            cpt_list.append(current_cpt)                
        resultant_cpt = self.cpt.factorMultiply(cpt_list)        
        outgoing_node_name = self.neighbours[idx].name
        summed_over_cpt = resultant_cpt.notSumOver(outgoing_node_name)        
        return summed_over_cpt

    def setIncomingCPT(self, neighbour_name, cpt):
        self.incoming_cpt[neighbour_name] = cpt
Пример #17
0
 def setCPT(self, cpt_values):
     self.cpt = CPT(self.neighbours[0].name, self.neighbours[1:], cpt_values)
Пример #18
0
            value = self.get(fkey)
            if value is not None:
                for v in fkey:
                    jkey.append(v)
                jpt.put(jkey, value)
        jpt.normalize()
        return jpt

    def __str__(self):
    
        string = ""
        for s in range(len(self.labels)):
            string += self.labels[s]
            if s < len(self.labels) -1:
                string += ","
            else:
                string += ":"
        temp =  "FT^" + self.name + "(" + string + ")"
        return temp + "\n" + NumericBoolTable.__str__(self)
    



if __name__ == '__main__':
    cpt1 = CPT('A', ['B', 'C', 'D'])
    cpt1.put([True, True, False], .45)
    print cpt1
    ft = FactorTable(cpt1)
    print 'FTable name :', ft.name
    print ft
    
    def __init__(self):
        '''
        Constructor
        '''
        self.train_values = []
        self.load_data()
        bn = BNet()
        #    sequence features node(s)
        nterm = CPT("NTERM", [])
        #    structural disorder nodes
        coils_c = CPT("COILS_C", [])
        hotloops_c = CPT("HOTLOOPS_C", [])
        disorder = CPT("DISORDER", ["COILS_C", "HOTLOOPS_C"])

        #    PTM nodes
        y_phosphorylation = CPT("Y_PHOSPHORYLATION", [])
        st_phosphorylation = CPT("ST_PHOSPHORYLATION", [])
        ypwm = GDT("Y_PWM", ["Y_PHOSPHORYLATION"])
        stpwm = GDT("ST_PWM", ["ST_PHOSPHORYLATION"])
        glycosylation = CPT("GLYCOSYLATION", [])
        acetylation = CPT("ACETYLATION", [])
        ptm = NoisyOR("PTM", [
            "Y_PHOSPHORYLATION", "ST_PHOSPHORYLATION", "GLYCOSYLATION",
            "ACETYLATION"
        ])

        # domainarchitecture nodes
        cadherin = CPT("CADHERIN", [])
        igc = CPT("IGC", [])
        znfc2 = CPT("ZNFC2", [])
        ig_like = CPT("IG_LIKE", [])
        sp = CPT("SP", [])
        cc = CPT("CC", [])
        ig = CPT("IG", [])
        tm = CPT("TM", [])
        rrm = CPT("RRM", [])
        #        glob = CPT("GLOB",  [ "IGC", "IG_LIKE","IG"]);
        domain = NoisyOR("DOMAIN", [
            "CADHERIN", "ZNFC2", "SP", "CC", "TM", "RRM", "IGC", "IG_LIKE",
            "IG"
        ])

        #define the stability nodes
        Sstability = CPT("STABLE", ["NTERM", "DOMAIN", "PTM", "DISORDER"])

        #add nodes to the network
        #sequence features
        bn.add(nterm)
        disorder
        bn.add(coils_c)
        bn.add(hotloops_c)
        bn.add(disorder)
        #        #PTMs
        bn.add(y_phosphorylation)
        bn.add(ypwm)
        bn.add(st_phosphorylation)
        bn.add(stpwm)
        bn.add(glycosylation)
        bn.add(acetylation)
        bn.add(ptm)
        #domain and architecture
        bn.add(cadherin)
        bn.add(igc)
        bn.add(znfc2)
        bn.add(ig_like)
        bn.add(sp)
        bn.add(cc)
        bn.add(ig)
        bn.add(tm)
        bn.add(rrm)
        #        bn.add(glob);
        bn.add(domain)
        #stability
        bn.add(Sstability)
        #        for i in self.train_values:
        #            print i
        bn.train([
            "Y_PHOSPHORYLATION", "Y_PWM", "ST_PHOSPHORYLATION", "ST_PWM",
            "GLYCOSYLATION", "ACETYLATION", "CADHERIN", "IGC", "ZNFC2",
            "IG_LIKE", "SP", "CC", "IG", "TM", "RRM", "NTERM", "COILS_C",
            "HOTLOOPS_C", "STABLE"
        ], self.train_values, ["PTM", "DOMAIN", "DISORDER"], 7)
        #        bn.train(["Y_PHOSPHORYLATION", "Y_PWM", "ST_PHOSPHORYLATION", "ST_PWM",
        #                 "GLYCOSYLATION", "ACETYLATION"], self.train_values, ["PTM"], 7)

        print nterm
        print coils_c
        print hotloops_c
        print disorder
        print y_phosphorylation
        print ypwm
        print st_phosphorylation
        print stpwm
        print glycosylation
        print acetylation
        print ptm
        print cadherin
        print igc
        print znfc2
        print ig_like
        print sp
        print cc
        print ig
        print tm
        print rrm
        print domain
        print Sstability
    def setUp(self):
        self.burglary = CPT("B", [])
        self.burglary.put([], 0.001)
        
        self.earthquake = CPT("E", [])
        self.earthquake.put([], 0.002)
        
        self.alarm = CPT("A", ["B", "E"],
                    {
                     (True, True): 0.95,
                     (True, False): 0.94,
                     (False, True): 0.29,
                     (False, False): 0.001
                     }
                    )

        self.johncalls = CPT("J", ["A"], 
                        {
                        (True,): 0.9,
                        (False,): 0.05
                        }
                        )

        self.marycalls = CPT("M", ["A"], 
                        {
                        (True,) : 0.7,
                        (False,) : 0.01
                        }
                        )

        self.bn = BNet()
        self.bn.add(self.burglary)
        self.bn.add(self.earthquake)
        self.bn.add(self.alarm)
        self.bn.add(self.johncalls)
        self.bn.add(self.marycalls)
        
        self.A = CPT("A", [])
        self.A.put([], 0.36)
        
        self.B = CPT("B", [])
        self.B.put([], 0.21)
        
        self.D = CPT("D", ["A"])
        self.D.put([True], 0.12)
        self.D.put([False], 0.84)
        
        self.E = CPT("E", ["A", "B"])
        self.E.put([True, True], 0.76)
        self.E.put([False, True], 0.14)
        self.E.put([True, False], 0.57)
        self.E.put([False, False], 0.68)
        
        self.F = CPT("F", ["D", "E"])
        self.F.put([True, True], 0.06)
        self.F.put([False, True], 0.007)
        self.F.put([True, False], 0.61)
        self.F.put([False, False], 0.061)
        
        self.G = CPT("G", ["F"])
        self.G.put([True], 0.481)
        self.G.put([False], 0.085)
        self.bn_2 = BNet()
        self.bn_2.add(self.A)
        self.bn_2.add(self.B)
        self.bn_2.add(self.D)
        self.bn_2.add(self.E)
        self.bn_2.add(self.F)
        self.bn_2.add(self.G)
class Test(unittest.TestCase):

    def setUp(self):
        self.burglary = CPT("B", [])
        self.burglary.put([], 0.001)
        
        self.earthquake = CPT("E", [])
        self.earthquake.put([], 0.002)
        
        self.alarm = CPT("A", ["B", "E"],
                    {
                     (True, True): 0.95,
                     (True, False): 0.94,
                     (False, True): 0.29,
                     (False, False): 0.001
                     }
                    )

        self.johncalls = CPT("J", ["A"], 
                        {
                        (True,): 0.9,
                        (False,): 0.05
                        }
                        )

        self.marycalls = CPT("M", ["A"], 
                        {
                        (True,) : 0.7,
                        (False,) : 0.01
                        }
                        )

        self.bn = BNet()
        self.bn.add(self.burglary)
        self.bn.add(self.earthquake)
        self.bn.add(self.alarm)
        self.bn.add(self.johncalls)
        self.bn.add(self.marycalls)
        
        self.A = CPT("A", [])
        self.A.put([], 0.36)
        
        self.B = CPT("B", [])
        self.B.put([], 0.21)
        
        self.D = CPT("D", ["A"])
        self.D.put([True], 0.12)
        self.D.put([False], 0.84)
        
        self.E = CPT("E", ["A", "B"])
        self.E.put([True, True], 0.76)
        self.E.put([False, True], 0.14)
        self.E.put([True, False], 0.57)
        self.E.put([False, False], 0.68)
        
        self.F = CPT("F", ["D", "E"])
        self.F.put([True, True], 0.06)
        self.F.put([False, True], 0.007)
        self.F.put([True, False], 0.61)
        self.F.put([False, False], 0.061)
        
        self.G = CPT("G", ["F"])
        self.G.put([True], 0.481)
        self.G.put([False], 0.085)
        self.bn_2 = BNet()
        self.bn_2.add(self.A)
        self.bn_2.add(self.B)
        self.bn_2.add(self.D)
        self.bn_2.add(self.E)
        self.bn_2.add(self.F)
        self.bn_2.add(self.G)


    def tearDown(self):
        pass

    def testsimple1(self):
        print "\n****Setting JohnCalls = True and MaryCalls = True****"
        self.johncalls.instance = True
        self.marycalls.instance = True
        test = self.bn.infer_naive(["B"])
        test.normalize()
        print test
        self.assertAlmostEqual(test.get([True]), 0.284, 3)

    def testsimple2(self):
        print "\n****Setting Earthquake = False****"
        self.burglary.instance = None
        self.earthquake.instance = False
        test = self.bn.infer_naive(["B", "E"])
        test.normalize()
        print test

    def testsimple3(self):
        print "\n****Setting burglary = True****"
        self.earthquake.instance = None
        self.burglary.instance = True
        test = self.bn.infer_naive(["B", "E"])
        test.normalize()
        print test

    def testancestors(self):
        print "\n****Checking Ancestors****"
        self.assertEqual(self.bn.get_ancestors("J"), 
                         set(["B", "E", "A"]))
        self.assertEqual(self.bn.get_ancestors("M"), 
                         set(["B", "E", "A"]))
        self.assertEqual(self.bn.get_ancestors("B"), 
                         set([]))
        self.assertEqual(self.bn.get_ancestors("E"), 
                         set([]))

    def testnodevalueget(self):
        print "\n****Checking getting node values****"
        print "Burglary True"
        print self.burglary.get(True, [])
        print "Earthquake False"
        print self.earthquake.get(False, [])
        print "Alarm True, parents True:True"
        print self.alarm.get(True, [True, True])
        print "Alarm True, parents False:True"
        print self.alarm.get(True, [False, True])

    def testroot(self):
        print "\n****Checking node root****"
        self.assertTrue(self.burglary.is_root())
        self.assertFalse(self.alarm.is_root())
        self.assertFalse(self.johncalls.is_root())
        X = CPT("X", ["M"])
        self.assertFalse(X.is_root())

    def testadvanced(self):
        print "\n****Testing inference larger network****"
        print "Setting marycalls = False & burglary = True, inferring X"
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
#        print X
#        print Y
#        print Z

        self.marycalls.instance = False
        self.burglary.instance = True
#        for node in self.bn.nodes.values():
#            print node
#            print node.name
#            print node.instance
        test =  self.bn.infer_naive(["X"])
        test.normalize()
        print test
        test2 = self.bn.infer_naive(["Z", "A", "E"])

    def testadvanced2(self):
        print "\n****Testing inference larger network****"
        print "Setting alarm = False & burglary = True, inferring [Z,A,E]"
        
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        
        self.alarm.instance = False
        self.burglary.instance = True
        
        test = self.bn.infer_naive(["Z", "A", "E"])
        test.normalize()
        print test
        
    def testadvanced3(self):
        print "\n****Testing inference larger network****"
        print "Setting alarm = False & Y = True, inferring C"
        
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)
        
        C = CPT("C", ["B"])
        C.put([True], .36)
        C.put([False], .67)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        self.bn.add(C)
        Y.instance = True
        self.alarm.instance = False
        
        test = self.bn.infer_naive(["C"])
        test.normalize()
        print test
    
    def testadvanced4(self):
        print "\n****Testing inference larger network (Train network)****"
        print "Setting A = True, inferring F"
        self.A.instance = True
        test = self.bn_2.infer_naive(["F"])
        test.normalize()
        print test
    
    def testadvanced5(self):
        print "\n****Testing inference larger network (Train network)****"
        print "Setting A = True, inferring extra C node (not GDT)"
        C = CPT("C", ["B"])
        C.put([True], 0.95)
        C.put([False], 0.86)
        self.bn_2.add(C)
        self.A.instance = True
        test = self.bn_2.infer_naive(["C"])
        test.normalize()
        print test
    
    def testadvanced6(self):
        print "\n****Testing inference larger network (Train network)****"
        print "Setting A = True, inferring extra C node (not GDT) and D,E"
        C = CPT("C", ["B"])
        C.put([True], 0.95)
        C.put([False], 0.86)
        self.bn_2.add(C)
        self.A.instance = True
        test = self.bn_2.infer_naive(["C", "D", "E"])
        test.normalize()
        print test
        
        
    def testrelevant(self):
        print "\n****Testing getting relevant network****"
        print "Getting relevant network for node J in expanded network"
        
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        
        print self.bn.get_relevant("J").nodes.keys()
    
    def test_GDT_advanced(self):
        print "\n****Testing large network inferring GDT****"
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)
        
        G  = GDT("G", ["X"])
        G.put(Gaussian(0.24, 0.5), [True])
        G.put(Gaussian(0.62, .1), [False])

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        self.bn.add(G)
#        print self.bn.get_relevant("G").nodes.keys()
#        print G
        test = self.bn.infer_naive("G")
        print test
        
    def testtrain1(self):
        """
        \n****Testing training the large train network,
        two unknown, one GDT****
        """
        values=[
            #  A      G      F      E      C
            [True,  False, True,  True,   0.1],
            [True,  True,  True,  True,   0.2],
            [True,  False, True,  True,   0.5],
            [False, True,  True,  False, -0.3],
            [True,  False, True,  False, -0.0],
            [True,  True,  True,  False, -0.2],
            [True,  False, False, True,   0.6],
            [False, True,  False, True,   0.8],
            [True,  True,  False, True,   0.1],
            [False, False, False, False, -0.8],
            [False, True,  False, False, -0.2],
            [False, True,  False, False, -0.3],
            [False, True,  False, False, -0.5]]
        
        a = CPT("A", [])
        b = CPT("B", [])
        c = GDT("C",  ["B"]) # Gaussian continuous
        d = CPT("D",  ["A"])
        e = CPT("E",  ["A", "B"])
        f = CPT("F",  ["D", "E"])
        g = CPT("G",  ["F"])
        bn = BNet()
        bn.add(a)
        bn.add(b)
        bn.add(c)
        bn.add(d)
        bn.add(e)
        bn.add(f)
        bn.add(g)
        
        bn.train( ["A", "G", "F", "E", "C"], values,  ["B", "D"], 9)
        c.instance = 0.5
        a.instance = True
        test = bn.infer_naive(["E"])
        test.normalize()
        print test
    
    def  testtrain2(self):
        """
        \n****Testing training the small train network,
        ****
        """
        values=[  
        [-0.1, None],
        [-0.5, None],
        [-0.3, True],
        [0.3, None],
        [0.4, None],
        [0.1, None],
        [-0.4, None],
        [-0.2, None],
        [-0.3, None],
        [0.1, None],
        [0.4, False],
        [0.2, None]
        ]
        # construct a BN with one boolean root and one continuous child
        a= CPT("A", [])
        b= GDT("B", ["A"])
        #b.setTieVariances(True)
        bn= BNet()
        bn.add(a)
        bn.add(b)
        #bn.EM_PRINT_STATUS=True
        # train it using EM
#        bn.train(["B","A"], values, [], 5)
        bn.train(["B"], values, ["A"], 5)
        print a
        print b
            
        b.instance = 0.0
        test = bn.infer_naive(["A"])
        test.normalize()
        print test
    def testadvanced(self):
        print "\n****Testing inference larger network****"
        print "Setting marycalls = False & burglary = True, inferring X"
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
#        print X
#        print Y
#        print Z

        self.marycalls.instance = False
        self.burglary.instance = True
#        for node in self.bn.nodes.values():
#            print node
#            print node.name
#            print node.instance
        test =  self.bn.infer_naive(["X"])
        test.normalize()
        print test
        test2 = self.bn.infer_naive(["Z", "A", "E"])
    def __init__(self):
        '''
        placeholder
        '''
        burglary = CPT("B", [], {(): 0.001})
        #        burglary.put([], 0.001)
        #        burglary.put(
        #                     {
        #                      (): 0.001
        #                      }
        #                     )
        #        print burglary

        earthquake = CPT("E", [], {(): 0.002})
        #        earthquake.put([], 0.002)
        #        print earthquake

        alarm = CPT(
            "A", ["B", "E"], {
                (True, True): 0.95,
                (True, False): 0.94,
                (False, True): 0.29,
                (False, False): 0.001
            })
        #        alarm.put([True, True], 0.95)
        #        alarm.put([True, False], 0.94)
        #        alarm.put([False, True], 0.29)
        #        alarm.put([False, False], 0.001)
        #        print alarm

        johncalls = CPT("J", ["A"], {(True, ): 0.9, (False, ): 0.05})
        #        johncalls.put([True], 0.90)
        #        johncalls.put([False], 0.05)
        #        print johncalls
        #
        marycalls = CPT("M", ["A"], {(True, ): 0.7, (False, ): 0.01})
        #        marycalls.put([True], 0.7)
        #        marycalls.put([False], 0.01)
        #        print marycalls
        thing = CPT("T", [])
        thing.put([], 0.56)
        thing2 = CPT("T2", ["T"])
        bn = BNet()
        bn.add(burglary)
        bn.add(earthquake)
        bn.add(alarm)
        bn.add(johncalls)
        bn.add(marycalls)
        #        bn.add(thing)
        #        bn.add(thing2)

        #        print "Relevant Nodes", bn.get_relevant(['J']).nodes
        #        print "Children", bn.get_children('B')
        print bn.connection_check()
        #        print bn.connection_check()
        #        print bn.connection_check()
        #        for i in bn.get_children('A'):
        #            print "Children", i
        #        print "Roots", bn.get_roots()
        #        print "Get", alarm.get(True, [True, False])
        #        print alarm.labels
        #        earthquake.set_prior(.45)
        #        print "E Get after prior set (T)", earthquake.get(True, [])
        #        print "E Get after prior set (F)", earthquake.get(False, [])
        johncalls.instance = True
        marycalls.instance = True
        #        earthquake.instance = False
        #        alarm.instance = True
        #        burglary.instance = True
        #        print earthquake.get(True, [])

        test = bn.infer_naive(["B"])
        print test.log_likelihood()
        print test
        test = bn.infer_naive(["B", "E"])
        test.normalize()
        print test.log_likelihood()
        print test
        print

        print "Setting earthquake=False"
        burglary.instance = None
        earthquake.instance = False
        test = bn.infer_naive(["B", "E"])
        print test.log_likelihood()
        test.normalize()
        print test
        print

        print "Setting burglary=True"
        earthquake.instance = None
        burglary.instance = True
        test = bn.infer_naive(["B", "E"])
        test.normalize()
        print test
        print

        print "old", bn.get_ancestors("B")
        print "old", bn.get_ancestors("J")
        print "old", bn.get_ancestors("M")
        print "old", bn.get_ancestors("A")
        x = CPT("Q", ["A", "M"])
        bn.add(x)
        print "old", bn.get_ancestors("Q")
        x = CPT("W", [])
        bn.add(x)
        x = CPT("R", ["W", "E", "B"])
        bn.add(x)
        print "old", bn.get_ancestors("R")
    def test(self):
        nor = NoisyOR("or", ["v1", "v2", "v3", "v4"])
        #        for i in range(16):
        #            print entry(i, 4)
        nor.put([True, True, True, True], 0.65)
        nor.put([False, True, True, True], 0.19)
        nor.put([True, False, True, True], 0.48)
        nor.put([False, False, True, True], 0.53)
        nor.put([True, True, False, True], 0.98)
        nor.put([False, True, False, True], 0.68)
        nor.put([True, False, False, True], 0.57)
        nor.put([False, False, False, True], 0.90)
        nor.put([True, True, True, False], 0.43)
        nor.put([False, True, True, False], 0.39)
        nor.put([True, False, True, False], 0.71)
        nor.put([False, False, True, False], 0.34)
        nor.put([True, True, False, False], 0.01)
        nor.put([False, True, False, False], 0.06)
        nor.put([True, False, False, False], 0.92)
        nor.put([False, False, False, False], 0.36)

        v1 = CPT("v1", [])
        v1.put([], 0.54)
        v2 = CPT("v2", [])
        v2.put([], 0.46)
        v3 = CPT("v3", [])
        v3.put([], 0.05)
        v4 = CPT("v4", [])
        v4.put([], 0.64)

        y = GDT("y", ["or"])
        y.put(Gaussian(0.51, 0.5), [True])
        y.put(Gaussian(0.61, .1), [False])

        #        for i in [v1,v2,v3,v4,y, nor]:
        #            print i
        bn = BNet()
        bn.add(nor)
        bn.add(v1)
        bn.add(v2)
        bn.add(v3)
        bn.add(v4)
        bn.add(y)

        test = bn.infer_naive(["or"])
        test.normalize()
        print test

        test = bn.infer_naive(["v1"])
        test.normalize()
        print test

        test = bn.infer_naive(["v2"])
        test.normalize()
        print test

        test = bn.infer_naive(["v3"])
        test.normalize()
        print test

        test = bn.infer_naive(["v4"])
        test.normalize()
        print test
Пример #25
0
 def setUp(self):
     self.cpt = CPT()
    def testrelevant(self):
        print "\n****Testing getting relevant network****"
        print "Getting relevant network for node J in expanded network"
        
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        
        print self.bn.get_relevant("J").nodes.keys()
    def testadvanced2(self):
        print "\n****Testing inference larger network****"
        print "Setting alarm = False & burglary = True, inferring [Z,A,E]"
        
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        
        self.alarm.instance = False
        self.burglary.instance = True
        
        test = self.bn.infer_naive(["Z", "A", "E"])
        test.normalize()
        print test
    def test_GDT_advanced(self):
        print "\n****Testing large network inferring GDT****"
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)
        
        G  = GDT("G", ["X"])
        G.put(Gaussian(0.24, 0.5), [True])
        G.put(Gaussian(0.62, .1), [False])

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        self.bn.add(G)
#        print self.bn.get_relevant("G").nodes.keys()
#        print G
        test = self.bn.infer_naive("G")
        print test
    def testadvanced3(self):
        print "\n****Testing inference larger network****"
        print "Setting alarm = False & Y = True, inferring C"
        
        X = CPT("X", ["M"])
        X.put([True], .89)
        X.put([False], .56)

        Y = CPT("Y", ["A", "B"])
        Y.put([True, False], .23)
        Y.put([False, True], .74)
        Y.put([False, False], .57)
        Y.put([True, True], .12)
#
        Z = CPT("Z", ["X", "Y"])
        Z.put([True, True], 0.23)
        Z.put([False, False], 0.41)
        Z.put([True, False], 0.16)
        Z.put([False, True], 0.75)
        
        C = CPT("C", ["B"])
        C.put([True], .36)
        C.put([False], .67)

        self.bn.add(X)
        self.bn.add(Y)
        self.bn.add(Z)
        self.bn.add(C)
        Y.instance = True
        self.alarm.instance = False
        
        test = self.bn.infer_naive(["C"])
        test.normalize()
        print test
Пример #30
0
class Root(Tk):

    cpt = CPT()
    imgResult = None
    txtResult = None

    def __init__(self):
        super(Root, self).__init__()
        self.tabControl = ttk.Notebook(self)
        self.iconbitmap('./icon.ico')

        self.tab0 = ttk.Frame(self.tabControl)
        self.tab1 = ttk.Frame(self.tabControl)
        self.tab2 = ttk.Frame(self.tabControl)

        self.tabControl.add(self.tab1, text='Encode')
        self.tabControl.add(self.tab2, text='Decode')
        self.tabControl.add(self.tab0, text='Author')
        self.tabControl.pack(expand=1, fill="both")
        self.title("Data-hiding though Image")
        self.resizable(False, False)

        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        self.geometry("750x550+%d+%d" %
                      (screen_width / 2 - 275, screen_height / 2 - 250))

        #=== Author ===
        self.createAuthourInfos()

        #=== ENCODE ===
        self.createBrowseImage_tab1()
        self.createBrowseText_tab1()
        self.createButtonConvert_tab1()
        self.createButtonSave_tab1()

        #== DECODE ===
        self.createBrowseImage_tab2()
        self.createButtonDecode_tab2()
        self.createButtonSave_tab2()

    #====== BEGIN AUTHOR TAB ===========#
    def createAuthourInfos(self):
        self.lblName_tab0 = ttk.Label(self.tab0, text="Name:")
        self.lblName_tab0.grid(row=1, column=0, sticky=W, pady=10, padx=20)
        self.lblName1_tab0 = ttk.Label(self.tab0, text="Nguyen, Truong An")
        self.lblName1_tab0.grid(row=1, column=1, sticky=W, pady=10)

        self.lblEmail1_tab0 = ttk.Label(self.tab0, text="Email:")
        self.lblEmail1_tab0.grid(row=2, column=0, sticky=W, pady=10, padx=20)
        self.lblEmail11_tab0 = ttk.Label(
            self.tab0, text="*****@*****.**")
        self.lblEmail11_tab0.grid(row=2, column=1, sticky=W)

        self.lblEmail2_tab0 = ttk.Label(
            self.tab0, text="*****@*****.**")
        self.lblEmail2_tab0.grid(row=3, column=1, sticky=W)

        self.lblFacebook_tab0 = ttk.Label(self.tab0, text="Facebook:")
        self.lblFacebook_tab0.grid(row=4, column=0, sticky=W, pady=20)
        self.lblFacebook1_tab0 = ttk.Label(
            self.tab0, text="https://www.facebook.com/yiianguyen")
        self.lblFacebook1_tab0.grid(row=4, column=1, sticky=W)

        self.lblGithub_tab0 = ttk.Label(self.tab0, text="Github:")
        self.lblGithub_tab0.grid(row=5, column=0, sticky=W, pady=10)
        self.lblGithub1_tab0 = ttk.Label(self.tab0,
                                         text="https://github.com/yiimnta")
        self.lblGithub1_tab0.grid(row=5, column=1, sticky=W)

        self.lblAlgorithm_tab0 = ttk.Label(self.tab0, text="Algorithm:")
        self.lblAlgorithm_tab0.grid(row=6, column=0, sticky=W, pady=20)
        self.lblAlgorithm1_tab0 = ttk.Label(self.tab0, text="Chen-Pan-Tseng")
        self.lblAlgorithm1_tab0.grid(row=6, column=1, sticky=W)

    #====== END AUTHOR TAB ===========#

    #====== BEGIN ENCODE TAB ===========#
    def createBrowseImage_tab1(self):
        self.lblImage_tab1 = ttk.Label(self.tab1, text="1. Select an Image")
        self.lblImage_tab1.grid(row=0, column=0, pady=15, sticky=W)
        self.btnImage_tab1 = ttk.Button(self.tab1,
                                        text="Browse",
                                        command=self.fileImageDialog_tab1)
        self.btnImage_tab1.grid(row=1, column=0)
        self.txtImage_tab1 = ttk.Entry(self.tab1, width=50, state='readonly')
        self.txtImage_tab1.grid(row=1, column=1)

    def fileImageDialog_tab1(self):
        self.filename_tab1 = filedialog.askopenfilename(
            initialdir="/",
            title="Select an Image",
            filetype=(("image files", "*.bmp;*.png;*.jpg"), ("all files",
                                                             "*.*")))
        self.txtImage_tab1.configure(state='normal')
        self.txtImage_tab1.delete(0, END)
        self.txtImage_tab1.insert(0, self.filename_tab1)
        self.createImageLeft_tab1(self.filename_tab1)
        self.txtImage_tab1.configure(state='readonly')

    def createBrowseText_tab1(self):
        self.lblText_tab1 = ttk.Label(self.tab1, text="2. Select Text file")
        self.lblText_tab1.grid(row=2, column=0, pady=15, sticky=W)
        self.btnText_tab1 = ttk.Button(self.tab1,
                                       text="Browse",
                                       command=self.selectFileText_tab1)
        self.btnText_tab1.grid(row=3, column=0)
        self.txtText_tab1 = ttk.Entry(self.tab1, width=50, state='readonly')
        self.txtText_tab1.grid(row=3, column=1)

    def selectFileText_tab1(self):
        self.textname_tab1 = filedialog.askopenfilename(
            initialdir="/",
            title="Select a File",
            filetype=(("Notepad files", "*.txt"), ("all files", "*.*")))
        self.txtText_tab1.configure(state='normal')
        self.txtText_tab1.delete(0, END)
        self.txtText_tab1.insert(0, self.textname_tab1)
        self.txtText_tab1.configure(state='readonly')

    def createImageLeft_tab1(self, filePath):
        if filePath is None or filePath == "":
            return
        self.lblInput_tab1 = ttk.Label(self.tab1, text="Input:")
        self.lblInput_tab1.grid(column=0,
                                sticky=W + E + S + N,
                                row=5,
                                columnspan=2,
                                pady=25,
                                padx=10)
        imgBinary = self.cpt.convertImageToBinaryImage(filePath)

        img = Image.fromarray(imgBinary)
        img = img.resize((290, 290))
        imgL = ImageTk.PhotoImage(img)
        self.imgLeft_tab1 = Label(self.tab1, image=imgL)
        self.imgLeft_tab1.image = imgL
        self.imgLeft_tab1.grid(column=0,
                               sticky=W,
                               row=6,
                               columnspan=2,
                               pady=4,
                               padx=10)

    def createButtonSave_tab1(self):
        self.btnSave_tab1 = ttk.Button(self.tab1,
                                       text="Save",
                                       command=self.saveFile_tab1,
                                       width=40)
        self.btnSave_tab1.grid(row=3,
                               rowspan=1,
                               column=3,
                               padx=20,
                               sticky=N + S + E + W)

    def saveFile_tab1(self):
        if self.imgResult is None:
            return

        files = [("image files", "*.bmp;*.png;*.jpg"), ('All Files', '*.*')]
        self.savePath_tab1 = filedialog.asksaveasfile(title="Save the image",
                                                      filetypes=files,
                                                      defaultextension=files)
        if self.savePath_tab1 is None:
            return
        path = self.savePath_tab1.name
        self.cpt.convertMatrixToImage(self.imgResult, path)
        messagebox.showinfo("Message Box", "Save image successfully!")

    def createButtonConvert_tab1(self):
        self.btnConvert_tab1 = ttk.Button(self.tab1,
                                          text="Convert",
                                          command=self.convert_tab1)
        self.btnConvert_tab1.grid(row=1,
                                  rowspan=2,
                                  column=3,
                                  padx=20,
                                  sticky=N + S + E + W)

    def convert_tab1(self):
        if self.filename_tab1 is None or self.filename_tab1 == "":
            messagebox.showerror("Error", "Please select an image")
            return
        if self.textname_tab1 is None or self.textname_tab1 == "":
            messagebox.showerror("Error", "Please select a Text file")
            return

        self.imgResult = self.cpt.runEncode(self.filename_tab1,
                                            self.textname_tab1)
        self.createImageRight_tab1()

    def createImageRight_tab1(self):
        self.lblOutput_tab1 = ttk.Label(self.tab1, text="Output:")
        self.lblOutput_tab1.grid(column=2,
                                 row=5,
                                 columnspan=2,
                                 pady=25,
                                 padx=10)

        img = Image.fromarray(self.imgResult)
        img = img.resize((290, 290))
        imgL = ImageTk.PhotoImage(img)
        self.imgRight_tab1 = Label(self.tab1, image=imgL)
        self.imgRight_tab1.image = imgL
        self.imgRight_tab1.grid(column=2, row=6, columnspan=2, pady=5, padx=10)
        messagebox.showinfo("Message Box", "Convert successfully!")

    # ====== END ENCODE TAB ===========#

    # ====== BEGIN DECODE TAB ===========#

    def createBrowseImage_tab2(self):
        self.lblImage_tab2 = ttk.Label(self.tab2, text="1. Select an Image")
        self.lblImage_tab2.grid(row=0, column=0, pady=15, sticky=W)
        self.btnImage_tab2 = ttk.Button(self.tab2,
                                        text="Browse",
                                        command=self.fileImageDialog_tab2)
        self.btnImage_tab2.grid(row=1, column=0)
        self.txtImage_tab2 = ttk.Entry(self.tab2, width=50, state='readonly')
        self.txtImage_tab2.grid(row=1, column=1)

    def fileImageDialog_tab2(self):
        self.filename_tab2 = filedialog.askopenfilename(
            initialdir="/",
            title="Select an Image",
            filetype=(("image files", "*.bmp;*.png;*.jpg;"), ("all files",
                                                              "*.*")))
        self.txtImage_tab2.configure(state='normal')
        self.txtImage_tab2.delete(0, END)
        self.txtImage_tab2.insert(0, self.filename_tab2)
        self.createImageLeft_tab2(self.filename_tab2)
        self.txtImage_tab2.configure(state='readonly')

    def createImageLeft_tab2(self, filePath):
        if filePath is None or filePath == "":
            return
        self.lblInput_tab2 = ttk.Label(self.tab2, text="Input:")
        self.lblInput_tab2.grid(column=0,
                                sticky=W + E + S + N,
                                row=2,
                                columnspan=2,
                                pady=25,
                                padx=10)
        img = Image.open(filePath)
        img = img.resize((290, 290))
        imgL = ImageTk.PhotoImage(img)
        self.imgLeft_tab2 = Label(self.tab2, image=imgL)
        self.imgLeft_tab2.image = imgL
        self.imgLeft_tab2.grid(column=0,
                               sticky=W,
                               row=3,
                               columnspan=2,
                               pady=4,
                               padx=10)

    def createButtonSave_tab2(self):
        self.btnSave_tab2 = ttk.Button(self.tab2,
                                       text="Save",
                                       command=self.saveFile_tab2)
        self.btnSave_tab2.grid(row=1,
                               rowspan=1,
                               column=4,
                               padx=20,
                               sticky=N + S + E + W)

    def saveFile_tab2(self):
        if self.txtResult is None:
            return

        files = [("Text file", "*.txt"), ('All Files', '*.*')]
        self.savePath_tab2 = filedialog.asksaveasfile(title="Save the text",
                                                      filetypes=files,
                                                      defaultextension=files)
        if self.savePath_tab2 is None:
            return
        path = self.savePath_tab2.name
        file = io.open(path, "w", encoding="utf8")
        file.write(self.txtResult)
        file.close()
        messagebox.showinfo("Message Box", "Save text file successfully!")

    def decode_tab2(self):
        if self.filename_tab2 is None or self.filename_tab2 == "":
            messagebox.showerror("Error", "Please select an image")
            return

        self.txtResult = self.cpt.runDecode(self.filename_tab2)
        self.createTextArea_tab2()

    def createButtonDecode_tab2(self):
        self.btnDecode_tab2 = ttk.Button(self.tab2,
                                         text="Decode",
                                         command=self.decode_tab2)
        self.btnDecode_tab2.grid(row=1,
                                 rowspan=1,
                                 column=3,
                                 padx=20,
                                 sticky=N + S + E + W)

    def createTextArea_tab2(self):
        if self.txtResult is None:
            messagebox.showerror("Error", "Please select an image")
            return

        self.lblOutput_tab2 = ttk.Label(self.tab2, text="Output:")
        self.lblOutput_tab2.grid(column=2,
                                 row=2,
                                 columnspan=2,
                                 pady=25,
                                 padx=10)
        self.txtarea_tab2 = tk.Text(self.tab2, height=19, width=30)
        self.txtarea_tab2.grid(row=3, column=2, columnspan=2)
        self.txtarea_tab2.delete('1.0', END)
        self.txtarea_tab2.insert(tk.END, self.txtResult)