Beispiel #1
0
def plot_samples(feature_input, values, vmin=None, vmax=None, ax=None):
    """Given a FeatureInput and a length-N ndarray of edge values, plot
    the edges

    """
    import matplotlib.pyplot
    g = feature_input.road_network.network
    imc = feature_input.base_layer.image_coords_from_lon_lat
    pos = (imc(*ll) for ll in feature_input.road_network.node_position())
    pos = dict(zip(g.node, pos))
    simp_edge_values = dict(
        zip(features.get_feature_axes(feature_input)[0], values))
    edge_list = list(g.edges)
    edge_values = [
        simp_edge_values[simplify_edge(feature_input, e)] for e in edge_list
    ]
    nx_draw.draw_networkx_edges(
        g,
        pos,
        edge_list,
        edge_color=edge_values,
        edge_cmap=matplotlib.cm.get_cmap('coolwarm'),
        edge_vmin=vmin,
        edge_vmax=vmax,
        ax=ax,
        arrows=False,
    )
    if ax is None: ax = matplotlib.pyplot.gca()
    ax.set_xlim([0, feature_input.base_layer.res_x])
    ax.set_ylim([feature_input.base_layer.res_y, 0])
    ax.set_aspect('auto')
Beispiel #2
0
def main(fn: str):
    infile = Path(f'{fn}')
    outfile = infile.parent.joinpath(infile.stem + '.pdf')
    outfilebw = infile.parent.joinpath(infile.stem + '.pdf.bw.txt')
    topo = json.loads(infile.read_text())
    nxg = nx_graph_from_topo(topo)
    pos = spring_layout(nxg, iterations=4000)
    fig = Figure()
    fig.set_dpi(300)
    ax = fig.add_subplot(111)
    ax.axis('off')
    labels = dict()
    for edge in nxg.edges:
        bw = nxg.get_edge_data(edge[0], edge[1], dict()).get('bw', None)
        bw = '' if bw is None else '%.1f mbps' % (bw, )
        labels[edge] = bw
    if len(set(labels.values())) > 1:
        draw_networkx_edge_labels(nxg,
                                  pos,
                                  ax=ax,
                                  edge_labels=labels,
                                  bbox=dict(facecolor='#FFFFFF88',
                                            edgecolor='none'),
                                  font_size='x-small')
    else:
        sbw = next(iter(set(labels.values())))
        outfilebw.write_text(sbw)
    draw_networkx_edges(nxg,
                        pos,
                        ax=ax,
                        edgelist=nxg.edges,
                        edge_color='black')
    draw_networkx_nodes(nxg,
                        pos,
                        ax=ax,
                        nodelist=topo[0],
                        node_color='lightgreen',
                        edgecolors="green")
    draw_networkx_nodes(nxg,
                        pos,
                        ax=ax,
                        nodelist=topo[1],
                        node_color='cyan',
                        edgecolors="darkcyan")
    draw_networkx_labels(nxg, pos, ax=ax, font_size='small')
    fig.savefig(str(outfile))
 def draw(self):
     G = netx.DiGraph()
     for u in range(1, g.n + 1):
         G.add_node(u, desc=str(u))
     for u in range(1, g.n + 1):
         for j in range(len(g.G[u])):
             v = g.G[u][j][0]
             G.add_edge(u, v, weight=g.G[u][j][1])
     self.get_conn_style()
     nxdraw.draw_networkx_nodes(G, self.posG)
     nxdraw.draw_networkx_edges(G, self.posG, edgelist=self.G_straight)
     nxdraw.draw_networkx_edges(G,
                                self.posG,
                                edgelist=self.G_curve,
                                connectionstyle='arc3,rad = 0.2')
     node_labels = netx.get_node_attributes(G, 'desc')
     netx.draw_networkx_labels(G, pos=self.posG, labels=node_labels)
     edge_labels = netx.get_edge_attributes(G, 'weight')
     netx.draw_networkx_edge_labels(G,
                                    pos=self.posG,
                                    edge_labels=edge_labels)
     plt.show()
