def to_bpo_param(self, attrs): from bluepyopt.parameters import Parameter lop = {} for k, v in attrs.items(): p = Parameter(name=k, bounds=v, frozen=False) lop[k] = p self.param = lop return lop
def to_bpo_param(attrs): from bluepyopt.parameters import Parameter lop = {} for k, v in attrs.items(): temp = tuple(sorted(v)) p = Parameter(name=k, bounds=temp[:], frozen=False) lop[k] = p return lop
def make_evaluator(nu_tests, MODEL_PARAMS, experiment=str('Neocortex pyramidal cell layer 5-6'), model=str('ADEXP')): objectives = [] nu_tests[0].score_type = ZScore simple_cell = ephys.models.ReducedCellModel(name='simple_cell', params=MODEL_PARAMS[model], backend=model) if "GLIF" in model: nu_tests_ = glif_specific_modifications(nu_tests) nu_tests = list(nu_tests_.values()) simple_cell.name = "GLIF" elif "L5PC" in model: nu_tests_ = l5pc_specific_modifications(nu_tests) nu_tests = list(nu_tests_.values()) simple_cell.name = "L5PC" else: simple_cell.name = model + experiment simple_cell.backend = model simple_cell.params = {k: np.mean(v) for k, v in simple_cell.params.items()} lop = {} for k, v in MODEL_PARAMS[model].items(): p = Parameter(name=k, bounds=v, frozen=False) lop[k] = p simple_cell.params = lop for tt in nu_tests: feature_name = tt.name ft = NUFeature_standard_suite(tt, simple_cell) objective = ephys.objectives.SingletonObjective(feature_name, ft) objectives.append(objective) score_calc = ephys.objectivescalculators.ObjectivesCalculator(objectives) sweep_protocols = [] for protocol_name, amplitude in [('step1', 0.05)]: protocol = ephys.protocols.SweepProtocol(protocol_name, [None], [None]) sweep_protocols.append(protocol) twostep_protocol = ephys.protocols.SequenceProtocol( 'twostep', protocols=sweep_protocols) cell_evaluator = ephys.evaluators.CellEvaluator( cell_model=simple_cell, param_names=MODEL_PARAMS[model].keys(), fitness_protocols={twostep_protocol.name: twostep_protocol}, fitness_calculator=score_calc, sim='euler') simple_cell.params_by_names(MODEL_PARAMS[model].keys()) return cell_evaluator, simple_cell, score_calc, [ tt.name for tt in nu_tests ]
def __init__(self, input_current, dt, init_values, parameters, fitness, target_voltage): self.input_current = input_current self.dt = dt self.init_values = init_values self.parameters = parameters self.fitness = fitness self.target_voltage = target_voltage super(Evaluator, self).__init__( objectives=[x for x in fitness.keys()], params=[ Parameter('El', value=parameters['El']['value'], bounds=parameters['El']['bounds'], frozen=parameters['El']['frozen']), Parameter('C', value=parameters['C']['value'], bounds=parameters['C']['bounds'], frozen=parameters['C']['frozen']), Parameter('G', value=parameters['G']['value'], bounds=parameters['G']['bounds'], frozen=parameters['G']['frozen']), Parameter('Th_inf', value=parameters['Th_inf']['value'], bounds=parameters['Th_inf']['bounds'], frozen=parameters['Th_inf']['frozen']), Parameter('t_ref', value=parameters['t_ref']['value'], bounds=parameters['t_ref']['bounds'], frozen=parameters['t_ref']['frozen']), Parameter('a_r', value=parameters['a_r']['value'], bounds=parameters['a_r']['bounds'], frozen=parameters['a_r']['frozen']), Parameter('b_r', value=parameters['b_r']['value'], bounds=parameters['b_r']['bounds'], frozen=parameters['b_r']['frozen']), Parameter('a_s', value=parameters['a_s']['value'], bounds=parameters['a_s']['bounds'], frozen=parameters['a_s']['frozen']), Parameter('b_s', value=parameters['b_s']['value'], bounds=parameters['b_s']['bounds'], frozen=parameters['b_s']['frozen']), ])
def freeze(self, param_dict): """Set params""" for param_name, param_value in param_dict.items(): if hasattr(self.params[param_name], 'freeze'): # is type(np.float): self.params[param_name].freeze(param_value) else: from bluepyopt.parameters import Parameter self.params[param_name] = Parameter(name=param_name, value=param_value, frozen=True)
def _defineFitObjects(self, f_d2s, f_s2d): # reference attenuation v_dat = data.DataContainer(with_zd=True) att_f = utils.AttFeature(v_dat) att_ref_d2s = att_f.att_d2s * f_d2s att_ref_s2d = att_f.att_s2d * f_d2s # fitness evaluator features = [utils.AttFeature_(att_ref_d2s, att_ref_s2d, v_dat)] self.objectives = [SingletonObjective('Att', features[0])] # parameters self.params = [ Parameter('g_h_0', value=0., bounds=[0., 50000.]), Parameter('g_h_1', value=200., bounds=[0., 50000.]), Parameter('g_h_2', value=2000., bounds=[0., 50000.]), Parameter('g_h_3', value=5000., bounds=[0., 50000.]), Parameter('e_r_h', value=-40., bounds=[-50., -30.]), Parameter('g_h_b', value=0., bounds=[0., 50000.]), ]
if feature_name in specific_filter_list: #if 'Spikecount_3.0x' == tt.name or if 'Spikecount_1.5x' == tt.name: ft = NUFeatureAllenMultiSpike(tt,model,cnt,yes_list,yes_list,spike_obs) objective = ephys.objectives.SingletonObjective( feature_name, ft) objectives.append(objective) score_calc = ephys.objectivescalculators.ObjectivesCalculator(objectives) lop={} for k,v in MODEL_PARAMS["ADEXP"].items(): p = Parameter(name=k,bounds=v,frozen=False) lop[k] = p simple_cell.params = lop sweep_protocols = [] for protocol_name, amplitude in [('step1', 0.05)]: protocol = ephys.protocols.SweepProtocol(protocol_name, [None], [None]) sweep_protocols.append(protocol) twostep_protocol = ephys.protocols.SequenceProtocol('twostep', protocols=sweep_protocols)
def _defineFitObjects(self, v_dat): # fitness evaluator features = [ utils.VeqFeature(v_dat), utils.VStepFeature(v_dat), utils.TraceFeature(v_dat) ] self.objectives = [ SingletonObjective('V_eq', features[0]), SingletonObjective('V_step', features[1]), SingletonObjective('V_trace', features[2]) ] # parameters self.params = [ Parameter('d_c_m', value=0.001, bounds=[0., 0.01]), Parameter('c_m_0', value=1., bounds=[0.50, 1.50]) ] for c_name in self.channel_names: if c_name == 'K_m' or c_name == 'K_m35' or c_name == 'K_ir': params = [ Parameter('d_' + c_name, value=-1. / 200., bounds=[-0.1, 0.]), Parameter('g0_' + c_name, value=40. * 1e2, bounds=[0., 10000.]), Parameter('e_r_' + c_name, value=-85., bounds=[-95., -80.]) ] elif c_name == 'h_HAY' or c_name == 'h_u': params = [ Parameter('d_' + c_name, value=1. / 200., bounds=[0.0, 0.1]), Parameter('g0_' + c_name, value=0.0099 * 1e2, bounds=[0., 10000.]), Parameter('e_r_' + c_name, value=-40., bounds=[-50., -30.]) ] elif c_name == 'Na_p' or c_name == 'NaP': params = [ Parameter('d_' + c_name, value=0., bounds=[-0.0001, 0.0001]), Parameter('g0_' + c_name, value=0.01 * 1e2, bounds=[0., 10000.]), Parameter('e_r_' + c_name, value=50., bounds=[40., 60.]) ] # elif c_name == 'L': # params = [Parameter('d_L', value=1./200., bounds=[0.0, 0.05]), # Parameter('g0_L', value=0.40*1e2, bounds=[0., 300.]), # Parameter('e_0_L', value=-90., bounds=[-100., -50.]), # Parameter('e_c_L', value=0., bounds=[-1./15, 1./15])] elif c_name == 'L': params = [ Parameter('d_L', value=1. / 1000., bounds=[0.0, 0.05]), Parameter('g0_L', value=0.40 * 1e2, bounds=[0., 300.]), Parameter('e_0_L', value=-90., bounds=[-100., -50.]) ] elif c_name == 'L_c': params = [ Parameter('d_L_c', value=1. / 1000., bounds=[0.0, 0.05]), Parameter('g0_L_c', value=0.40 * 1e2, bounds=[0., 300.]), Parameter('e_0_L_c', value=-90., bounds=[-100., -50.]), Parameter('e_c_L_c', value=0., bounds=[-1. / 15, 1. / 15]) ] # # original params # self.params = [Parameter('d_c_m', value=0.001, bounds=[0., 0.01]), # Parameter('c_m_0', value=1., bounds=[0.95, 1.05])] # for c_name in self.channel_names: # if c_name == 'K_m' or c_name == 'K_m35' or c_name == 'K_ir': # params = [Parameter('d_'+c_name, value=-1./100., bounds=[-0.1, 0.]), # Parameter('g0_'+c_name, value=40.*1e2, bounds=[0., 10000.]), # Parameter('e_r_'+c_name, value=-85., bounds=[-95.,-80.])] # elif c_name == 'h_HAY' or c_name == 'h_u': # params = [Parameter('d_'+c_name, value=1./100., bounds=[0.0, 0.1]), # Parameter('g0_'+c_name, value=0.0099*1e2, bounds=[0., 10000.]), # Parameter('e_r_'+c_name, value=-40., bounds=[-50.,-30.])] # elif c_name == 'Na_p' or c_name == 'NaP': # params = [Parameter('d_'+c_name, value=0., bounds=[-0.0001, 0.0001]), # Parameter('g0_'+c_name, value=0.01*1e2, bounds=[0.,10000.]), # Parameter('e_r_'+c_name, value=50., bounds=[40.,60.])] # # elif c_name == 'L': # # params = [Parameter('d_L', value=1./200., bounds=[0.0, 0.05]), # # Parameter('g0_L', value=0.40*1e2, bounds=[0., 300.]), # # Parameter('e_0_L', value=-90., bounds=[-100., -50.]), # # Parameter('e_c_L', value=0., bounds=[-1./15, 1./15])] # elif c_name == 'L': # params = [Parameter('d_L', value=1./200., bounds=[0.0, 0.05]), # Parameter('g0_L', value=0.40*1e2, bounds=[0., 300.]), # Parameter('e_0_L', value=-90., bounds=[-100., -50.])] # elif c_name == 'L_c': # params = [Parameter('d_L_c', value=1./200., bounds=[0.0, 0.05]), # Parameter('g0_L_c', value=0.40*1e2, bounds=[0., 300.]), # Parameter('e_0_L_c', value=-90., bounds=[-100., -50.]), # Parameter('e_c_L_c', value=0., bounds=[-1./15, 1./15])] else: warnings.warn('unrecognized ion channel \'' + c_name +'\'( choose from ' + \ ' '.join(['L', 'K_m', 'K_m35', 'K_ir', 'h_HAY', 'h_u']), + \ '), ignoring current channel.') self.params.extend(params)
def run_deap(model, bounds, labels_params, summary, obs_stats, labels_sum_stats, stats_std, algo='ibea', offspring_size=10, max_ngen=10, seed=None): """Runs genetic algorithm Parameters ---------- model : Model bounds : Bounds labels_params : list of str Labels of parameters summary : Function to compute summary statistics obs_stats : Observed summary statistics labels_sum_stats : Labels of summary statistics stats_std : array Standard deviations of summary statistics on pilot run algo: str Determines which genetic algorithm is run. So far only ibea ('ibea') and deap default ('deap') are implemented offspring_size : int Offspring size max_ngen : int Maximum number of generations seed : int or None If set, randomness in sampling is disabled """ n_params = len(bounds[:, 0]) params = [] for i in range(n_params): params.append( Parameter(labels_params[i], bounds=[bounds[i, 0], bounds[i, 1]])) # choose and run genetic algorithm evaluator = hh_evaluator(model, summary, obs_stats, labels_sum_stats, stats_std, params) if algo == 'ibea': opt = bpopt.deapext.optimisations.IBEADEAPOptimisation( evaluator, offspring_size=offspring_size, seed=seed) else: opt = bpopt.deapext.optimisations.DEAPOptimisation( evaluator, offspring_size=offspring_size, seed=seed) final_pop, halloffame, log, hist = opt.run(max_ngen=max_ngen) return final_pop, halloffame, log, hist