def main():
    # Create a directed graph
    G = nx.DiGraph()

    # An example
    l=[ ('a','b'),
        ('b','c'),
        ('c','d'),
        ('d','e'),
        ('e','f'),
        ('w','x'),
        ('w','t'),
        ('t','q'),
        ('q','r'),
        ('q','u')]

    # Build up a graph
    for t in l:
        G.add_edge(t[0], t[1])

    # Plot trees
    pos = graphviz_layout(G, prog='dot')
    nx.draw(G, pos, with_labels=True, arrows=False)

    plt.savefig('draw_trees_with_pygraphviz.png', bbox_inches='tight')   
    plt.show()
Example #2
0
    def draw(self, zscale=1):
        for n in self.history:
            if len(self.history[n]) % 2 != 0:
                self.history[n].append(self.turn)
        pos = graphviz_layout(self.wot, "twopi")

        for n in self.history:
            periods = list(zip(self.history[n], self.history[n][1:]))
            for i, p in enumerate(periods):
                nbpoints = abs(p[1] - p[0])*zscale
                zline = linspace(p[0]*zscale, p[1]*zscale, nbpoints)
                xline = linspace(pos[n][0], pos[n][0], nbpoints)
                yline = linspace(pos[n][1], pos[n][1], nbpoints)
                plot = self.ax.plot(xline, zline, yline, zdir='y', color=self.colors[n][0], alpha=1/(i % 2 + 1))

        for link in self.past_links:
            nbpoints = abs(pos[link[2]][0] - pos[link[1]][1])*zscale
            zline = linspace(link[0]*zscale, link[0]*zscale, nbpoints)
            xline = linspace(pos[link[2]][0], pos[link[1]][0], nbpoints)
            yline = linspace(pos[link[2]][1], pos[link[1]][1], nbpoints)
            if link[1] in self.colors:
                self.ax.plot(xline, zline, yline, zdir='y', color=self.colors[link[1]][0], alpha=0.1)

            #txt = self.ax.text(pos[n][0], pos[n][1], self.history[n][0]*zscale, n[:5], 'z')

        self.ax.set_xlim3d(-5, max([p[0] for p in pos.values()]))
        self.ax.set_ylim3d(-5, max([p[1] for p in pos.values()]))
        self.ax.set_zlim3d(-5, (self.turn+1)*zscale)
