Ejemplo n.º 1
0
def do_trial(what='teach', this_trial=1, teacher=None):
    raw_data_input = np.loadtxt(
        'reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_input_data_fede_format_20020508_pen_3_rec_2_'
        + str(this_trial) + '.txt')
    raw_data = np.loadtxt(
        'reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_data_fede_format_20020508_pen_3_rec_2_'
        + str(this_trial) + '.txt')

    #teach on reconstructed signal
    X = L.ts2sig(timev,
                 membrane,
                 raw_data_input[:, 0],
                 raw_data_input[:, 1],
                 n_neu=256)
    Y = L.ts2sig(timev, membrane, raw_data[:, 0], raw_data[:, 1], n_neu=256)

    if what == 'teach':
        teach_and_plot(X,
                       Y,
                       teach_sig,
                       timev,
                       show_activations=show_activations,
                       teacher=teach_sig)
    if what == 'test':
        test_and_plot(X,
                      Y,
                      teach_sig,
                      timev,
                      show_activations=show_activations,
                      teacher=teach_sig)
Ejemplo n.º 2
0
def analyze_data():

    root_data = 'reservoir_data/'
    datadir_1 = 'data_fede_format_20020508_pen_3_rec_2'
    datadir_2 = 'data_fede_format_20020608_pen_2_rec_2'
    num_trials = 3
    duration_sync = 4000
    datadir = datadir_1
    what = 'teach'
    sync_bioamp_channel = 305
    delta_mem = 25
    smoothing_len = 100
    duration_rec = 6000
    duration = 6000
    show_activations = True

    for this_trial in range(1,num_trials):
        raw_data_input = np.loadtxt('reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_input_data_fede_format_20020508_pen_3_rec_2_'+str(this_trial)+'.txt')
        raw_data = np.loadtxt('reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_data_fede_format_20020508_pen_3_rec_2_'+str(this_trial)+'.txt')

        ############# MEMBRANE
        # Time vector for analog signals
        Fs    = 1000/1e3 # Sampling frequency (in kHz)
        T     = duration
        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*delta_mem**2)))
        
        #teach on reconstructed signal
        X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 256)
        Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)     
        #generate teaching signal orthogonal signal to input
        teach = np.loadtxt('/home/federico/projects/work/Berkeley_wehr/Tools/'+datadir+'/1_.txt')#
        framepersec = len(teach)/15
        teach = teach[0:framepersec/(duration_rec/1000)]
        #interpolate to match lenght
        signal_ad = [np.linspace(0,duration_rec,len(teach)), teach]
        ynew = np.linspace(0,duration_rec,nT+1)
        s = interpolate.interp1d(signal_ad[0], signal_ad[1],kind="linear")

        teach_sig = s(ynew)     
        teach_sig = np.abs(sigtool.hilbert(teach_sig)) #get envelope
        teach_sig = smooth(teach_sig,window_len=smoothing_len,window='hanning')
     
        if what == 'teach':
            teach_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
        if what == 'test':
            test_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
def test_determinism():
    #inputs, outputs = res.stimulate_reservoir(stimulus, neu_sync = 10,  trials=ntrials)     
    Y = L.ts2sig(timev, membrane, outputs[0][:,0], outputs[0][:,1], n_neu = 256)

    figure(fid_h.number)
    for i in range(256):
        subplot(16,16,i)
        plot(Y[:,i])
        axis('off') 
Ejemplo n.º 4
0
def plot_determinism(filename, delta_mem=60, sync_bioamp_channel=305):
    rec = load_recordings(filename)
    n_trial = len(rec)

    fig_h = figure()
    fig_hh = figure()
    # Time vector for analog signals
    Fs = 100 / 1e3  # Sampling frequency (in kHz)
    T = np.max(rec[0][0].raw_data()[:, 0]) - np.min(
        rec[0][0].raw_data()[:, 0])  #assume all recordings have same lenght
    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 * delta_mem**2)))

    all_sign = []
    all_out = []
    for this_trial in range(n_trial):
        raw_data = rec[this_trial][0].raw_data()
        raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])

        #use delta as syn
        sync_index = raw_data[:, 1] == sync_bioamp_channel
        sync_index = np.where(sync_index)[0]
        time_start = raw_data[sync_index[0], 0]
        #delete what happen before the syn index
        index_to_del = np.where(raw_data[:, 0] < time_start)
        raw_data = np.delete(raw_data, index_to_del, axis=0)
        #delete bioamp spikes
        index_to_del = np.where(raw_data[:, 1] > 255)
        raw_data = np.delete(raw_data, index_to_del, axis=0)
        Y = L.ts2sig(timev,
                     membrane,
                     raw_data[:, 0],
                     raw_data[:, 1],
                     n_neu=256)
        figure(fig_h.number)
        for i in range(255):
            subplot(16, 16, i)
            plot(Y[:, i])
            axis('off')

        if (this_trial == 0):
            index_non_zeros = []
            for i in range(256):
                if (np.sum(Y[:, i]) != 0):
                    index_non_zeros.append(i)
            size_f = np.floor(np.sqrt(len(index_non_zeros)))
        figure(fig_hh.number)
        for i in range(int(size_f**2)):
            subplot(size_f, size_f, i)
            plot(Y[:, index_non_zeros[i]])
            axis('off')
def test_determinism():
    #inputs, outputs = res.stimulate_reservoir(stimulus, neu_sync = 10,  trials=ntrials)
    Y = L.ts2sig(timev,
                 membrane,
                 outputs[0][:, 0],
                 outputs[0][:, 1],
                 n_neu=256)

    figure(fid_h.number)
    for i in range(256):
        subplot(16, 16, i)
        plot(Y[:, i])
        axis('off')
