Ejemplo n.º 1
0
    def write_graphs_to_dots(self):
        assert self.build_graph
        self._load_packages()

        from pygraph.readwrite import dot

        base = self.output_dir

        with open(join(base, 'digraph.dot'), 'w') as f:
            data = dot.write(self.digraph)
            f.write(data)

        with open(join(base, 'bfs.dot'), 'w') as f:
            (st, order) = breadth_first_search(self.digraph)
            bfs = digraph()
            bfs.add_spanning_tree(st)
            data = dot.write(bfs)
            f.write(data)

        with open(join(base, 'dfs.dot'), 'w') as f:
            (st, pre, post) = depth_first_search(self.digraph)
            dfs = digraph()
            dfs.add_spanning_tree(st)
            data = dot.write(dfs)
            f.write(data)
Ejemplo n.º 2
0
    def write_graphs_to_dots(self):
        assert self.build_graph
        self._load_packages()

        from pygraph.readwrite import dot

        base = self.output_dir

        with open(join(base, 'digraph.dot'), 'w') as f:
            data = dot.write(self.digraph)
            f.write(data)

        with open(join(base, 'bfs.dot'), 'w') as f:
            (st, order) = breadth_first_search(self.digraph)
            bfs = digraph()
            bfs.add_spanning_tree(st)
            data = dot.write(bfs)
            f.write(data)

        with open(join(base, 'dfs.dot'), 'w') as f:
            (st, pre, post) = depth_first_search(self.digraph)
            dfs = digraph()
            dfs.add_spanning_tree(st)
            data = dot.write(dfs)
            f.write(data)
Ejemplo n.º 3
0
    def handle(self, **options):
        gr = graph()

        cats_by_id = dict((c.id, c) for c in Category.objects.all())

        # Add nodes
        dups = count()
        for c in cats_by_id.itervalues():
            try:
                gr.add_node(c)
            except AdditionError:
                dups.next()
                parent = cats_by_id.get(c.parent_id)
                print 'WARNING: duplicate node :: <Category %i | %s>' % (c.id,
                                                                         c)
                print '\twith parent ' + '<Category %i | %s>' % (
                    parent.id, parent) if parent else 'None'

        if dups.next() > 0: return

        # Add edges
        # gr.add_edge((CONCRETE_NODE, ROOT_NODE))
        for c in cats_by_id.itervalues():
            parent = cats_by_id.get(c.parent_id)
            if parent:
                gr.add_edge((c, parent))

        # import ipdb; ipdb.set_trace()
        # The whole tree from the root
        st, order = breadth_first_search(
            gr, root=Category.objects.get(title="Abstract"))
        gst = digraph()
        gst.add_spanning_tree(st)

        dot = write(gst)
        gvv = gv.readstring(dot)

        gv.layout(gvv, 'dot')
        gv.render(gvv, 'pdf', os.path.join(output_dir, 'abstract.pdf'))

        st, order = breadth_first_search(
            gr, root=Category.objects.get(title="Concrete"))
        gst = digraph()
        gst.add_spanning_tree(st)

        dot = write(gst)
        gvv = gv.readstring(dot)

        gv.layout(gvv, 'dot')
        gv.render(gvv, 'pdf', os.path.join(output_dir, 'concrete.pdf'))
Ejemplo n.º 4
0
    def _build_graph(self):
        """Build a graph representing the communication cost within that
        machine.

        The cost of communication for each pair of cores is the
        send_cost + receive_cost on that link. This is set as edge
        weight and used by the schedulers when sorting
        edges.

        """

        _c_list = self.get_cores()

        # Add all cores to the graph
        gr = digraph()
        gr.add_nodes(_c_list)

        for snd in _c_list:
            for rcv in _c_list:
                if snd!=rcv:
                    snd_cost = self.get_send_cost(snd, rcv)
                    rcv_cost = self.get_receive_cost(snd, rcv)
                    gr.add_edge((snd, rcv), rcv_cost + snd_cost)

        return gr
Ejemplo n.º 5
0
 def test_complete_digraph(self):
     gr = digraph()
     gr.add_nodes(range(10))
     gr.complete()
     for i in range(10):
         for j in range(10):
             self.assertTrue((i, j) in gr.edges() or i == j)
Ejemplo n.º 6
0
def author_centrality(titles_to_authors):
    """
    Identifies the centrality of an author

    :param titles_to_authors: a dict keying title strings to the authors associated
    :type titles_to_authors: dict

    :return: a dict matching author to centrality
    :rtype: dict
    """
    author_graph = digraph()
    author_graph.add_nodes(map(lambda x: u"title_%s" % x, titles_to_authors.keys()))
    author_graph.add_nodes(list(set([u'author_%s' % author[u'user']
                                     for authors in titles_to_authors.values()
                                     for author in authors])))

    for title in titles_to_authors:
        for author in titles_to_authors[title]:
            try:
                author_graph.add_edge((u'title_%s' % title, u'author_%s' % author[u'user']))
            except AdditionError:
                pass

    centralities = dict([('_'.join(item[0].split('_')[1:]), item[1])
                         for item in pagerank(author_graph).items() if item[0].startswith(u'author_')])

    centrality_scaler = MinMaxScaler(centralities.values())

    return dict([(cent_author, centrality_scaler.scale(cent_val))
                 for cent_author, cent_val in centralities.items()])
    def __init__(self, robot, viewer = False):
        self.robot = robot
        self.env = robot.GetEnv ()
        self.grasping_locations_cache = {}
        self.viewMode = viewer
        self.tray_stack = []
        self.unMovableObjects = {self.env.GetKinBody('table')}
        self.objSequenceInPlan = []
        self.handled_objs = set()

        self.object_mover = ObjectMover(self.env, use_ros, self.unMovableObjects)

        self.obstruction_digraph = digraph()
        
        v = self.robot.GetActiveDOFValues()
        v[self.robot.GetJoint('torso_lift_joint').GetDOFIndex()]=1.0
        self.robot.SetDOFValues(v)      

        if use_ros:
            self.pr2robot = PR2Robot(self.env)
            self.pr2robot.robot = self.robot
            self.arm_mover = PR2MoveArm()
        
        #loading the IK models
        utils.pr2_tuck_arm(robot)
        robot.SetActiveManipulator('leftarm')
        ikmodel = openravepy.databases.inversekinematics.InverseKinematicsModel(
                        robot,iktype=openravepy.IkParameterization.Type.Transform6D)    
        if not ikmodel.load():
            ikmodel.autogenerate()            
        robot.SetActiveManipulator('rightarm')
        ikmodel = openravepy.databases.inversekinematics.InverseKinematicsModel(
            robot,iktype=openravepy.IkParameterization.Type.Transform6D)    
        if not ikmodel.load():
            ikmodel.autogenerate()
Ejemplo n.º 8
0
def pygraphCrit(edgePairs, nameMap, timeMap, allEdts, allEvts, mainGuid):
    G = digraph()
    srcs = []
    dsts = []
    for i in range(len(edgePairs)):
        srcs.append(edgePairs[i][0])
        dsts.append(edgePairs[i][1])

    allNodes = set(srcs + dsts)
    for i in allNodes:
        if i == mainGuid:
            continue
        else:
            G.add_node(str(i))
    for i in range(len(edgePairs)):
        curTime = 0
        curSrc = str(edgePairs[i][0])
        curDst = str(edgePairs[i][1])
        if curSrc == mainGuid or curDst == mainGuid:
            continue
        if getNodeType(curSrc, allEdts, allEvts) == 'Event':
            curTime = 1
        else:
            curTime = edtGuidToTime(curSrc, timeMap)
        G.add_edge((curSrc, curDst), curTime)

    if len(critical_path(G)) == 0:
        print 'Cycle Detected, exiting; Please check that the OCR conifiguration file uses GUID type of COUNTED_MAP, and the application has no cycles.'
        print 'Dumping cycle below.'
        print find_cycle(G)
        os.remove(HTML_FILE_NAME)
        sys.exit(0)

    return critical_path(G)
