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