def run_genetic_algorithm(params_tuple): """Main function to run genetic algorithm with prefix number. params_tuple : tuple of 3 objects: number : number of GA to run (moreover its prefix). log_file : log file to write output. shared_dict : dictionary to share information between processes. """ np.random.seed() number = 'ID' try: def write_func(string): return support.write_log(log_file, string, write_to_stdout=not params.silence) number, params, log_file, shared_dict = params_tuple write_func('Run genetic algorithm number ' + str(number)) ga_instance = GA(params, prefix=str(number)) ga_instance.run(shared_dict=shared_dict) write_func('Finish genetic algorithm number ' + str(number)) return number, ga_instance.best_model() except Exception, e: err_str = 'GA number ' + str(number) + ': ' + str(e) support.error(err_str, error_instance=e, exit=False) raise RuntimeError(err_str)
def load_parameters_from_python_file(filename, as_module=False): if not as_module: f = support.check_file_existence(filename) try: module = imp.load_source('module', f) except Exceprint, e: support.error('File ' + filename + " is not valid python file.", error_instance=e)
def main(): parser = argparse.ArgumentParser( 'GADMA module for runs of local search on bootstrapped data. Is needed for calculating confidence intervals.' ) parser.add_argument('-b', '--boots', metavar="<dir>", required=True, help='Directory where bootstrapped data is located.') parser.add_argument( '-d', '--dem_model', metavar="<filename>", required=True, help= 'File with demographic model. Should contain `model_func` or `generated_model` function. One can put there several extra parameters and they will be taken automatically, otherwise one will need to enter them manually. Such parameters are:\n\t1) p0 (or popt) - initial parameters values\n\t2) lower_bound - list of lower bounds for parameters values\n\t4) upper_bound - list of upper bounds for parameters values\n\t5) par_labels/param_labels - list of string names for parameters 6) pts - pts for dadi (if there is no pts then moments will be run automatically).' ) parser.add_argument('-o', '--output', metavar="<dir>", required=True, help='Output directory.') parser.add_argument('-j', '--jobs', metavar="N", type=int, default=1, help='Number of threads for parallel run.') parser.add_argument( '--opt', metavar="log/powell", type=str, default='log', help= 'Local search algorithm, by now it can be:\n\t1) `log` - Inference.optimize_log\n\t2) `powell` - Inference.optimize_powell.' ) parser.add_argument( '-p', '--params', metavar="<filename>", type=str, default=None, help= 'Filename with parameters, should be valid python file. Parameters are presented in -d/--dem_model option description upper.' ) args = parser.parse_args() output = support.ensure_dir_existence(args.output, False) all_boot = gadma.Inference.load_bootstrap_data_from_dir( args.boots, return_filenames=True) print(str(len(all_boot)) + ' bootstrapped data found.') #extract model_func, we cannot put it to function as multiprocessing need pickleable functions args.dem_model = support.check_file_existence(args.dem_model) try: file_with_model_func = imp.load_source('module', args.dem_model) except Exception, e: support.error('File ' + args.dem_model + " is not valid python file.", error_instance=e)
args.dem_model = support.check_file_existence(args.dem_model) try: file_with_model_func = imp.load_source('module', args.dem_model) except Exception, e: support.error('File ' + args.dem_model + " is not valid python file.", error_instance=e) try: model_func = file_with_model_func.model_func name = 'model_func' except: try: model_func = file_with_model_func.generated_model name = 'generated_model' except: support.error( "File " + dem_model_filename + ' does not contain function named `model_func` or `generated_model`.' ) par_labels = get_params_names(args.dem_model, name) lower_bound, upper_bound, p0, new_par_labels, pts = load_parameters_from_python_file( file_with_model_func, as_module=True) if new_par_labels is not None: par_labels = new_par_labels if args.params is not None: new_lower_bound, new_upper_bound, new_p0, new_par_labels, new_pts = load_parameters_from_python_file( args.params) if new_lower_bound is not None: lower_bound = new_lower_bound if new_upper_bound is not None: upper_bound = new_upper_bound if new_p0 is not None:
def main(): params = options.parse_args() log_file = os.path.join( params.output_dir, 'GADMA.log') open(log_file, 'w').close() support.write_log(log_file, "--Successful arguments' parsing--\n") params_filepath = os.path.join( params.output_dir, 'params') params.save(params.output_dir) if not params.test: support.write_log( log_file, 'You can find all parameters of this run in:\t\t' + params_filepath + '\n') support.write_log(log_file, 'All output is saved (without warnings and errors) in:\t' + os.path.join(params.output_dir, 'GADMA.log\n')) support.write_log(log_file, '--Start pipeline--\n') # For debug # run_genetic_algorithm((1, params, log_file, None)) # Create shared dictionary m = Manager() shared_dict = m.dict() # Start pool of processes start_time = datetime.now() pool = Pool(processes=params.processes, initializer=worker_init) try: pool_map = pool.map_async( run_genetic_algorithm, [(i + 1, params, log_file, shared_dict) for i in range(params.repeats)]) pool.close() precision = 1 - int(math.log(params.epsilon, 10)) # graceful way to interrupt all processes by Ctrl+C min_counter = 0 while True: try: multiple_results = pool_map.get( 60 * params.time_for_print) break # catch TimeoutError and get again except multiprocessing.TimeoutError as ex: print_best_solution_now( start_time, shared_dict, params, log_file, precision, draw_model=params.matplotlib_available) except Exception, e: pool.terminate() support.error(str(e)) print_best_solution_now(start_time, shared_dict, params,log_file, precision, draw_model=params.matplotlib_available) support.write_log(log_file, '\n--Finish pipeline--\n') if params.test: support.write_log(log_file, '--Test passed correctly--') if params.theta is None: support.write_log( log_file, "\nYou didn't specify theta at the beginning. If you want change it and rescale parameters, please see tutorial.\n") if params.resume_dir is not None and (params.initial_structure != params.final_structure).any(): support.write_log( log_file, '\nYou have resumed from another launch. Please, check best AIC model, as information about it was lost.\n') support.write_log(log_file, 'Thank you for using GADMA!')
break # catch TimeoutError and get again except multiprocessing.TimeoutError as ex: print_best_solution_now( start_time, shared_dict, params, log_file, precision, draw_model=params.matplotlib_available) except Exception, e: pool.terminate() support.error(str(e)) print_best_solution_now(start_time, shared_dict, params,log_file, precision, draw_model=params.matplotlib_available) support.write_log(log_file, '\n--Finish pipeline--\n') if params.test: support.write_log(log_file, '--Test passed correctly--') if params.theta is None: support.write_log( log_file, "\nYou didn't specify theta at the beginning. If you want change it and rescale parameters, please see tutorial.\n") if params.resume_dir is not None and (params.initial_structure != params.final_structure).any(): support.write_log( log_file, '\nYou have resumed from another launch. Please, check best AIC model, as information about it was lost.\n') support.write_log(log_file, 'Thank you for using GADMA!') except KeyboardInterrupt: pool.terminate() support.error('KeyboardInterrupt') if __name__ == '__main__': main()