def main(args):
	init = init_state(L=args.L)
	obs = sim(obs=init, num_iters=args.num_iters, v_max=args.v_max, p=args.p)

	# Display the results
	disp = np.zeros(shape=(args.num_iters, args.L))
	for i in range(args.num_iters):
		for j in range(args.L):
			disp[i][j] = 1 if obs[i][j] > -1 else 0

	plt.imshow(disp, cmap="Greys", interpolation="nearest")
	plt.gca().invert_yaxis()
	plt.ylabel("Timestep")
	plt.xlabel("Flow")
	plt.show()
def substitute_sym_J(z, J_sym, dim=5, layout="REG", plot=True):
    r = analytic.redundant()
    r.create_redundant(layout=layout, order=dim, print_pq=False)
    g = z[:r.N]
    y = z[r.N:]
    r.J = J_sym
    J_num = r.substitute_J(g, y)
    if plot:
        plt.imshow(np.absolute(J_num), interpolation="nearest")
        plt.show()
    return J_num


if __name__ == "__main__":
    s = simulator.sim(layout="REG", order=5)
    s.generate_antenna_layout()
    phi, zeta = s.calculate_phi(s.ant[:, 0], s.ant[:, 1])

    J_sym, N, L = construct_sym_J_layout()
    g = np.random.randn(N) + 1j * np.random.randn(N)
    y = np.random.randn(L) + 1j * np.random.randn(L)
    #g = np.ones((N,))
    #y = np.ones((L,))
    z = np.hstack([g, y])

    J_num = substitute_sym_J(z, J_sym)

    JH_num = J_num.transpose().conjugate()
    v_num = compute_v(z, N, phi)
def substitute_sym_J(z,J_sym,dim=5,layout="REG",plot=True):
    r = analytic.redundant()
    r.create_redundant(layout=layout,order=dim,print_pq=False)
    g = z[:r.N]
    y = z[r.N:]
    r.J = J_sym
    J_num = r.substitute_J(g,y)
    if plot:
       plt.imshow(np.absolute(J_num),interpolation="nearest")
       plt.show()
    return J_num


   
if __name__ == "__main__":
   s = simulator.sim(layout="REG",order=5)
   s.generate_antenna_layout()
   phi,zeta = s.calculate_phi(s.ant[:,0],s.ant[:,1])
 
   J_sym,N,L = construct_sym_J_layout()
   g = np.random.randn(N)+1j*np.random.randn(N)
   y = np.random.randn(L)+1j*np.random.randn(L)
   #g = np.ones((N,))
   #y = np.ones((L,))
   z = np.hstack([g,y])
   
   J_num = substitute_sym_J(z,J_sym)

   JH_num = J_num.transpose().conjugate()
   v_num = compute_v(z,N,phi)
def do_sparc_experiment(SNR=5,method="PCG",min_order=1,max_order=3,layout="HEX",time_var=False,exp_num=5,type_exp="G_OLD",freq_enabled=False):
    order_vec = np.arange(min_order,max_order+1)
    for e in xrange(exp_num):
 
        dir_name = layout+"_"+method+"_"+str(SNR)+"_"+str(time_var)+"_"+str(e)

        if freq_enabled:
           dir_name = layout+"_"+method+"_"+str(SNR)+"_"+str(e)+"_"+str(time_var)+"_"+type_exp+"_f
        else:
           dir_name = layout+"_"+method+"_"+str(SNR)+"_"+str(e)+"_"+str(time_var)+"_"+type_exp
    
        if not os.path.isdir("./"+dir_name): 
           os.system("mkdir "+dir_name)

        for k in xrange(len(order_vec)):
            print "*********"
            print "e = ",e
            print "k = ",k
            print "order_vec = ",order_vec
            print "*********"
            n_steps = 50 #IF TIME_DOMAIN EXPERIMENT
            s = simulator.sim(nsteps=nsteps,layout=layout,order=order_vec[k],seed=e) #INSTANTIATE OBJECT
            s.generate_antenna_layout()
            phi,zeta = s.calculate_phi(s.ant[:,0],s.ant[:,1])
            PQ = s.create_PQ(phi,s.L)
            s.uv_tracks()
            point_sources = s.create_point_sources(100,fov=3,a=2)
            
            if freq_enabled: #DO A FREQ_DOMAIN EXPERIMENT 
               nsteps = 1024 #IF FREQ DOMAIN EXPERIMENT
               s.create_uv_f(num_channels = nsteps)
               u_temp = s.u_f
               v_temp = s.v_f
               w_temp = s.w_f
            else: #DO A TIME_DOMAIN EXPERIMENT
               u_temp = s.u_m
               v_temp = s.v_m
               w_temp = s.w_m

            #CHOOSE THE TYPE OF GAIN ERROR TO ADD
            if type_exp == "F":
               g = s.generate_phase_slope_gains(num_channels=nsteps)
            elif type_exp == "G":
               g = s.create_antenna_gains(s.N,0.9,0.8,10,1,5,nsteps,plot=False)
            else:
               g = s.create_antenna_gains_old(s.N,10,0.5,5,nsteps,plot = False)
	    
            D,sig = s.create_vis_mat(point_sources,u_temp,v_temp,g=g,SNR=SNR,w_m=None)
            M,sig = s.create_vis_mat(point_sources,u_temp,v_temp,g=g,SNR=None,w_m=None) #PREDICTED VIS
            sparc_object = SPARC(s.N,s.L,phi,zeta,PQ)
            z_cal,c_cal,G_cal,M_cal,time_mat,outer_loop,error=sparc_object.levenberg_marquardt_time  (D,s.psi_func_eval,s.xi_func_eval,s.convert_y_to_M,tol1=1e-6,tol2=1e-6,tol3=1e-15,lam=2,max_itr=10000,method=method,time_var=time_var)
            #NB::: time_var deactivates the computation of kappa 
            file_name = "./"+dir_name+"/"+str(order_vec[k])+"_"+str(s.N)+"_"+str(s.L)+"_"+dir_name+".p"

            output = open(file_name, 'wb')
            pickle.dump(order_vec[k], output) 
            pickle.dump(s.N, output) 
            pickle.dump(s.L, output) 
            pickle.dump(zeta, output)
            pickle.dump(PQ,output)
            pickle.dump(z_cal,output)        
            pickle.dump(c_cal,output)
            pickle.dump(time_mat,output)
            pickle.dump(outer_loop,output)
            pickle.dump(sparc_object.itr_vec,output)
            pickle.dump(sparc_object.kappa_vec,output)
            pickle.dump(G_cal,output)
            pickle.dump(M_cal,output)
            pickle.dump(D,output)
            pickle.dump(M,output)
            pickle.dump(error,output)
            output.close()
