def task((repetition_i,p)): eps_stim = eps_stim_low+(eps_stim_high-eps_stim_low)*np.random.rand() learn = get_default("learn") learn["eta"] = [1e-7, 0.0] learn["eps"] = [p["eps_learn"],10**eps_stim] neuron = get_default("neuron") neuron["phi"]["alpha"] = -52.0 neuron["phi"]["beta"] = 0.25 neuron["phi"]["r_max"] = 0.35 post_spikes = np.arange(40.0,2000.0,20.0) pre_spikes_learn = post_spikes - 10.0 pre_spikes_stim = post_spikes - p["stim_delta"] my_s = { 'start': 0.0, 'end': 2000.0, 'dt': 0.05, 'pre_spikes': [pre_spikes_learn, pre_spikes_stim], 'I_ext': lambda t: 0.0 } prob = p["prob"] seed = int(int(time.time()*1e8)%1e9) accs = [PeriodicAccumulator(['weights'], interval=100)] accums = run(my_s, get_fixed_spiker(post_spikes), get_dendr_spike_det(p["thresh"]), accs, neuron=neuron, seed=seed, learn=learn, p_backprop=prob) dump((eps_stim,accums),p['ident'])
def overfit((repetition_i, p)): neuron = get_default("neuron") neuron["phi"]['alpha'] = p["alpha"] neuron["phi"]['beta'] = p["beta"] neuron["phi"]['r_max'] = p["r_max"] learn = get_default("learn") learn["eta"] = 1e-7 my_s = { 'start': 0.0, 'end': 4000.0, 'dt': 0.05, 'pre_spikes': [np.arange(50.0, 4000.0, 250.0)], 'I_ext': lambda t: 0.0 } seed = int(int(time.time() * 1e8) % 1e9) accs = [PeriodicAccumulator(['weights'], interval=10)] accums = run(my_s, lambda **kwargs: False, get_dendr_spike_det_dyn_ref(p["thresh"], p["tau_ref_0"], p["theta_0"]), accs, seed=seed, neuron=neuron, voltage_clamp=True, U_clamp=p['Uclamp']) dump(accums, 'artola/' + p['ident'])
def fit((repetition_i,p)): neuron = get_default("neuron") neuron["phi"]['alpha'] = p["alpha"] neuron["phi"]['beta'] = p["beta"] neuron["phi"]['r_max'] = p["r_max"] learn = get_default("learn") learn["eta"] = 4e-7 my_s = { 'start': 0.0, 'end': 1000.0, 'dt': 0.05, 'pre_spikes': [np.arange(50.0,1000.0,250.0)], 'I_ext': lambda t:0.0 } seed = int(int(time.time()*1e8)%1e9) accs = [PeriodicAccumulator(['weights'], interval=10)] if p["h1"]: accums = run(my_s, lambda **kwargs:False, get_dendr_spike_det(p["thresh"]), accs, seed=seed, neuron=neuron, learn=learn, voltage_clamp=True, U_clamp=p['Uclamp'], h=1.0) else: accums = run(my_s, lambda **kwargs:False, get_dendr_spike_det(p["thresh"]), accs, seed=seed, neuron=neuron, learn=learn, voltage_clamp=True, U_clamp=p['Uclamp']) dump(accums,'artola/'+p['ident'])
def task((repetition_i, p)): n_syn = p["n_syn"] learn = get_default("learn") learn["eps"] = 1e-1 / n_syn learn["eta"] = 1e-3 / n_syn neuron = get_default("neuron") neuron["phi"]["alpha"] = p["alpha"] neuron["phi"]["beta"] = p["beta"] neuron["phi"]["r_max"] = p["r_max"] neuron["g_S"] = p["g_S"] epochs = 4 l_c = 6 eval_c = 2 cycles = epochs * l_c + (epochs + 1) * eval_c cycle_dur = p["cycle_dur"] t_end = cycles * cycle_dur def exc_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return 0.0 else: return ((1 + np.sin(np.pi / 2 + t / t_end * cycles * 2 * np.pi)) * 2e-3 * 1 + 8e-3) * p["g_factor"] def inh_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return 0.0 else: return 8e-2 * p["g_factor"] dt = 0.05 f_r = 0.01 # 10Hz t_pts = np.arange(0, t_end / cycles, dt) poisson_spikes = [t_pts[np.random.rand(t_pts.shape[0]) < f_r * dt] for _ in range(n_syn)] poisson_spikes = [[] if spikes.shape[0] == 0 else np.concatenate( [np.arange(spike, t_end, cycle_dur) for spike in spikes]) for spikes in poisson_spikes] for train in poisson_spikes: train.sort() my_s = { 'start': 0.0, 'end': t_end, 'dt': dt, 'pre_spikes': poisson_spikes, 'syn_cond_soma': {'E': exc_soma_cond, 'I': inh_soma_cond}, 'I_ext': lambda t: 0.0 } seed = int(int(time.time() * 1e8) % 1e9) accs = [PeriodicAccumulator(get_all_save_keys(), interval=40)] accums = run(my_s, get_fixed_spiker(np.array([])), get_phi_U_learner(neuron, dt), accs, neuron=neuron, seed=seed, learn=learn) dump((seed, accums), 'sine_task/' + p['ident'])
def vary((repetition_i,p)): n_vary = 5 values = {True: {"alpha":-55.0, "beta":0.4, "r_max":0.3}, False: {"alpha":-59.0, "beta":0.5, "r_max":0.17}} vary = {"alpha":(-2.0,2.0), "beta":(-0.1,0.2), "r_max":(-0.05,0.15)} down = vary[p["vary"]][0] up = vary[p["vary"]][1] middle = values[p["h1"]][p["vary"]] vary_val = np.linspace(middle+down, middle+up, n_vary)[p["i"]] values[p["h1"]][p["vary"]] = vary_val values = values[p["h1"]] neuron = get_default("neuron") neuron["phi"]['r_max'] = values["r_max"] neuron["phi"]['alpha'] = values["alpha"] neuron["phi"]['beta'] = values["beta"] learn = get_default("learn") learn["eps"] = learn["eps"]*p["l_f"] learn["eta"] = learn["eta"]*p["l_f"] if not p["h1"]: learn["eta"] = learn["eta"]*2.5 else: learn["eta"] = learn["eta"]*1.3 spikes = np.array([61.0]) my_s = { 'start': 0.0, 'end': 150.0, 'dt': 0.05, 'pre_spikes': [spikes + p["delta"]], 'I_ext': lambda t: 0.0 } seed = 1 accs = [PeriodicAccumulator(['y','weights'], interval=10), BooleanAccumulator(['spike', 'dendr_spike', 'pre_spikes'])] if p["h1"]: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, neuron=neuron, learn=learn, h=1.0) else: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, neuron=neuron, learn=learn) dump((accums, values),'bi_poo/'+p['ident'])
def fit((repetition_i, p)): learn = get_default("learn") if p["h1"]: learn['eta'] *= 0.125 / 8.0 else: learn["eta"] *= 0.1 neuron = get_default("neuron") neuron["phi"]['r_max'] = 0.2 neuron["phi"]['alpha'] = -54.0 neuron["phi"]['beta'] = 0.25 p_backprop = 0.75 freq = p["freq"] delta = p["delta"] n_spikes_in_burst = 10 burst_pause = 200.0 bursts = 50 / n_spikes_in_burst burst_dur = 1000.0 * n_spikes_in_burst / freq first_spike = 1000.0 / (2 * freq) isi = 1000.0 / freq t_end = bursts * (burst_dur + burst_pause) spikes_in_burst = np.arange(first_spike, burst_dur, isi) spikes = np.array([]) for i in range(bursts): spikes = np.concatenate((spikes, spikes_in_burst + i * (burst_dur + burst_pause))) pre_spikes = spikes + delta my_s = { 'start': 0.0, 'end': t_end, 'dt': 0.05, 'pre_spikes': [pre_spikes], 'I_ext': lambda t: 0.0 } seed = int(int(time.time() * 1e8) % 1e9) accs = [PeriodicAccumulator(['weights'], interval=100)] if p["h1"]: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, learn=learn, neuron=neuron, h=1.0, p_backprop=p_backprop) else: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, learn=learn, neuron=neuron, p_backprop=p_backprop) dump(accums, 'sjostrom/' + p['ident'])
def get_phi_spiker(neuron=None): """ returns a somatic spiker that implements an inhomogenuous poisson process, i.e. a spike will be elicited with probability dt*firing_rate """ if neuron is None: neuron = get_default("neuron") return lambda curr, dt, **kwargs: phi(curr['y'][0], neuron) * dt >= np.random.rand()
def get_phi_spiker(neuron=None): """ returns a somatic spiker that implements an inhomogenuous poisson process, i.e. a spike will be elicited with probability dt*firing_rate """ if neuron is None: neuron = get_default("neuron") return lambda curr, dt, **kwargs: phi(curr['y'][0], neuron ) * dt >= np.random.rand()
def vary((repetition_i,p)): etas = {True: 6e-8, False: 30e-8} varies = {"alpha": np.linspace(-52.0,-56.0,3), "beta": np.linspace(0.15,0.25,3), "r_max": np.linspace(0.1,0.3,3)} learn = get_default("learn") learn["eta"] = etas[p["h1"]] neuron = get_default("neuron") neuron["phi"]["alpha"] = -54.0 neuron["phi"]["beta"] = 0.25 neuron["phi"]["r_max"] = 0.2 neuron["phi"][p["vary"]] = varies[p["vary"]][p["ivary"]] spikes = np.arange(20.0,301.0,20.0) my_s = { 'start': 0.0, 'end': 350.0, 'dt': 0.05, 'pre_spikes': [spikes-10.0], 'I_ext': lambda t: 0.0 } # 0.2 <= p <= 1.0 prob = 0.2 + 0.8*np.random.rand() seed = int(int(time.time()*1e8)%1e9) accs = [PeriodicAccumulator(['weights'], interval=10)] if p["h1"]: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, neuron=neuron, seed=seed, learn=learn, p_backprop=prob, h=1.0) else: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, neuron=neuron, seed=seed, learn=learn, p_backprop=prob) dump((prob,accums),'sjostrom_switch/'+p['ident'])
def fit((repetition_i,p)): values = {True: {"alpha":-55.0, "beta":0.4, "r_max":0.3}, False: {"alpha":-59.0, "beta":0.5, "r_max":0.17}} neuron = get_default("neuron") neuron["phi"]['r_max'] = values[p["h1"]]["r_max"] neuron["phi"]['alpha'] = values[p["h1"]]["alpha"] neuron["phi"]['beta'] = values[p["h1"]]["beta"] learn = get_default("learn") if not p["h1"]: learn["eta"] = learn["eta"]*2.5 else: learn["eta"] = learn["eta"]*1.3 spikes = np.array([101.0]) my_s = { 'start': 0.0, 'end': 300.0, 'dt': 0.05, 'pre_spikes': [spikes + p["delta"]], 'I_ext': lambda t: 0.0 } seed = 1 accs = [PeriodicAccumulator(['y','weights'], interval=10), BooleanAccumulator(['spike', 'dendr_spike', 'pre_spikes'])] if p["h1"]: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, neuron=neuron, learn=learn, h=1.0) else: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, neuron=neuron, learn=learn) dump((accums, values),'bi_poo/'+p['ident'])
def task((repetition_i, p)): eps_stim = eps_stim_low + (eps_stim_high - eps_stim_low) * np.random.rand() learn = get_default("learn") learn["eta"] = [1e-7, 0.0] learn["eps"] = [p["eps_learn"], 10**eps_stim] neuron = get_default("neuron") neuron["phi"]["alpha"] = -52.0 neuron["phi"]["beta"] = 0.25 neuron["phi"]["r_max"] = 0.35 post_spikes = np.arange(40.0, 2000.0, 20.0) pre_spikes_learn = post_spikes - 10.0 pre_spikes_stim = post_spikes - p["stim_delta"] my_s = { 'start': 0.0, 'end': 2000.0, 'dt': 0.05, 'pre_spikes': [pre_spikes_learn, pre_spikes_stim], 'I_ext': lambda t: 0.0 } prob = p["prob"] seed = int(int(time.time() * 1e8) % 1e9) accs = [PeriodicAccumulator(['weights'], interval=100)] accums = run(my_s, get_fixed_spiker(post_spikes), get_dendr_spike_det(p["thresh"]), accs, neuron=neuron, seed=seed, learn=learn, p_backprop=prob) dump((eps_stim, accums), p['ident'])
def logger(self): if not self.__logger: self.__logger = logging.Logger(name=self.module_name) self.__logger.setLevel(helper.get_default( fn=lambda: int(logging.getLevelName("${log_level}")), default="INFO", )) handler = logging.StreamHandler() formatter = logging.Formatter(f"[%(levelname)s] [%(name)s] - %(message)s") handler.setFormatter(formatter) self.__logger.addHandler(handler) return self.__logger
def __process_iam_groups(self, processor=None): if not processor: return for group in self.iam_groups: group_name = group['group_name'] aws_group = self.aws_iam.Group(name=group_name) for user_name in group['user_names']: processing = user_name.lower() == args.arguments.api_caller.lower() or args.arguments.event is None args.arguments.logger.debug( f'Adding/removing user "{user_name}" to/from group "{group_name}": {processing}' ) if processing: aws_user = self.aws_iam.User(name=user_name) error = helper.get_default(fn=lambda: processor(aws_group, aws_user), ignore_error=False) if error: self.errors.append({'group_name': group_name, 'user_name': user_name, 'error': str(error)})
def _(aws_group, aws_user): policy_name = args.Arguments.FAKE_POLICY_NAME % aws_group.name aws_user_policy = self.aws_iam.UserPolicy(user_name=aws_user.name, name=policy_name) aws_user_policy.load() policy_doc = aws_user_policy.policy_document expired_statements = list(filter( lambda st: st.get('Effect', '').lower() == 'allow' and expired_term in st.get('Resource', ''), policy_doc['Statement'] )) for statement in expired_statements: st_resource = statement.get('Resource', '') args.arguments.logger.debug(f'Clearing {aws_user.name}.{policy_name}.Resource= {st_resource}') expired_time = st_resource[st_resource.find(expired_term) + len(expired_term):] expired_time = helper.get_default(fn=lambda: parser.parse(expired_time)) if not expired_time: continue args.arguments.logger.debug(f"Expired time: {expired_time}") if now.timestamp() >= expired_time.timestamp(): self.__revoke(aws_group, aws_user)
def task(inputs): repetition_i = inputs[0] p = inputs[1] n_syn = p["n_syn"] learn = get_default("learn") learn["eps"] = 1e-1 / n_syn learn["eta"] = 1e-3 / n_syn neuron = get_default("neuron") neuron["phi"]["alpha"] = p["alpha"] neuron["phi"]["beta"] = p["beta"] neuron["phi"]["r_max"] = p["r_max"] neuron["g_S"] = p["g_S"] epochs = 4 l_c = 6 eval_c = 2 cycles = epochs * l_c + (epochs + 1) * eval_c cycle_dur = p["cycle_dur"] t_end = cycles * cycle_dur def exc_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return 0.0 else: return ((1 + np.sin(np.pi / 2 + t / t_end * cycles * 2 * np.pi)) \ * 2e-3 * 1 + 8e-3) * p["g_factor"] def inh_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return 0.0 else: return 8e-2 * p["g_factor"] dt = 0.05 f_r = 0.01 # 10Hz t_pts = np.arange(0, t_end / cycles, dt) poisson_spikes = [ t_pts[np.random.rand(t_pts.shape[0]) < f_r * dt] for _ in range(n_syn) ] poisson_spikes = [[] if spikes.shape[0] == 0 else np.concatenate( [np.arange(spike, t_end, cycle_dur) for spike in spikes]) for spikes in poisson_spikes] for train in poisson_spikes: train.sort() my_s = { 'start': 0.0, 'end': t_end, 'dt': dt, 'pre_spikes': poisson_spikes, 'syn_cond_soma': { 'E': exc_soma_cond, 'I': inh_soma_cond }, 'I_ext': lambda t: 0.0 } seed = int(int(time.time() * 1e8) % 1e9) accs = [PeriodicAccumulator(get_all_save_keys(), interval=40)] accums = run(my_s, get_fixed_spiker(np.array([])), get_phi_U_learner(neuron, dt), accs, neuron=neuron, seed=seed, learn=learn) # TODO: create directrory if not exists dump((seed, accums), "results/" + p['ident'])
def time_to_expire(self): if self.__time_to_expire is None: self.__time_to_expire = helper.get_default(fn=lambda: int("${time_to_expire}"), default=600) return self.__time_to_expire
def module_name(self): if self.__module_name is None: self.__module_name = helper.get_default(fn=lambda: str("${module_name}"), default="dln") return self.__module_name
def task((repetition_i, p)): n_syn = p["n_syn"] learn = get_default("learn") learn["eps"] = 1e-1 / (1.0 * n_syn) learn["eta"] = learn["eps"] * p["eps_factor"] neuron = get_default("neuron") neuron["phi"]["alpha"] = p["alpha"] neuron["phi"]["beta"] = p["beta"] neuron["phi"]["r_max"] = 0.1 neuron["g_S"] = p["g_S"] learn_epochs = 2 test_epochs = 1 epochs = learn_epochs + test_epochs l_c = 4 eval_c = 2 cycles = epochs * l_c + (epochs + 1) * eval_c cycle_dur = p["cycle_dur"] epoch_dur = (l_c + eval_c) * cycle_dur t_end = cycles * cycle_dur g_factor = p["g_factor"] def exc_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c or t > learn_epochs * epoch_dur: return 0.0 return p["exc_level"] * p["g_factor"] def inh_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c or t > learn_epochs * epoch_dur: return 0.0 return 1e-1 * p["g_factor"] dt = 0.05 f_r = 1.0 / cycle_dur t_pts = np.arange(0, t_end / cycles, dt) seed = int(int(time.time() * 1e8) % 1e9) reg_spikes = [np.arange(i+1,t_end+1,10) for i in range(n_syn)] poisson_spikes = [t_pts[np.random.rand(t_pts.shape[0]) < f_r * dt] for _ in range(n_syn)] poisson_spikes = [[] if spikes.shape[0] == 0 else np.concatenate( [np.arange(spike, t_end, cycle_dur) for spike in spikes]) for spikes in poisson_spikes] for train in poisson_spikes: train.sort() my_s = { 'start': 0.0, 'end': t_end, 'dt': dt, 'pre_spikes': reg_spikes, 'syn_cond_soma': {'E': exc_soma_cond, 'I': inh_soma_cond}, 'I_ext': lambda t: 0.0 } phi_spiker = get_phi_spiker(neuron) # deprecated def my_spiker(curr, dt, **kwargs): # we want no spikes in eval cycles if curr['t'] % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return False else: return phi_spiker(curr, dt, **kwargs) if p["wiggle"] is None: dendr_predictor = phi else: us = np.linspace(-100,20,1000) ampl = neuron["phi"]["r_max"] phis = phi(us, neuron) alphas = [] for i in range(p["wiggle"]): alphas.append(us[phis > (i+0.5)*ampl/p["wiggle"]][0]) r_m = neuron['phi']['r_max']/p["wiggle"] def dendr_predictor(V, neuron): return np.sum([phi(V,{'phi':{'alpha': al, 'beta':p["beta_wiggle"], 'r_max': r_m}}) for al in alphas]) accs = [PeriodicAccumulator(get_all_save_keys(), interval=20, y_keep=3), BooleanAccumulator(['spike'])] accums = run(my_s, get_fixed_spiker(np.array([])), get_phi_U_learner(neuron, dt, p["exc_decrease"]), accs, neuron=neuron, seed=seed, learn=learn, dendr_predictor=dendr_predictor) dump((seed, accums), 'wiggle_test/' + p['ident'])
def task((repetition_i, p)): n_syn = p["n_syn"] learn = get_default("learn") learn["eps"] = 1e-1 / (1.0 * n_syn) learn["eta"] = learn["eps"] * p["eps_factor"] neuron = get_default("neuron") neuron["phi"]["alpha"] = p["alpha"] neuron["phi"]["beta"] = p["beta"] neuron["phi"]["r_max"] = 0.1 neuron["g_S"] = p["g_S"] learn_epochs = 20 test_epochs = 20 epochs = learn_epochs + test_epochs l_c = 8 eval_c = 2 cycles = epochs * l_c + (epochs + 1) * eval_c cycle_dur = 100.0 epoch_dur = (l_c + eval_c) * cycle_dur t_end = cycles * cycle_dur exc_level = p["exc_level"] g_factor = 50 def exc_soma_cond(t): if t % (cycle_dur * (l_c + eval_c) ) < cycle_dur * eval_c or t > learn_epochs * epoch_dur: return 0.0 else: return ((1 + np.sin(-np.pi / 2 + t / t_end * cycles * 2 * np.pi)) * exc_level + exc_level) * g_factor def inh_soma_cond(t): if t % (cycle_dur * (l_c + eval_c) ) < cycle_dur * eval_c or t > learn_epochs * epoch_dur: return 0.0 else: return 4e-2 * g_factor dt = 0.05 f_r = 0.01 # 10Hz t_pts = np.arange(0, t_end / cycles, dt) seed = int(int(time.time() * 1e8) % 1e9) poisson_spikes = [ t_pts[np.random.rand(t_pts.shape[0]) < f_r * dt] for _ in range(n_syn) ] poisson_spikes = [[] if spikes.shape[0] == 0 else np.concatenate( [np.arange(spike, t_end, cycle_dur) for spike in spikes]) for spikes in poisson_spikes] for train in poisson_spikes: train.sort() my_s = { 'start': 0.0, 'end': t_end, 'dt': dt, 'pre_spikes': poisson_spikes, 'syn_cond_soma': { 'E': exc_soma_cond, 'I': inh_soma_cond }, 'I_ext': lambda t: 0.0 } phi_spiker = get_phi_spiker(neuron) # deprecated def my_spiker(curr, dt, **kwargs): # we want no spikes in eval cycles if curr['t'] % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return False else: return phi_spiker(curr, dt, **kwargs) accs = [ PeriodicAccumulator(get_all_save_keys(), interval=20, y_keep=3), BooleanAccumulator(['spike']) ] accums = run(my_s, phi_spiker, get_inst_backprop(), accs, neuron=neuron, seed=seed, learn=learn) dump((seed, accums), 'sine_task_backprop/' + p['ident'])
def task((repetition_i, p)): n_syn = p["n_syn"] learn = get_default("learn") learn["eps"] = 1e-1 / (1.0 * n_syn) learn["eta"] = learn["eps"]*p["eps_factor"] neuron = get_default("neuron") neuron["phi"]["alpha"] = p["alpha"] neuron["phi"]["beta"] = p["beta"] neuron["phi"]["r_max"] = 0.1 neuron["g_S"] = p["g_S"] learn_epochs = 20 test_epochs = 20 epochs = learn_epochs + test_epochs l_c = 8 eval_c = 2 cycles = epochs * l_c + (epochs + 1) * eval_c cycle_dur = 100.0 epoch_dur = (l_c + eval_c) * cycle_dur t_end = cycles * cycle_dur exc_level = p["exc_level"] g_factor = 50 def exc_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c or t > learn_epochs*epoch_dur: return 0.0 else: return ((1 + np.sin(-np.pi/2 + t / t_end * cycles * 2 * np.pi)) * exc_level + exc_level) * g_factor def inh_soma_cond(t): if t % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c or t > learn_epochs*epoch_dur: return 0.0 else: return 4e-2 * g_factor dt = 0.05 f_r = 0.01 # 10Hz t_pts = np.arange(0, t_end / cycles, dt) seed = int(int(time.time() * 1e8) % 1e9) poisson_spikes = [t_pts[np.random.rand(t_pts.shape[0]) < f_r * dt] for _ in range(n_syn)] poisson_spikes = [[] if spikes.shape[0] == 0 else np.concatenate( [np.arange(spike, t_end, cycle_dur) for spike in spikes]) for spikes in poisson_spikes] for train in poisson_spikes: train.sort() my_s = { 'start': 0.0, 'end': t_end, 'dt': dt, 'pre_spikes': poisson_spikes, 'syn_cond_soma': {'E': exc_soma_cond, 'I': inh_soma_cond}, 'I_ext': lambda t: 0.0 } phi_spiker = get_phi_spiker(neuron) # deprecated def my_spiker(curr, dt, **kwargs): # we want no spikes in eval cycles if curr['t'] % (cycle_dur * (l_c + eval_c)) < cycle_dur * eval_c: return False else: return phi_spiker(curr, dt, **kwargs) accs = [PeriodicAccumulator(get_all_save_keys(), interval=20, y_keep = 3), BooleanAccumulator(['spike'])] accums = run(my_s, phi_spiker, get_inst_backprop(), accs, neuron=neuron, seed=seed, learn=learn) dump((seed, accums), 'sine_task_backprop/' + p['ident'])
def run(sim, spiker, spiker_dendr, accumulators, neuron=None, learn=None, normalizer=None, **kwargs): """ this is the main simulation routine, can either be called directly, or with the convenience routine do in helper.py arguments: sim -- a dictionary containing the following simulation parameters start: starting time end: ending time dt: time step I_ext: function for evaluating externally applied current pre_spikes: a list of presynaptic spikes spiker -- the somatic spiker spiker_dendr -- the dendritic spiker accumulators -- a list of accumulators for saving model variables during the simulation neuron -- a dictionary containing the neuron parameters, default_neuron is used if none specified learn -- a dictionary contianing the learning parameters, default_learn is used if none specified normalizer -- a function to normalize synaptic weights, e.g. the default normalizer ensures non-negative weights returns: a list of accumulators containing simulation results """ use_seed = kwargs.get('seed', 0) np.random.seed(use_seed) if neuron is None: neuron = get_default('neuron') if learn is None: learn = get_default('learn') # restrict to positive weights by default if normalizer is None: normalizer = lambda weights: np.where(weights > 0, weights, 0.0) # set some default parameters voltage_clamp = kwargs.get('voltage_clamp', False) p_backprop = kwargs.get('p_backprop', 1.0) syn_cond_soma = sim.get('syn_cond_soma', {sym: lambda t: 0.0 for sym in ['E', 'I']}) dendr_predictor = kwargs.get('dendr_predictor', phi) I_ext = sim.get('I_ext', step_current(np.array([[sim['start'], 0.0]]))) # ensure numpy arrays, for fancy indexing pre_spikes = sim['pre_spikes'] for i, pre_sp in enumerate(pre_spikes): pre_spikes[i] = np.array(pre_sp) n_syn = len(pre_spikes) for key in ['eps', 'eta', 'tau_delta']: if not isinstance(learn[key], collections.Iterable): learn[key] = np.array([learn[key] for _ in range(n_syn)]) for acc in accumulators: acc.prepare_arrays(n_syn) t_start, t_end, dt = sim['start'], sim['end'], sim['dt'] if voltage_clamp: U0 = kwargs['U_clamp'] else: U0 = neuron['E_L'] curr = { 't': t_start, 'y': np.concatenate((np.array([U0, neuron['E_L'], neuron['E_L']]), np.zeros(2 * n_syn))) } last_spike = {'t': float('-inf'), 'y': curr['y']} last_spike_dendr = {'t': float('-inf'), 'y': curr['y']} weights = np.array(learn['eps']) g_E_Ds = np.zeros(n_syn) syn_pots_sums = np.zeros(n_syn) PIVs = np.zeros(n_syn) deltas = np.zeros(n_syn) weight_updates = np.zeros(n_syn) while curr['t'] < t_end - dt / 2: # for each synapse: is there a presynaptic spike at curr['t']? curr_pres = np.array([ np.sum(np.isclose(pre_sp, curr['t'], rtol=1e-10, atol=1e-10)) for pre_sp in pre_spikes ]) g_E_Ds = g_E_Ds + curr_pres * weights g_E_Ds = g_E_Ds - dt * g_E_Ds / neuron['tau_s'] syn_pots_sums = np.array([ np.sum( np.exp(-(curr['t'] - pre_sp[pre_sp <= curr['t']]) / neuron['tau_s'])) for pre_sp in pre_spikes ]) # is there a postsynaptic spike at curr['t']? if curr['t'] - last_spike['t'] < neuron['tau_ref']: does_spike = False else: does_spike = spiker(curr=curr, dt=dt) if does_spike: last_spike = {'t': curr['t'], 'y': curr['y']} # does the dendrite detect a spike? dendr_spike = spiker_dendr(curr=curr, last_spike=last_spike, last_spike_dendr=last_spike_dendr) if dendr_spike: last_spike_dendr = {'t': curr['t'], 'y': curr['y']} # dendritic prediction dendr_pred = dendr_predictor(curr['y'][2], neuron) h = kwargs.get( 'h', phi_prime(curr['y'][2], neuron) / phi(curr['y'][2], neuron)) # update weights pos_PIVs = neuron['delta_factor'] * float( dendr_spike) / dt * h * curr['y'][4::2] neg_PIVs = dendr_pred * h * curr['y'][4::2] PIVs = pos_PIVs - neg_PIVs deltas += dt * (PIVs - deltas) / learn['tau_delta'] weight_updates = learn['eta'] * deltas weights = normalizer(weights + weight_updates) # advance state: integrate from curr['t'] to curr['t']+dt curr_I = I_ext(curr['t']) args = (curr['y'], curr['t'], curr['t'] - last_spike['t'], g_E_Ds, syn_pots_sums, curr_I, neuron, syn_cond_soma, voltage_clamp, p_backprop) curr['y'] += dt * urb_senn_rhs(*args) curr['t'] += dt # save state vals = { 'g_E_Ds': g_E_Ds, 'syn_pots_sums': syn_pots_sums, 'y': curr['y'], 'spike': float(does_spike), 'dendr_pred': dendr_pred, 'h': h, 'PIVs': PIVs, 'pos_PIVs': pos_PIVs, 'neg_PIVs': neg_PIVs, 'dendr_spike': float(dendr_spike), 'pre_spikes': curr_pres, 'weights': weights, 'weight_updates': weight_updates, 'deltas': deltas, 'I_ext': curr_I } for acc in accumulators: acc.add(curr['t'], **vals) for acc in accumulators: acc.cleanup() acc.add_variable('seed', use_seed) return accumulators
def run(sim, spiker, spiker_dendr, accumulators, neuron=None, learn=None, normalizer=None, **kwargs): """ this is the main simulation routine, can either be called directly, or with the convenience routine do in helper.py arguments: sim -- a dictionary containing the following simulation parameters start: starting time end: ending time dt: time step I_ext: function for evaluating externally applied current pre_spikes: a list of presynaptic spikes spiker -- the somatic spiker spiker_dendr -- the dendritic spiker accumulators -- a list of accumulators for saving model variables during the simulation neuron -- a dictionary containing the neuron parameters, default_neuron is used if none specified learn -- a dictionary contianing the learning parameters, default_learn is used if none specified normalizer -- a function to normalize synaptic weights, e.g. the default normalizer ensures non-negative weights returns: a list of accumulators containing simulation results """ use_seed = kwargs.get('seed', 0) np.random.seed(use_seed) if neuron is None: neuron = get_default('neuron') if learn is None: learn = get_default('learn') # restrict to positive weights by default if normalizer is None: normalizer = lambda weights: np.where(weights > 0, weights, 0.0) # set some default parameters voltage_clamp = kwargs.get('voltage_clamp', False) p_backprop = kwargs.get('p_backprop', 1.0) syn_cond_soma = sim.get('syn_cond_soma', {sym: lambda t: 0.0 for sym in ['E', 'I']}) dendr_predictor = kwargs.get('dendr_predictor', phi) I_ext = sim.get('I_ext', step_current(np.array([[sim['start'], 0.0]]))) # ensure numpy arrays, for fancy indexing pre_spikes = sim['pre_spikes'] for i, pre_sp in enumerate(pre_spikes): pre_spikes[i] = np.array(pre_sp) n_syn = len(pre_spikes) for key in ['eps', 'eta', 'tau_delta']: if not isinstance(learn[key], collections.Iterable): learn[key] = np.array([learn[key] for _ in range(n_syn)]) for acc in accumulators: acc.prepare_arrays(n_syn) t_start, t_end, dt = sim['start'], sim['end'], sim['dt'] if voltage_clamp: U0 = kwargs['U_clamp'] else: U0 = neuron['E_L'] curr = {'t': t_start, 'y': np.concatenate((np.array([U0, neuron['E_L'], neuron['E_L']]), np.zeros(2 * n_syn)))} last_spike = {'t': float('-inf'), 'y': curr['y']} last_spike_dendr = {'t': float('-inf'), 'y': curr['y']} weights = np.array(learn['eps']) g_E_Ds = np.zeros(n_syn) syn_pots_sums = np.zeros(n_syn) PIVs = np.zeros(n_syn) deltas = np.zeros(n_syn) weight_updates = np.zeros(n_syn) while curr['t'] < t_end - dt / 2: # for each synapse: is there a presynaptic spike at curr['t']? curr_pres = np.array( [np.sum(np.isclose(pre_sp, curr['t'], rtol=1e-10, atol=1e-10)) for pre_sp in pre_spikes]) g_E_Ds = g_E_Ds + curr_pres * weights g_E_Ds = g_E_Ds - dt * g_E_Ds / neuron['tau_s'] syn_pots_sums = np.array( [np.sum(np.exp(-(curr['t'] - pre_sp[pre_sp <= curr['t']]) / neuron['tau_s'])) for pre_sp in pre_spikes]) # is there a postsynaptic spike at curr['t']? if curr['t'] - last_spike['t'] < neuron['tau_ref']: does_spike = False else: does_spike = spiker(curr=curr, dt=dt) if does_spike: last_spike = {'t': curr['t'], 'y': curr['y']} # does the dendrite detect a spike? dendr_spike = spiker_dendr(curr=curr, last_spike=last_spike, last_spike_dendr=last_spike_dendr) if dendr_spike: last_spike_dendr = {'t': curr['t'], 'y': curr['y']} # dendritic prediction dendr_pred = dendr_predictor(curr['y'][2], neuron) h = kwargs.get('h', phi_prime(curr['y'][2], neuron) / phi(curr['y'][2], neuron)) # update weights pos_PIVs = neuron['delta_factor'] * float(dendr_spike) / dt * h * curr['y'][4::2] neg_PIVs = dendr_pred * h * curr['y'][4::2] PIVs = pos_PIVs - neg_PIVs deltas += dt * (PIVs - deltas) / learn['tau_delta'] weight_updates = learn['eta'] * deltas weights = normalizer(weights + weight_updates) # advance state: integrate from curr['t'] to curr['t']+dt curr_I = I_ext(curr['t']) args = (curr['y'], curr['t'], curr['t'] - last_spike['t'], g_E_Ds, syn_pots_sums, curr_I, neuron, syn_cond_soma, voltage_clamp, p_backprop) curr['y'] += dt * urb_senn_rhs(*args) curr['t'] += dt # save state vals = {'g_E_Ds': g_E_Ds, 'syn_pots_sums': syn_pots_sums, 'y': curr['y'], 'spike': float(does_spike), 'dendr_pred': dendr_pred, 'h': h, 'PIVs': PIVs, 'pos_PIVs': pos_PIVs, 'neg_PIVs': neg_PIVs, 'dendr_spike': float(dendr_spike), 'pre_spikes': curr_pres, 'weights': weights, 'weight_updates': weight_updates, 'deltas': deltas, 'I_ext': curr_I} for acc in accumulators: acc.add(curr['t'], **vals) for acc in accumulators: acc.cleanup() acc.add_variable('seed', use_seed) return accumulators
def fit((repetition_i, p)): learn = get_default("learn") if p["h1"]: learn['eta'] *= 0.125 / 8.0 else: learn["eta"] *= 0.1 neuron = get_default("neuron") neuron["phi"]['r_max'] = 0.2 neuron["phi"]['alpha'] = -54.0 neuron["phi"]['beta'] = 0.25 p_backprop = 0.75 freq = p["freq"] delta = p["delta"] n_spikes_in_burst = 10 burst_pause = 200.0 bursts = 50 / n_spikes_in_burst burst_dur = 1000.0 * n_spikes_in_burst / freq first_spike = 1000.0 / (2 * freq) isi = 1000.0 / freq t_end = bursts * (burst_dur + burst_pause) spikes_in_burst = np.arange(first_spike, burst_dur, isi) spikes = np.array([]) for i in range(bursts): spikes = np.concatenate( (spikes, spikes_in_burst + i * (burst_dur + burst_pause))) pre_spikes = spikes + delta my_s = { 'start': 0.0, 'end': t_end, 'dt': 0.05, 'pre_spikes': [pre_spikes], 'I_ext': lambda t: 0.0 } seed = int(int(time.time() * 1e8) % 1e9) accs = [PeriodicAccumulator(['weights'], interval=100)] if p["h1"]: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, learn=learn, neuron=neuron, h=1.0, p_backprop=p_backprop) else: accums = run(my_s, get_fixed_spiker(spikes), get_dendr_spike_det(-50.0), accs, seed=seed, learn=learn, neuron=neuron, p_backprop=p_backprop) dump(accums, 'sjostrom/' + p['ident'])