def testFromNetworkXGraph(self):
        try:
            import networkx
        except ImportError as error:
            logging.debug(error)
            return

        nxGraph = networkx.Graph()
        nxGraph.graph["VListType"] = GeneralVertexList
        #nxGraph.graph["numFeatures"] = 2
        #nxGraph.add_node(0)
        nxGraph.add_edge(0, 1)
        nxGraph.add_edge(1, 2)
        nxGraph.add_edge(1, 3)

        graph = self.GraphType.fromNetworkXGraph(nxGraph)

        self.assertTrue(graph.getNumVertices() == 4)
        self.assertTrue(graph.isUndirected() == True)
        self.assertTrue(graph.getNumEdges() == 3)
        self.assertTrue(graph.getEdge(0, 1) == 1)
        self.assertTrue(graph.getEdge(1, 2) == 1)
        self.assertTrue(graph.getEdge(1, 3) == 1)

        #Try directed graphs
        nxGraph = networkx.DiGraph()
        nxGraph.graph["VListType"] = GeneralVertexList
        #nxGraph.add_node(0)
        nxGraph.add_edge(0, 1)
        nxGraph.add_edge(1, 2)
        nxGraph.add_edge(1, 3)

        graph = self.GraphType.fromNetworkXGraph(nxGraph)

        self.assertTrue(graph.getNumVertices() == 4)
        self.assertTrue(graph.isUndirected() == False)
        self.assertTrue(graph.getNumEdges() == 3)
        self.assertTrue(graph.getEdge(0, 1) == 1)
        self.assertTrue(graph.getEdge(1, 2) == 1)
        self.assertTrue(graph.getEdge(1, 3) == 1)

        #Using a multigraph should fail
        nxGraph = networkx.MultiGraph()
        self.assertRaises(ValueError, self.GraphType.fromNetworkXGraph, nxGraph)

        #Test node labels
        nxGraph = networkx.DiGraph()
        nxGraph.graph["VListType"] = GeneralVertexList
        nxGraph.add_node("a", label="abc")
        nxGraph.add_node("b", label="i")
        nxGraph.add_node("c", label="am")
        nxGraph.add_node("d", label="here")
        nxGraph.add_edge("a", "b")
        nxGraph.add_edge("b", "c")
        nxGraph.add_edge("b", "d")

        graph = self.GraphType.fromNetworkXGraph(nxGraph)

        nodeDict = {}
        for i in range(len(nxGraph.nodes())):
            nodeDict[nxGraph.nodes()[i]] = i

        self.assertTrue(graph.getNumVertices() == 4)
        self.assertTrue(graph.isUndirected() == False)
        self.assertTrue(graph.getNumEdges() == 3)
        self.assertTrue(graph.getEdge(nodeDict["a"], nodeDict["b"]) == 1)
        self.assertTrue(graph.getEdge(nodeDict["b"], nodeDict["c"]) == 1)
        self.assertTrue(graph.getEdge(nodeDict["b"], nodeDict["d"]) == 1)

        self.assertTrue(graph.getVertex(0) == "abc")
        self.assertTrue(graph.getVertex(1) == "am")
        self.assertTrue(graph.getVertex(2) == "i")
        self.assertTrue(graph.getVertex(3) == "here")

        #Test in conjunction with toNetworkXGraph
        numVertices = 10
        numFeatures = 2 
        vList = VertexList(numVertices, numFeatures)
        vList.setVertices(numpy.random.rand(numVertices, numFeatures))

        graph = self.GraphType(vList)
        graph.addEdge(0, 1)
        graph.addEdge(0, 5)
        graph.addEdge(2, 5)
        graph.addEdge(3, 4)

        nxGraph = graph.toNetworkXGraph()
        graph2 = self.GraphType.fromNetworkXGraph(nxGraph)

        tol = 10**-6
        self.assertTrue(numpy.linalg.norm(graph.getVertexList().getVertices(list(range(numVertices))) -graph2.getVertexList().getVertices(list(range(numVertices)))) < tol)
        self.assertEquals(graph.getNumEdges(), graph2.getNumEdges())
        
        for i in range(numVertices):
            for j in range(numVertices):
                self.assertEquals(graph.getEdge(i, j), graph2.getEdge(i, j))

        #Use a GeneralVertexList
        numVertices = 10
        vList = GeneralVertexList(numVertices)
        for i in range(numVertices):
            vList.setVertex(i, "s" + str(i))

        graph = self.GraphType(vList)
        graph.addEdge(0, 1)
        graph.addEdge(0, 5)
        graph.addEdge(2, 5)
        graph.addEdge(3, 4)

        nxGraph = graph.toNetworkXGraph()
        graph2 = self.GraphType.fromNetworkXGraph(nxGraph)

        for i in range(numVertices):
            self.assertEquals(graph.getVertex(i), graph2.getVertex(i))
            
        self.assertEquals(graph.getNumEdges(), graph2.getNumEdges())
        
        for i in range(numVertices):
            for j in range(numVertices):
                self.assertEquals(graph.getEdge(i, j), graph2.getEdge(i, j))
dataset/ETHPy150Open charanpald/APGL/apgl/graph/test/MatrixGraphTest.py/MatrixGraphTest.testFromNetworkXGraph/original