コード例 #1
0
ファイル: NetViz.py プロジェクト: stephenjwilson/MeTeOR
def main(root, title=''):
    net = ReadNetwork(root)  # from Networks folder

    #print "Writing dot..."
    write_dot(net, '{}/MeTeOR{}.dot'.format(root, title))
    #write_dot(nonNormnet,'{}/MeTeOR_nonNorm{}.dot'.format(root,title))
    render(root, title)
コード例 #2
0
def main():
    device_dict = device_to_dict(node)
    static_device_set = get_static_devic_set(device_dict)
    device_list = read_device_without_noise(static_device_set)
    dict = connectivity_at_certain_time(time_switch(int(sys.argv[1])), device_list, node);
    G = dict_to_graph(dict)
    write_dot(G, 'graph.dot')
コード例 #3
0
    def extract_forward_slice(self, slice_root_node, function_name):
        slice_graph = nx.ego_graph(self.simplified_pdg_graph,
                                   slice_root_node,
                                   radius=100000)
        forward_slice_graph = nx.DiGraph()
        forward_slice_graph.add_nodes_from(slice_graph.nodes(data=True))
        forward_slice_graph.add_weighted_edges_from(slice_graph.edges)
        slice_name = function_name + '_{}'.format(
            self.slice_root_nodes[slice_root_node])
        slice_file = self.add_dependency_prefix(slice_name + '.fs.dot')
        write_dot(forward_slice_graph, slice_file)

        # elapsed_time = default_timer() - self.start_time
        # write_file(self.fs_time_file.format(slice_name), '{}'.format(elapsed_time))

        # self.start_time = default_timer()
        self.get_abstract_slice(forward_slice_graph, slice_root_node,
                                function_name,
                                self.slice_root_nodes[slice_root_node])
        elapsed_time = default_timer() - self.start_time
        afs_time_file = join_path(self.pdg_path,
                                  '{}.afs.time.txt'.format(slice_name))
        write_file(afs_time_file, '{}'.format(elapsed_time))
        # self.render_pdf_graph(slice_file)
        return forward_slice_graph, slice_file, slice_name
コード例 #4
0
def draw_graph(stacks):
    import networkx as nx
    import matplotlib.pyplot as plt
    from networkx.drawing.nx_agraph import write_dot, graphviz_layout

    G = nx.DiGraph()

    for l in stacks:
        reductions = l.split("->")
        for i, k in zip(reductions, reductions[1:]):
            G.add_edge(k.replace("cb_explore_adf_", ""),
                       i.replace("cb_explore_adf_", ""))
        if len(reductions) == 1:
            G.add_node(reductions[0])

    plt.figure(num=None,
               figsize=(24, 12),
               dpi=120,
               facecolor='w',
               edgecolor='k')

    write_dot(G, 'graphviz_format.dot')

    pos = graphviz_layout(
        G,
        prog='dot',
        args='-Nfontsize=10 -Nwidth=".2" -Nheight=".2" -Nmargin=0 -Gfontsize=12'
    )
    nx.draw(G, pos, with_labels=True, arrows=True, node_size=1600)
    plt.savefig('reduction_graph.png')
コード例 #5
0
def draw_graph(nodes, edges, graphs_dir, default_lang='all'):
    lang_graph = nx.MultiDiGraph()
    lang_graph.add_nodes_from(nodes)
    for edge in edges:
        if edges[edge] == 0:
            lang_graph.add_edge(edge[0], edge[1])
        else:
            lang_graph.add_edge(edge[0], edge[1], weight=float(edges[edge]), label=str(edges[edge]))

    # print graph info in stdout
    # degree centrality
    print('-----------------\n\n')
    print(default_lang)
    print(nx.info(lang_graph))
    try:
        # When ties are associated to some positive aspects such as friendship or collaboration,
        #  indegree is often interpreted as a form of popularity, and outdegree as gregariousness.
        DC = nx.degree_centrality(lang_graph)
        max_dc = max(DC.values())
        max_dc_list = [item for item in DC.items() if item[1] == max_dc]
    except ZeroDivisionError:
        max_dc_list = []
    # https://ru.wikipedia.org/wiki/%D0%9A%D0%BE%D0%BC%D0%BF%D0%BB%D0%B5%D0%BA%D1%81%D0%BD%D1%8B%D0%B5_%D1%81%D0%B5%D1%82%D0%B8
    print('maxdc', str(max_dc_list), sep=': ')
    # assortativity coef
    AC = nx.degree_assortativity_coefficient(lang_graph)
    print('AC', str(AC), sep=': ')
    # connectivity
    print("Слабо-связный граф: ", nx.is_weakly_connected(lang_graph))
    print("количество слабосвязанных компонент: ", nx.number_weakly_connected_components(lang_graph))
    print("Сильно-связный граф: ", nx.is_strongly_connected(lang_graph))
    print("количество сильносвязанных компонент: ", nx.number_strongly_connected_components(lang_graph))
    print("рекурсивные? компоненты: ", nx.number_attracting_components(lang_graph))
    print("число вершинной связности: ", nx.node_connectivity(lang_graph))
    print("число рёберной связности: ", nx.edge_connectivity(lang_graph))
    # other info
    print("average degree connectivity: ", nx.average_degree_connectivity(lang_graph))
    print("average neighbor degree: ", sorted(nx.average_neighbor_degree(lang_graph).items(),
                                              key=itemgetter(1), reverse=True))
    # best for small graphs, and our graphs are pretty small
    print("pagerank: ", sorted(nx.pagerank_numpy(lang_graph).items(), key=itemgetter(1), reverse=True))

    plt.figure(figsize=(16.0, 9.0), dpi=80)
    plt.axis('off')
    pos = graphviz_layout(lang_graph)
    nx.draw_networkx_edges(lang_graph, pos, alpha=0.5, arrows=True)
    nx.draw_networkx(lang_graph, pos, node_size=1000, font_size=12, with_labels=True, node_color='green')
    nx.draw_networkx_edge_labels(lang_graph, pos, edges)

    # saving file to draw it with dot-graphviz
    # changing overall graph view, default is top-bottom
    lang_graph.graph['graph'] = {'rankdir': 'LR'}
    # marking with blue nodes with maximum degree centrality
    for max_dc_node in max_dc_list:
        lang_graph.node[max_dc_node[0]]['fontcolor'] = 'blue'
    write_dot(lang_graph, os.path.join(graphs_dir, default_lang + '_links.dot'))

    # plt.show()
    plt.savefig(os.path.join(graphs_dir, 'python_' + default_lang + '_graph.png'), dpi=100)
    plt.close()