Example #3
0
def draw_comm_detection_res(graph):
    pos = graphviz_layout(graph)
    color_list = ['r', 'g', 'b', 'y']
    comm_dict, partition = get_comm_dict_and_partition(graph)

    # nodes
    for comm_id in comm_dict:
        nx.draw_networkx_nodes(graph, pos, nodelist=comm_dict[comm_id],
                               node_color=color_list[comm_id], node_size=500, alpha=0.8)

    # edges
    def get_edge_dict():
        edge_dict = {}
        for comm_id in comm_dict:
            edge_dict[comm_id] = []

        for edge in graph.edges():
            if partition[edge[0]] == partition[edge[1]]:
                edge_dict[partition[edge[0]]].append(edge)
        return edge_dict

    edge_list_dict = get_edge_dict()
    nx.draw_networkx_edges(graph, pos, width=4.0, alpha=0.5, edge_color='grey')
    for comm_id in edge_list_dict:
        print comm_id, color_list[comm_id], edge_list_dict[comm_id]
        nx.draw_networkx_edges(graph, pos, edgelist=edge_list_dict[comm_id],
                               width=8, alpha=0.5, edge_color=color_list[comm_id])

    # labels
    nx.draw_networkx_labels(graph, pos, font_size=16)
    plt.axis('off')
    plt.savefig('./karate_partition.pdf', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.savefig('./karate_partition.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #4
0
    def drawTrie(trie_dict):
        """
        Draws the trie structure of the PHT from dictionnary.

        @param trie_dict: Dictionnary of index entries (prefix -> entry).
        @type  trie_dict: dict
        """
        prefixes = list(trie_dict.keys())
        if len(prefixes) == 0:
            return

        edges = list([])
        for prefix in prefixes:
            for i in range(-1, len(prefix)-1):
                u = prefix[:i+1]
                x = ("." if i == -1 else u, u+"0")
                y = ("." if i == -1 else u, u+"1")
                if x not in edges:
                    edges.append(x)
                if y not in edges:
                    edges.append(y)

        # TODO: use a binary tree position layout...
        #   UPDATE : In a better way [change lib]
        G = nx.Graph(sorted(edges, key=lambda x: len(x[0])))
        plt.title("PHT: Tree")
        pos=graphviz_layout(G,prog='dot')
        nx.draw(G, pos, with_labels=True, node_color='white')
        plt.show()
Example #5
0
def draw_graph(graph):
    pos = graphviz_layout(graph)
    nx.draw(graph, pos, width=4.0, alpha=0.5, edge_color='grey',
            node_color='white', node_size=500, with_labels=True)
    plt.axis('off')
    plt.savefig('./origin_graph_before_lp' '.pdf', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.savefig('./origin_graph_before_lp' '.png', bbox_inches='tight', pad_inches=0, transparent=True)
Example #6
0
    def get_tree_layout(self, connected_component):
        layout = None
        tree = self.get_underlying_tree(connected_component)
        try:
            # Nice circular layout if you have graphviz
            from networkx.drawing.nx_agraph import graphviz_layout
            layout = graphviz_layout(tree, prog='twopi', root=str(tree.root))

            # Scale to fit grid, since twopi seems to ignore the size option
            min_x = min(pos[0] for pos in layout.values())
            max_x = max(pos[0] for pos in layout.values())
            min_y = min(pos[1] for pos in layout.values())
            max_y = max(pos[1] for pos in layout.values())

            center_x = min_x + (max_x - min_x) / 2
            center_y = min_y + (max_y - min_y) / 2
            # Re-center, scale and shift to fit the desired bounding box
            try:
                x_scale = (0.5 - self.layout_margin - 0.005) / (center_x - min_x)
            except ZeroDivisionError:
                x_scale = 1
            try:
                y_scale = (0.5 - self.layout_margin - 0.005) / (center_y - min_y)
            except ZeroDivisionError:
                y_scale = 1
            for vert, pos in layout.iteritems():
                layout[vert] = ((pos[0] - center_x) * x_scale + 0.5,
                        (pos[1] - center_y) * y_scale + 0.5)

        except ImportError:
            # Spring layout if you do not have grahpviz
            layout = nx.spring_layout(tree, scale=1-2*self.layout_margin-0.01,
                    center=(0.5, 0.5))
        return layout
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()
Example #8
0
def visualize(graphs, viz_path, args_viz_format):
  import matplotlib
  from networkx.drawing.nx_agraph import graphviz_layout
  meta_graph = networkx.Graph()
  for graph in graphs:
    add_graph(meta_graph, graph)
  pos = graphviz_layout(meta_graph)
  networkx.draw(meta_graph, pos)
  if viz_path:
    ext = os.path.splitext(viz_path)[1]
    if ext == '.dot':
      viz_format = 'graphviz'
    elif ext == '.png':
      viz_format = 'png'
  else:
    viz_format = args_viz_format
  if viz_format == 'graphviz':
    from networkx.drawing.nx_pydot import write_dot
    assert viz_path is not None, 'Must provide a filename to --visualize if using --viz-format "graphviz".'
    base_path = os.path.splitext(viz_path)
    write_dot(meta_graph, base_path+'.dot')
    run_command('dot', '-T', 'png', '-o', base_path+'.png', base_path+'.dot')
    logging.info('Wrote image of graph to '+base_path+'.dot')
  elif viz_format == 'png':
    if viz_path is None:
      matplotlib.pyplot.show()
    else:
      matplotlib.pyplot.savefig(viz_path)
Example #9
0
def main( ):
    g = nx.DiGraph( )
    # Add x nodes.
    xs = np.arange(0, 11)
    ys = np.arange(0, 11)

    [ g.add_node( 'x=%d' % i, w = i ) for i in xs ]
    [ g.add_node( 'y=%d' % i, w = i ) for i in ys ]

    # Now add edges.
    img = { }
    for x in xs:
        for z in [1,2,3]:
            y = (x + z) % 11
            g.add_edge( 'x=%d' % x, 'y=%d' % y, label='z=%d' % z, prob=1/3)
            print( '%d + %d -> %d' % (x, z, y) )
            img[ (x, y) ] = 1/3

    mat = np.zeros( shape=(11,11) )
    for k, v in img.items( ):
        mat[ k ] = v

    nx.draw_networkx( g, pos = graphviz_layout( g, 'dot' ) )
    nx.drawing.nx_agraph.write_dot( g, 'network.dot' )
    plt.savefig( 'graph.png' )
Example #10
0
def draw_comm_detection_res(graph):
    pos = graphviz_layout(graph)
    color_list = ['r', 'g', 'b', 'y']
    comm_dict, partition = get_comm_dict_and_partition()

    # nodes
    for comm_id in comm_dict:
        nx.draw_networkx_nodes(graph, pos, nodelist=comm_dict[comm_id],
                               node_color=color_list[comm_id], node_size=500, alpha=0.8)

    nx.draw_networkx_nodes(graph, pos, nodelist=[9, 11],
                           node_color='w', node_size=500, alpha=0.8)

    nx.draw_networkx_nodes(graph, pos, nodelist=[31], node_color='y', node_size=500, alpha=0.8)

    nx.draw_networkx_nodes(graph, pos, nodelist=[0], node_color='magenta', node_size=500, alpha=0.8)

    nx.draw_networkx_edges(graph, pos, width=4.0, alpha=0.5, edge_color='grey')

    # labels
    nx.draw_networkx_labels(graph, pos, font_size=16)
    plt.axis('off')
    plt.savefig('./clique_percolation_karate_partition.pdf', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.savefig('./clique_percolation_karate_partition.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
def plot_graph(graph):
    from networkx.drawing.nx_agraph import graphviz_layout
    pos = graphviz_layout(graph, prog='dot')

    for h in nx.connected_component_subgraphs(graph.to_undirected()):
        nx.draw(h, pos, with_labels=True)

    plt.show()
Example #12
0
def visualize_a_graph(graph):
    pos = graphviz_layout(graph)
    nx.draw(graph, pos, width=4.0, alpha=0.5, edge_color='grey',
            node_color='white', node_size=500, with_labels=True)

    nx.draw_networkx_edge_labels(graph, pos)
    nx.draw_networkx_labels(graph, pos)
    plt.axis('off')
    plt.show()
Example #13
0
def plot_nx(bn,**kwargs):
	"""
	Draw BayesNet object from networkx engine
	"""
	g = nx.DiGraph(bn.E)
	pos = graphviz_layout(g,'dot')
	#node_size=600,node_color='w',with_labels=False
	nx.draw_networkx(g,pos=pos, **kwargs)
	plt.axis('off')
	plt.show()
Example #14
0
    def graph_traditional_object_file(self, nodes_to_ignore):
        if len(self.object_list) == 0:
            return

        G = nx.DiGraph()

        node_rooms_list = []
        node_labels = {}

        for o in self.object_list:
            if o.parent_id in nodes_to_ignore:
                nodes_to_ignore.append(o.object_id)
                continue
            elif o.object_id in nodes_to_ignore:
                continue

            G.add_node(o.object_key())

            if ROOM_ATTRIBUTE_KEY in (int(attribute) for attribute in o.attributes):
                node_rooms_list.append(o.object_key())
                node_labels[o.object_key()] = o.description

        for o in self.object_list:
            if o.object_key() not in G.nodes():
                continue

            if o.parent_id != 0 and o.object_id not in nodes_to_ignore:
                parent_key = self.object_list[o.parent_id - 1].object_key()

                node_key_to_get = node_labels.get(parent_key, "")
                if node_key_to_get is not "":
                    node_labels[parent_key] = node_key_to_get + '\n\t' + o.description
                else:
                    node_labels[parent_key] = o.description

            for direction, node in o.directions.items():
                if node < len(self.object_list):
                    G.add_edge(self.object_list[node-1].object_key(), o.object_key(), direction=direction)

        pos = graphviz_layout(G, prog='dot')

        edge_labels = dict([((u, v,), d['direction'])
                            for u, v, d in G.edges(data=True)])

        nx.draw_networkx_nodes(G, pos, nodelist=node_rooms_list,
                               node_color="b", node_size=5000, alpha=0.8)

        nx.draw_networkx_edges(G, pos)

        nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=8)

        nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8, alpha=0.5)

        plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
        plt.show()
Example #15
0
 def one_layout(self, func, kwargs):
     """Calculates one arbitrary layout"""
     if 'fixed' in kwargs.keys():
         if not kwargs['fixed'] is None:
             kwargs['pos'] = nx.random_layout(self.G, dim=kwargs['dim'])
     if func == 'dh_spring_layout':
         return self.dh_spring_layout(self.G, **kwargs)
     elif func == 'draw_graphviz':
         return graphviz_layout(self.G, **kwargs)
     else:
         return getattr(nx, func)(self.G, **kwargs)
Example #16
0
def desenhaGrafo(G,pngfilename): # desenha o grafo e salva numa imagem png
    edge_labels=dict([((u,v,),d['weight']) # gera os labels das arestas
                    for u,v,d in G.edges(data=True)])
    colors = [G[u][v]['color'] for u,v in G.edges()]
    pos = graphviz_layout(G,prog='neato') # obtem a posicao dos nos (para desenhalo) # TODO: desativar isso?
    nx.draw_networkx_edges(G,pos, edge_color=colors) # desenha as arestas
    nx.draw_networkx_labels(G,pos) # desenha os labels das arestas
    nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels) # desenha os labels dos nos
    nx.draw_networkx_nodes(G,pos,node_color='w') # desenha os nos
    plt.axis('off') # desativa os eixos
    plt.savefig(pngfilename)
    plt.close("all")
Example #17
0
    def plot(self, savepath=None):
        """
        Draw this individual.
        """

        from networkx.drawing.nx_agraph import graphviz_layout

        fig = plt.figure(figsize=(40, 30))

        tree = self.tree  # type: nx.DiGraph
        pos = graphviz_layout(tree, root=0, prog='dot')

        node_list = tree.nodes(data=True)
        edge_list = tree.edges(data=True)

        node_labels = {
            x[0]: '%s: %s\n%s' % (str(x[1]['node_id']), str(x[1]['label']), '%s/%s' % (
                str(x[1]['inst_correct']), str(x[1]['inst_total'])) if x[1]['terminal'] else '')
            for x in node_list
        }
        node_colors = [x[1]['color'] for x in node_list]
        edge_labels = {(x1, x2): d['threshold'] for x1, x2, d in edge_list}

        nx.draw_networkx_nodes(tree, pos, node_size=1000, node_color=node_colors)  # nodes
        nx.draw_networkx_edges(tree, pos, edgelist=edge_list, style='dashed')  # edges
        nx.draw_networkx_labels(tree, pos, node_labels, font_size=16)  # node labels
        nx.draw_networkx_edge_labels(tree, pos, edge_labels=edge_labels, font_size=16)

        plt.text(
            0.2,
            0.9,
            '\n'.join([
                'individual id: %03.d' % self.ind_id,
                'height: %d' % self.height,
                'n_nodes: %d' % self.n_nodes,
                'train accuracy: %0.4f' % self.train_acc_score,
                'val accuracy: %0.4f' % self.val_acc_score,
                'test accuracy: %0.4f' % self.test_acc_score if self.test_acc_score is not None else '',
                'iteration: %d' % self.iteration if self.iteration is not None else ''

            ]),
            fontsize=15,
            horizontalalignment='right',
            verticalalignment='top',
            transform=fig.transFigure
        )

        plt.axis('off')

        if savepath is not None:
            plt.savefig(savepath, bbox_inches='tight', format='pdf')
            plt.close()
Example #18
0
def vis_input(graph):
    pos = graphviz_layout(graph)
    nx.draw(graph, with_labels=True, pos=pos, font_size=16, node_size=600, alpha=0.8, width=4,
            edge_color='grey', node_color='white')

    edge_dict = {}
    for edge in graph.edges():
        edge_dict[edge] = graph[edge[0]][edge[1]]['w']

    nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_dict, font_size=14, alpha=0.1, font_color='blue')
    plt.axis('off')
    plt.savefig('./demo_graph.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #19
0
    def draw_circular_graph(self):
        """ Draws a circular graph from the previously computed graph
        :return: None
        """

        pos = graphviz_layout(self.graph, prog="twopi", args="")
        plt.figure(figsize=(8, 8))
        nx.draw(self.graph, pos, node_size=20, alpha=0.5, node_color="blue", with_labels=False)
        nx.draw_networkx_labels(self.graph, pos, self.labels, font_size=6)

        plt.axis("equal")
        plt.savefig("circular_tree.png")
        plt.show()
Example #20
0
def draw_link_graph(graph):
    pos = graphviz_layout(graph)
    # pos = nx.circular_layout(graph)
    nx.draw(graph, pos, width=4.0, alpha=0.5, edge_color='grey', node_size=2000, node_shape='s', with_labels=True,
            font_size=20, node_color='w')

    edge_dict = {}
    for edge in graph.edges():
        edge_dict[edge] = str(list(set(edge[0]).intersection(set(edge[1]))))

    # nx.draw_networkx_edge_labels(graph, pos,edge_labels=edge_dict, font_size=6)
    plt.axis('off')
    plt.savefig('./link_graph.pdf', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.savefig('./link_graph.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #21
0
def draw_label_propagation_graph(graph, comm_dict, number):
    pos = graphviz_layout(graph)
    nx.draw(graph, pos, width=4.0, alpha=0.5, edge_color='grey',
            node_color='white', node_size=500, with_labels=True)

    color_dict = {6: 'red', 8: 'green', 10: 'blue', 13: 'yellow', 27: 'pink', 29: 'purple', 31: 'black', 32: 'orange',
                  33: 'cyan'}

    for idx, comm in comm_dict.iteritems():
        nx.draw_networkx_nodes(graph, pos, nodelist=comm, node_color=color_dict[idx], node_size=500, alpha=0.5)

    plt.axis('off')
    plt.savefig('./label_propagation_iter' + str(number) + '.pdf', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.savefig('./label_propagation_iter' + str(number) + '.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #22
0
def vis_post_output(graph, comm_list, color_list):
    pos = graphviz_layout(graph)
    nx.draw(graph, with_labels=True, pos=pos, font_size=16, node_size=600, alpha=0.8, width=4,
            edge_color='grey', node_color='white')

    for idx, comm in enumerate(comm_list):
        nx.draw_networkx_nodes(graph, with_labels=True, pos=pos, font_size=16, node_size=600, alpha=0.8, width=4,
                               node_color=color_list[idx], nodelist=comm)

    edge_dict = {}
    for edge in graph.edges():
        edge_dict[edge] = graph[edge[0]][edge[1]]['w']

    nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_dict, font_size=14, alpha=0.1, font_color='blue')
    plt.axis('off')
    plt.savefig('./output_post_graph' + '.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #23
0
    def draw_lanl_graph(self):
        """ Draws a lanl graph from the previously computed graph.
        :return: None
        """
        lanl_graph = sorted(nx.connected_component_subgraphs(self.graph), key=len, reverse=True)[0]

        plt.figure(figsize=(8, 8))
        # use graphviz to find radial layout
        pos = graphviz_layout(self.graph, prog="twopi", root=self.root)
        # draw nodes, coloring by rtt ping time
        nx.draw(self.graph, pos, with_labels=False, alpha=0.5, node_size=15)
        # adjust the plot limits
        xmax = 1.02 * max(xx for xx, yy in pos.values())
        ymax = 1.02 * max(yy for xx, yy in pos.values())
        plt.xlim(0, xmax)
        plt.ylim(0, ymax)
        # plt.show()
        plt.savefig("lanl_routes.png")
Example #24
0
    def dbg_draw(self, filename):
        """
        Draw the graph and save it to a PNG file.
        """
        import matplotlib.pyplot as pyplot  # pylint: disable=import-error
        from networkx.drawing.nx_agraph import graphviz_layout  # pylint: disable=import-error

        tmp_graph = networkx.DiGraph()
        for from_block, to_block in self.transition_graph.edges():
            node_a = "%#08x" % from_block.addr
            node_b = "%#08x" % to_block.addr
            if node_b in self._ret_sites:
                node_b += "[Ret]"
            if node_a in self._call_sites:
                node_a += "[Call]"
            tmp_graph.add_edge(node_a, node_b)
        pos = graphviz_layout(tmp_graph, prog='fdp')   # pylint: disable=no-member
        networkx.draw(tmp_graph, pos, node_size=1200)
        pyplot.savefig(filename)
Example #25
0
def handle_file(path):
    i = open(path, "r")
    lines = i.readlines()
    g = nx.parse_edgelist(lines[1:], nodetype=int, data=(('weight', float),))
    x1, x2, vol = lines[0].rstrip("\n").split(" ")
    i.close()
    x1 = int(x1)
    x2 = int(x2)
    vol = float(vol)
    g.add_node(0)
    g.add_weighted_edges_from([(0, x1, 0), (0, x2, 0)])
    calculate(g, vol)

    # create directed graph for presentation
    result = nx.DiGraph()
    result.add_nodes_from(nx.nodes(g))
    for x1, x2 in nx.edges(g):
        sx1, sx2 = tuple(sorted((x1, x2)))
        if g[x1][x2]['current'] > 0:
            result.add_edge(sx1, sx2, current=g[x1][x2]['current'])
        else:
            result.add_edge(sx2, sx1, current=-g[x1][x2]['current'])

    pos = graphviz_layout(result, prog='sfdp',)
    nx.draw_networkx_nodes(result, pos=pos, node_size=250, node_color='white')

    edges = nx.edges(result)
    currents_dict = nx.get_edge_attributes(result, 'current')
    currents_list = tuple(currents_dict[e] for e in edges)
    widths_list = tuple(0.3 + 4 * x/max(currents_list) for x in currents_list)
    colors = ("green", "yellow", "red")
    colors_list = [colors[(math.floor(len(colors)*x/max(widths_list) - 0.1))]
                   for x in widths_list]
    for key, val in currents_dict.items():
        currents_dict[key] = "{:.1f}".format(val)
    nx.draw_networkx_edges(result, pos=pos, edgelist=edges, width=widths_list,
                           edge_color=colors_list)
    bbox_props = dict(boxstyle="square,pad=0", fc="none", ec='none', lw=2)
    nx.draw_networkx_edge_labels(result, pos=pos, edge_labels=currents_dict,
                                 font_size=8, bbox=bbox_props)
    nx.draw_networkx_labels(result, pos=pos, font_size=8)
    plt.get_current_fig_manager().full_screen_toggle()
    plt.show()
def plot_network_with_results(psstc, model, time=0):
    G = create_network(psstc)

    fig, axs = plt.subplots(1, 1, figsize=(12, 9))
    ax = axs

    line_color_dict = dict()
    hour = 0
    for i, b in branch_df.iterrows():
        if model.ThermalLimit[i] != 0:
            line_color_dict[(b['F_BUS'], b['T_BUS'])] = round(abs(model.LinePower[i, hour].value / model.ThermalLimit[i]), 2)
        else:
            line_color_dict[(b['F_BUS'], b['T_BUS'])] = 0

    gen_color_dict = dict()
    hour = 0
    for i, g in generator_df.iterrows():
        gen_color_dict[(i, g['GEN_BUS'])] = round(abs(model.PowerGenerated[i, hour].value / model.MaximumPowerOutput[i]), 2)

    color_dict = line_color_dict.copy()
    color_dict.update(gen_color_dict)

    edge_color = list()

    for e in G.edges():
        try:
            edge_color.append( color_dict[(e[0], e[1])] )
        except KeyError:
            edge_color.append( color_dict[(e[1], e[0])] )

    ax.axis('off')
    pos = graphviz_layout(G, prog='sfdp')
    nx.draw_networkx_nodes(G, pos, list(generator_df.index),)
    nx.draw_networkx_nodes(G, pos, list(bus_df.index), node_color='black',)
    edges = nx.draw_networkx_edges(G, pos, edge_color=edge_color, edge_cmap=cmap, width=3)
    nx.draw_networkx_edge_labels(G, pos, edge_labels=color_dict)

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("left", size="5%", pad=0.05)
    cb = plt.colorbar(edges, cax=cax)
    cax.yaxis.set_label_position('left')
    cax.yaxis.set_ticks_position('left')
Example #27
0
def main(fname):
    
    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return None
    
    # convert J to specified adjacency
    J = 1.*(convert_adjacency(cells, spacing, J, adj=ADJ) != 0)
    
    G = nx.Graph(J)
    for k in G:
        G.node[k]['w'] = 1./len(G[k])**2
        
    pos = graphviz_layout(G)
    nx.draw(G, pos=pos, with_labels=True)
    plt.show()
    
    chlebikova(G)
Example #28
0
def show_tree(J, tree):
    ''' '''
    
    G = nx.Graph(J)
    pos = graphviz_layout(G)
    
    if not tree['children']:
        return
        
    # color nodes
    colors = ['r', 'b', 'g', 'k', 'm', 'c', 'o', 'y']
    for i, child in enumerate(tree['children']):
        inds = child['inds']
        print(inds)
        color = colors[i%len(colors)]
        nx.draw_networkx_nodes(G, pos, nodelist=inds, node_color=color)
    nx.draw_networkx_edges(G, pos)
    plt.show()

    for child in tree['children']:
        inds = child['inds']
        show_tree(J[inds, :][:, inds], child)
Example #29
0
def draw_partitioned_link_graph(graph, comm_list):
    pos = graphviz_layout(graph)
    # pos = nx.circular_layout(graph)

    nx.draw(graph, pos, width=4.0, alpha=0.8, edge_color='grey',
            node_color='white', node_size=2000, node_shape='s', with_labels=True, font_size=20)

    color_list = ['red', 'green', 'blue', 'purple', 'pink', 'orange', 'black', 'orange']
    for idx, comm in enumerate(comm_list):
        print idx, comm
        nx.draw_networkx_nodes(graph, pos, nodelist=comm, node_color=color_list[idx],
                               node_size=2000, node_shape='s', alpha=0.4, font_size=20)

    edge_dict = {}
    for edge in graph.edges():
        edge_dict[edge] = str(list(set(edge[0]).intersection(set(edge[1]))))

    # nx.draw_networkx_edge_labels(graph, pos,edge_labels=edge_dict, font_size=6)
    plt.axis('off')
    plt.savefig('./link_partition_graph.pdf', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.savefig('./link_partition_graph.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #30
0
def try_different_layouts(graph):
    layout_dir = 'graph_layout'
    if not os.path.exists(layout_dir):
        os.mkdir('graph_layout')
    nx.draw_random(graph, with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4, edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'rand.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()

    nx.draw_circular(graph, with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4, edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'circular.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()

    nx.draw_spectral(graph, with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4, edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'spectral.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()

    nx.draw_networkx(graph, with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4, edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'networkx.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()

    nx.draw(graph, pos=graphviz_layout(graph), with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4,
            edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'graphviz.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()

    nx.draw_shell(graph, with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4, edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'shell.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()

    nx.draw_spring(graph, with_labels=True, font_size=16, node_size=500, alpha=0.8, width=4, edge_color='grey')
    plt.axis('off')
    plt.savefig(layout_dir + os.sep + 'spring.png', bbox_inches='tight', pad_inches=0, transparent=True)
    plt.show()
Example #31
0
from os.path import join as pJoin
from networkx.drawing.nx_agraph import graphviz_layout

_myDir = os.path.dirname(os.path.abspath(__file__))
IN_PATH_OMD = pJoin(_myDir, 'superModel Tomorrow.omd')
OUT_PATH_OMD = pJoin(_myDir, 'superModel Tomorrow with latlons.omd')

with open(IN_PATH_OMD, 'r') as jsonFile:
    omd = json.load(jsonFile)
    tree = omd['tree']

# Use graphviz to lay out the graph.
inGraph = feeder.treeToNxGraph(tree)
# HACK: work on a new graph without attributes because graphViz tries to read attrs.
cleanG = nx.Graph(inGraph.edges())
# HACK2: might miss nodes without edges without the following.
cleanG.add_nodes_from(inGraph)
pos = graphviz_layout(cleanG, prog='neato')
# # Charting the feeder in matplotlib:
# feeder.latLonNxGraph(inGraph, labels=False, neatoLayout=True, showPlot=True)
# Insert the latlons.
for key in tree:
    obName = tree[key].get('name', '')
    thisPos = pos.get(obName, None)
    if thisPos != None:
        tree[key]['longitude'] = thisPos[0]
        tree[key]['latitude'] = thisPos[1]

with open(OUT_PATH_OMD, 'w') as outFile:
    json.dump(omd, outFile)
Example #32
0
def visualize_pouct_search_tree(root,
                                max_depth=1,
                                visit_threshold=1,
                                anonymize=False,
                                anonymize_actions=False,
                                anonymize_observations=False,
                                output_file=None,
                                use_dot=False,
                                ax=None):
    """
    Visualize the given tree up to depth `max_depth`. Display nodes
    with number of visits >= `visit_threshold`.

    If anonymize is True, will only display actions as a1,a2,... and observations
    as o1,o2,... .
    """
    relabel_actions = {}
    relabel_observations = {}
    if anonymize:
        anonymize_actions = True
        anonymize_observations = True
    if anonymize_actions or anonymize_observations:
        _build_relabel_dict(root,
                            None,
                            0,
                            relabel_actions,
                            relabel_observations,
                            max_depth=max_depth,
                            visit_threshold=visit_threshold)

        print("---- Action labels ----")
        action_map = {
            relabel_actions[action]: action
            for action in relabel_actions
        }
        for label in sorted(action_map):
            print("%s : %s" % (label, action_map[label]))
        print("---- Observation labels ----")
        observation_map = {
            relabel_observations[ob]: ob
            for ob in relabel_observations
        }
        for label in sorted(observation_map):
            print("%s : %s" % (label, observation_map[label]))

    if not anonymize_actions:
        relabel_actions = {}
    if not anonymize_observations:
        relabel_observations = {}

    # Build a networkx graph.
    G = nx.DiGraph()
    _build_graph(G,
                 root,
                 None,
                 None,
                 0,
                 max_depth=max_depth,
                 visit_threshold=visit_threshold,
                 relabel_actions=relabel_actions,
                 relabel_observations=relabel_observations)
    if use_dot:
        if output_file is None:
            raise TypeError("Please provide output .dot file.")
        nx.nx_agraph.write_dot(G, output_file)
        print("Dot file saved at %s" % output_file)
        print("Please run `dot -Tpng %s > %s.png" % (output_file, output_file))
    else:
        node_labels = {}
        color_map = []
        for node in G.nodes():
            belief_str = ""
            if hasattr(node, "belief"):
                belief_str = " | %d" % len(node.belief)
            if isinstance(node, RootVNode):
                color_map.append("cyan")
                node_labels[node] = "R(%d | %.2f%s)" % (node.num_visits,
                                                        node.value, belief_str)
            elif isinstance(node, VNode):
                color_map.append("yellow")
                node_labels[node] = "V(%d | %.2f%s)" % (node.num_visits,
                                                        node.value, belief_str)
            else:
                color_map.append("orange")
                node_labels[node] = "Q(%d | %.2f)" % (node.num_visits,
                                                      node.value)
        edge_labels = {(edge[0], edge[1]): edge[2]["label"]
                       for edge in G.edges(data=True)}
        edge_widths = [edge[2]["weight"] for edge in G.edges(data=True)]

        pos = graphviz_layout(G, prog='dot')
        nx.draw_networkx(G,
                         pos,
                         node_color=color_map,
                         labels=node_labels,
                         width=edge_widths,
                         font_size=7,
                         ax=ax)
        nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, ax=ax)
        if output_file is None:
            plt.show()
        else:
            plt.savefig(output_file)
Example #33
0
def pathmodel_pathway_picture(asp_code, picture_name, input_filename):
    """
    Create the pathway picture using ASP results code from PathModel inference.

    Args:
        asp_code (str): string containing PathModel results
        picture_name (str): path to the output picture file
        input_filename (str): path to PathModel intermediary file
    """
    DG = nx.DiGraph()

    known_compounds = []
    inferred_compounds = []

    known_reactions = []
    inferred_reactions = []

    absent_molecules = []

    with open(input_filename, 'r') as intermediate_file:
        for answer in ASP(intermediate_file.read(), use_clingo_module=False
                          ).parse_args.by_predicate.discard_quotes:
            for predicate in answer:
                if predicate == "absentmolecules":
                    for atom in answer[predicate]:
                        absent_molecules.append(atom[0])

    for answer in ASP(
            asp_code,
            use_clingo_module=False).parse_args.by_predicate.discard_quotes:
        for predicate in answer:
            for atom in answer[predicate]:
                reaction = atom[0]
                reactant = atom[1]
                product = atom[2]
                if predicate == "reaction":
                    if reactant not in absent_molecules:
                        known_compounds.append(reactant)
                    if product not in absent_molecules:
                        known_compounds.append(product)

                    if product not in absent_molecules and reactant not in absent_molecules:
                        known_reactions.append((reactant, product))
                        DG.add_edge(reactant, product, label=reaction)
                elif predicate == "newreaction":
                    if 'Prediction_' in reactant:
                        inferred_compounds.append(reactant)
                    if 'Prediction_' in product:
                        inferred_compounds.append(product)

                    inferred_reactions.append((reactant, product))
                    DG.add_edge(reactant, product, label=reaction)
    plt.figure(figsize=(25, 25))

    nx.draw_networkx_nodes(DG,
                           graphviz_layout(DG, prog='neato'),
                           nodelist=known_compounds,
                           node_color="green",
                           node_size=3000,
                           node_shape='s',
                           alpha=0.5)
    nx.draw_networkx_nodes(DG,
                           graphviz_layout(DG, prog='neato'),
                           nodelist=inferred_compounds,
                           node_color="blue",
                           node_size=2000,
                           node_shape='s',
                           alpha=0.5)

    nx.draw_networkx_edges(DG,
                           graphviz_layout(DG, prog='neato'),
                           edgelist=known_reactions,
                           edge_color="green",
                           alpha=0.5,
                           width=2.0,
                           arrows=True,
                           arrowstyle='->',
                           arrowsize=14)
    nx.draw_networkx_edges(DG,
                           graphviz_layout(DG, prog='neato'),
                           edgelist=inferred_reactions,
                           edge_color="blue",
                           alpha=0.5,
                           width=2.0,
                           arrows=True,
                           arrowstyle='->',
                           arrowsize=14)
    nx.draw_networkx_labels(DG,
                            graphviz_layout(DG, prog='neato'),
                            font_size=15)

    ax = plt.gca()
    ax.set_axis_off()

    extension = os.path.splitext(picture_name)[1].strip('.')
    plt.savefig(picture_name, dpi=144, format=extension)
Example #34
0
def crunch_graphical_model(pgm_path, path_datasets):
    from networkx.drawing.nx_agraph import graphviz_layout
    import plotly.graph_objs as go
    from plotly.offline import plot

    def build_graph(series):
        G = nx.DiGraph()

        node_labels = dict()

        for node_id in xrange(series.shape[1]):
            probs = series[:, node_id]

            G.add_node(node_id,
                       attr_dict=dict(color=max(probs),
                                      probs='<br>'.join([
                                          '%2.3f : %s' % (y, x)
                                          for x, y in it.izip(columns, probs)
                                      ])))
            parent = get_parent(node_id)
            if parent is not None:
                G.add_edge(parent, node_id)

            node_labels[node_id] = node_id

        return G

    def build_edges(_G):
        edge_trace = go.Scatter(x=[],
                                y=[],
                                line=go.Line(width=0.5, color='#999'),
                                hoverinfo='none',
                                mode='lines',
                                name='edges')

        for edge in _G.edges():
            x0, y0 = pos[edge[0]]
            x1, y1 = pos[edge[1]]
            edge_trace['x'] += [x0, x1, None]
            edge_trace['y'] += [y0, y1, None]

        return edge_trace

    def build_nodes(_G, _generation):
        nodes = _G.nodes(data=True)

        _node_trace = go.Scatter(
            x=[pos[node[0]][0] for node in nodes],
            y=[pos[node[0]][1] for node in nodes],
            name='gen %d' % _generation,
            text=[x[1]['probs'] for x in nodes],
            mode='markers',
            visible=True if _generation == 0 else 'legendonly',
            hoverinfo='text',
            marker=go.Marker(
                showscale=True,
                color=[x[1]['color'] for x in nodes],
                colorscale='RdBu',
                colorbar=dict(
                    title='Assurance',
                    xpad=100,
                ),
                cmin=0.,  # minimum color value
                cmax=1.,  # maximum color value
                cauto=False,  # do not automatically fit color values
                reversescale=False,
                size=15,
                line=dict(width=2)))
        return _node_trace

    sep = '\\' if os.name == 'nt' else '/'

    dataset_name = pgm_path.split(sep)[-1].split('_')[0]

    dataset = load_dataframe(
        os.path.join(path_datasets, dataset_name + '.arff'))
    columns = dataset.columns
    n_columns = dataset.shape[1]
    del dataset

    data = []

    with open(pgm_path, 'r') as f:
        csv_w = csv.reader(f, delimiter=',', quotechar='\"')
        for generation, line in enumerate(csv_w):
            series = np.array(line, dtype=np.float).reshape(
                n_columns,
                -1)  # each row is an attribute, each column a generation

            G = build_graph(series)

            pos = graphviz_layout(G, root=0, prog='dot')

            if generation == 0:
                data.append(build_edges(G))

            node_trace = build_nodes(G, generation)
            data += [node_trace]

        fig = go.Figure(
            data=go.Data(data),
            layout=go.Layout(
                title='Probabilistic Graphical Model<br>Dataset %s' %
                dataset_name,
                titlefont=dict(size=16),
                showlegend=True,
                hovermode='closest',
                xaxis=go.XAxis(showgrid=False,
                               zeroline=False,
                               showticklabels=False),
                yaxis=go.YAxis(showgrid=False,
                               zeroline=False,
                               showticklabels=False),
            ))

        plot(fig, filename=pgm_path.split(sep)[-1] + '.html')
Example #35
0
print('Initial Graph G')
print('Edges: ')
print(G.edges(data=True))
print('Nodes: ')
print(G.nodes(data=True))

n = len(G)
#creating the source sensor node graph with fixed edges
# G = nx.cycle_graph(5)
# G.add_edge(0,1,weight=2)
# G.add_edge(0,3,weight=3)
# G.add_edge(0,4,weight=3)
# G.add_edge(1,2,weight=3)
# G.add_edge(2,3,weight=5)
# G.add_edge(3,4,weight=2)
G.pos = graphviz_layout(G)

#sensor nodes are represented by green and relay nodes by red
print("Green nodes indicate Sensor nodes and Red nodes indicate Relay nodes")
colorvalues = [
    'green' if node[1].get('sensornode') else 'red'
    for node in G.nodes(data=True)
]

#ploting the source sensor node graph G
plt.figure(figsize=(20, 14))
plt.title("Sensor Node Graph G")
pos = G.pos
edge_labels = nx.get_edge_attributes(G, 'length')
nx.draw_networkx_edge_labels(G,
                             pos,
Example #36
0
    def display_graph(self):
        """
        Transform the graph to a `networkx.DiGraph`-structure and display it using `matplotlib` -- if the necessary
        libraries are installed.

        :return: `True` if the graph was drawn, `False` otherwise.
        """
        G = self.create_network_graph()
        if _nx and _plt and G:
            try:
                from networkx.drawing.nx_agraph import graphviz_layout
                pos = graphviz_layout(G, prog='dot')
            except ModuleNotFoundError:
                from networkx.drawing.layout import shell_layout
                pos = shell_layout(G)
            except ImportError:
                from networkx.drawing.layout import shell_layout
                pos = shell_layout(G)
            _plt.subplot(111)
            _plt.axis('off')
            _nx.draw_networkx_nodes(G,
                                    pos,
                                    node_color='r',
                                    node_size=1250,
                                    nodelist=[
                                        v.display_name for v in self.vertices
                                        if v.is_sampled
                                    ])
            _nx.draw_networkx_nodes(G,
                                    pos,
                                    node_color='b',
                                    node_size=1250,
                                    nodelist=[
                                        v.display_name for v in self.vertices
                                        if v.is_observed
                                    ])
            for v in self.vertices:
                _nx.draw_networkx_edges(G,
                                        pos,
                                        arrows=True,
                                        edgelist=[(a.display_name,
                                                   v.display_name)
                                                  for a in v.ancestors])
                if v.condition_ancestors is not None and len(
                        v.condition_ancestors) > 0:
                    _nx.draw_networkx_edges(G,
                                            pos,
                                            arrows=True,
                                            style='dashed',
                                            edge_color='g',
                                            edgelist=[
                                                (a.display_name,
                                                 v.display_name)
                                                for a in v.condition_ancestors
                                            ])
            _nx.draw_networkx_labels(G,
                                     pos,
                                     font_color='w',
                                     font_weight='bold')
            _plt.show()
            return True
        else:
            return False
Example #37
0
def p_statement_if(p):
    '''statement : IF LPAREN comparison RPAREN COLON NAME EQUALS expression SEMICOLON
                 | IF LPAREN comparison RPAREN COLON NAME EQUALS expression SEMICOLON ELSE COLON NAME EQUALS expression SEMICOLON'''
    if p[3]:
        names[p[6]] = p[8]
        if p[12] is not None:
            plt.clf()
            plt.cla()
            b.clear()
            b.add_node('stmt')
            b.add_nodes_from(['if','comp','stmt1','else','stmt2'])
            b.add_edges_from([('stmt','if'),('stmt','comp'),('stmt','stmt1'),('stmt','else'),('stmt','stmt2')])
            b.add_node(p[3])
            b.add_edge('comp',p[3])
            b.add_nodes_from(['name1','equel1','exp1'])
            b.add_edges_from([('stmt1','name1'),('stmt1','equel1'),('stmt1','exp1')])
            b.add_nodes_from(['name2','equel2','exp2'])
            b.add_edges_from([('stmt2','name2'),('stmt2','equel2'),('stmt2','exp2')])
            b.add_node(p[6])
            b.add_node(p[8])
            b.add_edge('name1',p[6])
            b.add_edge('exp1',p[8])
            b.add_node(p[12])
            b.add_node(p[14])
            b.add_edge('name2',p[12])
            b.add_edge('exp2',p[14])
            plt.title("if-else-statement")
            pos = graphviz_layout(b, prog='dot')
            nx.draw(b,pos, with_labels=True, arrows=True, node_size=600)
            plt.show()
            plt.clf()
            plt.cla()
        else:
            plt.clf()
            plt.cla()
            b.clear()
            b.add_node('stmt')
            b.add_nodes_from(['if','comp','stmt1'])
            b.add_edges_from([('stmt','if'),('stmt','comp'),('stmt','stmt1')])
            b.add_node(p[3])
            b.add_edge('comp',p[3])
            b.add_nodes_from(['name1','equel1','exp1'])
            b.add_edges_from([('stmt1','name1'),('stmt1','equel1'),('stmt1','exp1')])
            b.add_node(p[6])
            b.add_node(p[8])
            b.add_edge('name1',p[6])
            b.add_edge('exp1',p[8])
            plt.title("ifstatement")
            pos = graphviz_layout(b, prog='dot')
            nx.draw(b,pos, with_labels=True, arrows=True, node_size=600)
            plt.show()
            plt.clf()
            plt.cla()
    else:
        try:
            if p[12] is not None:
                names[p[12]] = p[14]
                plt.clf()
                plt.cla()
                b.clear()
                b.add_node('stmt')
                b.add_nodes_from(['if','comp','stmt1','else','stmt2'])
                b.add_edges_from([('stmt','if'),('stmt','comp'),('stmt','stmt1'),('stmt','else'),('stmt','stmt2')])
                b.add_node(p[3])
                b.add_edge('comp',p[3])
                b.add_nodes_from(['name1','equel1','exp1'])
                b.add_edges_from([('stmt1','name1'),('stmt1','equel1'),('stmt1','exp1')])
                b.add_nodes_from(['name2','equel2','exp2'])
                b.add_edges_from([('stmt2','name2'),('stmt2','equel2'),('stmt2','exp2')])
                b.add_node(p[6])
                b.add_node(p[8])
                b.add_edge('name1',p[6])
                b.add_edge('exp1',p[8])
                b.add_node(p[12])
                b.add_node(p[14])
                b.add_edge('name2',p[12])
                b.add_edge('exp2',p[14])
                plt.title("if-else-statement")
                nx.nx_agraph.write_dot(b,'test.dot')
                pos = graphviz_layout(b, prog='dot')
                nx.draw(b,pos, with_labels=True, arrows=True, node_size=600)
                plt.show()
                plt.clf()
                plt.cla()
        except:
            pass
Example #38
0
                ip.pop(i-1)
                ip.pop(i)
                i = 0
                top_prio, count_ops = find_top_prio(ip)
            if len(ip) == 1:
                op_lst.append(['=', ip[i], ' ', 'a'])

        i += 1
    i = 1
    while i in range(1,len(op_lst)):
        print(op_lst[i])
        i+=1
    b.clear()

    for i in range(1,len(op_lst)):
        if op_lst[i][2]==' ':
            break
        else:
            b.add_node(op_lst[i][1])
            b.add_node(op_lst[i][2])
            b.add_node(op_lst[i][3])
            b.add_edges_from([(op_lst[i][3],op_lst[i][1]),(op_lst[i][3],op_lst[i][2])])
    if len(op_lst)>1:
        plt.title("calcustatement")
        pos = graphviz_layout(b, prog='dot')
        nx.draw(b,pos, with_labels=True, arrows=True, node_size=600)
        plt.show()
        plt.clf()
        plt.cla()
    parser.parse(s)
Example #39
0
def drawPlot(tree,
             nodeDict=None,
             edgeDict=None,
             edgeLabsDict=None,
             displayLabs=False,
             customColormap=False,
             perUnitScale=False,
             rezSqIn=400,
             neatoLayout=False):
    ''' Draw a color-coded map of the voltage drop on a feeder.
	path is the full path to the GridLAB-D .glm file or OMF .omd file.
	workDir is where GridLAB-D will run, if it's None then a temp dir is used.
	neatoLayout=True means the circuit is displayed using a force-layout approach.
	edgeLabs property must be either 'Name', 'Current', 'Power', 'Rating', 'PercentOfRating', or None
	nodeLabs property must be either 'Name', 'Voltage', 'VoltageImbalance', or None
	edgeCol and nodeCol can be set to false to avoid coloring edges or nodes
	customColormap=True means use a one that is nicely scaled to perunit values highlighting extremes.
	Returns a matplotlib object.'''
    # Be quiet matplotlib:
    # warnings.filterwarnings('ignore')

    # Build the graph.
    fGraph = feeder.treeToNxGraph(tree)
    # TODO: consider whether we can set figsize dynamically.
    wlVal = int(math.sqrt(float(rezSqIn)))

    chart = plt.figure(figsize=(wlVal, wlVal))
    plt.axes(frameon=0)
    plt.axis('off')
    chart.gca().set_aspect('equal')
    plt.tight_layout()

    # Need to get edge names from pairs of connected node names.
    edgeNames = []
    for e in fGraph.edges():
        edgeNames.append(
            (fGraph.edge[e[0]][e[1]].get('name', 'BLANK')).replace('"', ''))

    #set axes step equal
    if neatoLayout:
        # HACK: work on a new graph without attributes because graphViz tries to read attrs.
        cleanG = nx.Graph(fGraph.edges())
        cleanG.add_nodes_from(fGraph)
        positions = graphviz_layout(cleanG, prog='neato')
    else:
        positions = {n: fGraph.node[n].get('pos', (0, 0)) for n in fGraph}

    #create custom colormap
    if customColormap:
        custom_cm = matplotlib.colors.LinearSegmentedColormap.from_list(
            'custColMap', [(0.0, 'blue'), (0.15, 'darkgray'),
                           (0.7, 'darkgray'), (1.0, 'red')])
        custom_cm.set_under(color='black')
    else:
        custom_cm = plt.cm.get_cmap('viridis')

    drawColorbar = False
    emptyColors = {}

    #draw edges with or without colors
    if edgeDict != None:
        drawColorbar = True
        edgeList = [edgeDict.get(n, 1) for n in edgeNames]
    else:
        edgeList = [emptyColors.get(n, .6) for n in edgeNames]

    edgeIm = nx.draw_networkx_edges(fGraph,
                                    pos=positions,
                                    edge_color=edgeList,
                                    width=1,
                                    edge_cmap=custom_cm)

    #draw edge labels
    if displayLabs:
        edgeLabelsIm = nx.draw_networkx_edge_labels(fGraph,
                                                    pos=positions,
                                                    edge_labels=edgeLabsDict,
                                                    font_size=8)

    # draw nodes with or without color
    if nodeDict != None:
        nodeList = [nodeDict.get(n, 1) for n in fGraph.nodes()]
        drawColorbar = True
    else:
        nodeList = [emptyColors.get(n, .6) for n in fGraph.nodes()]

    if perUnitScale:
        vmin = 0
        vmax = 1.25
    else:
        vmin = None
        vmax = None

    nodeIm = nx.draw_networkx_nodes(fGraph,
                                    pos=positions,
                                    node_color=nodeList,
                                    linewidths=0,
                                    node_size=30,
                                    vmin=vmin,
                                    vmax=vmax,
                                    cmap=custom_cm)

    #draw node labels
    if displayLabs and nodeDict != None:
        nodeLabelsIm = nx.draw_networkx_labels(fGraph,
                                               pos=positions,
                                               labels=nodeDict,
                                               font_size=8)

    plt.sci(nodeIm)
    # plt.clim(110,130)
    if drawColorbar:
        plt.colorbar()
    return chart
Example #40
0
#!/usr/bin/env python3

import json

import matplotlib.pyplot as plt
import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout

jpath = 'rumor/3567665295750659.json'
data = json.load(open(jpath))
G = nx.Graph()

cnt = 0
for item in data:
    if item['parent']:
        G.add_edge(item['parent'], item['mid'])
    else:
        cnt += 1
print(cnt)

pos = graphviz_layout(G, prog='twopi')
plt.figure(figsize=(8, 8))
nx.draw(G, pos, node_size=10, alpha=0.5, node_color='blue', with_labels=False)
plt.axis('equal')
plt.show()
Example #41
0
#!/usr/bin/python

import matplotlib.pyplot as plt
import networkx as nx
import test_spack
from networkx.drawing.nx_agraph import graphviz_layout

G = nx.DiGraph()
for case in test_spack.all_test_cases:
    G.add_node(case.package_name)
    for dep in case.depends_on:
        G.add_edge(case.package_name, dep)

pos = graphviz_layout(G, prog='neato')

nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos, font_size=10)

plt.show()
Example #42
0
def display_graph(vertices):
    """
    Transform the graph to a `networkx.DiGraph`-structure and display it using `matplotlib` -- if the necessary
    libraries are installed.
    :return: `True` if the graph was drawn, `False` otherwise.
    """
    G = create_network_graph(vertices)
    _is_conditioned = None
    if _nx and _plt and G:
        try:
            from networkx.drawing.nx_agraph import graphviz_layout
            pos = graphviz_layout(G, prog='dot')
        except ModuleNotFoundError:
            from networkx.drawing.layout import shell_layout
            pos = shell_layout(G)
        except ImportError:
            from networkx.drawing.layout import shell_layout
            pos = shell_layout(G)
        _plt.subplot(111)
        _plt.axis('off')
        _nx.draw_networkx_nodes(
            G,
            pos,
            node_color='r',
            node_size=1250,
            nodelist=[v.display_name for v in vertices if v.is_sampled])
        _nx.draw_networkx_nodes(
            G,
            pos,
            node_color='b',
            node_size=1250,
            nodelist=[v.display_name for v in vertices if v.is_observed])

        for v in vertices:
            _nx.draw_networkx_edges(G,
                                    pos,
                                    arrows=True,
                                    edgelist=[(a.display_name, v.display_name)
                                              for a in v.ancestors])
            if v.condition_ancestors is not None and len(
                    v.condition_ancestors) > 0:
                _is_conditioned = 1
                _nx.draw_networkx_edges(G,
                                        pos,
                                        arrows=True,
                                        style='dashed',
                                        edge_color='g',
                                        edgelist=[
                                            (a.display_name, v.display_name)
                                            for a in v.condition_ancestors
                                        ])
        _nx.draw_networkx_labels(G, pos, font_color='w', font_weight='bold')

        # for node, _ in G.nodes():
        red_patch = mpatches.Circle((0, 0),
                                    radius=2,
                                    color='r',
                                    label='Sampled Variables')
        blue_patch = mpatches.Circle((0, 0),
                                     radius=2,
                                     color='b',
                                     label='Observed Variables')
        green_patch = mpatches.Circle(
            (0, 0), radius=2, color='g',
            label='Conditioned Variables') if _is_conditioned else 0
        if _is_conditioned:
            _plt.legend(handles=[red_patch, blue_patch, green_patch])
        else:
            _plt.legend(handles=[red_patch, blue_patch])
        _plt.show()

        return True
    else:
        return False
Example #43
0
def draw_graph(G,
               save=False,
               file_name="stochastic_block_graph",
               file_type='png',
               transparent=False,
               title="Stochastic Block Model"):
    """
    Handles rendering and drawing the network.
    Parameters
    ----------
    :param G: `optional` 
    
    a networkx graph. If present draws this graph instead of the one built in the constructor.

    :param save: `optional` 
    
    A boolean. If true saves an image of the graph to `/visualizations` otherwise renders the graph.

    :param file_type: `optional` 
    
    A string. If save flag is true it saves graph with this file extension.

    :param transparent: `optional` 
    
    A Boolean. If true it only renders the tweet's; no edges or user nodes.
    """
    print("--- Adding colours and labels ---")
    colors = []
    legend = set()
    labels = {}
    pbar = tqdm.tqdm(total=len(G.nodes()))
    for node in G.nodes():
        attributes = G.nodes[node]
        if 'type' in attributes:
            if attributes['type'] == 'retweet':
                colors.append(
                    '#79bfd3ff') if not transparent else colors.append("white")
            elif attributes['type'] == 'tweet':
                cluster = return_colour(attributes["lda_cluster"])
                legend.add(cluster)
                colors.append(cluster[0])
            elif attributes['type'] == 'user':
                labels[node] = node
                colors.append('red') if not transparent else colors.append(
                    "white")
        pbar.update(1)
    pbar.close()
    plt.figure(figsize=(30, 30))
    pos = graphviz_layout(G, prog="sfdp")
    print("--- Drawing {} nodes and {} edges ---".format(
        len(G.nodes()), G.number_of_edges()))
    width = 0.2 if not transparent else 0
    node_size = 20 if not transparent else 200
    nx.draw_networkx(G,
                     pos,
                     node_color=colors,
                     with_labels=False,
                     alpha=0.75,
                     node_size=node_size,
                     width=width,
                     arrows=False)
    nx.draw_networkx_labels(G, pos, labels, font_size=16, font_color='r')
    plt.legend(handles=return_legend(legend), loc="best")
    plt.title(title, fontdict={'fontsize': 30})
    # Turn off borders
    plt.box(False)
    plt.savefig("../visualizations/random_graphs/{}.{}".format(
        file_name, file_type),
                bbox_inches="tight") if save else plt.show()
    plt.cla()
    plt.close()
Example #44
0
def voltPlot(glmPath, workDir=None, neatoLayout=False):
    ''' Draw a color-coded map of the voltage drop on a feeder.
	Returns a matplotlib object. '''
    tree = omf.feeder.parse(glmPath)
    # # Get rid of schedules and climate:
    for key in tree.keys():
        if tree[key].get("argument",
                         "") == "\"schedules.glm\"" or tree[key].get(
                             "tmyfile", "") != "":
            del tree[key]
    # Make sure we have a voltDump:
    def safeInt(x):
        try:
            return int(x)
        except:
            return 0

    biggestKey = max([safeInt(x) for x in tree.keys()])
    tree[str(biggestKey * 10)] = {
        "object": "voltdump",
        "filename": "voltDump.csv"
    }
    tree[str(biggestKey * 10 + 1)] = {
        "object": "currdump",
        "filename": "currDump.csv"
    }
    # Run Gridlab.
    if not workDir:
        workDir = tempfile.mkdtemp()
        print '@@@@@@', workDir
    gridlabOut = omf.solvers.gridlabd.runInFilesystem(tree,
                                                      attachments=[],
                                                      workDir=workDir)
    with open(pJoin(workDir, 'voltDump.csv'), 'r') as dumpFile:
        reader = csv.reader(dumpFile)
        reader.next()  # Burn the header.
        keys = reader.next()
        voltTable = []
        for row in reader:
            rowDict = {}
            for pos, key in enumerate(keys):
                rowDict[key] = row[pos]
            voltTable.append(rowDict)
    with open(pJoin(workDir, 'currDump.csv'), 'r') as currDumpFile:
        reader = csv.reader(currDumpFile)
        reader.next()  # Burn the header.
        keys = reader.next()
        currTable = []
        for row in reader:
            rowDict = {}
            for pos, key in enumerate(keys):
                rowDict[key] = row[pos]
            currTable.append(rowDict)
    # Calculate average node voltage deviation. First, helper functions.
    def digits(x):
        ''' Returns number of digits before the decimal in the float x. '''
        return math.ceil(math.log10(x + 1))

    def avg(l):
        ''' Average of a list of ints or floats. '''
        return sum(l) / len(l)

    # Detect the feeder nominal voltage:
    for key in tree:
        ob = tree[key]
        if type(ob) == dict and ob.get('bustype', '') == 'SWING':
            feedVoltage = float(ob.get('nominal_voltage', 1))
    # Tot it all up.
    nodeVolts = {}
    for row in voltTable:
        allVolts = []
        for phase in ['A', 'B', 'C']:
            phaseVolt = abs(float(row['volt' + phase + '_real']))
            if phaseVolt != 0.0:
                if digits(phaseVolt) > 3:
                    # Normalize to 120 V standard
                    phaseVolt = phaseVolt * (120 / feedVoltage)
                allVolts.append(phaseVolt)
        nodeVolts[row.get('node_name', '')] = avg(allVolts)
    # Add up currents.
    edgeCurrents = {}
    for row in currTable:
        phaseCurr = 0.0
        for phase in ['A', 'B', 'C']:
            phaseCurr += abs(float(row['curr' + phase + '_real']))
        edgeCurrents[row.get('link_name', '')] = phaseCurr
    # create edgeCurrent copy with to and from tuple as keys for labeling
    edgeTupleCurrents = {}
    for edge in edgeCurrents:
        for obj in tree.values():
            if obj.get('name') == edge:
                coord = (obj.get('from'), obj.get('to'))
                currVal = edgeCurrents.get(edge)
                edgeTupleCurrents[coord] = "{0:.3f}".format(currVal)
    # Build the graph.
    fGraph = omf.feeder.treeToNxGraph(tree)
    voltChart = plt.figure(figsize=(15, 15))
    plt.axes(frameon=0)
    plt.axis('off')
    voltChart.gca().set_aspect('equal')
    plt.tight_layout()
    # Need to get edge names from pairs of connected node names.
    edgeNames = []
    for e in fGraph.edges():
        edgeNames.append(fGraph.edge[e[0]][e[1]].get('name', 'BLANK'))
    #set axes step equal
    if neatoLayout:
        # HACK: work on a new graph without attributes because graphViz tries to read attrs.
        cleanG = nx.Graph(fGraph.edges())
        cleanG.add_nodes_from(fGraph)
        positions = graphviz_layout(cleanG, prog='neato')
    else:
        positions = {n: fGraph.node[n].get('pos', (0, 0)) for n in fGraph}
    edgeIm = nx.draw_networkx_edges(
        fGraph,
        pos=positions,
        edge_color=[edgeCurrents.get(n, 0) for n in edgeNames],
        width=1,
        edge_vmin=None,
        edge_vmax=None,
        edge_cmap=plt.cm.jet)
    edgeLabelsIm = nx.draw_networkx_edge_labels(fGraph,
                                                pos=positions,
                                                edge_labels=edgeTupleCurrents,
                                                font_size=10)
    nodeIm = nx.draw_networkx_nodes(
        fGraph,
        pos=positions,
        node_color=[nodeVolts.get(n, 0) for n in fGraph.nodes()],
        linewidths=0,
        node_size=30,
        vmin=110,
        vmax=130,
        cmap=plt.cm.jet)

    plt.sci(nodeIm)
    # plt.clim(110,130)
    plt.colorbar()
    return voltChart

def slice_network(G, T, copy=True):
    """
    Remove all edges witth weight<T from G or its copy,
    """
    F = G.copy() if copy else G
    F.remove_edges_from(
        (n1, n2) for n1, n2, w in F.edges(data="weight") if w < T)
    return F


# Draw different degrees of slicing for a random graph
ergraph = nx.erdos_renyi_graph(100, 0.1)
nx.set_edge_attributes(ergraph, {(n1, n2): random.random()
                                 for n1, n2 in ergraph.edges()}, "weight")

pos = graphviz_layout(ergraph)
for i, threshold in enumerate([0, 0.7, 0.8, 0.9, 0.95, 1.0]):
    ax = plt.subplots(3, 2, i + 1)
    slice_network(ergraph, threshold, copy=False)
    nx.draw_networkx_edges(ergraph,
                           pos,
                           alpha=0.65,
                           ax=ax,
                           **dzcnapy.small_attrs)
    nx.draw_networkx_nodes(ergraph, pos, ax=ax, **dzcnapy.small_attrs)
    dzcnapy.set_extent(pos, ax, "Threshold={}".format(threshold))

dzcnapy.plot("slicing")
Example #46
0
def p_statement_for(p):
    '''statement : FOR statement LOOP NUMBER COLON NAME EQUALS expression PLUS expression SEMICOLON
                 | FOR statement LOOP NUMBER COLON NAME EQUALS expression MINUS expression SEMICOLON
                 | FOR statement LOOP NUMBER COLON NAME EQUALS expression TIMES expression SEMICOLON
                 | FOR statement LOOP NUMBER COLON NAME EQUALS expression DIVIDE expression SEMICOLON
                 | FOR statement LOOP NUMBER COLON NAME EQUALS expression EXPON expression SEMICOLON
                 | FOR statement LOOP NUMBER COLON NAME EQUALS expression ROOT expression SEMICOLON '''
    plt.clf()
    plt.cla()
    b.clear()
    b.add_node('stmt')
    b.add_nodes_from(['for','stmt1','loop','time','stmt2'])
    b.add_edges_from([('stmt','for'),('stmt','stmt1'),('stmt','loop'),('stmt','time'),('stmt','stmt2')])
    b.add_node(p[6])
    b.add_node(p[4])
    b.add_edge('stmt1',p[6])
    b.add_edge('time',p[4])
    b.add_node('=')
    b.add_edge('stmt2',p[6])
    b.add_edge('stmt2','=')
    if p[9]=='+':
        b.add_node('+stmt')
        b.add_edge('stmt2','+stmt')
        b.add_node('t1')
        b.add_edge('+stmt','t1')
    elif p[9]=='-':
        b.add_node('-stmt')
        b.add_edge('stmt2','-stmt')
        b.add_node('t1')
        b.add_edge('-stmt','t1')
    elif p[9]=='*':
        b.add_node('*stmt')
        b.add_edge('stmt2','*stmt')
        b.add_node('t1')
        b.add_edge('*stmt','t1')
    elif p[9]=='/':
        b.add_node('/stmt')
        b.add_edge('stmt2','/stmt')
        b.add_node('t1')
        b.add_edge('/stmt','t1')
    elif p[9]=='^':
        b.add_node('^stmt')
        b.add_edge('stmt2','^stmt')
        b.add_node('t1')
        b.add_edge('^stmt','t1')
    elif p[9]=='**':
        b.add_node('**stmt')
        b.add_edge('stmt2','**stmt')
        b.add_node('t1')
        b.add_edge('**stmt','t1')
    plt.title("forstatement")
    pos = graphviz_layout(b, prog='dot')
    nx.draw(b,pos, with_labels=True, arrows=True, node_size=600)
    plt.show()
    plt.clf()
    plt.cla()
    if p[4]>0:

        for names[p[2]] in range(1,p[4]+1):
            names[p[2]]=names[p[2]]+1
            p[8]=int(names[p[2]])
            if p[9]=='+':
                if(names[p[2]]<=1):
                    p[0]=p[8]+p[10]
                else:
                    p[0]=names[p[6]]+p[10]
                    names[p[6]]=p[0]
            elif p[9]=='-':
                if(names[p[2]]<=1):
                    p[0]=p[8]-p[10]
                else:
                    p[0]=names[p[6]]-p[10]
                    names[p[6]]=p[0]
            elif p[9]=='*':
                if(names[p[2]]<=1):
                    p[0]=p[8]*p[10]
                else:
                    p[0]=names[p[6]]*p[10]
                    names[p[6]]=p[0]
            elif p[9]=='/':
                if(names[p[2]]<=1):
                    p[0]=p[8]/p[10]
                else:
                    p[0]=names[p[6]]/p[10]
                    names[p[6]]=p[0]
            elif p[9]=='^':
                if(names[p[2]]<=1):
                    p[0]=math.pow(p[8] , p[10])
                else:
                    p[0]=math.pow(names[p[6]],p[10])
                    names[p[6]]=p[0]
            elif p[9]=='**':
                if(names[p[2]]<=1):
                    p[0]=math.pow(p[8] , 1/p[10])
                else:
                    p[0]=math.pow(names[p[6]],1/p[10])
                    names[p[6]]=p[0]
Example #47
0
G = nx.DiGraph()
G.add_node("A", rank=0)
G.add_nodes_from(['B', 'C', 'D'], style='filled', fillcolor='red')
G.add_nodes_from(['D', 'F', 'G'])
G.add_nodes_from(['H'], label='target')
G.add_edge('A', 'B', arrowsize=2.0)
G.add_edge('A', 'C', penwidth=2.0)
G.add_edge('A', 'D')
G.add_edges_from([('B', 'E'), ('B', 'F')], color='blue')
G.add_edges_from([('C', 'E'), ('C', 'G')])
G.add_edges_from([('D', 'F'), ('D', 'G')])
G.add_edges_from([('E', 'H'), ('F', 'H'), ('G', 'H')])

# set defaults
G.graph['graph'] = {'rankdir': 'TD'}
G.graph['node'] = {'shape': 'circle'}
G.graph['edges'] = {'arrowsize': '4.0'}

A = to_agraph(G)
print(A)
A.layout('dot')
A.draw('abcd.png')

nx.draw(G,
        pos=graphviz_layout(G),
        node_size=1600,
        cmap=plt.cm.Reds,
        node_color=range(len(G)),
        prog='dot',
        with_labels=True)
plt.show()
    lista_nodos_bi = todos

    for node_ in lista_nodos_bi:

        if node_ % 2 == 0:
            color_map_bi.append('lightcoral')
            #print("female color")
        else:
            color_map_bi.append('mediumseagreen')
            #print("male color")

    ########################################################
    #Full binary tree plot

    fig = plt.figure(figsize=cm2inch(12, 10))
    pos = graphviz_layout(G, prog='dot')
    nx.draw(G,
            pos,
            with_labels=True,
            node_color=color_map_bi,
            edge_color='gray',
            font_size=5,
            node_size=150,
            arrows=True,
            width=0.5)
    vmin = 0
    vmax = len(generation)
    #sm = plt.cm.ScalarMappable(norm=matplotlib.colors.Normalize(vmin=vmin, vmax=vmax))
    #sm._A = []
    plt.savefig(r_dir + "/test_tree_bi" + str(seed) + ".png",
                dpi=300,
Example #49
0
def gDraw(edgelist, nodelist, colors, digName, visTimes, startV, lengthOfRW):

    #plotly.offline.init_notebook_mode()

    g = nx.Graph()
    g.add_edges_from(edgelist)

    # a dictionary to store these edges by their partition id
    partitionsDic = {}

    for e in edgelist:
        k = e[2]["partition"]
        ev = (e[0], e[1])
        if partitionsDic.has_key(k):
            partitionsDic[k].append(ev)
        else:
            partitionsDic[k] = [ev]

    # a dict to store the replica factor of each vertex
    verRF = {}
    verSets = []
    for bid, edges in partitionsDic.iteritems():
        tset = set()
        for x in edges:
            tset.add(x[0])
            tset.add(x[1])
        verSets.append(tset)
    for vset in verSets:
        for ver in vset:
            if verRF.has_key(ver):
                rf = verRF[ver]
                verRF[ver] = rf+1
            else:
                verRF[ver] = 1



    #partitionsDic = edgelist
    #for k, v in partitionsDic.iteritems():
    #    print k, len(v)

    # a list to store those seeds
    seeds = []
    # a list to store other normal vertices
    otherVertices = []

    for n in nodelist:
        if n[1] == "s":
            seeds.append(n[0])
        else:
            otherVertices.append(n[0])


    #import matplotlib.pyplot as plt

    #pos=nx.spring_layout(g) # positions for all nodes

    # position is stored as node attribute data for random_geometric_graph
    #pos=nx.get_node_attributes(g,'pos')

    # layout graphs with positions using graphviz neato
    import pygraphviz
    from networkx.drawing.nx_agraph import graphviz_layout
    #pos = graphviz_layout(g, prog="sfdp")
    pos = graphviz_layout(g, prog="neato")
    #pos = nx.fruchterman_reingold_layout(g)
    #pos=nx.spectral_layout(g)


    # to get the positions of seeds
    seeds_Xv = []
    seeds_Yv = []
    for s in seeds:
        if pos.has_key(s):
            seeds_Xv.append(pos[s][0])
            seeds_Yv.append(pos[s][1])

    # to get the positions of other normal vertices
    nor_Xv = []
    nor_Yv = []
    for s in otherVertices:
        if pos.has_key(s):
            nor_Xv.append(pos[s][0])
            nor_Yv.append(pos[s][1])

    # a list to store the names of seeds
    seedNms = []
    for i in range(len(seeds)):
        seedNms.append("seed_"+str(i+1))

    # the trace of seeds
    trace_vs = go.Scatter(x=seeds_Xv,
                          y=seeds_Yv,
                          xaxis='x2',
                          yaxis='y2',
                          mode='markers',
                          name='Seeds',
                          marker=go.Marker(symbol='dot',
                                           size=10,
                                           #color='#e1441c',
                                           line=go.Line(color='rgb(50,50,50)', width=0.6)
                                           ),
                          text=seedNms,
                          hoverinfo='text'
                          )

    # the trace of other vertices
    trace_ov = go.Scatter(x=nor_Xv,
                          y=nor_Yv,
                          xaxis='x2',
                          yaxis='y2',
                          mode='markers',
                          name='Normal Vertices',
                          hoverinfo='none',
                          marker=go.Marker(symbol='dot',
                                           size=2,
                                           #color='#6959CD',
                                           line=go.Line(color='rgb(50,50,50)', width=0.2)
                                           ),
                          )

    # the list of datas for plotting
    ds = []
    col = 100
    i = 0
    for key, val in partitionsDic.iteritems():
        block_id = "partition_" + str(key)
        Xed = []
        Yed = []
        #parNames = []
        for edge in val:
            Xed += [pos[edge[0]][0], pos[edge[1]][0], None]
            Yed += [pos[edge[0]][1], pos[edge[1]][1], None]
            #tep = block_id
            #parNames.append(tep)

        #col = col+30
        #scol = str(col)
        #rgbcol = "rgb("+scol+","+scol+","+scol+")"
        #hoinfo = [block_id] * len(val)
        trace = go.Scatter(x=Xed,
                           y=Yed,
                           xaxis='x2',
                           yaxis='y2',
                           mode='lines',
                           name=block_id,
                           #text=parNames,
                           #hoverinfo='text',
                           hoverinfo='name',
                           #line=go.Line(color=colors[i], width=2),
                           line=go.Line(width=2),
                           )
        i = i+1
        ds.append(trace)

    ds.append(trace_vs)
    ds.append(trace_ov)


    '''
    the display of visit times to each vertex in Random Walk Toy
    1) to category the vertices upon its visit times
    2) to draw each vertex upon its category
    '''
    if visTimes:
        numOfScals = lengthOfRW/scal
        # we use the scale of colors in 'Viridis', 3 ~ -3
        pd = numOfScals/float(6)

        # a dict to store the vertices of each scale upon visit-times
        scalsDic = {}
        for i in range(0, numOfScals+1):
            scalsDic[i] = []

        for v, tms in visTimes.iteritems():
            if tms > 0:
                scalsDic[tms/scal].append(v)

        # to draw the vertices
        for categ, vers in scalsDic.iteritems():
            if vers:
                # to get the positions of vertices in this category
                Xv = []
                Yv = []
                vts = []
                for s in vers:
                    if pos.has_key(s):
                        Xv.append(pos[s][0])
                        Yv.append(pos[s][1])
                        vts.append("VisTimes:"+str(visTimes[s]))

                # the trace of vertices
                trace = go.Scatter(x=Xv,
                                   y=Yv,
                                   xaxis='x2',
                                   yaxis='y2',
                                   mode='markers',
                                   name='visit-times in RW:' + str(categ*scal+1) + ' ~ ' + str(categ*scal+scal),
                                   text=vts,
                                   hoverinfo='text',
                                   marker=dict(
                                       size=str((categ+1)*3 + 13),
                                       color=5.0 - categ/pd,
                                       colorscale='Viridis'
                                   )

                                   )
                ds.append(trace)


    '''
    to display/draw the starting vertex of RW
    '''
    # to get the positions of starting Vertex
    startV_Xv = []
    startV_Yv = []
    if pos.has_key(startV):
        startV_Xv.append(pos[startV][0])
        startV_Yv.append(pos[startV][1])
    # the trace of starting vertex
    sv = []
    sv.append("Starting Vertex")
    trace_startV = go.Scatter(x=startV_Xv,
                              y=startV_Yv,
                              xaxis='x2',
                              yaxis='y2',
                              mode='markers',
                              name='The Starting Vertex of Random Walk(Toy)',
                              text=sv,
                              hoverinfo='text',
                              marker=dict(
                                  size = '15',
                                  color= 'rgb(0,0,0)',
                                  line=go.Line(color='rgb(250,250,250)', width=0.5)

                              )

    )
    ds.append(trace_startV)





    data1 = go.Data(ds)


    """
    Test:
    """
    # to calculate the overall times of communication happened in graph
    overTims = 0
    for ver, ts in visTimes.iteritems():
        overTims += ts * (verRF[ver]-1)
    # to get the sizes of partitions
    sizes = ""
    for bid, par in partitionsDic.iteritems():
        sizes += str(len(par))+", "
    # to calculate the VRF
    overallRF = 0
    for vset in verSets:
        overallRF += len(vset)
    vrf = overallRF/float(len(verRF))
    # Add table data
    table_data = [['Term', 'Result'],
                  ['Number of Vertices', str(len(verRF))],
                  ['Number of Edges', str(len(edgelist))],
                  #['Number of Blocks(for our method)', '...'],
                  ['Sizes of Partitions', sizes],
                  ['Vertex Replica Factor(VRF)', str(vrf)],
                  ['Length of Random Walk(RW)', str(lengthOfRW)],
                  ['Number of Communications(Inter-Partitions) during RW', str(overTims)]
                  ]
    # Initialize a figure with FF.create_table(table_data)
    #font = ['#FCFCFC','#000000','#000000','#000000','#000000', '#FF3030', '#0099ff','#0099ff']
    font = ['#FCFCFC', '#000000', '#000000', '#000000', '#FF3030', 'blue', 'blue']
    figure = FF.create_table(table_data, font_colors=font)

    # Add trace data to figure
    figure['data'].extend(data1)

    # Edit layout for subplots
    figure.layout.yaxis.update({'domain': [0, .25]})
    figure.layout.yaxis2.update({'domain': [.3, 1]})
    # The graph's yaxis2 MUST BE anchored to the graph's xaxis2 and vice versa
    figure.layout.yaxis2.update({'anchor': 'x2'})
    figure.layout.xaxis2.update({'anchor': 'y2'})
    #figure.layout.yaxis.update({'title': 'The Results of Partitioning and Random Walk Application'})
    # Update the margins to add a title and see graph x-labels.
    figure.layout.margin.update({'t': 75, 'l': 50})
    #figure.layout.update({'title': '2016 Hockey Stats'})
    # Update the height because adding a graph vertically will interact with
    # the plot height calculated for the table
    figure.layout.update({'height': 800})

    pu = plotly.offline.plot(figure, auto_open=False, filename=digName + ".html")



    #fig1 = go.Figure(data=data1)
    #pu = plotly.offline.plot(fig1, auto_open=False, filename=digName+".html")


    #pu = plotly.offline.plot_mpl(fig, auto_open=False)
    #plot_url = py.iplot_mpl(fig)
    print "pu: ", pu
    return pu
Example #50
0
def DrawGraph(ax, edgelist, sz=1, labels=False, weighted=False, threshold=1.0, show_th=False, \
             show_wts=False, save_graphx=False, gx_dir=None, seg_id=None, percentile=None, \
             percent=None, prune_jns=True, is_tree=False):
    def ShrinkGraph(G, threshold, debug=False, prune_jns=True):
        """ 
            Do not contract an edge if edge part of a triangle loop 
            with length shorter than the threshold.
        """
        def GetOrphan(a1, a2, s1, s2, G):
            orphan_, other_, orph_node_ = None, None, None
            if len(s1) == 0 and len(s2) == 2:
                orphan_ = a2
                other_ = a1
            if len(s2) == 0 and len(s1) == 2:
                orphan_ = a1
                other_ = a2
            # if no edge exists between two adjacent nodes
            # of orphan, then orphan can be deleted.
            if orphan_ is not None:
                orph_node_ = a1
                m, n = set(G[orphan_].keys()).difference({other_})
                if n not in G[m].keys():
                    return orph_node_
            return None

        if threshold == 0.0: return G
        if debug: PrintSummary(G)

        if percentile is not None:
            wts = np.array([d['weight'] for d in G.edges.values()])
            threshold = np.percentile(wts, percentile)
            perc_wts = wts / np.sum(wts)
            base_ = np.percentile(perc_wts, percentile)
            pivots = [percentile + 0.5 * (j + 1) for j in range(70)]
            for p in pivots:
                if p < 100 and np.percentile(perc_wts, p) > 50 * base_:
                    threshold = np.percentile(wts, p)
                    print('Threshold found at {:.2f}'.format(p))
                    break
        delete_count = 0
        while True:
            wts = np.array([d['weight'] for d in G.edges.values()])
            if prune_jns:
                idx = np.asarray(wts < threshold).nonzero()[0]
            else:
                mask = [(len(G[e[0][0]].keys()) == 1) or (len(G[e[0][1]].keys()) == 1) \
                        for e in G.edges.items()]
                idx = np.asarray((wts < threshold) & mask).nonzero()[0]
            if len(idx) == 0:
                break
            u, v = None, None
            orphan_node = None
            # find a candidate edge to delete in this loop
            for i in idx:
                a1, a2 = G.edges.items()[i][0]
                s1, s2 = set(G[a1].keys()).difference({a2}), \
                            set(G[a2].keys()).difference({a1})
                intersect_ = s1 & s2
                # If no common node in adjacency list then break
                if len(intersect_) == 0:
                    u, v = a1, a2
                    orphan_node = GetOrphan(a1, a2, s1, s2, G)
                    break
                # Else, make sure no triangular loop of decent size breaks
                else:
                    crucial_edge = False
                    for n in intersect_:
                        w1 = G.get_edge_data(n, a1)['weight']
                        w2 = G.get_edge_data(n, a2)['weight']
                        # if a loop (containing this edge) with
                        # len > 3*thresh exists, don't delete this edge
                        if w1 + w2 > 3 * threshold - wts[i]:
                            crucial_edge = True
                            break
                    if not crucial_edge:
                        u, v = a1, a2
                        orphan_node = GetOrphan(a1, a2, s1, s2, G)
                        break
            if u is None:
                break
            G = nx.contracted_nodes(G, u, v, self_loops=False)
            delete_count += 1
            if orphan_node is not None:
                m, n = G[orphan_node].keys()
                wm, wn = G[orphan_node][m]['weight'], G[orphan_node][n][
                    'weight']
                # add edge between m and n
                G.add_edge(m, n, weight=(wm + wn))
                # delete orphan node
                G.remove_node(orphan_node)
            if debug:
                print('Deleted edge {}-{}'.format(u, v))
                PrintSummary(G)
        print('Total edges deleted {}'.format(delete_count))
        return G

    G = nx.Graph()
    if not weighted:
        G.add_edges_from(edgelist)
        pos = graphviz_layout(G)
        nx.draw_networkx(G,
                         pos=pos,
                         node_size=sz,
                         ax=ax,
                         with_labels=labels,
                         font_size=8,
                         font_color='white',
                         font_weight='bold',
                         edge_color='red')
    else:
        G.add_edges_from(edgelist)

        t0_shrink = time.time()

        if is_tree:
            if True:
                factor = 5.0**(-3)
                wt = GetWt(G)
                t = factor * wt
                G = MergeTwoEdges(G)

                q0 = 20
                t0 = GetLeafPercentile(G, perc=q0)
                print('Original {:.0f}%ile weight {:.2f}'.format(q0, t0))
                for p in [25, 40, 80, 40, 20]:
                    t = GetLeafPercentile(G, perc=p)
                    G = DeLeaf(G, thresh=t)
                    G = MergeTwoEdges(G)

                G = ShrinkGraph(G, threshold=t0, prune_jns=True)
                G = DeLeaf(G, thresh=t0)
                G = MergeTwoEdges(G)
                max_n, max_deg = GetMaxDegree(G)

                if max_n in G.nodes.keys():
                    AddCenter(G, max_n)
        else:
            G = ShrinkGraph(G, threshold=threshold, prune_jns=True)

        if save_graphx:
            nx.write_gpickle(
                G, os.path.join(gx_dir,
                                str(seg_id) + '_networkx.obj'))
        print('Graph shrunk in {:.2f}s'.format(time.time() - t0_shrink))
        if show_th:
            edge_dict = {e[0]: '({:.1f}, {:.1f})'.format(e[1]['weight'], \
                                                         e[1]['thick']) for e in G.edges.items()}
        else:
            edge_dict = {
                e[0]: '{:.1f}'.format(e[1]['weight'])
                for e in G.edges.items()
            }
        pos = graphviz_layout(G)
        nx.draw_networkx(G,
                         pos=pos,
                         node_size=sz,
                         ax=ax,
                         with_labels=labels,
                         font_size=5,
                         font_color='white',
                         font_weight='bold')  #, edge_color='red')
        if show_wts:
            nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_dict)
Example #51
0
 def draw_and_show(self, figsize=(8, 8)):
     pos = graphviz_layout(self)
     plt.figure(figsize=figsize)
     nx.draw_networkx(self, pos=pos, with_labels=True)
     plt.show(block=True)
nb = 0
added_edges = []
while nb < 5:
    first_node = choice(G.nodes())  # pick a random node
    possible_nodes = set(G.nodes())
    neighbours = G.neighbors(first_node) + [first_node]
    possible_nodes.difference_update(
        neighbours
    )  # remove the first node and all its neighbours from the candidates
    second_node = choice(list(possible_nodes))
    G.add_edge(first_node, second_node, weight=1)
    added_edges.append((first_node, second_node))
    added_edges.append((second_node, first_node))
    print(first_node, second_node)
    nb += 1
pos = graphviz_layout(G, prog='sfdp')
nx.draw(G, pos, with_labels=False, node_size=1)
plt.show()

itr = 7
E_final = {}

for edge in G.edges():
    E_final[edge] = 0
while itr > 0:
    X = Algebraic_Rank.get_algebraic_rank(G, 0.5, 100)
    E = {}
    for edge in G.edges():
        E[edge] = abs(X[edge[0]] - X[edge[1]])
        if (E_final[edge] < E[edge]):
            E_final[edge] = E[edge]
Example #53
0
## aws logs get-log-events --log-group-name A --log-stream-name a --output text > a.log 
## cat a.log | grep -v REJECT | awk '/ / {print $6, $7}' > a-accept.log
## vi a-accept.log
## type:
## 1,$ s/ /", "/g
## and type:
## 1,$norm I     ("
## and type:
## 1,$norm A"),
## and type:
## :wq
## vi networkmap.py
## and type:
## :set number
## should see line 8 as start or lincoln_list
## go to row 29 and type:
## :.-1read a-accept.log
## that will read in the accepted connection hosts
## and type:
## :wq
#print(str(netmap))
#]
F = nx.DiGraph(netmap)
#F = nx.DiGraph(lincoln_list)

pos = graphviz_layout(F)
nx.draw_networkx(F, pos, **dzcnapy.attrs)
dzcnapy.set_extent(pos, plt)
dzcnapy.plot     ("lincoln")

Example #54
0
def voltPlot(omd, workDir=None, neatoLayout=False):
    ''' Draw a color-coded map of the voltage drop on a feeder.
	Returns a matplotlib object. '''
    tree = omd.get('tree', {})
    # # Get rid of schedules and climate:
    for key in tree.keys():
        if tree[key].get("argument",
                         "") == "\"schedules.glm\"" or tree[key].get(
                             "tmyfile", "") != "":
            del tree[key]
    # Make sure we have a voltDump:
    def safeInt(x):
        try:
            return int(x)
        except:
            return 0

    biggestKey = max([safeInt(x) for x in tree.keys()])
    tree[str(biggestKey * 10)] = {
        "object": "voltdump",
        "filename": "voltDump.csv"
    }
    # Run Gridlab.
    if not workDir:
        workDir = tempfile.mkdtemp()
    gridlabOut = gridlabd.runInFilesystem(tree,
                                          attachments=omd.get(
                                              'attachments', {}),
                                          workDir=workDir)
    with open(pJoin(workDir, 'voltDump.csv'), 'r') as dumpFile:
        reader = csv.reader(dumpFile)
        reader.next()  # Burn the header.
        keys = reader.next()
        voltTable = []
        for row in reader:
            rowDict = {}
            for pos, key in enumerate(keys):
                rowDict[key] = row[pos]
            voltTable.append(rowDict)
    # Calculate average node voltage deviation. First, helper functions.
    def pythag(x, y):
        ''' For right triangle with sides a and b, return the hypotenuse. '''
        return math.sqrt(x**2 + y**2)

    def digits(x):
        ''' Returns number of digits before the decimal in the float x. '''
        return math.ceil(math.log10(x + 1))

    def avg(l):
        ''' Average of a list of ints or floats. '''
        return sum(l) / len(l)

    # Detect the feeder nominal voltage:
    for key in tree:
        ob = tree[key]
        if type(ob) == dict and ob.get('bustype', '') == 'SWING':
            feedVoltage = float(ob.get('nominal_voltage', 1))
    # Tot it all up.
    nodeVolts = {}
    for row in voltTable:
        allVolts = []
        for phase in ['A', 'B', 'C']:
            phaseVolt = pythag(float(row['volt' + phase + '_real']),
                               float(row['volt' + phase + '_imag']))
            if phaseVolt != 0.0:
                if digits(phaseVolt) > 3:
                    # Normalize to 120 V standard
                    phaseVolt = phaseVolt * (120 / feedVoltage)
                allVolts.append(phaseVolt)
        nodeVolts[row.get('node_name', '')] = avg(allVolts)
    # Color nodes by VOLTAGE.
    fGraph = feeder.treeToNxGraph(tree)
    voltChart = plt.figure(figsize=(15, 15))
    plt.axes(frameon=0)
    plt.axis('off')
    #set axes step equal
    voltChart.gca().set_aspect('equal')
    if neatoLayout:
        # HACK: work on a new graph without attributes because graphViz tries to read attrs.
        cleanG = nx.Graph(fGraph.edges())
        cleanG.add_nodes_from(fGraph)
        positions = graphviz_layout(cleanG, prog='neato')
    else:
        positions = {n: fGraph.node[n].get('pos', (0, 0)) for n in fGraph}
    edgeIm = nx.draw_networkx_edges(fGraph, positions)
    nodeIm = nx.draw_networkx_nodes(
        fGraph,
        pos=positions,
        node_color=[nodeVolts.get(n, 0) for n in fGraph.nodes()],
        linewidths=0,
        node_size=30,
        cmap=plt.cm.jet)
    plt.sci(nodeIm)
    plt.clim(110, 130)
    plt.colorbar()
    return voltChart
Example #55
0
 def draw(self, ax):
     pos = graphviz_layout(self._td, prog='twopi', args='')
     nx.draw(self._td, pos, ax=ax, node_size=100, alpha=0.5, node_color='g')
     L = nx.get_node_attributes(self._td, 'label')
     nx.draw_networkx_labels(self._td, pos, ax=ax, font_size=8, labels=L)
     ax.margins(0.2)  # to restrict label-cut-off
Example #56
0
def drawPlot(path,
             workDir=None,
             neatoLayout=False,
             edgeLabs=None,
             nodeLabs=None,
             edgeCol=None,
             nodeCol=None,
             faultLoc=None,
             faultType=None,
             customColormap=False,
             scaleMin=None,
             scaleMax=None,
             rezSqIn=400,
             simTime='2000-01-01 0:00:00',
             loadLoc=None):
    ''' Draw a color-coded map of the voltage drop on a feeder.
	path is the full path to the GridLAB-D .glm file or OMF .omd file.
	workDir is where GridLAB-D will run, if it's None then a temp dir is used.
	neatoLayout=True means the circuit is displayed using a force-layout approach.
	edgeCol property must be either 'Current', 'Power', 'Rating', 'PercentOfRating', or None
	nodeCol property must be either 'Voltage', 'VoltageImbalance', 'perUnitVoltage', 'perUnit120Voltage', or None
	edgeLabs and nodeLabs properties must be either 'Name', 'Value', or None
	edgeCol and nodeCol can be set to false to avoid coloring edges or nodes
	customColormap=True means use a one that is nicely scaled to perunit values highlighting extremes.
	faultType and faultLoc are the type of fault and the name of the line that it occurs on.
	Returns a matplotlib object.'''
    # Be quiet matplotlib:
    # warnings.filterwarnings("ignore")
    if path.endswith('.glm'):
        tree = feeder.parse(path)
        attachments = []
    elif path.endswith('.omd'):
        with open(path) as f:
            omd = json.load(f)
        tree = omd.get('tree', {})
        attachments = omd.get('attachments', [])
    else:
        raise Exception('Invalid input file type. We require a .glm or .omd.')
    #print path
    # add fault object to tree
    def safeInt(x):
        try:
            return int(x)
        except:
            return 0

    biggestKey = max([safeInt(x) for x in tree.keys()])
    # Add Reliability module
    tree[str(biggestKey * 10)] = {
        "module": "reliability",
        "maximum_event_length": "18000",
        "report_event_log": "true"
    }
    CLOCK_START = simTime
    dt_start = parser.parse(CLOCK_START)
    dt_end = dt_start + relativedelta(seconds=+20)
    CLOCK_END = str(dt_end)
    CLOCK_RANGE = CLOCK_START + ',' + CLOCK_END
    if faultType != None:
        # Add eventgen object (the fault)
        tree[str(biggestKey * 10 + 1)] = {
            "object": "eventgen",
            "name": "ManualEventGen",
            "parent": "RelMetrics",
            "fault_type": faultType,
            "manual_outages": faultLoc + ',' + CLOCK_RANGE
        }  # TODO: change CLOCK_RANGE to read the actual start and stop time, not just hard-coded
        # Add fault_check object
        tree[str(biggestKey * 10 + 2)] = {
            "object": "fault_check",
            "name": "test_fault",
            "check_mode": "ONCHANGE",
            "eventgen_object": "ManualEventGen",
            "output_filename": "Fault_check_out.txt"
        }
        # Add reliabilty metrics object
        tree[str(biggestKey * 10 + 3)] = {
            "object": "metrics",
            "name": "RelMetrics",
            "report_file": "Metrics_Output.csv",
            "module_metrics_object": "PwrMetrics",
            "metrics_of_interest": '"SAIFI,SAIDI,CAIDI,ASAI,MAIFI"',
            "customer_group": '"groupid=METERTEST"',
            "metric_interval": "5 h",
            "report_interval": "5 h"
        }
        # Add power_metrics object
        tree[str(biggestKey * 10 + 4)] = {
            "object": "power_metrics",
            "name": "PwrMetrics",
            "base_time_value": "1 h"
        }

        # HACK: set groupid for all meters so outage stats are collected.
        noMeters = True
        for key in tree:
            if tree[key].get('object', '') in ['meter', 'triplex_meter']:
                tree[key]['groupid'] = "METERTEST"
                noMeters = False
        if noMeters:
            raise Exception(
                "No meters detected on the circuit. Please add at least one meter to allow for collection of outage statistics."
            )
    for key in tree:
        if 'clock' in tree[key]:
            tree[key]['starttime'] = "'" + CLOCK_START + "'"
            tree[key]['stoptime'] = "'" + CLOCK_END + "'"

    # dictionary to hold info on lines present in glm
    edge_bools = dict.fromkeys([
        'underground_line', 'overhead_line', 'triplex_line', 'transformer',
        'regulator', 'fuse', 'switch'
    ], False)
    # Map to speed up name lookups.
    nameToIndex = {tree[key].get('name', ''): key for key in tree.keys()}
    # Get rid of schedules and climate and check for all edge types:
    for key in list(tree.keys()):
        obtype = tree[key].get("object", "")
        if obtype == 'underground_line':
            edge_bools['underground_line'] = True
        elif obtype == 'overhead_line':
            edge_bools['overhead_line'] = True
        elif obtype == 'triplex_line':
            edge_bools['triplex_line'] = True
        elif obtype == 'transformer':
            edge_bools['transformer'] = True
        elif obtype == 'regulator':
            edge_bools['regulator'] = True
        elif obtype == 'fuse':
            edge_bools['fuse'] = True
        elif obtype == 'switch':
            edge_bools['switch'] = True
        if tree[key].get("argument",
                         "") == "\"schedules.glm\"" or tree[key].get(
                             "tmyfile", "") != "":
            del tree[key]
    # Make sure we have a voltage dump and current dump:
    tree[str(biggestKey * 10 + 5)] = {
        "object": "voltdump",
        "filename": "voltDump.csv"
    }
    tree[str(biggestKey * 10 + 6)] = {
        "object": "currdump",
        "filename": "currDump.csv"
    }
    # Line rating dumps
    tree[feeder.getMaxKey(tree) + 1] = {'module': 'tape'}
    for key in edge_bools.keys():
        if edge_bools[key]:
            tree[feeder.getMaxKey(tree) + 1] = {
                'object': 'group_recorder',
                'group': '"class=' + key + '"',
                'property': 'continuous_rating',
                'file': key + '_cont_rating.csv'
            }
    #Record initial status readout of each fuse/recloser/switch/sectionalizer before running
    # Reminder: fuse objects have 'phase_X_status' instead of 'phase_X_state'
    protDevices = dict.fromkeys(
        ['fuse', 'recloser', 'switch', 'sectionalizer'], False)
    #dictionary of protective device initial states for each phase
    protDevInitStatus = {}
    #dictionary of protective devices final states for each phase after running Gridlab-D
    protDevFinalStatus = {}
    #dictionary of protective device types to help the testing and debugging process
    protDevTypes = {}
    protDevOpModes = {}
    for key in tree:
        obj = tree[key]
        obType = obj.get('object')
        if obType in protDevices.keys():
            obName = obj.get('name', '')
            protDevTypes[obName] = obType
            if obType != 'fuse':
                protDevOpModes[obName] = obj.get('operating_mode',
                                                 'INDIVIDUAL')
            protDevices[obType] = True
            protDevInitStatus[obName] = {}
            protDevFinalStatus[obName] = {}
            for phase in ['A', 'B', 'C']:
                if obType != 'fuse':
                    phaseState = obj.get('phase_' + phase + '_state', 'CLOSED')
                else:
                    phaseState = obj.get('phase_' + phase + '_status', 'GOOD')
                if phase in obj.get('phases', ''):
                    protDevInitStatus[obName][phase] = phaseState
    #print protDevInitStatus

    #Create a recorder for protective device states
    for key in protDevices.keys():
        if protDevices[key]:
            for phase in ['A', 'B', 'C']:
                if key != 'fuse':
                    tree[feeder.getMaxKey(tree) + 1] = {
                        'object': 'group_recorder',
                        'group': '"class=' + key + '"',
                        'property': 'phase_' + phase + '_state',
                        'file': key + '_phase_' + phase + '_state.csv'
                    }
                else:
                    tree[feeder.getMaxKey(tree) + 1] = {
                        'object': 'group_recorder',
                        'group': '"class=' + key + '"',
                        'property': 'phase_' + phase + '_status',
                        'file': key + '_phase_' + phase + '_state.csv'
                    }

    # Run Gridlab.
    if not workDir:
        workDir = tempfile.mkdtemp()
        print('@@@@@@', workDir)
    # for i in range(6):
    # 	gridlabOut = gridlabd.runInFilesystem(tree, attachments=attachments, workDir=workDir)
    # 	#HACK: workaround for shoddy macOS gridlabd build.
    # 	if 'error when setting parent' not in gridlabOut.get('stderr','OOPS'):
    # 		break
    gridlabOut = gridlabd.runInFilesystem(tree,
                                          attachments=attachments,
                                          workDir=workDir)

    #Record final status readout of each fuse/recloser/switch/sectionalizer after running
    try:
        for key in protDevices.keys():
            if protDevices[key]:
                for phase in ['A', 'B', 'C']:
                    with open(pJoin(workDir,
                                    key + '_phase_' + phase + '_state.csv'),
                              newline='') as statusFile:
                        reader = csv.reader(statusFile)
                        # loop past the header,
                        keys = []
                        vals = []
                        for row in reader:
                            if '# timestamp' in row:
                                keys = row
                                i = keys.index('# timestamp')
                                keys.pop(i)
                                vals = next(reader)
                                vals.pop(i)
                        for pos, key2 in enumerate(keys):
                            protDevFinalStatus[key2][phase] = vals[pos]
    except:
        pass
    #print protDevFinalStatus
    #compare initial and final states of protective devices
    #quick compare to see if they are equal
    #print cmp(protDevInitStatus, protDevFinalStatus)
    #find which values changed
    changedStates = {}
    #read voltDump values into a dictionary.
    try:
        with open(pJoin(workDir, 'voltDump.csv'), newline='') as dumpFile:
            reader = csv.reader(dumpFile)
            next(reader)  # Burn the header.
            keys = next(reader)
            voltTable = []
            for row in reader:
                rowDict = {}
                for pos, key in enumerate(keys):
                    rowDict[key] = row[pos]
                voltTable.append(rowDict)
    except:
        raise Exception(
            'GridLAB-D failed to run with the following errors:\n' +
            gridlabOut['stderr'])
    # read currDump values into a dictionary
    with open(pJoin(workDir, 'currDump.csv'), newline='') as currDumpFile:
        reader = csv.reader(currDumpFile)
        next(reader)  # Burn the header.
        keys = next(reader)
        currTable = []
        for row in reader:
            rowDict = {}
            for pos, key in enumerate(keys):
                rowDict[key] = row[pos]
            currTable.append(rowDict)
    # read line rating values into a single dictionary
    lineRatings = {}
    rating_in_VA = []
    for key1 in edge_bools.keys():
        if edge_bools[key1]:
            with open(pJoin(workDir, key1 + '_cont_rating.csv'),
                      newline='') as ratingFile:
                reader = csv.reader(ratingFile)
                # loop past the header,
                keys = []
                vals = []
                for row in reader:
                    if '# timestamp' in row:
                        keys = row
                        i = keys.index('# timestamp')
                        keys.pop(i)
                        vals = next(reader)
                        vals.pop(i)
                for pos, key2 in enumerate(keys):
                    lineRatings[key2] = abs(float(vals[pos]))
    #edgeTupleRatings = lineRatings copy with to-from tuple as keys for labeling
    edgeTupleRatings = {}
    for edge in lineRatings:
        for obj in tree.values():
            if obj.get('name', '').replace('"', '') == edge:
                nodeFrom = obj.get('from')
                nodeTo = obj.get('to')
                coord = (nodeFrom, nodeTo)
                ratingVal = lineRatings.get(edge)
                edgeTupleRatings[coord] = ratingVal
    # Calculate average node voltage deviation. First, helper functions.
    def digits(x):
        ''' Returns number of digits before the decimal in the float x. '''
        return math.ceil(math.log10(x + 1))

    def avg(l):
        ''' Average of a list of ints or floats. '''
        # HACK: add a small value to the denominator to avoid divide by zero for out of service locations (i.e. zero voltage).
        return sum(l) / (len(l) + 0.00000000000000001)

    # Detect the feeder nominal voltage:
    for key in tree:
        ob = tree[key]
        if type(ob) == dict and ob.get('bustype', '') == 'SWING':
            feedVoltage = float(ob.get('nominal_voltage', 1))
    # Tot it all up.
    nodeVolts = {}
    nodeVoltsPU = {}
    nodeVoltsPU120 = {}
    voltImbalances = {}
    for row in voltTable:
        allVolts = []
        allVoltsPU = []
        allDiffs = []
        nodeName = row.get('node_name', '')
        for phase in ['A', 'B', 'C']:
            realVolt = abs(float(row['volt' + phase + '_real']))
            imagVolt = abs(float(row['volt' + phase + '_imag']))
            phaseVolt = math.sqrt((realVolt**2) + (imagVolt**2))
            if phaseVolt != 0.0:
                treeKey = nameToIndex.get(nodeName, 0)
                nodeObj = tree.get(treeKey, {})
                try:
                    nominal_voltage = float(nodeObj['nominal_voltage'])
                except:
                    nominal_voltage = feedVoltage
                allVolts.append(phaseVolt)
                normVolt = (phaseVolt / nominal_voltage)
                allVoltsPU.append(normVolt)
        avgVolts = avg(allVolts)
        avgVoltsPU = avg(allVoltsPU)
        avgVoltsPU120 = 120 * avgVoltsPU
        nodeVolts[nodeName] = float("{0:.2f}".format(avgVolts))
        nodeVoltsPU[nodeName] = float("{0:.2f}".format(avgVoltsPU))
        nodeVoltsPU120[nodeName] = float("{0:.2f}".format(avgVoltsPU120))
        if len(allVolts) == 3:
            voltA = allVolts.pop()
            voltB = allVolts.pop()
            voltC = allVolts.pop()
            allDiffs.append(abs(float(voltA - voltB)))
            allDiffs.append(abs(float(voltA - voltC)))
            allDiffs.append(abs(float(voltB - voltC)))
            maxDiff = max(allDiffs)
            voltImbal = maxDiff / avgVolts
            voltImbalances[nodeName] = float("{0:.2f}".format(voltImbal))
        # Use float("{0:.2f}".format(avg(allVolts))) if displaying the node labels
    nodeLoadNames = {}
    nodeNames = {}
    for key in nodeVolts.keys():
        nodeNames[key] = key
        if key == loadLoc:
            nodeLoadNames[key] = "LOAD: " + key
    # find edge currents by parsing currdump
    edgeCurrentSum = {}
    edgeCurrentMax = {}
    for row in currTable:
        allCurr = []
        for phase in ['A', 'B', 'C']:
            realCurr = abs(float(row['curr' + phase + '_real']))
            imagCurr = abs(float(row['curr' + phase + '_imag']))
            phaseCurr = math.sqrt((realCurr**2) + (imagCurr**2))
            allCurr.append(phaseCurr)
        edgeCurrentSum[row.get('link_name', '')] = sum(allCurr)
        edgeCurrentMax[row.get('link_name', '')] = max(allCurr)
    # When just showing current as labels, use sum of the three lines' current values, when showing the per unit values (current/rating), use the max of the three
    #edgeTupleCurrents = edgeCurrents copy with to-from tuple as keys for labeling
    edgeTupleCurrents = {}
    #edgeValsPU = values normalized per unit by line ratings
    edgeValsPU = {}
    #edgeTupleValsPU = edgeValsPU copy with to-from tuple as keys for labeling
    edgeTupleValsPU = {}
    #edgeTuplePower = dict with to-from tuples as keys and sim power as values for debugging
    edgeTuplePower = {}
    #edgeTupleNames = dict with to-from tuples as keys and names as values for debugging
    edgeTupleNames = {}
    #edgeTupleFaultNames = dict with to-from tuples as keys and the name of the Fault as the only value
    edgeTupleFaultNames = {}
    #edgeTupleProtDevs = dict with to-from tuples as keys and the initial of the type of protective device as the value
    edgeTupleProtDevs = {}
    #linePhases = dictionary containing the number of phases on each line for line-width purposes
    linePhases = {}
    edgePower = {}
    for edge in edgeCurrentSum:
        for obj in tree.values():
            obname = obj.get('name', '').replace('"', '')
            if obname == edge:
                objType = obj.get('object')
                nodeFrom = obj.get('from')
                nodeTo = obj.get('to')
                coord = (nodeFrom, nodeTo)
                currVal = edgeCurrentSum.get(edge)
                voltVal = avg([nodeVolts.get(nodeFrom), nodeVolts.get(nodeTo)])
                power = (currVal * voltVal) / 1000
                lineRating = lineRatings.get(edge, 10.0**9)
                edgePerUnitVal = (edgeCurrentMax.get(edge)) / lineRating
                edgeTupleCurrents[coord] = "{0:.2f}".format(currVal)
                edgeTuplePower[coord] = "{0:.2f}".format(power)
                edgePower[edge] = power
                edgeValsPU[edge] = edgePerUnitVal
                edgeTupleValsPU[coord] = "{0:.2f}".format(edgePerUnitVal)
                edgeTupleNames[coord] = edge
                if faultLoc == edge:
                    edgeTupleFaultNames[coord] = "FAULT: " + edge
                phaseStr = obj.get('phases', '').replace('"', '').replace(
                    'N', '').replace('S', '')
                numPhases = len(phaseStr)
                if (numPhases < 1) or (numPhases > 3):
                    numPhases = 1
                linePhases[edge] = numPhases
                protDevLabel = ""
                protDevBlownStr = ""
                if objType in protDevices.keys():
                    for phase in protDevFinalStatus[obname].keys():
                        if objType == 'fuse':
                            if protDevFinalStatus[obname][phase] == "BLOWN":
                                protDevBlownStr = "!"
                        else:
                            if protDevFinalStatus[obname][phase] == "OPEN":
                                protDevBlownStr = "!"
                if objType == 'fuse':
                    protDevLabel = 'F'
                elif objType == 'switch':
                    protDevLabel = 'S'
                elif objType == 'recloser':
                    protDevLabel = 'R'
                elif objType == 'sectionalizer':
                    protDevLabel = 'X'
                edgeTupleProtDevs[coord] = protDevLabel + protDevBlownStr
    #define which dict will be used for edge line color
    edgeColors = edgeValsPU
    #define which dict will be used for edge label
    edgeLabels = edgeTupleValsPU
    # Build the graph.
    fGraph = feeder.treeToNxGraph(tree)
    # TODO: consider whether we can set figsize dynamically.
    wlVal = int(math.sqrt(float(rezSqIn)))
    voltChart = plt.figure(figsize=(wlVal, wlVal))
    plt.axes(frameon=0)
    plt.axis('off')
    voltChart.gca().set_aspect('equal')
    plt.tight_layout()
    #set axes step equal
    if neatoLayout:
        # HACK: work on a new graph without attributes because graphViz tries to read attrs.
        cleanG = nx.Graph(fGraph.edges())
        cleanG.add_nodes_from(fGraph)
        positions = graphviz_layout(cleanG, prog='neato')
    else:
        remove_nodes = [
            n for n in fGraph if fGraph.nodes[n].get('pos', (0, 0)) == (0, 0)
        ]
        fGraph.remove_nodes_from(remove_nodes)
        positions = {n: fGraph.nodes[n].get('pos', (0, 0)) for n in fGraph}
    # Need to get edge names from pairs of connected node names.
    edgeNames = []
    for e in fGraph.edges():
        edgeNames.append((fGraph.edges[e].get('name',
                                              'BLANK')).replace('"', ''))
    #create custom colormap
    if customColormap:
        if scaleMin != None and scaleMax != None:
            scaleDif = scaleMax - scaleMin
            custom_cm = matplotlib.colors.LinearSegmentedColormap.from_list(
                'custColMap', [(scaleMin, 'blue'),
                               (scaleMin + (0.12 * scaleDif), 'darkgray'),
                               (scaleMin + (0.56 * scaleDif), 'darkgray'),
                               (scaleMin + (0.8 * scaleDif), 'red')])
            vmin = scaleMin
            vmax = scaleMax
        else:
            custom_cm = matplotlib.colors.LinearSegmentedColormap.from_list(
                'custColMap', [(0.0, 'blue'), (0.15, 'darkgray'),
                               (0.7, 'darkgray'), (1.0, 'red')])
            vmin = 0
            vmax = 1.25
        custom_cm.set_under(color='black')
    else:
        custom_cm = plt.cm.get_cmap('viridis')
        if scaleMin != None and scaleMax != None:
            vmin = scaleMin
            vmax = scaleMax
        else:
            vmin = None
            vmax = None
    drawColorbar = False
    emptyColors = {}
    #draw edges with or without colors
    if edgeCol != None:
        drawColorbar = True
        if edgeCol == "Current":
            edgeList = [edgeCurrentSum.get(n, 1) for n in edgeNames]
            drawColorbar = True
        elif edgeCol == "Power":
            edgeList = [edgePower.get(n, 1) for n in edgeNames]
            drawColorbar = True
        elif edgeCol == "Rating":
            edgeList = [lineRatings.get(n, 10.0**9) for n in edgeNames]
            drawColorbar = True
        elif edgeCol == "PercentOfRating":
            edgeList = [edgeValsPU.get(n, .5) for n in edgeNames]
            drawColorbar = True
        else:
            edgeList = [emptyColors.get(n, .6) for n in edgeNames]
            print(
                "WARNING: edgeCol property must be 'Current', 'Power', 'Rating', 'PercentOfRating', or None"
            )
    else:
        edgeList = [emptyColors.get(n, .6) for n in edgeNames]
    edgeIm = nx.draw_networkx_edges(
        fGraph,
        pos=positions,
        edge_color=edgeList,
        width=[linePhases.get(n, 1) for n in edgeNames],
        edge_cmap=custom_cm)
    #draw edge labels
    if edgeLabs != None:
        if edgeLabs == "Name":
            edgeLabels = edgeTupleNames
        elif edgeLabs == "Fault":
            edgeLabels = edgeTupleFaultNames
        elif edgeLabs == "Value":
            if edgeCol == "Current":
                edgeLabels = edgeTupleCurrents
            elif edgeCol == "Power":
                edgeLabels = edgeTuplePower
            elif edgeCol == "Rating":
                edgeLabels = edgeTupleRatings
            elif edgeCol == "PercentOfRating":
                edgeLabels = edgeTupleValsPU
            else:
                edgeLabels = None
                print(
                    "WARNING: edgeCol property cannot be set to None when edgeLabs property is set to 'Value'"
                )
        elif edgeLabs == "ProtDevs":
            edgeLabels = edgeTupleProtDevs
        else:
            edgeLabs = None
            print(
                "WARNING: edgeLabs property must be either 'Name', 'Value', or None"
            )
    if edgeLabs != None:
        edgeLabelsIm = nx.draw_networkx_edge_labels(fGraph,
                                                    pos=positions,
                                                    edge_labels=edgeLabels,
                                                    font_size=8)
    # draw nodes with or without color
    if nodeCol != None:
        if nodeCol == "Voltage":
            nodeList = [nodeVolts.get(n, 1) for n in fGraph.nodes()]
            drawColorbar = True
        elif nodeCol == "VoltageImbalance":
            nodeList = [voltImbalances.get(n, 1) for n in fGraph.nodes()]
            drawColorbar = True
        elif nodeCol == "perUnitVoltage":
            nodeList = [nodeVoltsPU.get(n, .5) for n in fGraph.nodes()]
            drawColorbar = True
        elif nodeCol == "perUnit120Voltage":
            nodeList = [nodeVoltsPU120.get(n, 120) for n in fGraph.nodes()]
            drawColorbar = True
        else:
            nodeList = [emptyColors.get(n, 1) for n in fGraph.nodes()]
            print(
                "WARNING: nodeCol property must be 'Voltage', 'VoltageImbalance', 'perUnitVoltage', 'perUnit120Voltage', or None"
            )
    else:
        nodeList = [emptyColors.get(n, .6) for n in fGraph.nodes()]

    nodeIm = nx.draw_networkx_nodes(fGraph,
                                    pos=positions,
                                    node_color=nodeList,
                                    linewidths=0,
                                    node_size=30,
                                    vmin=vmin,
                                    vmax=vmax,
                                    cmap=custom_cm)
    #draw node labels
    nodeLabels = {}
    if nodeLabs != None:
        if nodeLabs == "Name":
            nodeLabels = nodeNames
        elif nodeLabs == "Value":
            if nodeCol == "Voltage":
                nodeLabels = nodeVolts
            elif nodeCol == "VoltageImbalance":
                nodeLabels = voltImbalances
            elif nodeCol == "perUnitVoltage":
                nodeLabels = nodeVoltsPU
            elif nodeCol == "perUnit120Voltage":
                nodeLabels = nodeVoltsPU120
            else:
                nodeLabels = None
                print(
                    "WARNING: nodeCol property cannot be set to None when nodeLabs property is set to 'Value'"
                )
        #HACK: add hidden node label option for displaying specified load name
        elif nodeLabs == "Load":
            nodeLabels = nodeLoadNames
        else:
            nodeLabs = None
            print(
                "WARNING: nodeLabs property must be either 'Name', 'Value', or None"
            )
    if nodeLabs != None:
        nodeLabelsIm = nx.draw_networkx_labels(fGraph,
                                               pos=positions,
                                               labels=nodeLabels,
                                               font_size=8)
    plt.sci(nodeIm)
    # plt.clim(110,130)
    if drawColorbar:
        plt.colorbar()
    return voltChart
Example #57
0
    G0.rtt = {}
    for n in G0:
        G0.rtt[n] = time[n]

    return G0


if __name__ == '__main__':

    G = lanl_graph()

    print("graph has %d nodes with %d edges"
          % (nx.number_of_nodes(G), nx.number_of_edges(G)))
    print(nx.number_connected_components(G), "connected components")

    plt.figure(figsize=(8, 8))
    # use graphviz to find radial layout
    pos = graphviz_layout(G, prog="twopi", root=0)
    # draw nodes, coloring by rtt ping time
    nx.draw(G, pos,
            node_color=[G.rtt[v] for v in G],
            with_labels=False,
            alpha=0.5,
            node_size=15)
    # adjust the plot limits
    xmax = 1.02 * max(xx for xx, yy in pos.values())
    ymax = 1.02 * max(yy for xx, yy in pos.values())
    plt.xlim(0, xmax)
    plt.ylim(0, ymax)
    plt.show()
Example #58
0
def plot_nx_succession_diagram(g,
                               fig_dimensions=[],
                               pos='pydot',
                               detailed_labels=True,
                               node_size=[],
                               node_color='grey',
                               font_size=12,
                               font_color='black'):
    """Plot the input succession diagram. Requires matplotlib. For finer control
    over plot appearance, it is recommended to plot g directly.

    Parameters
    ----------
    g : networkx.DiGraph
        Labeled succession diagram, e.g., as is output from
        Export.networkx_succession_diagram_reduced_network_based().
    fig_dimensions : (int,int)
        Dimensions of the output figure. If [], then the dimensions are calculated
        based on the number of nodes in g (the default is []).
    pos : str or graphviz_layout
        Layout for the node labels; if 'pydot', these are automatically computed
        using pydot (requires pydot) (the default is 'pydot').
    detailed_labels : bool
        Whether node labels should be drawn (True) or left as metadata (False)
        (the default is True).
    node_size : int
        Size of the nodes. If [], the size is proportional to the number of nodes
        in g (the default is []).
    node_color : color or array of colors
        Node color. Can be a single color or a sequence of colors with the same
        length as nodelist. (the default is 'grey').
    font_size : int
        Font size for labels (the default is 12).
    font_color : str
        Color for label text (the default is 'black').

    """

    from networkx.drawing.nx_agraph import graphviz_layout
    import matplotlib.pyplot as plt

    if fig_dimensions == []:
        fig_dimensions = (2 * (g.number_of_nodes() + 2),
                          g.number_of_nodes() + 2)
    if node_size == []:
        node_size = 50 * g.number_of_nodes()

    if pos == 'pydot':
        pos = graphviz_layout(g, prog='dot')

    plt.figure(figsize=fig_dimensions)
    nx.drawing.draw_networkx_nodes(g,
                                   pos,
                                   node_shape='s',
                                   node_color=node_color,
                                   node_size=node_size)
    nx.draw_networkx_edges(g, pos, arrowstyle='fancy', arrowsize=10)
    if detailed_labels:
        nx.drawing.draw_networkx_labels(g,
                                        pos,
                                        labels=dict(g.nodes('label')),
                                        font_size=font_size,
                                        font_color=font_color)
    else:
        nx.drawing.draw_networkx_labels(g,
                                        pos,
                                        font_size=font_size,
                                        font_color=font_color)
    plt.axis('off')
    plt.show()
Example #59
0
def generateVoltChart(tree, rawOut, modelDir, neatoLayout=True):
    ''' Map the voltages on a feeder over time using a movie.'''
    # We need to timestamp frames with the system clock to make sure the browser caches them appropriately.
    genTime = str(datetime.datetime.now()).replace(':', '.')
    # Detect the feeder nominal voltage:
    for key in tree:
        ob = tree[key]
        if type(ob) == dict and ob.get('bustype', '') == 'SWING':
            feedVoltage = float(ob.get('nominal_voltage', 1))
    # Make a graph object.
    fGraph = feeder.treeToNxGraph(tree)
    if neatoLayout:
        # HACK: work on a new graph without attributes because graphViz tries to read attrs.
        cleanG = nx.Graph(fGraph.edges())
        cleanG.add_nodes_from(fGraph)
        # was formerly : positions = nx.graphviz_layout(cleanG, prog='neato') but this threw an error
        positions = graphviz_layout(cleanG, prog='neato')
    else:
        rawPositions = {n: fGraph.node[n].get('pos', (0, 0)) for n in fGraph}

        # HACK: the import code reverses the y coords.
        def yFlip(pair):
            try:
                return (pair[0], -1.0 * pair[1])
            except:
                return (0, 0)

        positions = {k: yFlip(rawPositions[k]) for k in rawPositions}
    # Plot all time steps.
    nodeVolts = {}
    for step, stamp in enumerate(rawOut['aVoltDump.csv']['# timestamp']):
        # Build voltage map.
        nodeVolts[step] = {}
        for nodeName in [
                x for x in rawOut.get('aVoltDump.csv', {}).keys() +
                rawOut.get('1nVoltDump.csv', {}).keys() +
                rawOut.get('1mVoltDump.csv', {}).keys() if x != '# timestamp'
        ]:
            allVolts = []
            for phase in ['a', 'b', 'c', '1n', '2n', '1m', '2m']:
                try:
                    voltStep = rawOut[phase + 'VoltDump.csv'][nodeName][step]
                except:
                    continue  # the nodeName doesn't have the phase we're looking for.
                # HACK: Gridlab complex number format sometimes uses i, sometimes j, sometimes d. WTF?
                if type(voltStep) is str: voltStep = voltStep.replace('i', 'j')
                v = complex(voltStep)
                phaseVolt = abs(v)
                if phaseVolt != 0.0:
                    if _digits(phaseVolt) > 3:
                        # Normalize to 120 V standard
                        phaseVolt = phaseVolt * (120 / feedVoltage)
                    allVolts.append(phaseVolt)
            # HACK: Take average of all phases to collapse dimensionality.
            nodeVolts[step][nodeName] = avg(allVolts)
    # Draw animation.
    voltChart = plt.figure(figsize=(15, 15))
    plt.axes(frameon=0)
    plt.axis('off')
    #set axes step equal
    voltChart.gca().set_aspect('equal')
    custom_cm = matplotlib.colors.LinearSegmentedColormap.from_list(
        'custColMap', [(0.0, 'blue'), (0.25, 'darkgray'), (0.75, 'darkgray'),
                       (1.0, 'yellow')])
    custom_cm.set_under(color='black')
    edgeIm = nx.draw_networkx_edges(fGraph, positions)
    nodeIm = nx.draw_networkx_nodes(
        fGraph,
        pos=positions,
        node_color=[nodeVolts[0].get(n, 0) for n in fGraph.nodes()],
        linewidths=0,
        node_size=30,
        cmap=custom_cm)
    plt.sci(nodeIm)
    plt.clim(110, 130)
    plt.colorbar()
    plt.title(rawOut['aVoltDump.csv']['# timestamp'][0])

    def update(step):
        nodeColors = np.array(
            [nodeVolts[step].get(n, 0) for n in fGraph.nodes()])
        plt.title(rawOut['aVoltDump.csv']['# timestamp'][step])
        nodeIm.set_array(nodeColors)
        return nodeColors,

    anim = FuncAnimation(voltChart,
                         update,
                         frames=len(rawOut['aVoltDump.csv']['# timestamp']),
                         interval=200,
                         blit=False)
    anim.save(pJoin(modelDir, 'voltageChart.mp4'),
              codec='h264',
              extra_args=['-pix_fmt', 'yuv420p'])
    # Reclaim memory by closing, deleting and garbage collecting the last chart.
    voltChart.clf()
    plt.close()
    del voltChart
    gc.collect()
    return genTime
Example #60
0
with open(sys.argv[1]) as f:
    for line in f:
        adapters.append(int(line.strip()))
adapters.sort()
device = adapters[-1] + 3
adapters = [0] + adapters + [device]

labels = {}
G = nx.DiGraph()
for a in adapters:
    labels[a] = a
    reachable = [x for x in adapters if 1 <= x - a <= 3]
    for b in reachable:
        G.add_edge(a, b)

# plt.figure(figsize=(15, 15))

# nx.draw_kamada_kawai(G)
# node_colors = ["red" if x == "shiny gold" else "C0" for x in G.nodes()]
pos = graphviz_layout(G, prog="neato")
print(pos)
nx.draw(G, pos, labels=labels, font_color="white", font_size=10, arrowsize=14)
# nx.draw(G, pos, node_color=node_colors, arrowsize=12, node_size=10, edge_color=(0, 0, 0, 0.2))
# edge_labels = nx.get_edge_attributes(G,'weight')
# nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)

if "--save" in sys.argv:
    plt.savefig("day10_viz.svg")
else:
    plt.show()