Пример #1
0
 def test_add_differing_graphs(self):
     cluster_a = DenGraphCluster(graph=DistanceGraph(
         nodes=[1, 2, 3], distance=None, symmetric=True))
     cluster_b = DenGraphCluster(graph=DistanceGraph(
         nodes=[2, 3, 4], distance=None, symmetric=True))
     with self.assertRaises(GraphError):
         cluster_a + cluster_b
     with self.assertRaises(GraphError):
         cluster_a += cluster_b
Пример #2
0
 def setUp(self):
     self.io_graph = DenGraphIO(base_graph=DistanceGraph(
         nodes=[1, 2, 3, 4], distance=DeltaDistance(), symmetric=True),
                                cluster_distance=1,
                                core_neighbours=5)
     self.one_cluster_graph = DenGraphIO(base_graph=DistanceGraph(
         nodes=[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1),
                (2, 2), (2, 3), (2, 4), (2, 5), (2, 6)],
         distance=ListDistance(),
         symmetric=True),
                                         cluster_distance=.1,
                                         core_neighbours=5)
Пример #3
0
 def test_deletion(self):
     cluster = DenGraphCluster(graph=DistanceGraph(
         nodes=[1, 2, 3, 4], distance=None, symmetric=True))
     cluster.categorize_node(1, cluster.CORE_NODE)
     self.assertTrue(1 in cluster)
     del cluster[1]
     self.assertFalse(1 in cluster)
Пример #4
0
    def __translate_graph(self):
        graph = DistanceGraph(
            nodes=tuple([v for v in self._network.vertices()]),
            distance=lambda node_from, node_to: self.__paper_distance(
                node_from, node_to))

        return graph
Пример #5
0
 def test_set(self):
     cluster = DenGraphCluster(graph=DistanceGraph(
         nodes=[1, 2, 3, 4], distance=None, symmetric=True))
     with self.assertRaises(TypeError):
         cluster[1:2] = 1
     with self.assertRaises(TypeError):
         cluster[1] = {}
Пример #6
0
 def test_zero_clusters(self):
     io_graph = DenGraphIO(base_graph=DistanceGraph(
         nodes=[1, 2, 3, 4], distance=DeltaDistance(), symmetric=True),
                           cluster_distance=1,
                           core_neighbours=5)
     with self.assertRaises(ValueError):
         davies_bouldin_score(io_graph.clusters, io_graph.graph)
Пример #7
0
 def test_two_cluster(self):
     io_graph = DenGraphIO(base_graph=DistanceGraph(
         nodes=[1, 2, 3, 4, 5, 6, 13, 14, 15, 16, 17, 18],
         distance=DeltaDistance(),
         symmetric=True),
                           cluster_distance=5,
                           core_neighbours=5)
     self.assertEqual(
         .25, davies_bouldin_score(io_graph.clusters, io_graph.graph))
Пример #8
0
 def test_two_cluster(self):
     io_graph = DenGraphIO(
         base_graph=DistanceGraph(
             nodes=[(1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,1), (2,2), (2,3), (2,4), (2,5), (2,6)],
             distance=ListDistance(),
             symmetric=True),
         cluster_distance=.1,
         core_neighbours=5
     )
     self.assertEqual(1.0, silhouette_score(io_graph.clusters, io_graph.graph))
Пример #9
0
 def test_one_cluster(self):
     io_graph = DenGraphIO(
         base_graph=DistanceGraph(
             nodes=[1, 2, 3, 4, 5, 6],
             distance=DeltaDistance(),
             symmetric=True),
         cluster_distance=5,
         core_neighbours=5
     )
     self.assertEqual(-1, silhouette_score(io_graph.clusters, io_graph.graph))
Пример #10
0
 def test_two_cluster_with_zero_distance(self):
     io_graph = DenGraphIO(
         base_graph=DistanceGraph(
             nodes=[(1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,1), (2,2), (2,3), (2,4), (2,5), (2,6)],
             distance=ListDistance(),
             symmetric=True),
         cluster_distance=.1,
         core_neighbours=5
     )
     self.assertEqual(float("inf"), calinski_harabasz_score(io_graph.clusters, io_graph.graph))
Пример #11
0
 def test_two_cluster(self):
     io_graph = DenGraphIO(
         base_graph=DistanceGraph(
             nodes=[1, 2, 3, 4, 5, 6, 13, 14, 15, 16, 17, 18],
             distance=DeltaDistance(),
             symmetric=True),
         cluster_distance=5,
         core_neighbours=5
     )
     self.assertAlmostEqual(123.4, calinski_harabasz_score(io_graph.clusters, io_graph.graph), 1)
