def test(omegas=[0.5,0.0,1.0]):
    '''
    test reservoir on an unseen combination of omegas
    '''
    #predict
    tot_in_scores = np.zeros([nT,n_neu, 2])
    tot_out_scores = np.zeros([nT,n_neu, 2])

    print "this gesture's omega ", omegas
    inputs, outputs = win.run(nT, framerate=framerate, omegas = omegas)  
         
    d,e,f = np.shape(win.teach_signals)
    teach_sign = np.zeros([nT,n_neu])
    teach_sign[:,0:e*f] = np.reshape(win.teach_signals, [nT,e*f])       
             
    #scaling patching in 256
    #from 6x3 input signals to matrix of nTx256 signals
    #X = np.zeros([nT,n_neu])
    #a,b,c = np.shape(win.inputs_signals)
    #X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])
        

    # Convert input and output spikes to analog signals
    inputs = convert_input(inputs)
    X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 256)
    Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
    zh = liquid.RC_predict (X,Y)
    #pred_in_scores = liquid.RC_score(zh["input"], teach_sign)
    #pred_out_scores = liquid.RC_score(zh["output"], teach_sign)
    
    return X, Y, teach_sign, zh
def plot_svd():
    inputs, outputs, time_exc = win.run(duration, framerate=framerate, neu_sync = nsync, speed=speed)
    Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
    small_inputs = convert_input(inputs)
    X = L.ts2sig(timev, membrane, small_inputs[0][:,0], small_inputs[0][:,1], n_neu = 256)
    figure()
    plot_inputs_small(small_inputs)
    figure()
    plot_inputs_small(outputs)
    figure()
    ac=np.mean(Y**2,axis=0)
    aci=np.mean(X**2,axis=0)
    max_pos = np.where(ac == np.max(ac))[0]
    max_posi = np.where(aci == np.max(aci))[0]
    subplot(3,1,1)
    plot(X[:,max_posi])
    xlabel('time (ds)')
    ylabel('freq (Hz)')
    subplot(3,1,2)
    plot(Y[:,max_pos])
    xlabel('time (ds)')
    ylabel('freq (Hz)')
    subplot(3,1,3)
    CO = np.dot(Y.T,Y)
    CI = np.dot(X.T,X)
    si = np.linalg.svd(CI, full_matrices=True, compute_uv=False)
    so = np.linalg.svd(CO, full_matrices=True, compute_uv=False)
    semilogy(so/so[0], 'bo-', label="outputs")
    semilogy(si/si[0], 'go-', label="inputs")
    xlabel('Singular Value number')
    ylabel('value')
    legend(loc="best") 
def test(omegas=[0.5, 0.0, 1.0]):
    '''
    test reservoir on an unseen combination of omegas
    '''
    #predict
    tot_in_scores = np.zeros([nT, n_neu, 2])
    tot_out_scores = np.zeros([nT, n_neu, 2])

    print "this gesture's omega ", omegas
    inputs, outputs = win.run(nT, framerate=framerate, omegas=omegas)

    d, e, f = np.shape(win.teach_signals)
    teach_sign = np.zeros([nT, n_neu])
    teach_sign[:, 0:e * f] = np.reshape(win.teach_signals, [nT, e * f])

    #scaling patching in 256
    #from 6x3 input signals to matrix of nTx256 signals
    #X = np.zeros([nT,n_neu])
    #a,b,c = np.shape(win.inputs_signals)
    #X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])

    # Convert input and output spikes to analog signals
    inputs = convert_input(inputs)
    X = L.ts2sig(timev, membrane, inputs[0][:, 0], inputs[0][:, 1], n_neu=256)
    Y = L.ts2sig(timev,
                 membrane,
                 outputs[0][:, 0],
                 outputs[0][:, 1],
                 n_neu=256)
    zh = liquid.RC_predict(X, Y)
    #pred_in_scores = liquid.RC_score(zh["input"], teach_sign)
    #pred_out_scores = liquid.RC_score(zh["output"], teach_sign)

    return X, Y, teach_sign, zh
