-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliques_test.py
135 lines (109 loc) · 3.76 KB
/
cliques_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
"""Pipeline for building the triangular cliques graph."""
import main_pipelines as mp
import networkx as nx
import matplotlib.pyplot as plt
import pickle
from random import seed
from random import random
from scipy.stats import bernoulli
import build_generic_network as bgn
import community as community_louvain
import matplotlib.cm as cm
INPUT_PICKLED_GRAPH = "output_files/exp_small-alpha-randcl/randcl_pickle"
def main():
G = random_cliques_graph()
print("Length of G: {}".format(len(G)))
produce_edgelist(G, "randcl")
return
def cliques_graph():
# Construct:
G = nx.Graph()
for i in range(21):
G.add_node(i)
for i in range(6):
for j in range(i + 1, 7):
G.add_edge(i, j)
for i in range(7, 13):
for j in range(i + 1, 14):
G.add_edge(i, j)
for i in range(14, 20):
for j in range(i + 1, 21):
G.add_edge(i, j)
G.add_edge(6, 13)
G.add_edge(13, 20)
# Display:
fig = plt.figure()
nx.draw_spring(G, with_labels=True)
plt.show()
plt.close()
# Pickle:
with open("output_files/cliques_pickle", 'wb') as pickle_file:
pickle.dump(G, pickle_file)
return G
def random_cliques_graph():
# Construct:
G = nx.Graph()
for i in range(300):
G.add_node(i)
intra_random_states = [0, 1, 2]
for k in range(len(intra_random_states)):
base = k * 100
trials = bernoulli.rvs(0.5, size=4950, random_state=intra_random_states[k])
index = 0
for i in range(base, base + 99):
for j in range(i + 1, base + 100):
if trials[index]:
G.add_edge(i, j)
index += 1
num_of_intra_edges = len(G.edges)
print("Expectation(|intra-community edges|) = ~7425:", num_of_intra_edges)
inter_random_states = [3, 4, 5]
for k in range(len(inter_random_states) - 1):
for m in range(k + 1, len(inter_random_states)):
source_base = k * 100
target_base = m * 100
trials = bernoulli.rvs(0.005, size=10000, random_state=inter_random_states[k])
index = 0
for i in range(source_base, source_base + 100):
for j in range(target_base, target_base + 100):
if trials[index]:
G.add_edge(i, j)
index += 1
num_of_inter_edges = len(G.edges) - num_of_intra_edges
print("Expectation(|inter-community edges|) = ~150:", num_of_inter_edges)
# Take the largest connected component:
G = bgn.largest_connected_component_transform(G)
# Display:
fig = plt.figure()
nx.draw_spring(G, with_labels=True)
plt.show()
plt.close()
# Pickle:
with open("output_files/randcl_pickle", 'wb') as pickle_file:
pickle.dump(G, pickle_file)
return G
def produce_edgelist(G, keyword):
with open("output_files/{}_edgelist.txt".format(keyword), 'w') as txt_file:
num_of_nodes = len(G.nodes)
directed = 0
txt_file.write("{}\t{}\n".format(num_of_nodes, directed))
for edge in G.edges:
txt_file.write("{}\t{}\n".format(edge[0], edge[1]))
return
def equality_test():
output = []
with open("output_files/exp_small-alpha-randcl/randcl_K3_composition_map.csv", mode='r') as file:
next(file)
for row in file:
row = row.split(",")
new_row = [row[0]]
print(row[0], row[1])
# error = 0
# cluster_nodes = {i - 1: set(row[i]) for i in range(1, 4)}
# print(cluster_nodes)
# for i in range(1, 4):
# for node in range((i-1) * 100, i * 100)):
# if node not in cluster_nodes[i-1]:
# error
if __name__ == "__main__":
main()