Ejemplo n.º 6
0
def plot_determinism(filename,delta_mem=60, sync_bioamp_channel = 305):
    rec = load_recordings(filename)
    n_trial = len(rec)

    fig_h = figure()
    fig_hh = figure()
    # Time vector for analog signals
    Fs    = 100/1e3 # Sampling frequency (in kHz)
    T     = np.max(rec[0][0].raw_data()[:,0])- np.min(rec[0][0].raw_data()[:,0]) #assume all recordings have same lenght 
    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*delta_mem**2)))

    all_sign = []
    all_out = []
    for this_trial in range(n_trial):
        raw_data = rec[this_trial][0].raw_data()
        raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])

        #use delta as syn
        sync_index = raw_data[:,1] == sync_bioamp_channel
        sync_index = np.where(sync_index)[0]
        time_start = raw_data[sync_index[0],0]
        #delete what happen before the syn index
        index_to_del = np.where(raw_data[:,0] < time_start)
        raw_data = np.delete(raw_data, index_to_del,axis=0)
        #delete bioamp spikes
        index_to_del = np.where(raw_data[:,1] > 255)
        raw_data = np.delete(raw_data, index_to_del,axis=0)
        Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)
        figure(fig_h.number)
        for i in range(255):
            subplot(16,16,i)
            plot(Y[:,i])
            axis('off')
      
        if (this_trial == 0):
            index_non_zeros = []
            for i in range(256):
                if(np.sum(Y[:,i]) != 0):
                    index_non_zeros.append(i)  
            size_f = np.floor(np.sqrt(len(index_non_zeros)))
        figure(fig_hh.number)
        for i in range(int(size_f**2)):
            subplot(size_f,size_f,i) 
            plot(Y[:,index_non_zeros[i]])  
            axis('off')
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
def encode_and_poke(duration_rec=15000,
                    delta_mem=25,
                    sync_bioamp_channel=305,
                    smoothing_len=90,
                    trial=1,
                    what='teach',
                    datadir='data_fede_format_20020508_pen_3_rec_2',
                    show_activations=False,
                    save_data=1,
                    save_data_dir='reservoir_data'):

    duration_sync = 4000
    #figs = figure()
    data_command = '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/' + datadir + '/do_stim.sh'
    command1 = subprocess.Popen(['sh', data_command, str(trial)])
    #command1 = subprocess.Popen(['sh', '/home/federico/projects/work/trunk/data/Insects/Insect Neurophys Data/do_teach.sh'])
    out = nsetup.stimulate({},
                           duration=duration_rec + duration_sync,
                           send_reset_event=False)
    command1 = subprocess.Popen(['killall', 'aplay'])
    #signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)

    #use delta as syn
    raw_data = out[0].raw_data()
    sync_index = raw_data[:, 1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2], 0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:, 0] < time_start + duration_sync)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    #make it start from zero
    raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])
    duration = np.max(raw_data[:, 0])

    ############# MEMBRANE
    # Time vector for analog signals
    Fs = 1000 / 1e3  # Sampling frequency (in kHz)
    T = duration
    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 * delta_mem**2)))

    #extract input and output
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:, 1] == dnch)[0]
    index_up = np.where(raw_data[:, 1] == upch)[0]
    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn, :])
    raw_data_input.extend(raw_data[index_up, :])
    raw_data_input = np.reshape(raw_data_input,
                                [len(index_dn) + len(index_up), 2])
    index_up = np.where(raw_data_input[:, 1] == upch)[0]
    index_dn = np.where(raw_data_input[:, 1] == dnch)[0]
    raw_data_input[index_dn, 1] = 1
    raw_data_input[index_up, 1] = 0
    raw_data = out[0].raw_data()
    sync_index = raw_data[:, 1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2], 0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:, 0] < time_start)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    #make it start from zero
    raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])

    index_to_del = np.where(raw_data[:, 1] > 255)
    raw_data = np.delete(raw_data, index_to_del, axis=0)

    np.savetxt(
        save_data_dir + '/raw_data_input_' + str(datadir) + '_' + str(trial) +
        '.txt', raw_data_input)
    np.savetxt(
        save_data_dir + '/raw_data_' + str(datadir) + '_' + str(trial) +
        '.txt', raw_data)

    #teach on reconstructed signal
    X = L.ts2sig(timev,
                 membrane,
                 raw_data_input[:, 0],
                 raw_data_input[:, 1],
                 n_neu=256)
    Y = L.ts2sig(timev, membrane, raw_data[:, 0], raw_data[:, 1], n_neu=256)
    #generate teaching signal orthogonal signal to input
    teach = np.loadtxt(
        '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'
        + datadir + '/1_.txt')  #
    #teach = np.fromfile(open(trainfile),np.int16)[24:]
    #teach = teach/np.max(teach)
    framepersec = len(teach) / 15
    teach = teach[0:framepersec / (duration_rec / 1000)]
    #interpolate to match lenght
    signal_ad = [np.linspace(0, duration_rec, len(teach)), teach]
    ynew = np.linspace(0, duration_rec, nT + 1)
    s = interpolate.interp1d(signal_ad[0], signal_ad[1], kind="linear")

    teach_sig = s(ynew)
    teach_sig = np.abs(sigtool.hilbert(teach_sig))  #get envelope
    teach_sig = smooth(teach_sig, window_len=smoothing_len, window='hanning')

    if what == 'teach':
        teach_and_plot(X,
                       Y,
                       teach_sig,
                       timev,
                       show_activations=show_activations)
    if what == 'test':
        test_and_plot(X,
                      Y,
                      teach_sig,
                      timev,
                      show_activations=show_activations)
        def test_determinism(duration_rec, n_trial=3, delta_mem=60, delta_up=15, plot_svd = False):
            fig_h = figure()
            fig_hh = figure()
            figs = figure()
            # Time vector for analog signals
            Fs    = 100/1e3 # Sampling frequency (in kHz)
            T     = duration_rec
            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*delta_mem**2)))
            membrane_up = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*delta_up**2)))
            
            #nsetup.mapper._program_detail_mapping(2**3+2**4)  
            command1 = subprocess.Popen(['sh', '/home/federico/project/work/trunk/data/Insects/Insect Neurophys Data/do_stim.sh'])
            out = nsetup.stimulate({},duration=duration_rec)
            command1 = subprocess.Popen(['killall', 'aplay'])
            all_sign = []
            for this_trial in range(n_trial):
                command1 = subprocess.Popen(['sh', '/home/federico/project/work/trunk/data/Insects/Insect Neurophys Data/do_stim.sh'])
                #time.sleep(0.2)
                out = nsetup.stimulate({},duration=duration_rec)
                signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)
                all_sign.append(signal)
                command1 = subprocess.Popen(['killall', 'aplay'])
                #time.sleep(1)
                #nsetup.mapper._program_detail_mapping(0) 
                raw_data = out[0].raw_data()
                index_to_del = np.where(raw_data[:,1] > 255)
                raw_data = np.delete(raw_data, index_to_del,axis=0)
                Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)
                figure(fig_h.number)
                for i in range(255):
                    subplot(16,16,i)
                    plot(Y[:,i])
                    axis('off')
              
                if (this_trial == 0):
                    index_non_zeros = []
                    for i in range(256):
                        if(np.sum(Y[:,i]) != 0):
                            index_non_zeros.append(i)  
                    size_f = np.floor(np.sqrt(len(index_non_zeros)))
                figure(fig_hh.number)
                for i in range(int(size_f**2)):
                    subplot(size_f,size_f,i) 
                    plot(Y[:,index_non_zeros[i]])  
                    axis('off')
            
            #out = nsetup.stimulate({},duration=duration_rec)  
            
              
            if(plot_svd == True): 
                #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(signal[:,0],signal[:,1])
                subplot(3,1,2)
                plot(Y[:,max_pos])
                subplot(3,1,3)
                CO = np.dot(Y.T,Y)
                CI = np.dot(signal,signal.T)
                si = np.linalg.svd(CI, full_matrices=False, 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")
                legend(loc="best")   
            
            raw_data = out[0].raw_data()
            index_to_del = np.where(raw_data[:,1] > 255)
            raw_data = np.delete(raw_data, index_to_del,axis=0)
            membrane = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*delta_mem**2)))
            Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)
            
            figure()
            #raster plot lsm   
            vlines(raw_data[:,0], raw_data[:,1] + .5, raw_data[:,1] + 1.5)
            ylim([0,256])
            xlim([0,duration_rec])
            xlabel('Time [ms]')
            ylabel('Neu Id')
  
            figure()
            #raster plot lsm   
            plot(raw_data[:,0], raw_data[:,1], '*', markersize=2)
            ylim([0,256])
            xlim([0,duration_rec])
            xlabel('Time [ms]')
            ylabel('Neu Id')
