Esempio n. 1
0
def TrjRtrv(param, kedm_output):
    N = param.N
    d = param.d
    P = param.P
    N_trn = param.N_trn
    mode = param.mode
    omega = param.omega
    K = param.K
    
    anchor_idx = ktools.randomAnchor(N_trn, N, d+1)
    
    tau_list = kedm_output.tau_list
    trn_list = kedm_output.trn_list
    tst_list = kedm_output.tst_list
    
    A = kedm_output.A
    G = kedm_output.G
    N_tst = param.N_tst
    output = TRJ_OUT()
    
    M = np.shape(anchor_idx)[0]
    if M != len(trn_list):
        print('Fix this!')
        return output
    
    X_ = []
    T = []
    cnt_wrong = 0
    cnt = 0
    for m in range(M):
        list_m = np.nonzero(anchor_idx[m,:])[0]
        if len(list_m) < d:
            cnt_wrong += 1
            continue
        t = trn_list[m]
        Xt = ktools.X_t(A,t,param)
        Xtm = Xt[:,list_m]
        weights = ktools.W(t, tau_list, param)
        Gt = ktools.G_t(G, weights, False)
        Xt_ = ktools.gram2x(Gt, d)
        Xt_m = Xt_[:,list_m]
        R = ktools.rotationXY(Xtm, Xt_m)
        Nt = Xtm.shape[1]
        Xt_ = np.matmul(R,Xt_ - np.matmul(Xt_m,np.ones((Nt,N))/Nt) ) + np.matmul(Xtm,np.ones((Nt,N))/Nt)
        Tt = ktools.T_t(t, param)
        if cnt == 0:
            X_ = Xt_
            T = Tt
        else:
            X_ = np.concatenate((X_, Xt_), axis=0)
            T = np.concatenate((T, Tt), axis=0)
        cnt += 1
    if cnt_wrong == M:
        print('nonsense')
        return output
    T_inv = np.linalg.pinv(T)
    A_ = np.matmul(T_inv,X_)
    A_ = ktools.deconcat(A_, param)
Esempio n. 2
0
def SketchX(param, A,colors, figName):
    import matplotlib.pyplot as plt
    from mpl_toolkits import mplot3d
    
    N = param.N
    d = param.d
    if d > 3 or d < 2:
        print('There is something wrong, dude!')
        return
    M = 300
    maxIter = param.maxIter
    T_tst = param.T_tst
    t = np.linspace(T_tst[0],T_tst[1],M)
    
    
    fSize = 18
    plt.rc('text', usetex=True)
    plt.rc('font', family='serif')
    
    if d == 2:
        plt.xlabel(r'\textit{x}_1',fontsize=18)
        plt.ylabel(r'\textit{x}_2',fontsize=18)
    else:
        ax = plt.axes(projection='3d')
        ax.set_xlabel(r'\textit{x}_1',fontsize=fSize)
        ax.set_ylabel(r'\textit{x}_2',fontsize=fSize)
        ax.set_zlabel(r'\textit{x}_3',fontsize=fSize);
    
    i = 0
    eX = 0
    eDo = 0
    eDi = 0
    while i < maxIter:
        kedm_output = KEDM(param, A)
        cvx_status = kedm_output.status
        if cvx_status == 'optimal':
            i += 1
            print(i,'out of', maxIter)
            trj_output = TrjRtrv(param, kedm_output)
            eX = eX + ktools.mean(trj_output.eX)/maxIter
            eDo = eDo + ktools.mean(kedm_output.eo)/maxIter
            eDi = eDi + ktools.mean(kedm_output.ei)/maxIter
            A_ = trj_output.A_
            X = np.zeros((M,d,N))
            for m in range(M):
                X[m,:,:] = ktools.X_t(A_,t[m],param)
            for n in range(N):
                if d == 2:
                    plt.plot(X[:,0,n], X[:,1,n],c = colors[:,n],linewidth=1/(maxIter**0.5))
                else:
                    ax.plot3D(X[:,0,n], X[:,1,n],X[:,2,n],c = colors[:,n],linewidth=1/(maxIter**0.5))
                plt.pause(0.01)
