-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy3.py
More file actions
151 lines (130 loc) · 5.95 KB
/
Copy pathstudy3.py
File metadata and controls
151 lines (130 loc) · 5.95 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
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
import community
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import re
import os
def community_size(path):
T = []
size = []
for d in range(0, 10):
T_d = []
size_d = []
for dir in os.listdir(path + ('/run_%d/partition' % d)):
m = re.match(r"([0-9]+)\.dat", dir)
if m != None:
ma = np.loadtxt(path + ('/run_%d/matrix/' % d) + dir, delimiter=' ')
G = nx.Graph()
for [a1, b1, w] in ma:
if w > 0:
G.add_edge(a1, b1, weight = w)
partition = np.loadtxt(path + ('/run_%d/partition/' % d) + dir, delimiter=' ')
size_d.append(len(np.unique(partition)))
T_d.append(int(m.groups(0)[0]))
T.append(T_d)
size.append(size_d)
T = np.mean(T, axis = 0)
size = np.mean(size, axis = 0)
[T, size] = list(zip(*sorted(zip(T, size))))
fig, ax = plt.subplots()
ax.set_xscale('log')
# ax.set_ylim([-0.1, 1])
ax.set_ylabel('Community Size')
ax.set_xlabel('Games per player')
ax.plot(np.array(T) / 100, np.array(size))
# axes[i ,j].plot(np.array(T) / 100, np.array(size) / np.max(size))
# fig.set_size_inches(2, 2)
fig.tight_layout()
plt.show()
def modularity(path):
a = [0.15, 0.3, 0.45]
b = [0.15, 0.3, 0.45]
# plt.setp(axes.flat, xlabel='Games per player', ylabel='Modularity')
# pad = 5
# for ax, col in zip(axes[0], ['lambda2: {}'.format(col) for col in b]):
# ax.annotate(col, xy=(0.5, 1), xytext=(0, pad),
# xycoords='axes fraction', textcoords='offset points',
# size='large', ha='center', va='baseline')
# for ax, row in zip(axes[:,0], ['lambda1: {}'.format(row) for row in a]):
# ax.annotate(row, xy=(0, 0.5), xytext=(-ax.yaxis.labelpad - pad, 0),
# xycoords=ax.yaxis.label, textcoords='offset points',
# size='large', ha='right', va='center')
for dir0 in os.listdir('study3/' + path):
m = re.match(r"l1=([0-9\.]+)l2=([0-9\.]+)", dir0)
if m != None:
l1 = float(m.groups()[0])
l2 = float(m.groups()[1])
if l1 in a and l2 in b:
i = int(a.index(float(m.groups()[0])))
j = int(b.index(float(m.groups()[1])))
T = []
modularity = []
# size = []
# size_std = []
for d in range(0, 10):
T_d = []
modularity_d = []
for dir in os.listdir('study3/' + path + '/' + dir0 + ('/run_%d/partition' % d)):
m = re.match(r"([0-9]+)\.dat", dir)
if m != None:
ma = np.loadtxt('study3/' + path + '/' + dir0 + ('/run_%d/matrix/' % d) + dir, delimiter=' ')
G = nx.Graph()
for [a1, b1, w] in ma:
if w > 0:
G.add_edge(a1, b1, weight = w)
partition = np.loadtxt('study3/' + path + '/' + dir0 + ('/run_%d/partition/' % d) + dir, delimiter=' ')
partition = {k :int(partition[k]) for k in range(len(partition))}
modularity_d.append((community.modularity(partition, G)))
T_d.append(int(m.groups(0)[0]))
# print partition.values()
# size_std.append(np.sqrt(np.var([len([k for k in range(100) if partition[k] == c]) for c in np.unique(partition.values())])))
# print 100.0 / (np.max(partition.values()) + 1)
# size.append(100.0 / (np.max(partition.values()) + 1))
# print(np.unique([int(k) for k in partition.keys()]))
# print(len(np.unique([int(k) for k in partition.keys()])))
# count.append(len(np.unique([int(k) for k in partition.keys()])))
T.append(T_d)
modularity.append(modularity_d)
T = np.mean(T, axis = 0)
modularity = np.mean(modularity, axis = 0)
[T, modularity] = list(zip(*sorted(zip(T, modularity))))
fig, ax = plt.subplots()
ax.set_xscale('log')
ax.set_ylim([-0.1, 1])
ax.set_ylabel('Modularity')
ax.set_xlabel('Games per player')
ax.plot(np.array(T) / 100, np.array(modularity))
# axes[i ,j].plot(np.array(T) / 100, np.array(size) / np.max(size))
fig.set_size_inches(2, 2)
fig.tight_layout()
fig.savefig('modularity%d%d.png' % (i, j))
def build_partition(path):
a = [0.15, 0.3, 0.45]
b = [0.15, 0.3, 0.45]
for dir0 in os.listdir('study3/' + path):
m = re.match(r"l1=([0-9\.]+)l2=([0-9\.]+)", dir0)
if m != None:
l1 = float(m.groups()[0])
l2 = float(m.groups()[1])
if l1 in a and l2 in b:
i = int(a.index(float(m.groups()[0])))
j = int(b.index(float(m.groups()[1])))
for d in range(0, 10):
T_d = []
modularity_d = []
for dir in os.listdir('study3/' + path '/' dir0 + ('/run_%d/matrix' % d)):
m = re.match(r"([0-9]+)\.dat", dir)
if m != None:
ma = np.loadtxt('study3/' + path + '/' + dir0 + ('/run_%d/matrix/' % d) + dir, delimiter=' ')
G = nx.Graph()
for [a1, b1, w] in ma:
if w > 0:
G.add_edge(a1, b1, weight = w)
partition = community.best_partition(G)
if not os.path.exists('study3/' + path + '/' + dir0 + ('/run_%d/partition/' % d)):
os.makedirs('study3/' + path + '/' + dir0 + ('/run_%d/partition/' % d))
np.savetxt('study3/' + path + '/' + dir0 + ('/run_%d/partition/' % d) + dir, partition.values(), delimiter=' ')
build_partition('null_model')
build_partition('live_model')
community_size('study3/null_model/l1=0.300000l2=0.300000')
community_size('study3/live_model/l1=0.300000l2=0.300000')