def test_determinism():
    inputs, outputs = win.run(nT, framerate=framerate, neu_sync = nsync, omegas = omegas) 
    Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
    print "we are plotting outputs"
    figure(fig_h.number)
    for i in range(256):
        subplot(16,16,i)
        plot(Y[:,i])
        axis('off')       
def test_determinism(ntrials):
    for i in range(ntrials):
        inputs, outputs, time_exc = win.run(nT, framerate=framerate, neu_sync = nsync)
        Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
        print "we are plotting outputs"
        figure(fig_h.number)
        for i in range(255):
            subplot(16,16,i)
            plot(Y[:,i])
            axis('off')
def plot_svd():
    inputs, outputs, time_exc = win.run(duration,
                                        framerate=framerate,
                                        neu_sync=nsync,
                                        speed=speed)
    Y = L.ts2sig(timev,
                 membrane,
                 outputs[0][:, 0],
                 outputs[0][:, 1],
                 n_neu=256)
    small_inputs = convert_input(inputs)
    X = L.ts2sig(timev,
                 membrane,
                 small_inputs[0][:, 0],
                 small_inputs[0][:, 1],
                 n_neu=256)
    figure()
    plot_inputs_small(small_inputs)
    figure()
    plot_inputs_small(outputs)
    figure()
    ac = np.mean(Y**2, axis=0)
    aci = np.mean(X**2, axis=0)
    max_pos = np.where(ac == np.max(ac))[0]
    max_posi = np.where(aci == np.max(aci))[0]
    subplot(3, 1, 1)
    plot(X[:, max_posi])
    xlabel('time (ds)')
    ylabel('freq (Hz)')
    subplot(3, 1, 2)
    plot(Y[:, max_pos])
    xlabel('time (ds)')
    ylabel('freq (Hz)')
    subplot(3, 1, 3)
    CO = np.dot(Y.T, Y)
    CI = np.dot(X.T, X)
    si = np.linalg.svd(CI, full_matrices=True, compute_uv=False)
    so = np.linalg.svd(CO, full_matrices=True, compute_uv=False)
    semilogy(so / so[0], 'bo-', label="outputs")
    semilogy(si / si[0], 'go-', label="inputs")
    xlabel('Singular Value number')
    ylabel('value')
    legend(loc="best")
def test_determinism(ntrials):
    ion()
    fig_h = figure()
    for i in range(ntrials):
        inputs, outputs, time_exc = win.run(duration, framerate=framerate, neu_sync = nsync, speed=speed)
        Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
        print "we are plotting outputs"
        figure(fig_h.number)
        for i in range(255):
            subplot(16,16,i)
            plot(Y[:,i])
            axis('off')
