示例#1
0
    def testQ(self):
        self.logger.info("BEGIN")

        # test modularity calculation
        ret = self.createSimpleGraph()
        clusterList = ret[1]
        self.logger.info("clusterList:\n{}".format(clusterList))
        level0 = Louvain("testQ", clusterList)
        self.assertEqual(5, level0._getM())

        self.logger.info("level0._Q:{}".format(level0._Q))
        self.assertAlmostEqual(level0._Q, 0.44)
        self.logger.info("END\n")
示例#2
0
    def testNode(self):
        self.logger.info("BEGIN")

        n0 = Node(clusterId="c0", nodeId=0)
        n1 = Node(clusterId="c0", nodeId=1)
        n2 = Node(clusterId="c0", nodeId=2)

        # undirected  triangle graph
        e0 = Edge(weight=1.0, srcId=0, targetId=1)
        n0._addEdge(e0)

        e1 = Edge(weight=1.0, srcId=1, targetId=0)
        n1._addEdge(e1)

        self.assertEqual(1, n0.getSumAdjWeights())
        self.assertEqual(1, n1.getSumAdjWeights())

        e2 = Edge(weight=1.0, srcId=0, targetId=2)
        n0._addEdge(e2)

        e3 = Edge(weight=1.0, srcId=2, targetId=0)
        n2._addEdge(e3)

        # test print functions
        self.logger.info("e3:{}".format(e3))

        self.assertEqual(2, n0.getSumAdjWeights())
        self.assertEqual(1, n2.getSumAdjWeights())

        # test print functions
        self.logger.info("n2:{}".format(n2))

        e4 = Edge(weight=1.0, srcId=1, targetId=2)
        n1._addEdge(e4)

        e5 = Edge(weight=1.0, srcId=2, targetId=1)
        n2._addEdge(e5)

        self.assertEqual(2, n1.getSumAdjWeights())
        self.assertEqual(2, n2.getSumAdjWeights())

        # create  cluster0
        cluster0 = Cluster(clusterId="c0", nodeList=[n0, n1, n2])
        self.assertEqual(3, cluster0._getM())

        # test print functions
        self.logger.info("cluster0:{}".format(cluster0))

        # create disjoint graph
        n3 = Node(clusterId="c1", nodeId=3)
        e6 = Edge(weight=1.0, srcId=3, targetId=4)
        n3._addEdge(e6)

        n4 = Node(clusterId="c1", nodeId=4)
        e6 = Edge(weight=1.0, srcId=4, targetId=3)
        n4._addEdge(e6)

        cluster1 = Cluster(clusterId="c1", nodeList=[n3, n4])
        self.assertEqual(1, cluster1._getM())

        # test modularity calculation
        level0 = Louvain("testNode", [cluster0, cluster1])
        self.assertEqual(4, level0._getM())

        self.logger.info("level0._Q:{}".format(level0._Q))
        self.assertEqual(level0._Q, 0.59375)

        # test

        self.logger.info("END\n")