Ejemplo n.º 9
0
    def condorcet_completion_method(self):

        # Initialize the candidate graph
        self.rounds = []
        graph = digraph()
        graph.add_nodes(self.candidates)

        # Loop until we've considered all possible pairs
        remaining_strong_pairs = deepcopy(self.strong_pairs)
        while len(remaining_strong_pairs) > 0:
            r = {}

            # Find the strongest pair
            largest_strength = max(remaining_strong_pairs.values())
            strongest_pairs = matching_keys(remaining_strong_pairs, largest_strength)
            if len(strongest_pairs) > 1:
                r["tied_pairs"] = strongest_pairs
                strongest_pair = self.break_ties(strongest_pairs)
            else:
                strongest_pair = list(strongest_pairs)[0]
            r["pair"] = strongest_pair

            # If the pair would add a cycle, skip it
            graph.add_edge(strongest_pair)
            if len(find_cycle(graph)) > 0:
                r["action"] = "skipped"
                graph.del_edge(strongest_pair)
            else:
                r["action"] = "added"
            del remaining_strong_pairs[strongest_pair]
            self.rounds.append(r)

        self.old_graph = self.graph
        self.graph = graph
        self.graph_winner()
Ejemplo n.º 10
0
    def _build_tree(self, g):
        """
        We build a bad tree by inverting all edge weights and running
        an MST algorithm on the resulting graph.

        """
        # Build graph with inverted edges
        g_inv = algorithms.invert_weights(g)

        # Run binary tree algorithm
        mst_inv = minimal_spanning_tree(g_inv, self.get_root_node())

        # Build a new graph
        badtree = digraph()

        # Add nodes
        for n in g.nodes():
            badtree.add_node(n)

        # Add edges, copy weights from non-inverted graph
        for (e,s) in mst_inv.items():
            if s != None:
                badtree.add_edge((s, e), g.edge_weight((s, e)))

        return badtree
Ejemplo n.º 11
0
 def test_add_spanning_tree(self):
     gr = digraph()
     st = {0: None, 1: 0, 2: 0, 3: 1, 4: 2, 5: 3}
     gr.add_spanning_tree(st)
     for each in st:
         self.assertTrue((st[each], each) in gr.edges()
                         or (each, st[each]) == (0, None))
Ejemplo n.º 12
0
def create_dtg_graph(sas):
    # TODO -- create DTGs graph
    gr = digraph()

    for var in sas.variables:
        gr.add_node(var.get_label())

    for cg in sas.causal_graphs:
        if cg.variable.is_always and not INCLUDE_ALWAYS:
            '''skip "always" variable'''
        elif cg.variable.isderived() and not INCLUDE_DERIVED_VARIABLE:
            ''' skip '''
        else:
            '''if not cg.variable.isderived():
                print(str(cg.variable.index) + ": " + cg.variable.name + " => " + str(cg.variable.isderived()))'''
            for causal in cg.causals:
                if causal[0].is_always and not INCLUDE_ALWAYS:
                    ''' skip '''
                elif causal[0].isderived() and not INCLUDE_DERIVED_VARIABLE:
                    ''' skip '''
                else:
                    #gr.add_edge((causal[0].get_label(), cg.variable.get_label()))
                    gr.add_edge(
                        (cg.variable.get_label(), causal[0].get_label()))

    return gr
Ejemplo n.º 13
0
    def drawGraph(self, inputs):

        fileName = 'planetModel.png'
        gr = graph()
        self.addGraphNode(1, gr, inputs)
        # Draw as PNG
        #with open("./planetModel.viz", 'wb') as f:
        #dot = write(gr,f)
        #f.write(dot)
        #gvv = gv.readstring(dot)
        #gv.layout(gvv,'dot')
        #gv.render(gvv,'png', fileName)
        #f.close()

        gst = digraph()
        self.addGraphNode(1, gst, inputs)
        with open("./planetModel.viz", 'wb') as f:
            #st, order = breadth_first_search(gst, root=1)
            #gst2 = digraph()
            #gst2.add_spanning_tree(gst.nodes())
            #gst2.(1, 'post')
            dot = write(gst, f)
            f.write(dot)
            gvv = gv.readstring(dot)
            gv.layout(gvv, 'dot')
            gv.render(gvv, 'png', fileName)
            f.close()

        return fileName
Ejemplo n.º 14
0
    def __init__(self, **kwargs):  # pylint:disable=super-init-not-called
        """Initialization."""
        self.graph = digraph()
        self.nodestatus = {}
        self.nodeexceptions = {}
        self.noderesults = {}
        self._run_phase = 0
        self._retry = {}  # retries left, sleep time, etc
        self._default_tries = None  # num of tries for Jobs that set no value
        self._default_delay = None  # default delay between Job retries
        self._default_retry_delay = None  # min delay between retries of a Job

        self._args = []  # Args for Check/Run methods
        self._kwargs = {}  # KWArgs for Check/Run methods
        self._root = None  # Root Job
        self._checknrun_cwd = None
        self._checknrun_storage = None
        self._classmap = {}  # map of name: actual_class_obj
        self._cleanup = True  # Should we automatically do a cleanup
        self._verbose = False
        self._debug = False
        self._no_act = False
        self._deps = {}
        self._objsrun = []  # which objects ran, for triggering Cleanup

        self._log = logging.getLogger('DoJobber')
        logtarget = logging.StreamHandler()
        logtarget.setFormatter(
            logging.Formatter('DoJobber %(levelname)-19s: %(message)s')
        )
        if kwargs.get('dojobber_loglevel'):
            self._log.setLevel(kwargs['dojobber_loglevel'])
        else:
            self._log.setLevel(logging.CRITICAL)
        self._log.addHandler(logtarget)
Ejemplo n.º 15
0
    def test_digraph_equality_attributes(self):
        """
        Digraph equality test. This one checks node equality. 
        """
        gr = digraph()
        gr.add_nodes([0, 1, 2])
        gr.add_edge((0, 1))
        gr.add_node_attribute(1, ('a', 'x'))
        gr.add_node_attribute(2, ('b', 'y'))
        gr.add_edge_attribute((0, 1), ('c', 'z'))

        gr2 = deepcopy(gr)

        gr3 = deepcopy(gr)
        gr3.del_edge((0, 1))
        gr3.add_edge((0, 1))

        gr4 = deepcopy(gr)
        gr4.del_edge((0, 1))
        gr4.add_edge((0, 1))
        gr4.add_edge_attribute((0, 1), ('d', 'k'))

        gr5 = deepcopy(gr)
        gr5.del_node(2)
        gr5.add_node(2)
        gr5.add_node_attribute(0, ('d', 'k'))

        assert gr == gr2
        assert gr2 == gr
        assert gr != gr3
        assert gr3 != gr
        assert gr != gr4
        assert gr4 != gr
        assert gr != gr5
        assert gr5 != gr