def test(omegas=[0.5,0.0,1.0], teach_orth = True, ind = 99, this_t = 99):
    '''
    test reservoir on an unseen combination of omegas
    '''
    #predict
    tot_in_scores = np.zeros([nT,n_neu, 2])
    tot_out_scores = np.zeros([nT,n_neu, 2])

    print "this gesture's omega ", omegas
    inputs, outputs, eff_time = win.run(nT, framerate=framerate, omegas = omegas)  

    X = np.zeros([nT,n_neu])
    a,b,c = np.shape(win.inputs_signals)
    X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c]) 
    
    if teach_orth:
        teach_ortogonal_sign = L.orth_signal(X)#np.zeros([nT,n_neu])
        teach_ortogonal = np.zeros([nT,n_neu])
        for i in range(n_neu):
            teach_ortogonal[:,i] = teach_ortogonal_sign
        teach_sign = teach_ortogonal 
    else:    
        d,e,f = np.shape(win.teach_signals)
        teach_sign = np.zeros([nT,n_neu])
        teach_sign[:,0:e*f] = np.reshape(win.teach_signals, [nT,e*f])       
             
    #scaling patching in 256
    #from 6x3 input signals to matrix of nTx256 signals
    #X = np.zeros([nT,n_neu])
    #a,b,c = np.shape(win.inputs_signals)
    #X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])        

    # Convert input and output spikes to analog signals
    #inputs = convert_input(inputs)
    #X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 256)
    Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)

    #pred_in_scores = liquid.RC_score(zh["input"], teach_sign)
    #pred_out_scores = liquid.RC_score(zh["output"], teach_sign)
    if save_data_to_disk:
        np.savetxt("lsm_ret/omegas_gesture_"+str(ind)+"_trial_"+str(this_t)+".txt", omegas)
        print "save data"        
        np.savetxt("lsm_ret/inputs_gesture_"+str(ind)+"_trial_"+str(this_t)+".txt", inputs[0])
        np.savetxt("lsm_ret/outputs_gesture_"+str(ind)+"_trial_"+str(this_t)+".txt", outputs[0])
        for i in range(e*f):
            np.savetxt("lsm_ret/teaching_signals_gesture_"+str(ind)+"_teach_input_"+str(i)+"_trial_"+str(this_t)+".txt", teach_sign[:,i])

    
    return X, Y, teach_sign
def test_determinism():
    inputs, outputs, eff_time = win.run(nT, framerate=framerate, neu_sync = nsync, omegas = omegas) 
    Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
    #X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 128*128)  
    X = np.zeros([nT,n_neu])
    a,b,c = np.shape(win.inputs_signals)
    X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])  

    print "we are plotting outputs"
    figure(fig_h.number)
    for i in range(256):
        subplot(16,16,i)
        plot(Y[:,i])
        axis('off') 
    print "we are plotting svd and inputs/outputs"     
    plot_svd(fig_hh, X, Y)
def test_determinism():
    inputs, outputs, eff_time = win.run(nT,
                                        framerate=framerate,
                                        neu_sync=nsync,
                                        omegas=omegas)
    Y = L.ts2sig(timev,
                 membrane,
                 outputs[0][:, 0],
                 outputs[0][:, 1],
                 n_neu=256)
    #X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 128*128)
    X = np.zeros([nT, n_neu])
    a, b, c = np.shape(win.inputs_signals)
    X[:, 0:b * c] = np.reshape(win.inputs_signals, [nT, b * c])

    print "we are plotting outputs"
    figure(fig_h.number)
    for i in range(256):
        subplot(16, 16, i)
        plot(Y[:, i])
        axis('off')
    print "we are plotting svd and inputs/outputs"
    plot_svd(fig_hh, X, Y)
