Exemplo n.º 1
0
def FP_L2_vs_Sup(N_spikes = 1000, N_trains=20):
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes

    D = DataHarvester('FPvsWFP_4x%d_N=%d'%(N_trains,N_spikes))
    for regime_name, T_thresh in zip(['subT','superT', 'crit', 'superSin'],
                                     4*[64.]):
        regime_label = base_name + regime_name
            
        for sample_id in xrange(1,N_trains +1):
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, Tsim=-1.0)
            
            #### N_thresh = 10
            binnedTrain.pruneBins(None, N_thresh = 10, T_thresh=T_thresh)
            D.addSample(sample_id, binnedTrain.getTf(), binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
                        
            abg_init = initialize_right_2std(binnedTrain)
            abg_init[1] = amax([.1, abg_init[1]])
            abg_init[2] = amax([.0, abg_init[2]])
            D.addEstimate(sample_id, 'init_N10', abg_init,.0) 


            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
                       
            dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0)            
            theta = binnedTrain.theta            
            binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)

            phis = binnedTrain.bins.keys();

            S = FPMultiPhiSolver(theta, phis,
                                 dx, dt,
                                 binnedTrain.getTf(), X_min = -1.0)            
            
            start = time.clock()
            abg_est = FPL2Estimator(S,binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FP_L2', abg_est, finish-start)
            
            start = time.clock()
            abg_est = FPSupEstimator(S,binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FP_Sup', abg_est, finish-start)
                    
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 2
0
def ThetaEstimate(N_spikes = 1000, N_trains=100, N_phi=20, 
                  thetas = [1, 5, 10, 20]):
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()
    base_name = 'sinusoidal_spike_train_N=%d_critical_theta='%N_spikes

    T_thresh = 64.
    
    D = DataHarvester('ThetaEstimate_%dx%d_N=%d'%(len(thetas),N_trains,N_spikes))
    for sample_id in xrange(1,N_trains +1):
        for theta in thetas:    
            regime_name = 'theta%d'%theta
            regime_label = base_name + '%d'%theta            
            file_name = regime_label + '_%d'%sample_id
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, Tsim=-1.0)
            
            binnedTrain.pruneBins(None, N_thresh = 5, T_thresh=T_thresh)
            D.addSample(sample_id, binnedTrain.getTf(), binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
                        
            abg_init = initialize_right_2std(binnedTrain)
            abg_init[1] = amax([.1, abg_init[1]])
            abg_init[2] = amax([.0, abg_init[2]])
            D.addEstimate(sample_id, 'Initializer', abg_init,.0, warnflag = 0) 
                       
            #RELOAD ALL DATA:               
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)
            
            #Weighted Fortet:            
            start = time.clock()
            abg_est, warnflag = FortetEstimatorSup(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'Fortet', abg_est, finish-start, warnflag)
            
            #Weighted F-P:
            dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0) 
            phis = binnedTrain.bins.keys();
            S = FPMultiPhiSolver(binnedTrain.theta, phis,
                                 dx, dt,
                                 binnedTrain.getTf(), X_min = -1.0)            
            
            start = time.clock()
            abg_est, warnflag = FPSupEstimator(S, binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FP', abg_est, finish-start, warnflag)
                    
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 3
0
def GradedDriver():
    from scipy.optimize import fmin_bfgs
    
    N_phi = 10;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    print 'GradedEstimator'
    
    for file_name in ['sinusoidal_spike_train_T=20000_subT_3.path',
                      'sinusoidal_spike_train_T=20000_subT_8.path',
                      'sinusoidal_spike_train_T=20000_subT_13.path']:
    
        print file_name
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        binnedTrain.pruneBins(None, N_thresh = 32, T_thresh = 32.)
        abg_est = abs( initialize5(binnedTrain))
        
        print 'abg_init = ',abg_est
        
        theta = binnedTrain.theta
        
        for T_thresh, N_thresh, max_iters in zip([32/8., 32/4., 32/2., 32.],
                                          [128, 128, 64, 32],
                                          [50,50,100,None]):
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            binnedTrain.pruneBins(None, N_thresh, T_thresh)
            print 'N_bins = ', len(binnedTrain.bins.keys())
        
            Tf = binnedTrain.getTf()
            print 'Tf = ', Tf 
            dx = .02; dt = FPMultiPhiSolver.calculate_dt(dx, 4., 10.)
        
            phis = binnedTrain.bins.keys();
            
            S = FPMultiPhiSolver(theta, phis, dx, dt,
                                 Tf, X_MIN = -.5)
            
            from scipy.optimize import fmin
            def func(abg):
                'Solve it:'
                Fs = S.solve(abg, visualize=False)[:,:,-1]
                Ss = S.transformSurvivorData(binnedTrain)
                Ls = Fs - Ss
        
                'Return'
                G = .5*sum(Ls*Ls)*S._dt 
                return G
    
            abg_est = fmin(func, abg_est, ftol = 1e-2*T_thresh, maxiter=max_iters)
            
            print 'current_estimate = ', abg_est
        
        print 'final estimate = ', abg_est
Exemplo n.º 4
0
def CustomEstimate(spike_trains):   
    D = DataPrinter('')
    for spike_train in spike_trains:
        regime_name = spike_train[0];
        sample_id = spike_train[1]
        N_spikes = spike_train[2];
        N_phi = spike_train[3];
    
        phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

        base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes

        T_thresh = 128.0;
        regime_label = base_name + regime_name
            
        file_name = regime_label + '_' + str(sample_id)
        print file_name
        
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        ps = binnedTrain._Train._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))
        
        D.setRegime(regime_name,abg_true, Tsim=-1.0)
        
        binnedTrain.pruneBins(None, N_thresh = 5, T_thresh=T_thresh)
        D.addSample(sample_id, binnedTrain.getTf(), binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
                    
        abg_init = initialize_right_2std(binnedTrain)
        abg_init[1] = amax([.1, abg_init[1]])
        abg_init[2] = amax([.0, abg_init[2]])
        D.addEstimate(sample_id, 'Initializer', abg_init,.0, warnflag = 0) 
                   
        #RELOAD ALL DATA:               
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)
        
        #Weighted Fortet:            
        start = time.clock()
        abg_est, warnflag = FortetEstimatorSup(binnedTrain, abg_init, verbose = True)
        finish = time.clock()
        D.addEstimate(sample_id, 'Fortet', abg_est, finish-start, warnflag)
        
        #Weighted F-P:
        dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0) 
        phis = binnedTrain.bins.keys();
        S = FPMultiPhiSolver(binnedTrain.theta, phis,
                             dx, dt,
                             binnedTrain.getTf(), X_min = -1.0)            
        
        start = time.clock()
        abg_est, warnflag = FPSupEstimator(S, binnedTrain, abg_init, verbose = True)
        finish = time.clock()
        D.addEstimate(sample_id, 'FP', abg_est, finish-start, warnflag)
                    
    del D  
