def test_directed(self):
        """
        Check that the ' directed ' class is added appropriately
        """
        G = nx.Graph()
        G.add_node("separate node 1")
        G.add_node("separate node 2")
        G.add_edge("separate node 1", "separate node 2")
        G.add_edge("separate node 2", "separate node 1")
        graph = Graph()
        graph.add_graph_from_networkx(G, directed=True)

        expected_nodes = [
            Node(classes="", data={"id": "separate node 1"}, position={}),
            Node(classes="", data={"id": "separate node 2"}, position={}),
        ]
        expected_edges = [
            Edge(
                data={
                    "source": "separate node 1",
                    "target": "separate node 2"
                },
                classes=" directed ",
            ),
            Edge(
                data={
                    "source": "separate node 2",
                    "target": "separate node 1"
                },
                classes=" directed ",
            ),
        ]

        compare_nodes(expected_nodes, graph.nodes)
        compare_edges(expected_edges, graph.edges)
Beispiel #2
0
    def test_classes(self):
        """
        Test to ensure that approriate classes are added to the networkx object
        """
        G = nx.Graph()
        G.add_node('separate node 1', classes='class1')
        G.add_node('separate node 2', classes='class2')
        G.add_edge('separate node 1', 'separate node 2')
        graph = Graph()
        graph.add_graph_from_networkx(G)

        expected_nodes = [
            Node(classes='class1', data={'id': 'separate node 1'},
                 position={}),
            Node(classes='class2', data={'id': 'separate node 2'}, position={})
        ]
        expected_edges = [
            Edge(data={
                'source': 'separate node 1',
                'target': 'separate node 2'
            },
                 position={})
        ]

        compare_nodes(expected_nodes, graph.nodes)
        compare_edges(expected_edges, graph.edges)
Beispiel #3
0
    def test_custom_node(self):
        class custom_node:
            def __init__(self, name):
                self.name = name

            def __str__(self):
                return "Node: " + str(self.name)

        n1 = custom_node("node 1")
        n2 = custom_node("node 2")

        G = nx.Graph()
        G.add_node(n1)
        G.add_node(n2)
        G.add_edge(n1, n2)
        graph = Graph()
        graph.add_graph_from_networkx(G)

        expected_nodes = [
            Node(classes='', data={'id': 'Node: node 1'}, position={}),
            Node(classes='', data={'id': 'Node: node 2'}, position={})
        ]
        expected_edges = [
            Edge(data={
                'source': 'Node: node 1',
                'target': 'Node: node 2'
            },
                 classes='',
                 position={})
        ]

        compare_edges(expected_nodes, graph.nodes)
        compare_edges(expected_edges, graph.edges)
