Пример #1
0
 def test_from_networkx_custom_nodes(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.375)])
     nodes = Dataset([(1, 'A'), (2, 'B'), (3, 'A'), (4, 'B')], 'index', 'some_attribute')
     graph = Graph.from_networkx(FG, nx.circular_layout, nodes=nodes)
     self.assertEqual(graph.nodes.dimension_values('some_attribute'), np.array(['A', 'B', 'A', 'B']))
Пример #2
0
 def test_from_networkx_dictionary_positions(self):
     import networkx as nx
     G = nx.Graph()
     G.add_nodes_from([1, 2, 3])
     positions = nx.circular_layout(G)
     graph = Graph.from_networkx(G, positions)
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
Пример #3
0
 def test_from_networkx_with_invalid_edge_attrs(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1, 2, []), (1, 3, []), (2, 4, []),
                                 (3, 4, [])])
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.vdims, [])
Пример #4
0
 def test_from_networkx_dictionary_positions(self):
     import networkx as nx
     G = nx.Graph()
     G.add_nodes_from([1, 2, 3])
     positions = nx.circular_layout(G)
     graph = Graph.from_networkx(G, positions)
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
Пример #5
0
 def test_from_networkx_with_edge_attrs(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2),
                                 (3, 4, 0.375)])
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.dimension_values('weight'),
                      np.array([0.125, 0.75, 1.2, 0.375]))
Пример #6
0
 def test_from_networkx_only_nodes(self):
     try:
         import networkx as nx
     except:
         raise SkipTest('Test requires networkx to be installed')
     G = nx.Graph()
     G.add_nodes_from([1, 2, 3])
     graph = Graph.from_networkx(G, nx.circular_layout)
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
Пример #7
0
 def test_from_networkx_custom_nodes(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2),
                                 (3, 4, 0.375)])
     nodes = Dataset([(1, 'A'), (2, 'B'), (3, 'A'), (4, 'B')], 'index',
                     'some_attribute')
     graph = Graph.from_networkx(FG, nx.circular_layout, nodes=nodes)
     self.assertEqual(graph.nodes.dimension_values('some_attribute'),
                      np.array(['A', 'B', 'A', 'B']))
Пример #8
0
 def test_from_networkx_with_invalid_node_attrs(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_node(1, test=[])
     FG.add_node(2, test=[])
     FG.add_edge(1, 2)
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.nodes.vdims, [])
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2]))
     self.assertEqual(graph.array(), np.array([(1, 2)]))
Пример #9
0
 def test_from_networkx_with_invalid_node_attrs(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_node(1, test=[])
     FG.add_node(2, test=[])
     FG.add_edge(1, 2)
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.nodes.vdims, [])
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2]))
     self.assertEqual(graph.array(), np.array([(1, 2)]))
Пример #10
0
 def test_from_networkx_with_edge_attrs(self):
     try:
         import networkx as nx
     except:
         raise SkipTest('Test requires networkx to be installed')
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2),
                                 (3, 4, 0.375)])
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.dimension_values('weight'),
                      np.array([0.125, 0.75, 1.2, 0.375]))
Пример #11
0
 def test_from_networkx_with_node_attrs(self):
     import networkx as nx
     G = nx.karate_club_graph()
     graph = Graph.from_networkx(G, nx.circular_layout)
     clubs = np.array([
         'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi',
         'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Mr. Hi',
         'Mr. Hi', 'Mr. Hi', 'Officer', 'Officer', 'Mr. Hi', 'Mr. Hi',
         'Officer', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Officer',
         'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer',
         'Officer', 'Officer', 'Officer', 'Officer'])
     self.assertEqual(graph.nodes.dimension_values('club'), clubs)
Пример #12
0
 def test_from_networkx_with_node_attrs(self):
     import networkx as nx
     G = nx.karate_club_graph()
     graph = Graph.from_networkx(G, nx.circular_layout)
     clubs = np.array([
         'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi',
         'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Mr. Hi',
         'Mr. Hi', 'Mr. Hi', 'Officer', 'Officer', 'Mr. Hi', 'Mr. Hi',
         'Officer', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Officer',
         'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer',
         'Officer', 'Officer', 'Officer', 'Officer'
     ])
     self.assertEqual(graph.nodes.dimension_values('club'), clubs)
