Browse Source

Remove unused functions for swp2

tforest 11 months ago
parent
commit
6a6d4bf6f9
1 changed files with 8 additions and 63 deletions
  1. 8 63
      swp2.py

+ 8 - 63
swp2.py View File

3
 import numpy as np
3
 import numpy as np
4
 import math
4
 import math
5
 import json
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
 def log_facto(k):
7
 def log_facto(k):
8
+    """
9
+    Using the Stirling's approximation
10
+    """
13
     k = int(k)
11
     k = int(k)
14
     if k > 1e6:
12
     if k > 1e6:
15
         return k * np.log(k) - k + np.log(2*math.pi*k)/2
13
         return k * np.log(k) - k + np.log(2*math.pi*k)/2
18
         val += np.log(i)
16
         val += np.log(i)
19
     return val
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
 def return_x_y_from_stwp_theta_file(stwp_theta_file, breaks, mu, tgen, relative_theta_scale = False):
19
 def return_x_y_from_stwp_theta_file(stwp_theta_file, breaks, mu, tgen, relative_theta_scale = False):
30
     with open(stwp_theta_file, "r") as swp_file:
20
     with open(stwp_theta_file, "r") as swp_file:
31
         # Read the first line
21
         # Read the first line
128
     #     #     x[i] = x[i]/N0
118
     #     #     x[i] = x[i]/N0
129
     return x,y,likelihood,thetas,sfs,L
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
 def plot_k_epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title", theta_scale = True):
122
 def plot_k_epochs_thetafolder(folder_path, mu, tgen, breaks = 2, title = "Title", theta_scale = True):
178
     scenari = {}
123
     scenari = {}
180
     for file_name in os.listdir(folder_path):
125
     for file_name in os.listdir(folder_path):
181
         if os.path.isfile(os.path.join(folder_path, file_name)):
126
         if os.path.isfile(os.path.join(folder_path, file_name)):
182
             # Perform actions on each file
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
                                                              tgen = tgen,
129
                                                              tgen = tgen,
185
                                      mu = mu, relative_theta_scale = theta_scale)
130
                                      mu = mu, relative_theta_scale = theta_scale)
186
             if x == 0 or y == 0:
131
             if x == 0 or y == 0:
396
         cpt +=1
341
         cpt +=1
397
         if os.path.isfile(os.path.join(folder_path, file_name)):
342
         if os.path.isfile(os.path.join(folder_path, file_name)):
398
             for k in range(breaks_max):
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
                                                                  tgen = tgen,
345
                                                                  tgen = tgen,
401
                                                                  mu = mu, relative_theta_scale = theta_scale)
346
                                                                  mu = mu, relative_theta_scale = theta_scale)
402
                 if thetas == 0:
347
                 if thetas == 0:
591
         cpt +=1
536
         cpt +=1
592
         if os.path.isfile(os.path.join(folder_path, file_name)):
537
         if os.path.isfile(os.path.join(folder_path, file_name)):
593
             for k in range(breaks_max):
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
                                                                  tgen = tgen,
540
                                                                  tgen = tgen,
596
                                                                  mu = mu, relative_theta_scale = theta_scale)
541
                                                                  mu = mu, relative_theta_scale = theta_scale)
597
                 if thetas == 0:
542
                 if thetas == 0:
751
     # # plot_all_epochs_thetafolder(folder_path, mu, tgen, title, theta_scale, ax = None)
696
     # # plot_all_epochs_thetafolder(folder_path, mu, tgen, title, theta_scale, ax = None)
752
     # # plot_test_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = breaks, ax = None)
697
     # # plot_test_theta(folder_path, mu, tgen, title, theta_scale, breaks_max = breaks, ax = None)
753
     # # plt.clf()
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
     with open(title+"_plotdata.json", 'r') as json_file:
700
     with open(title+"_plotdata.json", 'r') as json_file:
756
         loaded_data = json.load(json_file)
701
         loaded_data = json.load(json_file)
757
     # plot page 1 of summary
702
     # plot page 1 of summary