Beispiel #4
0
    def test_directed(self):
        """
        Check that the ' directed ' class is added appropriately
        """
        G = nx.Graph()
        G.add_node('separate node 1')
        G.add_node('separate node 2')
        G.add_edge('separate node 1', 'separate node 2')
        G.add_edge('separate node 2', 'separate node 1')
        graph = Graph()
        graph.add_graph_from_networkx(G, directed=True)

        expected_nodes = [
            Node(classes='', data={'id': 'separate node 1'}, position={}),
            Node(classes='', data={'id': 'separate node 2'}, position={})
        ]
        expected_edges = [
            Edge(data={
                'source': 'separate node 1',
                'target': 'separate node 2'
            },
                 classes=' directed ',
                 position={}),
            Edge(data={
                'source': 'separate node 2',
                'target': 'separate node 1'
            },
                 classes=' directed ',
                 position={})
        ]

        compare_nodes(expected_nodes, graph.nodes)
        compare_edges(expected_edges, graph.edges)
    def test_remove_node(self):
        """
        Test to ensure that nodes will be removed correctly
        """
        data = {
            "nodes": [
                {
                    "data": {
                        "id": "0"
                    }
                },
                {
                    "data": {
                        "id": "1"
                    }
                },
                {
                    "data": {
                        "id": "2"
                    }
                },
            ],
            "edges": [
                {
                    "data": {
                        "source": "0",
                        "target": "1"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "2"
                    }
                },
                {
                    "data": {
                        "source": "2",
                        "target": "0"
                    }
                },
            ],
        }
        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
        ]
        expected_edges = [
            Edge(classes="", data={
                "source": "1",
                "target": "2"
            }),
        ]

        graph = Graph()
        graph.add_graph_from_json(data)
        graph.remove_node(graph.nodes[0])
        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
    def test_add_node(self):
        """
        Test to ensure that a single node will be added to the graph
        """
        # will call add_edges, so no extensive test are necessary
        node = Node(data={"id": "0"})
        expected_nodes = [
            Node(data={"id": "0"}, position={}),
        ]
        expected_edges = []

        graph = Graph()
        graph.add_node(node)
        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
    def test_add_nodes(self):
        """
        Test to ensure that nodes will be added to the graph
        """
        # create some nodes
        ids = ["0", "1"]
        nodes = [Node(data={"id": i}) for i in ids]

        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
        ]
        expected_edges = []

        graph = Graph()
        graph.add_nodes(nodes)
        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
    def test_add_edges(self):
        """
        Test to ensure that an edge with the corresponding nodes will be added to the graph
        """
        edge = Edge(data={"source": "0", "target": "1"})
        edge_inv = Edge(data={"source": "1", "target": "0"})

        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
        ]
        expected_edges = [
            Edge(classes="", data={
                "source": "0",
                "target": "1"
            }),
        ]
        expected_edges_multiple = [
            Edge(classes="", data={
                "source": "0",
                "target": "1"
            }),
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "1",
                     "target": "0"
                 }),
        ]

        graph = Graph()
        graph.add_edge(copy.copy(edge))
        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        # test for passing arguments
        graph.add_edge(copy.copy(edge_inv), multiple_edges=True)
        compare_edges(expected_edges_multiple, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
    def test_classes(self):
        """
        Test to ensure that approriate classes are added to the networkx object
        """
        G = nx.Graph()
        G.add_node("separate node 1", classes="class1")
        G.add_node("separate node 2", classes="class2")
        G.add_edge("separate node 1", "separate node 2")
        graph = Graph()
        graph.add_graph_from_networkx(G)

        expected_nodes = [
            Node(classes="class1", data={"id": "separate node 1"}),
            Node(classes="class2", data={"id": "separate node 2"}),
        ]
        expected_edges = [
            Edge(data={
                "source": "separate node 1",
                "target": "separate node 2"
            })
        ]

        compare_nodes(expected_nodes, graph.nodes)
        compare_edges(expected_edges, graph.edges)
Beispiel #10
0
 def test_lonely_nodes(self):
     """
     Test to ensure that nodes with no associated edges end up in the graph
     """
     G1 = nx.complete_graph(5)
     G2 = nx.Graph()
     G2.add_node('unconnected_node')
     G2 = nx.complete_graph(1)
     graph = Graph()
     graph.add_graph_from_networkx(G1)
     graph.add_graph_from_networkx(G2)
     expected_nodes = [
         Node(data={'id': '0'}, position={}),
         Node(data={'id': '1'}, position={}),
         Node(data={'id': '2'}, position={}),
         Node(data={'id': '3'}, position={}),
         Node(data={'id': '4'}, position={}),
         Node(data={'id': 'unconnected_node'}, position={})
     ]
     expected_edges = [
         Edge(data={
             'source': '0',
             'target': '1'
         }, position={}),
         Edge(data={
             'source': '0',
             'target': '2'
         }, position={}),
         Edge(data={
             'source': '0',
             'target': '3'
         }, position={}),
         Edge(data={
             'source': '0',
             'target': '4'
         }, position={}),
         Edge(data={
             'source': '1',
             'target': '2'
         }, position={}),
         Edge(data={
             'source': '1',
             'target': '3'
         }, position={}),
         Edge(data={
             'source': '1',
             'target': '4'
         }, position={}),
         Edge(data={
             'source': '2',
             'target': '3'
         }, position={}),
         Edge(data={
             'source': '2',
             'target': '4'
         }, position={}),
         Edge(data={
             'source': '3',
             'target': '4'
         }, position={})
     ]
     compare_nodes(expected_nodes, graph.nodes)
     compare_edges(expected_edges, graph.edges)
Beispiel #11
0
    def test_multidigraphs(self):
        '''
        Test to ensure multidigraphs are generated correctly.
        '''
        G = nx.MultiDiGraph()
        G.add_edge(1, 2, weight=15)
        G.add_edge(1, 2, weight=15)
        G.add_edge(1, 2, weight=1)
        G.add_edge(2, 1, weight=10)
        G.add_edge(2, 4)
        graph = Graph()
        graph.add_graph_from_networkx(G)

        expected_edges = [
            Edge(classes=' directed  multiple_edges ',
                 data={
                     'source': '1',
                     'target': '2',
                     'weight': 15
                 },
                 position={}),
            Edge(classes=' directed  multiple_edges ',
                 data={
                     'source': '1',
                     'target': '2',
                     'weight': 15
                 },
                 position={}),
            Edge(classes=' directed  multiple_edges ',
                 data={
                     'source': '1',
                     'target': '2',
                     'weight': 1
                 },
                 position={}),
            Edge(classes=' directed  multiple_edges ',
                 data={
                     'source': '2',
                     'target': '1',
                     'weight': 10
                 },
                 position={}),
            Edge(classes=' directed  multiple_edges ',
                 data={
                     'source': '2',
                     'target': '4'
                 },
                 position={})
        ]

        expected_nodes = [
            Node(data={'id': '1'}, position={}),
            Node(data={'id': '2'}, position={}),
            Node(data={'id': '4'}, position={})
        ]

        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        # override default behavior
        graph.add_graph_from_networkx(G, multiple_edges=False, directed=False)
        expected_edges = [
            Edge(data={
                'source': '1',
                'target': '2',
                'weight': 15
            },
                 position={}),
            Edge(data={
                'source': '2',
                'target': '4'
            }, position={})
        ]

        compare_edges(expected_edges, graph.edges)
 def test_lonely_nodes(self):
     """
     Test to ensure that nodes with no associated edges end up in the graph
     """
     G1 = nx.complete_graph(5)
     G2 = nx.Graph()
     G2.add_node("unconnected_node")
     G2 = nx.complete_graph(1)
     graph = Graph()
     graph.add_graph_from_networkx(G1)
     graph.add_graph_from_networkx(G2)
     expected_nodes = [
         Node(data={"id": "0"}, position={}),
         Node(data={"id": "1"}, position={}),
         Node(data={"id": "2"}, position={}),
         Node(data={"id": "3"}, position={}),
         Node(data={"id": "4"}, position={}),
         Node(data={"id": "unconnected_node"}, position={}),
     ]
     expected_edges = [
         Edge(data={
             "source": "0",
             "target": "1"
         }),
         Edge(data={
             "source": "0",
             "target": "2"
         }),
         Edge(data={
             "source": "0",
             "target": "3"
         }),
         Edge(data={
             "source": "0",
             "target": "4"
         }),
         Edge(data={
             "source": "1",
             "target": "2"
         }),
         Edge(data={
             "source": "1",
             "target": "3"
         }),
         Edge(data={
             "source": "1",
             "target": "4"
         }),
         Edge(data={
             "source": "2",
             "target": "3"
         }),
         Edge(data={
             "source": "2",
             "target": "4"
         }),
         Edge(data={
             "source": "3",
             "target": "4"
         }),
     ]
     compare_nodes(expected_nodes, graph.nodes)
     compare_edges(expected_edges, graph.edges)
    def test_multidigraphs(self):
        """
        Test to ensure multidigraphs are generated correctly.
        """
        G = nx.MultiDiGraph()
        G.add_edge(1, 2, weight=15)
        G.add_edge(1, 2, weight=15)
        G.add_edge(1, 2, weight=1)
        G.add_edge(2, 1, weight=10)
        G.add_edge(2, 4)
        graph = Graph()
        graph.add_graph_from_networkx(G)

        expected_edges = [
            Edge(
                classes=" directed  multiple_edges ",
                data={
                    "source": "1",
                    "target": "2",
                    "weight": 15
                },
            ),
            Edge(
                classes=" directed  multiple_edges ",
                data={
                    "source": "1",
                    "target": "2",
                    "weight": 15
                },
            ),
            Edge(
                classes=" directed  multiple_edges ",
                data={
                    "source": "1",
                    "target": "2",
                    "weight": 1
                },
            ),
            Edge(
                classes=" directed  multiple_edges ",
                data={
                    "source": "2",
                    "target": "1",
                    "weight": 10
                },
            ),
            Edge(
                classes=" directed  multiple_edges ",
                data={
                    "source": "2",
                    "target": "4"
                },
            ),
        ]

        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "4"}, position={}),
        ]

        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        # override default behavior
        graph.add_graph_from_networkx(G, multiple_edges=False, directed=False)
        expected_edges = [
            Edge(data={
                "source": "1",
                "target": "2",
                "weight": 15
            }),
            Edge(data={
                "source": "2",
                "target": "4"
            }),
        ]

        compare_edges(expected_edges, graph.edges)
    def test_remove_edge(self):
        """
        Test to ensure that edges will be removed
        """
        # only a small test because everything else is covered in remove_edge_by_id()
        data = {
            "nodes": [
                {
                    "data": {
                        "id": "0"
                    }
                },
                {
                    "data": {
                        "id": "1"
                    }
                },
                {
                    "data": {
                        "id": "2"
                    }
                },
            ],
            "edges": [
                {
                    "data": {
                        "source": "0",
                        "target": "1"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "2"
                    }
                },
                {
                    "data": {
                        "source": "2",
                        "target": "0"
                    }
                },
            ],
        }
        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
        ]
        expected_edges = [
            Edge(classes="", data={
                "source": "1",
                "target": "2"
            }),
            Edge(classes="", data={
                "source": "2",
                "target": "0"
            }),
        ]

        graph = Graph()
        graph.add_graph_from_json(data)
        graph.remove_edge(graph.edges[0])
        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