def compute_sparsity():
    N = np.array(xrange(5,332))
    gamma = (5.0*N**2-7.0*N+3.0)/(8.0*N**2-8.0*N+2)

    N_REG = np.array([5,16,33,63,93,123,153,183,213,243,273,303,331])
    gamma_REG = np.zeros((len(N_REG),),dtype=float)    

    counter = 0

    for k in xrange(len(N_REG)):
        print "REG:: k = ",k
        s = simulator.sim(nsteps=50,layout="REG",order=N_REG[k],seed=1)   
        s.generate_antenna_layout()
        phi,zeta = s.calculate_phi(s.ant[:,0],s.ant[:,1])
        PQ = s.create_PQ(phi,s.L)
        sparc_object = SPARC(s.N,s.L,phi,zeta,PQ)
        H_int = sparc_object.generate_H_formula_int(s.psi_func_eval,s.xi_func_eval)
        non_zero = np.sum(H_int)
        gamma_REG[counter] = 1-(1.0*non_zero)/H_int.shape[0]**2 
        counter = counter + 1


    S_SQR = np.array([3,4,6,8,10,12,14,16,18])
    N_SQR = S_SQR**2
    gamma_SQR = np.zeros((len(N_SQR),),dtype=float)    

    counter = 0

    for k in xrange(len(S_SQR)):
        print "SQR:: k = ",k
        s = simulator.sim(nsteps=50,layout="SQR",order=S_SQR[k],seed=1)   
        s.generate_antenna_layout()
        phi,zeta = s.calculate_phi(s.ant[:,0],s.ant[:,1])
        PQ = s.create_PQ(phi,s.L)
        sparc_object = SPARC(s.N,s.L,phi,zeta,PQ)
        H_int = sparc_object.generate_H_formula_int(s.psi_func_eval,s.xi_func_eval)
        non_zero = np.sum(H_int)
        gamma_SQR[counter] = 1-(1.0*non_zero)/H_int.shape[0]**2 
        counter = counter + 1


    R_HEX = np.array([1,2,3,4,5,6,7,8,9,10])
    N_HEX = 3*R_HEX**2+3*R_HEX+1
    gamma_HEX = np.zeros((len(N_HEX),),dtype=float)    

    counter = 0

    for k in xrange(len(R_HEX)):
        print "HEX:: k = ",k
        s = simulator.sim(nsteps=50,layout="HEX",order=R_HEX[k],seed=1)   
        s.generate_antenna_layout()
        phi,zeta = s.calculate_phi(s.ant[:,0],s.ant[:,1])
        PQ = s.create_PQ(phi,s.L)
        sparc_object = SPARC(s.N,s.L,phi,zeta,PQ)
        H_int = sparc_object.generate_H_formula_int(s.psi_func_eval,s.xi_func_eval)
        non_zero = np.sum(H_int)
        gamma_HEX[counter] = 1-(1.0*non_zero)/H_int.shape[0]**2 
        counter = counter + 1 

    output = open("sparsity.p", 'wb')
    pickle.dump(N, output) 
    pickle.dump(gamma, output) 
    pickle.dump(N_REG, output) 
    pickle.dump(gamma_REG, output)
    pickle.dump(S_SQR,output)
    pickle.dump(N_SQR,output)
    pickle.dump(gamma_SQR,output)
    pickle.dump(R_HEX,output)
    pickle.dump(N_HEX,output)
    pickle.dump(gamma_HEX,output)
    output.close()
   

    '''