Example #1
0
def compute_my_pijs(conns, output_fn, tau_dict, input_fn_base):
    """
    conns = list of connections, i.e. tuples: (src, tgt)
    """

    dt = 1
    print 'pc_id computes pijs for %d connections' % (len(conns))
    my_traces_pre = {}
    my_traces_post = {}
    p_ij_string = '#pre_id\tpost_id\tpij[-1]\tw_ij[-1]\tbias\n'
    for i in xrange(len(conns)):
        if (i % 500) == 0:
            print "Pc %d conn: \t%d - %d; \t%d / %d\t%.4f percent complete" % (pc_id, conns[i][0], conns[i][1], i, len(conns), i * 100./len(conns))
        pre_id = conns[i][0]
        post_id = conns[i][1]
        if my_traces_pre.has_key(pre_id):
            (zi, pi) = my_traces_pre[pre_id]
        else:
            pre_trace = np.loadtxt(input_fn_base + str(pre_id) + '.dat')
            zi, ei, pi = Bcpnn.compute_traces(pre_trace, tau_dict['tau_zi'], tau_dict['tau_ei'], tau_dict['tau_pi'], eps=dt/tau_dict['tau_pi'])
            my_traces_pre[pre_id] = (zi, pi)

        if my_traces_post.has_key(post_id):
            (zj, pj) = my_traces_post[post_id]
        else: 
            post_trace = np.loadtxt(input_fn_base  + str(post_id) + '.dat')
            zj, ej, pj = Bcpnn.compute_traces(post_trace, tau_dict['tau_zj'], tau_dict['tau_ej'], tau_dict['tau_pj'], eps=dt/tau_dict['tau_pj']) # actually should be eps=dt/tau_dict['tau_pi']
            my_traces_post[post_id] = (zj, pj)

        pij, w_ij, bias = Bcpnn.compute_pij(zi, zj, pi, pj, tau_dict['tau_eij'], tau_dict['tau_pij'])
        p_ij_string += '%d\t%d\t%.8e\t%.8e\t%.8e\n' % (pre_id, post_id, pij, w_ij, bias)

    if comm != None:
        comm.barrier()

    print 'Writing p_ij output to:', output_fn
    f = file(output_fn, 'w')
    f.write(p_ij_string)
    f.close()
    if comm != None:
        comm.barrier()
    if n_proc > 1 and pc_id == 0:
        tmp_fn = params['bcpnntrace_folder'] + 'all_pij.dat'
        cat_cmd = 'cat %s* > %s' % (params['bcpnntrace_folder'] + 'pij_', tmp_fn)
        print cat_cmd
        os.system(cat_cmd)

    return my_traces_pre, my_traces_post
Example #2
0
def compute_pre_post_traces(conns, verbose=True):
    bcpnn_trace_len = params['t_stimulus'] / params['dt_rate']
    trace_len = params['t_stimulus'] / params['dt_rate']


    for i in xrange(len(conns)):
    #for i in xrange(100):
        print "Pc %d conn: \t%d - %d; \t%d / %d\t%.4f percent complete" % (pc_id, conns[i][0], conns[i][1], i + 1, len(conns), i * 100./len(conns))
        pre_id = conns[i][0]
        post_id = conns[i][1]

        pre_trace = np.loadtxt(params['input_rate_fn_base'] + str(pre_id) + '.dat')
        post_trace = np.loadtxt(params['input_rate_fn_base'] + str(post_id) + '.dat')

#        pre_trace = np.zeros(bcpnn_trace_len)
#        d = np.loadtxt(params['input_rate_fn_base'] + str(pre_id) + '.dat')
#        pre_trace[:trace_len] = d
#        post_trace = np.zeros(bcpnn_trace_len)
#        d = np.loadtxt(params['input_rate_fn_base'] + str(post_id) + '.dat')
#        post_trace[:trace_len] = d
#        post_trace[trace_len:] = 0.
#        post_trace.resize(bcpnn_trace_len)

        # compute
        wij, bias, pi, pj, pij, ei, ej, eij, zi, zj = Bcpnn.get_spiking_weight_and_bias(pre_trace, post_trace, get_traces=True, \
                tau_dict=tau_dict, f_max=1000.)

        weight_fn = params['weights_fn_base'] + '%d_%d.dat' % (pre_id, post_id)
        if verbose:
            print 'Saving to ', weight_fn
        np.savetxt(weight_fn, wij)
        output_fn = params['bias_fn_base'] + "%d.dat" % (post_id)
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, bias)

        output_fn = params['ztrace_fn_base'] + "%d.dat" % pre_id
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, zi)
        output_fn = params['ztrace_fn_base'] + "%d.dat" % post_id
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, zj)

        output_fn = params['etrace_fn_base'] + "%d.dat" % pre_id
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, ei)
        output_fn = params['etrace_fn_base'] + "%d.dat" % post_id
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, ej)
        output_fn = params['etrace_fn_base'] + "%d_%d.dat" % (pre_id, post_id)
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, eij)

        output_fn = params['ptrace_fn_base'] + "%d.dat" % pre_id
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, pi)
        output_fn = params['ptrace_fn_base'] + "%d.dat" % post_id
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, pj)
        output_fn = params['ptrace_fn_base'] + "%d_%d.dat" % (pre_id, post_id)
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, pij)

        output_fn = params['weights_fn_base'] + "%d_%d.dat" % (pre_id, post_id)
        if verbose:
            print 'Saving to ', output_fn
        np.savetxt(output_fn, wij)