Пример #13
0
 def test_from_networkx_with_node_attrs(self):
     try:
         import networkx as nx
     except:
         raise SkipTest('Test requires networkx to be installed')
     G = nx.karate_club_graph()
     graph = Graph.from_networkx(G, nx.circular_layout)
     clubs = np.array([
         'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi',
         'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Mr. Hi',
         'Mr. Hi', 'Mr. Hi', 'Officer', 'Officer', 'Mr. Hi', 'Mr. Hi',
         'Officer', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Officer',
         'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer',
         'Officer', 'Officer', 'Officer', 'Officer'
     ])
     self.assertEqual(graph.nodes.dimension_values('club'), clubs)
Пример #14
0
def generateGraph():
    df = processCSVMatrix(file)
    names = df.columns
    # submatrix for quicker development
    if (len(names) > 150):
        df = df.head(150)[names[0:150]]
    # set defaults for HoloViews
    extension('bokeh')
    renderer('bokeh').webgl = True
    defaults = dict(width=400, height=400, padding=0.1)
    opts.defaults(opts.EdgePaths(**defaults), opts.Graph(**defaults),
                  opts.Nodes(**defaults))

    G = from_pandas_adjacency(df)
    graph = Graph.from_networkx(G,
                                circular_layout).opts(directed=False,
                                                      width=600,
                                                      height=600,
                                                      arrowhead_length=0.0005)
    # Make a panel and widgets with param for choosing a layout
    return pn.Column(graph)
