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')
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