Exemplo n.º 5
0
def TestEstimate(N_spikes = 100, N_trains=5, N_phi=8):
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes
    
    D = DataPrinter('')

    for regime_name, T_thresh in zip(['superT'],
                                     [64]):
        regime_label = base_name + regime_name
            
        for sample_id in xrange(1,N_trains +1):
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, Tsim=-1.0)
            
            binnedTrain.pruneBins(None, N_thresh = 5, T_thresh=T_thresh)
            D.addSample(sample_id, binnedTrain.getTf(), binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
                        
            abg_init = initialize_right_2std(binnedTrain)
            abg_init[1] = amax([.1, abg_init[1]])
            abg_init[2] = amax([.0, abg_init[2]])
            D.addEstimate(sample_id, 'Initializer', abg_init,.0, warnflag = 0) 
                       
            #RELOAD ALL DATA:               
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)
            
            #Weighted Fortet:            
            start = time.clock()
            abg_est, warnflag = FortetEstimatorSup(binnedTrain, abg_init, verbose = True)
            finish = time.clock()
            D.addEstimate(sample_id, 'Fortet', abg_est, finish-start, warnflag)
            
            #Weighted F-P:
            dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0) 
            phis = binnedTrain.bins.keys();
            S = FPMultiPhiSolver(binnedTrain.theta, phis,
                                 dx, dt,
                                 binnedTrain.getTf(), X_min = -1.0)            
            
            start = time.clock()
            abg_est, warnflag = FPSupEstimator(S, binnedTrain, abg_init, verbose = True)
            finish = time.clock()
            D.addEstimate(sample_id, 'FP', abg_est, finish-start, warnflag)
                    
    del D  
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 6
0
def ThetaBox(thetas, sample_id = 1):   
    D = DataPrinter('')
    for theta in thetas:
        N_phi = 20;
        phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

        file_name = 'sinusoidal_spike_train_N=1000_critical_theta=%d_%d'%(theta, sample_id)
        print file_name
                    
        T_thresh = 128.0;
        
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        ps = binnedTrain._Train._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))
        print 'ps = ', ps.getParams()
        
        regime_name = 'theta=%d'%int(ps._theta)
        print regime_name
        D.setRegime(regime_name,abg_true, Tsim=-1.0)
        
        binnedTrain.pruneBins(None, N_thresh = 5, T_thresh=T_thresh)
        D.addSample(sample_id, binnedTrain.getTf(), binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
                    
        abg_init = initialize_right_2std(binnedTrain, cap_beta_gamma=True)
        D.addEstimate(sample_id, 'Initializer', abg_init,.0, warnflag = 0) 
                   
        #RELOAD ALL DATA:               
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)
        
        #Weighted Fortet:            
        start = time.clock()
        abg_est, warnflag = FortetEstimatorSup(binnedTrain, abg_init, verbose = False)
        finish = time.clock()
        D.addEstimate(sample_id, 'Fortet', abg_est, finish-start, warnflag)
        
        #Weighted F-P:
        dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0) 
        phis = binnedTrain.bins.keys();
        S = FPMultiPhiSolver(binnedTrain.theta, phis,
                             dx, dt,
                             binnedTrain.getTf(), X_min = -1.0)            
        
        start = time.clock()
        abg_est, warnflag = FPSupEstimator(S, binnedTrain, abg_init, verbose = False)
        finish = time.clock()
        D.addEstimate(sample_id, 'FP', abg_est, finish-start, warnflag)
                    
    del D  
Exemplo n.º 7
0
def Fortet_SupVsL2(N_spikes = 1000, N_trains = 16):
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes

    D = DataHarvester('Fortet_SupVsL2_4x%d'%N_trains)
