-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
308 lines (290 loc) · 9.92 KB
/
Copy pathpreprocess.py
File metadata and controls
308 lines (290 loc) · 9.92 KB
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
# -*- coding: utf-8 -*-
import time
import datetime
import numpy as np
import os
def cut_file_dblp(inputfname,outputfname,timebegin,timeend,interval):
fin = open(inputfname)
fout_set = {}
linknum = {}
fin.readline()
while True:
line = fin.readline()
if not line:
break
x = str(line).split()
u = int(x[0])
v = int(x[1])
t = int(x[3])
if u<v and t >= timebegin:
year = datetime.datetime.fromtimestamp(t).strftime('%Y')
begin = datetime.datetime.fromtimestamp(timebegin).strftime('%Y')
year_2 = (int(year) - begin)/interval*interval +begin
if year_2 not in fout_set:
if year_2+interval-1 > timeend:
fout_set[year_2] = open(outputfname + str(year_2) + '-' + str(timeend) + '.txt', 'w')
else:
fout_set[year_2] = open(outputfname + str(year_2) + '-' + str(year_2+interval-1) + '.txt', 'w')
linknum[year_2] = linknum.get(year_2, 0) + 1
fout = fout_set[year_2]
fout.write(str(u)+'\t'+str(v)+'\t'+str(year)+'\n')
fout = open(outputfname + 'distribution' + '.txt', 'w')
for item in sorted(linknum.iteritems(), key = lambda d:d[0], reverse = False):
if item[0]+interval-1 > timeend:
fout.write(str(item[0]) + '-' + str(timeend) + str() + '\t' + str(item[1]) + '\n')
else:
fout.write(str(item[0]) + '-' + str(item[0]+interval-1) + '\t' + str(item[1]) + '\n')
fout.close()
for fp in fout_set.values():
fp.close()
fin.close()
print 'ok'
def cut_file_fb(inputfname,outputfname,timebegin,interval):
fin = open(inputfname)
fout_set = {}
linknum = {}
begin_y = int(datetime.datetime.fromtimestamp(timebegin).strftime('%Y'))
begin_m = int(datetime.datetime.fromtimestamp(timebegin).strftime('%m'))
while True:
line = fin.readline()
if not line:
break
x = str(line).split()
u = int(x[0])
v = int(x[1])
t = int(x[2])
if u!=v and t >= timebegin:
current = datetime.datetime.fromtimestamp(t)
year = int(current.year)
month = int(current.month)
day = int(current.day)
key = ((year - begin_y)*12 + month-1)/interval*interval + begin_y*12
if key not in fout_set:
fout_set[key] = open(outputfname + str(key/12*100+key%12+1) + '-' + str((key+interval-1)/12*100+(key+interval-1)%12+1) + '.txt', 'w')
linknum[key] = linknum.get(key, 0) + 1
fout = fout_set[key]
fout.write(str(u)+'\t'+str(v)+'\t'+str(year*10000+month*100+day)+'\n')
for fp in fout_set.values():
fp.close()
fin.close()
print 'ok'
def cut_file_fb2(inputfname,outputfname,timebegin,interval):
fin = open(inputfname)
fout_set = {}
linknum = {}
begin = datetime.datetime.fromtimestamp(timebegin)
while True:
line = fin.readline()
if not line:
break
x = str(line).split()
u = int(x[0])
v = int(x[1])
t = int(x[2])
if u!=v and t >= timebegin:
current = datetime.datetime.fromtimestamp(t)
year = int(current.year)
month = int(current.month)
day = int(current.day)
key = begin + datetime.timedelta(days = (current-begin).days/interval*interval)
if key not in fout_set:
next = key + datetime.timedelta(days = interval-1)
fout_set[key] = open(outputfname+str(key.year*10000+key.month*100+key.day)+'-'+str(next.year*10000+next.month*100+next.day)+'.txt','w')
linknum[key] = linknum.get(key, 0) + 1
fout = fout_set[key]
fout.write(str(u)+'\t'+str(v)+'\t'+str(year*10000+month*100+day)+'\n')
for fp in fout_set.values():
fp.close()
fin.close()
print 'ok'
def cut_file_imdb(inputfname,outputfname,timebegin,timeend,interval):
fin = open(inputfname)
fout_set = {}
for line in fin:
x = str(line).split()
u = int(x[0])
v = int(x[1])
year = int(x[2])
year_2 = (year - timebegin)/interval*interval + timebegin
if year_2 < timebegin or year_2 >= timeend:
continue
if year_2 not in fout_set:
fout_set[year_2] = open(outputfname + str(year_2) + '-' + str(year_2+interval-1) + '.txt', 'w')
fout = fout_set[year_2]
fout.write(str(u)+'\t'+str(v)+'\t'+str(year)+'\n')
def cut_file_mail(inputfname,outputfname,begin,interval):
fin = open(inputfname)
fname = open(outputfname + 'filename.txt','w')
fout_set = {}
filenames = []
linknum = {}
while True:
line = fin.readline()
if not line:
break
x = str(line).split('\t')
u = int(x[0])
v = int(x[1])
current = datetime.datetime.strptime(x[2][:-13],'%a, %d %b %Y %H:%M:%S')
if u!=v and current >= begin:
year = int(current.year)
month = int(current.month)
day = int(current.day)
key = ((year - begin.year)*12 + month-1)/interval*interval + begin.year*12
fn = outputfname + str(key/12*100+key%12+1) + '-' + str((key+interval-1)/12*100+(key+interval-1)%12+1) + '.txt'
if key not in fout_set:
filenames.append(fn)
fout_set[key] = open(fn, 'w')
fout = fout_set[key]
linknum[fn] = linknum.get(fn,0) + 1
fout.write(str(u)+'\t'+str(v)+'\t'+str(year*10000+month*100+day)+'\n')
for fp in fout_set.values():
fp.close()
for fn in sorted(filenames):
fname.write(fn + '\t' + str(linknum[fn]) + '\n')
fname.close()
fin.close()
print 'ok'
def read_nodes(fn):
f=open(fn)
nodes=set()
for line in f:
x=str(line).split('\t')
u=int(x[0])
v=int(x[1])
nodes.add(u)
nodes.add(v)
f.close()
return nodes
def read_edges(fn):
f=open(fn)
edges=set()
for line in f:
x=str(line).split('\t')
u=int(x[0])
v=int(x[1])
edges.add((u,v))
f.close()
return edges
#indexed from 0
def build_net(edges):
dim = 0
for x in edges:
if x[1] > dim:
dim = x[1]
print dim
mat = np.zeros((dim, dim), dtype = np.int)
for x in edges:
u = x[0] - 1
v = x[1] - 1
mat[u][v] = mat[v][u] = 1
return mat
def preprocess_file(fname1,fname2,fname3,fname4):
nodes1 = read_nodes(fname1)
nodes2 = read_nodes(fname2)
nodes = nodes1 & nodes2
fin1 = open(fname1)
fout1 = open(fname3,'w')
linknum1 = 0
while True:
line = fin1.readline()
if not line:
break
x=str(line).split('\t')
u=int(x[0])
v=int(x[1])
if (u in nodes)&(v in nodes):
fout1.write(line)
linknum1 += 1
fin1.close()
fout1.close()
fin2 = open(fname2)
fout2 = open(fname4,'w')
linknum2 = 0
while True:
line = fin2.readline()
if not line:
break
x=str(line).split('\t')
u=int(x[0])
v=int(x[1])
if (u in nodes)&(v in nodes):
fout2.write(line)
linknum2 += 1
fin1.close()
fout1.close()
print fname1 + ' has ' + str(linknum1) + ' edges with two common nodes'
print fname2 + ' has ' + str(linknum2) + ' edges with two common nodes'
def preprocess2_file(fname1,fname2,fname3,fname4):
nodes1 = read_nodes(fname1)
nodes2 = read_nodes(fname2)
nodes = nodes1 & nodes2
fin1 = open(fname1)
fout1 = open(fname3,'w')
linknum1 = 0
while True:
line = fin1.readline()
if not line:
break
x=str(line).split('\t')
u=int(x[0])
v=int(x[1])
if (u in nodes)|(v in nodes):
fout1.write(line)
linknum1 += 1
fin1.close()
fout1.close()
fin2 = open(fname2)
fout2 = open(fname4,'w')
linknum2 = 0
while True:
line = fin2.readline()
if not line:
break
x=str(line).split('\t')
u=int(x[0])
v=int(x[1])
if (u in nodes)|(v in nodes):
fout2.write(line)
linknum2 += 1
fin1.close()
fout1.close()
print fname1 + ' has ' + str(linknum1) + ' edges with one common nodes'
print fname2 + ' has ' + str(linknum2) + ' edges with one common nodes'
if __name__ == '__main__':
'''a="1971-01-01 00:00:00"
timeBegin=time.strptime(a,"%Y-%m-%d %H:%M:%S")
timeStampBegin=int(time.mktime(timeBegin))
fn='C:\Users\cc\Desktop\dblp_coauthor\dblp_coauthor/out.dblp_coauthor'#out.dblp_coauthor
fout='C:\Users\cc\Desktop\dblp_coauthor\dblp_coauthor/dblp'
for interval in range(6):
path = fout + str(interval+1) + '/'
os.mkdir(path)
cut_file_dblp(fn,path,timeStampBegin,2014,interval+1)
a="2004-01-01 00:00:00"
timeBegin=time.strptime(a,"%Y-%m-%d %H:%M:%S")
timeStampBegin=int(time.mktime(timeBegin))
fn = 'C:\Users\cc\Desktop/facebook-wall/facebook-wall.txt.anon'
fout = 'C:\Users\cc\Desktop/facebook-wall/facebook'
for interval in range(3):
path = fout + str(interval+1) + '/'
os.mkdir(path)
cut_file_fb(fn,path,timeStampBegin,interval+1)
for interval in range(10,31,10):
path = fout + str(interval) + '/'
os.mkdir(path)
cut_file_fb2(fn,path,timeStampBegin,interval)'''
'''fn='D:\imdb_b\imdb_b.txt'
fout='D:\imdb_b\imdb'
for interval in range(1):
path = fout + str(interval+1) + '/'
os.mkdir(path)
cut_file_imdb(fn,path,1990,2005,interval+1)'''
a="2000-01-01 00:00:00"
timeBegin=datetime.datetime.strptime(a,"%Y-%m-%d %H:%M:%S")
fn = 'C:\Users\cc\Desktop\mail\mails.txt'
fout = 'C:\Users\cc\Desktop/mail/mail'
for interval in range(3):
path = fout + str(interval+1) + '/'
os.mkdir(path)
cut_file_mail(fn,path,timeBegin,interval+1)