Example #1
0
def graphFromList(edge_list, node_prefix='', directed=True):
    """Creates a basic graph out of an edge list.
    
    The edge list has to be a list of tuples representing
    the nodes connected by the edge.
    The values can be anything: bool, int, float, str.
    
    If the graph is undirected by default, it is only
    calculated from one of the symmetric halves of the matrix.

    If the graph has weighted edges, edge_list will be of the 
    form:
        [ (vert1, vert2, weight), ... ]
    else:
        [ (vert1, vert2), ... ]
    
    """
    weighted = len(edge_list[0]) % 2
    if directed:
            graph = Dot(graph_type='digraph')
    else:
            graph = Dot(graph_type='graph')
    for edge in edge_list:
            e = Edge(node_prefix+str(edge[0]), node_prefix+str(edge[1]))
            if weighted:
                e.label = e.weight = edge[2]
            graph.add_edge(e)
    return graph
Example #2
0
    def write(self, filename, format="png", append_ext=False, prog="dot"):
        "Builds a pydot graph and writes to file"
        from pydot import Dot, Edge, Node

        dot = Dot(graph_type='digraph', **self.graph_attributes)
        if format not in dot.formats:
            raise GraphException("invalid format '%s'" % format)
        node_cache = {}

        def get_node(s):
            try:
                return node_cache[s]
            except KeyError:
                node = Node(s, **self.node_attributes.get(s, {}))
                dot.add_node(node)
                node_cache[s] = node
                return node

        for (s0, s1), attr in self.edge_attributes.iteritems():
            dot.add_edge(Edge(get_node(s0), get_node(s1),
                            **self.edge_attributes.get((s0, s1), {})))

        for node in self.get_orphaned_nodes():
            get_node(node)

        if append_ext:
            filename = "%s.%s" % (filename, format)
        dot.write(filename, format=format, prog=prog)
def buildDependencyControlGraph(dependencies,sizes=[1,2],productionTarget=1.25):
    from pydot import Dot, Node, Edge, Cluster
    
    dgraph = Dot(graph_type='graph',fontname='Verdana',splines="line",maxiter="500")
    #splines="line",
    nodes = {}
    edges = {}
    
    for (controlMap,pvalue) in dependencies:
        targetNames = []
        size = len(controlMap)
        if size not in sizes:
            continue
        for (targetName,cvalue) in controlMap.items():
            #targetTag = "%s(%s)" % (targetName,cvalue)
            targetTag = targetName
            targetNames.append(targetTag)
            if size not in nodes.keys():
                nodes[size] = set()
            nodes[size].add((targetName,cvalue))            
        edgeCombos = combinations(targetNames,2)
        for value in edgeCombos:
            key = list(value)
            key.sort()
            (e1,e2) = key
            if (e1,e2) not in edges.keys() and (e2,e1) not in edges.keys():
                edge = Edge(e1,e2)
                edges[(e1,e2)] = edge
            else:
                #print "dup key %s %s" % (e1,e2)
                pass
            
    nodes = cleanNodeNames(nodes)
    
    for (key,nodeValues) in nodes.items():
        if key == 1:
            ishape = "rectangle"
        elif key == 2:
            ishape = "oval"
        elif key == 3:
            ishape = "hexagon"
        else:
            ishape = "circle"
        
        for (name,value) in nodeValues:
            icolor = "black"
            if value > 0:
                icolor = "red"
            if value < 0:
                icolor = "green"
            targetTag = "%s(%s)" % (name,value)
            targetTag = name       
            dgraph.add_node(Node(targetTag,shape=ishape,color=icolor))
            #print "Node: [%s] : size[%s] value[%s] shape[%s]" % (name,key,value,ishape)
        
    for (key,edge) in edges.items():
        dgraph.add_edge(edge)
    
    return dgraph 
Example #4
0
    def write(self, f_name):
        """write the DOT file to *fName*

        """
        for node in self.node_list:
            node.sync()
        Dot.write(self, f_name)
        # FixQuoteBug(fName, float(self.net_desc['link_attr']['delay']))
        FixQuoteBug(f_name)
Example #5
0
    def handle(self, filename=None, **options):
        try:
            from pydot import Dot, Edge, Node
        except ImportError:
            raise CommandError("need pydot python module ( apt-get install python-pydot )")

        graph = Dot()

        for status, description in STATUS_CHOICES:
            graph.add_node(Node(
                'status-%s' % status,
                label='"%s (%s)"' %
                    (description.encode('utf-8'), status))
            )

        from sadiki.core.workflow import workflow
        for transition_index in workflow.available_transitions():
            transition = workflow.get_transition_by_index(transition_index)
            graph.add_edge(Edge(
                'status-%s'% transition.src,
                'status-%s' % transition.dst,
                label='"%s (%s)"' % (transition.comment.encode('utf-8'), transition.index),
                style='solid' if transition.required_permissions else 'dashed',
            ))

        if filename:
            graph.write_png(filename)
        else:
            print graph.to_string()
class PydotAstWalker(object):

    def __init__(self, ast_root, course_code, valid_expr=True):
        self.course_code = course_code
        self.valid_expr = valid_expr
        self.ast = ast_root
        self.graph = Dot(graph_type='digraph')

    def _name_node(self, node):
        if node.is_course():
            # what if not leaf node?
            return node.code
        elif node.is_operator():
            name = str(node)
            for child in node.children:
                name += self._name_node(child)
            return name

    def generate_graph(self):
        course_root_node = Node(self.course_code, style="filled", colorscheme="set35", fillcolor="5")
        if not self.ast:
            if not self.valid_expr:
                return Node(self.course_code, style="filled", colorscheme="set35", fillcolor="4")
            else:
                return course_root_node
        edges = set()
        root_operator_node = self._visit_node(self.ast, edges)
        for edge in edges:
            self.graph.add_edge(edge.to_pydot_edge())
        self.graph.add_node(course_root_node)
        self.graph.add_edge(Edge(course_root_node, root_operator_node))
        return self.graph

    def _visit_node(self, ast_node, edges):
        if ast_node.is_course():
            node = Node(ast_node.code, style="filled", colorscheme="set35", fillcolor="2")
            self.graph.add_node(node)
            if not ast_node.is_leaf():
                child = self._visit_node(ast_node.child, edges)
                edge = UniqueEdge(node, child)
                edges.add(edge)
            return node
        elif ast_node.is_operator():
            name = self._name_node(ast_node)
            if isinstance(ast_node, AllOf):
                this_node = Node(name, label=str(ast_node), shape="square", style="filled", colorscheme="set35", fillcolor="3")
            else:
                this_node = Node(name, label=str(ast_node), shape="diamond", style="filled", colorscheme="set35", fillcolor="1")
            self.graph.add_node(this_node)
            child_nodes = [self._visit_node(child, edges) for child in ast_node.children]
            for child in child_nodes:
                edge = UniqueEdge(this_node, child)
                edges.add(edge)
            return this_node