Пример #12
0
 def test_node_not_in_cluster(self):
     io_graph = DenGraphIO(
         base_graph=DistanceGraph(
             nodes=[(1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,1)],
             distance=ListDistance(),
             symmetric=True),
         cluster_distance=.1,
         core_neighbours=5
     )
     with self.assertRaises(dengraph.graph.NoSuchNode):
         avg_inter_cluster_distance((2,1), io_graph.clusters[0], io_graph.graph)
Пример #13
0
 def test_one_cluster(self):
     io_graph = DenGraphIO(
         base_graph=DistanceGraph(
             nodes=[1, 2, 3, 4, 5, 6],
             distance=DeltaDistance(),
             symmetric=True),
         cluster_distance=5,
         core_neighbours=5
     )
     with self.assertRaises(ValueError):
         calinski_harabasz_score(io_graph.clusters, io_graph.graph)
Пример #14
0
 def test_several_cluster(self):
     node_list = []
     for node in range(10):
         for position in range(10):
             node_list.append((node, position))
     io_graph = DenGraphIO(base_graph=DistanceGraph(nodes=node_list,
                                                    distance=ListDistance(),
                                                    symmetric=True),
                           cluster_distance=.1,
                           core_neighbours=5)
     self.assertEqual(
         0.0, davies_bouldin_score(io_graph.clusters, io_graph.graph))
Пример #15
0
 def clusterer(self):
     if self._clusterer is None:
         distance = ClusterDistance(distance=self.distance)
         self._clusterer = DenGraphIO(
             base_graph=AdjacencyGraph(
                 DistanceGraph(nodes=self._nodes,
                               distance=distance,
                               symmetric=True)),
             cluster_distance=self.cluster_distance,
             core_neighbours=self.core_neighbours)
         self._clusterer.graph.distance = distance
     return self._clusterer
Пример #16
0
 def test_get(self):
     cluster = DenGraphCluster(graph=DistanceGraph(
         nodes=[1, 2, 3, 4], distance=DeltaDistance(), symmetric=True))
     with self.assertRaises(dengraph.graph.NoSuchEdge):
         cluster[1:2]
     cluster.categorize_node(1, cluster.CORE_NODE)
     with self.assertRaises(dengraph.graph.NoSuchEdge):
         cluster[1:2]
     cluster.categorize_node(2, cluster.BORDER_NODE)
     self.assertEqual(1, cluster[1:2])
     with self.assertRaises(dengraph.graph.NoSuchEdge):
         cluster[3:2]
Пример #17
0
 def test_recategorisation(self):
     cluster = DenGraphCluster(graph=DistanceGraph(
         nodes=[1, 2, 3, 4], distance=None, symmetric=True))
     cluster.categorize_node(1, cluster.CORE_NODE)
     self.assertTrue(1 in cluster.core_nodes
                     and 1 not in cluster.border_nodes)
     cluster.categorize_node(1, cluster.BORDER_NODE)
     self.assertTrue(1 in cluster.border_nodes
                     and 1 not in cluster.core_nodes)
     cluster.categorize_node(1, cluster.CORE_NODE)
     self.assertTrue(1 in cluster.core_nodes
                     and 1 not in cluster.border_nodes)
Пример #18
0
 def test_simple(self):
     io_graph = DenGraphVIO(
         base_graph=DistanceGraph(nodes=[1, 2, 3, 4, 5, 6],
                                  distance=DeltaDistance(),
                                  symmetric=True),
         cluster_distance=5,
         core_neighbours=5)
     cluster, distance = next(io_graph.probe(1))
     self.assertEqual(2.5, distance)
     io_graph[7] = {}
     cluster, distance = next(io_graph.probe(1))
     # expecting that algorithm uses cached mean
     self.assertEqual(2.5, distance)
Пример #19
0
    def test_sub_no_node(self):
        graph = DistanceGraph(nodes=[1, 2, 3, 4],
                              distance=None,
                              symmetric=True)
        cluster_a = DenGraphCluster(graph=graph)
        cluster_a.core_nodes = set([1, 2])
        cluster_b = DenGraphCluster(graph=graph)
        cluster_b.core_nodes = set([2, 3])

        with self.assertRaises(dengraph.graph.NoSuchNode):
            cluster_a - cluster_b
        with self.assertRaises(dengraph.graph.NoSuchNode):
            cluster_a -= cluster_b
