Exemple #1
0
def _ve_demo(asia):
    from Tkinter import Tk, Label, Frame, Button
    from gPy.Utils import scrolled_frame
    for cpt in asia.copy(True):
        cpt *= 1
    root = Tk()
    top=scrolled_frame(root,yscroll=8000, height=40000) 
    windows = (Frame(top),Frame(top))
    orders =(('VisitAsia', 'Tuberculosis', 'XRay',  'Dyspnea', 'Bronchitis', 
              'Smoking', 'TbOrCa'),
             ('TbOrCa', 'VisitAsia', 'Tuberculosis', 'XRay', 'Bronchitis', 
              'Smoking', 'Dyspnea'))
    asias = (asia.copy(True),asia.copy(True))
    for i in range(2):
        model, window, order = asias[i], windows[i], orders[i]
        window.pack()
        for variable in order:
            cpm = model.copy()
            step = model.eliminate_variable(variable,trace=True)
            prod_factor, message, hyperedges = step
            colours = {}
            for hyperedge in hyperedges:
                colours[hyperedge] = 'blue'
            Label(window,text = 'Eliminating '+ variable).pack() 
            cpm.gui_display(window,colours)
            if message is not None:
                fr = Frame(window)
                fr.pack()
                model[message].gui_main(fr,edit=False,bg='red').pack()
                Button(fr,text='Remove message',command=fr.destroy).pack()
    root.mainloop()
Exemple #2
0
def general_grahams_demo(hg,width=400,height=300,
                 scrolled_width=1000,scrolled_height=40000,
                 xscroll=1000,yscroll=1000,**config):
    """Demo of Graham's algorithm"""
    from Tkinter import Tk
    from gPy.Utils import scrolled_frame
    root=scrolled_frame(Tk(),width=scrolled_width,height=scrolled_height,
                        xscroll=xscroll,yscroll=yscroll)
    hg.gui_grahams_algorithm(root,width=width,height=height,**config)
    root.mainloop()
Exemple #3
0
def join_forest_demo():
    """Display 2 join forests for Asia for 2 different variable orderings"""
    from Tkinter import Toplevel, Label
    from Utils import scrolled_frame, pretty_str_set
    from Hypergraphs import JoinForest
    top=scrolled_frame(Toplevel(),yscroll=500, height=400)
    bottom=scrolled_frame(Toplevel(),yscroll=500, height=400)
    as2 = asia.copy(True)
    for cpt in as2:
        cpt *= 1
    as2.gui_display(top)
    order1 = ('VisitAsia', 'Tuberculosis', 'XRay',  'Dyspnea', 'Bronchitis', 
              'Smoking', 'TbOrCa', 'Cancer')
    order2 = ('TbOrCa', 'VisitAsia', 'Tuberculosis', 'XRay', 'Bronchitis', 
              'Smoking', 'Dyspnea', 'Cancer')
    for order in (order1, order2):
        ac = as2.copy()
        jf = JoinForest(ac.hypergraph().make_decomposable2(order)[0])
        jf._uforest.gui_display(bottom,pp_vertex=pretty_str_set,width=600,height=300)
        Label(bottom,text=order).pack()
    top.mainloop()
    bottom.mainloop()
Exemple #4
0
def cond_demo():
    """Show conditioning by deleting instantiations"""
    from Tkinter import Tk
    from gPy.Utils import scrolled_frame
    as2 = asia.copy(True)
    for cpt in as2:
        cpt *= 1
    root = Tk()
    top=scrolled_frame(root,yscroll=5000, height=40000)
    as2.gui_display(top)
    as2.condition({'Smoking':['smoker']})
    as2.gui_display(top)
    root.mainloop()
Exemple #5
0
def calibration_demo(width=400,height=300,
                 scrolled_width=40000,scrolled_height=40000,
                 xscroll=2000,yscroll=1000,**config):
    """Demo of join tree calibration"""
    from gPy.Models import JFR
    from Tkinter import Tk
    from gPy.Utils import scrolled_frame
    root=scrolled_frame(Tk(),width=scrolled_width,height=scrolled_height,
                        xscroll=xscroll,yscroll=yscroll)
    jfr = JFR(asia.copy(),modify=True,
              elimination_order=['VisitAsia','Tuberculosis','Smoking','Cancer',
                                 'TbOrCa','XRay','Bronchitis','Dyspnea'])
    jfr.gui_calibrate(root)
    root.mainloop()
Exemple #6
0
def asia_cpts():
    """Shows Asia BN: CPTs, DAG, CPTs as factors and moral graph"""
    from Tkinter import Tk
    from Utils import scrolled_frame
    root=Tk()
    top=scrolled_frame(root,yscroll=5000, height=4000)
    as2 = asia.copy(True)
    as2.gui_display(top)
    as2.adg().gui_display(top,width=400,height=300)
    for factor in as2:
        factor *= 1
    as2.gui_display(top)
    as2.adg().moralise().gui_display(top,width=400,height=300)
    root.mainloop()
Exemple #7
0
def cluster_tree():
    """Displays cluster trees for 2 elimination orders for Asia BN"""
    from Tkinter import Toplevel, Label
    from Utils import scrolled_frame, pretty_str_set
    top=scrolled_frame(Toplevel(),yscroll=500, height=400)
    bottom=Toplevel()
    for cpt in asia:
        cpt *= 1
    asia.gui_display(top)
    order1 = ('VisitAsia', 'Tuberculosis', 'XRay',  'Dyspnea', 'Bronchitis', 
              'Smoking', 'TbOrCa')
    order2 = ('TbOrCa', 'VisitAsia', 'Tuberculosis', 'XRay', 'Bronchitis', 
              'Smoking', 'Dyspnea')
    for order in (order1, order2):
        ac = asia.copy(True)
        cf = ac.variable_elimination_trace(order)
        cf.gui_display(bottom,pp_vertex=pretty_str_set,width=600,height=300)
        Label(bottom,text=order).pack()
    top.mainloop()
    bottom.mainloop()