#    for regime_name, T_thresh in zip(['subT', 'crit', 'superSin', 'superT'],
#                                     [64., 64, 32., 32.]):
    for regime_name, T_thresh in zip(['crit', 'superSin', 'superT'],
                                     [64, 32., 32.]):
        regime_label = base_name + regime_name
        
            
        for sample_id in xrange(1,N_trains+1):
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, Tsim=-1.0)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 10, T_thresh=T_thresh)
            D.addSample(sample_id, binnedTrain.getTf(), binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
                
            abg_init = initialize_right_2std(binnedTrain)
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            
            start = time.clock()
            abg_est = FortetEstimatorL2(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FortetL2', abg_est, finish-start)
            print abg_est, ' | %.2f'%(finish-start)
            
            start = time.clock()
            abg_est = FortetEstimatorSup(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FortetSup', abg_est, finish-start)
            print abg_est, ' | %.2f'%(finish-start)
        
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 8
0
def BFGSGradedEstimator():
    from scipy.optimize import fmin_bfgs
    
    print 'GradedEstimator'
    
    N_phi = 10;
    print 'N_phi = ', N_phi
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    phi_omit = None

    for file_name in ['sinusoidal_spike_train_T=20000_subT_4.path',
                      'sinusoidal_spike_train_T=20000_subT_7.path',
                      'sinusoidal_spike_train_T=20000_subT_13.path']:
    
        print file_name
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        binnedTrain.pruneBins(phi_omit, N_thresh = 32, T_thresh = 32.)
        abg_est = abs( initialize5(binnedTrain))
        
        print 'abg_init = ',abg_est
        
        for T_thresh, N_thresh, max_iters in zip([32/8., 32/4., 32/2., 32.],
                                                 [128, 128, 64, 32],
                                                 [32,24,16,8]):
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
            binnedTrain.pruneBins(phi_omit, N_thresh, T_thresh)
            Tf = binnedTrain.getTf()
            print 'Tf = ', Tf
            print 'N_bins = ', len(binnedTrain.bins.keys())
           
            solver_phis = binnedTrain.bins.keys();
            theta = binnedTrain.theta
            x_min = -.5;
            
            S = FPMultiPhiSolver(theta, solver_phis,
                             .1, .1,
                             Tf, X_MIN = x_min)
            
            lE = Estimator(S, binnedTrain, verbose = True)
            
        #    abg_est = fmin_bfgs(lE.func, abg_init, lE.dfunc,  gtol = 1e-6*binnedTrain.getTf(), maxiter= 128, full_output = 0)
            abg_est, fopt, gopt, Bopt, func_calls, grad_calls, warnflag  = fmin_bfgs(lE.func, abg_est,
                                                                                     lE.dfunc,  gtol = 1e-08*binnedTrain.getTf(), maxiter=max_iters, full_output = 1)
            
            print 'estimate gradient =', gopt
            print 'current_estimate = ', abg_est
        
        print 'final estimate = ', abg_est
Exemplo n.º 9
0
def InitTauSamples():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    base_name = 'sinusoidal_spike_train_N=1000_'

    for regime_name, T_thresh in zip(['superT', 'subT', 'crit', 'superSin'],
                                                       [4., 32, 16., 16.]):
       
        regime_label = base_name + regime_name
        sample_id = randint(1,17)
        file_name = regime_label + '_' + str(sample_id)
        print file_name
        
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        ps = binnedTrain._Train._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))
        print abg_true
            
        phi_omit = None
        binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
        Tf = binnedTrain.getTf()
                    
        abgt_init = initialize2_tau(binnedTrain)
        print '%.2g,%.2g,%.2g,%.2g' %(abgt_init[0],
                                      abgt_init[1],abgt_init[2],abgt_init[3])
        
        abgt_init = initialize5_tau(binnedTrain)
        print '%.2g,%.2g,%.2g,%.2g' %(abgt_init[0],
                                      abgt_init[1],abgt_init[2],abgt_init[3])
        
        print " "
Exemplo n.º 10
0
def GradedNMEstimator(file_name, phi_norms, abg_est, T_max, N_thresh_final):
        for T_thresh, N_thresh, max_iters in zip(array([1./8., 1./4., 1./2., 1.])*T_max,
                                                 array([2,2,2,1])*N_thresh_final,
                                                 [50,50,100,None]):
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            binnedTrain.pruneBins(None, N_thresh, T_thresh)
            print 'N_bins = ', len(binnedTrain.bins.keys())
        
            Tf = binnedTrain.getTf()
            print 'Tf = ', Tf 
            dx = .02; dt = FPMultiPhiSolver.calculate_dt(dx, 5., 10.)
        
            phis = binnedTrain.bins.keys();
            theta = binnedTrain.theta
            
            S = FPMultiPhiSolver(theta, phis, dx, dt,
                                 Tf, X_MIN = -.5)
            from scipy.optimize import fmin
            def func(abg):
                'Solve it:'
                Fs = S.solve(abg, visualize=False)[:,:,-1]
                Ss = S.transformSurvivorData(binnedTrain)
                Ls = Fs - Ss
        
                'Return'
                G = .5*sum(Ls*Ls)*S._dt 
                return G
    
            abg_est = fmin(func, abg_est, ftol = 1e-2*T_thresh, maxiter=max_iters)
            
            print 'current_estimate = ', abg_est
        
        return abg_est
Exemplo n.º 11
0
def calculateInitSDFs():
    N_phi = 16;
    print 'N_phi = ', N_phi
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    
    for regime_idx, regime_name in enumerate(['superT', 'superSin', 'crit','subT']):
    #        for regime_name in ['superT']:
        filename = RESULTS_DIR + '/Fs_%s.npz'%regime_name 
        npzfile = load(filename)
        ts = npzfile['ts']
        phis = npzfile['phis']
        
        regime_label = 'sinusoidal_spike_train_N=1000_' + regime_name + '_12' 
        binnedTrain = BinnedSpikeTrain.initFromFile(regime_label, phi_norms)
        binnedTrain.pruneBins(None, N_thresh = 16, T_thresh=128.)
           
        ps = binnedTrain._Train._params
        abg = initialize_right_2std(binnedTrain)
#        abg = array((ps._alpha, ps._beta, ps._gamma))
        
        GsInit = calcG_U_Const(abg,ts,phis);
        
        filename= RESULTS_DIR + '/Gs_%s'%regime_name
        print 'saving Gs to ', filename
        savez(filename, GsInit=GsInit);
Exemplo n.º 12
0
def writeWithHarvester():
    from BinnedSpikeTrain import BinnedSpikeTrain
    from InitBox import initialize5
    from Simulator import Path, OUSinusoidalParams
    import time

    N_phi = 20;
    print 'N_phi = ', N_phi
    phis =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    
#    D = DataHarvester('test2')
    D = DataHarvester('test2', 'test3')

    base_name = 'sinusoidal_spike_train_T='
    for regime_name, T_sim, T_thresh in zip(['superT', 'subT', 'crit', 'superSin'],
                                                       [5000 , 20000, 5000, 5000],
                                                       [4., 32, 16., 16.]): 
        regime_label = base_name + str(T_sim)+ '_' + regime_name
            
        for sample_id in xrange(3,4):
            file_name = regime_label + '_' + str(sample_id) + '.path'
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
            ps = binnedTrain._Path._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            
            D.setRegime(regime_name,abg_true, T_sim)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
            Tf = binnedTrain.getTf()
            D.addSample(sample_id, Tf, binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
            
            start = time.clock()
            abg_init = initialize5(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Initializer', abg_init, finish-start) 
                    
            abg_est = abg_init
            start = time.clock()
    #        abg_est = NMEstimator(S, binnedTrain, abg_init)
            time.sleep(rand())
            finish = time.clock()
            D.addEstimate(sample_id, 'Nelder-Mead', abg_est, finish-start) 
            
            start = time.clock()
    #        abg_est = BFGSEstimator(S, binnedTrain, abg_init)
            time.sleep(rand())
            finish = time.clock()
            D.addEstimate(sample_id, 'BFGS', abg_est, finish-start) 
            
            start = time.clock()
    #        abg_est = FortetEstimator(binnedTrain, abg_init)
            time.sleep(rand())
            finish = time.clock()
            D.addEstimate(sample_id, 'Fortet', abg_est, finish-start) 
Exemplo n.º 13
0
def CvsPyEstimate():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_N=1000_'

    D = DataHarvester('CvsPY_2x4')
    for regime_name, T_thresh in zip(['subT', 'superSin'],
                                                       [32, 16.]):
        regime_label = base_name + regime_name
            
        for sample_id in xrange(1,4):
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, Tsim=-1.0)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
            Tf = binnedTrain.getTf()
            D.addSample(sample_id, Tf, binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
             
            start = time.clock()
            abg_init = initialize_right_2std(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Initializer', abg_init, finish-start) 
             
            dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0)
            
            phis = binnedTrain.bins.keys();
            theta = binnedTrain.theta
            
            S = FPMultiPhiSolver(theta, phis,
                                 dx, dt,
                                 Tf, X_min = -1.0)

            start = time.clock()
            abg_est = cNMEstimator(S, binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FP-C', abg_est, finish-start)
               
            start = time.clock()
            abg_est = NMEstimator(S, binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'FP-PY', abg_est, finish-start) 
            
        
    D.closeFile()
Exemplo n.º 14
0
def FortetVsWeightedFortet():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_N=1000_'

    D = DataHarvester('FvsWF_4x16')
    for regime_name, T_thresh in zip(['superT', 'subT', 'crit', 'superSin'],
                                                       [6., 64, 32., 32.]):
        regime_label = base_name + regime_name
            
        for sample_id in xrange(1,17):
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, Tsim=-1.0)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 10, T_thresh=T_thresh)
            Tf = binnedTrain.getTf()
            D.addSample(sample_id, Tf, binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
             
            start = time.clock()
            abg_init = initialize_right_2std(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Initializer', abg_init, finish-start) 
            
            start = time.clock()
            abg_est = FortetEstimator(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'Fortet10', abg_est, finish-start)
            
            start = time.clock()
            abg_est = WeightedFortetEstimator(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'WeghtedFortet', abg_est, finish-start)
            
            binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
            start = time.clock()
            abg_est = FortetEstimator(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'Fortet64', abg_est, finish-start)
            
        
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 15
0
def NelderMeadSubTEstimator():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_T='

    D = DataHarvester('SubT_NMx16_refined_sim_dt')
    for regime_name, T_sim, T_thresh in zip(['subT'],
                                           [20000],
                                           [32.]):

        regime_label = base_name + str(T_sim)+ '_' + regime_name
            
        for sample_id in xrange(1,17):
            file_name = regime_label + '_' + str(sample_id) + '.path'
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, T_sim)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
            Tf = binnedTrain.getTf()
            D.addSample(sample_id, Tf, binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
             
            dx = .04; dt = FPMultiPhiSolver.calculate_dt(dx, 4., 2.)
        
            phis = binnedTrain.bins.keys();
            theta = binnedTrain.theta
            
            S = FPMultiPhiSolver(theta, phis,
                                 dx, dt,
                                 Tf, X_MIN = -.5)
        
            start = time.clock()
            abg_init = initialize5(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Initializer', abg_init, finish-start) 

            abg_init = abs(abg_init)            
            start = time.clock()
            abg_est = NMEstimator(S, binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'Nelder-Mead', abg_est, finish-start) 
        
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 16
0
def MixedDriver():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phis =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    
#    file_name = 'sinusoidal_spike_train_N=1000_superT_13'
    file_name = 'sinusoidal_spike_train_N=1000_subT_1'
#    file_name = 'sinusoidal_spike_train_N=1000_crit_5'

#    intervalStats(file_name)
    
    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)

    phi_omit = None
#    phi_omit = r_[(linspace(.15, .45, 4),
#                   linspace(.55,.95, 5) )]  *2*pi/ binnedTrain.theta
    binnedTrain.pruneBins(phi_omit, N_thresh = 80, T_thresh= 16.)
    print 'N_bins = ', len(binnedTrain.bins.keys())
    
    Tf = binnedTrain.getTf() #/ 2.
    print 'Tf = ', Tf

    params = binnedTrain._Train._params
    abg_true = (params._alpha, params._beta, params._gamma)
    print 'true = ',     abg_true

    abg_init = initialize_right_2std(binnedTrain)
    print 'init = ',     abg_init

    
    start = time.clock()
    abg_est = MixedEstimator(abg_init, binnedTrain)
    finish = time.clock()
    print 'Mixed est = ', abg_est
    mixed_time = finish-start
    print 'Mixed time = ', mixed_time
    
    from AdjointEstimator import FortetEstimator 
    start = time.clock()
    abg_est = FortetEstimator(binnedTrain, abg_est)
    finish = time.clock()
    print 'Mixed+Fortet est = ', abg_est
    print 'MIxed+Fortet time = ', finish-start + mixed_time

    #Compare with straight up Fortet:
    start = time.clock()
    abg_est = FortetEstimator(binnedTrain, abg_init)
    finish = time.clock()
    print 'Fortet est = ', abg_est
    print 'Fortet time = ', finish-start
Exemplo n.º 17
0
def estimateTau(regime = 'crit', number=11,
                     N_thresh = 64, T_thresh = 16. ):
    file_name = 'sinusoidal_spike_train_N=1000_' + regime + '_' + str(number)
    print file_name
    
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
    binnedTrain.pruneBins(None, N_thresh, T_thresh)
    print 'N_bins = ', len(binnedTrain.bins.keys())
    
    Tf = binnedTrain.getTf()
    print 'Tf = ', Tf 
    abg_init = initialize_right_2std(binnedTrain)

    
    phis = binnedTrain.bins.keys();
    theta = binnedTrain.theta

    dx = .02; dt = FPMultiPhiSolver.calculate_dt(dx, abg_init,x_min= -1.0)
                
    S = FPMultiPhiSolver(theta, phis, dx, dt,
                       Tf, X_min = -2.)
        
    abgt_init = [abg_init[0],
                 abg_init[1],
                 abg_init[2],                     
                 .5]
    
    print 'abgt_init = ', abgt_init
    
    start = time.clock()
    abgt_est = TaucharEstimator(S, binnedTrain, abgt_init)
    finish = time.clock()
    
    print 'abgt_est = ', abgt_est
    print 'compute time = ', finish-start
    
    print 'No tau comparison:'
    start = time.clock()
    abg_est = NMEstimator(S, binnedTrain, abg_init)
    finish = time.clock()
    
    print 'abg_est = ', abg_est
    print 'compute time = ', finish-start
    
    return abgt_est
Exemplo n.º 18
0
def estimateSubT(N_spikes=100, sample_id =4,  T_thresh = 32.):
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes
    regime_name = 'subT'
    regime_label = base_name + regime_name
    file_name = regime_label + '_' + str(sample_id)
    print file_name
            
    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
    ps = binnedTrain._Train._params
    print array((ps._alpha, ps._beta, ps._gamma))
    
    #### N_thresh = 10
    binnedTrain.pruneBins(None, N_thresh = 10, T_thresh=T_thresh)
                
    abg_init = initialize_right_2std(binnedTrain)
    print abg_init
    abg_init[1] = amax([.1, abg_init[1]])
    abg_init[2] = amax([.0, abg_init[2]])
            
    dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_init, -1.0)            
    print dx, dt
    theta = binnedTrain.theta            
      
    ##### N_thresh = 1
    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
    binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)
    phis = binnedTrain.bins.keys();
    S = FPMultiPhiSolver(theta, phis,
                         dx, dt,
                         binnedTrain.getTf(), X_min = -1.0)            
    abg_est = WeightedFPEstimator(S,binnedTrain, abg_init)
    print abg_est
Exemplo n.º 19
0
def BatchGradedNMEstimator():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_T='

    D = DataHarvester('GradedNMx16', 'GradedNM_SubTx16')
    N_thresh = 32
    for regime_name, T_sim, T_thresh in zip(['subT'],
                                                       [20000],
                                                       [32.]):
        regime_label = base_name + str(T_sim)+ '_' + regime_name
            
        for sample_id in xrange(4,17):
            file_name = regime_label + '_' + str(sample_id) + '.path'
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Path._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, T_sim)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = N_thresh, T_thresh=T_thresh)
            Tf = binnedTrain.getTf()
            D.addSample(sample_id, Tf, binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
        
            start = time.clock()
            abg_init = initialize5(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Initializer', abg_init, finish-start) 
                    
            start = time.clock()
            abg_est = GradedNMEstimator(file_name, phi_norms, abg_init, T_thresh, N_thresh)
            finish = time.clock()
            D.addEstimate(sample_id, 'Graded_Nelder-Mead', abg_est, finish-start) 
            
            start = time.clock()
            abg_est = FortetEstimator(binnedTrain, abg_init)
            finish = time.clock()
            D.addEstimate(sample_id, 'Fortet', abg_est, finish-start)
        
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 3600.0, ' hrs'
Exemplo n.º 20
0
def MultiTrainEstimator():
    phis =  linspace(.05, .95, 10)
    for file_name in ['sinusoidal_spike_train_T=1000.path',
                      'sinusoidal_spike_train_T=6000.path',
                      'sinusoidal_spike_train_T=10000_superSin.path',
                      'sinusoidal_spike_train_T=10000_crit.path']:

        print '#'*64
        
        print file_name
    
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
        
        phi_omit = None
        binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh = 10.)
        print 'N_bins = ', len(binnedTrain.bins.keys())
        
        Tf = binnedTrain.getTf()
        print 'Tf = ', Tf
            
        dx = .05; dt = FPMultiPhiSolver.calculate_dt(dx, 5., 2.)
    
        binphis = binnedTrain.bins.keys();
        theta = binnedTrain.theta
        
        S = FPMultiPhiSolver(theta, binphis,
                             dx, dt,
                             Tf)
    
        ps = binnedTrain._Train._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))
        print 'abg_true = ', abg_true
    
        abg_init = abs(abg_true + abg_true * randn(3)) ;
        print 'abg_init = ', abg_init
            
        start = time.clock()
        abg_est = TNCEstimator(S, binnedTrain, abg_init)
        print 'Est. time = ', time.clock() - start
        print 'abg_est = ', abg_est
        print 'error = ', abg_true - abg_est
        
        start = time.clock()
        abg_est = NMEstimator(S, binnedTrain, abg_init)
        print 'Est. time = ', time.clock() - start
        print 'abg_est = ', abg_est
        print 'error = ', abg_true - abg_est
