Пример #1
0
def SimSpikingStimulus(stim, time = 1000, t_sim = None, with_labels = True):
    '''
    Times must be sorted. ex: times = [0, 1, 2] ; scale = [1,0]
    *poisson*: integer, output is a poisson process with mean
    data/poisson, scaled by *poisson*.
    '''
    from pyNCSre import pyST
    n = np.shape(stim)[1]
    nc = 10
    stim[stim<=0] = 1e-5
    SL = pyST.SpikeList(id_list = range(n))
    SLd = pyST.SpikeList(id_list = range(n-nc))
    SLc = pyST.SpikeList(id_list = range(n-nc,n))
    for i in range(n-nc):
        SLd[i] = pyST.STCreate.inh_poisson_generator(stim[:,i],
                                                    range(0,len(stim)*time,time),
                                                    t_stop=t_sim, refractory = 4.)
    if with_labels:
        for t in range(0,len(stim)):
            SLt= pyST.SpikeList(id_list = range(n-nc,n))
            for i in range(n-nc,n):
                if stim[t,i]>1e-2:
                    SLt[i] = pyST.STCreate.regular_generator(stim[t,i],
                                                        jitter=True,
                                                        t_start=t*time,
                                                        t_stop=(t+1)*time)            
            if len(SLt.raw_data())>0: SLc = pyST.merge_spikelists(SLc, SLt)

    if len(SLc.raw_data())>0: 
        SL = pyST.merge_spikelists(SLd,SLc)
    else:
        SL = SLd
    return SL
Пример #2
0
def SimSpikingStimulus(stim1, stim2, time=1000, t_sim=None):
    '''
    Times must be sorted. ex: times = [0, 1, 2] ; scale = [1,0] *poisson*:
    integer, output is a poisson process with mean data/poisson, scaled by
    *poisson*.
    '''
    n = stim1.shape[1]
    SL = pyST.SpikeList(id_list=range(n+n-2))

    times = range(0, len(stim1)*time, time)
    for i in range(n):
        if np.any(stim1[:, i] > 0):
            SL[i] = pyST.STCreate.inh_poisson_generator(stim1[:, i],
                                                        times,
                                                        t_stop=t_sim)
    for i in range(n-2):
        if np.any(stim2[:, i] > 0):
            SL[n+i] = pyST.STCreate.inh_poisson_generator(stim2[:, i-n+2],
                                                          times,
                                                          t_stop=t_sim)
    plus_times1 = np.array([100]*32)+np.arange(0,2*time*32,2*time)
    plus_times2 = np.array([2*time]*32)+np.arange(0,2*time*32,2*time)
    plus_times = np.concatenate([plus_times1,plus_times2])

    minus_times1 = np.array([time]*32)+np.arange(0,2*time*32,2*time)
    minus_times2 = np.array([time+100]*32)+np.arange(0,2*time*32,2*time)
    minus_times = np.concatenate([minus_times1,minus_times2])
    
    SL[0] = pyST.SpikeTrain(np.sort(plus_times))
    SL[1] = pyST.SpikeTrain(np.sort(minus_times))
    return SL
Пример #3
0
def RegularSpikingStimulus(freqs, ticks=1000):
    N_NEURONS = np.shape(freqs)[0]
    SL = pyST.SpikeList(id_list=list(range(N_NEURONS)))
    for i in range(N_NEURONS):
        f = freqs[i]
        if f > 0:
            SL[i] = pyST.STCreate.regular_generator(freqs[i], t_stop=ticks)
    return nsat.exportAER(SL)
Пример #4
0
def SimSpikingStimulus(rates=[5, 10], t_start=1000, t_stop=4000):
    n = np.shape(rates)[0]
    SL = pyST.SpikeList(id_list=list(range(n)))
    for i in range(n):
        SL[i] = pyST.STCreate.regular_generator(rates[i],
                                                t_start=t_start,
                                                t_stop=t_stop,
                                                jitter=False)
        # SL[i] = pyST.STCreate.poisson_generator(rates[i], t_start, t_stop)
    return SL
