Package Bio :: Package PopGen :: Package FDist :: Module Async
[hide private]
[frames] | no frames]

Source Code for Module Bio.PopGen.FDist.Async

  1  # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """Asynchronous execution of Fdist and spliting of loads. 
  7   
  8  FDistAsync Allows for the execution of FDist. 
  9   
 10  SplitFDist splits a single Fdist execution in several, taking advantage 
 11  of multi-core architectures. 
 12  """ 
 13   
 14  import os 
 15  import shutil 
 16  import threading 
 17  from time import sleep 
 18  from Bio.PopGen.Async import Local 
 19  from Bio.PopGen.FDist.Controller import FDistController 
 20   
 21   
22 -class FDistAsync(FDistController):
23 """Asynchronous FDist execution. 24 """ 25
26 - def __init__(self, fdist_dir="", ext=None):
27 """Constructor. 28 29 Parameters: 30 31 - fdist_dir - Where fdist can be found, if = "", then it 32 should be on the path. 33 - ext - Extension of binary names (e.g. nothing on Unix, 34 ".exe" on Windows 35 """ 36 FDistController.__init__(self, fdist_dir, ext)
37
38 - def run_job(self, parameters, input_files):
39 """Runs FDist asynchronously. 40 41 Gets typical Fdist parameters from a dictionary and 42 makes a "normal" call. This is run, normally, inside 43 a separate thread. 44 """ 45 npops = parameters['npops'] 46 nsamples = parameters['nsamples'] 47 fst = parameters['fst'] 48 sample_size = parameters['sample_size'] 49 mut = parameters.get('mut', 0) 50 num_sims = parameters.get('num_sims', 20000) 51 data_dir = parameters.get('data_dir', '.') 52 is_dominant = parameters.get('is_dominant', False) 53 theta = parameters.get('theta', 0.06) 54 beta = parameters.get('beta', (0.25, 0.25)) 55 max_freq = parameters.get('max_freq', 0.99) 56 fst = self.run_fdist(npops, nsamples, fst, sample_size, 57 mut, num_sims, data_dir, 58 is_dominant, theta, beta, 59 max_freq) 60 output_files = {} 61 output_files['out.dat'] = open(data_dir + os.sep + 'out.dat', 'r') 62 return fst, output_files
63 64
65 -class SplitFDist(object):
66 """Splits a FDist run. 67 68 The idea is to split a certain number of simulations in smaller 69 numbers (e.g. 30.000 sims split in 30 packets of 1.000). This 70 allows to run simulations in parallel, thus taking advantage 71 of multi-core CPUs. 72 73 Each SplitFDist object can only be used to run a single FDist 74 simulation. 75 """
76 - def __init__(self, report_fun=None, 77 num_thr=2, split_size=1000, fdist_dir='', ext=None):
78 """Constructor. 79 80 Parameters: 81 82 - report_fun - Function that is called when a single packet is 83 run, it should have a single parameter: Fst. 84 - num_thr - Number of desired threads, typically the number 85 of cores. 86 - split_size - Size that a full simulation will be split in. 87 - ext - Binary extension name (e.g. nothing on Unix, '.exe' on 88 Windows). 89 """ 90 self.async = Local.Local(num_thr) 91 self.async.hooks['fdist'] = FDistAsync(fdist_dir, ext) 92 self.report_fun = report_fun 93 self.split_size = split_size
94 95 # There might be races when reporting...
96 - def monitor(self):
97 """Monitors and reports (using report_fun) execution. 98 99 Every time a partial simulation ends, calls report_fun. 100 IMPORTANT: monitor calls can be concurrent with other 101 events, ie, a tasks might end while report_fun is being 102 called. This means that report_fun should be consider that 103 other events might be happening while it is running (it 104 can call acquire/release if necessary). 105 """ 106 while(True): 107 sleep(1) 108 self.async.access_ds.acquire() 109 keys = list(self.async.done.keys()) # copy it 110 self.async.access_ds.release() 111 for done in keys: 112 self.async.access_ds.acquire() 113 fst, files = self.async.done[done] 114 del self.async.done[done] 115 out_dat = files['out.dat'] 116 f = open(self.data_dir + os.sep + 'out.dat', 'a') 117 f.writelines(out_dat.readlines()) 118 f.close() 119 out_dat.close() 120 self.async.access_ds.release() 121 for file in os.listdir(self.parts[done]): 122 os.remove(self.parts[done] + os.sep + file) 123 os.rmdir(self.parts[done]) 124 if self.report_fun: 125 self.report_fun(fst) 126 self.async.access_ds.acquire() 127 if len(self.async.waiting) == 0 and len(self.async.running) == 0 \ 128 and len(self.async.done) == 0: 129 break 130 self.async.access_ds.release()
131
132 - def acquire(self):
133 """Allows the external acquisition of the lock. 134 """ 135 self.async.access_ds.acquire()
136
137 - def release(self):
138 """Allows the external release of the lock. 139 """ 140 self.async.access_ds.release()
141 142 # You can only run a fdist case at a time
143 - def run_fdist(self, npops, nsamples, fst, sample_size, 144 mut=0, num_sims=20000, data_dir='.', 145 is_dominant=False, theta=0.06, beta=(0.25, 0.25), 146 max_freq=0.99):
147 """Runs FDist. 148 149 Parameters can be seen on FDistController.run_fdist. 150 151 It will split a single execution in several parts and 152 create separated data directories. 153 """ 154 num_parts = num_sims // self.split_size 155 self.parts = {} 156 self.data_dir = data_dir 157 for directory in range(num_parts): 158 full_path = data_dir + os.sep + str(directory) 159 try: 160 os.mkdir(full_path) 161 except OSError: 162 pass # Its ok, if it is already there 163 if "ss_file" in os.listdir(data_dir): 164 shutil.copy(data_dir + os.sep + "ss_file", full_path) 165 id = self.async.run_program('fdist', { 166 'npops': npops, 167 'nsamples': nsamples, 168 'fst': fst, 169 'sample_size': sample_size, 170 'mut': mut, 171 'num_sims': self.split_size, 172 'data_dir': full_path, 173 'is_dominant': is_dominant, 174 'theta': theta, 175 'beta': beta, 176 'max_freq': max_freq 177 }, {}) 178 self.parts[id] = full_path 179 threading.Thread(target=self.monitor).run()
180