-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplot_ts.py
356 lines (323 loc) · 13 KB
/
plot_ts.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
__author__ = 'Moses Boudourides <[email protected]>, Sergios Lenis <[email protected]>'
__coder__ ='Sergios Lenis <[email protected]>'
import argparse
import datetime
import os
import glob
import pickle
import matplotlib.pyplot as plt
import random
import json
import networkx as nx
import itertools as it
''' Run it with:
python plots_ts.py -h to see the optional arguments
'''
def dir_search_trees(argsr):
'''
Search the directory for folder with the preprocessed files and returns the directories
If --input is searching that it exist
If --join only the folders of join
'''
dir_in_search_list=[]
dir_search=os.getcwd()
if argsr.input !=None:
dir_in_search_list = [os.path.join('%s' % dir_search,argsr.input)]
return dir_in_search_list
else:
# print dir_search
for jj in os.listdir(dir_search):
dir_in_search = os.path.join('%s' % dir_search,jj)
try:
# print os.listdir(dir_in_search)
dir_in_search_dm=os.path.join('%s' % dir_in_search,'*.dmp')
dmp_files = glob.glob(dir_in_search_dm)
already_added=[]
if len(dmp_files) == 0:
continue
else:
if argsr.join !=None:
if jj in argsr.join:
dir_in_search_list.append(dir_in_search)
else:
dir_in_search_list.append(dir_in_search)
# print dmp_files
except Exception, e:
print e
# if argsr.input !=None:
return dir_in_search_list
def dic_operations(dir_search,argsr,ploted=False,bars=False):
colors_lists=['k','c','m','g','r','b']
colors_list=list(colors_lists)
fi_li=[]
lala=[]
labels=dict()
lalad=dict()
for dirpath in dir_search:
dir_in_search_dm=os.path.join('%s' % dirpath ,'*.dmp')
dmp_files = glob.glob(dir_in_search_dm)
file_name_of_direct=dirpath.split('/')[-1]
print file_name_of_direct
# print dmp_files
already_added=[]
if len(dmp_files) == 0:
print 'a'
continue
if len(colors_list)>0:
col=colors_list.pop()
g=col+'o-'
else:
colors_list=list(colors_lists)
col=colors_list.pop()
g=col+'o.-'
for fil in dmp_files:
# print fil
fi=fil.split('_')
fic=fi[-1].split('.')
if fic[0]=='count':
ff=open(fil)
created_at_count=pickle.load(ff)
ff.close()
fi_li.append(fi[0])
elif fic[0]=='dic':
ff=open(fil)
created_at_dic=pickle.load(ff)
ff.close()
elif fic[0]=='has':
ff=open(fil)
hashtags_count=pickle.load(ff)
ff.close()
elif fic[0]=='json':
ff=open(fil)
json_dic=pickle.load(ff)
ff.close()
elif fic[0]=='word':
ff=open(fil)
text_list=pickle.load(ff)
ff.close()
elif fic[0]=='retId':
ff=open(fil)
retweeted_set=pickle.load(ff)
ff.close()
elif fic[0]=='retweet':
ff=open(fil)
retweet=pickle.load(ff)
ff.close()
elif fic[0]=='tjson':
ff=open(fil)
json_time_dic=pickle.load(ff)
ff.close()
elif fic[0]=='user':
ff=open(fil)
user_set=pickle.load(ff)
ff.close()
elif fic[0]=='userIdName':
ff=open(fil)
user_id_name=pickle.load(ff)
ff.close()
elif fic[0]=='upperlevel' and 'hasthtag' in argsr.hasthags_graph:
ff=open(fil)
upperlevel=pickle.load(ff)
ff.close()
elif fic[0]=='upperlevelterms' and 'terms' in argsr.hasthags_graph :
ff=open(fil)
upperlevel_terms=pickle.load(ff)
ff.close()
elif fic[0]=='interlevel' and 'hasthtag' in argsr.hasthags_graph:
ff=open(fil)
interlevel=pickle.load(ff)
ff.close()
elif fic[0]=='interlevelterms' and 'terms' in argsr.hasthags_graph :
ff=open(fil)
interlevel_terms=pickle.load(ff)
ff.close()
elif fic[0]=='barHas':
ff=open(fil)
bar_has=pickle.load(ff)
ff.close()
else:
print fil,'no'
if ploted:
x=[]
y=[]
label_se=[]
for i in sorted(created_at_count.keys()):
x.append(i)
y.append(created_at_count[i])
if created_at_dic[i] in lala:
c=lalad[created_at_dic[i]]
if c>i:
labels[i]=created_at_dic[i]
lalad[created_at_dic[i]]=i
del labels[c]
else:
labels[i]=created_at_dic[i]
lalad[created_at_dic[i]]=i
lala.append(created_at_dic[i])
g=col+'o-'
print fi[0]
line, = plt.plot(x, y, g,label=fi[0])
plt.xticks(labels.keys(), labels.values(), rotation=45)
if argsr.userId:
users=user_set.union(retweeted_set)
notusers=retweeted_set.intersection(user_set)
print len(notusers),'<=== mention users added'
print len(users),'<=== Total users found'
filedir=os.path.join('%s' % dirpath,'users')
try:
os.stat(filedir)
except:
os.mkdir(filedir)
dic_filename=file_name_of_direct +'_userset.dmp'
print dic_filename
outfile_name = os.path.join('%s' % filedir,dic_filename)
ff=open(outfile_name,'w')
pickle.dump(users,ff)
ff.close()
json_tname='user_list.txt'
outfile=os.path.join('%s' % filedir,json_tname)
ff=open(outfile,'w')
for i in users:
# print i,type(i)
ff.write(str(i)+'\n')
ff.close()
if argsr.writing:
G=nx.DiGraph()
for dd in json_time_dic:
if dd[1] not in already_added:
filedir=filedir=os.path.join('%s' % dirpath,'time_jsons')
already_added.append(dd[1])
try:
os.stat(filedir)
except:
os.mkdir(filedir)
json_tname=dd[1]+'_.json'
outfile=os.path.join('%s' % filedir,json_tname)
ostream = outfile and file(outfile, 'a+') or sys.stdout
for kk in json_time_dic[dd]:
print >> ostream, json.dumps(kk)
ostream.close()
filedir=os.path.join('%s' % dirpath,'graphmls')
# already_added.append(dd[1])
try:
os.stat(filedir)
except:
os.mkdir(filedir)
json_tname='retweet.graphml'
outfile=os.path.join('%s' % filedir,json_tname)
# ostream = outfile and file(outfile, 'a+') or sys.stdout
for tt in retweet:
for rtee in tt[1]:
if G.has_edge(rtee,tt[0]):
wei=G[rtee][tt[0]]['weight']
else:
wei =0
G.add_edge(rtee,tt[0],weight= wei+1)
nx.write_graphml(G,outfile)
F=nx.Graph()
if 'hasthtag' in argsr.hasthags_graph :
json_tname='interlevel_hashtags.graphml'
outfile=os.path.join('%s' % filedir,json_tname)
for user_id in interlevel:
for hastt in interlevel[user_id]:
for hasht in hastt:
if interlevel[user_id][hasht]>0:
F.add_edge(user_id,hasht,weight=interlevel[user_id][hasht])
nx.write_graphml(F,outfile)
if 'terms' in argsr.hasthags_graph :
json_tname='interlevel_terms.graphml'
outfile=os.path.join('%s' % filedir,json_tname)
for user_id in interlevel_terms:
for hasht in interlevel_terms[user_id]:
if interlevel_terms[user_id][hasht]>0:
F.add_edge(user_id,hasht,weight=interlevel_terms[user_id][hasht])
nx.write_graphml(F,outfile)
fg=nx.Graph()
if 'hasthtag' in argsr.hasthags_graph :
ffffff=open('out.tst','w')
json_tname='upperlevel_hashtags.graphml'
outfile=os.path.join('%s' % filedir,json_tname)
for hs in it.combinations(upperlevel,2):
aa=upperlevel[hs[0]]
bb=upperlevel[hs[1]]
wei=set(aa).intersection(set(bb))
if len(wei)>0:
# fg.add_edge(hs[0],hs[1],weight=len(wei))
try:
ffffff.write(unicode(hs[0])+unicode(hs[1]))
fg.add_edge(unicode(hs[0]),unicode(hs[1]),weight=len(wei))
# print hs[0],hs[1]
except:
continue
nx.write_graphml(fg,outfile)
if 'terms' in argsr.hasthags_graph :
json_tname='upperlevel_terms.graphml'
print json_tname
outfile=os.path.join('%s' % filedir,json_tname)
for hs in it.combinations(upperlevel_terms,2):
aa=upperlevel_terms[hs[0]]
bb=upperlevel_terms[hs[1]]
wei=set(aa).intersection(set(bb))
if len(wei)>0:
fg.add_edge(unicode(hs[0]),unicode(hs[1]),weight=len(wei))
nx.write_graphml(fg,outfile)
if argsr.bars:
# json_tname='Hashtags_Twitter_id.xtx'
# outfile=os.path.join('%s' % filedir,json_tname)
# print outfile,'fdskjllllla'
# ostream = outfile and file(outfile, 'a+') or sys.stdout
# f=open(outfile,'a')
for i in bar_has:
print i,' ==> ',len(bar_has[i])
# print >> ostream, i ,' ==> ',len(bar_has[i])
# f.write(str(i)+' ==> '+str(bar_has[i]))
# f.close()
# print >> ostream, json.dumps(tt)
# ostream.close()
if ploted:
plt.legend()
plt.show()
if __name__ == '__main__':
# json_filename = None # The json to process
# lookup_text = None # Text to search for
# Parse command-line args for output file name.
parser = argparse.ArgumentParser(description=(
'Proccessing the temp file(s) and create the time jsons and word jsons'
'and plot the timesequence or (and) the hashtags'))
parser.add_argument('-o', '--output',nargs='?', metavar='Output FILE', type=str,
default=None, help='output folder name')
parser.add_argument('-i', '--input',nargs='*', metavar='Input FILE', type=str,
default=None, help='input folder name')
parser.add_argument('-p', '--plot',nargs='?', metavar='Ploting seq', type=bool,
default=False, help='ploting the tweets per sequence')
parser.add_argument('-b', '--bars',nargs='?', metavar='plot bars', type=bool,
default=False, help='bar plot enable')
parser.add_argument('-w', '--writing',nargs='?', metavar='writing json graph', type=bool,
default=True, help='writing json files and graph enable')
parser.add_argument('-j', '--join',nargs='*', metavar='Join FILES', type=str,
default=None, help='join files with names')
parser.add_argument('-u', '--userId',nargs='?', metavar='Write users', type=bool,
default=True, help='Write the list of users Id')
parser.add_argument('-ht', '--hasthags_graph',nargs='+', metavar='hasthtags or terms', type=str,
default='hasthtags', help='Create the hashtags or terms upperlevel interlevel graphs')
argsr = parser.parse_args()
print argsr
# print aaaa
if argsr.plot == False:
ploting=False
else:
ploting=True
if argsr.bars==False:
bar_plot=False
else:
bar_plot=True
dir_in_search_list=[]
# print ploting,bar_plot
dir_in_search_list=dir_search_trees(argsr)
if len(dir_in_search_list)==0:
print 'No folders found exiting...'
quit()
# print dir_in_search_list
# print aaa
dic_operations(dir_in_search_list,argsr,ploted=ploting,bars=bar_plot)