Statistics
| Branch: | Revision:

nepatest_popbabel / scripts / k_core.py @ 60ba786f

History | View | Annotate | Download (1.19 KB)

1
#!/usr/bin/env python
2
import networkx as nx
3
import sys
4
from matplotlib import pyplot as plt
5
from pprint import pprint
6
import sys
7
import code
8

    
9
fname=sys.argv[1]
10
if(fname.endswith(".edges")):
11
    g = nx.read_weighted_edgelist(fname)
12
elif(fname.endswith(".graphml")):
13
    g = nx.read_graphml(fname, node_type=int)
14
loads=nx.load_centrality(g,normalized=False,weight='weight')
15

    
16
g.remove_edges_from(g.selfloop_edges())
17
c = nx.k_core(g, 2)
18

    
19

    
20

    
21
a = [n for n in nx.articulation_points(c)]
22

    
23
fail_candidates = [ n for n in c.nodes() if n not in a]
24

    
25

    
26
toFails={}
27
for n in fail_candidates:
28
    gg = g.copy()
29
    gg.remove_node(n)
30
    comp = list(nx.connected_components(gg))
31
    #print n
32
    #pprint(comp)
33
    isolated_nodes = [x for component in comp[1:] for x in component]
34
    #code.interact(local=dict(globals(), **locals()))
35
    #print "XX", isolated_nodes
36
    if (len(isolated_nodes)==0):
37
            if (loads[n]>0):
38
                    toFails[n]=loads[n]
39

    
40

    
41
#pprint(sorted(toFails.items(), key=lambda x:x[1], reverse=True))
42
print fname, "size: ",len(g.nodes())
43
for e in sorted(toFails.items(), key=lambda x:x[1], reverse=True):
44
        sys.stdout.write(str(e[0])+",")
45
print ""
46
print "#nf", len(toFails)
47

    
48
'''nx.draw(g)
49
plt.show()
50
nx.draw(c)
51
plt.show()'''
52

    
53