Example #1
0
 def test_clustering_integer_nodes(self):
     # This tests an error that would happen when a node had an integer value
     # greater than the size of the graph
     orig_club = nx.karate_club_graph()
     club = nx.relabel_nodes(orig_club, { n: n + 5 for n in orig_club.nodes() })
     dendrogram = GreedyAgglomerativeClusterer().cluster(club)
     dendrogram.clusters() # This would crash
Example #2
0
 def test_clustering_integer_nodes(self):
     # This tests an error that would happen when a node had an integer value
     # greater than the size of the graph
     orig_club = nx.karate_club_graph()
     club = nx.relabel_nodes(orig_club,
                             {n: n + 5
                              for n in orig_club.nodes()})
     dendrogram = GreedyAgglomerativeClusterer().cluster(club)
     dendrogram.clusters()  # This would crash
Example #3
0
    def test_small_graph(self):
        graph = nx.Graph();
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)
        self.assertEqual(list(dendrogram.clusters()), [])

        graph.add_node(1)
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)
        self.assertEqual(list(dendrogram.clusters(1)[0]), [1])
        self.assertEqual(list(dendrogram.clusters()[0]), [1])
Example #4
0
    def test_clusterer_pickling(self):
        clusterer = GreedyAgglomerativeClusterer()
        p_clusterer = pickle.loads(pickle.dumps(clusterer))
        self.assertEqual(clusterer.optimal_clusters,
                         p_clusterer.optimal_clusters)

        clusterer = GreedyAgglomerativeClusterer(100)
        p_clusterer = pickle.loads(pickle.dumps(clusterer))
        self.assertEqual(clusterer.optimal_clusters,
                         p_clusterer.optimal_clusters)
Example #5
0
    def test_non_integer_clustering(self):
        graph = nx.Graph()
        graph.add_nodes_from(map(lambda i: chr(i), range(10)))
        graph.add_edges_from([['a', 'b'], ['b', 'c'], ['a', 'c']])
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)

        clusters = dendrogram.clusters()
        self.assertIn(set(['a', 'b', 'c']), clusters)
        for node_index in range(3, 10):
            self.assertIn(set([chr(node_index)]), clusters)
Example #6
0
    def test_orphans(self):
        graph = nx.Graph()
        graph.add_nodes_from(range(10))
        graph.add_edges_from([[0, 1], [1, 2], [0, 2]])
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)

        clusters = dendrogram.clusters()
        self.assertIn(set([0, 1, 2]), clusters)
        for node in range(3, 10):
            self.assertIn(set([node]), clusters)
Example #7
0
    def test_non_integer_clustering(self):
        graph = nx.Graph();
        graph.add_nodes_from(map(lambda i: chr(i), range(10)))
        graph.add_edges_from([['a', 'b'], ['b', 'c'], ['a', 'c']])
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)

        clusters = dendrogram.clusters()
        self.assertIn(set(['a', 'b', 'c']), clusters)
        for node_index in range(3, 10):
            self.assertIn(set([chr(node_index)]), clusters)
Example #8
0
    def test_orphans(self):
        graph = nx.Graph();
        graph.add_nodes_from(range(10))
        graph.add_edges_from([[0, 1], [1, 2], [0, 2]])
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)

        clusters = dendrogram.clusters()
        self.assertIn(set([0, 1, 2]), clusters)
        for node in range(3, 10):
            self.assertIn(set([node]), clusters)
Example #9
0
    def test_small_graph(self):
        graph = nx.Graph()
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)
        self.assertEqual(list(dendrogram.clusters()), [])

        graph.add_node(1)
        dendrogram = GreedyAgglomerativeClusterer().cluster(graph)
        self.assertEqual(list(dendrogram.clusters(1)[0]), [1])
        self.assertEqual(list(dendrogram.clusters()[0]), [1])
Example #10
0
 def karate_clustering(self, *args, **kwargs):
     return GreedyAgglomerativeClusterer().cluster(nx.karate_club_graph(),
                                                   *args, **kwargs)