コード例 #6
0
def select_graph(graph_file_name):
    global INPUT_FOLDER, G, graph_name, all_pairs_sp
    GRAPH_PATH = INPUT_FOLDER + graph_file_name
    input_file_name = os.path.basename(GRAPH_PATH)
    graph_name = input_file_name.split(".")[0]
    print(graph_name)

    # Reading the graphs
    G = nx_read_dot(GRAPH_PATH)  #this should be the default structure
    #if not nx.is_connected(G):
    #    print('The graph is disconnected')
    #    quit()

    # convert ids to integers
    G = nx.convert_node_labels_to_integers(G)

    # Set zero coordinates for all vertices
    for i in nx.nodes(G):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        #if i==0: x, y = 0, 0
        #if i==1: x, y = 1, 1
        #if i==2: x, y = 0, 1
        #if i==3: x, y = 1, 0
        curr_pos = str(x) + "," + str(y)
        nx.set_node_attributes(G, {i: curr_pos}, "pos")

    G = scale_graph(G, 100)
    write_dot(G, OUTPUT_FOLDER + graph_name + '_initial.dot')
    G = scale_graph(G, 1 / 100)
    all_pairs_sp = None
コード例 #7
0
def gen_strings_network(metadata_file, output_file, threshold):
    malware_data = pandas.read_csv(metadata_file, dtype=str, keep_default_na=False, skipinitialspace=True)

    malware_paths = malware_data['FILE'] # where we'll store the malware file paths
    malware_attributes = dict() # where we'll store the malware strings
    graph = networkx.Graph() # the similarity graph

    # filter out any paths that aren't PE files
    malware_paths = filter(pecheck, malware_paths)

    # get and store the strings for all of the malware PE files
    for path in malware_paths:
        attributes = getstrings(path)
        print("Extracted {0} attributes from {1} ...".format(len(attributes),path))
        malware_attributes[hashlib.sha256(open(path, 'rb').read()).hexdigest()] = attributes

        # add each malware file to the graph
        graph.add_node(path,label=hashlib.sha256(open(path, 'rb').read()).hexdigest()[:10])

    # iterate through all pairs of malware
    for malware1,malware2 in itertools.combinations(malware_paths,2):

        # compute the jaccard distance for the current pair
        jaccard_index = jaccard(malware_attributes[hashlib.sha256(open(malware1, 'rb').read()).hexdigest()],malware_attributes[hashlib.sha256(open(malware2, 'rb').read()).hexdigest()])

        # if the jaccard distance is above the threshold add an edge
        if jaccard_index > threshold:
            print(malware1,malware2,jaccard_index)
            graph.add_edge(malware1,malware2)
            # graph.add_edge(malware1,malware2,penwidth=1+(jaccard_index-threshold)*10)

    # write the graph to disk so we can visualize it
    write_dot(graph, output_file)
コード例 #8
0
ファイル: main.py プロジェクト: ghlin-daniel/SocialMining
def main():
    keyword, number = get_arguments()

    g = nx.DiGraph()

    api = twitter.Api(
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        access_token_key=access_token_key,
        access_token_secret=access_token_secret,
    )

    tweets = []

    while len(tweets) < number:
        max_id = "" if len(tweets) == 0 else ("&max_id=" + tweets[-1].id_str)
        count = "&count=100"
        query = "q=" + keyword + count + max_id

        next_tweets = api.GetSearch(raw_query=query)
        if len(tweets) != 0 and tweets[-1].id_str == next_tweets[0].id_str:
            next_tweets.pop(0)

        tweets += next_tweets

    for tweet in tweets:
        rt_sources = get_rt_sources(tweet.text)
        if not rt_sources:
            continue
        for rt_source in rt_sources:
            if rt_source[0] == '@':
                rt_source = rt_source[1:]
            g.add_edge(rt_source, tweet.user.screen_name)

    write_dot(g, "keyword.dot")
コード例 #9
0
def visualize_mdp(mdp, filename):

    log.info('in the visualize_mdp function')
    import pydot
    import networkx as nx
    from networkx.drawing.nx_agraph import write_dot

    G = nx.DiGraph()

    for s in mdp['states']:
        for a in s['actions']:
            for t in a['transitions']:
                ecolor = 'red' if a['id'] else 'green'
                elabel = 'p={}, r={}'.format(t['probability'], t['reward'])
                G.add_edge(s['id'], t['to'], color=ecolor, label=elabel)

    log.info('writing dot file')
    write_dot(G, filename.replace('.json', '.dot'))
    g = pydot.graph_from_dot_file(filename.replace('.json', '.dot'))[0]

    log.info('writing png from dot file')
    g.write_png(filename.replace('.json', '.png'))

    log.info('removing intermediate dot file')
    os.remove(filename.replace('.json', '.dot'))
    return filename.replace('.json', '.png')
コード例 #10
0
    def to_dot(self, dest_path):
        if not dest_path.endswith('.dot'):
            dest_path += '.dot'

        write_dot(self.g, dest_path)

        return dest_path
コード例 #11
0
def plot_graph_and_sol(graph_copy, solution):
    G = nx.Graph()
    for edge in graph_copy._edges:
        G.add_edge(edge._u,
                   edge._v,
                   label='{}={}'.format(edge._weights, edge.get_weight()))
    edge_path_shortest = []
    edge_path_second = []
    if len(solution._paths) >= 1:
        path = solution._paths[1]._path
        edge_path_shortest = [(u, v) for (u, v, d) in G.edges(data=True)
                              if any([u, v] == path[i:i + 2]
                                     for i in range(len(path) - 1)) or any(
                                         [v, u] == path[i:i + 2]
                                         for i in range(len(path) - 1))]
    for (u, v) in edge_path_shortest:
        G[u][v]['color'] = 'red'
        G[v][u]['color'] = 'red'

    if len(solution._paths) >= 2:
        path = solution._paths[2]._path
        edge_path_second = [(u, v) for (u, v, d) in G.edges(data=True)
                            if any([u, v] == path[i:i + 2]
                                   for i in range(len(path) - 1)) or any(
                                       [v, u] == path[i:i + 2]
                                       for i in range(len(path) - 1))]
    for (u, v) in edge_path_second:
        if G[u][v].get('color') == 'red' or G[v][u].get('color') == 'red':
            G[u][v]['color'] = '0.5:blue;0.5:red'
            G[v][u]['color'] = '0.5:blue;0.5:red'
        else:
            G[u][v]['color'] = 'blue'
            G[v][u]['color'] = 'blue'

    write_dot(G, 'my_g.dot')