Example #3
0
    text += 'tau_zj = %d\n' % tau_dict['tau_zj']
    text += 'tau_e = %d\n' % tau_dict['tau_ei']
    text += 'tau_p = %d\n' % tau_dict['tau_pi']
    text += 'x_stim(t) = (%.1f, %.1f) + (%.1f, %.1f) * t\n'% (mp[0], mp[1], mp[2], mp[3])
    import plot_bcpnn_traces as trace_plotter
    output_fig_fn = params['figures_folder'] + 'bcpnn_trace_%d_%d_%d.png' % (tau_dict['tau_zi'], tau_dict['tau_ei'], tau_dict['tau_pi'])
    times.append(time.time())
    t_comp = times[-1] - times[0]
    print 'Computation time: %d sec = %.1f min' % (t_comp, t_comp / 60.)

    # seperate computation of traces and weights
    pre_trace = np.loadtxt(params['input_rate_fn_base'] + '%d.dat' % my_conns[0][0])
    post_trace = np.loadtxt(params['input_rate_fn_base'] + '%d.dat' % my_conns[0][1])
    dt = 1.
    zi = np.zeros(pre_trace.size)
    zi, ei, pi = Bcpnn.compute_traces(pre_trace, tau_dict['tau_zi'], tau_dict['tau_ei'], tau_dict['tau_pi'], eps=dt/tau_dict['tau_pi'])
    pre_id = my_conns[0][0]
    post_id = my_conns[0][1]
    # print the seperately computed traces 
    np.savetxt('debug_zi_trace_%d.dat' % pre_id, zi)
    np.savetxt('debug_ei_trace_%d.dat' % pre_id, ei)
    np.savetxt('debug_pi_trace_%d.dat' % pre_id, pi)

    zj, ej, pj = Bcpnn.compute_traces(post_trace, tau_dict['tau_zj'], tau_dict['tau_ej'], tau_dict['tau_pj'], eps=dt/tau_dict['tau_pj'])
    np.savetxt('debug_zj_trace_%d.dat' % post_id, zj)
    np.savetxt('debug_ej_trace_%d.dat' % post_id, ej)
    np.savetxt('debug_pj_trace_%d.dat' % post_id, pj)

    pij, w_ij, bias = Bcpnn.compute_pij(zi, zj, pi, pj, tau_dict['tau_eij'], tau_dict['tau_pij'])
    print 'debug w_ij', pre_id, post_id, w_ij
    w_ij_trace, bias, pij, eij = Bcpnn.compute_pij(zi, zj, pi, pj, tau_dict['tau_eij'], tau_dict['tau_pij'], get_traces=True)