Пример #5
0
def SimSpikingStimulus(stim, t=1000, t_sim=None):
    '''
    Times must be sorted. ex: times = [0, 1, 2] ; scale = [1,0]
    *poisson*: integer, output is a poisson process with mean
    data/poisson, scaled by *poisson*.
    '''
    n = np.shape(stim)[0]
    SL = pyST.SpikeList(id_list=list(range(n)))
    for i in range(n):
        SL[i] = pyST.STCreate.regular_generator(stim[i], t_stop=t_sim)
    return SL
Пример #6
0
def SimSpikingStimulus(t_sim=None):
    SL = pyST.SpikeList(id_list=[0, 1, 2, 3])
    spk_train0 = [60, 110]
    spk_train1 = [10, 75, 140]
    spk_train2 = [15, 80, 135]
    spk_train3 = [20, 130]
    SL[0] = pyST.SpikeTrain(spk_train0, t_start=1)
    SL[1] = pyST.SpikeTrain(spk_train1, t_start=1)
    SL[2] = pyST.SpikeTrain(spk_train2, t_start=1)
    SL[3] = pyST.SpikeTrain(spk_train3, t_start=1)
    return SL
Пример #7
0
def PoissonSpikingStimulus(rates, n_inputs=2):
    SL = pyST.SpikeList(id_list=list(range(n_inputs)))
    for i in range(n_inputs):
        # Tracking
        # if i > 0 and i < 20:
        #     t_start = 0
        #     t_stop = 500
        #     rate = 50
        # elif i > 20 and i < 40:
        #     t_start = 500
        #     t_stop = 1000
        #     rate = 50
        # elif i > 40 and i < 60:
        #     t_start = 1000
        #     t_stop = 1500
        #     rate = 50
        # elif i > 60 and i < 80:
        #     t_start = 1500
        #     t_stop = 2000
        #     rate = 50
        # elif i > 80 and i < 100:
        #     t_start = 2000
        #     t_stop = 2500
        #     rate = 50
        # else:
        #     continue
        # SL[i] = pyST.STCreate.poisson_generator(rate,
        #                                         t_start,
        #                                         t_stop)

        t_start = 100
        t_stop = 500
        rate = 1
        if (i > 40 and i < 60):
            t_start = 100
            t_stop = 500
            rate = 35
        if (i < 40 or i > 60):
            t_start = 100
            t_stop = 500
            rate = 10
        SL[i] = pyST.STCreate.poisson_generator(rate, t_start, t_stop)

        # Selection
        # if (i > 20 and i < 40) or (i > 70 and i < 90):
        #     t_start = 100
        #     t_stop = 500
        #     rate = 50
        #     SL[i] = pyST.STCreate.poisson_generator(rate,
        #                                             t_start,
        #                                             t_stop)
    return nsat.exportAER(SL), SL
Пример #8
0
def PeriodicPrePostSpikingStimulus(freqs, diff, ticks=1000):
    SL = pyST.SpikeList(id_list=list(range(2)))
    base_phase = 100.0
    shift_phase = base_phase + diff

    SL[0] = pyST.STCreate.regular_generator(freqs,
                                            t_start=0,
                                            phase=base_phase,
                                            jitter=False,
                                            t_stop=ticks,
                                            array=False)
    SL[1] = pyST.STCreate.regular_generator(freqs,
                                            t_start=0,
                                            phase=shift_phase,
                                            jitter=False,
                                            t_stop=ticks,
                                            array=False)
    return SL
Пример #9
0
def RegularSpikingStimulus(freqs, ticks=1000):
    pyST.STCreate.seed(100)
    m = np.shape(freqs)[0]
    SL = pyST.SpikeList(id_list=list(range(m)))

    tm = 500
    for i in range(m):
        # SL[i] = pyST.STCreate.regular_generator(freqs[i],
        #                                         t_start=1,
        #                                         t_stop=ticks)
        if i != (m - 1):
            t_start = tm
            t_stop = tm + 2000
            SL[i] = pyST.STCreate.poisson_generator(freqs[i], t_start, t_stop)
            tm += 2000
        SL[m - 2] = SL[m - 3]
        SL[m - 1] = SL[m - 3]

    return SL