Exemplo n.º 21
0
def CLossFHarness():
    
    errors = []
    
    for N_spikes, N_phi in zip( [100,1000],
                                [8,20]):
        phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
        base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes


    for regime  in ['superT', 'crit', 'subT','superSin']:
        regime_label = base_name + regime

#        for sample_id in [1,23, 36, 77, 99]:
        for sample_id in [43]:
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        
            bins = binnedTrain.bins;
            phis = bins.keys()
            N_phi = len(phis)
                        
            def closs_function(abg):
                error = .0;
                for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
                    Is = bins[phi_m]['Is']
                    N_Is  = len(Is);
                    Tf = amax(Is);
                    ts = linspace(1e-8, Tf+1e-2, 500)
                    #Call C to compute the lhs - rhs
                    abgthphi = r_[abg, theta, phi_m]
                    difference = ext_fpc.FortetError(abgthphi, ts, Is)
                    raw_error  = amax(difference);
                    weighted_error = N_Is * raw_error;                    
                    error += weighted_error                    
                return error
            
            alpha,  beta, gamma, theta = binnedTrain._Train._params.getParams() 
            start = time.clock()
            c_error = closs_function([alpha, beta, gamma]) 
            print 'ctime = ', time.clock() - start
Exemplo n.º 22
0
def VisualizeSinusoidallyDominating():
        N_phi = 20;
        print 'N_phi = ', N_phi
        
        phis =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
        phi_omit = None
        
        file_name = 'sinusoidal_spike_train_T=5000_superSin_11.path'
        
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
        binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=16.)
        print 'N_bins = ', len(binnedTrain.bins.keys())
    
        Tf = binnedTrain.getTf()
        print 'Tf = ', Tf
       
        solver_phis = binnedTrain.bins.keys();
        theta = binnedTrain.theta;
    
        ps = binnedTrain._Path._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))

        abg = abg_true
        visualizeData_vs_Fortet(abg, binnedTrain, theta,
                                title_tag =  'TRUE: ',
                                save_fig_name='SuperSin_true_params')
        get_current_fig_manager().window.showMaximized()
       
        abg_est =  [.494,.140,1.11]
        abg = abg_est
        visualizeData_vs_Fortet(abg, binnedTrain, theta,
                                 title_tag =  'F-P: ',
                            save_fig_name='SuperSin_NM_est')
        get_current_fig_manager().window.showMaximized()
       
       
        abg_est =  [.541,.181, .983]
        abg = abg_est
        visualizeData_vs_Fortet(abg, binnedTrain,theta,
                                title_tag = 'Fortet: ',
                                save_fig_name='SuperSin_Fortet_est')
        get_current_fig_manager().window.showMaximized()