Ejemplo n.º 10
0
def test_determinism(duration_rec,
                     n_trial=3,
                     delta_mem=60,
                     delta_up=15,
                     plot_svd=False,
                     datadir='data_fede_format_20020508_pen_3_rec_2',
                     trial=1,
                     sync_bioamp_channel=305):

    data_command = '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/' + datadir + '/do_stim.sh'
    duration_sync = 1600

    fig_h = figure()
    fig_hh = figure()
    #figs = figure()
    # Time vector for analog signals
    Fs = 100 / 1e3  # Sampling frequency (in kHz)
    T = duration_rec
    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 * delta_mem**2)))
    membrane_up = lambda t, ts: np.atleast_2d(
        np.exp((-(t - ts)**2) / (2 * delta_up**2)))

    #nsetup.mapper._program_detail_mapping(2**3+2**4)
    #command1 = subprocess.Popen(['sh', data_command, str(trial)])
    #out = nsetup.stimulate({},duration=duration_rec, send_reset_event=True)
    #command1 = subprocess.Popen(['killall', 'aplay'])
    all_sign = []
    all_out = []
    for this_trial in range(n_trial):
        command1 = subprocess.Popen(['sh', data_command, str(trial)])
        #time.sleep(1.2)
        out = nsetup.stimulate({}, duration=0.2, send_reset_event=True)
        out = nsetup.stimulate({}, duration=0.2, send_reset_event=False)
        out = nsetup.stimulate({},
                               duration=duration_rec + duration_sync,
                               send_reset_event=False)
        all_out.append(out)
        signal = go_reconstruct_signal_from_out(out,
                                                fig_h,
                                                upch=300,
                                                dnch=305,
                                                delta_up=0.1,
                                                delta_dn=0.1,
                                                do_detrend=False,
                                                do_plot=False)
        all_sign.append(signal)
        time.sleep(1.2)
        command1 = subprocess.Popen(['killall', 'aplay'])
        #time.sleep(1)
        #nsetup.mapper._program_detail_mapping(0)
        #use delta as syn
        raw_data = out[0].raw_data()
        sync_index = raw_data[:, 1] == sync_bioamp_channel
        sync_index = np.where(sync_index)[0]
        time_start = raw_data[sync_index[0], 0]
        #delete what happen before the syn index
        index_to_del = np.where(raw_data[:, 0] < time_start + duration_sync)
        raw_data = np.delete(raw_data, index_to_del, axis=0)
        #make it start from zero
        raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])
        duration = np.max(raw_data[:, 0])

        index_to_del = np.where(raw_data[:, 1] > 255)
        raw_data = np.delete(raw_data, index_to_del, axis=0)
        Y = L.ts2sig(timev,
                     membrane,
                     raw_data[:, 0],
                     raw_data[:, 1],
                     n_neu=256)
        figure(fig_h.number)
        for i in range(255):
            subplot(16, 16, i)
            plot(Y[:, i])
            axis('off')

        if (this_trial == 0):
            index_non_zeros = []
            for i in range(256):
                if (np.sum(Y[:, i]) != 0):
                    index_non_zeros.append(i)
            size_f = np.floor(np.sqrt(len(index_non_zeros)))
        figure(fig_hh.number)
        for i in range(int(size_f**2)):
            subplot(size_f, size_f, i)
            plot(Y[:, index_non_zeros[i]])
            axis('off')

    return all_sign, all_out
Ejemplo n.º 11
0
def mini_encoding(duration_rec=3000, delta_mem=10):
    #from scipy.fftpack import fft
    #yyf = fft(Y[:,index_non_zeros[i]])

    import wave
    import sys
    spf = wave.open(
        '/home/federico/project/work/trunk/data/Insects/Insect Neurophys Data/Hackenelektrodenableitung_mecopoda_elongata_chirper_2_trim.wav',
        'r')
    #Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, 'Int16')
    fs = spf.getframerate()
    time_orig = np.linspace(0, len(signal) / fs, num=len(signal))

    from scipy import interpolate
    s = interpolate.interp1d(time_orig * 1000, signal, kind="linear")
    time_n = np.linspace(0, np.max(time_orig) * 1000, 10000)
    ynew = s(time_n)  #interpolate.splev(xnew, tck, der=0)

    #figure()
    #plot(time_orig,signal)
    mul_f = np.ceil(duration_rec / (np.max(time_orig) * 1000))
    time_orig = np.linspace(0, np.max(time_n) * mul_f, len(time_n) * mul_f)
    signal_f = []
    for i in range(int(mul_f)):
        signal_f.append(ynew)
    signal = np.reshape(signal_f, [len(ynew) * mul_f])

    figure()
    plot(time_orig, signal)

    #fig_h = figure()
    fig_hh = figure()

    # Time vector for analog signals
    Fs = 100 / 1e3  # Sampling frequency (in kHz)
    T = duration_rec
    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 * delta_mem**2)))

    out = nsetup.stimulate({}, duration=duration_rec)
    raw_data = out[0].raw_data()
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:, 1] == dnch)[0]
    index_up = np.where(raw_data[:, 1] == upch)[0]

    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn, :])
    raw_data_input.extend(raw_data[index_up, :])
    raw_data_input = np.reshape(raw_data_input,
                                [len(index_dn) + len(index_up), 2])
    index_up = np.where(raw_data_input[:, 1] == upch)[0]
    index_dn = np.where(raw_data_input[:, 1] == dnch)[0]
    raw_data_input[index_dn, 1] = 1
    raw_data_input[index_up, 1] = 0
    X = L.ts2sig(timev,
                 membrane,
                 raw_data_input[:, 0],
                 raw_data_input[:, 1],
                 n_neu=4)
    figure()
    for i in range(2):
        plot(X[:, i])

    raw_data = out[0].raw_data()
    index_to_del = np.where(raw_data[:, 1] > 255)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    Y = L.ts2sig(timev, membrane, raw_data[:, 0], raw_data[:, 1], n_neu=256)

    index_non_zeros = []
    for i in range(256):
        if (np.sum(Y[:, i]) != 0):
            index_non_zeros.append(i)
    size_f = np.floor(np.sqrt(len(index_non_zeros)))
    figure(fig_hh.number)
    for i in range(int(size_f**2)):
        #subplot(size_f,size_f,i)
        plot(Y[:, index_non_zeros[i]])
        #axis('off')
    figure()
    for i in range(int(size_f**2)):
        subplot(size_f, size_f, i)
        plot(Y[:, index_non_zeros[i]])
        ylim([0, 3])
        axis('off')

    figure()
    #raster plot lsm
    plot(raw_data[:, 0], raw_data[:, 1], '*', markersize=2)
    ylim([0, 256])
    xlim([0, duration_rec])
    xlabel('Time [ms]')
    ylabel('Neu Id')
