Statistics
| Branch: | Revision:

mobicen / util / MyUtil.py @ 05763cfb

History | View | Annotate | Download (2.08 KB)

1
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

    
7
import matplotlib.colors as mcolors
8
import sys
9
import code  # code.interact(local=dict(globals(), **locals()))
10

    
11
def draw(G, pos, xmax, ymax, measures=None, saveFig=False, path=None):
12
    fig = plt.figure()
13
    lay = {k:pos[k] for k in range(0,len(pos))}
14
    if not measures:
15
        measures = nx.betweenness_centrality(G, normalized = True, weight = 'weight', endpoints=True)
16

    
17
    measure_name = "Betweenness"
18

    
19
    nodes = nx.draw_networkx_nodes(G, lay, node_size=50, cmap=plt.cm.Spectral, 
20
                                       node_color=measures.values(),
21
                                       nodelist=measures.keys())
22

    
23
    #nodes.set_norm(mcolors.SymLogNorm(linthresh=0.01, linscale=1))
24
    mes=measures.values()
25
    nodes.set_norm(mcolors.LogNorm(vmin=0.001, vmax=max(mes)))
26
    #nodes.set_norm(mcolors.LogNorm())
27

    
28
    edges = nx.draw_networkx_edges(G, lay)
29
    plt.title(measure_name)
30

    
31
    myticks=np.geomspace(start=0.001, stop=max(mes), num=7)
32
    cbar=plt.colorbar(nodes, ticks=myticks, label='Norm BC')
33
    cbar.ax.set_yticklabels(["{0:0.4f}".format(i) for i in myticks])
34
    #plt.colorbar(ticks=range(6), label='digit value')
35
    #plt.clim(-0.5, 5.5)
36
    plt.xlim(0, xmax)
37
    plt.ylim(0, ymax)
38
    plt.xticks(range(0,105,5))
39
    plt.yticks(range(0,105,5))
40
    plt.grid(which='both')
41
    if not saveFig:
42
        plt.ion()
43
        plt.draw()
44
        plt.pause(0.01)
45
        plt.clf()
46
    else:
47
        fig.savefig(path, format='pdf')
48
        #plt.clf()
49
        #plt.show()
50

    
51
def dictAlikes(d1, d2, perc):
52
        if (sorted(d1.keys()) != sorted(d2.keys())):
53
                return False
54
        for k in d1:
55
                a = float(d1[k])
56
                b = float(d2[k])
57
                if (not b - (b*perc) <= a <= b + (b*perc)):
58
                        return False
59
        return True
60

    
61
def summary(v):
62
    vmin=min(v)
63
    vmax=max(v)
64
    tot=sum(v)
65
    avg=np.mean(v)
66
    std=np.std(v)
67
    t = PrettyTable(['min','max', 'mean','std','tot'])
68
    s = "%.4f %.4f %.4f %.4f %.4f" % (vmin,vmax,avg,std,tot)
69
    t.add_row(s.split())
70
    print t
71
    return avg, std, vmax, vmin, tot