Example #1
0
class NetDisplay:
    def __init__(self, net, LinGen, name):
        self.net = net
        self.LinGen = LinGen
        self.Grafo = Network(height="600px", width="1000px")
        self.name = name

        self.Grafo.toggle_physics(False)
        self.Grafo.inherit_edge_colors_from(False)

    def nodesPlot(self):
        self.CalcNeuronsYpositions()
        for layer in self.net:
            neurons = layer.ReturnNeurons()
            y_positions = layer.NeuronYPosition
            for neuron, y_pos in zip(neurons, y_positions):
                self.Grafo.add_node(
                    neuron, x=layer.number * 100, y=y_pos * 100)

        i = 0
        for con in self.LinGen:
            if con.enable:
                if con.weight >= 0:
                    color = 'blue'
                else:
                    color = 'red'

                if con.recurrent:
                    dashes = 'true'
                else:
                    dashes = 'false'

                try:
                    self.Grafo.add_edge(
                        con.input, con.output, value=abs(con.weight))
                    self.Grafo.edges[i].update(
                        {'color': color, 'arrows': 'to', 'arrowStrikethrough': 'false',
                         'title': 'IN: ' + str(con.innovation_number), 'dashes': dashes, 'shadow': dashes})
                    i += 1
                except:
                    pass

        self.Grafo.show_buttons()
        self.Grafo.set_edge_smooth('dynamic')
        self.Grafo.show(self.name + ".html")

    def CalcNeuronsYpositions(self):
        for layer in self.net:
            neuronsYposition = []
            size = len(layer.ReturnNeurons())
            if size % 2 == 0:
                for i in range(0, size // 2):
                    neuronsYposition += [i + 0.5, -i - 0.5]
            else:
                neuronsYposition = [0]
                for i in range(1, size // 2 + 1):
                    neuronsYposition += [i, -i]
            layer.NeuronYPosition = neuronsYposition
    def see_graph(self,
                  htmlname='MetabolicNetwork.html',
                  height="1080px",
                  width="1080px",
                  colorHighNodes=5):
        '''
        #***DEPRECATED METHOD***#
        #***use see_graph2***#
        Provides a visual representation of the network using pyvis
        Metabolites are represented by yellow dots
        Reationss are represented by green triangles
        Metabolite Highlights are represented by red dots and denote highest in_degrees metabolites
        if nodes are not metabolites or reaction default blue dot is used

        :param htmlname:(str) name of exit file
        :param height:(num) height of html window display
        :param width:(num) width of html window display
        :param colorHighNodes:(int) number of nodes to color highlight, if 0 will not highlight any node

        :return: creates html file and opens it on default browser
        '''
        nt = Network(height, width)
        nt.from_nx(self.mnetwork)

        for dit in nt.nodes:
            if str(dit['title']).startswith('R'):
                dit['shape'] = 'triangle'
                dit['color'] = 'green'
            if str(dit['title']).startswith('M'):
                dit['color'] = 'rgb(255, 211, 0)'
        nt.show_buttons(filter_=['physics', 'edges'])
        nt.inherit_edge_colors_from(False)

        if colorHighNodes > 0:
            mustcolor = self.biggestM_indegrees(colorHighNodes)
            for dit2 in nt.nodes:
                if str(dit2['title']) in mustcolor:
                    dit2['color'] = 'red'

        nt.barnes_hut(gravity=-3000)
        nt.toggle_stabilization(True)

        nt.show(htmlname)