Exemplo n.º 23
0
def BatchInit5_vs_InitRightstd():
    N_phi = 20;
    print 'N_phi = ', N_phi
    
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)

    batch_start = time.clock()    
    base_name = 'sinusoidal_spike_train_N=1000_'

    D = DataHarvester('InitComparison4x100_N100')
    for regime_name, T_thresh in zip(['superT', 'subT', 'crit', 'superSin'],
                                                       [4., 32, 16., 16.]):
        regime_label = base_name + regime_name
            
        for sample_id in xrange(1,17):
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
            ps = binnedTrain._Train._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            D.setRegime(regime_name,abg_true, -.1)
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
            Tf = binnedTrain.getTf()
            D.addSample(sample_id, Tf, binnedTrain.getBinCount(), binnedTrain.getSpikeCount())
        
            start = time.clock()
            abg_init = initialize5(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Init5pts', abg_init, finish-start) 
            
            start = time.clock()
            abg_init = initialize_right_2std(binnedTrain)
            finish = time.clock()
            D.addEstimate(sample_id, 'Init_right_2std', abg_init, finish-start) 
                                
    D.closeFile() 
   
    print 'batch time = ', (time.clock() - batch_start) / 60.0, ' mins'
Exemplo n.º 24
0
def calculateExactSDFs():
    N_phi = 4;
    print 'N_phi = ', N_phi
    
    phis =  2*pi*array([0,.25, .5, .75])
       
    
    for regime_idx, regime_name in enumerate(['superT', 'superSin', 'crit','subT']):
    #        for regime_name in ['superT']:
        regime_label = 'sinusoidal_spike_train_N=1000_' + regime_name + '_12' 
        binnedTrain = BinnedSpikeTrain.initFromFile(regime_label, phis)
        Tf = binnedTrain.getTf()
        print 'Tf = ', Tf
        
        theta = binnedTrain.theta;
        print 'theta = ', theta
        ps = binnedTrain._Train._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))
        abg = abg_true
        xmin = FPMultiPhiSolver.calculate_xmin(Tf, abg, theta)
#        dx = .0125#        dx = .0125; ;
        dx = .0125; 
        dt = FPMultiPhiSolver.calculate_dt(dx, abg, xmin, factor = 5.)
        print 'xmin = ', xmin, ', dx, dt = ', dx, dt
        
        S = FPMultiPhiSolver(theta, phis,
                         dx, dt, Tf, xmin)
   
        S.setTf(Tf)
        Fs = S.c_solve(abg)
        ts = S._ts;
         
        
        filename= RESULTS_DIR + '/Fs_%s'%regime_name
        print 'saving Fs to ', filename
        savez(filename, ts=ts,
                         Gs=squeeze(Fs[:,:,-1]),
                         phis=phis,
                         Tf = Tf);
Exemplo n.º 25
0
def postVisualizer():
    N_phi = 20
    print 'N_phi = ', N_phi
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    theta = 20

    base_name = 'sinusoidal_spike_train_N=1000_critical_theta=%d'%theta

    T_thresh = 64.
    
    analyzer = DataAnalyzer('ThetaEstimate_4x100_N=1000')
    sample_id = 32
    regime_label = base_name + '%d'%theta            
    file_name = base_name + '_%d'%sample_id
    print file_name

    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
    binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh)
    
    regime_name = 'theta%d'%theta
    abg_true = analyzer.getTrueParamValues(regime_name);
    print abg_true
    
    
    abg_fortet = analyzer.getEstimates(sample_id, regime_name, 'Fortet')[0]
    print abg_fortet
    visualizeData_vs_Fortet(abg_fortet, binnedTrain, theta,title_tag = 'Fortet: estimates', save_fig_name='theta20_Fortet_estimates')
    visualizeData_vs_Fortet(abg_true, binnedTrain, theta,title_tag = 'Fortet: true', save_fig_name='theta20_Fortet_true')
    
    abg_fp = analyzer.getEstimates(sample_id, regime_name, 'FP')[0]
    print abg_fp
    dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0) 
    phis = binnedTrain.bins.keys();
    S = FPMultiPhiSolver(binnedTrain.theta, phis,
                         dx, dt,
                         binnedTrain.getTf(), X_min = -1.0) 
    visualizeData_vs_FP(S, abg_fp, binnedTrain,title_tag = 'FP: estimates', save_fig_name='theta20_FP_estimates')
    visualizeData_vs_FP(S, abg_true, binnedTrain,title_tag = 'FP: true', save_fig_name='theta20_FP_true')
Exemplo n.º 26
0
def BFGSEstimator():
    from scipy.optimize import fmin_bfgs
    
    phis =  linspace(.05, .95, 10)
    phi_omit = None
    
    file_name = 'sinusoidal_spike_train_T=20000_subT_13.path'
    
    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
    binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh = 32.)
    print 'N_bins = ', len(binnedTrain.bins.keys())

    Tf = binnedTrain.getTf()
    print 'Tf = ', Tf
   
    solver_phis = binnedTrain.bins.keys();
    theta = binnedTrain.theta
    x_min = -.5;
    
    S = FPMultiPhiSolver(theta, solver_phis,
                     .1, .1,
                     Tf, X_MIN = x_min)
    
    ps = binnedTrain._Path._params
    abg_true = array([ps._alpha, ps._beta, ps._gamma]);
    print 'true = ', abg_true
#    abg_init = abg_true + .5*abg_true*randn(3);
    abg_init = [ 0.37345572 , 0.32178958 , 0.31556914]
    abg_init = [ 0.37366108 , 0.32516912,  0.31569902]
    print 'init = ', abg_init
    
    lE = Estimator(S, binnedTrain)
    
#    abg_est = fmin_bfgs(lE.func, abg_init, lE.dfunc,  gtol = 1e-6*binnedTrain.getTf(), maxiter= 128, full_output = 0)
    abg_est, fopt, gopt, Bopt, func_calls, grad_calls, warnflag  = fmin_bfgs(lE.func, abg_init,
                                                     lE.dfunc,  gtol = 1e-5*binnedTrain.getTf(), maxiter=32, full_output = 1)
    
    return abg_est
