-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization.py
127 lines (85 loc) · 3.49 KB
/
visualization.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
# imports
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import json
import os
from utils import phi
# variables
# functions
def recover_data(type: str, results_dir: str, job_number: int):
"""
inputs:
-> type: str ('WM', 'WM_mat', 'Star', 'Cycle', 'Clique')
-> results_dir: str (results directory)
-> job_number: int
output:
-> data: dict
recovers the data file "type_[job_number]_*.json and returns it as a dictionary
"""
datafile = None
beginning = type + '_' + str(job_number) + '_'
for filename in os.listdir(results_dir):
root, ext = os.path.splitext(filename)
if root.startswith(beginning) and ext == '.json':
datafile = filename
with open(results_dir + datafile, 'r') as file:
data = json.load(file)
return data
def compare_WM_mat(job_number: int):
results_dir = 'results/compare_sim-mat/'
mat_data = recover_data('WM_mat',results_dir, job_number)
sim_data = recover_data('WM',results_dir, job_number)
assert mat_data['s_range'] == sim_data['s_range']
s = np.array(mat_data['s_range'])
y1 = np.array(mat_data['fixation_probability'])
nb_trajectories = sim_data['parameters']['nb_trajectories']
y2 = np.array(sim_data['nb_fixations'], dtype=float)/nb_trajectories
y2_err = 2*np.sqrt(y2 * (1. - y2) / nb_trajectories) # 2*standard deviation
N, M = sim_data['parameters']['N'], sim_data['parameters']['M']
assert N == mat_data['parameters']['N'] and M == mat_data['parameters']['M']
y3 = np.array([phi(N,s_value, M/N, 1/N) for s_value in s])
fig, ax = plt.subplots()
ax.errorbar(s, y2, yerr= y2_err, fmt = 'o', alpha=0.5, label = 'Simulations')
ax.scatter(s, y1, marker='x', alpha=1, label = 'Matrix inversions')
ax.plot(s, y3, alpha=0.8, label = 'Diffusion approximation')
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel('Relative fitness')
ax.set_ylabel('Fixation probability')
ax.legend()
plt.show()
def WM_paper(min_job_number: int, max_job_number: int):
results_dir = 'results/'
n_jobs = 1 + max_job_number - min_job_number
first_data = recover_data('WM', results_dir, min_job_number)
s_range = np.array(first_data['s_range'])
nb_fixations = np.zeros((n_jobs, len(s_range)))
Ms = np.zeros(n_jobs)
Ms[0] = first_data['parameters']['M']
N= first_data['parameters']['N']
nb_trajectories = first_data['parameters']['nb_trajectories']
for i in range(min_job_number+1, max_job_number + 1):
data_i = recover_data('WM', results_dir, i)
Ms[i - min_job_number] = data_i['parameters']['M']
nb_fixations[i-min_job_number,:] = np.array(data_i['nb_fixations'])
cmap = mpl.colormaps['plasma']
colors = cmap(np.linspace(0, 1, len(Ms)))
fig, ax = plt.subplots()
for i,M in enumerate(Ms):
color = colors[i]
y = nb_fixations[i,:] / nb_trajectories
y_err = np.sqrt(y * (1-y)/nb_trajectories)
y_th = np.array([phi(N,s,M/N, 1/N) for s in s_range])
ax.errorbar(s_range, y, yerr= y_err, fmt = 'o', alpha=0.5, color=color)
ax.plot(s_range, y_th, label = f"M={round(M)} (update fraction: {round(M/N,2)} )", color= color)
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel('Relative fitness')
ax.set_ylabel('Fixation probability')
ax.legend()
plt.show()
if __name__ == '__main__':
#compare_WM_mat(1)
WM_paper(1, 5)