Ejemplo n.º 16
0
    def test_digraph_equality_labels(self):
        """
        Digraph equality test. This one checks node equality. 
        """
        gr = digraph()
        gr.add_nodes([0, 1, 2])
        gr.add_edge((0, 1), label="l1")
        gr.add_edge((1, 2), label="l2")

        gr2 = deepcopy(gr)

        gr3 = deepcopy(gr)
        gr3.del_edge((0, 1))
        gr3.add_edge((0, 1))

        gr4 = deepcopy(gr)
        gr4.del_edge((0, 1))
        gr4.add_edge((0, 1), label="l3")

        assert gr == gr2
        assert gr2 == gr
        assert gr != gr3
        assert gr3 != gr
        assert gr != gr4
        assert gr4 != gr
Ejemplo n.º 17
0
 def test_a_b_c_d_square(self):
     depgraph = digraph()
     add_action(depgraph, "a#ta")
     add_action(depgraph, "b#tb")
     add_action(depgraph, "c#tc")
     add_action(depgraph, "d#td")
     depgraph.add_edge(("a#ta", "b#tb"))
     depgraph.add_edge(("a#ta", "d#td"))
     depgraph.add_edge(("c#tc", "b#tb"))
     depgraph.add_edge(("c#tc", "d#td"))
     (ise_model, xml, error) = self.order(depgraph)
     self.assertActionsNb(ise_model, 4)
     instructions = ise_model.instructions
     self.assertNotEquals(instructions, None)
     self.assertEquals(len(instructions), 1)
     seq = instructions.pop()
     instructions = self.assertSequence(seq, nb=2)
     par = instructions[0]
     actions = self.assertParallel(par, nb=2)
     self.assertContainsAction(actions, "b#tb/Rule")
     self.assertContainsAction(actions, "d#td/Rule")
     par = instructions[1]
     actions = self.assertParallel(par, nb=2)
     self.assertContainsAction(actions, "a#ta/Rule")
     self.assertContainsAction(actions, "c#tc/Rule")
Ejemplo n.º 18
0
    def graphize(self):
        try:
            self.graph = pygraph.digraph()
        except (NameError, AttributeError):
            self.graph = digraph()

        modules = [self.topModule] + self.moduleList
        # first, we must add all the nodes. Only then can we add all the edges
        self.graph.add_nodes(modules)

        # here we have a strictly directed graph, so we need only insert directed edges
        for module in modules:

            def checkParent(child):
                if module.name == child.parent:
                    return True
                else:
                    return False

            children = filter(checkParent, modules)
            for child in children:
                # due to compatibility issues, we need these try catch to pick the
                # right function prototype.
                try:
                    self.graph.add_edge(module, child)
                except TypeError:
                    self.graph.add_edge((module, child))
Ejemplo n.º 19
0
 def test_complete_digraph(self):
     gr = digraph()
     gr.add_nodes(range(10))
     gr.complete()
     for i in range(10):
         for j in range(10):
             self.assertTrue((i, j) in gr.edges() or i == j)
Ejemplo n.º 20
0
 def test_add_empty_digraph(self):
     gr1 = testlib.new_digraph()
     gr1c = copy(gr1)
     gr2 = digraph()
     gr1.add_graph(gr2)
     self.assertTrue(gr1.nodes() == gr1c.nodes())
     self.assertTrue(gr1.edges() == gr1c.edges())
Ejemplo n.º 21
0
def build_bayes_graph(im,labels,sigma=1e2,kappa=2):
  """  Build a graph from 4-neighborhood of pixels.
    Foreground and background is determined from
    labels (1 for foreground, -1 for background, 0 otherwise)
    and is modeled with Gaussian Naive Bayes classifiers."""

  m,n = im.shape[:2]

  # RGB vector version (one pixel per row)
  vim = im.reshape((-1,3))

  # RGB for foreground and background
  foreground = im[labels==1].reshape((-1,3))
  background = im[labels==-1].reshape((-1,3))
  train_data = [foreground,background]

  print train_data
  # train naive Bayes classifier
  bc = bayes.BayesClassifier()
  bc.train(train_data)

  # get probabilities for all pixels
  bc_lables,prob = bc.classify(vim)
  prob_fg = prob[0]
  prob_bg = prob[1]

  # create graph with m*n+2 nodes
  gr = digraph()
  gr.add_nodes(range(m*n+2))

  source = m*n # second to last is source
  sink = m*n+1 # last node is sink

  # normalize
  for i in range(vim.shape[0]):
    vim[i] = vim[i] / linalg.norm(vim[i])

  # go through all nodes and add edges
  for i in range(m*n):
    # add edge from source
    gr.add_edge((source,i), wt=(prob_fg[i]/(prob_fg[i]+prob_bg[i])))

    # add edge to sink
    gr.add_edge((i,sink), wt=(prob_bg[i]/(prob_fg[i]+prob_bg[i])))

    # add edges to neighbors
    if i%n != 0: # left exists
      edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i-1])**2)/sigma)
      gr.add_edge((i,i-1), wt=edge_wt)
    if (i+1)%n != 0: # right exists
      edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i+1])**2)/sigma)
      gr.add_edge((i,i+1), wt=edge_wt)
    if i//n != 0: # up exists
      edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i-n])**2)/sigma)
      gr.add_edge((i,i-n), wt=edge_wt)
    if i//n != m-1: # down exists
      edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i+n])**2)/sigma)
      gr.add_edge((i,i+n), wt=edge_wt)

  return gr
Ejemplo n.º 22
0
    def _parse(self, file):
        max_x = self._parse_int(file.readline())
        max_y = self._parse_int(file.readline())

        if max_x < max_y:
            raise ValueError("Map must be horizontal: max_x >= max_y")

        edge_count = self._parse_int(file.readline())

        graph = digraph()

        for i in range(0, (max_x + 1) * (max_y + 1)):
            graph.add_node(i)

        for _ in range(0, edge_count):
            edge = file.readline().split()

            if len(edge) != 4:
                raise ValueError(
                    "Edge must be represented by 4 numbers: x1 y1 x2 y2")

            x1 = self._parse_int(edge[0])
            y1 = self._parse_int(edge[1])
            x2 = self._parse_int(edge[2])
            y2 = self._parse_int(edge[3])

            graph.add_edge((y1 * (max_x + 1) + x1, y2 * (max_x + 1) + x2))

        self._specification = MapSpecification(graph, max_x, max_y)
Ejemplo n.º 23
0
    def graphizeSynth(self):
        try:
            self.graphSynth = pygraph.digraph()
        except (NameError, AttributeError):
            self.graphSynth = digraph()
        modulesUnfiltered = [self.topModule] + self.moduleList
        # first, we must add all the nodes. Only then can we add all the edges
        # filter by synthesis boundaries
        modules = filter(checkSynth, modulesUnfiltered)
        self.graphSynth.add_nodes(modules)

        # here we have a strictly directed graph, so we need only insert directed edges
        for module in modules:

            def checkParent(child):
                if module.name == child.synthParent:
                    return True
                else:
                    return False

            children = filter(checkParent, modules)
            for child in children:
                #print "Adding p: " + module.name + " c: " + child.name
                try:
                    self.graphSynth.add_edge(module, child)
                except TypeError:
                    self.graphSynth.add_edge((module, child))
