forked from psychemedia/newt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nxGdfFilter.py
168 lines (140 loc) · 4.07 KB
/
nxGdfFilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import networkx as nx
import newtx as nwx
import csv,sys,newt
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
run=['filter']
#['filter','reciprocal']
api=newt.getTwitterAPI()
def getNodeAttr(cell):
cell=cell.strip()
cell=cell.split()
return cell[0].strip(),cell[1].strip()
def labelGraph(LG,idlist):
idlabels=newt.twNamesFromIds(api,idlist)
for id in idlabels:
if str(id) in LG.node:
LG.node[str(id)]['label']=idlabels[id]
return LG
#G=nx.Graph()
addUserFriendships=1
try:
mindegree=sys.argv[1]
except:
mindegree=10
try:
user=sys.arg[2]
except:
addUserFriendships=0
user=''
try:
typ=sys.argv[3]
except:
typ=0
path='reports'
#agent='fq50_combinedfollowers_0ormore_sample50'
#agent="edgehill"
#agent='hashtag-fote11/followers_2ormore_sample50'
agent='fq50_combinedfriends_4ormore_sample2000'
agent='hashtag-lseneted/followers_3ormore_sample500'
agent='foreignoffice_lists/foreign-office-on-twitter'
agent='theul'
agent='hashtag-gdslaunch/followers_3ormore_sample195'
typ='friends'
tt='extrafriends'
report=tt+'Net_2011-12-08-11-54-11.gdf'
fn='/'.join([path,agent,typ,report])
print 'Loading file...',fn
DG=nwx.directedNetworkFromGDF(fn)
'''
f=open('/'.join([path,agent,report]),'rb')
reader = csv.reader(f)
print "Loading graph..."
phase='nodes'
header=reader.next()
print "Loading nodes..."
for row in reader:
if row[0].startswith('edgedef>'):
phase='edges'
print "Moving on to edges"
continue
elif phase=='edges':
fromNode=row[0].strip()
toNode=row[1].strip()
#print ':'.join(['Adding edge',fromNode,toNode])
DG.add_edge(fromNode,toNode)
else:
id=row[0].strip()
val=row[1].strip()
#print ':'.join(['Adding node',id,val])
if id!='' and val!='':
DG.add_node(id,label=val)
'''
#print DG.degree()
print DG.order(),DG.size()
#Generate subgraph with nodes that have a greater than N degree:
def filterNet(DG,mindegree):
if addUserFriendships==1:
DG=addFocus(DG,user,typ)
mindegree=int(mindegree)
filter=[]
filter= [n for n in DG if DG.degree(n)>=mindegree]
H=DG.subgraph(filter)
print "Filter set:",filter
print H.order(),H.size()
LH=labelGraph(H,filter)
now = datetime.datetime.now()
ts = now.strftime("_%Y-%m-%d-%H-%M-%S")
nx.write_graphml(H, '/'.join([path,agent,typ,tt+"degree"+str(mindegree)+ts+".graphml"]))
nx.write_edgelist(H, '/'.join([path,agent,typ,tt+"degree"+str(mindegree)+ts+".txt"]),data=False)
#delimiter=''
#indegree=sorted(nx.indegree(DG).values(),reverse=True)
indegree=H.in_degree()
outdegree=H.out_degree()
inout = [indegree, outdegree]
inoutpair = {}
for k in indegree.iterkeys():
inoutpair[k] = tuple(inoutpair[k] for inoutpair in inout)
fig = plt.figure()
ax = fig.add_subplot(111)
#ax.plot(indegree,outdegree, 'o')
#ax.set_title('Indegree vs outdegree')
degree_sequence=sorted(indegree.values(),reverse=True)
plt.loglog(degree_sequence)
plt.savefig( '/'.join([path,agent,typ,tt+"degree"+str(mindegree)+"outdegree_histogram.png"]))
#plt.show()
def addFocus(DG,user,typ='all'):
userData=api.get_user(user)
userID=userData.id
if userID not in DG.nodes():
if typ=='all' or typ=='fr':
print 'adding in user friendships...'
userFriends=api.friends_ids(user)
print user,'as',userID
frNet=nwx.createTwitterFnet(api,user,typ='friends')
DG=nwx.mergeNets(DG,frNet)
if typ=='all' or typ=='fo':
print 'adding in user follwerships...'
userFollowers=api.followers_ids(user)
print user,'as',userID
foNet=nwx.createTwitterFnet(api,user,typ='followers')
DG=nwx.mergeNets(DG,foNet)
return DG
def reciprocalNet(DG):
if user !='' and addUserFriendships==1 :
DG=addFocus(DG,user)
print 'Looking for reciprocal edges...'
reciprocalG=DG.to_undirected(reciprocal=True)
print reciprocalG.order(),reciprocalG.size()
filter= [n for n in reciprocalG if reciprocalG.degree(n)>=1]
print 'Filtering out unconnected nodes...'
H=reciprocalG.subgraph(filter)
print "Filter set:",filter
print H.order(),H.size()
LH=nwx.labelGraph(api,H,filter)
nx.write_graphml(H, "junk/testReciprocal.graphml")
if 'filter' in run: filterNet(DG,mindegree)
if 'reciprocal' in run:
reciprocalNet(DG)