Ejemplo n.º 12
0
def analyze_data():

    root_data = 'reservoir_data/'
    datadir_1 = 'data_fede_format_20020508_pen_3_rec_2'
    datadir_2 = 'data_fede_format_20020608_pen_2_rec_2'
    num_trials = 3
    duration_sync = 4000
    datadir = datadir_1
    what = 'teach'
    sync_bioamp_channel = 305
    delta_mem = 25
    smoothing_len = 100
    duration_rec = 6000
    duration = 6000
    show_activations = True

    for this_trial in range(1, num_trials):
        raw_data_input = np.loadtxt(
            'reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_input_data_fede_format_20020508_pen_3_rec_2_'
            + str(this_trial) + '.txt')
        raw_data = np.loadtxt(
            'reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_data_fede_format_20020508_pen_3_rec_2_'
            + str(this_trial) + '.txt')

        ############# MEMBRANE
        # Time vector for analog signals
        Fs = 1000 / 1e3  # Sampling frequency (in kHz)
        T = duration
        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 * delta_mem**2)))

        #teach on reconstructed signal
        X = L.ts2sig(timev,
                     membrane,
                     raw_data_input[:, 0],
                     raw_data_input[:, 1],
                     n_neu=256)
        Y = L.ts2sig(timev,
                     membrane,
                     raw_data[:, 0],
                     raw_data[:, 1],
                     n_neu=256)
        #generate teaching signal orthogonal signal to input
        teach = np.loadtxt(
            '/home/federico/projects/work/Berkeley_wehr/Tools/' + datadir +
            '/1_.txt')  #
        framepersec = len(teach) / 15
        teach = teach[0:framepersec / (duration_rec / 1000)]
        #interpolate to match lenght
        signal_ad = [np.linspace(0, duration_rec, len(teach)), teach]
        ynew = np.linspace(0, duration_rec, nT + 1)
        s = interpolate.interp1d(signal_ad[0], signal_ad[1], kind="linear")

        teach_sig = s(ynew)
        teach_sig = np.abs(sigtool.hilbert(teach_sig))  #get envelope
        teach_sig = smooth(teach_sig,
                           window_len=smoothing_len,
                           window='hanning')

        if what == 'teach':
            teach_and_plot(X,
                           Y,
                           teach_sig,
                           timev,
                           show_activations=show_activations)
        if what == 'test':
            test_and_plot(X,
                          Y,
                          teach_sig,
                          timev,
                          show_activations=show_activations)
    teach_sig = np.zeros([nT, nScales])
    for this_te in range(nScales):
        teach_sig[:, this_te] = np.loadtxt(directory +
                                           "teaching_signals_gesture_" +
                                           str(this_g) + "_teach_input_" +
                                           str(this_te) + "_trial_" + str(0) +
                                           ".txt")

    for this_trial in range(repeat_same):
        inputs = np.loadtxt(directory + "inputs_gesture_" + str(this_g) +
                            "_trial_" + str(this_trial) + ".txt")
        outputs = np.loadtxt(directory + "outputs_gesture_" + str(this_g) +
                             "_trial_" + str(this_trial) + ".txt")

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

        print "train offline reservoir on gesture..", str(
            this_g), " trial ", str(this_trial)

        if (this_trial == 0):
            # 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][None, :]),
                             axis=1)
            tmp_ac = tmp_ac / np.max(tmp_ac)
            ac = tmp_ac[:, None]
            teach_sig = teach_sig * ac**4  # Windowed by activity
            print "teach_sign", np.shape(teach_sig)
rmse_tot_input = np.zeros([nScales, num_gestures, repeat_same])

#### TRAIN
for this_g in range(num_gestures):

    teach_sig = np.zeros([nT,nScales])
    for this_te in range(nScales):
        teach_sig[:,this_te] = np.loadtxt(directory+"teaching_signals_gesture_"+str(this_g)+"_teach_input_"+str(this_te)+"_trial_"+str(0)+".txt")
            
                        
    for this_trial in range(repeat_same):
        inputs = np.loadtxt(directory+"inputs_gesture_"+str(this_g)+"_trial_"+str(this_trial)+".txt")
        outputs = np.loadtxt(directory+"outputs_gesture_"+str(this_g)+"_trial_"+str(this_trial)+".txt")
        

        X = L.ts2sig(timev, membrane, outputs[:,0], outputs[:,1], n_neu = 256)
        Yt = L.ts2sig(timev, membrane, inputs[:,0], inputs[:,1], n_neu = 256)  
        
        print "train offline reservoir on gesture..", str(this_g), " trial ", str(this_trial) 
        
        if(this_trial == 0):
            # 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][None,:]), axis=1) 
            tmp_ac = tmp_ac / np.max(tmp_ac)
            ac = tmp_ac[:,None]
            teach_sig = teach_sig * ac**4 # Windowed by activity
            print "teach_sign", np.shape(teach_sig)
            
        res.train(X,Yt,teach_sig)   
        zh = res.predict(X, Yt, initNt=initNt)