Ejemplo n.º 24
0
 def drawGraph(self, inputs):
     
     fileName = 'planetModel.png'
     gr = graph()
     self.addGraphNode(1,gr, inputs)
 # Draw as PNG
     #with open("./planetModel.viz", 'wb') as f:
         #dot = write(gr,f)
         #f.write(dot)
         #gvv = gv.readstring(dot)
         #gv.layout(gvv,'dot')
         #gv.render(gvv,'png', fileName)
         #f.close()
         
     gst = digraph()
     self.addGraphNode(1,gst, inputs)            
     with open("./planetModel.viz", 'wb') as f:            
         #st, order = breadth_first_search(gst, root=1)
         #gst2 = digraph()
         #gst2.add_spanning_tree(gst.nodes())
         #gst2.(1, 'post')
         dot = write(gst,f)
         f.write(dot)
         gvv = gv.readstring(dot)
         gv.layout(gvv,'dot')
         gv.render(gvv,'png', fileName)   
         f.close()         
         
         
          
     return fileName
Ejemplo n.º 25
0
def pygraphCrit(edgePairs, nameMap, timeMap, allEdts, allEvts, mainGuid):
    G = digraph()
    srcs = []
    dsts = []
    for i in range(len(edgePairs)):
        srcs.append(edgePairs[i][0])
        dsts.append(edgePairs[i][1])

    allNodes = set(srcs+dsts)
    for i in allNodes:
        if i == mainGuid:
            continue
        else:
            G.add_node(str(i))
    for i in range(len(edgePairs)):
        curTime = 0
        curSrc = str(edgePairs[i][0])
        curDst = str(edgePairs[i][1])
        if curSrc == mainGuid or curDst == mainGuid:
            continue
        if getNodeType(curSrc, allEdts, allEvts) == 'Event':
            curTime = 1
        else:
            curTime = edtGuidToTime(curSrc, timeMap)
        G.add_edge((curSrc, curDst), curTime)

    if len(critical_path(G)) == 0:
        print 'Cycle Detected, exiting; Please check that the OCR conifiguration file uses GUID type of COUNTED_MAP, and the application has no cycles.'
        print 'Dumping cycle below.'
        print find_cycle(G)
        os.remove(HTML_FILE_NAME)
        sys.exit(0)

    return critical_path(G)
Ejemplo n.º 26
0
def switchgraph(switchaddrs, outfile):
    gr = digraph()

    nodes = {}
    edges = {}

    for addr in switchaddrs:
        print >> sys.stderr, "Querying %s..." % addr
        try:
            sysmac = switchquery(addr, PROCURVE_OIDS["system-mac"])
        except SnmpError, err:
            print >> sys.stderr, unicode(err)
            continue
        nodes[sysmac] = addr

        cdpneigh = switchquery(addr, PROCURVE_OIDS["cdp-neigh"])
        if not cdpneigh:
            print >> sys.stderr, "%s didn't return any data!" % addr
            continue
        for port in cdpneigh[6]:
            if 1 in cdpneigh[6][port]:
                othermac = cdpneigh[6][port][1][0]
            else:
                continue
            if othermac not in nodes:
                nodes[othermac] = "%s\n%s" % (othermac,
                                              cdpneigh[8][port][1][0][:20])
            if (sysmac, othermac) not in edges and (othermac,
                                                    sysmac) not in edges:
                edges[(sysmac, othermac)] = (port, cdpneigh[7][port][1][0])
Ejemplo n.º 27
0
    def draw(self, filename):
        print "initial state:", self.initial_state
        print "final states", self.final_state
        print "transition table:", self.transition_table

        vertexes = self.transition_table.keys()
        edges = self._edges()

        gr = digraph()
        #gr.add_nodes([str(vertex) for vertex in vertexes])
        for vertex in vertexes:
            attrs = []

            if ((isinstance(self.final_state, list)
                 and vertex in self.final_state)
                    or (isinstance(self.final_state, int)
                        and vertex == self.final_state)):
                attrs.append('final')
            gr.add_node(str(vertex), attrs=attrs)

        for edge, label in edges.items():
            label = ', '.join(label)
            gr.add_edge(edge=edge, label=label)

        dot = write(gr)
        gvv = gv.readstring(dot)
        gv.layout(gvv, 'dot')
        gv.render(gvv, 'png', '%s.png' % filename)
Ejemplo n.º 28
0
 def test_a_b_c_d_square(self):
     depgraph = digraph()
     add_action(depgraph, "a#ta")
     add_action(depgraph, "b#tb")
     add_action(depgraph, "c#tc")
     add_action(depgraph, "d#td")
     depgraph.add_edge(("a#ta", "b#tb"))
     depgraph.add_edge(("a#ta", "d#td"))
     depgraph.add_edge(("c#tc", "b#tb"))
     depgraph.add_edge(("c#tc", "d#td"))
     (ise_model, xml, error) = self.order(depgraph)
     self.assertActionsNb(ise_model, 4)
     instructions = ise_model.instructions
     self.assertNotEquals(instructions, None)
     self.assertEquals(len(instructions), 1)
     par = instructions.pop()
     actions = self.assertParallel(par, nb=4)
     a = self.assertContainsAction(actions, "a#ta/Rule")
     b = self.assertContainsAction(actions, "b#tb/Rule")
     c = self.assertContainsAction(actions, "c#tc/Rule")
     d = self.assertContainsAction(actions, "d#td/Rule")
     self.assertAction(a, id="a#ta/Rule", deps=set(['b#tb/Rule', 'd#td/Rule']))
     self.assertAction(b, id="b#tb/Rule", deps=set())
     self.assertAction(c, id="c#tc/Rule", deps=set(['b#tb/Rule', 'd#td/Rule']))
     self.assertAction(d, id="d#td/Rule", deps=set())
Ejemplo n.º 29
0
def main(argv):

    try:
        opts, args = getopt.getopt(argv, '')
    except getopt.GetoptError:
        print('test.py <dir> <dir>')
        sys.exit(2)

    assert (len(args) == 2)
    dirs = args
    graphs = [
        printDot(dirs[0] + '/graph.txt', '0'),
        printDot(dirs[1] + '/graph.txt', '0')
    ]

    types = [
        parseTypes(dirs[0] + '/types.txt'),
        parseTypes(dirs[1] + '/types.txt')
    ]

    newG = digraph()
    startN = ('0', '0')
    newG.add_node(startN)
    oneToOne(newG, graphs, types, startN)
    printGraph(newG, ('0', '0'))
Ejemplo n.º 30
0
    def __init__(self, var_lines):

        try:
            from pygraph.classes.digraph import digraph
        except:
            print(
                "Error: pygraph not available. Advanced SAS+ reasoning will not work."
            )
            return None

        # Parse the info
        assert 'begin_variable' == var_lines.pop(0)

        self.name = var_lines.pop(0)
        self.axiom_layer = int(var_lines.pop(0))
        domain_size = int(var_lines.pop(0))

        # Create the graph
        #  Node: int corresponding to the value of the SAS+ variable
        #  Edge: label that indicates the operators that affect it
        self.graph = digraph()
        self.graph.name = self.name
        self.transition_lookup = {}

        # Add a node for each value in the domain
        for i in range(domain_size):
            self.graph.add_node(i, [('fluent', var_lines.pop(0))])

        assert 'end_variable' == var_lines.pop(0)

        # Create the accepting states
        self.goal_vals = set([])
        self.init_val = 0
Ejemplo n.º 31
0
 def parse_generated_world(self, index, lines, data):
     """ Parse the generated world and store its data
         into the created data structure.
     """
     # Skip the line ======== Generated World =========
     index += 1
     vertices = []
     gr = digraph()
     node_lines = []
     edges = []
     cur = ""
     while index < len(lines) and "Weight" not in lines[index]:
         line = lines[index].strip()
         if "<-" in line:
             words = line.split()
             node = words[1]
             for line in node_lines:
                 if node in line:
                     e = (line, cur)
                     if e not in edges:
                         #print "adding edge " + str(e)
                         gr.add_edge(e)
                         edges.append(e)
                         break
         elif "=" in line:
             #print "adding node: " + str(line)
             gr.add_nodes([line])
             node_lines.append(line)
             cur = line
         index += 1
     return gr, index