Exemple #8
0
def _op_gui(lhs_f,rhs_f,op,op_str,title,root):
    """Generate a GUI to demonstrate e.g. multiplication of factors

    @param lhs_f: Factor on LHS of operation
    @type lhs_f: L{Parameters.Factor}
    @param rhs_f: Factor on RHS of operation
    @type rhs_f: L{Parameters.Factor}
    @param op: The binary operation to apply to the factors (e.g. muliplication)
    @type op: Binary operator
    @param op_str: Textual representation of C{op}
    @type op_str: String
    @param title: Title for demo
    @type title: String
    @param root: Parent widget
    @type root: Suitable Tkinter object, e.g. C{Tk}, C{Tkinter.Frame}
    """
    f1, f2 = lhs_f*1, rhs_f*1
    root.title('Factor %s' % title)

    
    data = Tkinter.Frame(scrolled_frame(root))
    data.pack()
    prod_variables = f1.variables() | f2.variables()
    f1exp = f1.copy().broadcast(prod_variables)
    f2exp = f2.copy().broadcast(prod_variables)
    result = op(f1,f2)
    factors = [(f1,'',f2,'',''),
               ('=','','=','',''),
               (f1exp,op_str,f2exp,'=',result)]
    for r in range(3):
        for c in range(5):
            if r==1 or c == 1 or c==3 or (c==4 and r == 0):
                widget = Tkinter.Label(data,text=factors[r][c])
                widget.grid(row=r,column=c)
            else:
                widget = factors[r][c].gui_main(data, False, False)
                widget.grid(row=r,column=c,sticky=Tkinter.NW)
    button = Tkinter.Button(root,text='Done',command=root.destroy)
    button.bind('<Return>', lambda event: root.destroy())
    button.grid()
Exemple #9
0
def max_card_search_demo(width=400,height=300,
                 scrolled_width=1000,scrolled_height=40000,
                 xscroll=1000,yscroll=1000,**config):
    """Demo of maximum cardinality search on the Asia moral graph and on
    L{tarjan1}."""
    from Tkinter import Tk
    root1 = Tk()
    from gPy.Utils import scrolled_frame
    root=scrolled_frame(root1,width=scrolled_width,height=scrolled_height,
                        xscroll=xscroll,yscroll=yscroll)
    root1.title("Maximum cardinality search demonstration")
    asia.adg().moralise().gui_maximum_cardinality_search(root,
                                                         width=width,
                                                         height=height,
                                                         **config)
    def m(set):
        x = max(set)
        set.remove(x)
        return x
    tarjan1.gui_maximum_cardinality_search(root,m,
                                           width=width,
                                           height=height,
                                           **config)
    root.mainloop()
Exemple #10
0
from gPy.Examples import asia
from Tkinter import *
from gPy.Utils import scrolled_frame
for cpt in asia:
    cpt *= 1
root = Tk()
top = scrolled_frame(root, yscroll=500, height=40000)
asia.gui_display(top)
asia.condition({'Smoking': ['smoker']})
asia.gui_display(top)
root.mainloop()
Exemple #11
0
from gPy.Examples import asia
from Tkinter import *
from gPy.Utils import scrolled_frame, pretty_str_set
from gPy.Structures import JoinForest
top=scrolled_frame(Toplevel(),yscroll=500, height=400)
bottom=Toplevel()
for cpt in asia:
    cpt *= 1
asia.gui_display(top)
order1 = ('VisitAsia', 'Tuberculosis', 'XRay',  'Dyspnea', 'Bronchitis', 
          'Smoking', 'TbOrCa', 'Cancer')
order2 = ('TbOrCa', 'VisitAsia', 'Tuberculosis', 'XRay', 'Bronchitis', 
          'Smoking', 'Dyspnea', 'Cancer')
for order in (order1, order2):
    ac = asia.copy()
    jf = JoinForest(ac.hypergraph().make_decomposable(order)[0])
    jf._uforest.gui_display(bottom,pp_vertex=pretty_str_set,width=600,height=300)
    Label(bottom,text=order).pack()
top.mainloop()
bottom.mainloop()
Exemple #12
0
from gPy.Examples import asia
from Tkinter import *
from gPy.Utils import scrolled_frame
for cpt in asia:
    cpt *= 1
asia.condition({'Bronchitis':['absent'],'XRay':['normal']})
root = Tk()
top=scrolled_frame(root,yscroll=500, height=40000) 
windows = (Frame(top),Frame(top))
orders =(('VisitAsia', 'Tuberculosis', 'XRay',  'Dyspnea', 'Bronchitis', 
          'Smoking', 'TbOrCa'),
         ('TbOrCa', 'VisitAsia', 'Tuberculosis', 'XRay', 'Bronchitis', 
          'Smoking', 'Dyspnea'))
asias = (asia,asia.copy())
for i in range(2):
    model, window, order = asias[i], windows[i], orders[i]
    window.pack()
    model.gui_display(window)
    for variable in order:
        model.eliminate_variable(variable)
        Label(window,text = 'Eliminating '+ variable).pack() 
        model.gui_display(window)
root.mainloop()