## mobicen / util / MyUtil.py @ b6158841

History | View | Annotate | Download (1.46 KB)

1 | e1cf8bea | LoreBz | from prettytable import PrettyTable |
---|---|---|---|

2 | import numpy as np |
||

3 | import networkx as nx |
||

4 | import random as rnd |
||

5 | import matplotlib.pyplot as plt |
||

6 | fa4a0a42 | LoreBz | plt.ion() |

7 | e1cf8bea | LoreBz | import matplotlib.colors as mcolors |

8 | import sys |
||

9 | import code # code.interact(local=dict(globals(), **locals())) |
||

10 | |||

11 | 2dbcade9 | LoreBz | def draw(G, pos, xmax, ymax, measures=None): |

12 | e1cf8bea | LoreBz | lay = {k:pos[k] for k in range(0,len(pos))} |

13 | 2dbcade9 | LoreBz | if not measures: |

14 | measures = nx.betweenness_centrality(G, normalized = False, weight = 'weight', endpoints=True) |
||

15 | ```
measure_name = "Betweenness"
``` |
||

16 | e1cf8bea | LoreBz | ```
nodes = nx.draw_networkx_nodes(G, lay, node_size=100, cmap=plt.cm.gnuplot2,
``` |

17 | node_color=measures.values(), |
||

18 | nodelist=measures.keys()) |
||

19 | nodes.set_norm(mcolors.SymLogNorm(linthresh=0.01, linscale=1)) |
||

20 | |||

21 | edges = nx.draw_networkx_edges(G, lay) |
||

22 | plt.title(measure_name) |
||

23 | plt.colorbar(nodes) |
||

24 | |||

25 | ```
plt.xlim(0, xmax)
``` |
||

26 | ```
plt.ylim(0, ymax)
``` |
||

27 | |||

28 | plt.draw() |
||

29 | ```
plt.pause(0.01)
``` |
||

30 | plt.clf() |
||

31 | fa4a0a42 | LoreBz | ```
#plt.show()
``` |

32 | e1cf8bea | LoreBz | |

33 | def dictAlikes(d1, d2, perc): |
||

34 | if (sorted(d1.keys()) != sorted(d2.keys())): |
||

35 | return False |
||

36 | for k in d1: |
||

37 | ```
a = float(d1[k])
``` |
||

38 | ```
b = float(d2[k])
``` |
||

39 | if (not b - (b*perc) <= a <= b + (b*perc)): |
||

40 | return False |
||

41 | return True |
||

42 | |||

43 | def summary(v): |
||

44 | ```
vmin=min(v)
``` |
||

45 | ```
vmax=max(v)
``` |
||

46 | ```
tot=sum(v)
``` |
||

47 | avg=np.mean(v) |
||

48 | std=np.std(v) |
||

49 | t = PrettyTable(['min','max', 'mean','std','tot']) |
||

50 | ```
s = "%.4f %.4f %.4f %.4f %.4f" % (vmin,vmax,avg,std,tot)
``` |
||

51 | t.add_row(s.split()) |
||

52 | ```
print t
``` |
||

53 | ` return avg, std, vmax, vmin, tot` |