if flag["reload"]:

    # Time vector for ALL analog signals
    T = np.max(timev)
    nT = np.round(2 * T / dt_spk2sig)

    t_analog = np.linspace(0, T, nT)  # milliseconds

    print "### PRE-LOADING TRAINING DATA"
    # Pre-load training data
    X_train = []
    W_train = []
    for i in xrange(n_teach):
        outputs = np.loadtxt(outputs_dat[index_teaching[i]])
        omegas = np.loadtxt(omegas_dat[index_teaching[i]])
        X = L.ts2sig(t_analog, membrane, \
                     outputs[:,0], outputs[:,1], n_neu = 256)
        X_train.append(X)
        W_train.append(omegas)

    W_train = np.array(W_train)

    print "### PRE-LOADING TEST DATA"
    # Pre-load training data
    X_test = []
    W_test = []
    for i in xrange(n_test):
        outputs = np.loadtxt(outputs_dat[index_testing[i]])
        omegas = np.loadtxt(omegas_dat[index_testing[i]])
        X = L.ts2sig(t_analog, membrane, \
                     outputs[:,0], outputs[:,1], n_neu = 256)
        index_dn = np.where(raw_data[:,1] == dnch)[0]
        index_up = np.where(raw_data[:,1] == upch)[0]
        raw_data_input = []
        raw_data_input.extend(raw_data[index_dn,:])
        raw_data_input.extend(raw_data[index_up,:])
        raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])
        index_up = np.where(raw_data_input[:,1] == upch)[0]
        index_dn = np.where(raw_data_input[:,1] == dnch)[0]
        raw_data_input[index_dn,1] = 1
        raw_data_input[index_up,1] = 0   
        raw_data = out[0].raw_data()
        index_to_del = np.where(raw_data[:,1] > 255)
        raw_data = np.delete(raw_data, index_to_del,axis=0)

        #teach on reconstructed signal       
        X = L.ts2sig(timev, membrane, raw_data_input[:,0]-np.min(raw_data_input[:,0]), raw_data_input[:,1], n_neu = n_neu_scale[this_readout])
        index_to_remove = np.where(raw_data[:,1] >= n_neu_scale[this_readout])[0]
        raw_data = np.delete(raw_data, index_to_remove,axis=0)
        Y = L.ts2sig(timev, membrane, raw_data[:,0]-np.min(raw_data[:,0]), raw_data[:,1], n_neu = n_neu_scale[this_readout])     
        #teaching signal is interpolated to match actual len of signals
        signal[:,1] = signal[:,1]/abs(signal[:,1]).max()
        signal_ad = [signal[:,0], signal[:,1]]
        ynew = np.linspace(np.min(signal_ad[0]),np.max(signal_ad[0]),nT)
        s = interpolate.interp1d(signal_ad[0], signal_ad[1],kind="linear")
        teach_sig = s(ynew)  
        tmp_ac = np.mean(func_avg(timev[:,None], raw_data[:,0][None,:]-np.min(raw_data[:,0][None,:])), axis=1) 
        tmp_ac = tmp_ac / np.max(tmp_ac)
        ac = tmp_ac[:,None]
        teach_sig = teach_sig * ac**2 # Windowed by activity
        print "teach_sign", np.shape(teach_sig)
 def encode_and_teach(duration_rec=6000,delta_mem=50):
 
     #chip.load_parameters('biases/biases_reservoir_synthetic_stimuli.biases')
     #then load bioamp biases on top of these
     rcnpop = pyNCS.Population('neurons', 'for fun') 
     rcnpop.populate_all(nsetup,'mn256r1','excitatory')
     res = L.Reservoir(rcnpop, cee=1.0, cii=0.45)
     res.program_config()    
     #c = 0.2
     #dim = np.round(np.sqrt(len(liquid.rcn.synapses['virtual_exc'].addr)*c))
         
     #reset previous learning
     res.reset()
     
     figs = figure()
     # Time vector for analog signals
     Fs    = 1000/1e3 # Sampling frequency (in kHz)
     T     = duration_rec
     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*delta_mem**2)))
     out = nsetup.stimulate({},duration=duration_rec)
     signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)
 
     #extract input and output
     raw_data = out[0].raw_data()
     dnch = 300
     upch = 305
     index_dn = np.where(raw_data[:,1] == dnch)[0]
     index_up = np.where(raw_data[:,1] == upch)[0]
     raw_data_input = []
     raw_data_input.extend(raw_data[index_dn,:])
     raw_data_input.extend(raw_data[index_up,:])
     raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])
     index_up = np.where(raw_data_input[:,1] == upch)[0]
     index_dn = np.where(raw_data_input[:,1] == dnch)[0]
     raw_data_input[index_dn,1] = 1
     raw_data_input[index_up,1] = 0   
     raw_data = out[0].raw_data()
     index_to_del = np.where(raw_data[:,1] > 255)
     raw_data = np.delete(raw_data, index_to_del,axis=0)
     
     #teach on reconstructed signal
     X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 256)
     Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)     
     #teaching signal is interpolated to match actual len of signals
     
     signal_ad = [signal[:,0], signal[:,1]]
     ynew = np.linspace(np.min(signal_ad[0]),np.max(signal_ad[0]),nT+1)
     s = interpolate.interp1d(signal_ad[0], signal_ad[1],kind="cubic")
     teach_sig = s(ynew)  
     
     res.train(X,Y,teach_sig[0:len(X),None])
     zh = res.predict(X, Y)
     
     figure()
     subplot(3,1,1)
     plot(timev[0::],teach_sig[1::],label='teach signal')
     legend(loc='best')
     subplot(3,1,2)
     plot(timev[0::],zh["input"], label='input')
     legend(loc='best')
     subplot(3,1,3)
     plot(timev[0::],zh["output"], label='output')
     legend(loc='best')
         
     figure()        
     index_non_zeros = []
     for i in range(256):
         if(np.sum(Y[:,i]) != 0):
             index_non_zeros.append(i)  
     size_f = np.floor(np.sqrt(len(index_non_zeros)))
     for i in range(int(size_f**2)):
         #subplot(size_f,size_f,i) 
         plot(Y[:,index_non_zeros[i]])  
        def go_plot_reservoir(duration_rec, delta_mem=50, delta_up = delta_mem/4.0):
            # Time vector for analog signals
            Fs    = 100/1e3 # Sampling frequency (in kHz)
            T     = duration_rec
            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*delta_mem**2)))
            membrane_up = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*delta_up**2)))
            
            #nsetup.mapper._program_detail_mapping(2**3+2**4)  
            time.sleep(2)
            out = nsetup.stimulate({},duration=duration_rec)
            #nsetup.mapper._program_detail_mapping(0) 
            raw_data = out[0].raw_data()
            index_to_del = np.where(raw_data[:,1] > 255)
            raw_data = np.delete(raw_data, index_to_del,axis=0)
            Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)
            figure()
            for i in range(255):
                subplot(16,16,i)
                plot(Y[:,i])
                axis('off')
             
            upch = 300
            dnch = 305
            deltaup = 0.1
            deltadn = 0.0725
            raw_data = out[0].raw_data()
            index_dn = np.where(raw_data[:,1] == dnch)[0]
            index_up = np.where(raw_data[:,1] == upch)[0]

            raw_data_input = []
            raw_data_input.extend(raw_data[index_dn,:])
            raw_data_input.extend(raw_data[index_up,:])
            raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])

            raw_data = np.delete(raw_data, index_dn,axis=0)
            raw_data = np.delete(raw_data, index_up,axis=0)
            index_sort = np.argsort(raw_data_input[:,0])   
            
            #reconstruct from up and down
            up_index = np.where(raw_data_input[:,1]==upch)
            dn_index = np.where(raw_data_input[:,1]==dnch)
            index_ord =  np.argsort(raw_data_input[:,0])   
            signal = np.zeros([len(raw_data_input)])
            for i in range(1,len(raw_data_input)):
                if(raw_data_input[index_ord[i],1] == upch):
                    signal[i] = signal[i-1] + deltaup
                if(raw_data_input[index_ord[i],1] == dnch):
                    signal[i] = signal[i-1] - deltadn
            figure()        
            plot(raw_data_input[index_ord,0],signal+45)
            signal_trace_good = [raw_data_input[index_ord,0],signal]
            
            raw_data_input[up_index,1] = 0
            raw_data_input[dn_index,1] = 1
            X = L.ts2sig(timev, membrane_up, raw_data_input[:,0], raw_data_input[:,1], n_neu = 2)
            figure()    
            for i in range(2):
                subplot(2,1,i)
                plot(X[:,i])
                axis('off')            

            #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)
            plot(Y[:,max_pos])
            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")
            legend(loc="best")    
      
            figure()
            #raster plot lsm   
            vlines(raw_data[:,0], raw_data[:,1] + .5, raw_data[:,1] + 1.5)
            ylim([0,256])
            xlim([0,duration_rec])
            xlabel('Time [ms]')
            ylabel('Neu Id')