Exemplo n.º 27
0
def NMSuperSinEstimator():
        phis =  linspace(.05, .95, 40)
        file_name = 'sinusoidal_spike_train_T=10000_superSin.path'

        print '#'*64
        
        print file_name
    
        binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
        
        phi_omit = None
        binnedTrain.pruneBins(phi_omit, N_thresh = 128, T_thresh = 8.)
        print 'N_bins = ', len(binnedTrain.bins.keys())
        
        Tf = binnedTrain.getTf()
        print 'Tf = ', Tf
            
        dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, 5., 2.)
    
        binphis = binnedTrain.bins.keys();
        theta = binnedTrain.theta
        
        S = FPMultiPhiSolver(theta, binphis,
                             dx, dt,
                             Tf)
    
        ps = binnedTrain._Train._params
        abg_true = array((ps._alpha, ps._beta, ps._gamma))
        print 'abg_true = ', abg_true
    
        abg_init = [0.716 , 0.199 , 0.51]
        print 'abg_init = ', abg_init
            
        start = time.clock()
        abg_est = NMEstimator(S, binnedTrain, abg_init)
        print 'Est. time = ', time.clock() - start
        print 'abg_est = ', abg_est
        print 'error = ', abg_true - abg_est
Exemplo n.º 28
0
def Harness(sample_id=13, regime_name='superSin', N_spikes = 1000, visualize=False):
    from scipy.stats.distributions import norm
    N_phi = 20;
    phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes

    regime_label = base_name + regime_name
#    T_thresh = 128.;     
    file_name = regime_label + '_' + str(sample_id)
    print file_name
        
    binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
    
#    print 'Warning: pruning bins'
#    binnedTrain.pruneBins(None, N_thresh = 100)
    
    bins = binnedTrain.bins;
    phis = bins.keys()
    N_phi = len(phis)
    
    alpha,  beta, gamma, theta = binnedTrain._Train._params.getParams() 

    def loss_function_simple(abg, visualize, fig_tag = ''):
        error = .0;
        if visualize:
            figure()
                        
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
            Is = bins[phi_m]['Is']
            uniqueIs = bins[phi_m]['unique_Is']
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m,binnedTrain.theta)
            
            LHS_numerator = movingThreshold(uniqueIs[1:]) *sqrt(2.)
            LHS_denominator = b * sqrt(1 - exp(-2*uniqueIs[1:]))
            LHS = 1 -  norm.cdf(LHS_numerator / LHS_denominator)
            
            RHS = zeros_like(LHS)
            N  = len(Is)
            for rhs_idx in xrange(1,len(uniqueIs)):
                t = uniqueIs[rhs_idx]
                lIs = Is[Is<t]
                taus = t - lIs;
                
                numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.)
                denominator = b *  sqrt(1. - exp(-2*taus))
                RHS[rhs_idx-1] = sum(1. - norm.cdf(numerator/denominator)) / N
            
            weight = len(Is)
            lerror = dot((LHS - RHS)**2 , diff(uniqueIs)) * weight;
            error += lerror
        
            if visualize:
                    subplot(ceil(len(phis)/2),2, phi_idx+1);hold(True)
                    ts = uniqueIs[1:]; 
                    plot(ts, LHS, 'b');
                    plot(ts, RHS, 'rx');
#                    annotate('$\phi$ = %.2g'%(phi_m), ((min(ts), max(LHS)/2.)), ) 
                    annotate('lerror = %.3g'%lerror,((min(ts), max(LHS)/2.)), ) 
        if visualize:
            subplot(ceil(len(phis)/2),2, 1);
            title(fig_tag)          
        return error
    
    def loss_function_nonvectorized(abg, visualize=False):
        error = .0;
        if visualize:
            figure();  
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
#        for phi_m in phis:
            Is = bins[phi_m]['Is']
            N  = len(Is);
            Tf = amax(Is);
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m)
            
            def LHS(ts):
                LHS_numerator = movingThreshold(ts) *sqrt(2.)
                LHS_denominator = b * sqrt(1 - exp(-2*ts))
                return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
            
#            def RHS(t):
#                lIs = Is[Is<t];
#                taus = t - lIs;
#                numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.)
#                denominator = b *  sqrt(1. - exp(-2*taus))
#                return sum(1. - norm.cdf(numerator/denominator)) / N
              
            def RHS(ts):
                if False == iterable(ts):
                    ts =  [ts] 
                rhs = empty_like(ts)
                for t, t_indx in zip(ts, xrange(size(ts))):
                    lIs = Is[Is<t];
                    taus = t - lIs;
                    numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.)
                    denominator = b *  sqrt(1. - exp(-2*taus))
                    rhs[t_indx] = sum(1. - norm.cdf(numerator/denominator)) / N
                return rhs
            
            integrand = lambda t: (LHS(t) - RHS(t)) **2
            from scipy.integrate import quad, quadrature, fixed_quad

#            quadrature, quad_error = quad(integrand, a= 1e-8, b=Tf+1e-8, limit = 50)
            quadrature, quad_error = quadrature(integrand, a= 1e-8, b=Tf+1e-8,
                                                tol=5e-03, rtol=1.49e-04,
                                                maxiter = 64,
                                                vec_func = True)
#            val , err_msg = fixed_quad( integrand, a= 1e-8, b=Tf+1e-8,
#                                                n = 12)
#            print 'quadrature = ',quadrature
#            print 'val = ',val
#            print 'difference = ', quadrature - val
            
            weight = len(Is)
            #VISUALIZE FOR NOW:
            if visualize:
                subplot(ceil(len(phis)/2),2, phi_idx+1);hold(True)
                ts = linspace(1e-8,  Tf+1e-8, 100) ; 
                lhs = empty_like(ts); rhs = empty_like(ts); 
                for t, t_indx in zip(ts,
                                      xrange(len(ts))):
                    lhs[t_indx] = LHS(t);
                    rhs[t_indx] = RHS(t);
                plot(ts, lhs, 'b');
                plot(ts, rhs, 'rx'); 
            
            error +=  quadrature* weight;
        return error
    
    def loss_function_quadGaussian(abg, visualize=False, fig_tag = ''):
        error = .0;
        if visualize:
            figure();  
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
#        for phi_m in phis:
            Is = bins[phi_m]['Is']
            unique_Is = bins[phi_m]['unique_Is']
            N_Is  = len(Is);
            Tf = amax(Is);
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m)
            
            def LHS(ts):
                LHS_numerator = movingThreshold(ts) *sqrt(2.)
                LHS_denominator = b * sqrt(1 - exp(-2*ts))
                return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
              
            def RHS(ts):
                if False == iterable(ts):
                    ts =  array([ts])
#                rhs = empty_like(ts)
                
#                Is.reshape((len(Is),1)) 
                lIs = tile(Is,  len(ts) ).reshape((len(ts), len(Is))).transpose()
                lts = tile(ts, (len(Is),1 ) )
                mask = lIs < lts
                taus = (lts - lIs); #*mask
                #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway:
                numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.)
                denominator = b *  sqrt(1. - exp(-2*abs(taus)))
                
                rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is
                return rhs
            
            integrand = lambda t: (LHS(t) - RHS(t)) **2
            from scipy.integrate import quad, quadrature, fixed_quad