Пример #10
0
def SimSpikingStimulus(rates, t_sim=None):
    m = np.shape(rates)[0]
    n = int(m / 2)

    C1 = (np.ones((n, n)) + np.random.uniform(0, 1, (n, n)) * 2)
    np.fill_diagonal(C1, rates[:n])
    C1 = np.maximum(C1, C1.T)

    cor_spk1 = correlated_spikes(C1, rates, n)
    cor_spk1.cox_process(time=t_sim)
    spk1 = cor_spk1.extract_pyNCS_list()
    tmp1 = nsat.importAER(spk1)

    SL = pyST.SpikeList(id_list=list(range(m)))
    for i in range(m):
        if i < n:
            SL[i] = tmp1[i]
        if i >= n:
            SL[i] = pyST.STCreate.poisson_generator(rates[i], t_stop=t_sim)
    return SL
Пример #11
0
def SimSpikingStimulus(N_INPUTS, **kwargs):
    '''
    Times must be sorted. ex: times = [0, 1, 2] ; scale = [1,0]
    *poisson*: integer, output is a poisson process with mean data/poisson, scaled by *poisson*.
    '''
    ids, spkt, tp = genCoincidencePattern(N=N_INPUTS,
                                          t=kwargs['t_sim'],
                                          pf=kwargs['pf'],
                                          rate=kwargs['f'],
                                          Nf_co=0.1,
                                          jitter=kwargs['jitter'])

    # Input spike pattern
    stimSpk = np.concatenate([[ids], [spkt]]).T
    stimSpk = stimSpk.astype(int)
    SL = nsat.build_SpikeList(spkt, ids)
    add = SL.id_list().astype('i')
    SL_shuffled = pyST.SpikeList(id_list=range(max(add)))
    # g = np.arange(110)
    np.random.shuffle(add)
    for i, k in enumerate(add):
        SL_shuffled[i] = SL[k]
    return SL_shuffled, tp