Example #7
0
    def create_graph_from_workflow(self, workflow):
        """
        :type workflow: Workflow
        :rtype: Dot
        """
        known_nodes = {}
        start = workflow.getStartNode()
        name = workflow.getName()
        g = Dot(graph_name=name, ranksep=1,
                labelloc="t", label="workflow: " + name, fontsize=18, fontcolor="blue")
        g.set_node_defaults(shape="box", fontsize=12)
        g.set_edge_defaults(fontsize=13, labeldistance=3)

        def add_node_rek(node):
            """Recurses through the workflow graph and adds nodes and edges.
            Repeated nodes are ignored (cycle detection).
            :type node: core.tree.Node
            """
            name = node.getName()
            if name in known_nodes:
                return known_nodes[name]
            dot_node = Node(name=name)
            g.add_node(dot_node)
            known_nodes[name] = dot_node
            logg.debug("created node %s", name)
            try:
                true_next_id = node.getTrueId()
            except:
                true_next_id = None
            if true_next_id:
                true_next = workflow.getStep(true_next_id)
                true_dot_next = add_node_rek(true_next)
                true_label = node.getTrueLabel() or " "
                g.add_edge(true_edge(dot_node, true_dot_next, label=true_label))
                logg.debug("created True edge: %s -> %s", name, true_next_id)
            try:
                false_next_id = node.getFalseId()
            except:
                false_next_id = None
            if false_next_id:
                false_next = workflow.getStep(false_next_id)
                false_dot_next = add_node_rek(false_next)
                false_label = node.getFalseLabel() or " "
                g.add_edge(false_edge(dot_node, false_dot_next, label=false_label))
                logg.debug("created False edge: %s -> %s", name, false_next_id)

            if not (true_next_id or false_next_id):
                # end node with no connections
                dot_node.set("color", "#04B45F")
            return dot_node
        # traverse from start node
        add_node_rek(start)
        return g
def main(infilename, outfilename):
    state_obj = yaml.load(open(infilename))
    assert(isinstance(state_obj, dict))
    assert(len(state_obj) == 1)
    state_obj = state_obj.values()[0]
    assert(isinstance(state_obj, list))
    graph = Dot("states", graph_type='digraph')

    # First, create all graph nodes
    nodes, nodemap = generate_nodes(graph, state_obj)
    # Then, generate our links
    generate_links(graph, state_obj, nodes, nodemap)
    graph.write(outfilename)
Example #9
0
class ContentTypeGraph(BaseGraph):
    """ Draw a graph for ContentType
    """

    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        # This node
        name = self.context.getId()
        node = queryAdapter(self.context, INode)
        self._graph.add_node(node())

        xtool = queryAdapter(self.context, IToolAccessor)
        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            if name == value_from:
                nto = self.tool.get(value_to)
                if (value_from != value_to) and nto:
                    node = queryAdapter(nto, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_to)
                    self._bad_relations.add(relation.Title())

            elif name == value_to:
                nfrom = self.tool.get(value_from)
                if (value_from != value_to) and nfrom:
                    node = queryAdapter(nfrom, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_from)
                    self._bad_relations.add(relation.Title())

        return self._graph
Example #10
0
class Graph(object):
	def __init__(self, sentence):
		self.dot = Dot()
		self.parse_sentence(sentence)

	def add(self, word1, word2):
		self.dot.add_edge(Edge(word1, word2))
		return word1
		
	def instantiate(self, word):
		return self.add(uniqueid(), word)
	
	def parse_sentence(self, sentence):
		for word1, word2 in pairwise(map(self.instantiate,tokenize(sentence))):
			self.add(word1, word2)
Example #11
0
def main():
    # setup.py install/develop creates an executable that calls this function
    options = parse_args(get_parser(), sys.argv[1:])
    deps = list(read_file(options.input))
    graph = Dot(
        "Dependencies",
        graph_type='digraph',
        rankdir='LR',
        label=sys.argv[1].split('.')[0],
        fontname='Impact',
        fontcolor="grey50",
        fontsize=36,
    )
    add_nodes(graph, get_tree(deps))
    add_edges(graph, deps)
    print(graph.to_string())
Example #12
0
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        xtool = queryAdapter(self.context, IToolAccessor)
        typesIds = set()
        for ctype in xtool.types(proxy=False):
            typesIds.add(ctype.getId())
            node = queryAdapter(ctype, INode)
            self._graph.add_node(node())

        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            edge = queryAdapter(relation, IEdge)()
            if edge:
                self._graph.add_edge(edge)
            else:
                self._bad_relations.add(relation.Title())
                if value_from not in typesIds:
                    self._bad_content.add(value_from)
                if value_to not in typesIds:
                    self._bad_content.add(value_to)

        return self._graph
Example #13
0
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        # Valid graph edge
        self._graph = PyGraph()

        value_from = self.context.getField('from').getAccessor(self.context)()
        nfrom = self.tool.get(value_from)
        if nfrom:
            node = queryAdapter(nfrom, INode)
            self._graph.add_node(node())
        else:
            self._bad_content.add(value_from)
            self._bad_relations.add(self.context.Title())

        value_to = self.context.getField('to').getAccessor(self.context)()
        nto = self.tool.get(value_to)
        if nto:
            if value_from != value_to:
                node = queryAdapter(nto, INode)
                self._graph.add_node(node())
        else:
            self._bad_content.add(value_to)
            self._bad_relations.add(self.context.Title())

        edge = queryAdapter(self.context, IEdge)()
        if edge:
            self._graph.add_edge(edge)

        return self._graph
Example #14
0
def RenderSIPCollection(sipGraph, dot=None):
    try:
        from pydot import Node, Edge, Dot
    except:
        import warnings
        warnings.warn("Missing pydot library", ImportWarning)
    if not dot:
        dot = Dot(graph_type='digraph')
        dot.leftNodesLookup = {}
    nodes = {}
    for N, prop, q in sipGraph.query(
            'SELECT ?N ?prop ?q {  ?prop a magic:SipArc . ?N ?prop ?q . }',
            initNs={u'magic': MAGIC}):

        if MAGIC.BoundHeadPredicate in sipGraph.objects(
                subject=N, predicate=RDF.type):
            NCol = [N]
        else:
            NCol = Collection(sipGraph, N)

        if q not in nodes:
            newNode = Node(makeMD5Digest(q),
                           label=normalizeTerm(q, sipGraph),
                           shape='plaintext')
            nodes[q] = newNode
            dot.add_node(newNode)

        bNode = BNode()
        nodeLabel = ', '.join([normalizeTerm(term, sipGraph)
                               for term in NCol])
        edgeLabel = ', '.join([var.n3()
                               for var in Collection(sipGraph,
                                                     first(sipGraph.objects(
                                                         prop, MAGIC.bindings)))])
        markedEdgeLabel = ''
        if nodeLabel in dot.leftNodesLookup:
            bNode, leftNode, markedEdgeLabel = dot.leftNodesLookup[nodeLabel]
            # print("\t", nodeLabel, edgeLabel,
            #       markedEdgeLabel, not edgeLabel == markedEdgeLabel
        else:
            leftNode = Node(makeMD5Digest(bNode),
                            label=nodeLabel, shape='plaintext')
            dot.leftNodesLookup[nodeLabel] = (bNode, leftNode, edgeLabel)
            nodes[bNode] = leftNode
            dot.add_node(leftNode)

        if not edgeLabel == markedEdgeLabel:
            edge = Edge(leftNode,
                        nodes[q],
                        label=edgeLabel)
            dot.add_edge(edge)
    return dot
