Exemplo n.º 1
0
class PlotNetwork:
    def __init__(self):
        self.plot_object = PlotBot()
        self.b_conflicts = self.plot_object.b_conflicts
        self.labels = {}
        self.root = ""
        self.graph = None

    def generate_network(self, max_nodes=None):
        """Generates a network from the b_conflicts in the plot_object, with
        nodes as conflicts and edges as suggested linked conflicts as per the
        Plotto text. User can set the max_nodes argument if they wish to produce
        a sub_graph of max_nodes nodes

        :param max_nodes: set the limit to the number of nodes generated in the network.
        :return: Returns a networkx DiGraph G

        """
        self.graph = nx.DiGraph()
        first = True
        i = 0
        for conflict in self.b_conflicts:
            if max_nodes is not None and nx.number_of_nodes(self.graph) >= max_nodes:
                break
            curr = self.b_conflicts[conflict]
            self.labels[curr.label] = curr.label
            if first:
                self.root = curr.label
                first = False
            self.graph.add_node(curr.label, object_data=curr)
            for sequel in self.plot_object.get_links(self.b_conflicts[conflict], "sequels"):
                self.graph.add_edge(curr.label, sequel.label)
                self.labels[sequel.label] = sequel.label
            for prequel in self.plot_object.get_links(self.b_conflicts[conflict], "prequels"):
                self.graph.add_edge(prequel.label, curr.label)
                self.labels[prequel.label] = prequel.label

            i += 1
        return self.graph

    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()

    def draw_node_color_map(self):
        """ Draws a node color map based on node degree from
            the previously computed graph.
        :return: None
        """
        pos = nx.spring_layout(self.graph, iterations=2000)
        nx.draw(self.graph, pos, node_color=range(nx.number_of_nodes(self.graph)), node_size=100, cmap=plt.cm.Blues)
        plt.savefig("../../College/FYP/FinalReport/images/node_colormap.png")  # save as png
        plt.show()  # display

    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")