-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_bip_str.py
More file actions
109 lines (85 loc) · 2.2 KB
/
convert_bip_str.py
File metadata and controls
109 lines (85 loc) · 2.2 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
import sys
filename_bip = sys.argv[1]
top_nodes = {}
str_top_nodes = {}
str_bot_nodes = {}
louvain = False
bisbm = False
infomap = True
if bisbm:
index = 1
index_bot = 0
else:
index = 0
index_bot = 0
if louvain or infomap:
with open(filename_bip, 'r') as file:
for line in file:
line = line.replace("\n", "").split()
top = line.pop(0)
index_top = index
top_nodes[index_top] = []
str_top_nodes[top] = index_top
for bot in line:
if bot not in str_bot_nodes:
if louvain:
index += 1
str_bot_nodes[bot] = index
elif infomap:
str_bot_nodes[bot] = index_bot
index_bot += 1
top_nodes[index_top].append(str_bot_nodes[bot])
index += 1
elif bisbm:
with open(filename_bip, 'r') as file:
for line in file:
line = line.replace("\n", "").split()
top_nodes[index] = []
top = line[0]
str_top_nodes[top] = index
index += 1
index_bot = index
with open(filename_bip, 'r') as file:
for line in file:
line = line.replace("\n", "").split()
top = line.pop(0)
index_top = str_top_nodes[top]
for bot in line:
if bot not in str_bot_nodes:
str_bot_nodes[bot] = index_bot
index_bot += 1
top_nodes[index_top].append(str_bot_nodes[bot])
f = open(filename_bip + '_num', 'w')
for top in top_nodes:
f.write(str(top))
for bot in top_nodes[top]:
f.write(' ' + str(bot))
f.write('\n')
f.close()
if louvain or bisbm:
f = open(filename_bip + '_num.adjency', 'w')
for top in str_top_nodes:
index_top = str_top_nodes[top]
for index_bot in top_nodes[index_top]:
f.write(str(index_top) + " " + str(index_bot) + "\n")
f.close()
elif infomap:
f = open(filename_bip + '_num.adjency', 'w')
for i in range(0, len(top_nodes)):
for bot in top_nodes[i]:
f.write("f" + str(i) + ' ' + "n" + str(bot) + '\n')
f.close()
if bisbm:
# type file for biSBM algo)
f = open(filename_bip + '_num.vertexType', 'w')
for i in range(0, index_top):
f.write("1\n")
for i in range(0, index_bot):
f.write("2\n")
f.close()
f = open(filename_bip + '_table_num', 'w')
for top in str_top_nodes:
f.write(top + " " + str(str_top_nodes[top]) + '\n')
"""for bot in str_bot_nodes:
f.write(bot + " " + str(str_bot_nodes[bot]) + '\n')"""
f.close()