Browse Source

Rectify swp2 plot

tforest 1 year ago
parent
commit
f79d48bf5b
1 changed files with 16 additions and 22 deletions
  1. 16 22
      swp2.py

+ 16 - 22
swp2.py View File

1
-
2
 import matplotlib.pyplot as plt
1
 import matplotlib.pyplot as plt
3
 import os
2
 import os
4
 
3
 
29
     if dim < breaks+1:
28
     if dim < breaks+1:
30
         return 0,0,0
29
         return 0,0,0
31
     # get n, the last bin of the last group
30
     # get n, the last bin of the last group
32
-    #n = int(groups[-1].split(",")[-1])
33
     # revert the list of groups as the most recent times correspond
31
     # revert the list of groups as the most recent times correspond
34
     # to the closest and last leafs of the coal. tree.
32
     # to the closest and last leafs of the coal. tree.
35
     groups = groups[::-1]
33
     groups = groups[::-1]
34
+    theta_site = theta_site[::-1]
36
     # initiate the dict of times
35
     # initiate the dict of times
37
     t = {}
36
     t = {}
38
-    # list of thetas 
37
+    # list of thetas
39
     theta_L = []
38
     theta_L = []
40
     sum_t = 0
39
     sum_t = 0
41
     for group_nb, group in enumerate(groups):
40
     for group_nb, group in enumerate(groups):
42
-        print(group_nb, group, theta_site[group_nb], len(theta_site))
41
+        ###print(group_nb, group, theta_site[group_nb], len(theta_site))
43
         # store all the thetas one by one, with one theta per group
42
         # store all the thetas one by one, with one theta per group
44
         theta_L.append(float(theta_site[group_nb]))
43
         theta_L.append(float(theta_site[group_nb]))
45
         # if the group is of size 1
44
         # if the group is of size 1
51
             i = int(group.split(",")[0])
50
             i = int(group.split(",")[0])
52
             j = int(group.split(",")[-1])
51
             j = int(group.split(",")[-1])
53
         t[i] = 0
52
         t[i] = 0
53
+        #t =
54
         if len(group.split(',')) == 1:
54
         if len(group.split(',')) == 1:
55
             k = i
55
             k = i
56
             t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
56
             t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
57
         else:
57
         else:
58
-            for k in range(i, j+1):
58
+            for k in range(j, i-1, -1 ):
59
                 t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
59
                 t[i] += ((theta_L[group_nb] ) / (k*(k-1)) * tgen) / mu
60
         # we add the cumulative times at the end
60
         # we add the cumulative times at the end
61
         t[i] += sum_t
61
         t[i] += sum_t
62
         sum_t = t[i]
62
         sum_t = t[i]
63
     # build the y axis (sizes)
63
     # build the y axis (sizes)
64
     y = []
64
     y = []
65
-    #theta_L.sort(reverse=True)
66
     for theta in theta_L:
65
     for theta in theta_L:
67
         # with size N = theta/4mu
66
         # with size N = theta/4mu
68
         size = theta / (4*mu)
67
         size = theta / (4*mu)
69
         y.append(size)
68
         y.append(size)
70
         y.append(size)
69
         y.append(size)
71
-    
72
     # build the time x axis
70
     # build the time x axis
73
     x = [0]
71
     x = [0]
74
-    # need to fix: t.values are sorted? and in reverse order because the groups and the times are sorted in opposite order
75
     for time in range(0, len(t.values())-1):
72
     for time in range(0, len(t.values())-1):
76
         x.append(list(t.values())[time])
73
         x.append(list(t.values())[time])
77
         x.append(list(t.values())[time])
74
         x.append(list(t.values())[time])
78
     x.append(list(t.values())[len(t.values())-1])
75
     x.append(list(t.values())[len(t.values())-1])
79
-
80
-    #x = x[::-1]
81
-    #y = y[::-1]
82
-    #x.sort(reverse=True)
83
-    #y.sort(reverse=True)
84
-    
85
     return x,y,likelihood
76
     return x,y,likelihood
