Esempio n. 1
0
    def test_one_community(self):
        # Create a graph that is essentially one community
        graph = Graph()
        graph.add_edge(1, 2)
        graph.add_edge(1, 3)
        graph.add_edge(1, 4)
        graph.add_edge(2, 3)
        graph.add_edge(2, 4)
        graph.add_edge(3, 4)

        dictionary = label_propagation(graph)
        for node, label in dictionary.iteritems():
            self.assertEqual(label, 0)
Esempio n. 2
0
    def test_one_community(self):
        # Create a graph that is essentially one community
        graph = Graph()
        graph.add_edge(1,2)
        graph.add_edge(1,3)
        graph.add_edge(1,4)
        graph.add_edge(2,3)
        graph.add_edge(2,4)
        graph.add_edge(3,4)

        dictionary = label_propagation(graph)
        for node, label in dictionary.iteritems():
            self.assertEqual(label, 0)
Esempio n. 3
0
    def test_two_communities(self):
        # Create a graph that should give two separate communities
        graph = Graph()
        # The first community
        graph.add_edge(1,2)
        graph.add_edge(1,3)
        graph.add_edge(2,3)
        graph.add_edge(2,5)
        graph.add_edge(2,4)
        graph.add_edge(3,5)
        graph.add_edge(4,5)
        
        # The other community
        graph.add_edge(6,7)
        graph.add_edge(7,8)
        graph.add_edge(6,8)
        graph.add_edge(7,9)
        graph.add_edge(6,9)
        graph.add_edge(8,9)

        # Link between the communities
        graph.add_edge(5,6)

        # should give us 0 and 1
        # First community = 0, second community = 1
        dictionary = label_propagation(graph)
        for node, label in dictionary.iteritems():
            if node >= 1 and node <= 5:
                self.assertEqual(label, 0)
            else:
                self.assertEqual(label, 1)
Esempio n. 4
0
    def test_two_communities(self):
        # Create a graph that should give two separate communities
        graph = Graph()
        # The first community
        graph.add_edge(1, 2)
        graph.add_edge(1, 3)
        graph.add_edge(2, 3)
        graph.add_edge(2, 5)
        graph.add_edge(2, 4)
        graph.add_edge(3, 5)
        graph.add_edge(4, 5)

        # The other community
        graph.add_edge(6, 7)
        graph.add_edge(7, 8)
        graph.add_edge(6, 8)
        graph.add_edge(7, 9)
        graph.add_edge(6, 9)
        graph.add_edge(8, 9)

        # Link between the communities
        graph.add_edge(5, 6)

        # should give us 0 and 1
        # First community = 0, second community = 1
        dictionary = label_propagation(graph)
        for node, label in dictionary.iteritems():
            if node >= 1 and node <= 5:
                self.assertEqual(label, 0)
            else:
                self.assertEqual(label, 1)
Esempio n. 5
0
    def test_small_graph_modularity(self):
		graph=Graph()
		graph.add_node('1')
		graph.add_node('2')
		graph.add_node('3')
		graph.add_node('4')
		graph.add_node('5')
		graph.add_node('6')
		graph.add_edge('1','2')
		graph.add_edge('3','2')
		graph.add_edge('1','3')
		graph.add_edge('4','5')
		graph.add_edge('4','6')
		graph.add_edge('5','6')
		graph.add_edge('2','5')
		community_assignment={0:['1','2','3'],1:['4','5','6']}
		expected_result=10.0/28 # hand calculated
		#print 'modularity: ' + str(modularity.modularity(graph, community_assignment))
		self.assertAlmostEqual(modularity.modularity(graph, community_assignment), expected_result)
		graph.add_node('7')
		graph.add_node('8')
		graph.add_node('9')
		graph.add_edge('7','8')
		graph.add_edge('8','9')
		graph.add_edge('9','7')
		graph.add_edge('8','5')
		graph.add_edge('8','2')
		expected_result=5.0/12 # hand calculated
		
		community_assignment={0:['1','2','3'],1:['4','5','6'],2:['7','8','9']}
		#print 'modularity: ' + str(modularity.modularity(graph, community_assignment))
		self.assertAlmostEqual(modularity.modularity(graph, community_assignment), expected_result)