Beispiel #15
0
    def test_lonely_nodes(self):
        """
        Test to ensure that nodes with no associated edges end up in the graph
        """
        G1 = nx.complete_graph(5)
        G2 = nx.Graph()
        G2.add_node("unconnected_node")
        G2 = nx.complete_graph(1)
        graph = Graph()
        graph.add_graph_from_networkx(G1)
        graph.add_graph_from_networkx(G2)

        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "3"}, position={}),
            Node(data={"id": "4"}, position={}),
            Node(data={"id": "unconnected_node"}, position={}),
        ]

        # remove individual node using node as input
        graph.remove_node(graph.nodes[0])

        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "3"}, position={}),
            Node(data={"id": "4"}, position={}),
            Node(data={"id": "unconnected_node"}, position={}),
        ]

        # remove individual node using index node as input
        graph.remove_node_by_id("3")

        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "4"}, position={}),
            Node(data={"id": "unconnected_node"}, position={}),
        ]

        # remove all nodes of the graph
        graph.clear()
        expected_nodes = []
    def test_remove_edge_by_id(self):
        """
        Test to ensure that edges will be removed given the ids of the nodes
        for different graphs
        """
        data = {
            "nodes": [
                {
                    "data": {
                        "id": "0"
                    }
                },
                {
                    "data": {
                        "id": "1"
                    }
                },
                {
                    "data": {
                        "id": "2"
                    }
                },
            ],
            "edges": [
                {
                    "data": {
                        "source": "0",
                        "target": "1",
                        "weight": "1"
                    }
                },
                {
                    "data": {
                        "source": "0",
                        "target": "1",
                        "weight": "2"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "0"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "2"
                    }
                },
                {
                    "data": {
                        "source": "2",
                        "target": "0"
                    }
                },
            ],
        }
        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
        ]
        expected_edges_undirected = [
            Edge(classes="", data={
                "source": "1",
                "target": "2"
            }),
            Edge(classes="", data={
                "source": "2",
                "target": "0"
            }),
        ]
        expected_edges_directed = [
            Edge(classes=" directed ", data={
                "source": "1",
                "target": "0"
            }),
            Edge(classes=" directed ", data={
                "source": "1",
                "target": "2"
            }),
            Edge(classes=" directed ", data={
                "source": "2",
                "target": "0"
            }),
        ]
        expected_edges_multiple = [
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "1",
                     "target": "2"
                 }),
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "2",
                     "target": "0"
                 }),
        ]

        graph = Graph()
        graph.add_graph_from_json(data)
        graph.remove_edge_by_id("0", "1")
        compare_edges(expected_edges_undirected, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        graph.add_graph_from_json(data, directed=True)
        graph.remove_edge_by_id("0", "1")
        compare_edges(expected_edges_directed, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
        graph.remove_edge_by_id("1", "0")
        compare_edges(expected_edges_directed[1:], graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        graph.add_graph_from_json(data, multiple_edges=True)
        graph.remove_edge_by_id("0", "1")
        compare_edges(expected_edges_multiple, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
    def test_add_edges(self):
        """
        Test to ensure that edges with the corresponding nodes will be added to the graph
        """
        ids = ["0", "1", "2"]
        edges = [
            Edge(data={
                "source": source,
                "target": target
            }) for source, target in zip(ids[:-1], ids[1:])
        ]
        edges_weighted = [
            Edge(data={
                "source": "0",
                "target": "1",
                "weight": str(i)
            }) for i in range(1, 3)
        ]
        edge_inv = Edge(data={"source": "1", "target": "0"})

        edges += edges_weighted
        edges += [edge_inv]

        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
        ]
        expected_edges_undirected = [
            Edge(classes="", data={
                "source": "0",
                "target": "1"
            }),
            Edge(classes="", data={
                "source": "1",
                "target": "2"
            }),
        ]
        expected_edges_directed = [
            Edge(classes=" directed ", data={
                "source": "0",
                "target": "1"
            }),
            Edge(classes=" directed ", data={
                "source": "1",
                "target": "2"
            }),
            Edge(classes=" directed ", data={
                "source": "1",
                "target": "0"
            }),
        ]
        expected_edges_multiple = [
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "0",
                     "target": "1"
                 }),
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "1",
                     "target": "2"
                 }),
            Edge(
                classes=" multiple_edges ",
                data={
                    "source": "0",
                    "target": "1",
                    "weight": "1"
                },
            ),
            Edge(
                classes=" multiple_edges ",
                data={
                    "source": "0",
                    "target": "1",
                    "weight": "2"
                },
            ),
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "1",
                     "target": "0"
                 }),
        ]

        graph = Graph()
        graph.add_edges([copy.copy(edge) for edge in edges])
        compare_edges(expected_edges_undirected, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        graph.add_edges([copy.copy(edge) for edge in edges], directed=True)
        compare_edges(expected_edges_directed, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        graph.add_edges([copy.copy(edge) for edge in edges],
                        multiple_edges=True)
        compare_edges(expected_edges_multiple, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)