def train(num_gestures,ntrials):
    '''
    train reservoir with retina inputs
    '''
    for ind in range(num_gestures):

        print "sample omegas from a 3d sphere surface"
        #N = np.random.randint(10000)
        #v = np.random.uniform(size=(3,N)) 
        #vn = v / np.sqrt(np.sum(v**2, 0))
        #omegas = [vn[0,N-1], vn[1,N-1], vn[2,N-1]]
        theta = np.linspace(0,2.0*pi,num_gestures)
        phi  = np.linspace(0,pi/10.0,350) #bigger smaller angle
        T,P = np.meshgrid(theta,phi)
        wX = sin(P)*cos(T)
        wY = sin(P)*sin(T) 
        wZ = cos(P)
        omegas = [wX[ntrials-1,ind], wY[ntrials-1,ind], wZ[ntrials-1,ind]]
        #print "this gesture's omega ", omegas

        #just poke it for the first time... to start in same configuration all the times..
        inputs, outputs = win.run(np.round(nT/10), framerate=framerate, neu_sync = nsync, omegas = omegas)    
        
        for this_t in xrange(ntrials): 
            #omegas = [wX[ind,this_t]+0.5, wY[ind,this_t], wZ[ind,this_t]]
            omegas = [wX[ind,this_t]+0.5, 0.0, 1.0]
            print "this gesture's omega ", omegas
        
            inputs, outputs = win.run(nT, framerate=framerate, neu_sync = nsync, omegas = omegas)        
            #from 6x3 input signals to matrix of nTx256 signals
            #X = np.zeros([nT,n_neu])
            #a,b,c = np.shape(win.inputs_signals)
            #X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])       
            d,e,f = np.shape(win.teach_signals)
            teach_sign = np.zeros([nT,n_neu])
            teach_sign[:,0:e*f] = np.reshape(win.teach_signals, [nT,e*f])    
            
            
            # Convert input and output spikes to analog signals
            if real_time_learn:
                #inputs = convert_input(inputs)
                #X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 256)
                X = np.zeros([nT,n_neu])
                a,b,c = np.shape(win.inputs_signals)
                X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])  

                #learn.. what? tip or something ortogonal to it?                
                if teach_orth:
                    teach_ortogonal_sign = L.orth_signal(X)#np.zeros([nT,n_neu])
                    teach_ortogonal = np.zeros([nT,n_neu])
                    for i in range(n_neu):
                        teach_ortogonal[:,i] = teach_ortogonal_sign
                    teach_sign = teach_ortogonal
                    
                Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)
                
                print "teaching non orthogonal stuff to the input signals..."
                liquid._realtime_learn (X,Y,teach_sign)
                print np.sum(liquid.CovMatrix['input']), np.sum(liquid.CovMatrix['output'])
                #evaluate
                zh = liquid.RC_predict (X,Y)
                score_in = liquid.RC_score(zh["input"], teach_sign)
                score_out = liquid.RC_score(zh["output"], teach_sign)
                tot_scores_in.append(score_in[0:e*f,:])
                tot_scores_out.append(score_out[0:e*f,:])
                #print "we are scoring...", scores                 
                    
            if save_data_to_disk:
                print "save data"        
                np.savetxt("lsm_ret/inputs_gesture_"+str(ind)+"_trial_"+str(this_t)+".txt", inputs[0])
                np.savetxt("lsm_ret/outputs_gesture_"+str(ind)+"_trial_"+str(this_t)+".txt", outputs[0])
                for i in range(e*f):
                    np.savetxt("lsm_ret/teaching_signals_gesture_"+str(ind)+"_teach_input_"+str(i)+"_trial_"+str(this_t)+".txt", teach_sign[:,i])

 
    return X, Y, teach_sign, zh