コード例 #12
0
    def __init__(self):
        self.parser = None
        self.args = None
        self.project = None
        self.cfg = None

        self._create_parser()
        self.args = self.parser.parse_args()

        self.ext = 'svg'
        self.fname = ''
        if self.args.outfile:
            self.fname, self.ext = os.path.splitext(self.args.outfile)
            if self.ext != '.svg' and self.ext != '.dot':
                l.error(
                    'Wrong output file foramt! Only support for .svg and .dot')
                raise Exception('Invalid Input')

        self._create_cfg()
        if self.ext == '.dot':
            write_dot(self.cfg.graph, self.args.outfile)
            l.info("CFG is exported to " + self.args.outfile)
        else:
            self._postprocess_cfg()
            if self.fname:
                endpoint = CFGVisEndpoint('cfg', self.cfg)
                for addr in self.addrs:
                    endpoint.serve(addr, fname=self.fname)
            else:
                self._launch()
                self.app = CFGExplorer(start_url='/api/cfg/%#08x' %
                                       self.addrs[0],
                                       port=self.args.port)
                self.add_endpoints()
コード例 #13
0
def debug_graph(gr, name="G"):
    """Some documentation"""
    write_dot(gr, '%s.dot' % name)
    lines = open('%s.dot' % name).read().split("\n")
    new_lines = []
    last_node = None
    for l in lines:
        if len(l.strip()) == 0:
            continue
        if l.strip()[0] == '"' and l.find("->") < 0:
            last_node = []
            last_node.append(l)
        elif (last_node is not None) and (l.find("];") < 0):
            last_node.append(l)
        elif (last_node is not None) and (l.find("];") > 0):
            last_node.append(l)
            subject = [l1.split("subject=")[1].split("]")[0] for l1 in last_node if l1.find("subject=") >= 0][0]
            new_lines.append("subgraph cluster_%s {" % subject)
            for l1 in last_node:
                new_lines.append(l1)
            new_lines.append("}")            
            last_node = None
        else:
            new_lines.append(l)
    open('%s.cluster.dot' % name, "w").write("\n".join(new_lines))
            
    #os.system('dot -Tpdf %s.dot -o %s.pdf' % (name, name))
    os.system('dot -Tpng %s.cluster.dot -o %s.png' % (name, name))
    return ("%s.png" % name)
コード例 #14
0
def visualize_mdp(mdp, filename):

    log.info('in the visualize_mdp function')
    import pydot
    import networkx as nx
    from networkx.drawing.nx_agraph import write_dot

    G=nx.DiGraph()

    for s in mdp['states']:
        for a in s['actions']:
            for t in a['transitions']:
                ecolor='red' if a['id'] else 'green'
                elabel='p={}, r={}'.format(t['probability'], t['reward'])
                G.add_edge(s['id'], t['to'],
                        color=ecolor,
                        label=elabel)

    log.info('writing dot file')
    write_dot(G, filename.replace('.json', '.dot'))
    g = pydot.graph_from_dot_file(filename.replace('.json', '.dot'))[0]

    log.info('writing png from dot file')
    g.write_png(filename.replace('.json', '.png'))

    log.info('removing intermediate dot file')
    os.remove(filename.replace('.json', '.dot'))
    return filename.replace('.json', '.png')
コード例 #15
0
ファイル: RandomDAGGen.py プロジェクト: Arka2009/pyWattex
def DAGGen(path, N, p, id):
    """
        Erdos-Renyi Random digraph (sort of)
        with number of Nodes = N and edge
        connection probability = p
    """
    G = nx.DiGraph()
    for u in range(N):
        b = random.choice(ut.BENCH)
        G.add_node(u,
                   bench=b,
                   aet=ut.AETDICT[b],
                   bet=ut.BETDICT[b],
                   ap=ut.APDICT[b],
                   bp=ut.BPDICT[b],
                   stack=1,
                   alloc=0)

    rv = bernoulli(p)

    for u in range(N):
        for v in range(u + 1, N):
            if rv.rvs() == 1:
                G.add_edge(u, v)

    if nx.is_directed_acyclic_graph(G):
        print(f'The Digraph{N}_{id} is acyclic')

    nxdr.write_dot(G, f'{path}/random{N}_{id}.dot')
コード例 #16
0
 def draw_graph(self):
     if self.debug >= 2:
         print(nx.info(self.G))
     if self.pos == "spring":
         pos = nx.spring_layout(self.G)
     elif self.pos == "circular":
         pos = nx.circular_layout(self.G)
     elif self.pos == "kamada":
         pos = nx.kamada_kawai_layout(self.G)
     elif self.pos == "random":
         pos = nx.random_layout(self.G)
     elif self.pos == "shell":
         pos = nx.shell_layout(self.G)
     elif self.pos == "spectral":
         pos = nx.spectral_layout(self.G)
     else:
         pos = nx.spring_layout(self.G)
     # plt.subplots_adjust(left=0.0, bottom=0.0, right=1.0,
     #                     top=1.0, wspace=0.0, hspace=0.0)
     if self.debug >= 1:
         print("Drawing graph nodes...")
     draw_graph_nodes(self.G, self.paths, pos, col_path, self.draw_grey)
     if self.debug >= 1:
         print("Drawing graph edges...")
     draw_graph_edges(self.G, self.paths, pos, col_path, self.draw_grey)
     # nx.draw_networkx_labels(self.G, pos)
     # plt.axis('off')
     if self.debug >= 1:
         print("Displaying graph")
     write_dot(self.G, self.pos + "_lemin.dot")
     # plt.show()
     write_dot(self.G, 'file.dot')
コード例 #17
0
    def export_graph_as_dot(self,
                            dependency_graph_between_bbl,
                            levels=None,
                            suffix='',
                            save_as_pdf=False):
        if levels is None:
            levels = {}

        if dependency_graph_between_bbl.order():
            self.log_handler.info(
                "Starting dumping BB graph dependencies as PDF...")
            from networkx.drawing.nx_agraph import write_dot
            import networkx as nx

            if len(levels) > 0:
                for i in levels.keys():
                    val = levels[i]
                    for nd in val:
                        if i % 2:
                            dependency_graph_between_bbl.node[nd][
                                'color'] = 'red'
                        else:
                            dependency_graph_between_bbl.node[nd][
                                'color'] = 'blue'

            base_file_name = self.prefix + 'depthGraph_bb' + suffix + self.appName
            write_dot(dependency_graph_between_bbl, base_file_name + '.dot')

            if save_as_pdf:
                import pydot
                (graph, ) = pydot.graph_from_dot_file(base_file_name + '.dot')
                graph.write_pdf(base_file_name + '.pdf')
        else:
            self.log_handler.error("Cannot export graph as a PDF.")
コード例 #18
0
def _plot_trackers(
        sender_ids,  # type: List[Union[Text, List[Event]]]
        output_file,  # type: Optional[Text]
        endpoint,  # type: EndpointConfig
        unconfirmed=None  # type: Optional[List[Event]]
):
    """Create a plot of the trackers of the passed sender ids.

    This assumes that the last sender id is the conversation we are currently
    working on. If there are events that are not part of this active tracker
    yet, they can be passed as part of `unconfirmed`. They will be appended
    to the currently active conversation."""

    if not output_file or not sender_ids:
        # if there is no output file provided, we are going to skip plotting
        # same happens if there are no sender ids
        return None

    event_sequences = _fetch_events(sender_ids, endpoint)

    if unconfirmed:
        event_sequences[-1].extend(unconfirmed)

    graph = visualize_neighborhood(event_sequences[-1],
                                   event_sequences,
                                   output_file=None,
                                   max_history=2)

    from networkx.drawing.nx_agraph import write_dot
    write_dot(graph, output_file)