def handle_recursive_calls(tpl_name, content):
    # create the call graph as a directed graph
    call_graph = digraph()

    # visited_templates items will look like this:
    # [("tpl1", "extends", "tpl2"), ...]
    visited_templates = [(tpl_name, "", "")]

    call_graph.add_node(tpl_name)
    i = 0
    while i < len(visited_templates):
        name = visited_templates[i][0]
        try:
            tpl_content = content if i == 0 else Template.objects.get(name=name).content
        except:
            i += 1
            continue

        called_tpls = get_called_templates(tpl_content, name)
        update_call_graph(call_graph, called_tpls)

        # raises InfiniteRecursivityError in case of a cycle
        cycle_test(call_graph, called_tpls)

        visited_templates.extend(called_tpls)
        i += 1
Ejemplo n.º 33
0
	def run(self):
		#print "save graph in dir"
		gr = digraph()
		for x in self.nodes:
			gr.add_node(x.name)
		
		for x in self.nodes:
			x.addEdges(gr)
		
		dirLck.acquire()
		if not os.path.isdir(self.dir):
			if -1 == os.getcwd().find(self.dir):
				os.mkdir(self.dir)
		dirLck.release()
			
		if -1 == os.getcwd().find(self.dir):
			os.chdir(self.dir)

		dot = write(gr)
		f = open(self.filename, 'w')
		f.write(dot)
		f.close()

		proc = subprocess.Popen('dot -Tjpg ' + self.filename +' -o ' +  self.filename + '.jpg', shell=True, stdout=subprocess.PIPE,)
		r = proc.communicate()[0]
		print ""
Ejemplo n.º 34
0
def merge_graphs(g1, g2):
    """
    Merge two graphs to a new graph (V, E) with V = g1.nodes \union g2.nodes
    and Edge e \in g1 or e \in g2 -> e \in E.
    """

    if g1.DIRECTED or g2.DIRECTED:
        g = digraph()
    else:
        g = graph()

    for n in g1.nodes():
        g.add_node(n)
    for n in g2.nodes():
        if not n in g.nodes():
            g.add_node(n)
    for e in g1.edges():
        try:
            g.add_edge(e, g1.edge_weight(e))
        except:
            logging.info("merge_graphs: adding edge %d %d failed" % (e[0], e[1]))
    for e in g2.edges():
        try:
            g.add_edge(e, g2.edge_weight(e))
        except:
            logging.info("merge_graphs: adding edge %d %d failed" % (e[0], e[1]))
    return g
Ejemplo n.º 35
0
    def calculate_results(self):

        # Don't bother if everyone's going to win
        super(SchulzeSTV, self).calculate_results()
        if hasattr(self, 'winners'):
            return

        # Generate the list of patterns we need to complete
        self.generate_completed_patterns()
        self.generate_vote_management_graph()

        # Build the graph of possible winners
        self.graph = digraph()
        for candidate_set in itertools.combinations(self.candidates, self.required_winners):
            self.graph.add_nodes([tuple(sorted(list(candidate_set)))])

        # Generate the edges between nodes
        for candidate_set in itertools.combinations(self.candidates, self.required_winners + 1):
            for candidate in candidate_set:
                other_candidates = sorted(set(candidate_set) - set([candidate]))
                completed = self.proportional_completion(candidate, other_candidates)
                weight = self.strength_of_vote_management(completed)
                if weight > 0:
                    for subset in itertools.combinations(other_candidates, len(other_candidates) - 1):
                        self.graph.add_edge((tuple(other_candidates), tuple(sorted(list(subset) + [candidate]))), weight)

        # Determine the winner through the Schwartz set heuristic
        self.graph_winner()

        # Split the "winner" into its candidate components
        self.winners = set(self.winner)
        del self.winner
Ejemplo n.º 36
0
def connect_graphs(g1, g2, connecting_edge, weight=1):
    """
    Build a new graph out of the two given graphs.

    Every node e in g1 will be represented as 1_e in the new graph and
    every node e' in g2 as 2_e'.

    @param connecting_edge: An edge (e_src, e_dst), where e_src is in
        g1 and e_dst is in g2. This edge will connect g1 with g2.
    @param weight: Weight of the connecting edge.
    """
    g = digraph()

    # Add nodes and edges
    for (index, gr) in [(1, g1), (2, g2)]:
        for n in gr.nodes():
            g.add_node('%d_%d' % (index, n))
        for (src, dst) in gr.edges():
            g.add_edge(('%d_%d' % (index, src),
                        '%d_%d' % (index, dst)),
                        g.edge_weight((src, dst)))

    # Connect subgraphs
    conn_src, conn_dst = connecting_edge
    g.add_edge(('%d_%d' % (1, conn_src),
                '%d_%d' % (2, conn_dst)))
    g.add_edge(('%d_%d' % (2, conn_dst),
                '%d_%d' % (1, conn_src)))

    return g
def friendly_rename(graph, name_prefix=""):
    """
    Builds a new weighted digraph, based on the provided weighted digraph (which isn't modified), 
    which discards all names in favor of alphanumeric node identifiers.
    """
    nextLetter = ord('A')
    nextNumber = 1
    identifierMap = {}
    newGraph = digraph()

    for node in graph.nodes():
        if not graph.incidents(node):
            identifier = name_prefix + chr(nextLetter)
            nextLetter += 1
        else:
            identifier = "@" + name_prefix + str(nextNumber)
            nextNumber += 1
        newGraph.add_node(identifier, graph.node_attributes(node))
        identifierMap[node] = identifier

    for edge in graph.edges():
        weight = graph.edge_weight(edge)
        label = graph.edge_label(edge)
        src = identifierMap[edge[0]]
        dest = identifierMap[edge[1]]
        new_edge = (src, dest)
        attrs = graph.edge_attributes(edge)
        newGraph.add_edge(new_edge, weight, label, attrs)

    return newGraph
Ejemplo n.º 38
0
def sequential(m, nodes, coordinator):
    """
    Construct a simple two-level tree. The coordinator is the root, and all
    the other nodes are its children.

    The weights of edges are taken from m.

    @type m: graph
    @param m: The machine model. The weights for the edges in the binary_tree
        will be extracted from the model.

    @type nodes: list
    @param nodes: list of nodes to build a tree for. If list is
        empty, it will default to m.nodes()

    @type coordinator: number
    @param coordinator: This node will be the root of the tree

    @return digraph
    """
    assert(len(m.nodes())>0) # graph has nodes

    g = digraph()
    g.add_node(coordinator)

    for n in nodes:
        if n != coordinator:
            g.add_node(n)
            g.add_edge((coordinator, n), \
                           m.edge_weight((n, coordinator)))
    return g
Ejemplo n.º 39
0
 def test_add_empty_digraph(self):
     gr1 = testlib.new_digraph()
     gr1c = copy(gr1)
     gr2 = digraph()
     gr1.add_graph(gr2)
     self.assertTrue(gr1.nodes() == gr1c.nodes())
     self.assertTrue(gr1.edges() == gr1c.edges())