def train(num_gestures, ntrials):
    '''
    train reservoir with retina inputs
    '''
    for ind in range(num_gestures):

        print "sample omegas from a 3d sphere surface"
        #N = np.random.randint(10000)
        #v = np.random.uniform(size=(3,N))
        #vn = v / np.sqrt(np.sum(v**2, 0))
        #omegas = [vn[0,N-1], vn[1,N-1], vn[2,N-1]]
        theta = np.linspace(0, 2.0 * pi, num_gestures)
        phi = np.linspace(0, pi / 10.0, 350)  #bigger smaller angle
        T, P = np.meshgrid(theta, phi)
        wX = sin(P) * cos(T)
        wY = sin(P) * sin(T)
        wZ = cos(P)
        omegas = [
            wX[ntrials - 1, ind], wY[ntrials - 1, ind], wZ[ntrials - 1, ind]
        ]
        #print "this gesture's omega ", omegas

        #just poke it for the first time... to start in same configuration all the times..
        inputs, outputs = win.run(np.round(nT / 10),
                                  framerate=framerate,
                                  neu_sync=nsync,
                                  omegas=omegas)

        for this_t in xrange(ntrials):
            #omegas = [wX[ind,this_t]+0.5, wY[ind,this_t], wZ[ind,this_t]]
            omegas = [wX[ind, this_t] + 0.5, 0.0, 1.0]
            print "this gesture's omega ", omegas

            inputs, outputs = win.run(nT,
                                      framerate=framerate,
                                      neu_sync=nsync,
                                      omegas=omegas)
            #from 6x3 input signals to matrix of nTx256 signals
            #X = np.zeros([nT,n_neu])
            #a,b,c = np.shape(win.inputs_signals)
            #X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])
            d, e, f = np.shape(win.teach_signals)
            teach_sign = np.zeros([nT, n_neu])
            teach_sign[:, 0:e * f] = np.reshape(win.teach_signals, [nT, e * f])

            # Convert input and output spikes to analog signals
            if real_time_learn:
                #inputs = convert_input(inputs)
                #X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 256)
                X = np.zeros([nT, n_neu])
                a, b, c = np.shape(win.inputs_signals)
                X[:, 0:b * c] = np.reshape(win.inputs_signals, [nT, b * c])

                #learn.. what? tip or something ortogonal to it?
                if teach_orth:
                    teach_ortogonal_sign = L.orth_signal(
                        X)  #np.zeros([nT,n_neu])
                    teach_ortogonal = np.zeros([nT, n_neu])
                    for i in range(n_neu):
                        teach_ortogonal[:, i] = teach_ortogonal_sign
                    teach_sign = teach_ortogonal

                Y = L.ts2sig(timev,
                             membrane,
                             outputs[0][:, 0],
                             outputs[0][:, 1],
                             n_neu=256)

                print "teaching non orthogonal stuff to the input signals..."
                liquid._realtime_learn(X, Y, teach_sign)
                print np.sum(liquid.CovMatrix['input']), np.sum(
                    liquid.CovMatrix['output'])
                #evaluate
                zh = liquid.RC_predict(X, Y)
                score_in = liquid.RC_score(zh["input"], teach_sign)
                score_out = liquid.RC_score(zh["output"], teach_sign)
                tot_scores_in.append(score_in[0:e * f, :])
                tot_scores_out.append(score_out[0:e * f, :])
                #print "we are scoring...", scores

            if save_data_to_disk:
                print "save data"
                np.savetxt(
                    "lsm_ret/inputs_gesture_" + str(ind) + "_trial_" +
                    str(this_t) + ".txt", inputs[0])
                np.savetxt(
                    "lsm_ret/outputs_gesture_" + str(ind) + "_trial_" +
                    str(this_t) + ".txt", outputs[0])
                for i in range(e * f):
                    np.savetxt(
                        "lsm_ret/teaching_signals_gesture_" + str(ind) +
                        "_teach_input_" + str(i) + "_trial_" + str(this_t) +
                        ".txt", teach_sign[:, i])

    return X, Y, teach_sign, zh
