Browse Source

Add some outputs for swp2 parsing

tforest 10 months ago
parent
commit
14538a747b
1 changed files with 42 additions and 9 deletions
  1. 42 9
      swp2.py

+ 42 - 9
swp2.py View File

@@ -458,6 +458,9 @@ def save_k_theta(folder_path, mu, tgen, title = "Title", theta_scale = True,
458 458
     return saved_plots
459 459
 
460 460
 def plot_scaled_theta(plot_lines, prop, title, mu, tgen, swp2_lines = None, ax = None, n_ticks = 10, subset = None, theta_scale = False):
461
+    recent_limit_years = 500
462
+    # recent limit in coal. time
463
+    recent_limit = recent_limit_years/tgen*mu
461 464
     # nb of plot_lines represent the number of epochs stored (len(plot_lines) = #breaks+1)
462 465
     nb_epochs = len(plot_lines)
463 466
     # fig 2 & 3
@@ -487,8 +490,8 @@ def plot_scaled_theta(plot_lines, prop, title, mu, tgen, swp2_lines = None, ax =
487 490
         # Plotting (fig 3) which is the same but log scale for x
488 491
         p3, = ax3.plot(x2_plot, y2_plot, linestyle="-", alpha=0.75, lw=2, label = 'swp2', color="black")
489 492
         lines_fig3.append(p3)
490
-    min_x = 0
491
-    min_y = 0
493
+    min_x = 1
494
+    min_y = 1
492 495
     max_x = 0
493 496
     max_y = 0
494 497
     for breaks, plot in enumerate(plot_lines):
@@ -502,11 +505,39 @@ def plot_scaled_theta(plot_lines, prop, title, mu, tgen, swp2_lines = None, ax =
502 505
                 min_y = min(min_y, min(y2_plot))
503 506
                 max_x = max(max_x, max(x2_plot))
504 507
                 max_y = max(max_y, max(y2_plot))
508
+
509
+                # skip the base 0 points x_plot[0:3]
510
+                t_max_below_limit = 0
511
+                t_min_below_limit = 1
512
+                recent_change = False
513
+                for t in x[1:]:
514
+                    if t <= recent_limit:
515
+                        recent_change = True
516
+                        t_max_below_limit = max(t_max_below_limit, t)
517
+                        t_min_below_limit = min(t_min_below_limit, t)
518
+                        Ne_max_below_limit = y[x.index(t_max_below_limit)]
519
+                        Ne_min_below_limit = y[x.index(t_min_below_limit)]
520
+                if recent_change:
521
+                    print(f"\n{breaks} breaks ; This is below the recent limit of {recent_limit_years} years:\n",
522
+                          f"t_min (most recent time point under the limit) : {t_min_below_limit/mu*tgen:.1f} t_max (most ancient time point under the limit) : {t_max_below_limit/mu*tgen:.1f}",
523
+                          f"\nNe_min (effective size at t_min) : {Ne_min_below_limit/(4*mu):.1f}  Ne_max (effective size at t_max) : {Ne_max_below_limit/(4*mu):.1f}",
524
+                          f"\nNe_min/Ne_max = {(Ne_min_below_limit/(4*mu)) / (Ne_max_below_limit/(4*mu)):.1f}",
525
+                          f"\nEvolution: {((Ne_min_below_limit/(4*mu)) - (Ne_max_below_limit/(4*mu)))/((Ne_max_below_limit/(4*mu)))*100:.1f}%")
526
+                else:
527
+                    print(f"Recent event under {recent_limit_years} years: NA")
528
+                    # need to compute the last change and when it occured
529
+                    tmin = x[1]
530
+                    tmin_plus_1 = x[2]
531
+                    Ne_min = y[1]
532
+                    Ne_min_plus_1 = y[2]
533
+                    print(f"Last was {tmin/mu*tgen:.1f} years ago. And was of {((Ne_min/(4*mu)) - (Ne_min_plus_1/(4*mu)))/(Ne_min_plus_1/(4*mu))*100:.1f}%")
534
+                    
505 535
             else:
506 536
                 masking_alpha = 0
507 537
                 autoscale = False
508 538
         ax2.set_autoscale_on(autoscale)
509 539
         ax3.set_autoscale_on(autoscale)
540
+                
510 541
         p2, = ax2.plot(x2_plot, y2_plot, 'o', linestyle="-", alpha=masking_alpha, lw=2, label = str(breaks)+' brks')
511 542
         # Plotting (fig 3) which is the same but log scale for x
512 543
         p3, = ax3.plot(x2_plot, y2_plot, 'o', linestyle="-", alpha=masking_alpha, lw=2, label = str(breaks)+' brks')
@@ -514,7 +545,8 @@ def plot_scaled_theta(plot_lines, prop, title, mu, tgen, swp2_lines = None, ax =
514 545
             # store for legend
515 546
             lines_fig2.append(p2)
516 547
             lines_fig3.append(p3)
517
-    ax3.axvline(x=500/tgen*mu, linestyle="--")
548
+    # put the vertical line of the "recent" time limit
549
+    ax3.axvline(x=recent_limit, linestyle="--")
518 550
     if theta_scale:
519 551
         xlabel = "Theta scaled by N0"
520 552
         ylabel = "Theta scaled by N0"
@@ -527,19 +559,20 @@ def plot_scaled_theta(plot_lines, prop, title, mu, tgen, swp2_lines = None, ax =
527 559
         plt.ylabel(ylabel, fontsize=fnt_size)
528 560
         #plt.xlim(left=0)
529 561
         #xlim_val = plt.gca().get_xlim()
530
-        x_ticks = list(plt.xticks())[0]
531
-        # plt.gca().set_xticks(x_ticks)
532
-        plt.xticks(x_ticks)
533
-        # plt.gca().set_xlim(xlim_val)
562
+        #x_ticks = list(plt.xticks())[0]
534 563
         plt.xlim(min(min_x,min(swp2_lines[0])), max(max(swp2_lines[0]), max_x))
564
+        x_ticks = list(plt.gca().get_xticks())
565
+        plt.gca().set_xticks(x_ticks)
566
+        # plt.xticks(x_ticks)
567
+        # plt.gca().set_xlim(xlim_val)
535 568
         plt.gca().set_xticklabels([f'{k:.0e}\n{k/(mu):.0e}\n{k/(mu)*tgen:.0e}' for k in x_ticks], fontsize = fnt_size*0.5)
536 569
         # rescale y to effective pop size
537 570
         # ylim_val = plt.gca().get_ylim()
571
+        plt.ylim(min(min_y,min(swp2_lines[1])), max(max_y+(max_y*0.05), max(swp2_lines[1])+(max(swp2_lines[1])*0.05)))        
538 572
         y_ticks = list(plt.yticks())[0]
539
-        # plt.gca().set_yticks(y_ticks)
573
+        plt.gca().set_yticks(y_ticks)
540 574
         # plt.gca().set_ylim(ylim_val)
541 575
         plt.yticks(y_ticks)
542
-        plt.ylim(min(min_y,min(swp2_lines[1])), max(max_y+(max_y*0.05), max(swp2_lines[1])+(max(swp2_lines[1])*0.05)))
543 576
         plt.gca().set_yticklabels([f'{k/(4*mu):.0e}' for k in y_ticks], fontsize = fnt_size*0.5)
544 577
         plt.title(title, fontsize=fnt_size)
545 578
         plt.legend(handles=lines_fig2, loc='best', fontsize = fnt_size*0.5)