Example #15
0
    def initPlugin(self, signalproxy):
        """Initialise the systemc block diagram plugin"""

        self.signalproxy = signalproxy
        self.do = signalproxy.distributedObjects

        # systemc stuff and and pointer type strings needed for casting in gdb
        # because systemc objects can be systemc modules, systemc ports, etc.
        self.ctx = None
        self.ctx_pointer = None
        self.ctx_func = "sc_get_curr_simcontext()"
        self.ctx_found = False

        self.ctx_type = "(sc_core::sc_simcontext*)"
        self.port_type = "(sc_core::sc_port_base*)"
        self.module_type = "(sc_core::sc_module*)"
        self.object_type = "(sc_core::sc_object*)"
        self.prim_channel_type = "(sc_core::sc_prim_channel*)"

        # dict with a string that represents the pointer as key and
        # a nested dict with parent, children, etc as key-values
        #   dst_dict[ptr] = {"wrapper":    vw,
        #                    "name":       None,
        #                    "parent_ptr": None,
        #                    "children":   {}}
        self.sysc_modules = {}
        self.sysc_objects = {}
        self.sysc_ports = {}
        self.sysc_prim_channels = {}

        # because of how we built the interface for the tracepoints on the
        # datagraph we first need to create an file obj. We choose StringIO
        # because it uses a string as buffer and does not acces the filesystem
        self._file_obj = StringIO()
        self.image = SVGImage("SystemC Block Diagram", self._file_obj)
        self.image_wrapper = SVGDataGraphVW(self.image, self.do)

        self.signalproxy.inferiorStoppedNormally.connect(self.update)

        # hook Datagraph variable wrapper into the datagraph controller
        # after self.action.commit is called the image will be displayed at the
        # datagraph
        self.action = self.do.actions.\
            getAddSVGToDatagraphAction(self.image_wrapper,
                                       self.do.
                                       datagraphController.addVar)

        # pydot graph visualization library
        self.block_diagram = Dot(graph_type='digraph')
        self.block_diagram.set_graph_defaults(compound='true',
                                              splines='ortho',
                                              rankdir='LR')
        self.block_diagram.set_node_defaults(shape='box')

        # Needed for creating the right variables and variable wrappers from
        # the systemc pointers
        self.vwFactory = VarWrapperFactory()
        self.variableList = VariableList(self.vwFactory, self.do)
Example #16
0
 def plot_state(self, boxes, deltas, name = '', outdir = None):
     """ Make a graph of a given state """
     graph = Dot(graph_type='digraph', fontname="Verdana", size="10, 5", fixedsize= True)
     i_box = 0
     for box in boxes:            
         textcolor = 'white' if sum( [ self.color_chars.index(col) for col in self.plots_conf[box]['color'].split('#')[1] ] ) < 35 else 'black' 
         node_box = Node(box, style="filled", label = '<<font POINT-SIZE="10" color="'+textcolor+'">'+box+'<br/> '+
                         "%.7f" % round(deltas[i_box], 7)+'</font>>',
             fillcolor = self.plots_conf[box]['color'], shape = self.plots_conf[box]['shape'])
         i_box += 1
         graph.add_node(node_box)
         
     for box_from, boxes_to in self.Flux.iteritems():
         for box_to, flux in boxes_to.iteritems():
             if flux !=0:
                 if flux > 0:
                     edge = Edge(box_from, box_to,  label = '<<font POINT-SIZE="10">'+str(flux)+'</font>>')
                 elif flux < 0:
                     edge = Edge(box_to, box_from,  label = '<<font POINT-SIZE="10">'+str(flux)+'</font>>')                
                 graph.add_edge(edge)
     
     if outdir is None:
         outdir = self.result_dir
         
     outfile = outdir+'/state'+name+'.png'
     graph.write_png(outfile)
     logger.info('State has been saved to '+set_style(outfile, 'emph'))
Example #17
0
def get_session_svg(viz_data):
    """Take session visualization data and return svg."""
    
    graph = Dot('graphname', graph_type='digraph')
    
    #loop create all nodes and store by id
    node_dict = {}
    for i, node_data in enumerate(viz_data['nodes']):
        id = node_data['id']
        node_dict[id] = str(i)
        graph.add_node(Node(str(i)))
        
    #add edges by links
    for link_data in viz_data['links']:
        snode = node_dict[viz_data['nodes'][link_data['source']]['id']]
        tnode = node_dict[viz_data['nodes'][link_data['target']]['id']]
        graph.add_edge(Edge(snode, tnode))
    
    #get svg of graph
    file = NamedTemporaryFile()
    graph.write_svg(file.name)
    svg = file.read()
    file.close()
    
    #f = open('/tmp/session/session.svg', 'w')
    #f.write("%s\n" % svg)
    #f.close()

    return svg