Ejemplo n.º 40
0
def invert_weights(g):
    """
    Invert the weights of the given graph.

    The most expensive links will then be the cheapest and vice versa.
    """

    assert isinstance(g, digraph)

    # Determine the most expensive edge
    w = 0
    for (s,d) in g.edges():
        w = max(w, g.edge_weight((s,d)))
    print 'Maximum edge weight is %d' % w
    w += 1 # Make sure the most expensive edge will have a cost of 1 afterwards
    g_inv = digraph()
    for n in g.nodes():
        g_inv.add_node(n)
    for (s,d) in g.edges():
        assert g.edge_weight((s,d))<w
        w_inv = w-g.edge_weight((s,d))
        assert w_inv>0
        try:
            g_inv.add_edge((s,d), w_inv)
        except:
            assert g_inv.edge_weight((s,d)) == w_inv # This one fails
            print "Edge %d %d already in graph, ignoring .. " % (s,d)
    return g_inv
Ejemplo n.º 41
0
def author_centrality(titles_to_authors):
    author_graph = digraph()
    author_graph.add_nodes(map(lambda x: u"title_%s" % x,
                               titles_to_authors.keys()))
    author_graph.add_nodes(list(set(
        [u'author_%s' % author[u'user'] for authors in
         titles_to_authors.values() for author in authors])))

    for title in titles_to_authors:
        log.debug(u"Working on title: %s" % title)
        for author in titles_to_authors[title]:
            try:
                author_graph.add_edge(
                    (u'title_%s' % title, u'author_%s' % author[u'user']))
            except AdditionError:
                pass

    centralities = dict([
        ('_'.join(item[0].split('_')[1:]), item[1]) for item in
        pagerank(author_graph).items() if item[0].startswith(u'author_')])

    centrality_scaler = MinMaxScaler(centralities.values())

    return dict([(cent_author, centrality_scaler.scale(cent_val))
                 for cent_author, cent_val in centralities.items()])
Ejemplo n.º 42
0
    def referergraph(self, refererlist, drawpath):
        """
        Draw the graph of referers
        """

        gr = digraph()
        attr = list([('style', 'filled'), ('fillcolor', 'green'), ('shape', 'doublecircle')])

        #first add all referer
        for ref in refererlist:

            try:
                gr.add_node(ref.altname, attr)
            except pygraph.classes.exceptions.AdditionError as e:
                print e

            print "=========================="

        #now add the url that have the referer previously added
        for ref in refererlist:
            for url in ref.getlist:
                print url
                try:
                    gr.add_node(url[0])
                except pygraph.classes.exceptions.AdditionError as e:
                    print "From url: " + str(e)

                try:
                    gr.add_edge((ref.altname, url[0]),1,url[1])
                except pygraph.classes.exceptions.AdditionError as e:
                    print "Edge addition: " + str(e)

        return self.export(gr, drawpath, "referer")
Ejemplo n.º 43
0
 def parse_generated_world(self, index, lines, data):
     """ Parse the generated world and store its data
         into the created data structure.
     """
     # Skip the line ======== Generated World =========
     index += 1
     vertices = []
     gr = digraph()
     node_lines = []
     edges = []
     cur = ""
     while index < len(lines) and "Weight" not in lines[index]:
         line = lines[index].strip()
         if "<-" in line:
             words = line.split()
             node = words[1]
             for line in node_lines:
                 if node in line:
                     e = (line, cur)
                     if e not in edges:
                         #print "adding edge " + str(e)
                         gr.add_edge(e)
                         edges.append(e)
                         break
         elif "=" in line:
             #print "adding node: " + str(line)
             gr.add_nodes([line])
             node_lines.append(line)
             cur = line
         index += 1
     return gr, index