Ejemplo n.º 19
0
def encode_and_teach(duration_rec=6000, delta_mem=50, sync_bioamp_channel=305):

    #reset previous learning
    res.reset()

    figs = figure()

    command1 = subprocess.Popen([
        'sh',
        '/home/federico/project/work/trunk/data/Insects/Insect Neurophys Data/do_stim.sh'
    ])
    out = nsetup.stimulate({}, duration=duration_rec, send_reset_event=False)
    command1 = subprocess.Popen(['killall', 'aplay'])
    signal = go_reconstruct_signal_from_out(out,
                                            figs,
                                            upch=300,
                                            dnch=305,
                                            delta_up=0.1,
                                            delta_dn=0.1,
                                            do_detrend=False)

    #use delta as syn
    raw_data = out[0].raw_data()
    sync_index = raw_data[:, 1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[0], 0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:, 0] < time_start)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    #make it start from zero
    raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])
    duration = np.max(raw_data[:, 0])

    ############# MEMBRANE
    # Time vector for analog signals
    Fs = 1000 / 1e3  # Sampling frequency (in kHz)
    T = duration
    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 * delta_mem**2)))

    #extract input and output
    #raw_data = out[0].raw_data()
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:, 1] == dnch)[0]
    index_up = np.where(raw_data[:, 1] == upch)[0]
    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn, :])
    raw_data_input.extend(raw_data[index_up, :])
    raw_data_input = np.reshape(raw_data_input,
                                [len(index_dn) + len(index_up), 2])
    index_up = np.where(raw_data_input[:, 1] == upch)[0]
    index_dn = np.where(raw_data_input[:, 1] == dnch)[0]
    raw_data_input[index_dn, 1] = 1
    raw_data_input[index_up, 1] = 0
    raw_data = out[0].raw_data()
    sync_index = raw_data[:, 1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[0], 0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:, 0] < time_start)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    #make it start from zero
    raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])

    index_to_del = np.where(raw_data[:, 1] > 255)
    raw_data = np.delete(raw_data, index_to_del, axis=0)

    #teach on reconstructed signal
    X = L.ts2sig(timev,
                 membrane,
                 raw_data_input[:, 0],
                 raw_data_input[:, 1],
                 n_neu=256)
    Y = L.ts2sig(timev, membrane, raw_data[:, 0], raw_data[:, 1], n_neu=256)
    #generate teaching signal orthogonal signal to input
    teach = L.orth_signal(X)

    res.train(Y, X, teach[0:len(X), None])  #teach_sig[0:len(X),None])
    zh = res.predict(Y, X)

    figure()
    subplot(3, 1, 1)
    plot(timev, teach, label='teach signal')
    legend(loc='best')
    subplot(3, 1, 2)
    plot(timev, zh["input"], label='input')
    plot(timev, teach, label='teach signal')
    legend(loc='best')
    subplot(3, 1, 3)
    plot(timev, zh["output"], label='output')
    plot(timev, teach, label='teach signal')
    legend(loc='best')

    figure()
    index_non_zeros = []
    for i in range(256):
        if (np.sum(Y[:, i]) != 0):
            index_non_zeros.append(i)
    size_f = np.floor(np.sqrt(len(index_non_zeros)))
    for i in range(int(size_f**2)):
        #subplot(size_f,size_f,i)
        plot(Y[:, index_non_zeros[i]])