コード例 #19
0
    def draw_a_grn(self,
                   grn,
                   is_to_save=True,
                   save_path="",
                   file_name="",
                   with_labels=False):
        # drawing
        # pos = nx.spring_layout(grn)
        if isinstance(grn, list):
            grn = self.generate_directed_grn(grn)
        pos = nx.circular_layout(grn)

        node_no = len(grn.nodes())
        partition = {}
        for i in range(node_no):
            partition[i] = int(i / 5)

        # pos = draw_communities.community_layout(grn, partition)
        nx.draw(grn,
                pos,
                node_color=partition.values(),
                with_labels=with_labels,
                edge_color=nx.get_edge_attributes(grn, 'color').values())

        if not is_to_save:
            plt.show()
        if is_to_save:
            if save_path == "" or file_name == "":
                raise Exception('Save path is not specified.')
            plt.savefig(save_path + os.sep + file_name)
            plt.close()

            for key, value in np.ndenumerate(grn):
                grn.node[key[0]]['pos'] = value
            write_dot(grn, save_path + os.sep + 'directed_graph.dot')
コード例 #20
0
    def draw_graph(self):
        G = nx.DiGraph()
        stack = [(self.root, 1, 0, '')]
        ni = 2
        labels = {}
        edge_labels = {}
        while len(stack) > 0:
            node, curr, parent, weight = stack.pop()
            G.add_node(curr)
            labels[curr] = node.attr_id if len(self.labels) == 0 or len(node.value) == 0 \
                                        else self.labels[node.attr_id]
            if parent != 0:
                G.add_edge(parent, curr)
                edge_labels[(parent, curr)] = weight
            for v in node.value:
                stack.append((node.value[v], ni, curr, v))
                ni += 1

        write_dot(G, 'test.dot')

        plt.title('Decision Tree')
        pos = graphviz_layout(G, prog='dot')
        nx.draw(G,
                pos,
                node_color='pink',
                arrows=True,
                labels=labels,
                node_size=2000)
        nx.draw_networkx_edge_labels(G,
                                     pos,
                                     edge_labels=edge_labels,
                                     font_color='red')
        plt.show()
コード例 #21
0
 def tranform_swaps_to_cnots(self):
     ghw = self.G_hw
     swapIDs = []
     for gate_id in ghw.nodes:
         dt = ghw.nodes[gate_id]['details']
         if not dt['type'] == "SWAP":
             continue
         swapIDs.append(gate_id)
     for gate_id in swapIDs:
         dt = ghw.nodes[gate_id]['details']
         q1 = dt['hwqubits'][0]
         q2 = dt['hwqubits'][1]
         if q2 in self.mDigraph[q1]:
             #q1->q2 exists
             #swap sequence (q1->q2), (q2->q1), (q1->q2)
             newQ1 = q1
             newQ2 = q2
         elif q1 in self.mDigraph[q2]:
             #q2->q1 exists
             #swap sequence (q2->q1), (q1->q2), (q2->q1)
             newQ1 = q2
             newQ2 = q1
         parentsL = [p for p in ghw.predecessors(gate_id)]
         childrenL = [c for c in ghw.successors(gate_id)]
         ctime0 = dt['startTime']
         connect1, ctime1 = self._add_direction_aware_cnot(
             ghw, newQ1, newQ2, parentsL, ctime0)
         connect2, ctime2 = self._add_direction_aware_cnot(
             ghw, newQ2, newQ1, connect1, ctime1)
         connect3, ctime3 = self._add_direction_aware_cnot(
             ghw, newQ1, newQ2, connect2, ctime2)
         self._connect_nodes_all_pairs(ghw, connect3, childrenL)
         ghw.remove_node(gate_id)
         assert (ctime3 - ctime0 == dt['duration'])
     write_dot(self.G_hw, "G_final.dot")
コード例 #22
0
 def extract_forward_slice_chunk_bb(self, forward_slice_graph,
                                    slice_root_node, function_name,
                                    window_size):
     basic_block_data_flow_graph = nx.DiGraph()
     bb_nodes = defaultdict(list)
     # root_basic_block = forward_slice_graph.nodes[slice_root_node]['basic_block_id']
     # start_time = default_timer()
     # seen_nodes = []
     # print 'start building bb graph ...'
     for node in forward_slice_graph.nodes:
         #seen_nodes.append(node)
         if 'basic_block_id' in forward_slice_graph.nodes[node]:
             node_basic_block_id = forward_slice_graph.nodes[node][
                 'basic_block_id']
             bb_nodes[str(node_basic_block_id)].append(node)
             basic_block_data_flow_graph.add_node(node_basic_block_id)
         for succ_node in forward_slice_graph.successors(node):
             #if su
             if 'basic_block_id' in forward_slice_graph.nodes[node]:
                 succ_node_basic_block_id = forward_slice_graph.nodes[
                     succ_node]['basic_block_id']
                 if node_basic_block_id != succ_node_basic_block_id:
                     basic_block_data_flow_graph.add_edge(
                         node_basic_block_id, succ_node_basic_block_id)
     # elapsed_time = default_timer() - start_time
     # print 'BB graph finding', elapsed_time
     # print 'End building bb graph!'
     bbgraph_name = '{}_{}.bbgraph.dot'.format(
         self.pdg_graph_file, self.slice_root_nodes[slice_root_node])
     bbgraph_name = self.add_dependency_prefix(bbgraph_name)
     write_dot(basic_block_data_flow_graph, bbgraph_name)
     self.get_all_subgraphs_size(basic_block_data_flow_graph,
                                 forward_slice_graph, window_size,
                                 slice_root_node, function_name, bb_nodes)
コード例 #23
0
 def draw_entity_disambiguation_graph(self):
     print 'Drawing Entity Disambiguation Graph......',
     graph_name = self.graph_path + 'edg' + str(self.table_number)
     write_dot(self.miniEDG, graph_name + '.dot')
     os.system('dot -Tsvg ' + graph_name + '.dot' + ' -o ' + graph_name +
               '.svg')
     print 'Done!'
コード例 #24
0
def export_graph(graph_to_export, name_of_file_without_extension):

    # Possibilities: adjlist, dot, edgelist, gexf, gml, graphml, multiline_adjlist, pajek, yaml
    write_dot(graph_to_export, name_of_file_without_extension + ".dot")
    logger.info("File saved: %s", name_of_file_without_extension + ".dot")

    return