Example #4
0
    def compute_my_pijs(self):

        pre_traces_computed = np.zeros(params['n_exc'], dtype=np.bool)
        post_traces_computed = np.zeros(params['n_exc'], dtype=np.bool)

        tau_dict = self.params['tau_dict']
        zi_traces = self.initial_value * np.ones((self.n_time_steps, self.pre_ids.size), dtype=np.double)
        zj_traces = self.initial_value * np.ones((self.n_time_steps, self.post_ids.size), dtype=np.double)
        ei_traces = self.initial_value * np.ones((self.n_time_steps, self.pre_ids.size), dtype=np.double)
        ej_traces = self.initial_value * np.ones((self.n_time_steps, self.post_ids.size), dtype=np.double)
        pi_traces = self.initial_value * np.ones((self.n_time_steps, self.pre_ids.size), dtype=np.double)
        pj_traces = self.initial_value * np.ones((self.n_time_steps, self.post_ids.size), dtype=np.double)
        eij_trace = self.initial_value ** 2 * np.ones(self.n_time_steps, dtype=np.double)
        pij_trace = self.initial_value ** 2 * np.ones(self.n_time_steps, dtype=np.double)
        wij_trace = np.zeros(self.n_time_steps, dtype=np.double)
        bias_trace = np.log(self.initial_value) * np.ones(self.n_time_steps, dtype=np.double)
        input_fn_base = self.training_input_folder + self.params['abstract_input_fn_base']

        self.my_wijs = np.zeros((self.my_conns[:, 0].size, 4), dtype=np.double) # array for wij and pij
        self.my_bias = np.zeros((self.post_ids.size, 2), dtype=np.double) # array for wij and pij
        bias_idx = 0
        for i in xrange(self.my_conns[:, 0].size):
            if (i % 1000) == 0:
                print "Pc %d conn: \t%d / %d\t%.4f percent complete; Stimulus iteration: %d" % (pc_id, i, self.my_conns[:, 0].size, i * 100./self.my_conns[:, 0].size, self.iteration)
            pre_id = self.my_conns[i, 0]
            post_id = self.my_conns[i, 1]
            if pre_traces_computed[pre_id]:
                idx = self.gid_idx_map_pre[pre_id]
                (zi, ei, pi) = zi_traces[:, idx], ei_traces[:, idx], pi_traces[:, idx]
            else:
                pre_trace = np.loadtxt(input_fn_base + str(pre_id) + '.dat')
                idx = self.gid_idx_map_pre[pre_id]
                zi_traces[0, idx] = self.zi_init[pre_id]
                ei_traces[0, idx] = self.ei_init[pre_id]
                pi_traces[0, idx] = self.pi_init[pre_id]
                Bcpnn.compute_traces_new(pre_trace, zi_traces[:, idx], ei_traces[:, idx], pi_traces[:, idx], \
                        tau_dict['tau_zi'], tau_dict['tau_ei'], tau_dict['tau_pi'], \
                        dt=self.params['dt_rate'], eps=self.eps)
                pre_traces_computed[pre_id] = True
                self.zi_init[pre_id] = zi_traces[-1, idx]
                self.ei_init[pre_id] = ei_traces[-1, idx]
                self.pi_init[pre_id] = pi_traces[-1, idx]

            if post_traces_computed[post_id]:
                idx = self.gid_idx_map_post[post_id]
                (zj, ej, pj) = zj_traces[:, idx], ej_traces[:, idx], pj_traces[:, idx]
            else:
                post_trace = np.loadtxt(input_fn_base + str(post_id) + '.dat')
                idx = self.gid_idx_map_post[post_id]
                zj_traces[0, idx] = self.zj_init[post_id]
                ej_traces[0, idx] = self.ej_init[post_id]
                pj_traces[0, idx] = self.pj_init[post_id]
                Bcpnn.compute_traces_new(post_trace, zj_traces[:, idx], ej_traces[:, idx], pj_traces[:, idx], \
                        tau_dict['tau_zj'], tau_dict['tau_ej'], tau_dict['tau_pj'], \
                        dt=self.params['dt_rate'], eps=self.eps)
                post_traces_computed[post_id] = True
                self.zj_init[post_id] = zj_traces[-1, idx]
                self.ej_init[post_id] = ej_traces[-1, idx]
                self.pj_init[post_id] = pj_traces[-1, idx]
                self.my_bias[bias_idx, :] = post_id, np.log(pj_traces[-1, idx])
                bias_idx += 1

            idx_pre = self.gid_idx_map_pre[pre_id]
            idx_post = self.gid_idx_map_post[post_id]
            eij_trace[0] = self.eij_init[pre_id, post_id]
            pij_trace[0] = self.pij_init[pre_id, post_id]
            wij_trace[0] = self.wij_init[pre_id, post_id]
            bias_trace[0] = self.bias_init[post_id]

            if ((pre_id, post_id) in self.my_selected_conns):
                # write selected traces to files
#                print 'Proc %d prints BCPNN pre-traces for cell %d:' % (self.pc_id, pre_id)
                idx = self.gid_idx_map_pre[pre_id]
                np.savetxt(self.params['bcpnntrace_folder'] + 'zi_%d_%d.dat' % (self.iteration, pre_id), zi_traces[:, idx])
                np.savetxt(self.params['bcpnntrace_folder'] + 'ei_%d_%d.dat' % (self.iteration, pre_id), ei_traces[:, idx])
                np.savetxt(self.params['bcpnntrace_folder'] + 'pi_%d_%d.dat' % (self.iteration, pre_id), pi_traces[:, idx])
