-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnowballSampleFromSNAPpokecNetwork.py
executable file
·397 lines (316 loc) · 14.9 KB
/
snowballSampleFromSNAPpokecNetwork.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
##############################################################################
#
# snowballSampleFromSNAPpokecNetwork.py - snowball sample Pokec network
#
# File: snowballSampleFromSNAPpokecNetwork.py
# Author: Alex Stivala
# Created: November 2018
#
#
##############################################################################
"""Do snowball sampling in a the Pokec online social network (directed)
Output files (sample description file giving names of following files,
subgraphs as Pajek edge lists (node numbers 1..N_s), zone files giving
zone for each node, attirbute files giving attributes for each node)
in a directory in format used by EstimNetDirected.
Usage:
snowballSampleFromSNAPpokecNetwork.py data_dir num_samples num_seeds num_waves outputdir
data_dir is directory containing the Pokec data from SNAP
(https://snap.stanford.edu/data/soc-Pokec.html)
num_samples is number of snowball samples to create
num_seeds it number of seeds in each sample
num_Waves is numer of snowball sampling waves
outputdir is output directory to create output files in
WARNING: the output files are overwritten if they exist.
For SNAP see
http://snap.stanford.edu/snappy/index.html
Used version 4.1.0.
Note this requires a lot of memory, at least 10 GB.
"""
import sys,os,time
import getopt
import random
import datetime
import snap
from load_pokec_data import load_pokec_data
from snowballSample import snowball_sample,write_graph_file,write_zone_file
#-----------------------------------------------------------------------------
#
# Functions
#
#-----------------------------------------------------------------------------
def pdate_to_days_since_1970(pdate):
"""
pdate_to_days_since_1970() - convert date to days since 9170
convert date of registration in the SNAP Pokec data set format e.g.
"2007-12-13 00:00:00.0" to days since January 1, 1970
to be compatible with R script
Parameters:
pdate - date in SNAP Pokec data set format
Return value:
days since 1970
"""
thedate = datetime.datetime.strptime(pdate.split(' ')[0], "%Y-%m-%d")
return (thedate - datetime.datetime(1970,1,1,0,0)).days
def convert_to_int_cat(attrs):
"""
convert_to_int_cat() - convert string categorical attrs to integer
Like factor() in R, convert categories represented as strings into
integers.
Parameters:
attrs - list of string attributes
Return value:
list of integer attributes corresponding to the strings
"""
# build dict mapping string to integer for unique strings in attrs list
fdict = dict([(y,x) for (x,y) in enumerate(set(attrs))])
print(fdict) # output for possible future reversal (TODO write to file)
return ['NA' if x == 'null' else fdict[x] for x in attrs]
def write_subactors_file_binary(filename, G, nodelist, profile, colnames):
"""
write_subactors_file_binary() - write binary node attribute file
The EstimNetDirected format of the binary actor attribute file is
the header line with attribute names and then
the attribute value for each on one line per node. See
load_integer_attributes() in digraph.c
Parameters:
filename -filename to write to (warning: overwritten)
G - SNAP graph/network object.
nodelist - list of nodeids used to order the nodes in the output
profile - dictionary mapping node ID (int) to list
of attributes (all strings)
colnames - dict mapping attribute name to
index of the profile list so e.g. we can look
up AGE of userid 123 with
profile[123][colnames['AGE']]
Return value:
None
"""
assert(len(nodelist) == G.GetNodes())
assert(len(profile) >= G.GetNodes())
binattrs = ['gender', 'public']
# rename gender to male for binary attribute
binattr_names = ['male' if x == 'gender' else x for x in binattrs]
with open(filename, 'w') as f:
f.write(' '.join(binattr_names) + '\n')
for i in nodelist:
for attr in binattrs:
val = profile[i][colnames[attr]]
val = val if val in ['0','1'] else 'NA'
f.write(val)
if attr == binattrs[-1]:
f.write('\n')
else:
f.write(' ' )
def write_subactors_file_categorical(filename, G, nodelist, profile, colnames):
"""
write_subactors_file_categorical() - write categorical node attribute file
The EstimNetDirected format of the categorical actor attribute file is
the header line with attribute names and then
bthe attribute value (integer) for each on one line per node. See
load_integer_attributes() in digraph.c
Parameters:
filename -filename to write to (warning: overwritten)
G - SNAP graph/network object.
nodelist - list of nodeids used to order the nodes in the output
profile - dictionary mapping node ID (int) to list
of attributes (all strings)
colnames - dict mapping attribute name to
index of the profile list so e.g. we can look
up AGE of userid 123 with
profile[123][colnames['AGE']]
Return value:
None
"""
assert(len(nodelist) == G.GetNodes())
assert(len(profile) >= G.GetNodes())
catattrs = ['gender', 'region']
catattr_names = catattrs
with open(filename, 'w') as f:
f.write(' '.join(catattr_names) + '\n')
for i in nodelist:
for attr in catattrs:
val = profile[i][colnames[attr]]
val = val if isinstance(val, int) else (int(val) if val.isdigit() else 'NA')
f.write(str(val))
if attr == catattrs[-1]:
f.write('\n')
else:
f.write(' ' )
def write_subactors_file_continuous(filename, G, nodelist, profile, colnames):
"""
write_subactors_file_continuous() - write continuous node attribute file
The EstimNetDirected format of the continuous actor attribute file is
the header line with attribute names and then
bthe attribute value (integer) for each on one line per node. See
load_integer_attributes() in digraph.c
Parameters:
filename -filename to write to (warning: overwritten)
G - SNAP graph/network object.
nodelist - list of nodeids used to order the nodes in the output
profile - dictionary mapping node ID (int) to list
of attributes (all strings)
colnames - dict mapping attribute name to
index of the profile list so e.g. we can look
up AGE of userid 123 with
profile[123][colnames['AGE']]
Return value:
None
"""
assert(len(nodelist) == G.GetNodes())
assert(len(profile) >= G.GetNodes())
contattrs = ['AGE', 'registration', 'completion_percentage']
contattr_names = [s.lower() for s in contattrs]
with open(filename, 'w') as f:
f.write(' '.join(contattr_names) + '\n')
for i in nodelist:
for attr in contattrs:
val = profile[i][colnames[attr]]
if attr == 'AGE':
# integer, 0 - age attribute not set
val = 'NA' if (val == 'null' or float(val) == 0) else float(val)
elif attr == 'registration':
# datetime, time at which the
# user registered at the site
## e.g. "2007-12-13 00:00:00.0"
## convert date of registration to days since January 1, 1970
## to be compatible with R script
val = 'NA' if val == 'null' else str(pdate_to_days_since_1970(val))
else:
val = 'NA' if val == 'null' else float(val)
f.write(str(val))
if attr == contattrs[-1]:
f.write('\n')
else:
f.write(' ' )
def write_subgraph_nodeids(filename, nodelist):
"""write_subgraph_nodeids() - write mapping from subgraph sequential ids
to original graph node ids
Writes the original graph node identifiers in file one per line in
same order as zones and attributes so we can cross-reference the
subgraph nodes back to the original grpah if necessary. First
line is just header "nodeid" than next line is original node id of
node 1 in subgraph, etc.
Paramters:
filename - filename to write to (warning: overwritten)
nodelist - list of nodeids used to order the nodes in the output
Return value:
None.
"""
with open(filename, 'w') as f:
f.write('nodeid\n')
for i in nodelist:
f.write(str(i) + '\n')
#-----------------------------------------------------------------------------
#
# main
#
#-----------------------------------------------------------------------------
def usage(progname):
"""
print usage msg and exit
"""
sys.stderr.write("usage: " + progname + " data_dir num_samples num_seeds num_waves outputdir\n")
sys.exit(1)
def main():
"""
See usage message in module header block
"""
directed = True
try:
opts,args = getopt.getopt(sys.argv[1:], "")
except:
usage(sys.argv[0])
for opt,arg in opts:
usage(sys.argv[0])
if len(args) != 5:
usage(sys.argv[0])
data_dir = args[0]
num_samples = int(args[1])
num_seeds = int(args[2])
num_waves = int(args[3]) - 1 # -1 for consistency with SPNet
outputdir = args[4]
print "directed:", directed
print "number of samples:", num_samples
print "number of seeds:", num_seeds
print "number of waves:", num_waves
print "output directory:", outputdir
if not os.path.exists(outputdir):
os.mkdir(outputdir)
sys.stdout.write('loading data from ' + data_dir + '...')
start = time.time()
(G, profile, colnames) = load_pokec_data(data_dir)
print time.time() - start, 's'
snap.PrintInfo(G)
# We do not add attributes to nodes as SNAP node attribute as
# these seem to get lost by varoius operations including subgraph
# that we need to use, so instead maintain them just in the
# dictionary mapping the original node ids to the attributes -
# fortunately the original node ids are maintained by
# GetSubGraph() so we can used these to index the profile
# dictoinary in the subgraphs
## https://snap.stanford.edu/data/soc-pokec-readme.txt
## region:
## string, mostly regions in Slovakia (example: "zilinsky kraj,
## kysucke nove mesto" means county Zilina, town Kysucke Nove Mesto,
## Slovakia), some foreign countries (example: "zahranicie,
## zahranicie - nemecko" means foreign country Germany (nemecko)),
## some Czech regions (example: "ceska republika, cz - ostravsky
## kraj" means Czech Republic, county Ostrava (ostravsky kraj))
## We just make this a factor, looking at the output written by print
## below, it looks reasonable, but is is only a categorical variable
## allowing us to tell if two users are in the same region or not.
## TODO we could recode this so that we can have different variables
## for being in a different country, major city, etc.
# Cannot do this:
#profile[:][colnames['region']] = convert_to_int_cat(profile[:][colnames['region']]) # like factor in R
# as get "TypeError: unhashable type" so have to do this instead:
id_regions = [(k, p[colnames['region']]) for (k,p) in profile.iteritems()]
id_regions_int = convert_to_int_cat([x[1] for x in id_regions])
for i in xrange(len(id_regions)):
profile[id_regions[i][0]][colnames['region']] = id_regions_int[i]
for attr in ['region']:
sys.stdout.write('There are %d NA for %s\n' % ([p[colnames[attr]] for p in profile.itervalues()].count('NA'), attr))
# get num_samples * num_seeds distinct random seed nodes (sample without replacement)
# and convert to list of lists where each list is seed set for one sample
allseeds = random.sample([node.GetId() for node in G.Nodes()], num_samples * num_seeds)
seedsets = [allseeds[i:i+num_seeds] for i in range(0, len(allseeds), num_seeds)]
sampledesc_filename = outputdir + os.path.sep + "sampledesc" + os.path.extsep + "txt"
sampledesc_f = open(sampledesc_filename, 'w')
for i in range(num_samples):
sys.stdout.write( 'generating snowball sample ' + str(i+1) + '... ' )
start = time.time()
# have to convert seedset to TIntV for SNAP
seedsVec = snap.TIntV()
for nodeid in seedsets[i]:
seedsVec.Add(nodeid)
Gsample = snowball_sample(G, num_waves, seedsVec)
nodelist = list() # keep this iteration in list so we always use same order in future
zonedict = dict() # map nodeid : zone
for node in Gsample.Nodes():
nodelist.append(node.GetId())
zonedict[node.GetId()] = Gsample.GetIntAttrDatN(node.GetId(), "zone")
print time.time() - start, 's'
snap.PrintInfo(Gsample)
subgraph_filename = outputdir + os.path.sep + "subgraph" + str(i) + os.path.extsep + "txt"
write_graph_file(subgraph_filename, Gsample, nodelist)
subzone_filename = outputdir + os.path.sep + "subzone" + str(i) + os.path.extsep + "txt"
write_zone_file(subzone_filename, Gsample, nodelist, zonedict)
subactor_binary_filename = outputdir + os.path.sep + "subactorbin" + str(i) + os.path.extsep + "txt"
subactor_categorical_filename = outputdir + os.path.sep + "subactorcat" + str(i) + os.path.extsep + "txt"
subactor_continuous_filename = outputdir + os.path.sep + "subactorcont" + str(i) + os.path.extsep + "txt"
write_subactors_file_binary(subactor_binary_filename, Gsample, nodelist, profile, colnames)
write_subactors_file_categorical(subactor_categorical_filename, Gsample, nodelist, profile, colnames)
write_subactors_file_continuous(subactor_continuous_filename, Gsample, nodelist, profile, colnames)
nodeid_filename = outputdir + os.path.sep + "subnodeid" + str(i) + os.path.extsep + "txt"
write_subgraph_nodeids(nodeid_filename, nodelist)
# format of sampledesc file is:
# N subzone_filename subgraph_filename binary_Filename cat_filename cont_filename
sampledesc_filename = outputdir + os.path.sep + "sampledesc" + os.path.extsep + "txt"
sampledesc_f.write("%d %s %s %s %s %s\n" % (Gsample.GetNodes(), subzone_filename,
subgraph_filename, subactor_binary_filename,
subactor_categorical_filename, subactor_continuous_filename))
sampledesc_f.close()
if __name__ == "__main__":
main()