Esempio n. 1
0
    def load_bn_from_file(self, file_name):
        """
        Load a bayesian network to show.
        Initially, the bn can be loaded by itself, but vertex positions must be loaded independently.
        """
        try:
            # load bayesian network
            self.disc_bn = DiscreteBayesianNetworkExt()
            self.disc_bn.load(file_name)

            ### Load Vertex locations
            json_data = dic_from_json_file(file_name)
            # Vertex locations
            if "vertex_loc" in json_data.keys():
                self.dict_to_gpoints(json_data["vertex_loc"])
            else:
                vl = ugraphic.create_vertex_locations(self.disc_bn)
                self.dict_to_gpoints(vl)

        except Exception:
            ugraphic.show_warning(self.window,
                                  "Error loading the Bayesian Network",
                                  Exception)
            return

        self._draw_mode_edit()
Esempio n. 2
0
    def __init__(self, window, disc_bn=DiscreteBayesianNetworkExt()):
        self.window = window

        # Create graphic widgets
        self.box_disc_bn, self.drawing_box, self.toolbar_edit, self.toolbar_evidence, self.bedit, self.brun, self.bvertex, \
            self.bclear_evidence = ugraphic.create_widget(
                res.TAB_DISC_BAYES_NET_GLADE,
                [BOX_DISC_BN, DRAWING_BOX, TB_EDIT_BN, TB_EVIDENCE, RB_EDIT, RB_RUN, RB_VERTEX, RB_CLEAR_EVIDENCE], self)

        super(BoxDiscreteBN, self).__init__(spacing=1)
        self.pack_start(self.box_disc_bn, True, True, 0)

        self.set_visible(True)

        self.drawer = GraphDrawer()
        self.drawing_box.pack_start(self.drawer.get_drawing_area(), True, True,
                                    0)

        self.mode = Mode.edit_vertex

        # Temporal vertex for edge
        self.vertex_1 = None
        self.selected_vertex = None
        self.selected_edge = None
        self.marginals = None
        self.evidences = {}

        # Temporal arrow for mouse motion
        self.tmp_arrow = None

        # Graph
        self.disc_bn = disc_bn

        # Vertex locations to draw
        self.vertex_locations = {}

        self.clicked_point = None
        self.button_pressed = False

        # connect listeners
        self.drawer.clicked_element_listener = self.clicked_element
        self.drawer.clicked_clear_space_listener = self.clicked_clear_space
        self.drawer.right_click_elem_listener = self.right_clicked_elem
        self.drawer.double_clicked_element_listener = self.double_click_on_elem

        # Graphical objects in the background
        self.background_gobjs = []
Esempio n. 3
0
    def do_activate(self):
        # Graph
        vertices = ["A", "B", "C", "D"]
        edges = [["A", "B"], ["C", "B"], ["D", "B"]]

        states = {
            "A": ["atrue", "afalse"],
            "B": ["true", "false"],
            "C": ["ctrue", "cfalse"],
            "D": ["1", "2", "3"]
        }

        cpt_v = "D"
        disc_bn = DiscreteBayesianNetworkExt()
        disc_bn.set_vertices(vertices)
        disc_bn.E = edges

        # States
        for s, iss in states.items():
            for i in iss:
                disc_bn.add_state(s, i)

        gtable = gwidgets.GraphicCptTable(disc_bn, cpt_v)
        # table = gwidgets.create_treeview_for_cpt(vertices, edges, states, cpt_v)

        ## Window
        window = Gtk.Window(application=self, title="CPT for " + cpt_v)
        window.set_default_size(250, 100)
        window.set_border_width(10)

        # the label we use to show the selection
        label = Gtk.Label()
        label.set_text("fads")

        # a grid to attach the widgets
        grid = Gtk.Grid()
        grid.attach(gtable.view, 0, 0, 1, 1)
        grid.attach(label, 0, 1, 1, 1)

        # attach the grid to the window
        window.add(grid)
        window.show_all()

        if window:
            window.connect("destroy", Gtk.main_quit)
Esempio n. 4
0
from gi.repository import Gtk, GObject
import random

from lib_sallybn.disc_bayes_net.DiscreteBayesianNetworkExt import DiscreteBayesianNetworkExt
from lib_sallybn.disc_bayes_net.BoxDiscreteBN import BoxDiscreteBN


# Create window
window = Gtk.Window()
window.set_size_request(800, 600)

## Discrete bn
disc_bn = DiscreteBayesianNetworkExt()
# Vertex and its states
disc_bn.add_vertex("A", states=["true1", "false1"])
disc_bn.add_vertex("C", states=["true", "false"])
disc_bn.add_vertex("B", states=["true", "false"])

# Edges
disc_bn.add_edge(["A", "B"])
disc_bn.add_edge(["C", "B"])

# CPTs
cprob_b = {
    "['true1', 'true']": [.3, .7],
    "['true1', 'false']": [.9, .1],
    "['false1', 'true']": [.05, .95],
    "['false1', 'false']": [.5, .5]
}
cprob_a = [.3, .7]
cprob_c = [.2, .8]