Esempio n. 3
0
def PlotX(A, param, mode, xlim, ylim, name , color):
    
    import matplotlib.pyplot as plt
    from mpl_toolkits import mplot3d
    import math
    
    N = param.N
    d = param.d
    print(d)
    if d > 3 or d < 2:
        print('There is something wrong, dude!')
        return
    M = 1000
    J = np.eye(N) - np.ones((N,N))/N
    if mode == 'test':
        T_tst = param.T_tst
        T_trn = param.T_trn
        T1 = T_trn[0]- T_tst[0]
        T2 = T_trn[1] - T_trn[0]
        T3 = T_tst[1] - T_trn[1]
        T = T1 + T2 + T3
        int(np.floor(N/2))
        M1 = int(np.floor(T1/T * M))
        M3 = int(np.floor(T3/T * M))
        M2 = M - M1 - M3
        t = np.linspace(T_tst[0],T_tst[1],M)
        dash=[8,3]
    else:
        T_tst = param.T_tst
        t = np.linspace(T_tst[0],T_tst[1],M)
    
    X = np.zeros((M,d,N))
    ax = plt.gca()
    ax.rc('text', usetex=True)
    ax.rc('font', family='serif')
    print(d)
    if d == 2:
        ax.xlabel(r'\textit{x}_1',fontsize=18)
        ax.ylabel(r'\textit{x}_2',fontsize=18)
    else:
        ax = ax.axes(projection='3d')
        ax.set_xlabel(r'\textit{x}_1',fontsize=fSize)
        ax.set_ylabel(r'\textit{x}_2',fontsize=fSize)
        ax.set_zlabel(r'\textit{x}_3',fontsize=fSize);
    
    for m in range(M):
        X[m,:,:] = ktools.X_t(A,t[m],param)
    for n in range(N):
        if mode == 'test':
            idx1 = list(range(0,M1,1))
            idx2 = list(range(M1,M1+M2,1))
            idx3 = list(range(M1+M2,M,1))
            if d==2:
                ax.plot(X[idx2,0,n], X[idx2,1,n],c = color[:,n],linewidth=2)
                ax.plot(X[idx1,0,n], X[idx1,1,n],c = np.zeros((3)),dashes=dash,linewidth=0.5)
                ax.plot(X[idx3,0,n], X[idx3,1,n],c = np.zeros((3)),dashes=dash,linewidth=0.5)
            elif d==3:
                ax.plot3D(X[idx2,0,n], X[idx2,1,n],c = color[:,n],linewidth=2)
                ax.plot3D(X[idx1,0,n], X[idx1,1,n],c = np.zeros((3)),dashes=dash,linewidth=1)
                ax.plot3D(X[idx3,0,n], X[idx3,1,n],c = np.zeros((3)),dashes=dash,linewidth=1)
        else:
            if d==2:
                plt.plot(X[:,0,n], X[:,1,n],c = color[:,n],linewidth=2)
            elif d==3:
                ax.plot3D(X[:,0,n], X[:,1,n],c = colors[:,n],linewidth=2)
    if d==2:
        plt.ylim(ylim)
        plt.xlim(xlim)
    plt.savefig(name+'.eps')
    plt.show()
Esempio n. 4
0
            T = np.concatenate((T, Tt), axis=0)
        cnt += 1
    if cnt_wrong == M:
        print('nonsense')
        return output
    T_inv = np.linalg.pinv(T)
    A_ = np.matmul(T_inv,X_)
    A_ = ktools.deconcat(A_, param)


if mode == 2:
    A_ = ktools.sine2fourier(A_)
    
    eX = np.zeros(N_tst)
    for i_t, t in enumerate(tst_list):
        X_ = ktools.X_t(A_,t,param)
        X = ktools.X_t(A,t,param)
        eX[i_t] = np.linalg.norm(X-X_,'fro')/np.linalg.norm(X,'fro')

    output.A_ = A_
    output.eX = eX

return output
###########################################################################
def PlotX(A, param, mode, xlim, ylim, name , color):
    
    import matplotlib.pyplot as plt
    from mpl_toolkits import mplot3d
    import math
    
    N = param.N
Esempio n. 5
0
t = np.linspace(param.T_tst[0], param.T_tst[1], M)

colors = np.random.rand(3, N)
fig_name = 'kedm_ambiguity.pdf'
fSize = 18
A = ktools.randomAs(param)

B0 = np.random.randn(d, d)
B1 = np.random.randn(d, d)
B2 = np.random.randn(d, d)
B3 = np.random.randn(d, d)
J = np.eye(N) - np.ones((N, N)) / N
X = np.zeros((M, d, N))
Y = np.zeros((M, d, N))
for m in range(M):
    X[m, :, :] = ktools.X_t(A, t[m], param)
    Y[m, :, :] = np.matmul(X[m, :, :], J)
    f = B0 + t[m] / 10 * B1 + math.cos(t[m]) / 20 * B2 + math.sin(
        t[m]) * B3 / 20
    ff = np.matmul(f, f.T)
    u, s, vh = np.linalg.svd(ff, full_matrices=True)
    Y[m, :, :] = np.matmul(u, Y[m, :, :])

if d == 2:
    fig, axes = plt.subplots(1, 2)
    for n in range(N):
        axes[0].plot(X[:, 0, n], X[:, 1, n], c=colors[:, n], linewidth=2)
    for n in range(N):
        axes[1].plot(Y[:, 0, n], Y[:, 1, n], c=colors[:, n], linewidth=2)
elif d == 3:
    fig = plt.figure(figsize=plt.figaspect(0.5))