Пример #15
0
 def test_from_networkx_only_nodes(self):
     import networkx as nx
     G = nx.Graph()
     G.add_nodes_from([1, 2, 3])
     graph = Graph.from_networkx(G, nx.circular_layout)
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
Пример #16
0
        def make_plot(self):
            from graphion.session.handler import get_directed  # dependency cycle fix

            if get_directed(self.sid):
                G = from_pandas_adjacency(df, create_using=DiGraph)
            else:
                G = from_pandas_adjacency(df, create_using=Graph)
            self.nodeCount = number_of_nodes(G)
            """
            Create NetworkX graph layout manager
            """
            if diagramType == "FORCE":
                layout = spring_layout(G,
                                       k=10.42 / sqrt(self.nodeCount),
                                       seed=server.config['SEED'])
            elif diagramType == "HIERARCHICAL":
                if self.nodeCount > 1:
                    layout = graphviz_layout(Graph([
                        (u, v, d) for u, v, d in G.edges(data=True)
                    ]),
                                             prog='dot')
                else:
                    layout = circular_layout(
                        G
                    )  # graphviz_layout does not work with one node, just display a "circular_layout"
            elif diagramType == "RADIAL":
                layout = circular_layout(G)
            else:
                pass

            # get node and edge information from graph
            nodes, nodes_coordinates = zip(*sorted(layout.items()))
            nodes_x, nodes_y = list(zip(*nodes_coordinates))

            # calculate centrality
            centrality = degree_centrality(G)
            _, nodeCentralities = zip(*sorted(centrality.items()))

            if self.nodeCount > 1:
                # get degree information
                if is_directed(G):
                    inDegreeSize = dict(G.in_degree)
                    inDegree = inDegreeSize.copy()
                    outDegreeSize = dict(G.out_degree)
                    outDegree = outDegreeSize.copy()
                    totalDegreeSize = {}
                    for n in nodes:
                        totalDegreeSize[n] = inDegreeSize[n] + outDegreeSize[n]
                    totalDegree = totalDegreeSize.copy()
                else:
                    inDegreeSize = dict(G.degree)
                    inDegree = inDegreeSize.copy()
                    outDegreeSize = inDegreeSize.copy()
                    outDegree = inDegreeSize.copy()
                    totalDegreeSize = inDegreeSize.copy()
                    totalDegree = inDegreeSize.copy()

                # get weight information
                if is_directed(G):
                    inWeightSize = dict(G.in_degree(weight='weight'))
                    inWeight = inWeightSize.copy()
                    outWeightSize = dict(G.out_degree(weight='weight'))
                    outWeight = outWeightSize.copy()
                    totalWeightSize = {}
                    for n in nodes:
                        totalWeightSize[n] = inWeightSize[n] + outWeightSize[n]
                    totalWeight = totalWeightSize.copy()
                else:
                    inWeightSize = dict(G.degree(weight='weight'))
                    inWeight = inWeightSize.copy()
                    outWeightSize = inWeightSize.copy()
                    outWeight = inWeightSize.copy()
                    totalWeightSize = inWeightSize.copy()
                    totalWeight = inWeightSize.copy()

                # Creating a scale to ensure that the node sizes don't go bananas
                minNodeSize = 0.1  # minNodeSize * maxNodeSize = minimum node size
                maxIn = -maxsize - 1
                minIn = maxsize
                maxOut = -maxsize - 1
                minOut = maxsize
                maxTot = -maxsize - 1
                minTot = maxsize
                maxInw = -maxsize - 1
                minInw = maxsize
                maxOutw = -maxsize - 1
                minOutw = maxsize
                maxTotw = -maxsize - 1
                minTotw = maxsize
                for n in nodes:
                    ind = inDegreeSize[n]
                    outd = outDegreeSize[n]
                    totd = totalDegreeSize[n]
                    inw = inWeightSize[n]
                    outw = outWeightSize[n]
                    totw = totalWeightSize[n]
                    if ind > maxIn:
                        maxIn = ind
                    elif ind < minIn:
                        minIn = ind
                    if outd > maxOut:
                        maxOut = outd
                    elif outd < minOut:
                        minOut = outd
                    if totd > maxTot:
                        maxTot = totd
                    elif totd < minTot:
                        minTot = totd
                    if inw > maxInw:
                        maxInw = inw
                    elif inw < minInw:
                        minInw = inw
                    if outw > maxOutw:
                        maxOutw = outw
                    elif outw < minOutw:
                        minOutw = outw
                    if totw > maxTotw:
                        maxTotw = totw
                    elif totw < minTotw:
                        minTotw = totw

                if maxIn == minIn:
                    sameInDegree = True
                else:
                    sameInDegree = False
                    for n in nodes:
                        result = (inDegreeSize[n] - minIn) / maxIn
                        if result < minNodeSize:
                            inDegreeSize[n] = minNodeSize
                        else:
                            inDegreeSize[n] = result
                if maxOut == minOut:
                    sameOutDegree = True
                else:
                    sameOutDegree = False
                    for n in nodes:
                        result = (outDegreeSize[n] - minOut) / maxOut
                        if result < minNodeSize:
                            outDegreeSize[n] = minNodeSize
                        else:
                            outDegreeSize[n] = result
                if maxTot == minTot:
                    sameTotalDegree = True
                else:
                    sameTotalDegree = False
                    for n in nodes:
                        result = (totalDegreeSize[n] - minTot) / maxTot
                        if result < minNodeSize:
                            totalDegreeSize[n] = minNodeSize
                        else:
                            totalDegreeSize[n] = result
                if maxInw == minInw:
                    sameInWeight = True
                else:
                    sameInWeight = False
                    for n in nodes:
                        result = (inWeightSize[n] - minInw) / maxInw
                        if result < minNodeSize:
                            inWeightSize[n] = minNodeSize
                        else:
                            inWeightSize[n] = result
                if maxOutw == minOutw:
                    sameOutWeight = True
                else:
                    sameOutWeight = False
                    for n in nodes:
                        result = (outWeightSize[n] - minOutw) / maxOutw
                        if result < minNodeSize:
                            outWeightSize[n] = minNodeSize
                        else:
                            outWeightSize[n] = result
                if maxTotw == minTotw:
                    sameTotalWeight = True
                else:
                    sameTotalWeight = False
                    for n in nodes:
                        result = (totalWeightSize[n] - minTotw) / maxTotw
                        if result < minNodeSize:
                            totalWeightSize[n] = minNodeSize
                        else:
                            totalWeightSize[n] = result

                # Making a dictionary for all attributes, and ensuring none of the values go crazy.
                attributes = {}
                maxNodeSize = 30
                for n in nodes:
                    outd = outDegreeSize[n]
                    totd = totalDegreeSize[n]
                    inw = inWeightSize[n]
                    outw = outWeightSize[n]
                    totw = totalWeightSize[n]

                    if sameInDegree:
                        ind = 1
                    else:
                        ind = inDegreeSize[n]
                    if sameOutDegree:
                        outd = 1
                    else:
                        outd = outDegreeSize[n]
                    if sameTotalDegree:
                        totd = 1
                    else:
                        totd = totalDegreeSize[n]
                    if sameInWeight:
                        inw = 1
                    else:
                        inw = inWeightSize[n]
                    if sameOutWeight:
                        outw = 1
                    else:
                        outw = outWeightSize[n]
                    if sameTotalWeight:
                        totw = 1
                    else:
                        totw = totalWeightSize[n]

                    attributes[n] = {
                        'indegreesize': ind * maxNodeSize,
                        'outdegreesize': outd * maxNodeSize,
                        'totaldegreesize': totd * maxNodeSize,
                        'inweightsize': inw * maxNodeSize,
                        'outweightsize': outw * maxNodeSize,
                        'totalweightsize': totw * maxNodeSize,
                        'indegree': inDegree[n],
                        'outdegree': outDegree[n],
                        'totaldegree': totalDegree[n],
                        'inweight': inWeight[n],
                        'outweight': outWeight[n],
                        'totalweight': totalWeight[n],
                        'count': 0
                    }

                set_node_attributes(G, attributes)
                plot = HVGraph.from_networkx(G, layout).opts(
                    directed=get_directed(self.sid), arrowhead_length=0.01)

                # disabling displaying all node info on hovering over the node
                tooltips = [('Index', '@index'), ('In-Degree', '@indegree'),
                            ('Out-Degree', '@outdegree'),
                            ('Total Degree', '@totaldegree'),
                            ('In Edge Weight', '@inweight'),
                            ('Out Edge-Weight', '@outweight'),
                            ('Total Edge-Weight', '@totalweight')]
                hover = HoverTool(tooltips=tooltips)
            else:
                attributes = {}
                for n in nodes:
                    attributes[n] = {
                        'indegreesize': 1,
                        'outdegreesize': 1,
                        'totaldegreesize': 1,
                        'inweightsize': 1,
                        'outweightsize': 1,
                        'totalweightsize': 1,
                        'indegree': 0,
                        'outdegree': 0,
                        'totaldegree': 0,
                        'inweight': 0,
                        'outweight': 0,
                        'totalweight': 0,
                        'count': 0
                    }

                set_node_attributes(G, attributes)
                plot = HVGraph.from_networkx(G, layout).opts(
                    directed=get_directed(self.sid), arrowhead_length=0.01)
                tooltips = [('Index', '@index'), ('In-Degree', '@indegree'),
                            ('Out-Degree', '@outdegree'),
                            ('Total Degree', '@totaldegree'),
                            ('In Edge Weight', '@inweight'),
                            ('Out Edge-Weight', '@outweight'),
                            ('Total Edge-Weight', '@totalweight')]
                hover = HoverTool(tooltips=tooltips)

            # Make custom dictionary with color palettes
            for c in self.colorList:
                if c == 'cividis':
                    self.colorMap[c] = Cividis256
                elif c == 'viridis':
                    self.colorMap[c] = Viridis256
                elif c == 'inferno':
                    self.colorMap[c] = Inferno256
                else:
                    self.colorMap[c] = palette[c]

            if max(nodeCentralities) > 0:
                if datashaded and self.nodeCount > 1:
                    plot = bundle_graph(plot)
            points = plot.nodes
            points.opts(cmap=self.colorMap[self.color_palette],
                        color=self.node_color,
                        size=self.node_size,
                        tools=['box_select', 'lasso_select', 'tap', hover],
                        active_tools=['wheel_zoom'],
                        toolbar='above',
                        show_legend=False,
                        width=self.size,
                        height=self.size)

            plot.opts(node_size=0,
                      node_color=None,
                      node_line_width=0,
                      node_hover_fill_color='green')
            return plot, points
Пример #17
0
 def test_from_networkx_only_nodes(self):
     import networkx as nx
     G = nx.Graph()
     G.add_nodes_from([1, 2, 3])
     graph = Graph.from_networkx(G, nx.circular_layout)
     self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
Пример #18
0
 def test_from_networkx_with_invalid_edge_attrs(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1,2,[]), (1,3,[]), (2,4,[]), (3,4,[])])
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.vdims, [])
Пример #19
0
 def test_from_networkx_with_edge_attrs(self):
     import networkx as nx
     FG = nx.Graph()
     FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.375)])
     graph = Graph.from_networkx(FG, nx.circular_layout)
     self.assertEqual(graph.dimension_values('weight'), np.array([0.125, 0.75, 1.2, 0.375]))