86
 
77
 
87
 def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title"):
78
 def plot_3epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title"):
88
-    
79
+
89
     scenari = {}
80
     scenari = {}
90
     cpt = 0
81
     cpt = 0
91
-    
82
+
92
     for file_name in os.listdir(folder_path):
83
     for file_name in os.listdir(folder_path):
93
         if os.path.isfile(os.path.join(folder_path, file_name)):
84
         if os.path.isfile(os.path.join(folder_path, file_name)):
94
             # Perform actions on each file
85
             # Perform actions on each file
95
-            x,y,likelihood = return_x_y_from_stwp_theta_file(folder_path+file_name, breaks = breaks, 
86
+            x,y,likelihood = return_x_y_from_stwp_theta_file(folder_path+file_name, breaks = breaks,
96
                                                              tgen = tgen,
87
                                                              tgen = tgen,
97
                                                              mu = mu)
88
                                                              mu = mu)
98
             if x == 0 or y == 0:
89
             if x == 0 or y == 0:
100
             cpt +=1
91
             cpt +=1
101
             scenari[likelihood] = x,y
92
             scenari[likelihood] = x,y
102
     print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(breaks)+"\n*******\n")
93
     print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(breaks)+"\n*******\n")
103
-    print(cpt, "theta files have been scanned.")
94
+    print(cpt, "theta file(s) have been scanned.")
104
     # sort starting by the smallest -log(Likelihood)
95
     # sort starting by the smallest -log(Likelihood)
105
     best10_scenari = (sorted(list(scenari.keys())))[:10]
96
     best10_scenari = (sorted(list(scenari.keys())))[:10]
106
     print("10 greatest Likelihoods", best10_scenari)
97
     print("10 greatest Likelihoods", best10_scenari)
109
     my_dpi = 300
100
     my_dpi = 300
110
     plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
101
     plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
111
     plt.plot(x, y, 'r-', lw=2, label = 'Lik='+greatest_likelihood)
102
     plt.plot(x, y, 'r-', lw=2, label = 'Lik='+greatest_likelihood)
103
+    plt.yscale('log')
104
+    #plt.xscale('log')
105
+    plt.grid(True,which="both", linestyle='--')
106
+
112
     for scenario in best10_scenari[1:]:
107
     for scenario in best10_scenari[1:]:
113
         x,y = scenari[scenario]
108
         x,y = scenari[scenario]
114
-        print("\n----  Lik:",scenario,"\n\nt=", x,"\n\nN=",y, "\n\n")
109
+        #print("\n----  Lik:",scenario,"\n\nt=", x,"\n\nN=",y, "\n\n")
115
         plt.plot(x, y, '--', lw=1, label = 'Lik='+scenario)
110
         plt.plot(x, y, '--', lw=1, label = 'Lik='+scenario)
116
     plt.ylabel("Individuals (N)")
111
     plt.ylabel("Individuals (N)")
117
     plt.xlabel("Time (years)")
112
     plt.xlabel("Time (years)")
119
     plt.title(title)
114
     plt.title(title)
120
     #plt.gcf().set_size(1000, 500)
115
     #plt.gcf().set_size(1000, 500)
121
     plt.savefig(title+'_b'+str(breaks)+'.pdf')
116
     plt.savefig(title+'_b'+str(breaks)+'.pdf')
122
-    #plt.show()
123
 
117
 
124
 if __name__ == "__main__":
118
 if __name__ == "__main__":
125
-            
119
+
126
     if len(sys.argv) != 4:
120
     if len(sys.argv) != 4:
127
         print("Need 3 args: ThetaFolder MutationRate GenerationTime")
121
         print("Need 3 args: ThetaFolder MutationRate GenerationTime")
128
         exit(0)
122
         exit(0)
129
-        
123
+
130
     folder_path = sys.argv[1]
124
     folder_path = sys.argv[1]
131
     mu = sys.argv[2]
125
     mu = sys.argv[2]
132
     tgen = sys.argv[3]
126
     tgen = sys.argv[3]