コード例 #25
0
def draw_graph(G,edgeLabels):

# G = nx.DiGraph()


# figure(num=None, figsize=(4, 4), dpi=80, facecolor='w', edgecolor='k')

# write dot file to use with graphviz
    # run "dot -Tpng test.dot >test.png"
    write_dot(G,'test.dot')

    # same layout using matplotlib with no labels
    plt.title('draw_networkx')
    pos =graphviz_layout(G, prog='dot')
    # nx.draw(G, pos, with_labels=True, arrows=True, font_size=8)
    nx.draw(G, pos, with_labels=True, arrows=True, font_size=8, node_size=8)

    nx.draw_networkx_edge_labels(G,pos,edge_labels=edgeLabels,font_color='red',font_size=8)
    # for v in G.node():
    #     print(len(v))


    # nx.draw(G, with_labels=True,  arrows=True)
    # nx.draw_networkx_nodes(G, pos, node_size=[len(v) * 100 for v in G.nodes()])

    plt.show()
コード例 #26
0
def visualizegraphalgorithm(algorithm, name, nodes, edges, scale):
    G = nx.Graph()
    G.add_nodes_from(nodes)
    try:
        G.add_weighted_edges_from(edges, color = 'black')
    except:
        G.add_edges_from(edges, color = 'black')
    algorithms = {"prim":graphListFromPrim, "kruskal":graphListFromKruskal, "depthsearch":graphListFromDepthSearch}
    graphlist = algorithms[algorithm](G)

    for graph in graphlist:
        setlabelweight(graph)

    importantlines.append(r"%END" + "\n")
    importantlines.append(r"\begin{frame}" + "\n")
    if not os.path.exists(name):
        os.mkdir(name)
    parentpath = os.getcwd()
    os.chdir(name)
    for index, graph in enumerate(graphlist):
        filename = 'graph' + str(index) + '.dot'
        filenametikz = 'graph' + str(index) + '.tex'
        write_dot(graph, filename)
        os.system(f"dot -Txdot {filename} | dot2tex -ftikz -tmath --figonly --tikzedgelabels --prog=circo --graphstyle='scale={scale}, auto' > {filenametikz}")#--nodeoptions scale=0.5
        importantlines.append(f"\\only<{index+1}>" + "{\n")
        importantlines.append(r"\input{" + name + "/" + filenametikz + "}\n")
        importantlines.append("}\n")
    importantlines.append(r"\end{frame}")
    os.chdir(parentpath)
    with open(plotframe, "w") as f:
        f.writelines(importantlines)
コード例 #27
0
    def draw(self, method="graphviz", title="knowledge_graph", save_path=""):
        """
        This function helps to visualize a network DiGraph

        :param method: which visualization method to use
        :param title: name of the graph
        :param save_path: save directory
        :return:
        """
        if method == "graphviz":
            # There are two ways to visualize networkx graph
            # 1. write dot file to use with graphviz
            # run "dot -Tpng test.dot > test.png"
            dot_path = os.path.join(save_path, '{}.dot'.format(title))
            png_path = os.path.join(save_path, '{}.png'.format(title))
            write_dot(self.graph, dot_path)
            cmd = 'dot -Tpng {} > {}'.format(dot_path, png_path)
            os.system(cmd)
        elif method == "matplotlib":
            # 2. same layout using matplotlib with no labels
            # Not so good
            plt.title(title)
            pos = graphviz_layout(self.graph, prog='dot')
            nx.draw(self.graph, pos, with_labels=False, arrows=True)
            plt.savefig(os.path.join(save_path, 'nx_test.png'))
コード例 #28
0
def plot_image_hierarchy(fig: plt.Figure, G: nx.Graph):
    # Adapted from https://stackoverflow.com/questions/53967392/creating-a-graph-with-images-as-nodes
    ax = fig.gca()
    ax.set_aspect('equal', adjustable='box')

    write_dot(G, 'test.dot')
    pos = graphviz_layout(G, prog='dot', args='-Nsep="+1000,+1000";')
    nx.draw_networkx_edges(G, pos, ax=ax)

    trans = ax.transData.transform
    trans2 = fig.transFigure.inverted().transform

    piesize = 0.2  # this is the image size
    p2 = piesize / 2.0
    for n in G:
        xx, yy = trans(pos[n])  # figure coordinates
        xa, ya = trans2((xx, yy))  # axes coordinates
        a = plt.axes([xa - p2, ya - p2, piesize, piesize])
        a.set_aspect('equal')
        a.imshow(plt.imread(G.node[n]['image']))
        a.set_xticks([])
        a.set_yticks([])
        # a.set_title(n.name)

    ax.axis('off')
コード例 #29
0
ファイル: graph.py プロジェクト: LuciaVG/adv-db
    def breadthFirstSearch(self, root):
        from collections import deque
        import networkx as nx
        from networkx.drawing.nx_agraph import write_dot, graphviz_layout
        import matplotlib.pyplot as plt

        G = nx.DiGraph()
        G.add_node(root)

        visited_nodes = list()
        visited_nodes.append(root)
        queue = deque([root])
        tree = dict()

        while len(queue) > 0:
            node = queue.popleft()
            adj_nodes = self.nodes[node].adj_nodes.keys()
            remaining_elements = set(adj_nodes).difference(set(visited_nodes))
            tree[node] = list(remaining_elements)
            if len(remaining_elements) > 0:
                for elem in sorted(remaining_elements):
                    G.add_node(elem)
                    G.add_edge(node, elem)
                    visited_nodes.append(elem)
                    queue.append(elem)

        write_dot(G, 'bfs_tree.dot')
        plt.title('BFS TREE')
        pos = graphviz_layout(G, prog='dot')
        nx.draw(G, pos, with_labels=False, arrows=True)
        plt.savefig('bfs_tree.png')
コード例 #30
0
ファイル: Graph.py プロジェクト: lhchen42/DecisionTree-ID3-
    def draw(self,
             dot_file_name='default.dot',
             image_file_name='default.png',
             font_size=5,
             node_size=200,
             line_width=0.25,
             label_size=5):
        write_dot(self.G, dot_file_name)
        plt.figure()
        plt.title(self.title)

        # layout setting, hierarchical
        pos = graphviz_layout(self.G, prog='dot')
        nx.draw(self.G,
                pos,
                with_labels=True,
                arrows=False,
                node_size=node_size,
                font_size=font_size,
                width=line_width)
        nx.draw_networkx_edge_labels(self.G,
                                     pos,
                                     edge_labels=self.edge_labels,
                                     font_size=label_size)

        # output
        plt.show()
        plt.savefig(image_file_name, dpi=1000)