#            valcheck, quad_error = quad(integrand, a= 1e-8, b=Tf+1e-8, limit = 64)
            
            val, quad_error = quadrature(integrand, a= 1e-8, b=Tf+1e-8,
                                                tol=5e-03, rtol=1.49e-04,
                                                maxiter = 64,
                                                vec_func = True)
            
            weight = len(Is)
            lerror = val* weight;
            error += lerror

            if visualize:
                subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True)
#                ts = linspace(1e-8,  Tf+1e-8, 100) ; 
                ts = unique_Is[1:];
                lhs  = LHS(ts);
                rhs  = RHS(ts);
                plot(ts, lhs, 'b');
                plot(ts, rhs, 'rx'); 
                annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) 
        
        if visualize:
            subplot(ceil(len(phis)/2),2,1);
            title(fig_tag)
        return error
    
    def loss_function_L1(abg, visualize=False, fig_tag = ''):
        error = .0;
        if visualize:
            figure();  
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
#        for phi_m in phis:
            Is = bins[phi_m]['Is']
            unique_Is = bins[phi_m]['unique_Is']
            N_Is  = len(Is);
            Tf = amax(Is);
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m, theta)
            
            def LHS(ts):
                LHS_numerator = movingThreshold(ts) *sqrt(2.)
                LHS_denominator = b * sqrt(1 - exp(-2*ts))
                return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
              
            def RHS(ts):
                if False == iterable(ts):
                    ts =  array([ts])
#                rhs = empty_like(ts)
                
#                Is.reshape((len(Is),1)) 
                lIs = tile(Is,  len(ts) ).reshape((len(ts), len(Is))).transpose()
                lts = tile(ts, (len(Is),1 ) )
                mask = lIs < lts
                taus = (lts - lIs); #*mask
                #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway:
                numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.)
                denominator = b *  sqrt(1. - exp(-2*abs(taus)))
                
                rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is
                return rhs
            
            integrand = lambda t: abs(LHS(t) - RHS(t))
            from scipy.integrate import quad, quadrature, fixed_quad

            print unique_Is
            val, quad_error = quad(integrand, a= 1e-8, b=Tf+1., limit = 64,
                                   points = sort(unique_Is) )
            
#            val, quad_error = quadrature(integrand, a= 1e-8, b=Tf+1e-8,
#                                                tol=5e-03, rtol=1.49e-04,
#                                                maxiter = 64,
#                                                vec_func = True)
#            val , err_msg = fixed_quad( integrand, a= 1e-8, b=Tf+1e-8,

            weight = len(Is)
            lerror = val* weight;
            error += lerror

            if visualize:
                subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True)
#                ts = linspace(1e-8,  Tf+1e-8, 100) ; 
                ts = unique_Is[1:];
                lhs  = LHS(ts);
                rhs  = RHS(ts);
                plot(ts, lhs, 'b');
                plot(ts, rhs, 'rx'); 
                annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) 
        
        if visualize:
            subplot(ceil(len(phis)/2),2,1); title(fig_tag)
        return error
    
    
    def loss_function_manualquad(abg, visualize=False, fig_tag = ''):
        error = .0;
        if visualize:
            figure();  
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
            Is = bins[phi_m]['Is']
            unique_Is = bins[phi_m]['unique_Is']
            N_Is  = len(Is);
            Tf = amax(Is);
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m, binnedTrain.theta)
            
            def LHS(ts):
                LHS_numerator = movingThreshold(ts) *sqrt(2.)
                LHS_denominator = b * sqrt(1 - exp(-2*ts))
                return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
              
            def RHS(ts):
                if False == iterable(ts):
                    ts =  array([ts])
                lIs = tile(Is,  len(ts) ).reshape((len(ts), len(Is))).transpose()
                lts = tile(ts, (len(Is),1 ) )
                mask = lIs < lts
                taus = (lts - lIs); #*mask
                #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway:
                numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.)
                denominator = b *  sqrt(1. - exp(-2*abs(taus)))
                
                rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is
                return rhs
            
            integrand = lambda t: (LHS(t) - RHS(t)) **2
            dt = 1e-2;
            ts_manual = arange(1e-8, Tf+1e-2, dt )
            integrand = LHS(ts_manual) - RHS(ts_manual)
            val  = dot(integrand, integrand)*dt;
            
            weight = len(Is)
            lerror = val* weight;
            error += lerror

            if visualize:
                subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True)
#                ts = linspace(1e-8,  Tf+1e-8, 100) ; 
                ts = unique_Is[1:];
                lhs  = LHS(ts);
                rhs  = RHS(ts);
                plot(ts, lhs, 'b');
                plot(ts, rhs, 'rx'); 
                annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) 
        
        if visualize:
            subplot(ceil(len(phis)/2),2,1);
            title(fig_tag)
        return error

    def loss_function_supnormalized(abg, visualize=False, fig_tag = ''):
        error = .0;
        if visualize:
            figure();  
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
            Is = bins[phi_m]['Is']
            unique_Is = bins[phi_m]['unique_Is']
            N_Is  = len(Is);
            Tf = amax(Is);
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m, binnedTrain.theta)
            
            def LHS(ts):
                LHS_numerator = movingThreshold(ts) *sqrt(2.)
                LHS_denominator = b * sqrt(1 - exp(-2*ts))
                return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
              
            def RHS(ts):
                if False == iterable(ts):
                    ts =  array([ts])
                lIs = tile(Is,  len(ts) ).reshape((len(ts), len(Is))).transpose()
                lts = tile(ts, (len(Is),1 ) )
                mask = lIs < lts
                taus = (lts - lIs); #*mask
                #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway:
                numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.)
                denominator = b *  sqrt(1. - exp(-2*abs(taus)))
                
                rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is
                return rhs
            
            dt = 1e-3;
            ts_manual = arange(1e-8, Tf+1e-2, dt)
            lhs = LHS(ts_manual)
            difference = abs(lhs - RHS(ts_manual))/amax(lhs)
            val  = amax(difference);
            
            weight = len(Is)
            lerror = val* weight;
            error += lerror

            if visualize:
                subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True)
#                ts = linspace(1e-8,  Tf+1e-8, 100) ; 
                ts = unique_Is[1:];
                lhs  = LHS(ts);
                rhs  = RHS(ts);
                plot(ts, lhs, 'b');
                plot(ts, rhs, 'rx'); 
                annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) 
        
        if visualize:
            subplot(ceil(len(phis)/2),2,1);
            title(fig_tag)
            
        return error
    def loss_function_sup(abg, visualize=False, fig_tag = ''):
        error = .0;
        if visualize:
            figure();  
        for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
            Is = bins[phi_m]['Is']
            unique_Is = bins[phi_m]['unique_Is']
            N_Is  = len(Is);
            Tf = amax(Is);
             
            a,b,g = abg[0], abg[1], abg[2]
            movingThreshold = getMovingThreshold(a,g, phi_m, binnedTrain.theta)
            
            def LHS(ts):
                LHS_numerator = movingThreshold(ts) *sqrt(2.)
                LHS_denominator = b * sqrt(1 - exp(-2*ts))
                return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
              
            def RHS(ts):
                if False == iterable(ts):
                    ts =  array([ts])
                lIs = tile(Is,  len(ts) ).reshape((len(ts), len(Is))).transpose()
                lts = tile(ts, (len(Is),1 ) )
                mask = lIs < lts
                taus = (lts - lIs); #*mask
                #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway:
                numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.)
                denominator = b *  sqrt(1. - exp(-2*abs(taus)))
                
                rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is
                return rhs
            
            dt = 1e-3;
            ts_manual = arange(1e-8, Tf+1e-2, dt)
            lhs = LHS(ts_manual)
            difference = abs(lhs - RHS(ts_manual)) 
            val  = amax(difference);
            
            weight = len(Is)
            lerror = val* weight;
            error += lerror

            if visualize:
                subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True)