Ejemplo n.º 20
0
def test_determinism(duration_rec, n_trial=3, delta_mem=60, delta_up=15, plot_svd = False, datadir = 'data_fede_format_20020508_pen_3_rec_2', trial=1, sync_bioamp_channel=305):


    data_command = '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'+datadir+'/do_stim.sh'
    duration_sync = 1600
    
    fig_h = figure()
    fig_hh = figure()
    #figs = figure()
    # Time vector for analog signals
    Fs    = 100/1e3 # Sampling frequency (in kHz)
    T     = duration_rec
    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*delta_mem**2)))
    membrane_up = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*delta_up**2)))

    #nsetup.mapper._program_detail_mapping(2**3+2**4)  
    #command1 = subprocess.Popen(['sh', data_command, str(trial)])
    #out = nsetup.stimulate({},duration=duration_rec, send_reset_event=True)
    #command1 = subprocess.Popen(['killall', 'aplay'])
    all_sign = []
    all_out = []
    for this_trial in range(n_trial):
        command1 = subprocess.Popen(['sh', data_command, str(trial)])
        #time.sleep(1.2)
        out = nsetup.stimulate({},duration=0.2, send_reset_event=True)
        out = nsetup.stimulate({},duration=0.2, send_reset_event=False)
        out = nsetup.stimulate({},duration=duration_rec+duration_sync,send_reset_event=False)
        all_out.append(out)
        signal = go_reconstruct_signal_from_out(out,fig_h,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False, do_plot=False)
        all_sign.append(signal)
        time.sleep(1.2)
        command1 = subprocess.Popen(['killall', 'aplay'])
        #time.sleep(1)
        #nsetup.mapper._program_detail_mapping(0)         
        #use delta as syn
        raw_data = out[0].raw_data()
        sync_index = raw_data[:,1] == sync_bioamp_channel
        sync_index = np.where(sync_index)[0]
        time_start = raw_data[sync_index[0],0]
        #delete what happen before the syn index
        index_to_del = np.where(raw_data[:,0] < time_start+duration_sync)
        raw_data = np.delete(raw_data, index_to_del,axis=0)
        #make it start from zero
        raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
        duration = np.max(raw_data[:,0])
        
        index_to_del = np.where(raw_data[:,1] > 255)
        raw_data = np.delete(raw_data, index_to_del,axis=0)
        Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)
        figure(fig_h.number)
        for i in range(255):
            subplot(16,16,i)
            plot(Y[:,i])
            axis('off')
      
        if (this_trial == 0):
            index_non_zeros = []
            for i in range(256):
                if(np.sum(Y[:,i]) != 0):
                    index_non_zeros.append(i)  
            size_f = np.floor(np.sqrt(len(index_non_zeros)))
        figure(fig_hh.number)
        for i in range(int(size_f**2)):
            subplot(size_f,size_f,i) 
            plot(Y[:,index_non_zeros[i]])  
            axis('off')

    return all_sign, all_out
Ejemplo n.º 21
0
def mini_encoding(duration_rec = 3000, delta_mem = 10):
    #from scipy.fftpack import fft
    #yyf = fft(Y[:,index_non_zeros[i]])

    import wave
    import sys
    spf = wave.open('/home/federico/project/work/trunk/data/Insects/Insect Neurophys Data/Hackenelektrodenableitung_mecopoda_elongata_chirper_2_trim.wav','r')
    #Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, 'Int16')
    fs = spf.getframerate()
    time_orig=np.linspace(0, len(signal)/fs, num=len(signal))
    
    
    from scipy import interpolate
    s = interpolate.interp1d(time_orig*1000, signal,kind="linear")
    time_n  = np.linspace(0,np.max(time_orig)*1000,10000)
    ynew = s(time_n)#interpolate.splev(xnew, tck, der=0)
    
    
    #figure()
    #plot(time_orig,signal)
    mul_f = np.ceil(duration_rec/(np.max(time_orig)*1000))
    time_orig = np.linspace(0,np.max(time_n)*mul_f, len(time_n)*mul_f)
    signal_f = []
    for i in range(int(mul_f)):
        signal_f.append(ynew)
    signal = np.reshape(signal_f,[len(ynew)*mul_f])

    figure()
    plot(time_orig,signal)


    #fig_h = figure()
    fig_hh = figure()

    # Time vector for analog signals
    Fs    = 100/1e3 # Sampling frequency (in kHz)
    T     = duration_rec
    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*delta_mem**2)))
 
    out = nsetup.stimulate({},duration=duration_rec)
    raw_data = out[0].raw_data()
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:,1] == dnch)[0]
    index_up = np.where(raw_data[:,1] == upch)[0]

    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn,:])
    raw_data_input.extend(raw_data[index_up,:])
    raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])
    index_up = np.where(raw_data_input[:,1] == upch)[0]
    index_dn = np.where(raw_data_input[:,1] == dnch)[0]
    raw_data_input[index_dn,1] = 1
    raw_data_input[index_up,1] = 0
    X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 4)       
    figure()
    for i in range(2):
        plot(X[:,i])
    
    raw_data = out[0].raw_data()
    index_to_del = np.where(raw_data[:,1] > 255)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)

    index_non_zeros = []
    for i in range(256):
        if(np.sum(Y[:,i]) != 0):
            index_non_zeros.append(i)  
    size_f = np.floor(np.sqrt(len(index_non_zeros)))
    figure(fig_hh.number)
    for i in range(int(size_f**2)):
        #subplot(size_f,size_f,i) 
        plot(Y[:,index_non_zeros[i]])  
        #axis('off')  
    figure()
    for i in range(int(size_f**2)):
        subplot(size_f,size_f,i) 
        plot(Y[:,index_non_zeros[i]])  
        ylim([0,3])
        axis('off')      
        
    figure()
    #raster plot lsm   
    plot(raw_data[:,0], raw_data[:,1], '*', markersize=2)
    ylim([0,256])
    xlim([0,duration_rec])
    xlabel('Time [ms]')
    ylabel('Neu Id')
Ejemplo n.º 22
0
def encode_and_teach(duration_rec=6000,delta_mem=50, sync_bioamp_channel=305):
        
    #reset previous learning
    res.reset()

    figs = figure()

    command1 = subprocess.Popen(['sh', '/home/federico/project/work/trunk/data/Insects/Insect Neurophys Data/do_stim.sh'])
    out = nsetup.stimulate({},duration=duration_rec, send_reset_event=False)
    command1 = subprocess.Popen(['killall', 'aplay'])
    signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)

    #use delta as syn
    raw_data = out[0].raw_data()
    sync_index = raw_data[:,1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[0],0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:,0] < time_start)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    #make it start from zero
    raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
    duration = np.max(raw_data[:,0])

    ############# MEMBRANE
    # Time vector for analog signals
    Fs    = 1000/1e3 # Sampling frequency (in kHz)
    T     = duration
    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*delta_mem**2)))
    
    #extract input and output
    #raw_data = out[0].raw_data()
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:,1] == dnch)[0]
    index_up = np.where(raw_data[:,1] == upch)[0]
    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn,:])
    raw_data_input.extend(raw_data[index_up,:])
    raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])
    index_up = np.where(raw_data_input[:,1] == upch)[0]
    index_dn = np.where(raw_data_input[:,1] == dnch)[0]
    raw_data_input[index_dn,1] = 1
    raw_data_input[index_up,1] = 0   
    raw_data = out[0].raw_data()
    sync_index = raw_data[:,1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[0],0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:,0] < time_start)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    #make it start from zero
    raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
    
    index_to_del = np.where(raw_data[:,1] > 255)
    raw_data = np.delete(raw_data, index_to_del,axis=0)

    #teach on reconstructed signal
    X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 256)
    Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)     
    #generate teaching signal orthogonal signal to input
    teach = L.orth_signal(X)
    
    res.train(Y,X,teach[0:len(X),None])#teach_sig[0:len(X),None])
    zh = res.predict(Y,X)

    figure()
    subplot(3,1,1)
    plot(timev,teach,label='teach signal')
    legend(loc='best')
    subplot(3,1,2)
    plot(timev,zh["input"], label='input')
    plot(timev,teach,label='teach signal')
    legend(loc='best')
    subplot(3,1,3)
    plot(timev,zh["output"], label='output')
    plot(timev,teach,label='teach signal')
    legend(loc='best')
        
    figure()        
    index_non_zeros = []
    for i in range(256):
        if(np.sum(Y[:,i]) != 0):
            index_non_zeros.append(i)  
    size_f = np.floor(np.sqrt(len(index_non_zeros)))
    for i in range(int(size_f**2)):
        #subplot(size_f,size_f,i) 
        plot(Y[:,index_non_zeros[i]])  