Example #18
0
    def save(self, cfg, filename, print_ir=False, format='dot', options=None):
        """Save basic block graph into a file.
        """
        if options is None:
            options = {}

        try:
            dot_graph = Dot(**self.graph_format)

            # Add nodes.
            nodes = {}
            for bb in cfg.basic_blocks:
                nodes[bb.address] = self._create_node(bb, cfg.name, print_ir, options)

                dot_graph.add_node(nodes[bb.address])

            # Add edges.
            for bb_src in cfg.basic_blocks:
                for bb_dst_addr, branch_type in bb_src.branches:
                    edge = self._create_edge(nodes[bb_src.address], nodes[bb_dst_addr], branch_type)

                    dot_graph.add_edge(edge)

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save basic block graph: %s (%s)", filename, format, exc_info=True)
Example #19
0
    def save(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        node_format = {
            'shape' : 'Mrecord',
            'rankdir' : 'LR',
            'fontname' : 'monospace',
            'fontsize' : '9.0'
        }

        edge_format = {
            'fontname' : 'monospace',
            'fontsize' : '8.0'
        }

        edge_colors = {
            'taken' : 'green',
            'not-taken' : 'red',
            'direct' : 'blue'
        }

        try:
            # for each conneted component
            for idx, gr in enumerate(networkx.connected_component_subgraphs(self._graph.to_undirected())):
                graph = Dot(graph_type="digraph", rankdir="TB")

                # add nodes
                nodes = {}
                for bb_addr in gr.node.keys():
                    dump = self._dump_bb(self._bb_by_addr[bb_addr], print_ir)

                    # html-encode colon character
                    dump = dump.replace("!", "&#33;")
                    dump = dump.replace("#", "&#35;")
                    dump = dump.replace(":", "&#58;")
                    dump = dump.replace("{", "&#123;")
                    dump = dump.replace("}", "&#125;")

                    label = "{<f0> 0x%08x | %s}" % (bb_addr, dump)

                    nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                    graph.add_node(nodes[bb_addr])

                # add edges
                for bb_src_addr in gr.node.keys():
                    for bb_dst_addr, branch_type in self._bb_by_addr[bb_src_addr].branches:
                        graph.add_edge(Edge(nodes[bb_src_addr],
                            nodes[bb_dst_addr], label=branch_type, \
                            color=edge_colors[branch_type], **edge_format))

                graph.write("%s_%03d.%s" % (filename, idx, format), format=format)
        except Exception as err:
            import traceback
            import sys
            print("[E] Error loading BARF (%s:%d) : '%s'" %
                (__name__, sys.exc_traceback.tb_lineno, str(err)))
            print("")
            print(traceback.format_exc())
Example #20
0
class TestUtilN3toDot(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()
        self.graph.parse(StringIO(n3source), format="n3")
        self.dot = Dot()

    def test_util_graph_to_dot(self):
        res = graphutils.graph_to_dot(self.graph, self.dot)
        res = self.dot.to_string()
        self.assert_('swap/Primer#Person' in res, res)
def process_plan(filename, plan):
    graph = Dot()
    teams = fetch_teams()

    nodes = make_nodes(plan, teams)

    for team_node in nodes:
        graph.add_node(nodes[team_node])

    for team in plan:
        edges = graph_way(team, plan[team], nodes)
        for edge in edges:
            graph.add_edge(edge)

    graph.set_prog("circo")
    graph.write_png(filename)
Example #22
0
 def make_pydot_graph(self, figname='tree'):
     ''' Use Graphviz to generate a graphical representation of the tree.
 Args:
   figname (str): where to store the figure.
 '''
     graph = Dot(graph_type='graph')
     #graph.add_node(Node(self.root.display(),shape='diamond'))
     edgelist = self.root.append_edgelist()
     for edge in edgelist:
         graph.add_node(edge[0])
         graph.add_node(edge[1])
         graph.add_edge(Edge(*edge))
     graph.write_png(figname + '.png')
Example #23
0
    def markBrokenRelations(self):
        """ Construct graph and return message with info about broken 
        relations for ToolGraph if any errors are found
        """
        bad_relations = []
        bad_content = [] 
        bad_rel = ""

        graph = PyGraph()
        tool = queryAdapter(self.context, IToolAccessor)
        types = tool.types(proxy=False)
        for ctype in types:
            node = queryAdapter(ctype, INode)
            graph.add_node(node())

        relations = tool.relations(proxy=False)
        for relation in relations:
            edge = queryAdapter(relation, IEdge)
            res = edge()
            if res:
                graph.add_edge(res)
                continue
            else:
                # if no result then check which relation id is missing
                from_rel = relation['from']
                to_rel = relation['to']
                pr_from = self.pt_relations.get(from_rel)
                pr_to = self.pt_relations.get(to_rel)
                if not pr_from:
                    bad_rel = from_rel
                if not pr_to:
                    bad_rel = to_rel
                if bad_rel and bad_rel not in bad_content:
                    bad_content.append(bad_rel)
                bad_relations.append(relation.Title())

        self.graph_res = graph
        if bad_relations:
            return self.brokenRelationMessage(bad_content, bad_relations)
        return ""
Example #24
0
    def _draw_subgraph(diagram: pydot.Dot,
                       label_last_seen: DefaultDict[str, str],
                       subgraph_name: str,
                       subgraph_ops: List[Union[Op, Trace, Any]]) -> None:
        """Draw a subgraph of ops into an existing `diagram`.

        Args:
            diagram: The diagram to be appended to.
            label_last_seen: A mapping of {data_dict_key: node_id} indicating the last node which generated the key.
            subgraph_name: The name to be associated with this subgraph.
            subgraph_ops: The ops to be wrapped in this subgraph.
        """
        subgraph = pydot.Cluster(style='dashed', graph_name=subgraph_name)
        subgraph.set('label', subgraph_name)
        subgraph.set('labeljust', 'l')
        for idx, op in enumerate(subgraph_ops):
            node_id = str(id(op))
            Traceability._add_node(subgraph, op, node_id)
            if isinstance(op, (Op, Trace)):
                # Need the instance check since subgraph_ops might contain a tf dataset or torch dataloader
                edge_srcs = defaultdict(lambda: [])
                for inp in op.inputs:
                    if inp == '*':
                        continue
                    edge_srcs[label_last_seen[inp]].append(inp)
                for src, labels in edge_srcs.items():
                    diagram.add_edge(pydot.Edge(src=src, dst=node_id, label=f" {', '.join(labels)} "))
                for out in op.outputs:
                    label_last_seen[out] = node_id
            if isinstance(op, Trace) and idx > 0:
                # Invisibly connect traces in order so that they aren't all just squashed horizontally into the image
                diagram.add_edge(pydot.Edge(src=str(id(subgraph_ops[idx - 1])), dst=node_id, style='invis'))
        diagram.add_subgraph(subgraph)
Example #25
0
    def create_graph_from_workflow(self, workflow):
        """
        :type workflow: Workflow
        :rtype: Dot
        """
        known_nodes = {}
        start = workflow.getStartNode()
        name = workflow.getName()
        g = Dot(graph_name=name,
                ranksep=1,
                labelloc="t",
                label="workflow: " + name,
                fontsize=18,
                fontcolor="blue")
        g.set_node_defaults(shape="box", fontsize=12)
        g.set_edge_defaults(fontsize=13, labeldistance=3)

        def add_node_rek(node):
            """Recurses through the workflow graph and adds nodes and edges.
            Repeated nodes are ignored (cycle detection).
            :type node: core.tree.Node
            """
            name = node.getName()
            if name in known_nodes:
                return known_nodes[name]
            dot_node = Node(name=name)
            g.add_node(dot_node)
            known_nodes[name] = dot_node
            logg.debug("created node %s", name)
            try:
                true_next_id = node.getTrueId()
            except:
                true_next_id = None
            if true_next_id:
                true_next = workflow.getStep(true_next_id)
                true_dot_next = add_node_rek(true_next)
                true_label = node.getTrueLabel() or " "
                g.add_edge(true_edge(dot_node, true_dot_next,
                                     label=true_label))
                logg.debug("created True edge: %s -> %s", name, true_next_id)
            try:
                false_next_id = node.getFalseId()
            except:
                false_next_id = None
            if false_next_id:
                false_next = workflow.getStep(false_next_id)
                false_dot_next = add_node_rek(false_next)
                false_label = node.getFalseLabel() or " "
                g.add_edge(
                    false_edge(dot_node, false_dot_next, label=false_label))
                logg.debug("created False edge: %s -> %s", name, false_next_id)

            if not (true_next_id or false_next_id):
                # end node with no connections
                dot_node.set("color", "#04B45F")
            return dot_node

        # traverse from start node
        add_node_rek(start)
        return g
Example #26
0
 def draw(self):
     dt = Dot(graph_type='digraph',fontname="Verdana")
     for node in self.nodes:
         dt.add_node(pydot.Node(node.name, label=self.simplabel(node)))
     for edge in self.edges:
         dt.add_edge(pydot.Edge(edge.frm.name, edge.to.name))
     return dt
Example #27
0
    def _delayed_edge(key: str, progenitor: pydot.Dot, old_source: str,
                      new_source: str) -> str:
        """Draw a specific edge between two nodes, modifying the old label if applicable.

        Args:
            key: The key associated with the edge.
            progenitor: The parent cluster.
            old_source: The edge source.
            new_source: The edge sync.

        Returns:
            The `new_source`.
        """
        edge = progenitor.get_edge(old_source, new_source)
        if edge:
            edge = edge[0]
            label = f"{edge.get_label()}, {key}"
            edge.set_label(label)
        else:
            progenitor.add_edge(
                pydot.Edge(src=old_source, dst=new_source, label=f" {key}"))
        return new_source
Example #28
0
    def __init__(self, _contract: Contract):
        """
        Takes a Contract object and constructs the DDG for it.

        *** To be completed
            Currently cannot detect indirect read.
            Indirect write can be detected.
        """
        self.contract = _contract
        self.node_dic = {}
        self.edge_dic = {}
        self.graph = Dot()

        for f in _contract.functions + _contract.constructor_as_list:
            if f.name in [
                    'slitherConstructorVariables',
                    'slitherConstructorConstantVariables'
            ] or not f.is_public_or_external:
                continue
            self.construct_node(f)

        self.construct_graph(_contract)
Example #29
0
def visualize(graph, filename="graph.png", include_args=True, transitive=False):
    data = to_graphviz(graph, transitive)
    dot = Dot(graph_type="digraph")

    for node in data["nodes"]:
        fmt = format_edge(graph, node) if include_args else node
        dot.add_node(Node(node, label=fmt))
    for a, b in data["edges"]:
        dot.add_edge(Edge(a, b))

    dot.write_png(filename)
Example #30
0
 def build_site_graph(self):
     self.site_graph = Dot(graph_type='digraph')
     self.site_graph.set_label( 'Sitemap for "%s"' % self.base_uri )
     self.site_graph.set_simplify( True )
     # add nodes
     for page in self.site_dict['pages']:
         self.site_graph.add_node( page.to_node() )
     # add edges
     for page in self.site_dict['pages']:
         from_node = page.uri
         for link in page.attributes['links']:
           to_node = link
           self.site_graph.add_edge( Edge(from_node, to_node) )
Example #31
0
    def _add_edge(progenitor: pydot.Dot, op: Union[Trace, Op],
                  label_last_seen: Dict[str, str]):
        """Draw edges into a given Node.

        Args:
            progenitor: The very top level diagram onto which Edges should be written.
            op: The op (or trace) to be visualized.
            label_last_seen: A mapping of {data_dict_key: node_id} indicating the last node which generated the key.
        """
        node_id = str(id(op))
        edge_srcs = defaultdict(lambda: [])
        for inp in op.inputs:
            if inp == '*':
                continue
            edge_srcs[label_last_seen[inp]].append(inp)
        for src, labels in edge_srcs.items():
            progenitor.add_edge(
                pydot.Edge(src=src,
                           dst=node_id,
                           label=f" {', '.join(labels)} "))
        for out in op.outputs:
            label_last_seen[out] = node_id
Example #32
0
def plot_tree(sent_id):
    global sentences

    graph = Dot(graph_type='digraph')

    basic_dependencies = sentences[sent_id].findall(
        "./dependencies[@type='collapsed-dependencies']/dep")
    for dep in basic_dependencies:

        gov = "{}\n{}".format(dep[0].get("idx"), dep[0].text)
        dep = "{}\n{}".format(dep[1].get("idx"), dep[1].text)
        graph.add_node(Node(gov))
        graph.add_node(Node(dep))
        graph.add_edge(Edge(dep, gov))
    graph.write_png(f"sentence_{sent_id}.png")
Example #33
0
def main(input):
    state_obj = yaml.load(input)
    graph = Dot("states", graph_type='digraph')

    rules = {
        'require': {'color': 'blue'},
        'require_in': {'color': 'blue', 'reverse': True},
        'watch': {'color': 'red'},
        'watch_in': {'color': 'red', 'reverse': True},
    }

    for top_key, props in state_obj.iteritems():
        # Add a node for each state type embedded in this state
        # keys starting with underscores are not candidates

        if top_key == '__extend__':
            # TODO - merge these into the main states and remove them
            sys.stderr.write(
                    "Removing __extend__ states:\n{0}\n".format(str(props)))
            continue

        for top_key_type, states in props.iteritems():
            if top_key_type[:2] == '__':
                continue

            node_name = make_node_name(top_key_type, top_key)
            graph.add_node(Node(node_name))

            for edge_type, ruleset in rules.iteritems():
                for relname in find_edges(states, edge_type):
                    if 'reverse' in ruleset and ruleset['reverse']:
                        graph.add_edge(Edge(
                            node_name, relname, color=ruleset['color']))
                    else:
                        graph.add_edge(Edge(
                            relname, node_name, color=ruleset['color']))

    graph.write('/dev/stdout')
Example #34
0
    def graph_representation(self):
        """
        Construct a dot graph representation of these results.
        :return: The dot graph
        """
        from pydot import Dot, Edge, Node

        g = Dot()

        for r in self.resources:
            g.add_node(
                Node('i_' + r,
                     label='{:.3} {}'.format(
                         float(self.produced(r) + self.supplied(r)), r),
                     style='dashed'))
        for recipe, batches in self.recipes.items():
            g.add_node(
                Node('r_' + recipe,
                     label='{:.3} {}'.format(batches[1], recipe),
                     shape='box'))

            r: Recipe = self.book[recipe]
            for output in r.outputs():
                weight = r.produced(output, batches[1])
                g.add_edge(
                    Edge('r_' + recipe,
                         'i_' + output,
                         label='{:.3}'.format(weight),
                         weight=weight))
            for input in r.inputs():
                weight = r.consumed(input, batches[1])
                g.add_edge(
                    Edge('i_' + input,
                         'r_' + recipe,
                         label='{:.3}'.format(weight),
                         weight=weight))

        return g
Example #35
0
def getGraph(gr: dot.Dot, size=None) -> str:
    """
  get an HTML representation of a pydot graph

  Parameters
  ----------
  gr: pydot.Dot
    the graph
  size: float|str
    the size of the rendered graph

  Returns
  -------
    the HTML representation of the graph (as a string)
  """
    if size is None:
        size = gum.config["notebook", "default_graph_size"]

    # workaround for some badly parsed graph (pyparsing>=3.03)
    gr.del_node('"\\n"')
    gr.del_node('"\\n\\n"')

    return _reprGraph(gr, size, asString=True)
Example #36
0
    def show_diagram(self, path=None):
        """
            Creates the graph associated with this DFA
        """
        # Nodes are set of states

        graph = Dot(graph_type='digraph', rankdir='LR')
        nodes = {}
        for state in self.states:
            if state == self.initial_state:
                # color start state with green
                if state in self.final_states:
                    initial_state_node = Node(state,
                                              style='filled',
                                              peripheries=2,
                                              fillcolor='#66cc33')
                else:
                    initial_state_node = Node(state,
                                              style='filled',
                                              fillcolor='#66cc33')
                nodes[state] = initial_state_node
                graph.add_node(initial_state_node)
            else:
                if state in self.final_states:
                    state_node = Node(state, peripheries=2)
                else:
                    state_node = Node(state)
                nodes[state] = state_node
                graph.add_node(state_node)
        # adding edges
        for from_state, lookup in self.transitions.items():
            for to_label, to_state in lookup.items():
                graph.add_edge(
                    Edge(nodes[from_state], nodes[to_state], label=to_label))
        if path:
            graph.write_png(path)
        return graph
Example #37
0
class ToolGraph(BaseGraph):
    """ Draw a graph for portal_relations
    """
    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        xtool = queryAdapter(self.context, IToolAccessor)
        typesIds = set()
        for ctype in xtool.types(proxy=False):
            typesIds.add(ctype.getId())
            node = queryAdapter(ctype, INode)
            self._graph.add_node(node())

        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            edge = queryAdapter(relation, IEdge)()
            if edge:
                self._graph.add_edge(edge)
            else:
                self._bad_relations.add(relation.Title())
                if value_from not in typesIds:
                    self._bad_content.add(value_from)
                if value_to not in typesIds:
                    self._bad_content.add(value_to)

        return self._graph
def process_plan(filename, plan, names=True, with_dist=False, with_label=False):
    graph = Dot(overlap="false", splines="true", esep=.2)
    teams = fetch_teams()

    distances = fetch_distances()

    nodes = make_nodes(plan, teams, names)

    for team_node in nodes:
        graph.add_node(nodes[team_node])

    for team in plan:
        edges = graph_way(team, plan[team], nodes, distances, with_dist, with_label)
        for edge in edges:
            graph.add_edge(edge)

    if with_dist:
        graph.write_png(filename, prog="neato")
    else:
        graph.write_png(filename, prog="dot")
Example #39
0
    def _draw_subgraph(progenitor: pydot.Dot,
                       diagram: Union[pydot.Dot, pydot.Cluster],
                       label_last_seen: DefaultDict[str, str],
                       subgraph_name: str,
                       subgraph_ops: List[Union[Op, Trace, Any]]) -> None:
        """Draw a subgraph of ops into an existing `diagram`.

        Args:
            progenitor: The very top level diagram onto which Edges should be written.
            diagram: The diagram into which to add new Nodes.
            label_last_seen: A mapping of {data_dict_key: node_id} indicating the last node which generated the key.
            subgraph_name: The name to be associated with this subgraph.
            subgraph_ops: The ops to be wrapped in this subgraph.
        """
        subgraph = pydot.Cluster(style='dashed', graph_name=subgraph_name, color='black')
        subgraph.set('label', subgraph_name)
        subgraph.set('labeljust', 'l')
        for idx, op in enumerate(subgraph_ops):
            node_id = str(id(op))
            Traceability._add_node(progenitor, subgraph, op, label_last_seen)
            if isinstance(op, Trace) and idx > 0:
                # Invisibly connect traces in order so that they aren't all just squashed horizontally into the image
                progenitor.add_edge(pydot.Edge(src=str(id(subgraph_ops[idx - 1])), dst=node_id, style='invis'))
        diagram.add_subgraph(subgraph)
Example #40
0
    def work():
        graph = Dot(graph_name='Parse Tree', graph_type='graph')

        def start_graph(g, node, parent_node_name=None):
            g.add_node(
                Node(name=node.getNodeNum(),
                     shape='plaintext',
                     label=node.getLabel()))
            if parent_node_name:
                g.add_edge(Edge(parent_node_name, node.getNodeNum()))
            if len(node.getKids()) > 0:
                for kid in node.getKids():
                    start_graph(g, kid, node.getNodeNum())
            else:
                g.add_node(
                    Node(name=f'{node.getNodeNum()}_content',
                         shape='plaintext',
                         label=node.getContent()))
                g.add_edge(
                    Edge(node.getNodeNum(), f'{node.getNodeNum()}_content'))

        start_graph(graph, program_tree)
        graph.write_png(outputFilePath if outputFilePath[-4:] ==
                        '.png' else outputFilePath + '.png')
Example #41
0
def test_BioProvDocument():
    """
    Tests the construction of an instance of BioProvDocument.
    :return:
    """
    # The BioProvDocument constructor with add_attributes=True
    # is tested in the test_src_main.py module
    prov = BioProvDocument(project)

    # __repr__
    assert str(prov).startswith("BioProvDocument")

    # dot property
    assert type(prov.dot) == Dot
    prov.dot = Dot()
Example #42
0
def crawl_function(node_summaries: Dict[str, NodeSummary],
                   function_summaries: Dict[str, Summary], func_name: str,
                   g: pydot.Dot):
    """
    Creates the `Summary` for the function `func_name` and stores it in the global variable `function_summaries`

    :param node_summaries:
    :param function_summaries:
    :param func_name: The name of the function whose Summary will be created
    :param g: The compiler-generated graph
    """
    if func_name in function_summaries:
        if function_summaries[func_name].status == SummaryState.PROCESSING:
            function_summaries[func_name].type = NodeType.LOOP
        return

    s = function_summaries[func_name] = Summary()

    func_graph_list = g.get_subgraph("\"cluster_" + func_name + "\"")
    if len(
            func_graph_list
    ) < 1:  # Função pertence a alguma biblioteca, logo não é motivo de preocupação
        s.status = SummaryState.DONE
        return
    func_graph = func_graph_list[0]

    s.status = SummaryState.PROCESSING

    for edge in func_graph.get_edges():
        src: str = edge.get_source().split(':', maxsplit=1)[0]
        dst: str = edge.get_destination().split(':', maxsplit=1)[0]
        if src not in node_summaries:
            node_summaries[src] = NodeSummary()
        if dst not in node_summaries:
            node_summaries[dst] = NodeSummary()
        node_summaries[src].out_nodes.add(dst)

    first_node = crawl_subgraph(node_summaries, function_summaries, func_graph)

    s.variables = node_summaries[first_node].variables
    s.open_locks = node_summaries[first_node].open_locks

    if s.type == NodeType.LOOP:
        for v, r in s.variables.items():
            r.loop()

    s.status = SummaryState.DONE
    return
Example #43
0
def renderNetwork(network, nsMap={}):
    """
    Takes an instance of a compiled ReteNetwork and a namespace mapping (for constructing QNames
    for rule pattern terms) and returns a BGL Digraph instance representing the Rete network
    #(from which GraphViz diagrams can be generated)
    """
    # from FuXi.Rete import BuiltInAlphaNode
    # from BetaNode import LEFT_MEMORY, RIGHT_MEMORY, LEFT_UNLINKING
    dot = Dot(graph_type='digraph')
    namespace_manager = NamespaceManager(Graph())
    for prefix, uri in list(nsMap.items()):
        namespace_manager.bind(prefix, uri, override=False)

    visitedNodes = {}
    edges = []
    idx = 0
    for node in list(network.nodes.values()):
        if node not in visitedNodes:
            idx += 1
            visitedNodes[node] = generateBGLNode(dot, node, namespace_manager,
                                                 str(idx))
            dot.add_node(visitedNodes[node])
    nodeIdxs = {}
    for node in list(network.nodes.values()):
        for mem in node.descendentMemory:
            if not mem:
                continue
            bNode = mem.successor
        for bNode in node.descendentBetaNodes:
            for idx, otherNode in enumerate([bNode.leftNode, bNode.rightNode]):
                if node == otherNode and (node, otherNode) not in edges:
                    for i in [node, bNode]:
                        if i not in visitedNodes:
                            idx += 1
                            nodeIdxs[i] = idx
                            visitedNodes[i] = generateBGLNode(
                                dot, i, namespace_manager, str(idx))
                            dot.add_node(visitedNodes[i])
                    edge = Edge(visitedNodes[node],
                                visitedNodes[bNode],
                                label=idx == 0 and 'left' or 'right')
                    dot.add_edge(edge)
                    edges.append((node, bNode))

    return dot
Example #44
0
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        # This node
        name = self.context.getId()
        node = queryAdapter(self.context, INode)
        self._graph.add_node(node())

        xtool = queryAdapter(self.context, IToolAccessor)
        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            if name == value_from:
                nto = self.tool.get(value_to)
                if (value_from != value_to) and nto:
                    node = queryAdapter(nto, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_to)
                    self._bad_relations.add(relation.Title())

            elif name == value_to:
                nfrom = self.tool.get(value_from)
                if (value_from != value_to) and nfrom:
                    node = queryAdapter(nfrom, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_from)
                    self._bad_relations.add(relation.Title())

        return self._graph
def main(input):
    state_obj = yaml.load(input)
    graph = Dot("states", graph_type='digraph')

    rules = {
        'require': {'color': 'blue'},
        'require_in': {'color': 'blue', 'reverse': True},
        'watch': {'color': 'red'},
        'watch_in': {'color': 'red', 'reverse': True},
    }

    for top_key, props in state_obj.iteritems():
        # Add a node for each state type embedded in this state
        # keys starting with underscores are not candidates

        if top_key == '__extend__':
            # TODO - merge these into the main states and remove them
            sys.stderr.write(
                    "Removing __extend__ states:\n{0}\n".format(str(props)))
            continue

        for top_key_type, states in props.iteritems():
            if top_key_type[:2] == '__':
                continue

            node_name = make_node_name(top_key_type, top_key)
            graph.add_node(Node(node_name))

            for edge_type, ruleset in rules.iteritems():
                for relname in find_edges(states, edge_type):
                    if 'reverse' in ruleset and ruleset['reverse']:
                        graph.add_edge(Edge(
                            node_name, relname, color=ruleset['color']))
                    else:
                        graph.add_edge(Edge(
                            relname, node_name, color=ruleset['color']))

    graph.write('/dev/stdout')
Example #46
0
    def build_graph_parser(subparsers):
        try:
            from pydot import Dot
            formats = Dot().formats
        except ImportError:
            return

        p_graph = subparsers.add_parser(
            'graph',
            help='Build a graph from the XML tags relationships.',
            description='Build a graph from the XML tags relationships.')

        def act(ns):
            extension = ns.outfile.split('.')[-1]
            if ns.format:
                if extension != ns.format:
                    ns.outfile += '.' + ns.format
            else:
                if extension in formats:
                    ns.format = extension

            root = etree.parse(ns.infile).getroot()
            write_tag_graph(ns.path(root), ns.outfile, ns.format)

        p_graph.set_defaults(action=act)
        p_graph.add_argument('--format',
                             choices=formats,
                             metavar='FORMAT',
                             help='The format for the graph image.\n'
                             'It will be appended to the filename '
                             'unless they already concur '
                             'or -F is passed.\n'
                             'Choose from ' + str(formats))
        p_graph.add_argument('-F',
                             '--force-extension',
                             help='Allow the filename extension to differ '
                             'from the file format.\n'
                             'Without this option, the format extension '
                             'will be appended to the filename.')
        p_graph.add_argument(dest='outfile',
                             default=None,
                             help='The filename for the graph image.\n'
                             'If no --format is given, '
                             'it will be based on this name.')
Example #47
0
def leveler(df):
    thislist, deptlist = get_reports(df)
    newdict = {"Berenecea Johnson-Eanes": 1}
    newdict.update({x[0]: 0 for x in thislist})
    while True:
        if len([value for value in newdict.values() if value == 0]) == 0:
            break
        for person in newdict.keys():
            if newdict[person] == 0:
                try:
                    if newdict[[x[1] for x in thislist
                                if x[0] == person][0]] == 0:
                        pass
                    else:
                        newnum = newdict[[
                            x[1] for x in thislist if x[0] == person
                        ][0]] + 1
                        newdict.update({person: newnum})
                except:
                    print(f'{person} not found')
            else:
                pass
    for i in range(max([value for value in newdict.values()])):
        graph = Dot(graph_type='graph',
                    ratio='.3',
                    size=150,
                    dpi=150,
                    splines='ortho')
        #graph.set_node_defaults(style="filled", fillcolor="grey")
        graph.set_edge_defaults(color="blue", arrowhead="vee", weight="1")
        for report_to, report in thislist:
            try:
                if newdict[report_to] in [
                        i, i + 1, i + 2
                ] or newdict[report] in [i, i + 1, i + 2]:
                    graph.add_edge(Edge(report, report_to))
            except:
                print(report_to, report)
        #graph.write("c:\\users\\shane\\desktop\\out.dot")
        graph.write_png(f"c:\\users\\shane\\desktop\\orgcharts\\level{i}.png")
Example #48
0
File: Util.py Project: drewp/FuXi
def renderNetwork(network, nsMap={}):
    """
    Takes an instance of a compiled ReteNetwork and a namespace mapping (for constructing QNames
    for rule pattern terms) and returns a BGL Digraph instance representing the Rete network
    #(from which GraphViz diagrams can be generated)
    """
    # from FuXi.Rete import BuiltInAlphaNode
    # from BetaNode import LEFT_MEMORY, RIGHT_MEMORY, LEFT_UNLINKING
    dot = Dot(graph_type='digraph')
    namespace_manager = NamespaceManager(Graph())
    for prefix, uri in list(nsMap.items()):
        namespace_manager.bind(prefix, uri, override=False)

    visitedNodes = {}
    edges = []
    idx = 0
    for node in list(network.nodes.values()):
        if node not in visitedNodes:
            idx += 1
            visitedNodes[node] = generateBGLNode(
                dot, node, namespace_manager, str(idx))
            dot.add_node(visitedNodes[node])
    nodeIdxs = {}
    for node in list(network.nodes.values()):
        for mem in node.descendentMemory:
            if not mem:
                continue
            bNode = mem.successor
        for bNode in node.descendentBetaNodes:
            for idx, otherNode in enumerate([bNode.leftNode, bNode.rightNode]):
                if node == otherNode and (node, otherNode) not in edges:
                    for i in [node, bNode]:
                        if i not in visitedNodes:
                            idx += 1
                            nodeIdxs[i] = idx
                            visitedNodes[i] = generateBGLNode(
                                dot, i, namespace_manager, str(idx))
                            dot.add_node(visitedNodes[i])
                    edge = Edge(visitedNodes[node],
                                visitedNodes[bNode],
                                label=idx == 0 and 'left' or 'right')
                    dot.add_edge(edge)
                    edges.append((node, bNode))

    return dot
    def _draw_subgraph(diagram: pydot.Dot, label_last_seen: DefaultDict[str,
                                                                        str],
                       subgraph_name: str,
                       subgraph_ops: List[Union[Op, Trace]]) -> None:
        """Draw a subgraph of ops into an existing `diagram`.

        Args:
            diagram: The diagram to be appended to.
            label_last_seen: A mapping of {data_dict_key: node_id} indicating the last node which generated the key.
            subgraph_name: The name to be associated with this subgraph.
            subgraph_ops: The ops to be wrapped in this subgraph.
        """
        subgraph = pydot.Cluster(style='dashed', graph_name=subgraph_name)
        subgraph.set('label', subgraph_name)
        subgraph.set('labeljust', 'l')
        for idx, op in enumerate(subgraph_ops):
            node_id = str(id(op))
            if isinstance(op, ModelOp):
                label = f"{op.__class__.__name__} ({FEID(id(op))}): {op.model.model_name}"
                model_ref = Hyperref(Marker(name=str(op.model.model_name),
                                            prefix='subsec'),
                                     text=NoEscape(r'\textcolor{blue}{') +
                                     bold(op.model.model_name) +
                                     NoEscape('}')).dumps()
                texlbl = f"{HrefFEID(FEID(id(op)), name=op.__class__.__name__).dumps()}: {model_ref}"
            else:
                label = f"{op.__class__.__name__} ({FEID(id(op))})"
                texlbl = HrefFEID(FEID(id(op)),
                                  name=op.__class__.__name__).dumps()
            subgraph.add_node(pydot.Node(node_id, label=label, texlbl=texlbl))
            edge_srcs = defaultdict(lambda: [])
            for inp in op.inputs:
                if inp == '*':
                    continue
                edge_srcs[label_last_seen[inp]].append(inp)
            for src, labels in edge_srcs.items():
                diagram.add_edge(
                    pydot.Edge(src=src,
                               dst=node_id,
                               label=f" {', '.join(labels)} "))
            for out in op.outputs:
                label_last_seen[out] = node_id
            if isinstance(op, Trace) and idx > 0:
                # Invisibly connect traces in order so that they aren't all just squashed horizontally into the image
                diagram.add_edge(
                    pydot.Edge(src=str(id(subgraph_ops[idx - 1])),
                               dst=node_id,
                               style='invis'))
        diagram.add_subgraph(subgraph)
Example #50
0
def render_state_diagram(C_M, S_M, filename, depth=None):
    tot_M = C_M + S_M

    if depth:
        reachable_states = find_reachable_states(tot_M, len(tot_M) - 1, depth)
        reachable_states.add(len(tot_M) - 1)
    else:
        reachable_states = range(len(tot_M))

    graph = Dot(graph_type="digraph", rankdir="LR")
    nodes = [Node(str(i), shape="circle") for i in range(len(tot_M) + 1)]
    nodes[-2].set_shape("point")
    nodes[-1].set_shape("doublecircle")

    for i in reachable_states:
        graph.add_node(nodes[i])
    graph.add_node(nodes[-1])

    def _render_edges(M, color, end_state):
        for i in reachable_states:
            row_sum = sum(tot_M[i])
            col_sum = sum(tot_M[:, i])
            if not end_state:
                for j in reachable_states:
                    elem = M[i, j]
                    if elem > 0:
                        if col_sum > 0:
                            prob = elem / col_sum
                        else:
                            prob = elem / row_sum
                        edge = Edge(nodes[i], nodes[j], label="%.2f" % prob, color=color)
                        graph.add_edge(edge)
            else:
                if row_sum < col_sum:
                    prob = (col_sum - row_sum) / col_sum
                    edge = Edge(nodes[i], nodes[-1], label="%.2f" % prob, color=color)
                    graph.add_edge(edge)

    _render_edges(C_M, "blue", False)
    _render_edges(S_M, "red", False)
    _render_edges(None, "black", True)

    graph.write_png(filename)
    def save(self, cfg, filename, print_ir=False, format='dot', options=None):
        """Save basic block graph into a file.
        """
        if options is None:
            options = {}

        try:
            dot_graph = Dot(**self.graph_format)

            # Add nodes.
            nodes = {}
            for bb in cfg.basic_blocks:
                nodes[bb.address] = self._create_node(bb, cfg.name, print_ir,
                                                      options)

                dot_graph.add_node(nodes[bb.address])

            # Add edges.
            for bb_src in cfg.basic_blocks:
                for bb_dst_addr, branch_type in bb_src.branches:
                    if bb_dst_addr in nodes:
                        edge = self._create_edge(nodes[bb_src.address],
                                                 nodes[bb_dst_addr],
                                                 branch_type)

                        dot_graph.add_edge(edge)
                    else:
                        logger.warning(
                            "Destination basic block not found! (0x%x)",
                            bb_dst_addr)

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save basic block graph: %s (%s)",
                         filename,
                         format,
                         exc_info=True)
Example #52
0
    def save(self, cf, filename, format='dot'):
        """Save basic block graph into a file.
        """
        try:
            dot_graph = Dot(**self.graph_format)

            # add nodes
            nodes = {}
            for cfg_addr in cf._graph.node.keys():
                nodes[cfg_addr] = self._create_node(cfg_addr, cf)

                dot_graph.add_node(nodes[cfg_addr])

            # add edges
            for cfg_src_addr in cf._graph.node.keys():
                for cfg_dst_addr in cf._edges.get(cfg_src_addr, []):
                    edge = self._create_edge(nodes, cfg_src_addr, cfg_dst_addr)

                    dot_graph.add_edge(edge)

            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception as err:
           logger.error("Failed to save call graph: %s (%s)", filename, format, exc_info=True)
Example #53
0
 def draw(self, prob, targetFile):
     # Do the graphing stuff here...
     # Root graph
     g = Dot(graph_type="digraph", nodesep=2, overlap=False)
     #g.set_edge_defaults(weight="0", minlen="10")
     # Organise by adding constraints (adds edges too)
     for constr in prob.constrs:
         # Node for constraint
         constrNode = Node(constr.name, shape="ellipse", style="filled", fillcolor = "#aaaaff")
         constrNode.set_label(constr.name + ": " + constr.getTextFormula())
         g.add_node(constrNode)
         # Associated expressions
         for expr in constr.exprs:
             self.addNodesForChildren(g, expr, constr)
     # Finally, render
     #g.write_png("problem_structure.png", prog="dot")
     g.write_png(targetFile, prog="neato")
Example #54
0
    def save(self, cfg, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        try:
            dot_graph = Dot(**self.graph_format)

            # Add nodes.
            nodes = {}
            for bb in cfg.basic_blocks:
                nodes[bb.address] = self._create_node(bb, cfg.name, print_ir)

                dot_graph.add_node(nodes[bb.address])

            # Add edges.
            for bb_src in cfg.basic_blocks:
                for bb_dst_addr, branch_type in bb_src.branches:
                    edge = self._create_edge(nodes[bb_src.address], nodes[bb_dst_addr], branch_type)

                    dot_graph.add_edge(edge)

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save basic block graph: %s (%s)", filename, format, exc_info=True)
Example #55
0
def draw_pydot(nodes, edges, direction='LR', **kwargs):
    def make_subgraph(path, parent_graph):
        subgraph = Cluster(path,
                           label=split(path)[1],
                           style='rounded, filled',
                           fillcolor='#77777744')
        parent_graph.add_subgraph(subgraph)
        return subgraph

    subgraphs = cache(
        lambda path: make_subgraph(path, subgraphs[parent(path)]))
    subgraphs[''] = g = Dot(rankdir=direction, directed=True, **kwargs)
    g.set_node_defaults(shape='box',
                        style='rounded, filled',
                        fillcolor='#ffffff')

    for node, path, attr in nodes:
        p, stub = split(path)
        subgraphs[p].add_node(Node(name=node, label=stub, **attr))
    for src, dst, attr in edges:
        g.add_edge(Edge(src, dst, **attr))

    return g.create_svg()
Example #56
0
def indiv_with_sub(df, outfolder):
    thislist, deptlist = get_reports(df)
    for x in list(set([i[1] for i in thislist])):
        graph = Dot(graph_type='graph',
                    ratio='auto',
                    size=150,
                    dpi=150,
                    splines='ortho')
        #people who report to x
        reports = [y for y in thislist if y[1] == x]
        #and the people they report to
        reports2 = [y for y in thislist if y[1] in [z[0] for z in reports]]
        reporting = reports + reports2
        reporting = list(set(reporting))
        for report_to, report in reporting:
            graph.add_edge(Edge(report, report_to))
        print(f"Now writing file for {x}")
        graph.write_png(f"{outfolder}\\{x}.png")
Example #57
0
    def markBrokenRelations(self):
        """ Construct graph and return message with info about broken 
        relations for RelationGraph if any errors are found
        """ 
        bad_relations = []
        bad_content = []
        bad_rel = ""

        graph = PyGraph()
        value_from = self.context.getField('from').getAccessor(self.context)()
        nfrom = self.pt_relations.get(value_from)
        if nfrom:
            node = queryAdapter(nfrom, INode)
            graph.add_node(node())

        value_to = self.context.getField('to').getAccessor(self.context)()
        nto = self.pt_relations.get(value_to)
        if not (value_from == value_to) and nto:
            node = queryAdapter(nto, INode)
            graph.add_node(node())

        edge = queryAdapter(self.context, IEdge)
        res = edge()
        if res:
            graph.add_edge(res)
            self.graph_res = graph
            return ""

        if not nfrom:
            bad_rel = value_from
        if not nto:
            bad_rel = value_to
        relation = self.pt_relations[self.context.getId()]
        if bad_rel and bad_rel not in bad_content:
            bad_content.append(bad_rel)
            bad_relations.append(relation.Title())
            self.graph_res = graph
            return self.brokenRelationMessage(bad_content, bad_relations)
Example #58
0
def make_pydot(nodes, edges, direction='LR', sep='_', **kwargs):
    from pydot import Dot, Cluster, Node, Edge
    class Subgraphs(dict):
        def __missing__(self, path):
            *parent, label = path
            subgraph = Cluster(sep.join(path), label=label, style='rounded, filled', fillcolor='#77777744')
            self[tuple(parent)].add_subgraph(subgraph)
            return subgraph
    g = Dot(rankdir=direction, directed=True, **kwargs)
    g.set_node_defaults(
        shape='box', style='rounded, filled', fillcolor='#ffffff')
    subgraphs = Subgraphs({(): g})
    for path, attr in nodes:
        *parent, label = path.split(sep)
        subgraphs[tuple(parent)].add_node(
            Node(name=path, label=label, **attr))
    for src, dst, attr in edges:
        g.add_edge(Edge(src, dst, **attr))
    return g
Example #59
0
    def graph(self, tables, skip_tables=()):
        graph = Dot(**self.graph_options)
        for table in tables:
            if table.name in skip_tables:
                continue

            graph.add_node(
                Node(
                    self.quote(table.name),
                    label=self.node_table(
                        table.name,
                        self._table_columns(table),
                        self._table_indices(table),
                    ),
                    **self.style["node"],
                )
            )
            for fk in table.foreign_keys:
                fk_table = fk.column.table
                if fk_table not in tables or fk_table.name in skip_tables:
                    continue
                is_single_parent = fk.parent.primary_key or fk.parent.unique
                options = self.style["edge"].copy()
                options["arrowtail"] = "empty" if is_single_parent else "crow"
                options["dir"] = "both"
                if fk.parent.primary_key and fk.column.primary_key:
                    # Inheritance relationship
                    edge = fk_table.name, table.name
                    options["arrowhead"] = "none"
                    options["tailport"] = fk.column.name
                    options["headport"] = fk.parent.name
                else:
                    edge = table.name, fk_table.name
                    options["arrowhead"] = "odot"
                    options["tailport"] = fk.parent.name
                    options["headport"] = fk.column.name
                graph.add_edge(Edge(*map(self.quote, edge), **options))
        return graph