This repository has been archived by the owner on Apr 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
152 lines (117 loc) · 4.09 KB
/
test.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
# %%
import numpy as np
import snap
from graph import Graph
from partitionplan import PartitionPlan
import spar
import sys
# import randalloc as spar
# action = [
# 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12,
# 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0, 19, 0, 20
# ]
def generate_action(file_name):
record = []
with open(file_name, 'r') as file:
for line in file.readlines():
line = line.replace('\n', '')
if line != '':
user_ids = line.split(' ')
user1 = int(user_ids[0])
user2 = int(user_ids[1])
record.append((user1, user2))
return record
# generate a random action file for further usage, call this method only when you first run this method
def random_action(file_name):
record = []
with open(file_name, 'r') as file:
for line in file.readlines():
line = line.replace('\n', '')
if line != '':
record.append(line)
rand_id = np.random.choice(len(record), len(record), replace=False)
file_name_random = file_name[0:-4] + str('_rand.txt')
write_back = ''
for it_id in rand_id:
write_back = write_back + record[it_id] + '\n'
with open(file_name_random, 'w+') as file:
file.write(write_back)
# random_action('./data/snap/facebook/facebook_combined/facebook_combined.txt')
# if load pplan, you need also load the graph, call this function when you call pp.load(*args)
def load_graph(graph: Graph, action_file):
for (n1, n2) in generate_action(action_file):
graph.add_node(n1)
graph.add_node(n2)
graph.add_edge(n1, n2)
def partition(save, file):
global pp
con = {}
for (node1, node2) in generate_action(file):
if node1 not in con.keys():
con[node1] = 1
else:
con[node1] += 1
if node2 not in con.keys():
con[node2] = 1
else:
con[node2] += 1
if not pp.contains_user(node1):
pp = spar.add_node(pp, node1)
G.add_node(node1)
if not pp.contains_user(node2):
pp = spar.add_node(pp, node2)
G.add_node(node2)
G.add_edge(node1, node2)
pp = spar.add_edge(pp, node1, node2, G)
pp.save(save)
c = 0
for i in con.keys():
c += con[i]
print(len(con.keys()), c / (len(con.keys())))
def load(load_name, file):
global pp
pp.load(load_name)
load_graph(G, file)
def preprocess_twitter(file_name):
record = {}
index = 0
save = ''
with open(file_name, 'r') as file:
for line in file.readlines():
line = line.replace('\n', '')
if line != '':
user_ids = line.split(' ')
user1 = int(user_ids[0])
user2 = int(user_ids[1])
if user1 not in record.keys():
record[user1] = index
index = index + 1
if user2 not in record.keys():
record[user2] = index
index = index + 1
save = save + str(record[user1]) + ' ' + str(record[user2]) + '\n'
print(index)
file_name_random = file_name[0:-4] + str('_rand.txt')
with open(file_name_random, 'w+') as file:
file.write(save)
if __name__ == '__main__':
# print(sys.argv)
# print(sys.argv)
# pp = PartitionPlan(512, 5000, 512)
#
# pp.partition_ids()
# G1 = snap.TNGraph.New()
# G = Graph(G1)
# preprocess_twitter('./data/snap/twitter/twitter_combined.txt')
# random_action('./data/snap/lasftm_asia/lastfm_asia.txt')
# num = int(sys.argv[1])
num = 1
for server_num in [num]:
pp = PartitionPlan(server_num, 5000, server_num)
pp.partition_ids()
G1 = snap.TUNGraph.New()
G = Graph(G1)
partition('./facebook_new_K0_' + str(server_num), './data/snap/lasftm_asia/lastfm_asia_rand.txt')
# load('./test_server' + str(512))
# partitaion('./test_serverface512_2', './data/snap/facebook/facebook_combined/facebook_combined_rand.txt')
# print(pp.u2p)