def parameter_explorer_traveller_using_asymmetry_criteria( parameter_exploration_program, num_tweaks_to_generate=11, num_processes=4, sequential=False, num_loops_before_giving_up=100, TOTAL_TIME=500): given_parameter_labels = [] given_parameter_values = [] given_parameter_num_values = [] num_estimated_combinations = 1 for parameter_label, start_value, end_value, range_resolution in parameter_exploration_program: given_parameter_labels.append(parameter_label) given_parameter_values.append( np.linspace(start_value, end_value, num=range_resolution)) given_parameter_num_values.append(range_resolution) num_estimated_combinations = num_estimated_combinations * range_resolution all_parameter_labels = parameterorg.standard_parameter_dictionary_keys for parameter_label in given_parameter_labels: if parameter_label not in all_parameter_labels: raise StandardError( "Parameter label {} not in standard parameter dictionary.". format(parameter_label)) print "Number of parameter combinations: ", num_estimated_combinations # pick random starting point global executed_pd_indices global executed_pds global executed_pd_scores current_pd_index = pick_random_point(given_parameter_num_values) improved_score = False current_no_improvement_streak = 0 current_best_score = -1.0 worker_pool = multiproc.Pool(processes=num_processes, maxtasksperchild=750) while current_no_improvement_streak < num_loops_before_giving_up: st = time.time() print "current best score: ", current_best_score print "current_no_improvement_streak: ", current_no_improvement_streak improved_score = False pd_indices = [current_pd_index] + [ x for x in generate_tweaks_from_given_point( given_parameter_num_values, current_pd_index, num_tweaks_to_generate) if x not in executed_pd_indices ] num_pd_indices_to_run = len(pd_indices) print "num_pd_indices_to_run: ", num_pd_indices_to_run if num_pd_indices_to_run < 0.5 * (num_tweaks_to_generate + 1): print "re-randomizing current pd indices..." current_pd_index = pick_random_point(given_parameter_num_values) pds = [ generate_parameter_update_dict(given_parameter_labels, given_parameter_values, pd_index) for pd_index in pd_indices ] chunky_pd_indices = general.chunkify(pd_indices, num_processes) chunky_pds = general.chunkify(pds, num_processes) scores = [] for n, pd_index_chunk in enumerate(chunky_pds): executed_pd_indices += chunky_pd_indices[n] executed_pds += pd_index_chunk task_args = [ make_environment_defn(TOTAL_TIME, -1, parameter_override_dict=pd_index) for pd_index in pd_index_chunk ] loop_result_cells = [] print "running simulations..." if sequential == True: loop_result_cells = [] for task in task_args: loop_result_cells.append(run_environment_dynamics(task)) else: loop_result_cells = worker_pool.map(run_environment_dynamics, task_args) scores = [ analysis.score_symmetry(a_cell, significant_difference=0.2, weigh_by_timepoint=True) for a_cell in loop_result_cells ] averaged_scores = [ np.average(score_dict.values()) for score_dict in scores ] print "scores: ", scores executed_pd_scores += scores for i, avg_score in enumerate(averaged_scores): if avg_score > current_best_score: print "improved score!" current_pd_index = pd_indices[n + i] current_best_score = avg_score improved_score = True if improved_score == True: current_no_improvement_streak = 0 else: current_no_improvement_streak += 1 et = time.time() print "time taken: ", np.round(et - st, decimals=1) print "-----------------------" return zip(executed_pd_scores, executed_pds)
def parameter_explorer_asymmetry_criteria(parameter_exploration_program, num_timesteps, num_nodes_per_cell, num_cells, task_chunk_size=4, num_processes=4, sequential=False, min_symmetry_score=0.01, filters=[]): given_parameter_labels = [] given_parameter_values = [] num_estimated_combinations = 1 for parameter_label, start_value, end_value, range_resolution in parameter_exploration_program: given_parameter_labels.append(parameter_label) given_parameter_values.append( np.linspace(start_value, end_value, num=range_resolution)) num_estimated_combinations = num_estimated_combinations * range_resolution all_parameter_labels = parameterorg.standard_parameter_dictionary_keys if not np.all([(parameter_label in all_parameter_labels) for parameter_label in given_parameter_labels]): raise StandardError( "Not all given parameter labels are in the standard parameter dictionary." ) print "Estimated number of combinations: ", num_estimated_combinations task_value_arrays = cartesian(tuple(given_parameter_values)) num_combinations = len(task_value_arrays) print "Number of parameter combinations: ", num_combinations chunky_task_value_array_indices = general.chunkify( np.arange(num_combinations), task_chunk_size) num_task_chunks = len(chunky_task_value_array_indices) results = [] num_results = 0 worker_pool = multiproc.Pool(processes=num_processes, maxtasksperchild=750) for chunk_index, task_index_chunk in enumerate( chunky_task_value_array_indices): st = time.time() print "Executing task chunk %d/%d..." % (chunk_index + 1, num_task_chunks) parameter_dicts = [] task_chunk = [] for task_index in task_index_chunk: task_value_array = task_value_arrays[task_index] update_dict = dict(zip(given_parameter_labels, task_value_array)) parameter_dicts.append(update_dict) task_environment_cell_group_defn = make_parameter_dictionary( task_index, update_dict, num_nodes_per_cell, num_cells) task_chunk.append( (task_environment_cell_group_defn, num_timesteps)) loop_result_cells = [] if sequential == True: loop_result_cells = [] for task in task_chunk: loop_result_cells.append(run_environment_dynamics(task)) else: loop_result_cells = worker_pool.map(run_environment_dynamics, task_chunk) symmetry_results = [ analysis.score_symmetries(a_cell) for a_cell in loop_result_cells ] for sym_result, parameter_dict in zip(symmetry_results, parameter_dicts): if np.all(np.array(sym_result.values()) > min_symmetry_score): results.append((sym_result, parameter_dict)) num_results = num_results + 1 et = time.time() print "Time: ", np.round(et - st, decimals=1) print "Results: ", num_results return results
def parameter_explorer_asymmetry_criteria(parameter_exploration_program, task_chunk_size=4, num_processes=4, sequential=False, min_polarity_score=0, filters=[], TOTAL_TIME=500): given_parameter_labels = [] given_parameter_values = [] num_estimated_combinations = 1 for parameter_label, start_value, end_value, range_resolution in parameter_exploration_program: given_parameter_labels.append(parameter_label) given_parameter_values.append( np.linspace(start_value, end_value, num=range_resolution)) num_estimated_combinations = num_estimated_combinations * range_resolution all_parameter_labels = parameterorg.standard_parameter_dictionary_keys for parameter_label in given_parameter_labels: if parameter_label not in all_parameter_labels: raise StandardError( "Parameter label {} not in standard parameter dictionary.". format(parameter_label)) print "Estimated number of combinations: ", num_estimated_combinations task_value_arrays = cartesian(tuple(given_parameter_values)) num_combinations = len(task_value_arrays) print "Number of parameter combinations: ", num_combinations chunky_task_value_array_indices = general.chunkify( np.arange(num_combinations), task_chunk_size) num_task_chunks = len(chunky_task_value_array_indices) results = [] num_results = 0 worker_pool = multiproc.Pool(processes=num_processes, maxtasksperchild=750) global global_results for chunk_index, task_index_chunk in enumerate( chunky_task_value_array_indices): st = time.time() print "Executing task chunk %d/%d..." % (chunk_index + 1, num_task_chunks) parameter_dicts = [] task_chunk = [] for task_index in task_index_chunk: task_value_array = task_value_arrays[task_index] update_dict = dict(zip(given_parameter_labels, task_value_array)) parameter_dicts.append(update_dict) task_environment_defn = make_environment_defn( TOTAL_TIME, task_index, parameter_override_dict=update_dict) task_chunk.append(task_environment_defn) loop_result_cells = [] if sequential == True: loop_result_cells = [] for task in task_chunk: loop_result_cells.append(run_environment_dynamics(task)) else: loop_result_cells = worker_pool.map(run_environment_dynamics, task_chunk) polarity_rating_tuples_per_loop_cell = [ analysis.calculate_rgtpase_polarity_score( a_cell, significant_difference=0.2, weigh_by_timepoint=False) for a_cell in loop_result_cells ] polarity_ratings_per_loop_cell = [ x[0] for x in polarity_rating_tuples_per_loop_cell ] print "polarity ratings: ", polarity_ratings_per_loop_cell for polarity_result, parameter_dict in zip( polarity_ratings_per_loop_cell, parameter_dicts): if polarity_result > min_polarity_score: results.append((polarity_result, parameter_dict)) num_results = num_results + 1 global_results = results et = time.time() print "Time: ", np.round(et - st, decimals=1) print "Results: ", num_results return results
def parameter_explorer_genetic(parameter_exploration_program, invariant_parameter_overrides, task_chunk_size=4, num_processes=4, sequential=False, TOTAL_TIME=500, num_children=10, num_generations=100, mutation_probability=0.02, init_parents=[]): given_parameter_labels = [x[0] for x in parameter_exploration_program] all_parameter_labels = parameterorg.standard_parameter_dictionary_keys for parameter_label in given_parameter_labels: if parameter_label not in all_parameter_labels: raise StandardError( "Parameter label {} not in standard parameter dictionary.". format(parameter_label)) if len(init_parents) != 0: init_p1_dict = init_parents.pop() init_p1 = ParDict( dict([(x[0], init_p1_dict[x[0]]) for x in parameter_exploration_program]), None, TOTAL_TIME, invariant_parameter_overrides) else: init_p1 = ParDict( dict([(x[0], x[1] + (x[2] - x[1]) * np.random.rand()) for x in parameter_exploration_program]), None, TOTAL_TIME, invariant_parameter_overrides) if len(init_parents) != 0: init_p2_dict = init_parents.pop() init_p2 = ParDict( dict([(x[0], init_p2_dict[x[0]]) for x in parameter_exploration_program]), None, TOTAL_TIME, invariant_parameter_overrides) else: init_p2 = ParDict( dict([(x[0], x[1] + (x[2] - x[1]) * np.random.rand()) for x in parameter_exploration_program]), None, TOTAL_TIME, invariant_parameter_overrides) worker_pool = multiproc.Pool(processes=num_processes, maxtasksperchild=750) global g_generations g_generations = [[init_p1, init_p2]] for generation_number in range(num_generations): print "------GENERATION {}--------".format(generation_number) st = time.time() current_generation = g_generations[generation_number] print "Num elements in current generation: ", len(current_generation) chunky_current_generation = general.chunkify(current_generation, task_chunk_size) num_chunks = len(chunky_current_generation) print "Num chunks: ", num_chunks for i, current_gen_chunk in enumerate(chunky_current_generation): print "Executing chunk {}/{}".format(i + 1, num_chunks) # fitness_results = [] # for pdobj in current_gen_chunk: # fitness_results.append(pdobj.update_fitness()) fitness_results = worker_pool.map(mp_update_fitness, current_gen_chunk) for fitness_result, cg_pdo in zip(fitness_results, current_gen_chunk): cg_pdo.fitness = fitness_result current_generation = sorted(current_generation, key=lambda x: x.fitness[1], reverse=True) g_generations[generation_number] = current_generation et = time.time() print "Multiproc calcs time: {}".format(np.round(et - st, decimals=2)) fittest_in_generation = current_generation[:4] fitness_of_generation = np.array( [x.fitness[1] for x in fittest_in_generation]) print "generation_fitness: ", fitness_of_generation mating_pairs = determine_mating_pairs(len(fittest_in_generation)) # if np.all(np.abs(np.average(fitness_of_generation) - fitness_of_generation) < 1e-8) == True: # print "Breaking because fittest_have_homogeneized..." # break if generation_number != num_generations - 1: print "Generating children..." next_generation = [] for mating_pair in mating_pairs: mate1 = fittest_in_generation[mating_pair[0]] mate2 = fittest_in_generation[mating_pair[1]] children = mate1.mate_with(mate2, num_children, parameter_exploration_program, mutation_probability) next_generation += children print "Num elements in next gen: ", len(next_generation) + len( fittest_in_generation) g_generations.append(fittest_in_generation + next_generation) print "Done." print "---------------------------" return g_generations