#                print 'Proc %d prints BCPNN post-traces for cell %d:' % (self.pc_id, post_id)
                idx = self.gid_idx_map_post[post_id]
                np.savetxt(self.params['bcpnntrace_folder'] + 'zj_%d_%d.dat' % (self.iteration, post_id), zj_traces[:, idx])
                np.savetxt(self.params['bcpnntrace_folder'] + 'ej_%d_%d.dat' % (self.iteration, post_id), ej_traces[:, idx])
                np.savetxt(self.params['bcpnntrace_folder'] + 'pj_%d_%d.dat' % (self.iteration, post_id), pj_traces[:, idx])
                wij, bias, pij, eij = Bcpnn.compute_pij_new(zi_traces[:, idx_pre], zj_traces[:, idx_post], pi_traces[:, idx_pre], pj_traces[:, idx_post], \
                                        eij_trace, pij_trace, wij_trace, bias_trace, \
                                        tau_dict['tau_eij'], tau_dict['tau_pij'], get_traces=True, dt=self.params['dt_rate'])
                np.savetxt(self.params['bcpnntrace_folder'] + 'wij_%d_%d_%d.dat' % (self.iteration, pre_id, post_id), wij)
                np.savetxt(self.params['bcpnntrace_folder'] + 'bias_%d_%d_%d.dat' % (self.iteration, pre_id, post_id), bias)
                np.savetxt(self.params['bcpnntrace_folder'] + 'eij_%d_%d_%d.dat' % (self.iteration, pre_id, post_id), eij)
                np.savetxt(self.params['bcpnntrace_folder'] + 'pij_%d_%d_%d.dat' % (self.iteration, pre_id, post_id), pij)
            else:
                Bcpnn.compute_pij_new(zi_traces[:, idx_pre], zj_traces[:, idx_post], pi_traces[:, idx_pre], pj_traces[:, idx_post], \
                        eij_trace, pij_trace, wij_trace, bias_trace, \
                        tau_dict['tau_eij'], tau_dict['tau_pij'], dt=self.params['dt_rate'])
                    
            # update the nr.0 value for the next stimulus
            self.eij_init[pre_id, post_id] = eij_trace[-1]
            self.pij_init[pre_id, post_id] = pij_trace[-1]
            self.wij_init[pre_id, post_id] = wij_trace[-1]
            self.bias_init[post_id] = bias_trace[-1]
            self.my_wijs[i, :] = pre_id, post_id, wij_trace[-1], pij_trace[-1]

        # store wijs and bias in the tmp folder
        np.savetxt(self.params['tmp_folder'] + 'wij_%d_%d.dat' % (self.iteration, self.pc_id), self.my_wijs)
        np.savetxt(self.params['tmp_folder'] + 'bias_%d_%d.dat' % (self.iteration, self.pc_id), self.my_bias)
        if self.comm != None:
            self.comm.barrier()
Example #5
0
import os
import sys
import simulation_parameters
import Bcpnn
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
pc_id, n_proc = comm.rank, comm.size


sim_cnt = int(sys.argv[1])

network_params = simulation_parameters.parameter_storage()  # network_params class containing the simulation parameters
params = network_params.load_params()                       # params stores cell numbers, etc as a dictionary
conn_list = np.loadtxt(params['conn_list_ee_fn_base'] + str(sim_cnt) + '.dat')

params = network_params.load_params()                       # params stores cell numbers, etc as a dictionary
Bcpnn.bcpnn_offline_noColumns(params, conn_list, sim_cnt, True, comm)
d = np.loadtxt(params["input_rate_fn_base"] + str(pre_id) + ".dat")
print "debug", trace_len, d.size, bcpnn_trace_len
pre_trace[:trace_len] = d
post_trace = np.zeros(bcpnn_trace_len)
d = np.loadtxt(params["input_rate_fn_base"] + str(post_id) + ".dat")
post_trace[:trace_len] = d
post_trace[trace_len:] = 0.0
post_trace.resize(bcpnn_trace_len)

for i in xrange(5):
    tau_z = 10
    tau_e = 100
    tau_p = 500 + 500 * i
    # compute
    wij, bias, pi, pj, pij, ei, ej, eij, zi, zj = Bcpnn.get_spiking_weight_and_bias(
        pre_trace, post_trace, get_traces=True, tau_z=tau_z, tau_e=tau_e, tau_p=tau_p, f_max=1000.0
    )
    weight_fn = params["weights_fn_base"] + "%d_%d.dat" % (pre_id, post_id)
    print "Saving to ", weight_fn
    np.savetxt(weight_fn, wij)
    output_fn = params["bias_fn_base"] + "%d_%d.dat" % (pre_id, post_id)
    np.savetxt(output_fn, bias)

    output_fn = params["ztrace_fn_base"] + "%d.dat" % pre_id
    np.savetxt(output_fn, zi)
    output_fn = params["ztrace_fn_base"] + "%d.dat" % post_id
    np.savetxt(output_fn, zj)

    output_fn = params["etrace_fn_base"] + "%d.dat" % pre_id
    np.savetxt(output_fn, ei)
    output_fn = params["etrace_fn_base"] + "%d.dat" % post_id