Debian Codegraph @

altgraph/GraphStat.py

raw symbols SCIP
1 """
2 altgraph.GraphStat - Functions providing various graph statistics
3 =================================================================
4 """
5
6
7 def degree_dist(graph, limits=(0, 0), bin_num=10, mode="out"):
8 """
9 Computes the degree distribution for a graph.
10
11 Returns a list of tuples where the first element of the tuple is the
12 center of the bin representing a range of degrees and the second element
13 of the tuple are the number of nodes with the degree falling in the range.
14
15 Example::
16
17 ....
18 """
19
20 deg = []
21 if mode == "inc":
22 get_deg = graph.inc_degree
23 else:
24 get_deg = graph.out_degree
25
26 for node in graph:
27 deg.append(get_deg(node))
28
29 if not deg:
30 return []
31
32 results = _binning(values=deg, limits=limits, bin_num=bin_num)
33
34 return results
35
36
37 _EPS = 1.0 / (2.0**32)
38
39
40 def _binning(values, limits=(0, 0), bin_num=10):
41 """
42 Bins data that falls between certain limits, if the limits are (0, 0) the
43 minimum and maximum values are used.
44
45 Returns a list of tuples where the first element of the tuple is the
46 center of the bin and the second element of the tuple are the counts.
47 """
48 if limits == (0, 0):
49 min_val, max_val = min(values) - _EPS, max(values) + _EPS
50 else:
51 min_val, max_val = limits
52
53 # get bin size
54 bin_size = (max_val - min_val) / float(bin_num)
55 bins = [0] * (bin_num)
56
57 # will ignore these outliers for now
58 for value in values:
59 try:
60 if (value - min_val) >= 0:
61 index = int((value - min_val) / float(bin_size))
62 bins[index] += 1
63 except IndexError:
64 pass
65
66 # make it ready for an x,y plot
67 result = []
68 center = (bin_size / 2) + min_val
69 for i, y in enumerate(bins):
70 x = center + bin_size * i
71 result.append((x, y))
72
73 return result