コード例 #31
0
def save_graphviz(g, fname):
    dag = g.copy()
    counter = defaultdict(int)
    for n in dag.nodes(True):
        n[1]["shape"] = "box"
        if dag.in_degree(n[0]) == 0:
            counter["raw"] += 1
            n[1]["color"] = "blue"
        if dag.out_degree(n[0]) == 0:
            n[1]["color"] = "deeppink"

            if dag.in_degree(n[0]) > 0:
                counter["output"] += 1

        if dag.out_degree(n[0]) > 0 and dag.in_degree(n[0]) > 0:
            counter["intermediate"] += 1

    pprint.pprint(counter)

    for n in dag.nodes(True):
        if not dag.has_node(n[0]):
            continue
        contract_inputs(dag, n)

    write_dot(dag, fname)
コード例 #32
0
def _plot_multi_graph(G):
    """Given a multigraph G, produces a corresponding visualization

    Returns void
    """
    write_dot(G, 'blockchain/multi_blockchain.dot')
    subprocess.call("./convert.sh", shell=True)
コード例 #33
0
def domain_graphs_with_subgraphs(nodes, edges, subgraphs, graphs_dir, language):
    domain_graph = nx.MultiDiGraph()
    domain_graph.add_nodes_from(nodes)
    for edge in edges:
        domain_graph.add_edge(edge[0], edge[1], weight=float(edges[edge]), label=str(edges[edge]))

    for lang, nodelist in subgraphs:
        domain_graph.subgraph(nodelist, name=lang)
        # subgr.name = lang

    # saving file to draw it with dot-graphviz
    # changing overall graph view, default is top-bottom
    domain_graph.graph['graph'] = {'rankdir': 'LR'}
    write_dot(domain_graph, os.path.join(graphs_dir, 'domains_' + language + '_links.dot'))
コード例 #34
0
ファイル: read_write.py プロジェクト: swatisaoji1/ipy_twitter
def write_dot_output(graphml_file, outfile=''):
    """
    This function takes the graphml file and writes the dot file for visualization

    :param graphml_file: Input file in graphml format
    :param outfile: Output file name
    :return: path of output file
    """
    graph = nx.read_graphml(graphml_file) # read the graphml file to a graph

    # use the infile name and path if no outfile is specified
    if outfile == '':
       outfile = change_extension('.dot', graphml_file)
    write_dot(graph, outfile)
    print('dot file written to {0}'.format(outfile))
コード例 #35
0
ファイル: graph_utils.py プロジェクト: Chadi-akel/cere
def plot(g, step=""):
    import os
    for n,d in g.nodes(data=True):
        d["label"]="{} {} {} ({})".format(n, d['_name'], d['_self_coverage'], d['_coverage'])
        if not d['_valid'] or not d['_tested']: d["style"]="dotted"
        else: d["style"]="solid"
        if d['_tested']:
            d["style"]="solid"
            if not d['_matching']: d['color']="red"
            else: d['color']="green"
        if d['_to_test']: d['color']="orange"
    for u,v,d in g.edges(data=True):
        d["label"] = round(d["weight"], 2)
    write_dot(g,"{0}/graph_{1}.dot".format(var.CERE_PROFILE_PATH, step))
    try:
        logger.debug(subprocess.check_output("dot -Tpdf {0}/graph_{1}.dot -o {0}/graph_{1}.pdf".format(var.CERE_PROFILE_PATH, step), stderr=subprocess.STDOUT, shell=True))
    except subprocess.CalledProcessError as err:
        logger.error(str(err))
        logger.error(err.output)
        logger.error("Cannot create the pdf")
コード例 #36
0
ファイル: plot.py プロジェクト: ncullen93/neuroBN
def plot_gv(bn, save=False):
	def execute(command):
		command = ' '.join(command)
		process = subprocess.Popen(command, 
			shell=True, 
			stdout=subprocess.PIPE, 
			stderr=subprocess.STDOUT)

		output = process.communicate()[0]
		returncode = process.returncode

		if returncode == 0:
			return True, output
		else:
			return False, output

	cmd = ['mkdir' , 'neuroBN/plotting/images']
	p = execute(cmd)

	G = nx.DiGraph(bn.E)
	write_dot(G,"neuroBN/plotting/images/bn.dot")

	cmd = ['/usr/local/bin/dot', '-Tpng' , 
	'neuroBN/plotting/images/bn.dot', 
	'>neuroBN/plotting/images/bn.png']

	p = execute(cmd)
	plt.figure(facecolor="white")
	img=mpimg.imread('neuroBN/plotting/images/bn.png')
	_img = plt.imshow(img, aspect='auto')
	plt.axis('off')
	plt.show(_img)

	if not save:
		cmd = ['rm' , '-r', 'neuroBN/plotting/images']
		p = execute(cmd)
	else:
		cmd = ['rm', '-r', 'neuroBN/plotting/images/bn.dot']
		p = execute(cmd)
コード例 #37
0
 def plotGraph(self):
     
     self.G = nx.DiGraph()
     for i in range(0,self.mutalInformationMatrix.shape[0]+1):
         self.G.add_node(i)
     self.G.add_edges_from(self.graphMst)
     #ADD THE EDGE FOR THE LABEL NODE TOO
     for i in range(0,self.mutalInformationMatrix.shape[0]):
         self.G.add_edge(self.X_train.shape[1]-1,i)
     
     self.renameNodes()
     pos=nx.spring_layout(self.G)
     nx.draw_networkx_nodes(self.G,pos)
     nx.draw_networkx_labels(self.G,pos)
     nx.draw_networkx_edges(self.G,pos)
     plt.show()
     #A=A=nx.to_agraph(G)
     pos = graphviz_layout(self.G)
     #nx.draw(G,pos)
     #plt.show()
     write_dot(self.G,'BayesNet4.dot')
     print 'so far so good'
コード例 #38
0
ファイル: plot.py プロジェクト: ncullen93/neuroBN
def iplot(bn, h=350, w=450):
	"""
	Inline Plotting of a BayesNet object
	"""
	def execute(command):
		command = ' '.join(command)
		process = subprocess.Popen(command, 
			shell=True, 
			stdout=subprocess.PIPE, 
			stderr=subprocess.STDOUT)

		output = process.communicate()[0]
		returncode = process.returncode

		if returncode == 0:
			return True, output
		else:
			return False, output

	cmd = ['mkdir' , 'neuroBN/plotting/images']
	p = execute(cmd)

	G = nx.DiGraph(bn.E)
	write_dot(G,"neuroBN/plotting/images/bn.dot")

	cmd = ['/usr/local/bin/dot', '-Tpng' , 
	'neuroBN/plotting/images/bn.dot', 
	'>neuroBN/plotting/images/bn.png']
	p = execute(cmd)
	
	im = Image.open("neuroBN/plotting/images/bn.png")
	out = im.resize((w,h))

	cmd = ['rm' , '-r', 'neuroBN/plotting/images']
	p = execute(cmd)

	return out
