1import unittest 2 3from altgraph import GraphStat 4from altgraph import Graph 5import sys 6 7class TestDegreesDist (unittest.TestCase): 8 9 def test_simple(self): 10 a = Graph.Graph() 11 self.assertEqual(GraphStat.degree_dist(a), []) 12 13 a.add_node(1) 14 a.add_node(2) 15 a.add_node(3) 16 17 self.assertEqual(GraphStat.degree_dist(a), GraphStat._binning([0, 0, 0])) 18 19 for x in range(100): 20 a.add_node(x) 21 22 for x in range(1, 100): 23 for y in range(1, 50): 24 if x % y == 0: 25 a.add_edge(x, y) 26 27 counts_inc = [] 28 counts_out = [] 29 for n in a: 30 counts_inc.append(a.inc_degree(n)) 31 counts_out.append(a.out_degree(n)) 32 33 self.assertEqual(GraphStat.degree_dist(a), GraphStat._binning(counts_out)) 34 self.assertEqual(GraphStat.degree_dist(a, mode='inc'), GraphStat._binning(counts_inc)) 35 36class TestBinning (unittest.TestCase): 37 def test_simple(self): 38 39 # Binning [0, 100) into 10 bins 40 a = list(range(100)) 41 out = GraphStat._binning(a, limits=(0, 100), bin_num=10) 42 43 self.assertEqual(out, 44 [ (x*1.0, 10) for x in range(5, 100, 10) ]) 45 46 47 # Check that outliers are ignored. 48 a = list(range(100)) 49 out = GraphStat._binning(a, limits=(0, 90), bin_num=9) 50 51 self.assertEqual(out, 52 [ (x*1.0, 10) for x in range(5, 90, 10) ]) 53 54 55 out = GraphStat._binning(a, limits=(0, 100), bin_num=15) 56 binSize = 100 / 15.0 57 result = [0]*15 58 for i in range(100): 59 bin = int(i/binSize) 60 try: 61 result[bin] += 1 62 except IndexError: 63 pass 64 65 result = [ (i * binSize + binSize/2, result[i]) for i in range(len(result))] 66 67 self.assertEqual(result, out) 68 69if __name__ == "__main__": # pragma: no cover 70 unittest.main() 71