summaryrefslogtreecommitdiff
path: root/python/altgraph/altgraph_tests/test_graphstat.py
blob: b628b6f24c76c10730abb7e2c6ad198a08d3a097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import unittest

from altgraph import GraphStat
from altgraph import Graph
import sys

class TestDegreesDist (unittest.TestCase):

    def test_simple(self):
        a = Graph.Graph()
        self.assertEqual(GraphStat.degree_dist(a), [])

        a.add_node(1)
        a.add_node(2)
        a.add_node(3)

        self.assertEqual(GraphStat.degree_dist(a), GraphStat._binning([0, 0, 0]))

        for x in range(100):
            a.add_node(x)

        for x in range(1, 100):
            for y in range(1, 50):
                if x % y == 0:
                    a.add_edge(x, y)

        counts_inc = []
        counts_out = []
        for n in a:
            counts_inc.append(a.inc_degree(n))
            counts_out.append(a.out_degree(n))

        self.assertEqual(GraphStat.degree_dist(a), GraphStat._binning(counts_out))
        self.assertEqual(GraphStat.degree_dist(a, mode='inc'), GraphStat._binning(counts_inc))

class TestBinning (unittest.TestCase):
    def test_simple(self):

        # Binning [0, 100) into 10 bins
        a = list(range(100))
        out = GraphStat._binning(a, limits=(0, 100), bin_num=10)

        self.assertEqual(out,
                [ (x*1.0, 10) for x in range(5, 100, 10) ])


        # Check that outliers are ignored.
        a = list(range(100))
        out = GraphStat._binning(a, limits=(0, 90), bin_num=9)

        self.assertEqual(out,
                [ (x*1.0, 10) for x in range(5, 90, 10) ])


        out = GraphStat._binning(a, limits=(0, 100), bin_num=15)
        binSize = 100 / 15.0
        result = [0]*15
        for i in range(100):
            bin = int(i/binSize)
            try:
                result[bin] += 1
            except IndexError:
                pass

        result = [ (i * binSize + binSize/2, result[i]) for i in range(len(result))]

        self.assertEqual(result, out)

if __name__ == "__main__": # pragma: no cover
    unittest.main()