コード例 #39
0
ファイル: graphs.py プロジェクト: navnidhirajput/pymatgen
    def draw_graph_to_file(self, filename="graph",
                           diff=None,
                           hide_unconnected_nodes=False,
                           hide_image_edges=True,
                           edge_colors=False,
                           node_labels=False,
                           weight_labels=False,
                           image_labels=False,
                           color_scheme="VESTA",
                           keep_dot=False,
                           algo="fdp"):
        """
        Draws graph using GraphViz.

        The networkx graph object itself can also be drawn
        with networkx's in-built graph drawing methods, but
        note that this might give misleading results for
        multigraphs (edges are super-imposed on each other).

        If visualization is difficult to interpret,
        `hide_image_edges` can help, especially in larger
        graphs.

        :param filename: filename to output, will detect filetype
        from extension (any graphviz filetype supported, such as
        pdf or png)
        :param diff (StructureGraph): an additional graph to
        compare with, will color edges red that do not exist in diff
        and edges green that are in diff graph but not in the
        reference graph
        :param hide_unconnected_nodes: if True, hide unconnected
        nodes
        :param hide_image_edges: if True, do not draw edges that
        go through periodic boundaries
        :param edge_colors (bool): if True, use node colors to
        color edges
        :param node_labels (bool): if True, label nodes with
        species and site index
        :param weight_labels (bool): if True, label edges with
        weights
        :param image_labels (bool): if True, label edges with
        their periodic images (usually only used for debugging,
        edges to periodic images always appear as dashed lines)
        :param color_scheme (str): "VESTA" or "JMOL"
        :param keep_dot (bool): keep GraphViz .dot file for later
        visualization
        :param algo: any graphviz algo, "neato" (for simple graphs)
        or "fdp" (for more crowded graphs) usually give good outputs
        :return:
        """

        if not which(algo):
            raise RuntimeError("StructureGraph graph drawing requires "
                               "GraphViz binaries to be in the path.")

        # Developer note: NetworkX also has methods for drawing
        # graphs using matplotlib, these also work here. However,
        # a dedicated tool like GraphViz allows for much easier
        # control over graph appearance and also correctly displays
        # mutli-graphs (matplotlib can superimpose multiple edges).

        g = self.graph.copy()

        g.graph = {'nodesep': 10.0, 'dpi': 300, 'overlap': "false"}

        # add display options for nodes
        for n in g.nodes():

            # get label by species name
            label = "{}({})".format(str(self.structure[n].specie), n) if node_labels else ""

            # use standard color scheme for nodes
            c = EL_COLORS[color_scheme].get(str(self.structure[n].specie.symbol), [0, 0, 0])

            # get contrasting font color
            # magic numbers account for perceived luminescence
            # https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
            fontcolor = '#000000' if 1 - (c[0] * 0.299 + c[1] * 0.587
                                          + c[2] * 0.114) / 255 < 0.5 else '#ffffff'

            # convert color to hex string
            color = "#{:02x}{:02x}{:02x}".format(c[0], c[1], c[2])

            g.add_node(n, fillcolor=color, fontcolor=fontcolor, label=label,
                       fontname="Helvetica-bold", style="filled", shape="circle")

        edges_to_delete = []

        # add display options for edges
        for u, v, k, d in g.edges(keys=True, data=True):

            # retrieve from/to images, set as origin if not defined
            to_image = d['to_jimage']

            # set edge style
            d['style'] = "solid"
            if to_image != (0, 0, 0):
                d['style'] = "dashed"
                if hide_image_edges:
                    edges_to_delete.append((u, v, k))

            # don't show edge directions
            d['arrowhead'] = "none"

            # only add labels for images that are not the origin
            if image_labels:
                d['headlabel'] = "" if to_image == (0, 0, 0) else "to {}".format((to_image))
                d['arrowhead'] = "normal" if d['headlabel'] else "none"

            # optionally color edges using node colors
            color_u = g.node[u]['fillcolor']
            color_v = g.node[v]['fillcolor']
            d['color_uv'] = "{};0.5:{};0.5".format(color_u, color_v) if edge_colors else "#000000"

            # optionally add weights to graph
            if weight_labels:
                units = g.graph.get('edge_weight_units', "")
                if d.get('weight'):
                    d['label'] = "{:.2f} {}".format(d['weight'], units)

            # update edge with our new style attributes
            g.edges[u, v, k].update(d)

        # optionally remove periodic image edges,
        # these can be confusing due to periodic boundaries
        if hide_image_edges:
            for edge_to_delete in edges_to_delete:
                g.remove_edge(*edge_to_delete)

        # optionally hide unconnected nodes,
        # these can appear when removing periodic edges
        if hide_unconnected_nodes:
            g = g.subgraph([n for n in g.degree() if g.degree()[n] != 0])

        # optionally highlight differences with another graph
        if diff:
            diff = self.diff(diff, strict=True)
            green_edges = []
            red_edges = []
            for u, v, k, d in g.edges(keys=True, data=True):
                if (u, v, d['to_jimage']) in diff['self']:
                    # edge has been deleted
                    red_edges.append((u, v, k))
                elif (u, v, d['to_jimage']) in diff['other']:
                    # edge has been added
                    green_edges.append((u, v, k))
            for u, v, k in green_edges:
                g.edges[u, v, k].update({'color_uv': '#00ff00'})
            for u, v, k in red_edges:
                g.edges[u, v, k].update({'color_uv': '#ff0000'})

        basename, extension = os.path.splitext(filename)
        extension = extension[1:]

        write_dot(g, basename+".dot")

        with open(filename, "w") as f:

            args = [algo, "-T", extension, basename+".dot"]
            rs = subprocess.Popen(args,
                                  stdout=f,
                                  stdin=subprocess.PIPE, close_fds=True)
            rs.communicate()
            if rs.returncode != 0:
                raise RuntimeError("{} exited with return code {}.".format(algo, rs.returncode))

        if not keep_dot:
            os.remove(basename+".dot")
コード例 #40
0
ファイル: toolbook.py プロジェクト: lpp1985/lpp_Script
def display_networkx_graph(g):
    dot_file = _to_tmp_file('.dot')
    write_dot(g, dot_file)
    display_dot(dot_file)
コード例 #41
0
#!/usr/bin/python

import networkx as nx
from networkx.drawing.nx_agraph import read_dot
from networkx.drawing.nx_agraph import write_dot
from networkx.readwrite import json_graph

#G = read_dot("all_nobreak.dot")
g = read_dot("inkscape-mergecycl.dot")
cycles = list(nx.simple_cycles(g))
print cycles
for n1 in g.nodes_iter():
    if g.has_edge(n1, n1):
        g.remove_edge(n1, n1)
    for n2 in g.successors(n1):
        for n3 in g.successors(n2):
            for n4 in nx.dfs_preorder_nodes(g, n3):
                if g.has_edge(n1, n4):
                    g.remove_edge(n1, n4)    
