|
@@ -3,13 +3,11 @@ import os
|
3
|
3
|
import numpy as np
|
4
|
4
|
import math
|
5
|
5
|
import json
|
6
|
|
-import io
|
7
|
|
-from scipy.special import gammaln
|
8
|
|
-from matplotlib.backends.backend_pdf import PdfPages
|
9
|
|
-from matplotlib.ticker import MaxNLocator
|
10
|
|
-from mpl_toolkits.axes_grid1.inset_locator import inset_axes
|
11
|
|
-from matplotlib.ticker import MultipleLocator
|
|
6
|
+
|
12
|
7
|
def log_facto(k):
|
|
8
|
+ """
|
|
9
|
+ Using the Stirling's approximation
|
|
10
|
+ """
|
13
|
11
|
k = int(k)
|
14
|
12
|
if k > 1e6:
|
15
|
13
|
return k * np.log(k) - k + np.log(2*math.pi*k)/2
|
|
@@ -18,14 +16,6 @@ def log_facto(k):
|
18
|
16
|
val += np.log(i)
|
19
|
17
|
return val
|
20
|
18
|
|
21
|
|
-def log_facto_1(k):
|
22
|
|
- startf = 1 # start of factorial sequence
|
23
|
|
- stopf = int(k+1) # end of of factorial sequence
|
24
|
|
-
|
25
|
|
- q = gammaln(range(startf+1, stopf+1)) # n! = G(n+1)
|
26
|
|
-
|
27
|
|
- return q[-1]
|
28
|
|
-
|
29
|
19
|
def return_x_y_from_stwp_theta_file(stwp_theta_file, breaks, mu, tgen, relative_theta_scale = False):
|
30
|
20
|
with open(stwp_theta_file, "r") as swp_file:
|
31
|
21
|
# Read the first line
|
|
@@ -128,51 +118,6 @@ def return_x_y_from_stwp_theta_file(stwp_theta_file, breaks, mu, tgen, relative_
|
128
|
118
|
# # x[i] = x[i]/N0
|
129
|
119
|
return x,y,likelihood,thetas,sfs,L
|
130
|
120
|
|
131
|
|
-def return_x_y_from_stwp_theta_file_as_is(stwp_theta_file, breaks, mu, tgen, relative_theta_scale = False):
|
132
|
|
- with open(stwp_theta_file, "r") as swp_file:
|
133
|
|
- # Read the first line
|
134
|
|
- line = swp_file.readline()
|
135
|
|
- L = float(line.split()[2])
|
136
|
|
- rands = swp_file.readline()
|
137
|
|
- line = swp_file.readline()
|
138
|
|
- # skip empty lines before SFS
|
139
|
|
- while line == "\n":
|
140
|
|
- line = swp_file.readline()
|
141
|
|
- sfs = np.array(line.split()).astype(float)
|
142
|
|
- # Process lines until the end of the file
|
143
|
|
- while line:
|
144
|
|
- # check at each line
|
145
|
|
- if line.startswith("dim") :
|
146
|
|
- dim = int(line.split()[1])
|
147
|
|
- if dim == breaks+1:
|
148
|
|
- likelihood = line.split()[5]
|
149
|
|
- groups = line.split()[6:6+dim]
|
150
|
|
- theta_site = line.split()[6+dim:6+dim+1+dim]
|
151
|
|
- elif dim < breaks+1:
|
152
|
|
- line = swp_file.readline()
|
153
|
|
- continue
|
154
|
|
- elif dim > breaks+1:
|
155
|
|
- break
|
156
|
|
- #return 0,0,0
|
157
|
|
- # Read the next line
|
158
|
|
- line = swp_file.readline()
|
159
|
|
- #### END of parsing
|
160
|
|
- # quit this file if the number of dimensions is incorrect
|
161
|
|
- if dim < breaks+1:
|
162
|
|
- return 0,0
|
163
|
|
- # get n, the last bin of the last group
|
164
|
|
- # revert the list of groups as the most recent times correspond
|
165
|
|
- # to the closest and last leafs of the coal. tree.
|
166
|
|
- groups = groups[::-1]
|
167
|
|
- theta_site = theta_site[::-1]
|
168
|
|
-
|
169
|
|
- thetas = {}
|
170
|
|
-
|
171
|
|
- for i in range(len(groups)):
|
172
|
|
- groups[i] = groups[i].split(',')
|
173
|
|
- # print(groups[i], len(groups[i]))
|
174
|
|
- thetas[i] = [float(theta_site[i]), groups[i], likelihood]
|
175
|
|
- return thetas, sfs
|
176
|
121
|
|
177
|
122
|
def plot_k_epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title", theta_scale = True):
|
178
|
123
|
scenari = {}
|
|
@@ -180,7 +125,7 @@ def plot_k_epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title"
|
180
|
125
|
for file_name in os.listdir(folder_path):
|
181
|
126
|
if os.path.isfile(os.path.join(folder_path, file_name)):
|
182
|
127
|
# Perform actions on each file
|
183
|
|
- x,y,likelihood,sfs,L = return_x_y_from_stwp_theta_file(folder_path+file_name, breaks = breaks,
|
|
128
|
+ x, y, likelihood, theta, sfs, L = return_x_y_from_stwp_theta_file(folder_path+file_name, breaks = breaks,
|
184
|
129
|
tgen = tgen,
|
185
|
130
|
mu = mu, relative_theta_scale = theta_scale)
|
186
|
131
|
if x == 0 or y == 0:
|
|
@@ -396,7 +341,7 @@ def save_k_theta(folder_path, mu, tgen, title = "Title", theta_scale = True,
|
396
|
341
|
cpt +=1
|
397
|
342
|
if os.path.isfile(os.path.join(folder_path, file_name)):
|
398
|
343
|
for k in range(breaks_max):
|
399
|
|
- thetas,sfs = return_x_y_from_stwp_theta_file_as_is(folder_path+file_name, breaks = k,
|
|
344
|
+ x,y,likelihood,thetas,sfs,L = return_x_y_from_stwp_theta_file(folder_path+file_name, breaks = k,
|
400
|
345
|
tgen = tgen,
|
401
|
346
|
mu = mu, relative_theta_scale = theta_scale)
|
402
|
347
|
if thetas == 0:
|
|
@@ -591,7 +536,7 @@ def plot_test_theta(folder_path, mu, tgen, title = "Title", theta_scale = True,
|
591
|
536
|
cpt +=1
|
592
|
537
|
if os.path.isfile(os.path.join(folder_path, file_name)):
|
593
|
538
|
for k in range(breaks_max):
|
594
|
|
- thetas,sfs = return_x_y_from_stwp_theta_file_as_is(folder_path+file_name, breaks = k,
|
|
539
|
+ x, y, likelihood, theta, sfs, L = return_x_y_from_stwp_theta_file(folder_path+file_name, breaks = k,
|
595
|
540
|
tgen = tgen,
|
596
|
541
|
mu = mu, relative_theta_scale = theta_scale)
|
597
|
542
|
if thetas == 0:
|
|
@@ -751,7 +696,7 @@ def combined_plot(folder_path, mu, tgen, breaks, title = "Title", theta_scale =
|
751
|
696
|
# # plot_all_epochs_thetafolder(folder_path, mu, tgen, title, theta_scale, ax = None)
|
752
|
697
|
# # plot_test_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = breaks, ax = None)
|
753
|
698
|
# # plt.clf()
|
754
|
|
- # save_k_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = breaks, output = title+"_plotdata.json")
|
|
699
|
+ save_k_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = breaks, output = title+"_plotdata.json")
|
755
|
700
|
with open(title+"_plotdata.json", 'r') as json_file:
|
756
|
701
|
loaded_data = json.load(json_file)
|
757
|
702
|
# plot page 1 of summary
|