def __init__(self, model, exploreParameters): """Defines the model to explore and the parameter range :param model: Model to explore :type model: Model :param exploreParameters: Parameter dictionary with each exploration parameter as a key and a list of all parameter values as a value :type param: dict """ self.model = model self.exploreParameters = exploreParameters # pypet interaction starts here self.pypetParametrization = pypet.cartesian_product(exploreParameters) logging.info("Number of parameter configurations: {}".format(len(self.pypetParametrization[list(self.pypetParametrization.keys())[0]]))) # create hdf file path if it does not exist yet pathlib.Path(paths.HDF_DIR).mkdir(parents=True, exist_ok=True) # set default hdf filename self.HDF_FILE = os.path.join(paths.HDF_DIR, "exploration.hdf") # todo: use random ICs for every explored point or rather reuse the ones that are generated at model initialization self.useRandomICs = False # bool to check whether pypet was initialized properly self.initialized = False
def evalPopulationUsingPypet(self, traj, toolbox, pop, gIdx): """Evaluate the fitness of the popoulation of the current generation using pypet :param traj: Pypet trajectory :type traj: `pypet.trajectory.Trajectory` :param toolbox: `deap` toolbox :type toolbox: deap.base.Toolbox :param pop: Population :type pop: list :param gIdx: Index of the current generation :type gIdx: int :return: Evaluated population with fitnesses :rtype: list """ # Add as many explored runs as individuals that need to be evaluated. # Furthermore, add the individuals as explored parameters. # We need to convert them to lists or write our own custom IndividualParameter ;-) # Note the second argument to `cartesian_product`: # This is for only having the cartesian product # between ``generation x (ind_idx AND individual)``, so that every individual has just one # unique index within a generation. traj.f_expand( pp.cartesian_product( { "generation": [gIdx], "id": [x.id for x in pop], "individual": [list(x) for x in pop], }, [("id", "individual"), "generation"], )) # the current generation # unique id of each individual # increment the evaluationCounter self.evaluationCounter += len(pop) # run simulations for one generation evolutionResult = toolbox.map(toolbox.evaluate) # This error can have different reasons but is most likely # due to multiprocessing problems. One possibility is that your evaluation # funciton is not pickleable or that it returns an object that is not pickleable. assert len( evolutionResult) > 0, "No results returned from simulations." for idx, result in enumerate(evolutionResult): runIndex, packedReturnFromEvalFunction = result # packedReturnFromEvalFunction is the return from the evaluation function # it has length two, the first is the fitness, second is the model output assert ( len(packedReturnFromEvalFunction) == 2 ), "Evaluation function must return tuple with shape (fitness, output_data)" fitnessesResult, returnedOutputs = packedReturnFromEvalFunction pop[idx].outputs = returnedOutputs # store outputs of simulations in population pop[idx].fitness.values = fitnessesResult # mean fitness value pop[idx].fitness.score = np.nansum( pop[idx].fitness.wvalues) / (len(pop[idx].fitness.wvalues)) return pop
def initializeExploration(self, filename="exploration.hdf"): """Initialize the pypet environment :param filename: hdf filename to store the results in , defaults to "exploration.hdf" :type filename: str, optional """ # create hdf file path if it does not exist yet pathlib.Path(paths.HDF_DIR).mkdir(parents=True, exist_ok=True) # set default hdf filename self.HDF_FILE = os.path.join(paths.HDF_DIR, filename) # initialize pypet environment trajectoryName = "results" + datetime.datetime.now().strftime( "-%Y-%m-%d-%HH-%MM-%SS") trajectoryfilename = self.HDF_FILE nprocesses = multiprocessing.cpu_count() logging.info("Number of processes: {}".format(nprocesses)) # set up the pypet environment env = pypet.Environment( trajectory=trajectoryName, filename=trajectoryfilename, multiproc=True, ncores=nprocesses, complevel=9, # log_stdout=False, # log_config=None, # report_progress=True, # log_multiproc=False, ) self.env = env # Get the trajectory from the environment self.traj = env.trajectory self.trajectoryName = self.traj.v_name # Add all parameters to the pypet trajectory if self.model is not None: # if a model is specified, use the default parameter of the # model to initialize pypet self.addParametersToPypet(self.traj, self.model.params) else: # else, use a random parameter of the parameter space self.addParametersToPypet(self.traj, self.parameterSpace.getRandom(safe=True)) # Tell pypet which parameters to explore self.pypetParametrization = pypet.cartesian_product( self.exploreParameters) logging.info("Number of parameter configurations: {}".format( len(self.pypetParametrization[list( self.pypetParametrization.keys())[0]]))) self.traj.f_explore(self.pypetParametrization) # initialization done logging.info("BoxSearch: Environment initialized.") self.initialized = True
def _initializeExploration(self, filename="exploration.hdf"): """Initialize the pypet environment :param filename: hdf filename to store the results in , defaults to "exploration.hdf" :type filename: str, optional """ # create hdf file path if it does not exist yet pathlib.Path(paths.HDF_DIR).mkdir(parents=True, exist_ok=True) # set default hdf filename self.HDF_FILE = os.path.join(paths.HDF_DIR, filename) # initialize pypet environment trajectoryName = "results" + datetime.datetime.now().strftime( "-%Y-%m-%d-%HH-%MM-%SS") trajectoryfilename = self.HDF_FILE # set up the pypet environment env = pypet.Environment( trajectory=trajectoryName, filename=trajectoryfilename, multiproc=True, ncores=self.ncores, complevel=9, log_config=paths.PYPET_LOGGING_CONFIG, ) self.env = env # Get the trajectory from the environment self.traj = env.trajectory self.trajectoryName = self.traj.v_name # Add all parameters to the pypet trajectory if self.model is not None: # if a model is specified, use the default parameter of the # model to initialize pypet self._addParametersToPypet(self.traj, self.model.params) else: # else, use a random parameter of the parameter space self._addParametersToPypet( self.traj, self.parameterSpace.getRandom(safe=True)) # Tell pypet which parameters to explore self.pypetParametrization = pypet.cartesian_product( self.exploreParameters) # explicitely add all parameters within star notation, hence unwrap star notation into actual params names if self.parameterSpace.star: assert self.model is not None, "With star notation, model cannot be None" self.pypetParametrization = unwrap_star_dotdict( self.pypetParametrization, self.model) self.nRuns = len(self.pypetParametrization[list( self.pypetParametrization.keys())[0]]) logging.info(f"Number of parameter configurations: {self.nRuns}") self.traj.f_explore(self.pypetParametrization) # initialization done logging.info("BoxSearch: Environment initialized.") self.initialized = True
def main(): """ Main *boilerplate* function to start simulation """ # Now let's make use of logging logger = logging.getLogger() # Create folders for data and plots folder = os.path.join(os.getcwd(), 'experiments', 'ca_patterns_pypet') if not os.path.isdir(folder): os.makedirs(folder) filename = os.path.join(folder, 'all_patterns.hdf5') # Create an environment env = Environment(trajectory='cellular_automata', multiproc=True, ncores=4, wrap_mode='QUEUE', filename=filename, overwrite_file=True) # extract the trajectory traj = env.traj traj.par.ncells = Parameter('ncells', 400, 'Number of cells') traj.par.steps = Parameter('steps', 250, 'Number of timesteps') traj.par.rule_number = Parameter('rule_number', 30, 'The ca rule') traj.par.initial_name = Parameter('initial_name', 'random', 'The type of initial state') traj.par.seed = Parameter('seed', 100042, 'RNG Seed') # Explore exp_dict = { 'rule_number': [10, 30, 90, 110, 184], 'initial_name': ['single', 'random'], } # # You can uncomment the ``exp_dict`` below to see that changing the # # exploration scheme is now really easy: # exp_dict = {'rule_number' : [10, 30, 90, 110, 184], # 'ncells' : [100, 200, 300], # 'seed': [333444555, 123456]} exp_dict = cartesian_product(exp_dict) traj.f_explore(exp_dict) # Run the simulation logger.info('Starting Simulation') env.run(wrap_automaton) # Load all data traj.f_load(load_data=2) logger.info('Printing data') for idx, run_name in enumerate(traj.f_iter_runs()): # Plot all patterns filename = os.path.join(folder, make_filename(traj)) plot_pattern(traj.crun.pattern, traj.rule_number, filename) progressbar(idx, len(traj), logger=logger) # Finally disable logging and close all log-files env.disable_logging()
def main(): """ Main *boilerplate* function to start simulation """ # Now let's make use of logging logger = logging.getLogger() # Create folders for data and plots folder = os.path.join(os.getcwd(), 'experiments', 'ca_patterns_pypet') if not os.path.isdir(folder): os.makedirs(folder) filename = os.path.join(folder, 'all_patterns.hdf5') # Create an environment env = Environment(trajectory='cellular_automata', multiproc=True, ncores=4, wrap_mode='QUEUE', filename=filename, overwrite_file=True) # extract the trajectory traj = env.traj traj.v_lazy_adding = True traj.par.ncells = 400, 'Number of cells' traj.par.steps = 250, 'Number of timesteps' traj.par.rule_number = 30, 'The ca rule' traj.par.initial_name = 'random', 'The type of initial state' traj.par.seed = 100042, 'RNG Seed' # Explore exp_dict = {'rule_number' : [10, 30, 90, 110, 184], 'initial_name' : ['single', 'random'],} # # You can uncomment the ``exp_dict`` below to see that changing the # # exploration scheme is now really easy: # exp_dict = {'rule_number' : [10, 30, 90, 110, 184], # 'ncells' : [100, 200, 300], # 'seed': [333444555, 123456]} exp_dict = cartesian_product(exp_dict) traj.f_explore(exp_dict) # Run the simulation logger.info('Starting Simulation') env.run(wrap_automaton) # Load all data traj.f_load(load_data=2) logger.info('Printing data') for idx, run_name in enumerate(traj.f_iter_runs()): # Plot all patterns filename = os.path.join(folder, make_filename(traj)) plot_pattern(traj.crun.pattern, traj.rule_number, filename) progressbar(idx, len(traj), logger=logger) # Finally disable logging and close all log-files env.disable_logging()
def explore(traj): explored ={'Normal.trial': [0], 'Numpy.double': [np.array([1.0,2.0,3.0,4.0]), np.array([-1.0,3.0,5.0,7.0])], 'csr_mat' :[spsp.csr_matrix((2222,22)), spsp.csr_matrix((2222,22))]} explored['csr_mat'][0][1,2]=44.0 explored['csr_mat'][1][2,2]=33 traj.f_explore(cartesian_product(explored))
def evalPopulationUsingPypet(self, traj, toolbox, pop, gIdx): """ Eval the population fitness using pypet Params: pop: List of inidivual (population to evaluate) gIdx: Index of the current generation Return: The population with updated fitness values """ # Add as many explored runs as individuals that need to be evaluated. # Furthermore, add the individuals as explored parameters. # We need to convert them to lists or write our own custom IndividualParameter ;-) # Note the second argument to `cartesian_product`: # This is for only having the cartesian product # between ``generation x (ind_idx AND individual)``, so that every individual has just one # unique index within a generation. traj.f_expand( pp.cartesian_product( { "generation": [gIdx], "id": [x.id for x in pop], "individual": [list(x) for x in pop], }, [("id", "individual"), "generation"], )) # the current generation # unique id of each individual # increment the evaluationCounter self.evaluationCounter += len(pop) # run simulations for one generation evolutionResult = toolbox.map(toolbox.evaluate) assert len( evolutionResult) > 0, "No results returned from simulations." for idx, result in enumerate(evolutionResult): runIndex, packedReturnFromEvalFunction = result # packedReturnFromEvalFunction is the return from the evaluation function # it has length two, the first is the fitness, second is the model output assert ( len(packedReturnFromEvalFunction) == 2 ), "Evaluation function must return tuple with shape (fitness, output_data)" fitnessesResult, returnedOutputs = packedReturnFromEvalFunction pop[idx].outputs = returnedOutputs # store outputs of simulations in population pop[idx].fitness.values = fitnessesResult # mean fitness value pop[idx].fitness.score = np.nansum( pop[idx].fitness.wvalues) / (len(pop[idx].fitness.wvalues)) return pop
def generate_gain_space(pars, ranges, Ngrid, values=None): if len(pars) != 1 or len(Ngrid) != 1 or len(ranges) != 1: raise ValueError( 'There must be a single parameter, range and space step') r, s = ranges[0], Ngrid[0] if values is None: values = np.linspace(r[0], r[1], s) values = [flatten_list([[[a, b] for a in values] for b in values])] values_dict = dict(zip(pars, values)) space = cartesian_product(values_dict) return space
def xr(self, bold=False): """ Return `xr.Dataset` from the exploration results. :param bold: if True, will load and return only BOLD output :type bold: bool """ assert self.results is not None, "Run `loadResults()` first to populate the results" assert len(self.results) == len(self.dfResults) # create intrisinsic dims for one run timeDictKey, run_coords = self._getCoordsFromRun(self.results[0], bold=bold) dataarrays = [] orig_search_coords = pypet.cartesian_product(self.exploreParameters) for runId, run_result in self.results.items(): # take exploration coordinates for this run expl_coords = {k: v[runId] for k, v in orig_search_coords.items()} outputs = [] run_result = self._filterDictionaryBold(run_result, bold=bold) for key, value in run_result.items(): if key == timeDictKey: continue outputs.append(value) # create DataArray for run only - we need to add exploration coordinates data_temp = xr.DataArray(np.stack(outputs), dims=["output", "space", "time"], coords=run_coords, name="exploration") expand_coords = {} # iterate exploration coordinates for k, v in expl_coords.items(): # if single values, just assing if isinstance(v, (str, float, int)): expand_coords[k] = [v] # if arrays, check whether they can be sqeezed into one value elif isinstance(v, np.ndarray): if np.unique(v).size == 1: # if yes, just assing that one value expand_coords[k] = [float(np.unique(v))] else: # if no, sorry - coordinates cannot be array raise ValueError("Cannot squeeze coordinates") # assing exploration coordinates to the DataArray dataarrays.append(data_temp.expand_dims(expand_coords)) # finally, combine all arrays into one combined = xr.combine_by_coords(dataarrays)["exploration"] if self.parameterSpace.star: combined.attrs = { k: list(self.model.params[k].keys()) for k in orig_search_coords.keys() } return combined
def main(fail=False): try: if compat.python_major == 3: print('Running Python 3, will not use Sumatra.') sumatra_project = None else: sumatra_project = '.' if fail: print('There better be not any diffs.') # Create an environment that handles running with Environment(trajectory='Example1_Quick_And_Not_So_Dirty', filename=os.path.join('experiments', 'HDF5',), file_title='Example1_Quick_And_Not_So_Dirty', comment='The first example!', complib='blosc', small_overview_tables=False, git_repository='.', git_message='Im a message!', git_fail=fail, sumatra_project=sumatra_project, sumatra_reason='Testing!') as env: # Get the trajectory from the environment traj = env.v_trajectory # Add both parameters traj.f_add_parameter('x', 1, comment='Im the first dimension!') traj.f_add_parameter('y', 1, comment='Im the second dimension!') # Explore the parameters with a cartesian product: traj.f_explore(cartesian_product({'x':[1,2,3], 'y':[6,7,8]})) # Run the simulation env.f_run(multiply) # Check that git information was added to the trajectory assert 'config.git.hexsha' in traj assert 'config.git.committed_date' in traj assert 'config.git.message' in traj assert 'config.git.name_rev' in traj print("Python git test successful") # traj.f_expand({'x':[3,3],'y':[42,43]}) # # env.f_run(multiply) except Exception as exc: print(repr(exc)) sys.exit(1)
def explore(self, traj, trials=3): self.explored ={'Normal.trial': range(trials), 'Numpy.double': [np.array([1.0,2.0,3.0,4.0]), np.array([-1.0,3.0,5.0,7.0])], 'csr_mat' :[spsp.lil_matrix((2222,22)), spsp.lil_matrix((2222,22))]} self.explored['csr_mat'][0][1,2]=44.0 self.explored['csr_mat'][1][2,2]=33 self.explored['csr_mat'][0] = self.explored['csr_mat'][0].tocsr() self.explored['csr_mat'][1] = self.explored['csr_mat'][0].tocsr() traj.f_explore(cartesian_product(self.explored))
def add_exploration(traj): """Explores different values of `I` and `tau_ref`.""" print('Adding exploration of I and tau_ref') explore_dict = {'neuron.I': [0.1, 0.2], 'neuron.tau_ref': [5.0, 7.5, 10.0]} explore_dict = cartesian_product(explore_dict, ('neuron.tau_ref', 'neuron.I')) # The second argument, the tuple, specifies the order of the cartesian product, # The variable on the right most side changes fastest and defines the # 'inner for-loop' of the cartesian product traj.f_explore(explore_dict)
def main(fail=False): try: sumatra_project = '.' if fail: print('There better be not any diffs.') # Create an environment that handles running with Environment(trajectory='Example1_Quick_And_Not_So_Dirty', filename=os.path.join( 'experiments', 'HDF5', ), file_title='Example1_Quick_And_Not_So_Dirty', comment='The first example!', complib='blosc', small_overview_tables=False, git_repository='.', git_message='Im a message!', git_fail=fail, sumatra_project=sumatra_project, sumatra_reason='Testing!') as env: # Get the trajectory from the environment traj = env.v_trajectory # Add both parameters traj.f_add_parameter('x', 1, comment='Im the first dimension!') traj.f_add_parameter('y', 1, comment='Im the second dimension!') # Explore the parameters with a cartesian product: traj.f_explore(cartesian_product({'x': [1, 2, 3], 'y': [6, 7, 8]})) # Run the simulation env.f_run(multiply) # Check that git information was added to the trajectory assert 'config.git.hexsha' in traj assert 'config.git.committed_date' in traj assert 'config.git.message' in traj assert 'config.git.name_rev' in traj print("Python git test successful") # traj.f_expand({'x':[3,3],'y':[42,43]}) # # env.f_run(multiply) except Exception as exc: print(repr(exc)) sys.exit(1)
def add_exploration(traj): """Explores different values of `I` and `tau_ref`.""" print('Adding exploration of I and tau_ref') explore_dict = {'neuron.I': np.arange(0, 1.01, 0.01).tolist(), 'neuron.tau_ref': [5.0, 7.5, 10.0]} explore_dict = cartesian_product(explore_dict, ('neuron.tau_ref', 'neuron.I')) # The second argument, the tuple, specifies the order of the cartesian product, # The variable on the right most side changes fastest and defines the # 'inner for-loop' of the cartesian product traj.f_explore(explore_dict)
def explore(traj): explored = { 'Normal.trial': [0], 'Numpy.double': [np.array([1.0, 2.0, 3.0, 4.0]), np.array([-1.0, 3.0, 5.0, 7.0])], 'csr_mat': [spsp.csr_matrix((2222, 22)), spsp.csr_matrix((2222, 22))] } explored['csr_mat'][0][1, 2] = 44.0 explored['csr_mat'][1][2, 2] = 33 traj.f_explore(cartesian_product(explored))
def main(): """Main function to protect the *entry point* of the program. If you want to use multiprocessing under Windows you need to wrap your main code creating an environment into a function. Otherwise the newly started child processes will re-execute the code and throw errors (also see https://docs.python.org/2/library/multiprocessing.html#windows). """ # Create an environment that handles running. # Let's enable multiprocessing with 2 workers. filename = os.path.join('hdf5', 'example_04.hdf5') env = Environment( trajectory='Example_04_MP', filename=filename, file_title='Example_04_MP', log_stdout=True, comment='Multiprocessing example!', multiproc=True, ncores=4, use_pool=True, # Our runs are inexpensive we can get rid of overhead # by using a pool freeze_input=True, # We can avoid some # overhead by freezing the input to the pool wrap_mode=pypetconstants.WRAP_MODE_QUEUE, graceful_exit=True, # We want to exit in a data friendly way # that safes all results after hitting CTRL+C, try it ;-) overwrite_file=True) # Get the trajectory from the environment traj = env.trajectory # Add both parameters traj.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj.f_add_parameter('y', 1.0, comment='I am the second dimension!') # Explore the parameters with a cartesian product, but we want to explore a bit more traj.f_explore( cartesian_product({ 'x': [float(x) for x in range(20)], 'y': [float(y) for y in range(20)] })) # Run the simulation env.run(multiply) # Finally disable logging and close all log-files env.disable_logging()
def main(): """Main function to protect the *entry point* of the program. If you want to use multiprocessing under Windows you need to wrap your main code creating an environment into a function. Otherwise the newly started child processes will re-execute the code and throw errors (also see https://docs.python.org/2/library/multiprocessing.html#windows). """ # Create an environment that handles running. # Let's enable multiprocessing with 2 workers. filename = os.path.join('hdf5', 'example_04.hdf5') env = Environment(trajectory='Example_04_MP', filename=filename, file_title='Example_04_MP', log_stdout=True, comment='Multiprocessing example!', multiproc=True, ncores=4, use_pool=True, # Our runs are inexpensive we can get rid of overhead # by using a pool freeze_input=True, # We can avoid some # overhead by freezing the input to the pool wrap_mode=pypetconstants.WRAP_MODE_QUEUE, graceful_exit=True, # We want to exit in a data friendly way # that safes all results after hitting CTRL+C, try it ;-) overwrite_file=True) # Get the trajectory from the environment traj = env.trajectory # Add both parameters traj.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj.f_add_parameter('y', 1.0, comment='I am the second dimension!') # Explore the parameters with a cartesian product, but we want to explore a bit more traj.f_explore(cartesian_product({'x':[float(x) for x in range(20)], 'y':[float(y) for y in range(20)]})) # Run the simulation env.run(multiply) # Finally disable logging and close all log-files env.disable_logging()
def main(): """Main function to protect the *entry point* of the program. If you want to use multiprocessing with SCOOP you need to wrap your main code creating an environment into a function. Otherwise the newly started child processes will re-execute the code and throw errors (also see http://scoop.readthedocs.org/en/latest/usage.html#pitfalls). """ # Create an environment that handles running. # Let's enable multiprocessing with scoop: filename = os.path.join('hdf5', 'example_21.hdf5') env = Environment(trajectory='Example_21_SCOOP', filename=filename, file_title='Example_21_SCOOP', log_stdout=True, comment='Multiprocessing example using SCOOP!', multiproc=True, freeze_input=True, # We want to save overhead and freeze input use_scoop=True, # Yes we want SCOOP! wrap_mode=pypetconstants.WRAP_MODE_LOCAL, # SCOOP only works with 'LOCAL' # or 'NETLOCK' wrapping overwrite_file=True) # Get the trajectory from the environment traj = env.trajectory # Add both parameters traj.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj.f_add_parameter('y', 1.0, comment='I am the second dimension!') # Explore the parameters with a cartesian product, but we want to explore a bit more traj.f_explore(cartesian_product({'x':[float(x) for x in range(20)], 'y':[float(y) for y in range(20)]})) # Run the simulation env.run(multiply) # Let's check that all runs are completed! assert traj.f_is_completed() # Finally disable logging and close all log-files env.disable_logging()
def main(): # Create an environment that handles running filename = os.path.join('hdf5', 'example_12.hdf5') env = Environment( trajectory='Multiplication', filename=filename, file_title='Example_12_Sharing_Data', overwrite_file=True, comment='The first example!', continuable= False, # We have shared data in terms of a multiprocessing list, # so we CANNOT use the continue feature. multiproc=True, ncores=2) # The environment has created a trajectory container for us traj = env.trajectory # Add both parameters traj.f_add_parameter('x', 1, comment='I am the first dimension!') traj.f_add_parameter('y', 1, comment='I am the second dimension!') # Explore the parameters with a cartesian product traj.f_explore(cartesian_product({'x': [1, 2, 3, 4], 'y': [6, 7, 8]})) # We want a shared list where we can put all out results in. We use a manager for this: result_list = mp.Manager().list() # Let's make some space for potential results result_list[:] = [0 for _dummy in range(len(traj))] # Run the simulation env.run(multiply, result_list) # Now we want to store the final list as numpy array traj.f_add_result('z', np.array(result_list)) # Finally let's print the result to see that it worked print(traj.z) #Disable logging and close all log-files env.disable_logging()
def main(): # Create an environment that handles running filename = os.path.join('hdf5', 'example_12.hdf5') env = Environment(trajectory='Multiplication', filename=filename, file_title='Example_12_Sharing_Data', overwrite_file=True, comment='The first example!', continuable=False, # We have shared data in terms of a multiprocessing list, # so we CANNOT use the continue feature. multiproc=True, ncores=2) # The environment has created a trajectory container for us traj = env.trajectory # Add both parameters traj.f_add_parameter('x', 1, comment='I am the first dimension!') traj.f_add_parameter('y', 1, comment='I am the second dimension!') # Explore the parameters with a cartesian product traj.f_explore(cartesian_product({'x':[1,2,3,4], 'y':[6,7,8]})) # We want a shared list where we can put all out results in. We use a manager for this: result_list = mp.Manager().list() # Let's make some space for potential results result_list[:] =[0 for _dummy in range(len(traj))] # Run the simulation env.run(multiply, result_list) # Now we want to store the final list as numpy array traj.f_add_result('z', np.array(result_list)) # Finally let's print the result to see that it worked print(traj.z) #Disable logging and close all log-files env.disable_logging()
def grid_search_dict(pars, ranges, Ngrid, values=None): if values is not None: values_dict = dict(zip(pars, values)) else: Npars, Nsteps = len(pars), len(Ngrid) if any([type(s) != int for s in Ngrid]): raise ValueError('Parameter space steps are not integers') if Npars != Nsteps: if Nsteps == 1: Ngrid = [Ngrid[0]] * Npars print('Using the same step for all parameters') else: raise ValueError( 'Number of parameter space steps does not match number of parameters and is not one' ) if np.isnan(ranges).any(): raise ValueError('Ranges of parameters not provided') values_dict = {} for par, (low, high), s in zip(pars, ranges, Ngrid): range = np.linspace(low, high, s).tolist() values_dict.update({par: range}) space = cartesian_product(values_dict) return space
values = [a * Nv for a in values] + flatten_list([[v] * Nspace for v in vs]) returned_params += [p] space = dict(zip(returned_params, values)) return space if __name__ == '__main__': # This will execute the main function in case the script is called from the one true # main process and not from a child processes spawned by your environment. # Necessary for multiprocessing under Windows. batch_id = 'template' batch_idx = 0 space = cartesian_product({ 'fly_params.sensorimotor_params.torque_coef': [0.08, 0.09], 'fly_params.sensorimotor_params.ang_damping': [0.6, 0.3] }) batch_run(batch_id=batch_id, batch_idx=batch_idx, space=space) def PI_computation(traj, dataset): arena_xdim_in_mm = traj.parameters.env_params.arena_params.arena_xdim * 1000 ind = dataset.compute_preference_index( arena_diameter_in_mm=arena_xdim_in_mm) traj.f_add_result('PI', ind, comment=f'The preference index') return dataset, ind def heat_map_generation(traj): path = traj.config.dir_path csv_filepath = f'{path}/PIs.csv'
traj.f_add_result('positions', sim.positions, comment='End positions of particles') traj.f_add_result('t', sim.t, comment='duration of flight') env = Environment(trajectory='FanSimulation', filename='./pypet/', large_overview_tables=True, add_time=True, multiproc=False, ncores=6, log_config='DEFAULT') traj = env.v_trajectory add_parameters(traj, dt=1e-2) explore_dict = {'vent_radius':[0.1, 0.5, 1.0], 'vmax':[10, 50, 100], 'incline':[0.1, 1.0, 5.0]} to_explore = cartesian_product(explore_dict) traj.f_explore(to_explore) env.f_run(run_simulation) env.f_disable_logging() # In[ ]:
pp.check_dir(save_path, overwrite=False) print_dict = parameter_dict.copy() print_dict.update(explore_dict) """ create pypet environment """ env = pypet.Environment(trajectory='explore_perf', log_stdout=False, add_time=False, multiproc=True, ncores=12, filename=os.path.join(save_path, 'explore_perf.hdf5')) traj = env.v_trajectory pp.add_parameters(traj, parameter_dict) explore_dict = pypet.cartesian_product( explore_dict, tuple(explore_dict.keys()) ) #if not all entry of dict need be explored through cartesian product replace tuple(.) only with relevant dict keys in tuple explore_dict['name'] = pp.set_run_names(explore_dict, parameter_dict['name']) traj.f_explore(explore_dict) """ launch simulation with pypet for parameter exploration """ tic = time.time() env.f_run(pp.launch_exploration, images_train, labels_train, images_test, labels_test, save_path) toc = time.time() """ save parameters to file """ helper.print_params(print_dict, save_path, runtime=toc - tic) """ plot results """ name_best = pp.plot_results(folder_path=save_path) pp.faceting(save_path)
def explore_large(self, traj): self.explored ={'Normal.trial': [0,1]} traj.f_explore(cartesian_product(self.explored))
comment='I am going to be merged into some other trajectory!') # Get the trajectories from the environment traj1 = env1.trajectory traj2 = env2.trajectory # Add both parameters traj1.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj1.f_add_parameter('y', 1.0, comment='I am the second dimension!') traj2.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj2.f_add_parameter('y', 1.0, comment='I am the second dimension!') # Explore the parameters with a cartesian product for the first trajectory: traj1.f_explore( cartesian_product({ 'x': [1.0, 2.0, 3.0, 4.0], 'y': [6.0, 7.0, 8.0] })) # Let's explore slightly differently for the second: traj2.f_explore( cartesian_product({ 'x': [3.0, 4.0, 5.0, 6.0], 'y': [7.0, 8.0, 9.0] })) # Run the simulations with all parameter combinations env1.run(multiply) env2.run(multiply) # Now we merge them together into traj1 # We want to remove duplicate entries # like the parameter space point x=3.0, y=7.0.
def main(): env = Environment(trajectory='deap', overwrite_file=True, multiproc=True, ncores=4, log_level=50, # only display ERRORS log_stdout=False, wrap_mode='QUEUE', use_pool=True, freeze_input=True, # To avoid copying the trajectory for each run automatic_storing=False, # This is important, we want to run several # batches with the Environment so we want to avoid re-storing all # data over and over again to save some overhead. comment='Using pypet and DEAP' ) traj = env.traj # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100, comment='Population size') traj.f_add_parameter('CXPB', 0.5, comment='Crossover term') traj.f_add_parameter('MUTPB', 0.2, comment='Mutation probability') traj.f_add_parameter('NGEN', 20, comment='Number of generations') traj.f_add_parameter('generation', 0, comment='Current generation') traj.f_add_parameter('ind_idx', 0, comment='Index of individual') traj.f_add_parameter('ind_len', 50, comment='Length of individual') traj.f_add_parameter('indpb', 0.005, comment='Mutation parameter') traj.f_add_parameter('tournsize', 3, comment='Selection parameter') traj.f_add_parameter('seed', 42, comment='Seed for RNG') # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) toolbox.register("evaluate", eval_one_max) toolbox.register("map", env.run_map) # Important to use `run_map` instead of `run` # because we pass an iterable argument to our fitness function # ------- Initialize Population -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN print("Start of evolution") for g in range(traj.NGEN): # ------- Evaluate current generation -------- # print("-- Generation %i --" % g) # Determine individuals that need to be evaluated eval_pop = [ind for ind in pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated traj.f_expand(cartesian_product({'generation': [g], 'ind_idx': range(len(eval_pop))})) fitnesses_results = toolbox.map(toolbox.evaluate, eval_pop) # evaluate using our fitness function # fitnesses_results is a list of # a nested tuple: [(run_idx, (fitness,)), ...] for idx, result in enumerate(fitnesses_results): # Update fitnesses_results _, fitness = result # The environment returns tuples: [(run_idx, run), ...] eval_pop[idx].fitness.values = fitness print(" Evaluated %i individuals" % len(fitnesses_results)) # Gather all the fitnesses_results in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if g < traj.NGEN -1: # not necessary for the last generation # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring pop[:] = offspring print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) traj.f_store() # We switched off automatic storing, so we need to store manually
def main(): env = Environment( trajectory='faster_deap', overwrite_file=True, multiproc=False, log_stdout=False, log_level=50, # only display ERRORS automatic_storing=False, # This is important, we want to run several # batches with the Environment so we want to avoid re-storing all # data over and over again to save some overhead. comment='Using pypet and DEAP with less overhead') traj = env.traj # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100, comment='Population size') traj.f_add_parameter('CXPB', 0.5, comment='Crossover term') traj.f_add_parameter('MUTPB', 0.2, comment='Mutation probability') traj.f_add_parameter('NGEN', 20, comment='Number of generations') traj.f_add_parameter('generation', 0, comment='Current generation') traj.f_add_parameter('ind_idx', 0, comment='Index of individual') traj.f_add_parameter('ind_len', 50, comment='Length of individual') traj.f_add_parameter('indpb', 0.005, comment='Mutation parameter') traj.f_add_parameter('tournsize', 3, comment='Selection parameter') traj.f_add_parameter('seed', 42, comment='Seed for RNG') # Placeholders for individuals and results that are about to be explored traj.f_add_derived_parameter('individual', [0 for x in range(traj.ind_len)], 'An indivudal of the population') traj.f_add_result('fitnesses', [], comment='Fitnesses of all individuals') # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) toolbox.register("evaluate", eval_one_max) toolbox.register("map", env.run) # We pass the individual as part of traj, so # we no longer need the `run_map` but just `run` # ------- Initialize Population -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN print("Start of evolution") for g in range(traj.NGEN): # ------- Evaluate current generation -------- # print("-- Generation %i --" % g) # Determine individuals that need to be evaluated eval_pop = [ind for ind in pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated. # Furthermore, add the individuals as explored parameters. # We need to convert them to lists or write our own custom IndividualParameter ;-) # Note the second argument to `cartesian_product`: # This is for only having the cartesian product # between ``generation x (ind_idx AND individual)``, so that every individual has just one # unique index within a generation. traj.f_expand( cartesian_product( { 'generation': [g], 'ind_idx': range(len(eval_pop)), 'individual': [list(x) for x in eval_pop] }, [('ind_idx', 'individual'), 'generation'])) fitnesses_results = toolbox.map( toolbox.evaluate) # evaluate using our fitness function # fitnesses_results is a list of # a nested tuple: [(run_idx, (fitness,)), ...] for idx, result in enumerate(fitnesses_results): # Update fitnesses _, fitness = result # The environment returns tuples: [(run_idx, run), ...] eval_pop[idx].fitness.values = fitness # Append all fitnesses (note that DEAP fitnesses are tuples of length 1 # but we are only interested in the value) traj.fitnesses.extend([x.fitness.values[0] for x in eval_pop]) print(" Evaluated %i individuals" % len(fitnesses_results)) # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x * x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if g < traj.NGEN - 1: # not necessary for the last generation # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring pop[:] = offspring print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) traj.f_store( ) # We switched off automatic storing, so we need to store manually
def main(): # No environment here ;-) filename = os.path.join('experiments', 'example_20.hdf5') traj = Trajectory('onemax', filename=filename, overwrite_file=True) # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100) traj.f_add_parameter('CXPB', 0.5) traj.f_add_parameter('MUTPB', 0.2) traj.f_add_parameter('NGEN', 20) traj.f_add_parameter('generation', 0) traj.f_add_parameter('ind_idx', 0) traj.f_add_parameter('ind_len', 50) traj.f_add_parameter('indpb', 0.005) traj.f_add_parameter('tournsize', 3) traj.f_add_parameter('seed', 42) traj.f_store(only_init=True) # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) toolbox.register("evaluate", eval_wrapper) pool = multip.Pool(4) toolbox.register("map", pool.map) # We use the pool's map function! # ------- Initialize Population -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN start_idx = 0 # We need to count executed runs print("Start of evolution") for g in range(traj.NGEN): print("-- Generation %i --" % g) # Determine individuals that need to be evaluated eval_pop = [ind for ind in pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated traj.f_expand( cartesian_product({ 'generation': [g], 'ind_idx': range(len(eval_pop)) })) # We need to make the storage service multiprocessing safe mc = MultiprocContext(traj, wrap_mode='QUEUE') mc.f_start() # Create a single iterable to be passed to our fitness function (wrapper). # `yields='copy'` is important, the pool's `map` function will # go over the whole iterator at once and store it in memory. # So for every run we need a copy of the trajectory. # Alternatively, you could use `yields='self'` and use the pool's `imap` function. zip_iterable = izip(traj.f_iter_runs(start_idx, yields='copy'), eval_pop) fitnesses = toolbox.map(eval_wrapper, zip_iterable) # fitnesses is just a list of tuples [(fitness,), ...] for idx, fitness in enumerate(fitnesses): # Update fitnesses eval_pop[idx].fitness.values = fitness # Finalize the multiproc wrapper mc.f_finalize() # Update start index start_idx += len(eval_pop) print(" Evaluated %i individuals" % len(eval_pop)) # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x * x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if g < traj.NGEN - 1: # not necessary for the last generation # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring pop[:] = offspring # Stop the multiprocessing pool pool.close() pool.join() print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) traj.f_store() # And store all the rest of the data
FFcentralEval = FFfromLCSM.FF(q, CentralCoeff) # Create an environment env = Environment(overwrite_file=True) # Get the trajectory from the environment traj = env.traj # Add parameters traj.f_add_parameter('a1', np.float64(1), comment='First dimension') traj.f_add_parameter('a2', np.float64(1), comment='Second dimension') traj.f_add_parameter('a3', np.float64(1), comment='Third dimension') traj.f_explore( cartesian_product({ 'a1': [MinCoeff[0], CentralCoeff[i][0], MaxCoeff[0]], 'a2': [MinCoeff[1], CentralCoeff[i][1], MaxCoeff[1]], 'a3': [MinCoeff[2], CentralCoeff[i][2], MaxCoeff[2]] })) # Define the observable in the par. space def scan(traj): CentralCoeff[i][0] = traj.a1 CentralCoeff[i][1] = traj.a2 CentralCoeff[i][2] = traj.a3 return (FFfromLCSM.FF(q, CentralCoeff)) Result = env.run(scan) #Results is a sequence of lists, we want to put in a nicer form, #tensor of dimension (N^ParameterSpacePoints, N^qvalues, N^FFs) Results = np.zeros((len(Result), len(q), len(Uncertainties)))
def explore_large(self, traj): self.explored = {'Normal.trial': [0, 1]} traj.f_explore(cartesian_product(self.explored))
def __call__(self, traj, fitnesses_results): """Implements post-processing""" CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN while fitnesses_results: result = fitnesses_results.pop() # Update fitnesses run_index, fitness = result # The environment returns tuples: [(run_idx, run), ...] # We need to convert the current run index into an ind_idx # (index of individual within one generation) traj.v_idx = run_index ind_index = traj.par.ind_idx # Use the ind_idx to update the fintess self.eval_pop[ind_index].fitness.values = fitness traj.v_idx = -1 # set the trajectory back to default # Append all fitnesses (note that DEAP fitnesses are tuples of length 1 # but we are only interested in the value) traj.fitnesses.extend([x.fitness.values[0] for x in self.eval_pop]) print(" Evaluated %i individuals" % len(fitnesses_results)) # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in self.pop] length = len(self.pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if self.g < traj.NGEN -1: # not necessary for the last generation # Select the next generation individuals offspring = self.toolbox.select(self.pop, len(self.pop)) # Clone the selected individuals offspring = list(map(self.toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: self.toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: self.toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring self.pop[:] = offspring self.eval_pop = [ind for ind in self.pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated. # Furthermore, add the individuals as explored parameters. # We need to convert them to lists or write our own custom IndividualParameter ;-) # Note the second argument to `cartesian_product`: # This is for only having the cartesian product # between ``generation x (ind_idx AND individual)``, # so that every individual has just one # unique index within a generation. self.g += 1 # Update generation counter traj.f_expand(cartesian_product({'generation': [self.g], 'ind_idx': range(len(self.eval_pop)), 'individual':[list(x) for x in self.eval_pop]}, [('ind_idx', 'individual'),'generation']))
def main(): env = Environment(trajectory='postproc_deap', overwrite_file=True, log_stdout=False, log_level=50, # only display ERRORS automatic_storing=True, # Since we us post-processing, we # can safely enable automatic storing, because everything will # only be stored once at the very end of all runs. comment='Using pypet and DEAP with less overhead' ) traj = env.traj # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100, comment='Population size') traj.f_add_parameter('CXPB', 0.5, comment='Crossover term') traj.f_add_parameter('MUTPB', 0.2, comment='Mutation probability') traj.f_add_parameter('NGEN', 20, comment='Number of generations') traj.f_add_parameter('generation', 0, comment='Current generation') traj.f_add_parameter('ind_idx', 0, comment='Index of individual') traj.f_add_parameter('ind_len', 50, comment='Length of individual') traj.f_add_parameter('indpb', 0.005, comment='Mutation parameter') traj.f_add_parameter('tournsize', 3, comment='Selection parameter') traj.f_add_parameter('seed', 42, comment='Seed for RNG') # Placeholders for individuals and results that are about to be explored traj.f_add_derived_parameter('individual', [0 for x in range(traj.ind_len)], 'An indivudal of the population') traj.f_add_result('fitnesses', [], comment='Fitnesses of all individuals') # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) # ------- Initialize Population and Trajectory -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) eval_pop = [ind for ind in pop if not ind.fitness.valid] traj.f_explore(cartesian_product({'generation': [0], 'ind_idx': range(len(eval_pop)), 'individual':[list(x) for x in eval_pop]}, [('ind_idx', 'individual'),'generation'])) # ----------- Add postprocessing ------------------ # postproc = Postprocessing(pop, eval_pop, toolbox) # Add links to important structures env.add_postprocessing(postproc) # ------------ Run applying post-processing ---------- # env.run(eval_one_max) # ------------ Finished all runs and print result --------------- # print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
print_dict.update(explore_dict) print_dict.update({'images_params':images_params}) """ create pypet environment """ env = pypet.Environment(trajectory = 'explore_perf', log_stdout = False, add_time = False, multiproc = True, ncores = 16, filename = os.path.join(save_path, 'explore_perf.hdf5')) traj = env.v_trajectory pp.add_parameters(traj, parameter_dict) explore_dict = pypet.cartesian_product(explore_dict, tuple(explore_dict.keys())) #if not all entry of dict need be explored through cartesian product replace tuple(.) only with relevant dict keys in tuple explore_dict['name'] = pp.set_run_names(explore_dict, parameter_dict['name']) traj.f_explore(explore_dict) """ launch simulation with pypet for parameter exploration """ tic = time.time() env.f_run(pp.launch_exploration, images_dict, labels_dict, images_params, save_path) toc = time.time() """ save parameters to file """ save_file = os.path.join(save_path, parameter_dict['name'] + '_params.txt') ex.print_params(print_dict, save_file, runtime=toc-tic) """ plot results """ name_best = pp.plot_results(folder_path=save_path)
def main(): env = Environment(trajectory='faster_deap', overwrite_file=True, multiproc=False, log_stdout=False, log_level=50, # only display ERRORS automatic_storing=False, # This is important, we want to run several # batches with the Environment so we want to avoid re-storing all # data over and over again to save some overhead. comment='Using pypet and DEAP with less overhead' ) traj = env.traj # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100, comment='Population size') traj.f_add_parameter('CXPB', 0.5, comment='Crossover term') traj.f_add_parameter('MUTPB', 0.2, comment='Mutation probability') traj.f_add_parameter('NGEN', 20, comment='Number of generations') traj.f_add_parameter('generation', 0, comment='Current generation') traj.f_add_parameter('ind_idx', 0, comment='Index of individual') traj.f_add_parameter('ind_len', 50, comment='Length of individual') traj.f_add_parameter('indpb', 0.005, comment='Mutation parameter') traj.f_add_parameter('tournsize', 3, comment='Selection parameter') traj.f_add_parameter('seed', 42, comment='Seed for RNG') # Placeholders for individuals and results that are about to be explored traj.f_add_derived_parameter('individual', [0 for x in range(traj.ind_len)], 'An indivudal of the population') traj.f_add_result('fitnesses', [], comment='Fitnesses of all individuals') # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) toolbox.register("evaluate", eval_one_max) toolbox.register("map", env.run) # We pass the individual as part of traj, so # we no longer need the `run_map` but just `run` # ------- Initialize Population -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN print("Start of evolution") for g in range(traj.NGEN): # ------- Evaluate current generation -------- # print("-- Generation %i --" % g) # Determine individuals that need to be evaluated eval_pop = [ind for ind in pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated. # Furthermore, add the individuals as explored parameters. # We need to convert them to lists or write our own custom IndividualParameter ;-) # Note the second argument to `cartesian_product`: # This is for only having the cartesian product # between ``generation x (ind_idx AND individual)``, so that every individual has just one # unique index within a generation. traj.f_expand(cartesian_product({'generation': [g], 'ind_idx': range(len(eval_pop)), 'individual':[list(x) for x in eval_pop]}, [('ind_idx', 'individual'),'generation'])) fitnesses_results = toolbox.map(toolbox.evaluate) # evaluate using our fitness function # fitnesses_results is a list of # a nested tuple: [(run_idx, (fitness,)), ...] for idx, result in enumerate(fitnesses_results): # Update fitnesses _, fitness = result # The environment returns tuples: [(run_idx, run), ...] eval_pop[idx].fitness.values = fitness # Append all fitnesses (note that DEAP fitnesses are tuples of length 1 # but we are only interested in the value) traj.fitnesses.extend([x.fitness.values[0] for x in eval_pop]) print(" Evaluated %i individuals" % len(fitnesses_results)) # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if g < traj.NGEN -1: # not necessary for the last generation # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring pop[:] = offspring print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) traj.f_store() # We switched off automatic storing, so we need to store manually
"""Sophisticated simulation of multiplication""" z=traj.x*traj.y traj.f_add_result('z',z=z, comment='I am the product of two reals!') # Create an environment that handles running. # Let's enable multiprocessing with 2 workers. env = Environment(trajectory='Example_04_MP', filename='experiments/example_04/HDF5/example_04.hdf5', file_title='Example_04_MP', log_folder='experiments/example_04/LOGS/', comment = 'Multiprocessing example!', multiproc=True, ncores=2, use_pool=True, wrap_mode=pypetconstants.WRAP_MODE_LOCK) # Get the trajectory from the environment traj = env.v_trajectory # Add both parameters traj.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj.f_add_parameter('y', 1.0, comment='I am the second dimension!') # Explore the parameters with a cartesian product, but we want to explore a bit more traj.f_explore(cartesian_product({'x':[float(x) for x in range(15)], 'y':[float(y) for y in range(15)]})) # Run the simulation env.f_run(multiply)
# Create an environment that handles running filename = os.path.join('hdf5', 'example_08.hdf5') env = Environment(trajectory='Example08',filename=filename, file_title='Example08', overwrite_file=True, comment='Another example!') # Get the trajectory from the environment traj = env.trajectory # Add both parameters traj.f_add_parameter('x', 1, comment='I am the first dimension!') traj.f_add_parameter('y', 1, comment='I am the second dimension!') # Explore the parameters with a cartesian product: traj.f_explore(cartesian_product({'x':[1,2,3,4], 'y':[6,7,8]})) # Run the simulation env.run(multiply) # We load all results traj.f_load(load_results=pypetconstants.LOAD_DATA) # And now we want to find som particular results, the ones where x was 2 or y was 8. # Therefore, we use a lambda function my_filter_predicate= lambda x,y: x==2 or y==8 # We can now use this lambda function to search for the run indexes associated with x==2 OR y==8. # We need a list specifying the names of the parameters and the predicate to do this. # Note that names need to be in the order as listed in the lambda function, here 'x' and 'y': idx_iterator = traj.f_find_idx(['x','y'], my_filter_predicate)
from pypet import Environment, cartesian_product def multiply(traj): """Example of a sophisticated numerical experiment that involves multiplying two integer values. :param traj: Trajectory containing the parameters in a particular combination, it also serves as a container for results. """ z = traj.x * traj.y traj.f_add_result('z', z, comment='Result of x*y') # Create an environment that handles running the experiment env = Environment(trajectory='Multiplication', filename='multiply.hdf5', comment='A simulation of multiplication') # The environment provides a trajectory container for us traj = env.v_trajectory # Add two parameters, both with default value 0 traj.f_add_parameter('x', 0, comment='First dimension') traj.f_add_parameter('y', 0, comment='Second dimension') # Explore the Cartesian product of x in {1,2,3,4} and y in {6,7,8} traj.f_explore(cartesian_product({'x': [1, 2, 3, 4], 'y': [6, 7, 8]})) # Run simulation function `multiply` with all parameter combinations env.f_run(multiply)
traj = env.trajectory traj.f_add_parameter('loc', np.float64(1)) traj.f_add_parameter( 'std', np.float64(1), comment='Standard deviation of random number distribution') traj.f_add_parameter( 'size', np.float64(1), comment='Number of random numbers per data point to generate') traj.f_explore( cartesian_product({ 'std': np.logspace(-2, 4, 7), 'size': np.ones(10) })) env.run(generateRandomNumbers) plotResultsKeys = { 'results std': { 'varName': 'value', 'function': np.std, 'name': 'results error std', 'plot': 'line' }, 'results scatter': { 'varName': 'value', 'function': np.abs, 'name': 'result scatter',
def main(): env = Environment( trajectory='deap', overwrite_file=True, multiproc=True, ncores=4, log_level=50, # only display ERRORS log_stdout=False, wrap_mode='QUEUE', use_pool=True, freeze_input=True, # To avoid copying the trajectory for each run automatic_storing=False, # This is important, we want to run several # batches with the Environment so we want to avoid re-storing all # data over and over again to save some overhead. comment='Using pypet and DEAP') traj = env.traj # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100, comment='Population size') traj.f_add_parameter('CXPB', 0.5, comment='Crossover term') traj.f_add_parameter('MUTPB', 0.2, comment='Mutation probability') traj.f_add_parameter('NGEN', 20, comment='Number of generations') traj.f_add_parameter('generation', 0, comment='Current generation') traj.f_add_parameter('ind_idx', 0, comment='Index of individual') traj.f_add_parameter('ind_len', 50, comment='Length of individual') traj.f_add_parameter('indpb', 0.005, comment='Mutation parameter') traj.f_add_parameter('tournsize', 3, comment='Selection parameter') traj.f_add_parameter('seed', 42, comment='Seed for RNG') # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) toolbox.register("evaluate", eval_one_max) toolbox.register( "map", env.run_map) # Important to use `run_map` instead of `run` # because we pass an iterable argument to our fitness function # ------- Initialize Population -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN print("Start of evolution") for g in range(traj.NGEN): # ------- Evaluate current generation -------- # print("-- Generation %i --" % g) # Determine individuals that need to be evaluated eval_pop = [ind for ind in pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated traj.f_expand( cartesian_product({ 'generation': [g], 'ind_idx': range(len(eval_pop)) })) fitnesses_results = toolbox.map( toolbox.evaluate, eval_pop) # evaluate using our fitness function # fitnesses_results is a list of # a nested tuple: [(run_idx, (fitness,)), ...] for idx, result in enumerate(fitnesses_results): # Update fitnesses_results _, fitness = result # The environment returns tuples: [(run_idx, run), ...] eval_pop[idx].fitness.values = fitness print(" Evaluated %i individuals" % len(fitnesses_results)) # Gather all the fitnesses_results in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x * x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if g < traj.NGEN - 1: # not necessary for the last generation # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring pop[:] = offspring print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) traj.f_store( ) # We switched off automatic storing, so we need to store manually
from brian2.units import ms, mV, second, Hz from pypet import cartesian_product input_dict = {'crs_crrs_rec': [0], 'syn_scl_rec' : [0], 'syn_iscl_rec' : [0], 'synEE_rec': [0], 'synEI_rec': [0], 'syn_noise': [0], 'synee_activetraces_rec' : [0], 'synee_Apretraces_rec': [0], 'synee_Aposttraces_rec': [0], 'synee_a_nrecpoints': [1], 'synei_a_nrecpoints': [1], 'synEEdynrec': [1], 'synEIdynrec': [1], 'turnover_rec': [0], 'syn_cond_mode': ['biexp'], 'syn_cond_mode_EI': ['biexp'], 'tau_e_rise': [3.5*ms], 'tau_i_rise': [0.75*ms], 'norm_f_EE' : [2.1], 'norm_f_EI' : [1.0,2.1]} name = 'test_standard_net' explore_dict = cartesian_product(input_dict)
filename = os.path.join('hdf5', 'example_10.hdf5') env = Environment(trajectory='Example10', filename=filename, file_title='Example10', comment='Another example!') # Get the trajectory from the environment traj = env.v_trajectory # Add both parameters traj.f_add_parameter('x', 1, comment='I am the first dimension!') traj.f_add_parameter('y', 1, comment='I am the second dimension!') # Explore the parameters with a cartesian product: x_length = 12 y_length = 12 traj.f_explore(cartesian_product({'x': range(x_length), 'y': range(y_length)})) # Run the simulation env.f_run(multiply) # We load all results traj.f_load(load_results=pypetconstants.LOAD_DATA) # We access the ranges for plotting xs = traj.f_get('x').f_get_range() ys = traj.f_get('y').f_get_range() # Now we want to directly get all numbers z from all runs # for plotting. # We use `fast_access=True` to directly get access to # the values.
def main(): # No environment here ;-) filename = os.path.join('experiments', 'example_20.hdf5') traj = Trajectory('onemax', filename=filename, overwrite_file=True) # ------- Add parameters ------- # traj.f_add_parameter('popsize', 100) traj.f_add_parameter('CXPB', 0.5) traj.f_add_parameter('MUTPB', 0.2) traj.f_add_parameter('NGEN', 20) traj.f_add_parameter('generation', 0) traj.f_add_parameter('ind_idx', 0) traj.f_add_parameter('ind_len', 50) traj.f_add_parameter('indpb', 0.005) traj.f_add_parameter('tournsize', 3) traj.f_add_parameter('seed', 42) traj.f_store(only_init=True) # ------- Create and register functions with DEAP ------- # creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # Attribute generator toolbox.register("attr_bool", random.randint, 0, 1) # Structure initializers toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, traj.ind_len) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # Operator registering toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=traj.indpb) toolbox.register("select", tools.selTournament, tournsize=traj.tournsize) toolbox.register("evaluate", eval_wrapper) pool = multip.Pool(4) toolbox.register("map", pool.map) # We use the pool's map function! # ------- Initialize Population -------- # random.seed(traj.seed) pop = toolbox.population(n=traj.popsize) CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.NGEN start_idx = 0 # We need to count executed runs print("Start of evolution") for g in range(traj.NGEN): print("-- Generation %i --" % g) # Determine individuals that need to be evaluated eval_pop = [ind for ind in pop if not ind.fitness.valid] # Add as many explored runs as individuals that need to be evaluated traj.f_expand(cartesian_product({'generation': [g], 'ind_idx': range(len(eval_pop))})) # We need to make the storage service multiprocessing safe mc = MultiprocContext(traj, wrap_mode='QUEUE') mc.f_start() # Create a single iterable to be passed to our fitness function (wrapper). # `yields='copy'` is important, the pool's `map` function will # go over the whole iterator at once and store it in memory. # So for every run we need a copy of the trajectory. # Alternatively, you could use `yields='self'` and use the pool's `imap` function. zip_iterable = izip(traj.f_iter_runs(start_idx, yields='copy'), eval_pop) fitnesses = toolbox.map(eval_wrapper, zip_iterable) # fitnesses is just a list of tuples [(fitness,), ...] for idx, fitness in enumerate(fitnesses): # Update fitnesses eval_pop[idx].fitness.values = fitness # Finalize the multiproc wrapper mc.f_finalize() # Update start index start_idx += len(eval_pop) print(" Evaluated %i individuals" % len(eval_pop)) # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) # ------- Create the next generation by crossover and mutation -------- # if g < traj.NGEN -1: # not necessary for the last generation # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # The population is entirely replaced by the offspring pop[:] = offspring # Stop the multiprocessing pool pool.close() pool.join() print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) traj.f_store() # And store all the rest of the data
#print(SMpred) # Create an environment env = Environment(overwrite_file=True) # Get the trajectory from the environment traj = env.traj # Add parameters traj.f_add_parameter('A0', [1.,1., 1.] , comment = 'First dimension') traj.f_add_parameter('A1', [1.,1., 1.], comment = 'Second dimension') #traj.f_add_parameter('A2bF', 1., comment = 'Second dimension') #traj.f_add_parameter('T1bF', 1., comment = 'Third dimension') #traj.f_add_parameter('T2bF', 1., comment = 'Fourth dimension') #traj.f_add_parameter('T3bF', 1., comment = 'Fifth dimension') traj.f_explore(cartesian_product ({'A0' : [ [0.002, 0.465, 1.222], [0.002, 0.715, 1.724]], #min and max values 'A1' : [ [-0.038, -0.074, 0.179], [0.012, -0.038, 0.137]] })) # 'A2bF' : [-0.127, -0.083], # 'T1bF' : [-0.066, 0.042], #'T2bF' : [0.196, 0.11], # 'T3bF' : [0.367, 0.249] # Define the observable in the par. space def scan(traj): import imp imp.reload(P5p_anomaly) P5p_anomaly.Lmb_corr_par['A0'] = traj.A0 P5p_anomaly.Lmb_corr_par['A1'] = traj.A1 # P5p_anomaly.Lmb_corr_par['A2'][1]=traj.A2bF # P5p_anomaly.Lmb_corr_par['T1'][1]=traj.T1bF # P5p_anomaly.Lmb_corr_par['T2'][1]=traj.T2bF
# is enough add_time=True, comment = 'I am going to be merged into some other trajectory!') # Get the trajectories from the environment traj1 = env1.trajectory traj2 = env2.trajectory # Add both parameters traj1.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj1.f_add_parameter('y', 1.0, comment='I am the second dimension!') traj2.f_add_parameter('x', 1.0, comment='I am the first dimension!') traj2.f_add_parameter('y', 1.0, comment='I am the second dimension!') # Explore the parameters with a cartesian product for the first trajectory: traj1.f_explore(cartesian_product({'x':[1.0,2.0,3.0,4.0], 'y':[6.0,7.0,8.0]})) # Let's explore slightly differently for the second: traj2.f_explore(cartesian_product({'x':[3.0,4.0,5.0,6.0], 'y':[7.0,8.0,9.0]})) # Run the simulations with all parameter combinations env1.run(multiply) env2.run(multiply) # Now we merge them together into traj1 # We want to remove duplicate entries # like the parameter space point x=3.0, y=7.0. # Several points have been explored by both trajectories and we need them only once. # Therefore, we set remove_duplicates=True (Note this takes O(N1*N2)!). # We also want to backup both trajectories, but we let the system choose the filename. # Accordingly we choose backup_filename=True instead of providing a filename.
SMpred = P5p_anomaly.P5p_binned( ) #Central value prediction (!!Run only once then comment) # Create an environment env = Environment(overwrite_file=True) # Get the trajectory from the environment traj = env.traj # Add parameters traj.f_add_parameter('m_b', 1., comment='First dimension') traj.f_add_parameter('m_c', 1., comment='Second dimension') traj.f_add_parameter('alpha_s_b', 1., comment='Third dimension') traj.f_explore( cartesian_product({ 'm_b': [4.1, 4.3], 'm_c': [1.27, 1.33], 'alpha_s_b': [0.192, 0.235] })) # Define the observable in the par. space def scan(traj): P5p_anomaly.m_b = traj.m_b P5p_anomaly.m_c = traj.m_c P5p_anomaly.alpha_s_b = traj.alpha_s_b return P5p_anomaly.P5p_binned() Result = env.run(scan)