def get_id(paramMap): print gen_string_id(paramMap, key_params=GridWalk.key_params) return gen_hash_id( gen_string_id(paramMap, key_params=GridWalk.key_params))
def get_id(paramMap): return gen_hash_id(gen_string_id(paramMap,key_params=GridInputs.get_key_params(paramMap)))
def __init__(self, paramMap, scale_a_by_density=False, force=False): self.header_str = """ ================================================================= GRID DETAILED RATE SIMULATION =================================================================""" self.force_gen_inputs = False self.force_gen_corr = False self.force = force # general parameters self.dt = None self.sim_time = None self.eta = None self.seed = None self.num_snaps = None # input parameters self.n = None self.input_mean = None self.sigma = None self.inputs_type = None self.periodic_inputs = None self.num_gau_mix = None self.centers_std = None self.inputs_seed = None self.tap_inputs = None self.norm_bound_add = None self.norm_bound_mul = None # walk parameters self.L = None self.nx = None self.periodic_walk = None self.speed = None self.bounce = None self.theta_sigma = None self.position_dt = None self.walk_time = None self.walk_seed = None self.variable_speed = None self.correct_border_effects = None # filter parameters self.filter_type = FilterType.FILTER_INPUT if 'filter_type' not in paramMap.keys( ) or paramMap['filter_type'] == FilterType.FILTER_INPUT: self.tau1 = None self.tau2 = None self.tau3 = None self.mu1 = None self.mu2 = None self.mu3 = None else: self.tau_in = None self.tau_out = None self.mu_out = None # plasticiy params self.a = None self.J_av_target = None self.gamma = None self.up_bound = None self.J0_std = None self.r0 = None self.J0_dist = None self.J0_mean_factor = None self.base_n = None # base number of neurons to scale a accordingly # flags self.add_boundary_input = None self.clip_weights = None self.clip_out_rate = None self.compute_scores = None self.scale_a_by_n = None # set parameter values from input map for param, value in paramMap.items(): setattr(self, param, value) if self.scale_a_by_n is True: self.a = self.a * self.n**2 / self.base_n**2 # parameters we never change self.plastic = True self.transient_time = 2. self.arena_shape = 'square' self.virtual_bound_ratio = 1.0 self.bounce_theta_sigma = 0.0 self.debug_vars = False self.sigmoid_out_rate = False self.r_out_max = 5. self.position_dt = self.L / self.nx if self.periodic_inputs is True: assert (self.add_boundary_input == False) if (self.mu1 + self.mu2 + self.mu3) < 0.: assert (self.gamma == 0.0) else: assert (self.gamma > 0.0) # init parameters map self.initParamMap = { 'arena_shape': self.arena_shape, 'L': self.L, 'n': self.n, 'nx': self.nx, 'sigma': self.sigma, 'input_mean': self.input_mean, 'speed': self.speed, 'theta_sigma': self.theta_sigma, 'seed': self.seed, 'sim_time': self.sim_time, 'dt': self.dt, 'position_dt': self.position_dt, 'num_snaps': self.num_snaps, 'eta': self.eta, 'a': self.a, 'gamma': self.gamma, 'J_av_target': self.J_av_target, 'J0_std': self.J0_std, 'up_bound': self.up_bound, 'r0': self.r0, 'J0_mean_factor': self.J0_mean_factor, 'clip_weights': self.clip_weights, 'periodic_inputs': self.periodic_inputs, 'outside_ratio': self.outside_ratio, 'clip_out_rate': self.clip_out_rate, 'inputs_type': self.inputs_type, 'num_gau_mix': self.num_gau_mix, 'inputs_seed': self.inputs_seed, 'walk_seed': self.walk_seed, 'walk_time': self.walk_time, 'periodic_walk': self.periodic_walk, 'bounce': self.bounce, 'bounce_theta_sigma': self.bounce_theta_sigma, 'virtual_bound_ratio': self.virtual_bound_ratio, 'compute_scores': self.compute_scores, 'add_boundary_input': self.add_boundary_input, 'J0_dist': self.J0_dist, 'sigmoid_out_rate': self.sigmoid_out_rate, 'r_out_max': self.r_out_max, 'centers_std': self.centers_std } if self.filter_type == FilterType.FILTER_INPUT: self.initParamMap = map_merge( self.initParamMap, { 'tau1': self.tau1, 'tau2': self.tau2, 'tau3': self.tau3, 'mu1': self.mu1, 'mu2': self.mu2, 'mu3': self.mu3 }) else: self.initParamMap = map_merge( self.initParamMap, { 'tau_in': self.tau_in, 'tau_out': self.tau_out, 'mu_out': self.mu_out }) if self.variable_speed is True: self.initParamMap = map_merge( self.initParamMap, { 'variable_speed': self.variable_speed, 'speed_theta': self.speed_theta, 'speed_sigma': self.speed_sigma }) if self.correct_border_effects is True: self.initParamMap = map_merge( self.initParamMap, { 'correct_border_effects': self.correct_border_effects, 'border_edge_type': self.border_edge_type, 'border_size_perc': self.border_size_perc }) if self.tap_inputs is True: self.initParamMap = map_merge( self.initParamMap, { 'tap_inputs': self.tap_inputs, 'tap_border_type': self.tap_border_type, 'tap_border_size': self.tap_border_size }) if self.norm_bound_add is True: self.initParamMap = map_merge( self.initParamMap, {'norm_bound_add': self.norm_bound_add}) if self.norm_bound_mul is True: self.initParamMap = map_merge( self.initParamMap, {'norm_bound_mul': self.norm_bound_mul}) # human-readable parameter strings (just for printing) key_params=GridRate.key_params_filter_input if self.filter_type == FilterType.FILTER_INPUT \ else GridRate.key_params_filter_output self.key_params_str = params_to_str(self.initParamMap, keyParams=key_params, compact=True) self.input_params_str = params_to_str( self.initParamMap, keyParams=GridInputs.get_key_params(self.initParamMap), compact=True) self.walk_params_str = params_to_str( self.initParamMap, keyParams=GridWalk.get_key_params(paramMap), compact=True) # generate id and paths self.str_id = gen_string_id(self.initParamMap) self.hash_id = gen_hash_id(self.str_id) self.paramsPath = os.path.join(GridRate.results_path, self.hash_id + '_log.txt') self.dataPath = os.path.join(GridRate.results_path, self.hash_id + '_data.npz') self.figurePath = os.path.join(GridRate.results_path, self.hash_id + '_fig.png') if os.path.exists(self.dataPath): print 'Data hash %s already present' % self.hash_id self.do_run = False else: self.do_run = True
def post_init(self, force=False, do_print=False): # create simulation objects for each simulation to be run self.sims = [] self.hashes = [] self.pars_to_hash_map = {} # in this case we simulate all parameter combinations, i.e. (a,b); (1,2) -> (a,1); (a,2); (b,1); (b,2) if self.all_combinations is True: self.all_par_values = sorted( itertools.product(*self.batch_override_map.values())) # in this case we run one simulation per parameter set, i.e., (a,b); (1,2) -> (a,1); (b,2) else: # check that all parameters have the same number of values all_val_nums = np.array( [len(vals) for vals in self.batch_override_map.values()]) num_vals = all_val_nums[0] assert (np.all(all_val_nums == num_vals)) self.all_par_values = [] for i in xrange(num_vals): par_comb = [] for key in self.batch_override_map.keys(): par_comb.append(self.batch_override_map[key][i]) self.all_par_values.append(par_comb) self.batch_override_str = ' '.join([ '%s (%s-%s)' % (key, sl.format_val(min(values)), sl.format_val(max(values))) for key, values in self.batch_override_map.items() ]) # loop over all combinations of paramater values for par_values in self.all_par_values: override_param_map = { k: v for (k, v) in zip(self.batch_override_map.keys(), par_values) } parMap = sl.map_merge(self.batch_default_map, override_param_map) sim = self.sim_class(parMap) # always run if do_run attribute is not present if not hasattr(sim, 'do_run'): sim.do_run = True #print sim.hash_id+' Run: %s'%sim.do_run if sim.do_run is True or force is True: self.sims.append(sim) self.hashes.append(sim.hash_id) self.pars_to_hash_map[str(par_values)] = sim.hash_id # generate batch hash self.batch_hash = sl.gen_hash_id('_'.join(self.hashes)) self.batch_data_path = os.path.join( self.batch_data_folder, '%s_%s_%s.hd5' % (self.sim_class.__name__, self.batch_hash, self.suffix)) self.batch_params_path = os.path.join( self.batch_data_folder, '%s_%s_%s.txt' % (self.sim_class.__name__, self.batch_hash, self.suffix)) self.batch_summary_str=\ """ BATCH SIMULATION CLASS: %s BATCH HASH: %s BATCH PARAMS = %s"""%\ ( str(self.sim_class), self.batch_hash, self.batch_override_str ) if do_print: print self.batch_summary_str self.toSaveMap = { 'hashes': self.hashes, 'batch_override_map': self.batch_override_map, 'batch_default_map': self.batch_default_map } if os.path.exists(self.batch_data_path) and not force: if do_print: print """ *** BATCH DATA PRESENT *** """ print self.batch_data_path print return False else: if do_print: print """ *** BATCH DATA NOT PRESENT *** """ print self.batch_data_path print print '%d/%d simulations to be run' % (len( self.sims), len(self.all_par_values)) print print 'Parameters' print self.batch_override_map.keys() print for sim, par_values in zip(self.sims, self.all_par_values): print str(par_values) + ' : ' + sim.hash_id return True
def post_init(self): ############################################################################## ###### CREATE POOL ############################################################################## self.batch_data_folder = batch_data_folder_map[self.model_type] ensureDir(self.batch_data_folder) # create pool self.host = socket.gethostname() if self.host in procs_by_host.keys(): self.num_procs = procs_by_host[self.host] else: self.num_procs = 7 self.pool = Pool(processes=self.num_procs) self.sims = [] self.hashes = [] self.all_par_values = sorted( itertools.product(*self.batch_override_map.values())) self.batch_override_str = ' '.join([ '%s (%s-%s)' % (key, format_val(min(values)), format_val(max(values))) for key, values in self.batch_override_map.items() ]) # loop over all different paramater values for par_values in self.all_par_values: override_param_map = { k: v for (k, v) in zip(self.batch_override_map.keys(), par_values) } parMap = map_merge(self.batch_default_map, override_param_map) if self.model_type == ModelType.MODEL_RATE: self.sim_class = GridRate elif self.model_type == ModelType.MODEL_RATE_AVG: self.sim_class = GridRateAvg elif self.model_type == ModelType.MODEL_SPIKING: self.sim_class = GridSpikes sim = self.sim_class(parMap) #print sim.hash_id+' Run: %s'%sim.do_run if self.force: sim.force_gen_inputs = True sim.force_gen_corr = True sim.do_run = True if sim.do_run is True: self.sims.append(sim) self.hashes.append(sim.hash_id) # generate batch hash self.batch_hash = gen_hash_id('_'.join(self.hashes)) self.batch_data_path = os.path.join(self.batch_data_folder, '%s_data.npz' % self.batch_hash) self.batch_params_path = os.path.join( self.batch_data_folder, '%s_params.txt' % self.batch_hash) self.batch_summary_str=\ "\n\nBATCH HASH: %s\n\nBATCH PARAMS = %s\n\n"%\ (self.batch_hash, self.batch_override_str ) print self.batch_summary_str self.toSaveMap = { 'hashes': self.hashes, 'batch_override_map': self.batch_override_map, 'batch_default_map': self.batch_default_map } if os.path.exists(self.batch_data_path) and not self.force: return False else: print '\n\n*** BATCH DATA NOT PRESENT!! ***\n\n' print self.batch_data_path print '%d/%d simulations to be run' % (len( self.sims), len(self.all_par_values)) return True
def get_id(paramMap): string_id = gen_string_id( paramMap, key_params=RandomWalk.get_key_params(paramMap)) #print string_id return gen_hash_id(string_id)