Пример #20
0
    def test_inplace_add(self):
        graph = DistanceGraph(nodes=[1, 2, 3, 4],
                              distance=None,
                              symmetric=True)
        cluster_a = DenGraphCluster(graph)
        cluster_b = DenGraphCluster(graph)
        cluster_a.border_nodes = set([2, 3])
        cluster_a.core_nodes = set([1])
        cluster_b.border_nodes = set([3, 4])
        cluster_b.core_nodes = set([2])

        cluster_a += cluster_b
        self.assertEqual(set([1, 2]), cluster_a.core_nodes)
        self.assertEqual(set([3, 4]), cluster_a.border_nodes)
Пример #21
0
 def test_simple_incremental(self):
     io_graph = DenGraphVIO(base_graph=DistanceGraph(
         nodes=[1, 2, 3, 4, 5, 6],
         distance=IncrementalListDistance(),
         symmetric=True),
                            cluster_distance=5,
                            core_neighbours=5)
     base_object = [1]
     io_graph.probe(base_object)
     for i in range(1, 4):
         cluster, current_distance = next(io_graph.probe([1 + i]))
         _, new_distance = next(io_graph.update_probe(base_object, [1]))
         base_object[0] += 1
         self.assertEqual(current_distance, new_distance)
Пример #22
0
    def test_frozencluster(self):
        graph = DistanceGraph(nodes=[1, 2, 3, 4],
                              distance=None,
                              symmetric=True)
        cluster = DenGraphCluster(graph=graph)
        cluster.categorize_node(1, cluster.CORE_NODE)
        cluster.categorize_node(2, cluster.BORDER_NODE)
        cluster.categorize_node(3, cluster.BORDER_NODE)

        frozen = FrozenDenGraphCluster(cluster)
        with self.assertRaises(TypeError):
            frozen.categorize_node(4, frozen.BORDER_NODE)
        with self.assertRaises(TypeError):
            frozen += cluster
        with self.assertRaises(TypeError):
            frozen -= cluster
        self.assertIsNotNone({frozen: True})
Пример #23
0
    def test_sub(self):
        graph = DistanceGraph(nodes=[1, 2, 3, 4],
                              distance=None,
                              symmetric=True)
        cluster_a = DenGraphCluster(graph=graph)
        cluster_b = DenGraphCluster(graph=graph)
        cluster_a.border_nodes = set([2, 3])
        cluster_a.core_nodes = set([1])
        cluster_b.border_nodes = set([3])
        cluster_b.core_nodes = set([])

        cluster_c = cluster_a - cluster_b
        self.assertNotEqual(cluster_c, cluster_a)
        self.assertNotEqual(cluster_c, cluster_b)
        self.assertEqual(set([1]), cluster_c.core_nodes)
        self.assertEqual(set([2]), cluster_c.border_nodes)

        cluster_a -= cluster_b
        self.assertEqual(cluster_a, cluster_c)

        cluster_a -= cluster_a
        self.assertEqual(set([]), cluster_a.core_nodes)
        self.assertEqual(set([]), cluster_a.border_nodes)
Пример #24
0
    def test_creation(self):
        io_graph = DenGraphVIO(
            base_graph=DistanceGraph(nodes=[1, 2, 3, 4, 5, 6],
                                     distance=DeltaDistance(),
                                     symmetric=True),
            cluster_distance=5,
            core_neighbours=5)
        self.assertIsNotNone(io_graph)

        literal = textwrap.dedent("""
        1,2,3,4,5,6
        0,1,1,1,1,1
        1,0,1,1,1,1
        1,1,0,1,1,1
        1,1,1,0,1,1
        1,1,1,1,0,1
        1,1,1,1,1,0
        """.strip())
        with self.assertRaises(dengraph.distance.NoDistanceSupport):
            io_graph = DenGraphVIO(
                base_graph=dengraph.graphs.graph_io.csv_graph_reader(
                    literal.splitlines(), symmetric=True),
                cluster_distance=5,
                core_neighbours=5)
Пример #25
0
from __future__ import print_function
from dengraph.graphs.distance_graph import DistanceGraph
from dengraph.dengraph import DenGraphIO

# numeric data
sequence = (
    1, 2, 3, 4, 5,
    10, 11, 13, 14, 15, 17,
    22, 23, 24, 25,
    28, 29, 30, 31
)

# create graph based on element-to-element difference
graph = DistanceGraph(nodes=sequence, distance=lambda node_from, node_to: abs(node_from - node_to))

clusterings = []
for neighbours in range(1, 4):
    for distance in range(1, 4):
        dengraph = DenGraphIO(graph, cluster_distance=distance, core_neighbours=neighbours)
        clusterings.append((neighbours, distance, dengraph))


# Helpers for printing
def format_sequence(seq):
    fmt = ''
    for num in range(min(seq), max(seq) + 1):
        if num in sequence:
            fmt += '#'
        else:
            fmt += ' '
    return fmt