#                ts = linspace(1e-8,  Tf+1e-8, 100) ; 
                ts = unique_Is[1:];
                lhs  = LHS(ts);
                rhs  = RHS(ts);
                plot(ts, lhs, 'b');
                plot(ts, rhs, 'rx'); 
                annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) 
        
        if visualize:
            subplot(ceil(len(phis)/2),2,1);
            title(fig_tag)
            
        return error
    
    #EXPERIMENT:
   
#    Analyzer = DataAnalyzer()
    
    def outlinept():
        pass
    
    
    
    analyzer = DataAnalyzer('FvsWF_4x16');

    
    
    abg_true = analyzer.getTrueParamValues(regime_name)
    loss_function_L1(abg_true, visualize=True)
    return 

    quad_estimated = analyzer.getEstimates(sample_id, regime_name, 'QuadFortet')[0]
    simple_estimated = analyzer.getEstimates(sample_id, regime_name, 'Fortet')[0]

    for abg, tag, L in zip(3*[abg_true, quad_estimated],
                           ['sup_true_params'      , 'sup_estimated_params',
                            'supnormailzed_true_params', 'supnormailzed_estimated_params',
                            'manualquad_true_params' , 'manualquad_estimated_params'],
                           2*[loss_function_sup]+
                           2*[loss_function_supnormalized] +
                           2*[loss_function_manualquad]):
        start = time.clock()
        loss =  L(abg,visualize, fig_tag = regime + '_' + tag);
        end = time.clock()  
        print tag, ':%.2f,%.2f,%.2f:' %(abg[0],abg[1],abg[2]), 'error = %.4f'%loss , ' | compute time = ', end - start  
Exemplo n.º 29
0
def FortetHarness():
    
    errors = []
    
    for N_spikes, N_phi in zip( [100,1000],
                                [8,20]):
        phi_norms =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
        base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes


    for regime  in ['superT', 'crit', 'subT','superSin']:
        regime_label = base_name + regime

        for sample_id in [1,23, 36, 77, 99]:
            file_name = regime_label + '_' + str(sample_id)
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms)
        
            bins = binnedTrain.bins;
            phis = bins.keys()
            N_phi = len(phis)
            
            alpha,  beta, gamma, theta = binnedTrain._Train._params.getParams() 
            
            from scipy.stats.distributions import norm
            def loss_function_supnormalized(abg):
                error = .0;
                for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
                    Is = bins[phi_m]['Is']
                    N_Is  = len(Is);
                    Tf = amax(Is);
                     
                    a,b,g = abg[0], abg[1], abg[2]
                    movingThreshold = getMovingThreshold(a,g, phi_m,binnedTrain.theta)
                    
                    def LHS(ts):
                        LHS_numerator = movingThreshold(ts) *sqrt(2.)
                        LHS_denominator = b * sqrt(1 - exp(-2*ts))
                        return 1 -  norm.cdf(LHS_numerator / LHS_denominator);
                      
                    def RHS(ts):
                        if False == iterable(ts):
                            ts =  array([ts])
                        lIs = tile(Is,  len(ts) ).reshape((len(ts), len(Is))).transpose()
                        lts = tile(ts, (len(Is),1 ) )
                        mask = lIs < lts
                        taus = (lts - lIs); #*mask
                        #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway:
                        numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.)
                        denominator = b *  sqrt(1. - exp(-2*abs(taus)))
                        
                        rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is
                        return rhs
                    
                    ts_manual = linspace(1e-8, Tf+1e-2, 500)
                    lhs = LHS(ts_manual)
                    
                    rhs = RHS(ts_manual)
                    
                    difference = abs(lhs - rhs)/amax(lhs)
                    
                    val  = amax(difference);
                    
                    weight = len(Is)
                    lerror = val* weight;                    
                    
                    error += lerror                    
                return error
            
            def closs_function(abg):
                error = .0;
                for (phi_m, phi_idx) in zip(phis, xrange(N_phi)):
                    Is = bins[phi_m]['Is']
                    N_Is  = len(Is);
                    Tf = amax(Is);
                    ts = linspace(1e-8, Tf+1e-2, 500)
                    #Call C to compute the lhs - rhs
                    abgthphi = r_[abg, theta, phi_m]
                    difference = ext_fpc.FortetError(abgthphi, ts, Is)
                    raw_error  = amax(difference);
                    weighted_error = N_Is * raw_error;                    
                    error += weighted_error                    
                return error
            
            abg = array([alpha,beta,gamma])
            start = time.clock()
            py_error = loss_function_supnormalized(abg)
            print 'pytime = ', time.clock() - start
            start = time.clock()
            c_error = closs_function(abg) 
            print 'ctime = ', time.clock() - start
            
            errors.append([py_error,
                           c_error])
            
    pys = array(errors)[:,0]
    cs = array(errors)[:,1]
    figure(); hold(True)
    plot(pys, cs, '*'); plot(pys, pys, '-')
Exemplo n.º 30
0
def writeManual():
    from BinnedSpikeTrain import BinnedSpikeTrain
    from InitBox import initialize5
    import time

    N_phi = 20;
    print 'N_phi = ', N_phi
    phis =  linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi)
    
    h5file = openFile("manual_write.h5", mode = "w", title = "Manually Write Estimate Data")
    grp = h5file.createGroup("/", 'Estimates', "Estimates INformation")
    
    base_name = 'sinusoidal_spike_train_T='
    for regime_name, T_sim, T_thresh in zip(['superT', 'subT', 'crit', 'superSin'],
                                                       [5000 , 20000, 5000, 5000],
                                                       [4., 32, 16., 16.]): 
        
        estTable = h5file.createTable(grp, regime_name, Estimate , "Regime Estimate")
#        sampleTbl = h5file.createTable(grp, regime_name, Estimate , "Regime Estimate")
        
        regime_label = base_name + str(T_sim)+ '_' + regime_name
            
        for sample_id in xrange(1,3):
            file_name = regime_label + '_' + str(sample_id) + '.path'
            print file_name
            
            binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phis)
            ps = binnedTrain._Path._params
            abg_true = array((ps._alpha, ps._beta, ps._gamma))
            if 1 == sample_id:
                print 'abg_true = ', abg_true
            
            phi_omit = None
            binnedTrain.pruneBins(phi_omit, N_thresh = 64, T_thresh=T_thresh)
            print 'N_bins = ', len(binnedTrain.bins.keys())
            
            Tf = binnedTrain.getTf()
            print 'Tf = ', Tf
        
            #Estimate:
            estimate = estTable.row
            
            start = time.clock()
            abg_init = initialize5(binnedTrain)
            finish = time.clock()
            estimate['method'] = 'Initializer'
            estimate['sample_id'] = sample_id
            estimate['alpha'] = abg_init[0]
            estimate['beta'] = abg_init[1]
            estimate['gamma'] = abg_init[2]
            estimate['walltime'] = finish - start
            estimate.append()
                                
            abg_est = abg_init
            start = time.clock()
    #        abg_est = NMEstimator(S, binnedTrain, abg_init)
            time.sleep(rand())
            print 'Est. time = ', time.clock() - start
            print 'abg_est = ', abg_est
                   
            start = time.clock()
    #        abg_est = FortetEstimator(binnedTrain, abg_init)
            time.sleep(2*rand())
            print 'Est. time = ', time.clock() - start
            print 'abg_est = ', abg_est
            
        estTable.flush()
         
    print '#'*44
    print h5file
    h5file.close()