def system_of_different_constraints():
    """
    x1 - x2 <= 0
    x1 - x5 <= -1
    x2 - x5 <= 1
    x3 - x1 <= 5
    x4 - x1 <= 4
    x4 - x3 <= -1
    x5 - x3 <= -3
    x5 - x4 <= -3
    """
    m = int(input("Enter the number of limited conditions ->"))
    n = int(input("Enter the number of variables ->"))
    g = digraph()
    for i in range(0, n + 1):
        node = 'x' + str(i)
        g.add_node(node)
        if i > 0:
            g.add_edge(('x0', node), 0)
    print(g.nodes())
    print(g.edges())

    print("Enter the conditions: ")
    for i in range(m):
        print("The %d-th condition:" % (i + 1))
        v1 = input("First variable: ")
        v2 = input("Second variable: ")
        b = int(input("Biase: "))
        g.add_edge((v2, v1), b)
    print(g.edges())
    previous, dist = bellman_fold_notCLRS(g, 'x0')
    print(previous, dist)
    """
Ejemplo n.º 45
0
def init_graph():
	global g
	if(g):
		return g
	
	g = digraph()
	deps = []
	for cls, lst in manifest.declarations.iteritems():
		for declaration_id, dec in lst.iteritems():
			log.debug('adding node %r' % declaration_id)
			g.add_node(declaration_id)
			reqs = getattr(dec, 'require', [])
			if not(isinstance(reqs, (list, tuple))):
				reqs = [reqs]
			d = []
			for req in reqs:
				d.append([req.declaration_id, declaration_id])
			if(d):
				deps.append(d)
	
	for dep in deps:
		log.debug('adding edge %r' % dep)
		g.add_edge(*dep)
	
	return g
Ejemplo n.º 46
0
def fix_orphan_nodes(commit_graph, release):
    new_graph = digraph()
    new_graph.add_nodes(commit_graph.nodes())
    [new_graph.add_edge(edge) for edge in commit_graph.edges()]
    orphan_nodes = [node for node in new_graph.nodes() if not new_graph.incidents(node)]
    [new_graph.add_edge((release, node)) for node in orphan_nodes if node != release]
    return new_graph
Ejemplo n.º 47
0
 def test_pagerank(self):
     #Test example from wikipedia: http://en.wikipedia.org/wiki/File:Linkstruct3.svg
     G = digraph()
     G.add_nodes([1, 2, 3, 4, 5, 6, 7])        
     G.add_edge((1, 2))
     G.add_edge((1, 3))
     G.add_edge((1, 4))
     G.add_edge((1, 5))
     G.add_edge((1, 7))
     G.add_edge((2, 1))
     G.add_edge((3, 1))
     G.add_edge((3, 2))
     G.add_edge((4, 2))
     G.add_edge((4, 3))
     G.add_edge((4, 5))
     G.add_edge((5, 1))
     G.add_edge((5, 3))
     G.add_edge((5, 4))
     G.add_edge((5, 6))
     G.add_edge((6, 1))
     G.add_edge((6, 5))
     G.add_edge((7, 5))
     expected_pagerank = {
         1: 0.280, 
         2: 0.159,
         3: 0.139,
         4: 0.108,
         5: 0.184,
         6: 0.061,
         7: 0.069,
     }
     pr = pagerank(G)
     for k in pr:
         self.assertAlmostEqual(pr[k], expected_pagerank[k], places=3)
Ejemplo n.º 48
0
  def graphizeSynth(self):
    try:
      self.graphSynth = pygraph.digraph()
    except (NameError, AttributeError):
      self.graphSynth = digraph()
    modulesUnfiltered = [self.topModule] + self.moduleList
    # first, we must add all the nodes. Only then can we add all the edges
    # filter by synthesis boundaries
    modules = filter(checkSynth, modulesUnfiltered)
    self.graphSynth.add_nodes(modules)

    # here we have a strictly directed graph, so we need only insert directed edges
    for module in modules:
      def checkParent(child):
        if module.name == child.synthParent:
          return True
        else:
          return False

      children = filter(checkParent, modules)
      for child in children:
        #print "Adding p: " + module.name + " c: " + child.name
        try:
          self.graphSynth.add_edge(module,child) 
        except TypeError:
          self.graphSynth.add_edge((module,child)) 
Ejemplo n.º 49
0
def create_object_graph(objects):
    # create dict to map objectname to file object
    mapped_objects = {}
    for o in objects:
        mapped_objects[o.get_object_name()] = o
    object_graph = digraph()
    object_graph.add_nodes([o.get_object_name() for o in objects])
    # Create relationship list
    successors = [(i.get_object_name(), [] if i.get_successors() is None else [mapped_objects[j] for j in i.get_successors()] ) for i in objects]
    #
    for obj, suc in successors:
        for s in suc:
            object_graph.add_edge((obj, s.get_object_name()))

    object_names = [o.get_object_name() for o in objects]
    for o in objects:
        # Bind objects to previous release
        predecessors = o.get_predecessors()
        if predecessors is not None:
            for p in predecessors:
                if p not in object_names:
                    if not object_graph.has_node(p):
                        object_graph.add_node(p)
                        object_graph.add_edge((p,o.get_object_name()))

    return object_graph
Ejemplo n.º 50
0
    def createCircularDiGraph(self, polygon_tup, rates):
        """
			Takes a polygon tuple and creates a circular directed graph
			of the tuple.

			returns the directed graph
		"""
        # Creating a digraph - directed graph - from rates dictionary
        #rates_digraph = convertDictToDiGraph(rates)
        dg = digraph()
        dg.add_nodes(polygon_tup)
        for i in range(len(polygon_tup)):
            if i < len(polygon_tup) - 1:
                #polygon_tup[i]
                weight = rates[polygon_tup[i]][polygon_tup[i + 1]]
                dg.add_edge((polygon_tup[i], polygon_tup[i + 1]),
                            wt=float(weight),
                            label=str(weight))
            elif i == len(polygon_tup) - 1:
                weight = rates[polygon_tup[-1]][polygon_tup[0]]
                dg.add_edge((polygon_tup[-1], polygon_tup[0]),
                            wt=float(weight),
                            label=str(weight))

        #self.createImageFromDiGraph(dg, 'best_polygon_solution_digraph.png')

        return dg
Ejemplo n.º 51
0
  def graphize(self):
    try:
      self.graph = pygraph.digraph()
    except (NameError, AttributeError):
      self.graph = digraph()   

    modules = [self.topModule] + self.moduleList
    # first, we must add all the nodes. Only then can we add all the edges
    self.graph.add_nodes(modules)

    # here we have a strictly directed graph, so we need only insert directed edges
    for module in modules:
      def checkParent(child):
        if module.name == child.parent:
          return True
        else:
          return False

      children = filter(checkParent, modules)
      for child in children:
        # due to compatibility issues, we need these try catch to pick the 
        # right function prototype.
        try:
          self.graph.add_edge(module,child) 
        except TypeError:
          self.graph.add_edge((module,child)) 
Ejemplo n.º 52
0
    def __init__(self, name='', preamble='', postamble=''):
        """Initializes SDF graph class
        """
        # SDF related attributes
        self.graph = digraph()  # graph representation
        self.name = name  # name of sdf function
        self.preamble = preamble  # C-code for preamble
        self.postamble = postamble  # C-code for postamble
        self.no_code = False  # Error state for parsing
        self.index_ctr = 0

        # node related attributes
        self.node_index = {}  # index of node to order parallel nodes properly
        self.node_type = {}  # node type generic or splitter/joiner
        self.node_param = {}  # parameters of actors
        self.repetition = {}  # repetition vector
        self.actor_code = {}  # code of actor for actor firing
        self.comm_code = {}
        self.state = {}  # state variables of actors
        self.init = {}  # initialization for state variables
        self.exec_time = {
        }  # execution times of a single invocation for each actors
        self.allocation = {}

        # edge related attributes
        self.consumption = {}  # consumption rate
        self.production = {}  # production rate
        self.delay = {}  # number of delay tokens (i.e., init tokens)
        self.token_type = {}  # data type of token
        self.source_tokens = {}  # tokens for producer
        self.target_tokens = {}  # tokens for consumer
        self.peek_tokens = {}  # tokens for peeking
        self.delay_tokens = {}  # delay tokens on the edge
Ejemplo n.º 53
0
    def test_par_a_b_n_actions(self):
        depgraph = digraph()
        na = random.randint(5, 10)
        actions = []
        for i in range(0, na):
            actions.append(("Rule"+str(i), "Cmd"+str(i)))

        add_action(depgraph, "a#ta", actions)

        nb = random.randint(5, 10)
        actions = []
        for i in range(0, nb):
            actions.append(("Rule"+str(i), "Cmd"+str(i)))

        add_action(depgraph, "b#tb", actions)
        (ise_model, xml, error) = self.order(depgraph)
        self.assertActionsNb(ise_model, na+nb)
        instructions = ise_model.instructions
        self.assertNotEquals(instructions, None)
        self.assertEquals(len(instructions), 1)
        par = instructions.pop()
        seqs = self.assertParallel(par, 2)
        seqa = self.assertSequence(seqs[0], na)
        for i in range(0, na):
            self.assertAction(seqa[i], id="a#ta/Rule"+str(i),
                              cs="a#ta", cmd="Cmd"+str(i))

        seqb = self.assertSequence(seqs[1], nb)

        for i in range(0, nb):
            self.assertAction(seqb[i], id="b#tb/Rule"+str(i),
                              cs="b#tb", cmd="Cmd"+str(i))
Ejemplo n.º 54
0
    def test_seq_a_b_n_actions(self):
        depgraph = digraph()
        na = random.randint(5, 10)
        actions = []
        for i in range(0, na):
            actions.append(("Rule"+str(i), "Cmd"+str(i)))

        add_action(depgraph, "a#ta", actions)

        nb = random.randint(5, 10)
        actions = []
        for i in range(0, nb):
            actions.append(("Rule"+str(i), "Cmd"+str(i)))

        add_action(depgraph, "b#tb", actions)
        depgraph.add_edge(("a#ta", "b#tb"))

        (ise_model, xml, error) = self.order(depgraph)
        self.assertActionsNb(ise_model, na+nb)
        instructions = ise_model.instructions
        self.assertNotEquals(instructions, None)
        self.assertEquals(len(instructions), 1)
        seq = instructions.pop()
        actions = self.assertSequence(seq, na+nb)
        for i in range(0, nb):
            self.assertAction(actions[i], id="b#tb/Rule"+str(i),
                              cs="b#tb", cmd="Cmd"+str(i))

        for i in range(0, na):
            self.assertAction(actions[nb+i], id="a#ta/Rule"+str(i),
                              cs="a#ta", cmd="Cmd"+str(i))
Ejemplo n.º 55
0
    def calculate_results(self):

        # Don't bother if everyone's going to win
        super(SchulzeSTV, self).calculate_results()
        if hasattr(self, 'winners'):
            return

        # Generate the list of patterns we need to complete
        self.generate_completed_patterns()
        self.generate_vote_management_graph()

        # Build the graph of possible winners
        self.graph = digraph()
        for candidate_set in itertools.combinations(self.candidates, self.required_winners):
            self.graph.add_nodes([tuple(sorted(list(candidate_set)))])

        # Generate the edges between nodes
        for candidate_set in itertools.combinations(self.candidates, self.required_winners + 1):
            for candidate in candidate_set:
                other_candidates = sorted(set(candidate_set) - set([candidate]))
                completed = self.proportional_completion(candidate, other_candidates)
                weight = self.strength_of_vote_management(completed)
                if weight > 0:
                    for subset in itertools.combinations(other_candidates, len(other_candidates) - 1):
                        self.graph.add_edge((tuple(other_candidates), tuple(sorted(list(subset) + [candidate]))), weight)

        # Determine the winner through the Schwartz set heuristic
        self.graph_winner()

        # Split the "winner" into its candidate components
        self.winners = set(self.winner)
        del self.winner
Ejemplo n.º 56
0
def build_bayes_graph(im, labels, sigma=1e2, kappa=2):
    ### """从像素四邻域建立一个图,前景和背景(前景用 1 标记,背景用 -1 标记,
    ### 其他的用 0 标记)由 labels 决定,并用朴素贝叶斯分类器建模 """

    m, n = im.shape[:2]

    # 每行是一个像素的 RGB 向量
    vim = im.reshape((-1, 3))

    # 前景和背景(RGB)
    foreground = im[labels == 1].reshape((-1, 3))
    background = im[labels == -1].reshape((-1, 3))
    train_data = [foreground, background]

    # 训练朴素贝叶斯分类器
    bc = bayes.BayesClassifier()
    bc.train(train_data)

    # 获取所有像素的概率
    bc_lables, prob = bc.classify(vim)
    prob_fg = prob[0]
    prob_bg = prob[1]

    # 用m*n+2 个节点创建图
    gr = digraph()
    gr.add_nodes(range(m * n + 2))
    source = m * n  # 倒数第二个是源点
    sink = m * n + 1  # 最后一个节点是汇点

    # 归一化
    for i in range(vim.shape[0]):
        vim[i] = vim[i] / linalg.norm(vim[i])

    # 遍历所有的节点,并添加边
    for i in range(m * n):
        # 从源点添加边
        gr.add_edge((source, i), wt=(prob_fg[i] / (prob_fg[i] + prob_bg[i])))
        # 向汇点添加边
        gr.add_edge((i, sink), wt=(prob_bg[i] / (prob_fg[i] + prob_bg[i])))

    # 向相邻节点添加边
    if i % n != 0:  #左边存在
        edge_wt = kappa * np.exp(-1.0 * np.sum(
            (vim[i] - vim[i - 1])**2) / sigma)
        gr.add_edge((i, i - 1), wt=edge_wt)
    if (i + 1) % n != 0:  # 如果右边存在
        edge_wt = kappa * np.exp(-1.0 * np.sum(
            (vim[i] - vim[i + 1])**2) / sigma)
        gr.add_edge((i, i + 1), wt=edge_wt)
    if i // n != 0:  #如果上方存在
        edge_wt = kappa * np.exp(-1.0 * np.sum(
            (vim[i] - vim[i - n])**2) / sigma)
        gr.add_edge((i, i - n), wt=edge_wt)
    if i // n != m - 1:  # 如果下方存在
        edge_wt = kappa * np.exp(-1.0 * np.sum(
            (vim[i] - vim[i + n])**2) / sigma)
        gr.add_edge((i, i + n), wt=edge_wt)

    return gr
def build_bayes_graph(im,labels,sigma=1e2,kappa=1):
    """    Build a graph from 4-neighborhood of pixels. 
        Foreground and background is determined from
        labels (1 for foreground, -1 for background, 0 otherwise) 
        and is modeled with naive Bayes classifiers."""
    
    m,n = im.shape[:2]
    
    # RGB vector version (one pixel per row)
    vim = im.reshape((-1,3))
    
    # RGB for foreground and background
    foreground = im[labels==1].reshape((-1,3))
    background = im[labels==-1].reshape((-1,3))    
    train_data = [foreground,background]
    
    # train naive Bayes classifier
    bc = bayes.BayesClassifier()
    bc.train(train_data)

    # get probabilities for all pixels
    bc_lables,prob = bc.classify(vim)
    prob_fg = prob[0]
    prob_bg = prob[1]
    
    # create graph with m*n+2 nodes
    gr = digraph()
    gr.add_nodes(range(m*n+2))
    
    source = m*n # second to last is source
    sink = m*n+1 # last node is sink

    # normalize
    for i in range(vim.shape[0]):
        vim[i] = vim[i] / (linalg.norm(vim[i]) + 1e-9)
    
    # go through all nodes and add edges
    for i in range(m*n):
        # add edge from source
        gr.add_edge((source,i), wt=(prob_fg[i]/(prob_fg[i]+prob_bg[i])))
        
        # add edge to sink
        gr.add_edge((i,sink), wt=(prob_bg[i]/(prob_fg[i]+prob_bg[i])))
        
        # add edges to neighbors
        if i%n != 0: # left exists
            edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i-1])**2)/sigma)
            gr.add_edge((i,i-1), wt=edge_wt)
        if (i+1)%n != 0: # right exists
            edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i+1])**2)/sigma)
            gr.add_edge((i,i+1), wt=edge_wt)
        if i//n != 0: # up exists
            edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i-n])**2)/sigma)
            gr.add_edge((i,i-n), wt=edge_wt)
        if i//n != m-1: # down exists
            edge_wt = kappa*exp(-1.0*sum((vim[i]-vim[i+n])**2)/sigma)
            gr.add_edge((i,i+n), wt=edge_wt)
        
    return gr    
