-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_graph.py
64 lines (46 loc) · 1.33 KB
/
label_graph.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
# Prepares data for input to snap-base mhrw system
# See makefile for default arguments
# usage:
#python write_snap.py [labeled_users] [all_users] [test_users] [graph_data] [snap_output]
#
import sys
import re
import math
import json
import networkx as nx
import pickle
import pqdict
from build_graph import *
def set_orientation_by_file (filename, field_name, u):
test_data = file(filename)
while True:
line = test_data.readline()
if line == "":
break
orientation, description, key, profile_image_url, profile_banner_url, profile_background_image_url = line.split("\t")
ego = int(key)
if ego in u:
if orientation == "Straight":
u.node[ego][field_name] = -1
if orientation == "Gay":
u.node[ego][field_name] = 1
test_data.close()
return u
###################
#
# Global variables
#
u = nx.Graph()
def main():
global u
graph_file = file (sys.argv[1])
u = pickle.load(graph_file)
graph_file.close()
u = set_orientation_by_file (sys.argv[3], "orientation", u)
if len(sys.argv) > 4:
u = set_orientation_by_file (sys.argv[4], "test_orientation", u)
pkl_file = open(sys.argv[2], "w")
pickle.dump(u, pkl_file)
pkl_file.close()
if __name__ == "__main__":
main()