Beispiel #4
0
    def draw_tree(self, treeKey, net=None):
        """
        Show tree representation of network.

        Attributes:
            treeKey (str):
                key in nodes memory (dictionary) where tree data is stored
                storage format can be a list off tree neighbors or a dict:
                    {'parent': parent_node,
                     'children': [child_node1, child_node2 ...]}
        """
        if not net:
            net = self.net
        treeNet = net.get_tree_net(treeKey)
        if treeNet:
            self.tree_collection = draw_networkx_edges(
                treeNet, treeNet.pos, treeNet.edges(), width=1.8, alpha=0.6, ax=self.axes
            )
Beispiel #5
0
    def draw_tree(self, treeKey, net=None):
        """
        Show tree representation of network.

        Attributes:
            treeKey (str):
                key in nodes memory (dictionary) where tree data is stored
                storage format can be a list off tree neighbors or a dict:
                    {'parent': parent_node,
                     'children': [child_node1, child_node2 ...]}
        """
        if not net:
            net = self.net
        treeNet = net.get_tree_net(treeKey)
        if treeNet:
            self.tree_collection = draw_networkx_edges(treeNet,
                                                       treeNet.pos,
                                                       treeNet.edges(),
                                                       width=1.8,
                                                       alpha=0.6,
                                                       ax=self.axes)
Beispiel #6
0
def plot(ax, currentfile):
    ax.clear()
    ax.axis('off')
    if not currentfile.exists():
        return
    toponame = currentfile.read_text().strip()
    statepath = Path(f'{toponame}.state')
    if toponame not in topos:
        topos[toponame] = json.loads(Path(f'{toponame}.json').read_text())
    topo = topos[toponame]
    if toponame not in nxgs:
        nxgs[toponame] = nx_graph_from_topo(topo)
    nxg = nxgs[toponame]
    if toponame not in topoPos:
        topoPos[toponame] = spring_layout(nxg, iterations=2000)
    pos = topoPos[toponame]
    if toponame not in topoDjkt:
        djkt = Dijkstra(graph_from_topo(topo))
        djkt_pairs = list()
        for h1 in topo[0]:
            for h2 in topo[0]:
                if h1 != h2:
                    h1, h2 = _sort_pair(h1, h2)
                    djkt_seq = djkt(h1)(h2)[0]
                    for i in range(len(djkt_seq) - 1):
                        djkt_pairs.append(_sort_pair(*djkt_seq[i:i + 2]))
        topoDjkt[toponame] = djkt_pairs
    djkt = topoDjkt[toponame]
    state_txt = ""
    if statepath.exists():
        state_txt = statepath.read_text()
    paths, path_segments, loads = parse_state(state_txt)
    labels = dict()
    for unsortededge in nxg.edges:
        edge = _sort_pair(*list(map(str, unsortededge)))
        labels[unsortededge] = "%0.4f" % (loads.get(edge, 0.0), )
        # labels[unsortededge] = repr(loads.get(edge, 0.0),)
    edlab = draw_networkx_edge_labels(nxg,
                                      pos,
                                      ax=ax,
                                      edge_labels=labels,
                                      bbox=dict(facecolor='none',
                                                edgecolor='none'))
    loaded_sws = set(topo[1]).intersection(get_loaded_switches())
    unloaded_sws = set(topo[1]).difference(loaded_sws)
    for unsortededge in nxg.edges:
        edge = _sort_pair(*list(map(str, unsortededge)))
        in_djkt = edge in djkt
        in_sgmt = min(
            1.0,
            max(
                0.0,
                path_segments.get(
                    edge, path_segments.get(tuple(reversed(edge)), 0.0))))
        in_sgmt = ('0' + (hex(round(in_sgmt * 255))[2:]))[-2:].upper()
        in_unld = (edge[0] in unloaded_sws) or (edge[1] in unloaded_sws)
        color = '#' + CLR[in_djkt] + CLR[in_unld] + in_sgmt
        draw_networkx_edges(nxg, pos, ax=ax, edgelist=[edge], edge_color=color)
    draw_networkx_nodes(nxg,
                        pos,
                        ax=ax,
                        nodelist=topo[0],
                        node_color='lightgreen')
    draw_networkx_nodes(nxg,
                        pos,
                        ax=ax,
                        nodelist=unloaded_sws,
                        node_color='pink')
    draw_networkx_nodes(nxg,
                        pos,
                        ax=ax,
                        nodelist=loaded_sws,
                        node_color='cyan')
    draw_networkx_labels(nxg, pos, ax=ax)