Rectify swp2 plot

master
tforest 2023-11-17 17:12:27 +01:00
parent bbc06411e9
commit f79d48bf5b
1 changed files with 16 additions and 22 deletions

26
swp2.py
View File

@ -1,4 +1,3 @@
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import os import os
@ -29,17 +28,17 @@ def return_x_y_from_stwp_theta_file(stwp_theta_file, breaks, mu, tgen):
if dim < breaks+1: if dim < breaks+1:
return 0,0,0 return 0,0,0
# get n, the last bin of the last group # get n, the last bin of the last group
#n = int(groups[-1].split(",")[-1])
# revert the list of groups as the most recent times correspond # revert the list of groups as the most recent times correspond
# to the closest and last leafs of the coal. tree. # to the closest and last leafs of the coal. tree.
groups = groups[::-1] groups = groups[::-1]
theta_site = theta_site[::-1]
# initiate the dict of times # initiate the dict of times
t = {} t = {}
# list of thetas # list of thetas
theta_L = [] theta_L = []
sum_t = 0 sum_t = 0
for group_nb, group in enumerate(groups): for group_nb, group in enumerate(groups):
print(group_nb, group, theta_site[group_nb], len(theta_site)) ###print(group_nb, group, theta_site[group_nb], len(theta_site))
# store all the thetas one by one, with one theta per group # store all the thetas one by one, with one theta per group
theta_L.append(float(theta_site[group_nb])) theta_L.append(float(theta_site[group_nb]))
# if the group is of size 1 # if the group is of size 1
@ -51,37 +50,29 @@ def return_x_y_from_stwp_theta_file(stwp_theta_file, breaks, mu, tgen):
i = int(group.split(",")[0]) i = int(group.split(",")[0])
j = int(group.split(",")[-1]) j = int(group.split(",")[-1])
t[i] = 0 t[i] = 0
#t =
if len(group.split(',')) == 1: if len(group.split(',')) == 1:
k = i k = i
t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
else: else:
for k in range(i, j+1): for k in range(j, i-1, -1 ):
t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
# we add the cumulative times at the end # we add the cumulative times at the end
t[i] += sum_t t[i] += sum_t
sum_t = t[i] sum_t = t[i]
# build the y axis (sizes) # build the y axis (sizes)
y = [] y = []
#theta_L.sort(reverse=True)
for theta in theta_L: for theta in theta_L:
# with size N = theta/4mu # with size N = theta/4mu
size = theta / (4*mu) size = theta / (4*mu)
y.append(size) y.append(size)
y.append(size) y.append(size)
# build the time x axis # build the time x axis
x = [0] x = [0]
# need to fix: t.values are sorted? and in reverse order because the groups and the times are sorted in opposite order
for time in range(0, len(t.values())-1): for time in range(0, len(t.values())-1):
x.append(list(t.values())[time]) x.append(list(t.values())[time])
x.append(list(t.values())[time]) x.append(list(t.values())[time])
x.append(list(t.values())[len(t.values())-1]) x.append(list(t.values())[len(t.values())-1])
#x = x[::-1]
#y = y[::-1]
#x.sort(reverse=True)
#y.sort(reverse=True)
return x,y,likelihood return x,y,likelihood
def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title"): def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title"):
@ -100,7 +91,7 @@ def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title")
cpt +=1 cpt +=1
scenari[likelihood] = x,y scenari[likelihood] = x,y
print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(breaks)+"\n*******\n") print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(breaks)+"\n*******\n")
print(cpt, "theta files have been scanned.") print(cpt, "theta file(s) have been scanned.")
# sort starting by the smallest -log(Likelihood) # sort starting by the smallest -log(Likelihood)
best10_scenari = (sorted(list(scenari.keys())))[:10] best10_scenari = (sorted(list(scenari.keys())))[:10]
print("10 greatest Likelihoods", best10_scenari) print("10 greatest Likelihoods", best10_scenari)
@ -109,9 +100,13 @@ def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title")
my_dpi = 300 my_dpi = 300
plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi) plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
plt.plot(x, y, 'r-', lw=2, label = 'Lik='+greatest_likelihood) plt.plot(x, y, 'r-', lw=2, label = 'Lik='+greatest_likelihood)
plt.yscale('log')
#plt.xscale('log')
plt.grid(True,which="both", linestyle='--')
for scenario in best10_scenari[1:]: for scenario in best10_scenari[1:]:
x,y = scenari[scenario] x,y = scenari[scenario]
print("\n---- Lik:",scenario,"\n\nt=", x,"\n\nN=",y, "\n\n") #print("\n---- Lik:",scenario,"\n\nt=", x,"\n\nN=",y, "\n\n")
plt.plot(x, y, '--', lw=1, label = 'Lik='+scenario) plt.plot(x, y, '--', lw=1, label = 'Lik='+scenario)
plt.ylabel("Individuals (N)") plt.ylabel("Individuals (N)")
plt.xlabel("Time (years)") plt.xlabel("Time (years)")
@ -119,7 +114,6 @@ def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title")
plt.title(title) plt.title(title)
#plt.gcf().set_size(1000, 500) #plt.gcf().set_size(1000, 500)
plt.savefig(title+'_b'+str(breaks)+'.pdf') plt.savefig(title+'_b'+str(breaks)+'.pdf')
#plt.show()
if __name__ == "__main__": if __name__ == "__main__":