-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualise.py
131 lines (106 loc) · 5.85 KB
/
visualise.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
import argparse
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
from labellines import labelLines
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--directory', help='Directory path where simulation results are stored')
arg_parser.add_argument('--mechanism', help='Mechanism name')
def plot_data(data: DataFrame, rewiring_probabilities: list, selection_pressures=None,
n_values=None, mechanism="neutral_change") -> None:
"""
Plot the proportion of innovation A (y-axis) against population size (x-axis)
for each social network type, represented by different rewiring probabilities.
Parameters:
-----------
data: A DataFrame containing experiment results for a specific mechanism.
rewiring_probabilities: A list of rewiring probabilities.
selection_pressures: An optional list of selection pressures.
n_values: An optional list of proportions of leaders.
"""
max_N = data['population_size'].max()
min_N = data['population_size'].min()
fig, axs = plt.subplots(1, len(rewiring_probabilities))
if mechanism == 'interactor_selection':
for i, rewiring_probability in enumerate(rewiring_probabilities):
ax = axs[i]
data_is = data[data['rewiring_probability'] == rewiring_probability]
for j, n_value in enumerate(n_values):
n_data = data_is[data_is['n'] == n_value]
for selection_pressure in selection_pressures:
selection_data = n_data[n_data['selection_pressure'] == selection_pressure]
selection_data.plot(x='population_size', y='final_A', ax=ax)
ax.set_ylabel('L', fontdict={'fontsize': 12, 'fontstyle': 'italic'})
ax.set_xlabel('N', fontdict={'fontsize': 12, 'fontstyle': 'italic'})
ax.set_xlim([min_N, max_N])
ax.set_ylim([0, 1])
if rewiring_probability == 0.00:
ax.set_title('Regular Network', fontsize=12)
elif rewiring_probability == 0.01:
ax.set_title('Small-world Network', fontsize=12)
elif rewiring_probability == 1:
ax.set_title('Random Network', fontsize=12)
else:
ax.set_title(f'Rewiring Probability {rewiring_probability}', fontsize=12)
ax.legend_ = None
elif mechanism == 'replicator_selection':
for ax, rewiring_probability in zip(axs, rewiring_probabilities):
data_rs = data[data['rewiring_probability'] == rewiring_probability]
for selection_pressure in selection_pressures:
selection_data = data_rs[data_rs['selection_pressure'] == selection_pressure]
selection_data.plot(x='population_size', y='final_A', ax=ax, label=str(round(selection_pressure, 1)))
ax.set_ylabel('L', fontdict={'fontsize': 12, 'fontstyle': 'italic'})
ax.set_xlabel('N', fontdict={'fontsize': 12, 'fontstyle': 'italic'})
ax.set_xlim([min_N, max_N])
ax.set_ylim([0, 1])
if rewiring_probability == 0.00:
ax.set_title('Regular Network', fontsize=12)
elif rewiring_probability == 0.01:
ax.set_title('Small-world Network', fontsize=12)
elif rewiring_probability == 1:
ax.set_title('Random Network', fontsize=12)
else:
ax.set_title(f'Rewiring Probability {rewiring_probability}', fontsize=12)
labelLines(ax.get_lines(), zorder=2.5, fontsize=6)
ax.legend_ = None
else:
for ax, rewiring_probability in zip(axs, rewiring_probabilities):
data_ng = data[data['rewiring_probability'] == rewiring_probability]
data_ng.plot(x='population_size', y='final_A', ax=ax)
ax.set_ylabel('L', fontdict={'fontsize': 12, 'fontstyle': 'italic'})
ax.set_xlabel('N', fontdict={'fontsize': 12, 'fontstyle': 'italic'})
ax.set_xlim([min_N, max_N])
ax.set_ylim([0, 1])
if rewiring_probability == 0.00:
ax.set_title('Regular Network', fontsize=12)
elif rewiring_probability == 0.01:
ax.set_title('Small-world Network', fontsize=12)
elif rewiring_probability == 1:
ax.set_title('Random Network', fontsize=12)
else:
ax.set_title(f'Rewiring Probability {rewiring_probability}', fontsize=12)
ax.legend_ = None
plt.tight_layout()
plt.show()
if __name__ == '__main__':
args = arg_parser.parse_args()
directory = args.directory
mech = args.mechanism
df = pd.read_csv(directory)
dat = None
if mech == 'neutral_change':
dat = df.groupby(['rewiring_probability', 'population_size'])['final_A'].mean().reset_index()
plot_data(mechanism=mech, data=dat, rewiring_probabilities=df['rewiring_probability'].unique())
if mech == 'replicator_selection':
dat = df.groupby(['rewiring_probability', 'selection_pressure', 'population_size'])[
'final_A'].mean().reset_index()
plot_data(mechanism=mech, data=dat,
rewiring_probabilities=df['rewiring_probability'].unique(),
selection_pressures=df['selection_pressure'].unique())
if mech == 'interactor_selection':
dat = df.groupby(['rewiring_probability', 'selection_pressure', 'n', 'population_size'])[
'final_A'].mean().reset_index()
plot_data(mechanism=mech, data=dat,
rewiring_probabilities=df['rewiring_probability'].unique(),
selection_pressures=df['selection_pressure'].unique(),
n_values=df['n'].unique())