#nx.write_graphml(g, "inkscape-reduced.graphml")
write_dot(g,"inkscape-reduced.dot")
nx.write_gml(g,"inkscape-reduced.gml")
g = nx.convert_node_labels_to_integers(g)
data = json_graph.node_link_data(g)
print data

#import json
#serial = json.dumps(data)
#print serial 

コード例 #42
0
ファイル: plot_write_dotfile.py プロジェクト: jg-you/networkx
#    Aric Hagberg <*****@*****.**>
#    Dan Schult <*****@*****.**>
#    Pieter Swart <*****@*****.**>
#    All rights reserved.
#    BSD license.

import networkx as nx

# and the following code block is not needed
# but we want to see which module is used and
# if and why it fails
try:
    import pygraphviz
    from networkx.drawing.nx_agraph import write_dot
    print("using package pygraphviz")
except ImportError:
    try:
        import pydot
        from networkx.drawing.nx_pydot import write_dot
        print("using package pydot")
    except ImportError:
        print()
        print("Both pygraphviz and pydot were not found ")
        print("see  https://networkx.github.io/documentation/latest/reference/drawing.html")
        print()
        raise

G = nx.grid_2d_graph(5, 5)  # 5x5 grid
write_dot(G, "grid.dot")
print("Now run: neato -Tps grid.dot >grid.ps")
コード例 #43
0
def makeNetworkDot(networkGraph):
    # Constants definition
    # List of load types with a generation icon.
    GENERATION_TYPES=['G']
    # List of load types with an industrial icon.
    INDUSTRIAL_TYPES=['I1','I2','I3']

    # Map where keys are bus ids and value graphviz ids.
    idToGvId={}

    # Style of the graph
    g=networkx.Graph()

    # Create nodes
    unknownCount=len(networkGraph.nodes())
    transformersCount=0
    for n,ndata in networkGraph.nodes(data=True):
        # Take care of unknown nodes
        id=None
        internalId=id
        try:
            internalId=ndata["id"]
            id="BUS%s"%ndata["internalId"]
        except KeyError:
            unknownCount-=1
            internalId="?"
            id="BUS%s"%(-unknownCount)
        idToGvId[n]=id

        # Add styled node
        g.add_node(id,{"xlabel":internalId,"shape":"box","style":"filled","color":"#000000","fixedsize":"true","width":0.5,"height":0.075,"label":" ","fontsize":10,"id":id})

        # Transformer
        tId=None
        try:
            if SHOW_TRANSFORMERS and len(ndata["transformers"]) >= 1:
                t=ndata["transformers"][0]
                transformersCount+=1

                tId="TF%s"%t.internalId
                g.add_node(tId,{"xlabel":t.id,"shape":"circle","style":"filled","color":"#000000","fixedsize":"true","width":0.15,"height":0.15,"label":" ","fontsize":10})
                g.add_edge(id,tId,{"weight":10000})
        except KeyError:
            pass

        # Load
        load=ndata["load"]
        if load is not None:
            loadId="LOAD%s"%load.internalId
            edgeAttr={"weight":10000}
            if load.loadType in GENERATION_TYPES:
                g.add_node(loadId,{"label": "~","shape":"circle","style":"bold","color":"#000000","fixedsize":"true", "penwidth":2, "width":0.2, "height":0.2,"fontsize":18})
            elif load.loadType in INDUSTRIAL_TYPES:
                g.add_node(loadId,{"xlabel":load.loadType,"label":" ","shape":"invtriangle","color":"#000000","fixedsize":"true", "penwidth":2, "width":0.2, "height":0.2,"portPos":"n","fontsize":10})
                edgeAttr["tailport"]="n"
            else:
                g.add_node(loadId,{"xlabel":load.loadType,"label":" ","style":"filled","shape":"invtriangle","color":"#000000","fixedsize":"true", "penwidth":2, "width":0.2, "height":0.2,"portPos":"n","fontsize":10})
                edgeAttr["tailport"]="n"

            # Connect the load
            if tId is None:
                g.add_edge(id,loadId,edgeAttr)
            else:
                g.add_edge(tId,loadId,edgeAttr)

    # Create edges
    for u,v,edata in networkGraph.edges(data=True):
        if not edata["closed"]:
            continue
        uId=idToGvId[u]
        vId=idToGvId[v]
        if not g.has_edge(uId,vId):
            w=10/(1+edata["length"])
            g.add_edge(uId,vId,{"label":"     .     ","fontsize":10, "id": "LINE%s"%(edata["internalId"]+1)})

    # Detect cycles
    try:
        cycles = networkx.cycle_basis(g)
        for cy in cycles:
            networkx.set_node_attributes(g,'color',dict(zip(cy,['#FF0000']*len(cy))))
            print([g.node[b]['xlabel'] for b in cy])
        print("Cycles: %d" %len(cycles))
    except Exception as e:
        print("Error %s" % e)

    if not networkx.is_connected(g):
        print("Warning: graph is not connected.")
        components = [c for c in sorted(networkx.connected_components(g), key=len, reverse=True)]
        print("Warning: graph contains %d connected components:" % len(components))
        print(components)

    write_dot(g,'network.gv')

    # Convert to pdf and gml
    with open(os.devnull, 'wb') as devnull:
        subprocess.check_call(['neato', '-Tsvg', '-onetwork.svg', 'network.gv'], stdout=devnull, stderr=subprocess.STDOUT)
        subprocess.check_call(['neato', '-Tpdf', '-onetwork.pdf', 'network.gv'], stdout=devnull, stderr=subprocess.STDOUT)
        subprocess.check_call(['gv2gml', '-onetwork.gml', 'network.gv'], stdout=devnull, stderr=subprocess.STDOUT)
コード例 #44
0
ファイル: gml2dot.py プロジェクト: felicedeluca/GraphMetrics
    ''' Extracts the attributes of the vertices and edges from
    the gml data structure
    <tt>return</tt> the graph with the standard dot attributes'''

    graphics = nx.get_node_attributes(G, "graphics")
    # print(graphics)

    for k in graphics.keys():

        pos = str(graphics[k]['x'])  +"," + str(graphics[k]['y'])
        nx.set_node_attributes(G, {k:pos}, 'pos')
        nx.set_node_attributes(G, {k:""}, "graphics")
        nx.set_node_attributes(G, {k:k}, "label")

    G = nx.Graph(G)

    return G



g_path = sys.argv[1]
outputpath = sys.argv[2]
g_name = os.path.basename(g_path).split(".")[0]


# Reading graph and subgraph
G = nx.read_gml(g_path)
G = nx.Graph(G)
G = set_graph_properties(G)
write_dot(G, outputpath)