Пример #12
0
def setup():
    global SL
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    np.random.seed(100)  # Numpy RNG seed
    pyST.STCreate.seed(130)  # PyNCS RNG seed
    N_CORES = 1  # Number of cores
    N_NEURONS = [100]  # Number of neurons per core
    N_INPUTS = [101]  # Number of inputs per core
    N_STATES = [4]  # Number of states pare core
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total units

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN
    NLRN_GROUPS = 8
    N_GROUPS = 8

    # Main class instance
    cfg = nsat.ConfigurationNSAT(sim_ticks=sim_ticks,
                                 N_CORES=N_CORES,
                                 N_INPUTS=N_INPUTS,
                                 N_NEURONS=N_NEURONS,
                                 N_STATES=N_STATES,
                                 monitor_states=True,
                                 monitor_spikes=True,
                                 monitor_weights=True,
                                 w_check=False,
                                 plasticity_en=np.array([True], 'bool'),
                                 ben_clock=True)

    cfg.core_cfgs[core].sigma[0] = [0, 0, 10, 0]

    # Transition matrix group 0
    cfg.core_cfgs[core].A[0] = [[-5, OFF, OFF, OFF], [3, -5, OFF, OFF],
                                [OFF, OFF, -6, OFF], [OFF, OFF, OFF, OFF]]

    # Transition matrix group 1
    cfg.core_cfgs[core].A[1] = [[OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                                [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]

    # Sign matrix group 0
    cfg.core_cfgs[core].sA[0] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1],
                                 [1, 1, 1, -1]]

    # Threshold
    cfg.core_cfgs[core].Xth[0] = 25000
    # Refractory period
    cfg.core_cfgs[core].t_ref[0] = 40
    # Bias
    cfg.core_cfgs[core].b[0] = [0, 0, 0, 0]
    # Initial conditions
    cfg.core_cfgs[core].Xinit = np.array([[0, 0, 0, 0]
                                          for _ in range(N_NEURONS[0])])
    # Reset value
    cfg.core_cfgs[core].Xreset[0] = [0, MAX, MAX, MAX]
    # Turn reset on
    cfg.core_cfgs[core].XresetOn[0] = [True, False, False, False]

    # Turn plasticity per state on
    cfg.core_cfgs[core].plastic[0] = True
    # Turn stdp per state on
    cfg.core_cfgs[core].stdp_en[0] = True
    cfg.core_cfgs[core].is_stdp_exp_on[0] = True

    # Global modulator state
    cfg.core_cfgs[core].modstate[0] = 2

    # Parameters groups mapping function
    cfg.core_cfgs[core].nmap = np.zeros((N_NEURONS[0], ), dtype='int')
    lrnmap = 1 + np.zeros((N_GROUPS, N_STATES[0]), dtype='int')
    lrnmap[0, 1] = 0
    cfg.core_cfgs[core].lrnmap = lrnmap

    # Synaptic weights
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[core]], 'int')
    W[0:100, N_INPUTS[core]:, 1] = np.eye(N_NEURONS[core]) * 100
    W[100, N_INPUTS[core]:, 2] = 100

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='int')
    CW[0:100, N_INPUTS[core]:, 1] = np.eye(N_NEURONS[core])
    CW[100, N_INPUTS[core]:, 2] = 1

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[core].wgt_table = wgt_table
    cfg.core_cfgs[core].ptr_table = ptr_table

    # Learning STDP parameters
    cfg.core_cfgs[core].tca = [[24, 48] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].hica = [[-3, -5, -6] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].sica = [[1, 1, 1] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].slca = [[16, 16, 16] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].tac = [[-32, -64] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].hiac = [[-6, -8, -9] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].siac = [[-1, -1, -1] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].slac = [[16, 16, 16] for _ in range(NLRN_GROUPS)]

    # Prepare external stimulus (spikes events)
    stim = [50] * N_NEURONS[core]
    SL = SimSpikingStimulus(stim, t_sim=sim_ticks)
    SLr = pyST.SpikeList(id_list=[100])
    SLr[100] = pyST.STCreate.inh_poisson_generator(rate=np.array(
        [0., 100., 0.]),
                                                   t=np.array([0, 400, 600]),
                                                   t_stop=sim_ticks)
    SL = pyST.merge_spikelists(SL, SLr)
    ext_evts_data = nsat.exportAER(SL)
    cfg.set_ext_events(ext_evts_data)

    # Write C NSAT parameters binary file
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_rSTDP')
    c_nsat_writer.write()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
Пример #13
0
    cfg.core_cfgs[core].ptr_table = ptr_table

    # Learning STDP parameters
    cfg.core_cfgs[core].tca  = [[24, 48] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].hica = [[-3, -5, -6] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].sica = [[1, 1, 1] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].slca = [[16, 16, 16] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].tac  = [[-32, -64] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].hiac = [[-6, -8, -9] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].siac = [[-1, -1, -1] for _ in range(NLRN_GROUPS)]
    cfg.core_cfgs[core].slac = [[16, 16, 16] for _ in range(NLRN_GROUPS)]

    # Prepare external stimulus (spikes events)
    stim = [50]*N_NEURONS[core]
    SL = SimSpikingStimulus(stim, t_sim=sim_ticks)
    SLr = pyST.SpikeList(id_list=[100])
    SLr[100] = pyST.STCreate.inh_poisson_generator(rate=np.array([0., 100., 0.]),
                                                 t=np.array([0, 400, 600]),
                                                 t_stop=sim_ticks)
    SL = pyST.merge_spikelists(SL, SLr)
    ext_evts_data = nsat.exportAER(SL)
    cfg.set_ext_events(ext_evts_data)

    # Write C NSAT parameters binary file
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_rSTDP')
    c_nsat_writer.write()

    # Write Intel FPGA parameters hex files
#    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
#                                             prefix='test_rSTDP')
#    intel_fpga_writer.write()