def test(omegas=[0.5, 0.0, 1.0], teach_orth=True, ind=99, this_t=99):
    '''
    test reservoir on an unseen combination of omegas
    '''
    #predict
    tot_in_scores = np.zeros([nT, n_neu, 2])
    tot_out_scores = np.zeros([nT, n_neu, 2])

    print "this gesture's omega ", omegas
    inputs, outputs, eff_time = win.run(nT, framerate=framerate, omegas=omegas)

    X = np.zeros([nT, n_neu])
    a, b, c = np.shape(win.inputs_signals)
    X[:, 0:b * c] = np.reshape(win.inputs_signals, [nT, b * c])

    if teach_orth:
        teach_ortogonal_sign = L.orth_signal(X)  #np.zeros([nT,n_neu])
        teach_ortogonal = np.zeros([nT, n_neu])
        for i in range(n_neu):
            teach_ortogonal[:, i] = teach_ortogonal_sign
        teach_sign = teach_ortogonal
    else:
        d, e, f = np.shape(win.teach_signals)
        teach_sign = np.zeros([nT, n_neu])
        teach_sign[:, 0:e * f] = np.reshape(win.teach_signals, [nT, e * f])

    #scaling patching in 256
    #from 6x3 input signals to matrix of nTx256 signals
    #X = np.zeros([nT,n_neu])
    #a,b,c = np.shape(win.inputs_signals)
    #X[:,0:b*c] = np.reshape(win.inputs_signals, [nT,b*c])

    # Convert input and output spikes to analog signals
    #inputs = convert_input(inputs)
    #X = L.ts2sig(timev, membrane, inputs[0][:,0], inputs[0][:,1], n_neu = 256)
    Y = L.ts2sig(timev,
                 membrane,
                 outputs[0][:, 0],
                 outputs[0][:, 1],
                 n_neu=256)

    #pred_in_scores = liquid.RC_score(zh["input"], teach_sign)
    #pred_out_scores = liquid.RC_score(zh["output"], teach_sign)
    if save_data_to_disk:
        np.savetxt(
            "lsm_ret/omegas_gesture_" + str(ind) + "_trial_" + str(this_t) +
            ".txt", omegas)
        print "save data"
        np.savetxt(
            "lsm_ret/inputs_gesture_" + str(ind) + "_trial_" + str(this_t) +
            ".txt", inputs[0])
        np.savetxt(
            "lsm_ret/outputs_gesture_" + str(ind) + "_trial_" + str(this_t) +
            ".txt", outputs[0])
        for i in range(e * f):
            np.savetxt(
                "lsm_ret/teaching_signals_gesture_" + str(ind) +
                "_teach_input_" + str(i) + "_trial_" + str(this_t) + ".txt",
                teach_sign[:, i])

    return X, Y, teach_sign
figure()
signal_trace_good = [raw_data_times[index_ord],signal]                    
plot(signal_trace_good[0], signal_trace_good[1])   
                    
#X = L.ts2sig(timev, membrane, raw_data_times, raw_data_amp, n_neu = 256)
#X = np.loadtxt("bioamp/XXspikes.txt")

# Time vector for analog signals
Fs    = 1000/1e3 # Sampling frequency (in kHz)
T     = 12000
nT    = np.round (Fs*T)
timev = np.linspace(0,T,nT)

#Conversion from spikes to analog
membrane = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*1**2)))
X = L.ts2sig(timev, membrane, raw_data_times, raw_data_amp, n_neu = 256)
       
figure()
for i in range(2):
    plot(X[:,i])
    

#SVD
figure()
ac=np.mean(Y**2,axis=0)
aci=np.mean(X**2,axis=0)
max_pos = np.where(ac == np.max(ac))[0]
max_posi = np.where(aci == np.max(aci))[0]
subplot(3,1,1)
plot(X[:,max_posi])
subplot(3,1,2)
Пример #15
0
        #                                            c = c, 
        #                                            duration=duration,  
        #                                            delay_sync=delay_sync,  
        #                                            max_freq= 2800, min_freq = 400)
    
    
        #nsetup.chips['mn256r1'].load_parameters('biases/biases_reservoir.biases')
        #time.sleep(0.2)    
        #stimulate
        if not use_retina:
            inputs, outputs = liquid.RC_poke(stimulus)
        else:
            inputs, outputs = win.run(counts, framerate=framerate)

        # Convert input and output spikes to analog signals
        X = L.ts2sig(timev, membrane, inputs[0][:,0], np.floor(inputs[0][:,1]))
        Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1])
        
        #if(learn_real_time == True):
        # Calculate activity of current inputs.
        # As of now the reservoir can only give answers during activity
        tmp_ac = np.mean(func_avg(timev[:,None], outputs[0][:,0][None,:]), axis=1) 
        tmp_ac = tmp_ac / np.max(tmp_ac)
        if (np.sum(tmp_ac) > np.sum(ac)) or (this_t==0):   
            ac = tmp_ac[:,None]

        #teach_sig = L.orth_signal(X)*ac.T**4 #
        #teach_sig = L.orth_signal(X)[None,:]
        teach_sig = gesture_teach * ac**4 # Windowed by activity

        #learn