-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_snap1.py
212 lines (184 loc) · 7.13 KB
/
write_snap1.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
# 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 snap_write import *
def label_by_weighted_voting (u, ep, test_labeled, threshold):
gay_labeled_gay = 0
gay_labeled_straight = 0
gay_unlabeled = 0
straight_labeled_straight = 0
straight_labeled_gay = 0
straight_unlabeled = 0
straight_count = 0
gay_count = 0
#################
#
# Initialize priorities
#
unlabeled = pqdict.PQDict()
for ego in u:
if not "orientation" in u.node[ego]:
u.node[ego]["total_weight"] = 0
u.node[ego]["gay_weight"] = 0
u.node[ego]["straight_weight"] = 0
count = 0
total_weight = 0
for alter in u.neighbors(ego):
if "orientation" in u.node[alter]:
if u.node[alter]["orientation"] == 1:
u.node[ego]["gay_weight"] += u[ego][alter]["embeddedness"]**ep
if u.node[alter]["orientation"] == -1:
u.node[ego]["straight_weight"] += u[ego][alter]["embeddedness"]**ep
#total_weight += u[ego][alter]["embeddedness"]**ep
u.node[ego]["total_weight"] += u[ego][alter]["embeddedness"]**ep
if u.node[ego]["gay_weight"] > u.node[ego]["straight_weight"]:
priority = -float(u.node[ego]["gay_weight"])/float(u.node[ego]["total_weight"])
else:
priority = -float(u.node[ego]["straight_weight"])/float(u.node[ego]["total_weight"])
unlabeled[ego] = priority
#################
#
# Create labels
#
logfile = file("labeling_log.txt", "w")
while len(unlabeled) > 0:
(ego, score) = unlabeled.popitem()
if score == 0:
break
gay_alters = 0.0
straight_alters = 0.0
gay_list = list()
straight_list = list()
if float(u.node[ego]["gay_weight"] - u.node[ego]["straight_weight"])/float(u.node[ego]["total_weight"]) > threshold:
u.node[ego]["orientation"] = 1
logfile.write ("GAY\t\t");
gay_count += 1
else:
u.node[ego]["orientation"] = -1
logfile.write ("STRAIGHT\t");
straight_count += 1
if ego in test_labeled:
if u.node[ego]["orientation"] == 1:
if u.node[ego]["test_orientation"] == 1:
gay_labeled_gay += 1
else:
straight_labeled_gay += 1
else:
if u.node[ego]["test_orientation"] == 1:
gay_labeled_straight += 1
else:
straight_labeled_straight += 1
logfile.write ("%d: degree: %d, priority: %f, gay: %s; straight %s\n" % (ego, u.degree(ego), score, str(gay_list), str(straight_list)))
#print "%d %d %f %d %d %d %d %d %d" % (ego, u.degree(ego) , u.node[ego]["total_weight"], gay_count, straight_count, gay_labeled_gay, gay_labeled_straight, straight_labeled_gay, straight_labeled_straight)
#priority = -float(count)/float(len(u.neighbors(ego)))
for alter in u.neighbors(ego):
if not "orientation" in u.node[alter]:
if u.node[ego]["orientation"] == 1:
u.node[alter]["gay_weight"] += u[ego][alter]["embeddedness"]**ep
elif u.node[ego]["orientation"] == -1:
u.node[alter]["straight_weight"] += u[ego][alter]["embeddedness"]**ep
if u.node[alter]["gay_weight"] > u.node[alter]["straight_weight"]:
priority = -float(u.node[alter]["gay_weight"])/float(u.node[alter]["total_weight"])
else:
priority = -float(u.node[alter]["straight_weight"])/float(u.node[alter]["total_weight"])
unlabeled[alter] = priority
logfile.close()
return u
def main():
missing = file ("missing.txt", "w")
global di
global u
global v
global gays
global straights
global gay_cliques
global straight_cliques
global B
graph_file = file (sys.argv[3])
u = pickle.load(graph_file)
graph_file.close()
u = set_orientation_by_file (sys.argv[1], "orientation", u)
u = set_orientation_by_file (sys.argv[2], "test_orientation", u)
'''
B = nx.make_clique_bipartite(u)
for node in gays:
gay_cliques = gay_cliques | set (B.neighbors(node))
for node in straights:
straight_cliques = straight_cliques | set (B.neighbors(node))
'''
##################################
#
# Invent new node labels that snap will understand
# i.e., from 0 to n-1
# put the labeled training nodes first, followed by test nodes,
# followed by the rest
#
# Note that u is not actually relabeled. The labels are applied
# when the output file is written.
#
count = 0
not_labeled = list()
labeled = list()
test_labeled = list()
test_labels = list()
labeled_data_for_snap = file("snap_labels.txt", "w")
node_untrans = dict()
for ego in u:
if "orientation" in u.node[ego]:
node_trans[ego] = count
node_untrans[count] = ego
labeled_data_for_snap.write("%d\n" % u.node[ego]["orientation"])
labeled.append(u.node[ego]["orientation"])
count += 1
elif "test_orientation" in u.node[ego]:
test_labeled.append(ego)
test_labels.append(u.node[ego]["test_orientation"])
else:
not_labeled.append(ego)
labeled_data_for_snap.close()
for ego in test_labeled:
node_trans[ego] = count
node_untrans[count] = ego
count +=1
for ego in not_labeled:
node_trans[ego] = count
node_untrans[count] = ego
count +=1
######################
#
# Output graph with labels and node translations
# These are intermediate outputs that may later be
# helpful in interpreting the data
#
pkl_file = open(sys.argv[6], "w")
pickle.dump(u, pkl_file)
pkl_file.close()
pkl_file = open(sys.argv[7], "w")
pickle.dump(node_trans, pkl_file)
pkl_file.close()
#u = label_by_voting (u)
#u = label_by_weighted_voting (u, float(sys.argv[5]))
#u = label_by_weighted_voting2 (u, float(sys.argv[5]), test_labeled)
#u = label_by_weighted_voting (u, float(sys.argv[5]), test_labeled)
#dump_tests (u, test_labeled)
#u = label_by_revoting (u, float(sys.argv[5]), test_labeled)
#dump_tests (u, test_labeled)
for x in range(-10, 11):
threshold = float(x)/10.0
v = u.copy()
v = label_by_weighted_voting (v, float(sys.argv[5]), test_labeled, threshold)
sys.stdout.write ("%f " % threshold)
dump_tests (v, test_labeled)
u = label_by_weighted_voting (u, float(sys.argv[5]), test_labeled, 0)
write_to_snap (sys.argv[4], u, node_trans, node_untrans, labeled, test_labels, float(sys.argv[5]))
if __name__ == "__main__":
main()