Ejemplo n.º 23
0
def encode_and_poke(duration_rec=15000,delta_mem=25, sync_bioamp_channel=305, smoothing_len=90, trial = 1, what = 'teach', datadir = 'data_fede_format_20020508_pen_3_rec_2',  show_activations = False, save_data = 1, save_data_dir = 'reservoir_data'):
        
    duration_sync = 4000
    #figs = figure()
    data_command = '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'+datadir+'/do_stim.sh'
    command1 = subprocess.Popen(['sh', data_command, str(trial)])
    #command1 = subprocess.Popen(['sh', '/home/federico/projects/work/trunk/data/Insects/Insect Neurophys Data/do_teach.sh'])
    out = nsetup.stimulate({},duration=duration_rec+duration_sync, send_reset_event=False)
    command1 = subprocess.Popen(['killall', 'aplay'])
    #signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)

    #use delta as syn
    raw_data = out[0].raw_data()
    sync_index = raw_data[:,1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2],0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:,0] < time_start+duration_sync)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    #make it start from zero
    raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
    duration = np.max(raw_data[:,0])

    ############# MEMBRANE
    # Time vector for analog signals
    Fs    = 1000/1e3 # Sampling frequency (in kHz)
    T     = duration
    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*delta_mem**2)))
    
    #extract input and output
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:,1] == dnch)[0]
    index_up = np.where(raw_data[:,1] == upch)[0]
    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn,:])
    raw_data_input.extend(raw_data[index_up,:])
    raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])
    index_up = np.where(raw_data_input[:,1] == upch)[0]
    index_dn = np.where(raw_data_input[:,1] == dnch)[0]
    raw_data_input[index_dn,1] = 1
    raw_data_input[index_up,1] = 0   
    raw_data = out[0].raw_data()
    sync_index = raw_data[:,1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2],0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:,0] < time_start)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    #make it start from zero
    raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
    
    index_to_del = np.where(raw_data[:,1] > 255)
    raw_data = np.delete(raw_data, index_to_del,axis=0)

    np.savetxt(save_data_dir+'/raw_data_input_'+str(datadir)+'_'+str(trial)+'.txt', raw_data_input)
    np.savetxt(save_data_dir+'/raw_data_'+str(datadir)+'_'+str(trial)+'.txt', raw_data)

    #teach on reconstructed signal
    X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 256)
    Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)     
    #generate teaching signal orthogonal signal to input
    teach = np.loadtxt('/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'+datadir+'/1_.txt')#
    #teach = np.fromfile(open(trainfile),np.int16)[24:]
    #teach = teach/np.max(teach)
    framepersec = len(teach)/15
    teach = teach[0:framepersec/(duration_rec/1000)]
    #interpolate to match lenght
    signal_ad = [np.linspace(0,duration_rec,len(teach)), teach]
    ynew = np.linspace(0,duration_rec,nT+1)
    s = interpolate.interp1d(signal_ad[0], signal_ad[1],kind="linear")

    teach_sig = s(ynew)     
    teach_sig = np.abs(sigtool.hilbert(teach_sig)) #get envelope
    teach_sig = smooth(teach_sig,window_len=smoothing_len,window='hanning')
 
    if what == 'teach':
        teach_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
    if what == 'test':
        test_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
if flag["reload"]:

    # Time vector for ALL analog signals
    T  = np.max(timev)
    nT = np.round(2 * T / dt_spk2sig)

    t_analog = np.linspace(0,T,nT) # milliseconds

    print "### PRE-LOADING TRAINING DATA"
    # Pre-load training data
    X_train = []
    W_train = []
    for i in xrange(n_teach):
        outputs = np.loadtxt(outputs_dat[index_teaching[i]])
        omegas  = np.loadtxt(omegas_dat[index_teaching[i]])
        X = L.ts2sig(t_analog, membrane, \
                     outputs[:,0], outputs[:,1], n_neu = 256)
        X_train.append(X)
        W_train.append(omegas)

    W_train = np.array (W_train)

    print "### PRE-LOADING TEST DATA"
    # Pre-load training data
    X_test = []
    W_test = []
    for i in xrange(n_test):
        outputs = np.loadtxt(outputs_dat[index_testing[i]])
        omegas  = np.loadtxt(omegas_dat[index_testing[i]])
        X = L.ts2sig(t_analog, membrane, \
                     outputs[:,0], outputs[:,1], n_neu = 256)
        raw_data_input.extend(raw_data[index_dn, :])
        raw_data_input.extend(raw_data[index_up, :])
        raw_data_input = np.reshape(raw_data_input,
                                    [len(index_dn) + len(index_up), 2])
        index_up = np.where(raw_data_input[:, 1] == upch)[0]
        index_dn = np.where(raw_data_input[:, 1] == dnch)[0]
        raw_data_input[index_dn, 1] = 1
        raw_data_input[index_up, 1] = 0
        raw_data = out[0].raw_data()
        index_to_del = np.where(raw_data[:, 1] > 255)
        raw_data = np.delete(raw_data, index_to_del, axis=0)

        #teach on reconstructed signal
        X = L.ts2sig(timev,
                     membrane,
                     raw_data_input[:, 0] - np.min(raw_data_input[:, 0]),
                     raw_data_input[:, 1],
                     n_neu=n_neu_scale[this_readout])
        index_to_remove = np.where(
            raw_data[:, 1] >= n_neu_scale[this_readout])[0]
        raw_data = np.delete(raw_data, index_to_remove, axis=0)
        Y = L.ts2sig(timev,
                     membrane,
                     raw_data[:, 0] - np.min(raw_data[:, 0]),
                     raw_data[:, 1],
                     n_neu=n_neu_scale[this_readout])
        #teaching signal is interpolated to match actual len of signals
        signal[:, 1] = signal[:, 1] / abs(signal[:, 1]).max()
        signal_ad = [signal[:, 0], signal[:, 1]]
        ynew = np.linspace(np.min(signal_ad[0]), np.max(signal_ad[0]), nT)
        s = interpolate.interp1d(signal_ad[0], signal_ad[1], kind="linear")