Ejemplo n.º 58
0
    def calculate_results(self):

        remaining_candidates = self.candidates.copy()
        self.order = []
        self.rounds = []

        if self.winner_threshold is None:
            winner_threshold = len(self.candidates)
        else:
            winner_threshold = min(len(self.candidates),
                                   self.winner_threshold + 1)

        for self.required_winners in range(1, winner_threshold):

            # Generate the list of patterns we need to complete
            self.generate_completed_patterns()
            self.generate_vote_management_graph()

            # Generate the edges between nodes
            self.graph = digraph()
            self.graph.add_nodes(remaining_candidates)
            self.winners = set([])
            self.tied_winners = set([])

            # Generate the edges between nodes
            for candidate_from in remaining_candidates:
                other_candidates = sorted(
                    list(remaining_candidates - set([candidate_from])))
                for candidate_to in other_candidates:
                    completed = self.proportional_completion(
                        candidate_from,
                        set([candidate_to]) | set(self.order))
                    weight = self.strength_of_vote_management(completed)
                    if weight > 0:
                        self.graph.add_edge((candidate_to, candidate_from),
                                            weight)

            # Determine the round winner through the Schwartz set heuristic
            self.schwartz_set_heuristic()

            # Extract the winner and adjust the remaining candidates list
            self.order.append(self.winner)
            round = {"winner": self.winner}
            if len(self.tied_winners) > 0:
                round["tied_winners"] = self.tied_winners
            self.rounds.append(round)
            remaining_candidates -= set([self.winner])
            del self.winner
            del self.actions
            if hasattr(self, "tied_winners"):
                del self.tied_winners

        # Attach the last candidate as the sole winner if necessary
        if self.winner_threshold is None or self.winner_threshold == len(
                self.candidates):
            self.rounds.append({"winner": list(remaining_candidates)[0]})
            self.order.append(list(remaining_candidates)[0])

        del self.winner_threshold
Ejemplo n.º 59
0
 def _get_instrs_graph(self):
     #initialize graph
     igraph = digraph()
     igraph.add_nodes(self.code.itervalues())
     for ins in self.code.itervalues():
         for suc in ins.succ:
             igraph.add_edge((ins, self.code[suc]))
     return igraph