def plotiterationinfo(fname,noise, m,n,ts,plot="no"):
    # import glob
    import json
    # import re
    noisestr = ""

    if(noise!="0"):
        noisestr = "_noisepct"+noise
    folder = "%s%s_%s"%(fname,noisestr,ts)
    if not os.path.exists(folder+"/plots"):
        os.mkdir(folder+'/plots')

    # optjsonfile = folder+"/plots/Joptdeg_"+fname+noisestr+"_jsdump_opt6.json"
    #
    # if not os.path.exists(optjsonfile):
    #     print("optjsonfile: " + optjsonfile+ " not found")
    #     exit(1)
    #
    # if optjsonfile:
    #     with open(optjsonfile, 'r') as fn:
    #         optjsondatastore = json.load(fn)
    #
    # optm = optjsondatastore['optdeg']['m']
    # optn = optjsondatastore['optdeg']['n']
    # print(optm,optn)
    rappsipfile = "%s/out/%s%s_%s_p%s_q%s_ts%s.json"%(folder,fname,noisestr,ts,m,n,ts)

    if rappsipfile:
        with open(rappsipfile, 'r') as fn:
            datastore = json.load(fn)
    ts = datastore['trainingscale']
    trainingsize =datastore['trainingsize']
    totaltime = datastore['log']['fittime']
    iterationinfono = len(datastore['iterationinfo'])

    noofmultistarts = np.array([])
    mstime = np.array([])
    robobj = np.array([])
    fittime = np.array([])
    lsqobj = np.array([])

    interationinfo = datastore['iterationinfo']
    robargdata = {0:[],1:[],2:[]}



    for num,iter in enumerate(interationinfo):
        roboinfo = iter["robOptInfo"]["info"]
        noofmultistarts = np.append(noofmultistarts,roboinfo[len(roboinfo)-1]["log"]["noRestarts"])
        mstime = np.append(mstime,roboinfo[len(roboinfo)-1]["log"]["time"])
        robobj = np.append(robobj,roboinfo[len(roboinfo)-1]["robustObj"])
        fittime = np.append(fittime,iter["log"]["time"])
        lsqobj = np.append(lsqobj,iter["leastSqObj"])
        print(str(num))
        print(roboinfo[0]["robustArg"])
        if(plot == "yes_3" or plot == "yes_2"):
            for i in range(3):
                robargdata[i].append(roboinfo[0]["robustArg"][i])

    Xvals = range(1,len(interationinfo)+1)
    import matplotlib.pyplot as plt
    # f, axes = plt.subplots(4, sharex=True,figsize=(12,12))
    f, axes = plt.subplots(2, sharex=True, figsize=(12,12))
    # p0, = axes[0].plot(Xvals,np.ma.log10(noofmultistarts),'g')
    tmp = axes[0]
    axes[0] = axes[1]
    axes[1] = tmp
    msax = axes[0].twinx()
    p01, = axes[0].plot(Xvals,np.ma.log10(mstime),'r--',label="multistart time")
    p02, = msax.plot(Xvals,robobj,'b-')

    firerrorax = axes[1].twinx()
    p11, = axes[1].plot(Xvals,np.ma.log10(fittime),'r--')
    p12, = firerrorax.plot(Xvals,np.ma.log10(lsqobj),'b')

    axes[1].set_xlabel("no. of iterations")
    # axes[0].set_ylabel("log$_{10}$(no. of multistarts)")
    axes[0].set_ylabel("log$_{10}$(multistart time in sec)")
    msax.set_ylabel("$min$ $q(x)$")
    axes[1].set_ylabel("log$_{10}$(fit time in sec)")
    firerrorax.set_ylabel("$log_{10}\\left(min\ \\left|\\left|f-\\frac{p}{q}\\right|\\right||_2^2\\right)$")

    for ax in axes.flat:
        ax.label_outer()
    axes[0].yaxis.label.set_color(p01.get_color())
    msax.yaxis.label.set_color(p02.get_color())
    axes[1].yaxis.label.set_color(p11.get_color())
    firerrorax.yaxis.label.set_color(p12.get_color())

    tkw = dict(size=4, width=1.5)
    axes[0].tick_params(axis='y', colors=p01.get_color(), **tkw)
    msax.tick_params(axis='y', colors=p02.get_color(), **tkw)
    # axes[1].tick_params(axis='y', colors=p1.get_color(), **tkw)
    # axes[2].tick_params(axis='y', colors=p2.get_color(), **tkw)
    axes[1].tick_params(axis='y', colors=p11.get_color(), **tkw)
    firerrorax.tick_params(axis='y', colors=p12.get_color(), **tkw)

    # f.suptitle("%s. m = %d, n = %d, ts = %d (%s). \n Total CPU time = %.4f, Total # of iterations = %d.\nl1 = %.4f, l2 = %.4f, linf = %.4f, nnz = %d, l2/nnz = %f"%(desc,m,n,trainingsize,ts,totaltime,len(interationinfo),l1,l2,linf,nnz,l2/nnz), size=15)
    outfile = "%s/plots/Piterinfo_%s%s_p%s_q%s_ts%s.pdf"%(folder, fname,noisestr,m,n,ts )
    plt.savefig(outfile)
    print("open %s;"%(outfile))
    plt.clf()

    if(plot=="yes_3"):

        from mpl_toolkits.mplot3d import Axes3D
        fig = plt.figure(figsize=(15,10))

        ax = fig.add_subplot(1, 1, 1, projection='3d')
        for num in range(len(robargdata[0])):
            if(abs(robargdata[0][num]) == 1 and abs(robargdata[1][num]) == 1):
                ax.scatter(robargdata[0][num],robargdata[1][num], robargdata[2][num], marker='o',c='blue',s=1000,alpha = 1.0)
            else:
                ax.scatter(robargdata[0][num],robargdata[1][num],robargdata[2][num], marker='x',c='red',s=200,alpha = 1.0)
        # ax.view_init(azim=135, elev=90)

        # ax.scatter(robargdata[0],robargdata[1],robargdata[2])
        ax.set_xlabel('$x1$', fontsize = 12)
        ax.set_ylabel('$x2$', fontsize = 12)
        ax.set_zlabel('$x3$', fontsize = 12)
        outfile = "%s/plots/Piterinfo_robarg_%s%s_p%s_q%s_ts%s.pdf"%(folder, fname,noisestr,m,n,ts )
        ZZ = np.arange(-1, 1, 0.01)
        for l1 in [-1,1]:
            for l2 in [-1,1]:
                XX = l1*np.ones(len(ZZ))
                YY = l2*np.ones(len(ZZ))
                ax.plot(XX,YY,ZZ,c='orange')
        # plt.savefig(outfile)
        # print("open %s;"%(outfile))
        plt.show()
    elif(plot == "yes_2"):
        props = dict(boxstyle='square', facecolor='wheat', alpha=0.5)
        from math import ceil,sqrt
        no = int(ceil(sqrt(iterationinfono)))
        rows = no
        cols = no
        fig, axarr = plt.subplots(rows,cols,figsize=(15,15),sharex=True)
        index = 0
        for index in range(iterationinfono):
            r = int(index / no)
            c = index % no
            if rappsipfile:
                with open(rappsipfile, 'r') as fn:
                    datastore = json.load(fn)
            datastore['pcoeff'] = datastore['iterationinfo'][index]['pcoeff']
            datastore['qcoeff'] = datastore['iterationinfo'][index]['qcoeff']
            rappsip = RationalApproximationSIP(datastore)
            lbbb=-0.95
            ubbb=0.95
            ZZ = np.arange(lbbb, ubbb, 0.01)
            for l1 in [lbbb,ubbb]:
                for l2 in [lbbb,ubbb]:
                    XX = l1*np.ones(len(ZZ))
                    YY = l2*np.ones(len(ZZ))
                    qx = []
                    for num in range(len(ZZ)):
                        X=rappsip._scaler.scale(np.array([XX[num],YY[num],ZZ[num]]))
                        qx.append(rappsip.denom(X))
                    axarr[r][c].plot(ZZ,qx, label="x1 = %.2f, x2 = %.2f"%(l1,l2), linewidth=1)
                    if(abs(robargdata[0][index]) == 1 and abs(robargdata[1][index]) == 1):
                        x1 = np.array([robargdata[0][index],robargdata[1][index],robargdata[2][index]])
                        qx1 = rappsip.denom(x1)
                        ux = rappsip._scaler.unscale(x1)
                        axarr[r][c].scatter(ux[2],qx1, marker='o',c='black',s=100,alpha = 1.0)

            # axarr[r][c].set_xlabel('$x3$', fontsize = 12)
            # axarr[r][c].set_ylabel('$q(x)$', fontsize = 12)
            # axarr[r][c].legend()
            axarr[r][c].set_title("Iteration: %d"%(index+1))
            index+=1

        l= ("x1 = %.2f, x2 = %.2f"%(lbbb,lbbb),
            "x1 = %.2f, x2 = %.2f"%(lbbb,ubbb),
            "x1 = %.2f, x2 = %.2f"%(ubbb,lbbb),
            "x1 = %.2f, x2 = %.2f"%(ubbb,ubbb)
        )
        fig.legend(l,loc='upper center', ncol=4,bbox_to_anchor=(0.5, 0.93), borderaxespad=0.,shadow=False)




        # plt.scatter(robargdata[0],robargdata[1])

        # outfile = "%s/plots/Piterinfo_robarg_%s%s_p%s_q%s_ts%s.pdf"%(folder, fname,noisestr,m,n,ts )
        # plt.savefig(outfile)
        # print("open %s;"%(outfile))
        # plt.show()
        plt.savefig("/Users/mkrishnamoorthy/Desktop/f23-2.pdf")
def plotoptiterationmaps(farr, noisearr, ts):
    import glob
    import json
    import re
    import json
    openfileStr = ""
    lx = "$\\log_{10}(\\alpha(M) + \\alpha(N))$"
    ly = "$\\log_{10}(\\Delta_{MN})$"
    logy = True
    logx = True
    for num, fname in enumerate(farr):
        box = getbox(fname)
        if (len(box) != 2):
            print("{} cannot handle dim != 2. Box len was {}".format(
                sys.argv[0], len(box)))
            sys.exit(1)
        npoints = 1000
        X_test1 = np.linspace(box[0][0], box[0][1], num=npoints)
        X_test2 = np.linspace(box[1][0], box[1][1], num=npoints)

        for noise in noisearr:
            noisestr = ""
            if (noise != "0"):
                noisestr = "_noisepct" + noise
            folder = "%s%s_%s" % (fname, noisestr, ts)
            if not os.path.exists(folder):
                print("Folder '{}' not found.".format(folder))
                sys.exit(1)
            if not os.path.exists(folder + "/plots"):
                os.mkdir(folder + '/plots')
            filelist = np.array(glob.glob(folder + "/out/*.json"))
            filelist = np.sort(filelist)

            for file in filelist:
                if file:
                    with open(file, 'r') as fn:
                        datastore = json.load(fn)
                if (datastore["dim"] != 2):
                    print(
                        "{} cannot handle dim != 2 Dim found in datastore was {}"
                        .format(sys.argv[0], datastore["dim"]))
                    sys.exit(1)
                if (len(datastore["iterationinfo"]) != 3):
                    continue
                # if(datastore["m"] !=5 or datastore["n"] !=3):
                #     continue
                print(file)
                maxy_pq = 0
                miny_pq = np.inf
                maxy_q = 0
                miny_q = np.inf
                data = {}
                m = datastore["m"]
                n = datastore["n"]

                outx1 = "%s/plots/Cimap_X_%s%s_p%d_q%d_ts%s.csv" % (
                    folder, fname, noisestr, m, n, ts)
                outy_pq = "%s/plots/Cimap_Y_pq_%s%s_p%d_q%d_ts%s.csv" % (
                    folder, fname, noisestr, m, n, ts)
                outy_q = "%s/plots/Cimap_Y_q_%s%s_p%d_q%d_ts%s.csv" % (
                    folder, fname, noisestr, m, n, ts)
                polex = "%s/plots/Cimap_pole_x_%s%s_p%d_q%d_ts%s.csv" % (
                    folder, fname, noisestr, m, n, ts)

                for iterno in range(3):
                    data[iterno] = {}
                    if file:
                        with open(file, 'r') as fn:
                            datastore = json.load(fn)
                    ii = datastore["iterationinfo"]
                    datastore['pcoeff'] = ii[iterno]['pcoeff']
                    datastore['qcoeff'] = ii[iterno]['qcoeff']
                    iro = ii[iterno]['robOptInfo']['robustArg']
                    iro.append(ii[iterno]['robOptInfo']['robustObj'])
                    data[iterno]['robarg'] = iro

                    rappsip = RationalApproximationSIP(datastore)
                    Y_pred_pq = []
                    for x in X_test1:
                        for y in X_test2:
                            Y_pred_pq.append(rappsip.predict([x, y]))
                    mmm = max(Y_pred_pq)
                    # print(mmm)
                    if (mmm > maxy_pq):
                        maxy_pq = mmm
                    mmm = min(Y_pred_pq)
                    # print(mmm)
                    if (mmm < miny_pq):
                        miny_pq = mmm
                    data[iterno]['Y_pred_pq'] = Y_pred_pq

                    Y_pred_q = []
                    for x in X_test1:
                        for y in X_test2:
                            X = [x, y]
                            X = rappsip._scaler.scale(np.array(X))
                            Y_pred_q.append(rappsip.denom(X))
                    mmm = max(Y_pred_q)
                    # print(mmm)
                    if (mmm > maxy_q):
                        maxy_q = mmm
                    mmm = min(Y_pred_q)
                    # print(mmm)
                    if (mmm < miny_q):
                        miny_q = mmm
                    data[iterno]['Y_pred_q'] = Y_pred_q

                    np.savetxt(outx1,
                               np.stack((X_test1, X_test2), axis=1),
                               delimiter=",")

                np.savetxt(outy_pq,
                           np.stack(
                               (data[0]['Y_pred_pq'], data[1]['Y_pred_pq'],
                                data[2]['Y_pred_pq']),
                               axis=1),
                           delimiter=",")
                np.savetxt(outy_q,
                           np.stack((data[0]['Y_pred_q'], data[1]['Y_pred_q'],
                                     data[2]['Y_pred_q']),
                                    axis=1),
                           delimiter=",")
                np.savetxt(polex,
                           np.stack((data[0]['robarg'], data[1]['robarg'],
                                     data[2]['robarg']),
                                    axis=1),
                           delimiter=",")

                import matplotlib
                import matplotlib.colors as colors
                import matplotlib as mpl
                import matplotlib.pyplot as plt
                import matplotlib.text as text
                cmap1 = matplotlib.cm.RdYlBu
                cmap3 = matplotlib.cm.seismic
                cmap4 = matplotlib.cm.viridis
                cmap5 = matplotlib.cm.coolwarm
                cmap6 = matplotlib.cm.magma
                cmap2 = 'hot'
                x1lim = (box[0][0], box[0][1])
                x2lim = (box[1][0], box[1][1])
                fig = plt.figure(figsize=(17, 8))
                ypredmaster = []
                from matplotlib.colors import LinearSegmentedColormap

                def CustomCmap(from_rgb, to_rgb):

                    # from color r,g,b
                    r1, g1, b1 = from_rgb

                    # to color r,g,b
                    r2, g2, b2 = to_rgb

                    cdict = {
                        'red': ((0, r1, r1), (1, r2, r2)),
                        'green': ((0, g1, g1), (1, g2, g2)),
                        'blue': ((0, b1, b1), (1, b2, b2))
                    }

                    ccc = LinearSegmentedColormap('custom_cmap', cdict)
                    return ccc

                cmap7 = CustomCmap([0.00, 0.00, 0.00], [0.02, 0.75, 1.00])
                cmap8 = CustomCmap([1.00, 0.42, 0.04], [0.02, 0.75, 1.00])
                cmap = cmap8

                # axarray = np.array([])
                for iterno in range(3):
                    ax = fig.add_subplot(2, 3, iterno + 1, projection='3d')
                    min111 = min(data[iterno]['Y_pred_pq'])
                    max111 = max(data[iterno]['Y_pred_pq'])
                    Y_pred_pq = np.reshape(
                        data[iterno]['Y_pred_pq'],
                        [len(X_test1), len(X_test2)])
                    # im1 = ax.contour3D(X_test1, X_test2, Y_pred_pq, 100, cmap=cmap2, norm = colors.SymLogNorm(linthresh=1, linscale=1,vmin=miny_pq, vmax=maxy_pq),alpha=0.8)
                    im1 = ax.contour3D(X_test1,
                                       X_test2,
                                       Y_pred_pq,
                                       100,
                                       cmap=cmap,
                                       norm=colors.SymLogNorm(linthresh=0.2,
                                                              linscale=0.5,
                                                              vmin=miny_pq,
                                                              vmax=maxy_pq),
                                       alpha=0.8)

                    # im1 = ax.contour3D(X_test1, X_test2, Y_pred_pq, cmap=matplotlib.cm.seismic,vmin=miny_pq, vmax=maxy_pq)
                    # axarray = np.append(axarray,ax)
                    if (iterno == 0):
                        ypredmaster = Y_pred_pq
                    if (iterno == 2):
                        mmm = plt.cm.ScalarMappable(cmap=cmap)
                        mmm.set_array(ypredmaster)
                        mmm.set_clim(miny_pq, maxy_pq)
                        b1 = fig.colorbar(mmm)
                        b1.set_label("$\\frac{p(x_1,x_2)}{q(x_1,x_2)}$",
                                     fontsize=16)

                    # ax.set_title('Iteration: %d'%(iterno+1), fontsize = 12)
                    ax.set_xlabel('$x_1$', fontsize=14)
                    ax.set_ylabel('$x_2$', fontsize=14)
                    # b1 = fig.colorbar(im1,ax=ax, shrink=0.95,extend='both')
                    # if(iterno ==2):
                    #     b1.set_label("$\\frac{p_m(x_1,x_2)}{q_n(x_1,x_2)}$", fontsize = 14)
                    ax.set(xlim=x1lim, ylim=x2lim)
                    ax.view_init(60, 35)
                    if (iterno != 2):
                        ax.scatter(data[iterno]['robarg'][1],
                                   data[iterno]['robarg'][0],
                                   marker='*',
                                   c="black",
                                   s=333,
                                   alpha=1,
                                   zorder=1)

                for iterno in range(3):
                    ax = fig.add_subplot(2, 3, iterno + 4, projection='3d')
                    min111 = min(data[iterno]['Y_pred_pq'])
                    max111 = max(data[iterno]['Y_pred_pq'])
                    Y_pred_q = np.reshape(
                        data[iterno]['Y_pred_q'],
                        [len(X_test1), len(X_test2)])
                    im2 = ax.contour3D(X_test1,
                                       X_test2,
                                       Y_pred_q,
                                       cmap=cmap,
                                       norm=colors.SymLogNorm(linthresh=4,
                                                              linscale=0.6,
                                                              vmin=miny_q,
                                                              vmax=maxy_q))
                    # im2 = ax.contour3D(X_test1, X_test2, Y_pred_q, cmap=cmap2,vmin=miny_q, vmax=maxy_q)
                    # ax.set_title('Iteration: %d'%(iterno+1), fontsize = 12)
                    ax.set_xlabel('$x_1$', fontsize=14)
                    ax.set_ylabel('$x_2$', fontsize=14)
                    # b2 = fig.colorbar(im2,ax=ax, shrink=0.95,extend='both')
                    if (iterno == 0):
                        ypredmaster = Y_pred_q
                    if (iterno == 2):
                        mmm = plt.cm.ScalarMappable(cmap=cmap)
                        mmm.set_array(ypredmaster)
                        mmm.set_clim(miny_q, maxy_q)
                        b2 = fig.colorbar(mmm)
                        b2.set_label("$q(x_1,x_2)$", fontsize=12)

                    ax.view_init(60, 35)
                    if (iterno != 2):
                        ax.scatter(data[iterno]['robarg'][1],
                                   data[iterno]['robarg'][0],
                                   marker='*',
                                   c="black",
                                   s=333,
                                   alpha=1,
                                   zorder=1)
                # b2=fig.colorbar(im2,ax=ax, shrink=0.95,extend='both')
                # fig.suptitle("%s%s. m = %d, n = %d. trainingsize = %s "%(fname, noisestr,m,n,ts), fontsize = 18)
                # plt.show()

                # exit(1)
                #
                # cmap2='hot'
                # import matplotlib
                # import matplotlib.colors as colors
                # cmap1 = matplotlib.cm.RdYlBu
                # import matplotlib as mpl
                # import matplotlib.pyplot as plt
                # import matplotlib.text as text
                # fig, axarr = plt.subplots(2,3, figsize=(17,8))
                # x1lim = (box[0][0]-0.5,box[0][1]+0.5)
                # x2lim = (box[1][0]-0.5,box[1][1]+0.5)
                # # if(abs(miny_pq) <  abs(maxy_pq)):
                # #     miny_pq = -abs(maxy_pq)
                # #     maxy_pq = abs(maxy_pq)
                # # elif(abs(miny_pq) >  abs(maxy_pq)):
                # #     miny_pq = -abs(miny_pq)
                # #     maxy_pq = abs(miny_pq)
                # # xx,yy = np.meshgrid(X_test1,X_test2)
                # for iterno in range(3):
                #     min111 = min(data[iterno]['Y_pred_pq'])
                #     max111 = max(data[iterno]['Y_pred_pq'])
                #     # print(data[iterno]['Y_pred'])
                #     Y_pred_pq = np.reshape(data[iterno]['Y_pred_pq'], [len(X_test1), len(X_test2)])
                #     # print(Y_pred_pq)
                #     # im1 = axarr[0][iterno].pcolormesh(X_test1, X_test2, Y_pred_pq, cmap=cmap, vmin=min111, vmax=max111)
                #     # im1 = axarr[0][iterno].pcolormesh(X_test1,X_test2, Y_pred_pq, cmap=cmap)
                #     # im1 = axarr[0][iterno].pcolormesh(X_test1, X_test2, Y_pred_pq)
                #     # im1 = axarr[0][iterno].pcolormesh(X_test1, X_test2, Y_pred_pq, cmap=cmap1, vmin=miny_pq, vmax=maxy_pq)
                #     im1 = axarr[0][iterno].pcolormesh(X_test1, X_test2, Y_pred_pq, cmap=cmap1, norm = colors.SymLogNorm(linthresh=0.1, linscale=1,vmin=miny_pq, vmax=maxy_pq))
                #     # im1 = axarr[0][iterno].imshow(Y_pred_pq, extent=[-1, 1, -1, 1], cmap=cmap1)
                #     # axarr[0][iterno].axis(aspect='image')
                #     axarr[0][iterno].set_xlabel('$x_1$', fontsize = 14)
                #     axarr[0][iterno].set_ylabel('$x_2$', fontsize = 14)
                #     axarr[0][iterno].set(xlim=x1lim,ylim=x2lim)
                #     if(iterno !=2):
                #         axarr[0][iterno].scatter(data[iterno]['robarg'][1], data[iterno]['robarg'][0], marker = 'x', c = "black"  ,s=50, alpha = 1)
                #     # if(iterno ==0):
                #     # fig.colorbar(im1)
                #     # axarr[0][iterno].axis('tight')
                #
                #     Y_pred_q = np.reshape(data[iterno]['Y_pred_q'], [len(X_test1), len(X_test2)])
                #     # im2 = axarr[1][iterno].contour(X_test1, X_test2, Y_pred_q, cmap=cmap2, vmin=miny_q, vmax=maxy_q)
                #     im2 = axarr[1][iterno].pcolormesh(X_test1, X_test2, Y_pred_q, cmap=cmap1, norm = colors.SymLogNorm(linthresh=4, linscale=0.6,vmin=miny_q, vmax=maxy_q))
                #     axarr[1][iterno].set_xlabel('$x_1$', fontsize = 14)
                #     axarr[1][iterno].set_ylabel('$x_2$', fontsize = 14)
                #     axarr[1][iterno].set(xlim=x1lim,ylim=x2lim)
                #
                #     if(iterno !=2):
                #         axarr[1][iterno].scatter(data[iterno]['robarg'][1], data[iterno]['robarg'][0], marker = 'x', c = "black"  ,s=50, alpha = 1)
                # for ax in axarr.flat:
                #     ax.label_outer()
                # b1=fig.colorbar(im1,ax=axarr[0].ravel().tolist(), shrink=0.95,extend='both')
                # b1.set_label("$\\frac{p_m(x_1,x_2)}{q_n(x_1,x_2)}$", fontsize = 16)
                # b2=fig.colorbar(im2,ax=axarr[1].ravel().tolist(), shrink=0.95,extend='both')
                # b2.set_label("$q_n(x_1,x_2)$", fontsize = 16)
                #
                # fig.suptitle("%s%s. m = %d, n = %d. trainingsize = %s "%(fname, noisestr,m,n,ts), fontsize = 18)

                if not os.path.exists(folder + "/plots"):
                    os.mkdir(folder + '/plots')

                outfile = "%s/plots/Pimap_%s%s_p%d_q%d_ts%s.pdf" % (
                    folder, fname, noisestr, m, n, ts)
                plt.savefig(outfile)
                plt.clf()
                openfileStr += "open " + outfile + "; "
                print(openfileStr)
        plt.close('all')
    print(openfileStr)
Exemple #3
0
def tablesinc(m, n, ts, table_or_latex):
    from apprentice import monomial
    print(apprentice.monomialStructure(3, 3))
    fname = "f20"

    larr = [10**-6, 10**-3]
    uarr = [2 * np.pi, 4 * np.pi]
    lbdesc = {0: "-6", 1: "-3"}
    ubdesc = {0: "2pi", 1: "4pi"}
    lblatex = {0: "$10^{-6}$", 1: "$10^{-3}$"}
    ublatex = {0: "$2\\pi$", 1: "$4\\pi$"}

    noisestr = ""

    folder = "%s%s_%s/sincrun" % (fname, noisestr, ts)
    if not os.path.exists(folder):
        print("folder %s not found")

    if not os.path.exists(folder + "/benchmarkdata"):
        os.mkdir(folder + '/benchmarkdata')

    data = {}
    for dim in range(2, 8):
        data[dim] = {}
        for numlb, lb in enumerate(larr):
            for numub, ub in enumerate(uarr):
                key = lbdesc[numlb] + ubdesc[numub]
                data[dim][key] = {}
                fndesc = "%s%s_%s_p%d_q%d_ts%s_d%d_lb%s_ub%s" % (
                    fname, noisestr, ts, m, n, ts, dim, lbdesc[numlb],
                    ubdesc[numub])
                file = folder + "/" + fndesc + '/out/' + fndesc + "_p" + str(
                    m) + "_q" + str(n) + "_ts" + ts + ".json"
                if not os.path.exists(file):
                    print("%s not found" % (file))
                    exit(1)

                if file:
                    with open(file, 'r') as fn:
                        datastore = json.load(fn)

                data[dim][key]['l2error'] = 0

                testfile = "%s/benchmarkdata/%s%s_d%d_lb%s_ub%s_test.csv" % (
                    folder, fname, noisestr, dim, lbdesc[numlb], ubdesc[numub])
                if not os.path.exists(testfile):
                    print("%s not found" % (testfile))
                    exit(1)
                bottom_or_all = all
                try:
                    X, Y = readData(testfile)
                except:
                    DATA = tools.readH5(testfile, [0])
                    X, Y = DATA[0]

                if (bottom_or_all == "bottom"):
                    testset = [i for i in range(trainingsize, len(X_test))]
                    X_test = X[testset]
                    Y_test = Y[testset]
                else:
                    X_test = X
                    Y_test = Y

                rappsip = RationalApproximationSIP(datastore)
                Y_pred_rappsip = rappsip.predictOverArray(X_test)
                Y_diff = (Y_pred_rappsip - Y_test)**2
                print(dim, key)
                print(np.c_[Y_test[1:10], Y_pred_rappsip[1:10], Y_diff[1:10]])
                l2allrappsip = np.sum((Y_pred_rappsip - Y_test)**2)
                l2allrappsip = np.sqrt(l2allrappsip)
                data[dim][key]['l2error'] = l2allrappsip

                rappsiptime = datastore['log']['fittime']
                rdof = int(datastore['M'] + datastore['N'])
                rnoiters = len(datastore['iterationinfo'])
                rpnnl = datastore['M'] - (dim + 1)
                rqnnl = datastore['N'] - (dim + 1)
                data[dim][key]['rappsiptime'] = rappsiptime
                data[dim][key]['rdof'] = rdof
                data[dim][key]['rnoiters'] = rnoiters
                data[dim][key]['rpnnl'] = rappsiptime
                data[dim][key]['rqnnl'] = rqnnl

    # print(data)
    s = ""
    if (table_or_latex == "table"):
        print("TBD")
    elif (table_or_latex == "latex"):
        for dim in range(2, 8):
            for numlb, lb in enumerate(larr):
                for numub, ub in enumerate(uarr):

                    key = lbdesc[numlb] + ubdesc[numub]
                    s += "%d&%d&%d&%s&%s&%.3f&%d&%.3f" % (
                        dim, data[dim][key]['rdof'], data[dim][key]['rqnnl'],
                        lblatex[numlb], ublatex[numub],
                        data[dim][key]['rappsiptime'],
                        data[dim][key]['rnoiters'], data[dim][key]['l2error'])
                    s += "\\\\\hline\n"
    # print(s)

    import matplotlib.pyplot as plt
    X = range(2, 8)
    rangearr = []
    labelarr = []
    for numub, ub in enumerate(uarr):
        for numlb, lb in enumerate(larr):
            rangearr.append(lbdesc[numlb] + ubdesc[numub])
            labelarr.append(lblatex[numlb] + " - " + ublatex[numub])
    for r in rangearr:
        Y = []
        for x in X:
            Y.append(data[x][r]['l2error'])
        plt.plot(X, np.log10(Y), linewidth=1)
    plt.legend(labelarr, loc='upper right')
    # plt.show()
    plt.savefig("/Users/mkrishnamoorthy/Desktop/sincerror.pdf")
    plt.clf()

    # ##############################################

    import matplotlib.pyplot as plt
    X = range(2, 8)
    rangearr = []
    labelarr = []
    for numub, ub in enumerate(uarr):
        for numlb, lb in enumerate(larr):
            rangearr.append(lbdesc[numlb] + ubdesc[numub])
            labelarr.append(lblatex[numlb] + " - " + ublatex[numub])
    for r in rangearr:
        Y = []
        for x in X:
            Y.append(data[x][r]['rnoiters'])
        plt.plot(X, np.log10(Y), linewidth=1)
    plt.legend(labelarr, loc='upper left')
    # plt.show()
    plt.savefig("/Users/mkrishnamoorthy/Desktop/sinc.pdf")
    plt.clf()

    exit(1)
    # ##############################################
    dim = 3
    fndesc = "%s%s_%s_p%d_q%d_ts%s_d%d_lb%s_ub%s" % (
        fname, noisestr, ts, m, n, ts, dim, lbdesc[0], ubdesc[1])
    file = folder + "/" + fndesc + '/out/' + fndesc + "_p" + str(
        m) + "_q" + str(n) + "_ts" + ts + ".json"
    if not os.path.exists(file):
        print("%s not found" % (file))

    if file:
        with open(file, 'r') as fn:
            datastore = json.load(fn)

    iterinfo = datastore['iterationinfo']
    print("#################")
    for iter in iterinfo:
        print(iter['robOptInfo']['robustArg'])
    print("#################")

    rappsip = RationalApproximationSIP(datastore)

    X1vals = np.arange(lb, ub, 0.1)
    X2vals = np.arange(lb, ub, 0.1)
    X3vals = np.arange(lb, ub, 0.1)
    print(len(X1vals) * len(X2vals) * len(X3vals))

    Y_pred = []
    Y_orig = []
    for x1 in X1vals:
        for x2 in X2vals:
            for x3 in X3vals:
                Y_pred.append(rappsip([x1, x2, x3]))
                Y_orig.append(sinc([x1, x2, x3], 3))
    l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2)
    l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals))

    print("\nUnscaled\n")
    print(datastore['scaler'])
    print("#################")
    for iter in iterinfo:
        x = rappsip._scaler.unscale(iter['robOptInfo']['robustArg'])
        print(x)

    print("#################")
    print("Min max  for n=3 after final iteration")
    print(min(Y_pred), max(Y_pred))
    print(min(Y_orig), max(Y_orig))
    print("#################")

    print("#################")
    print("\nMean error after the final approximation = %f\n" % (l22))
    print("#################")

    datastore['pcoeff'] = iterinfo[0]['pcoeff']
    datastore['qcoeff'] = iterinfo[0]['qcoeff']
    rappsip = RationalApproximationSIP(datastore)
    lb = larr[0]
    ub = uarr[1]

    Y_pred = []
    Y_orig = []
    for x1 in X1vals:
        for x2 in X2vals:
            for x3 in X3vals:
                Y_pred.append(rappsip([x1, x2, x3]))
                Y_orig.append(sinc([x1, x2, x3], 3))
    print("#################")
    print("Min max  for n=3 after first iteration")
    print(min(Y_pred), max(Y_pred))
    print(min(Y_orig), max(Y_orig))
    l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2)
    l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals))
    print("#################")
    print("\nMean error after the first approximation = %f\n" % (l22))
    print("#################")
    print("#################")
    print("#################")
    print("#################")
    # exit(1)
    # ##############################################
    # Plotting
    import matplotlib.pyplot as plt

    if file:
        with open(file, 'r') as fn:
            datastore = json.load(fn)

    iterinfo = datastore['iterationinfo']
    iterinfono = len(iterinfo)
    for iterno in range(iterinfono):
        if file:
            with open(file, 'r') as fn:
                datastore = json.load(fn)
        iterinfo = datastore['iterationinfo']
        datastore['pcoeff'] = iterinfo[iterno]['pcoeff']
        datastore['qcoeff'] = iterinfo[iterno]['qcoeff']
        rappsip = RationalApproximationSIP(datastore)
        fig = plt.figure(figsize=(15, 15))
        for num, s in enumerate(['x1=-1', 'x2=-1', 'x3-1']):
            other1 = []
            other2 = []
            Y_pred = []
            Y_orig = []
            q_pred = []

            for x2 in X1vals:
                for x3 in X1vals:
                    if (num == 0):
                        X111 = [lb, x2, x3]
                    if (num == 1):
                        X111 = [x2, lb, x3]
                    if (num == 2):
                        X111 = [x2, x3, lb]
                    other1.append(x2)
                    other2.append(x3)
                    Y_pred.append(rappsip(X111))
                    Y_orig.append(sinc(X111, 3))
                    X111 = rappsip._scaler.scale(np.array(X111))
                    q_pred.append(rappsip.denom(X111))

            # Y_pred = np.reshape(np.array(Y_pred), [len(other1), len(other2)])
            # Y_orig = np.reshape(np.array(Y_orig), [len(other1), len(other2)])
            # q_pred = np.reshape(np.array(q_pred), [len(other1), len(other2)])

            ax = fig.add_subplot(3, 3, 3 * num + 1, projection='3d')
            ax.plot3D(other1, other2, Y_orig, "b.", alpha=0.5)
            ax.set_xlabel("x2")
            ax.set_ylabel("x3")
            ax = fig.add_subplot(3, 3, 3 * num + 2, projection='3d')
            ax.plot3D(other1, other2, Y_pred, "r.", alpha=0.5)
            ax.set_xlabel("x2")
            ax.set_ylabel("x3")
            ax = fig.add_subplot(3, 3, 3 * num + 3, projection='3d')
            ax.plot3D(other1, other2, q_pred, "g.", alpha=0.5)
            ax.set_xlabel("x2")
            ax.set_ylabel("x3")
        plt.savefig("/Users/mkrishnamoorthy/Desktop/sinc/iter" + str(iterno) +
                    ".pdf")

        plt.clf()
    exit(1)
    # ##############################################
    dim = 4
    fndesc = "%s%s_%s_p%d_q%d_ts%s_d%d_lb%s_ub%s" % (
        fname, noisestr, ts, m, n, ts, dim, lbdesc[0], ubdesc[1])
    file = folder + "/" + fndesc + '/out/' + fndesc + "_p" + str(
        m) + "_q" + str(n) + "_ts" + ts + ".json"
    if not os.path.exists(file):
        print("%s not found" % (file))

    if file:
        with open(file, 'r') as fn:
            datastore = json.load(fn)

    iterinfo = datastore['iterationinfo']
    print("#################")
    for iter in iterinfo:
        print(iter['robOptInfo']['robustArg'])
    print("#################")

    rappsip = RationalApproximationSIP(datastore)

    X1vals = np.arange(lb, ub, 0.3)
    X2vals = np.arange(lb, ub, 0.3)
    X3vals = np.arange(lb, ub, 0.3)
    X4vals = np.arange(lb, ub, 0.3)
    for x1 in X1vals:
        for x2 in X2vals:
            for x3 in X3vals:
                for x4 in X4vals:
                    Y_pred.append(rappsip([x1, x2, x3, x4]))
                    Y_orig.append(sinc([x1, x2, x3, x4], 4))
    print("min max for n=4 after final iteration")
    print(min(Y_pred), max(Y_pred))
    print(min(Y_orig), max(Y_orig))
    l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2)
    l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals) * len(X4vals))

    print(len(X1vals) * len(X2vals) * len(X3vals) * len(X4vals))

    print("\nUnscaled\n")
    print(datastore['scaler'])
    print("#################")
    for iter in iterinfo:
        x = rappsip._scaler.unscale(iter['robOptInfo']['robustArg'])
        print(x)
    print("#################")

    print("#################")
    print("Min max  for n=4 after final iteration")
    print(min(Y_pred), max(Y_pred))
    print(min(Y_orig), max(Y_orig))
    print("#################")
    print("#################")
    print("Mean error after the final approximation = %f\n" % (l22))
    print("#################")

    datastore['pcoeff'] = iterinfo[0]['pcoeff']
    datastore['qcoeff'] = iterinfo[0]['qcoeff']
    rappsip = RationalApproximationSIP(datastore)
    lb = larr[0]
    ub = uarr[1]

    Y_pred = []
    Y_orig = []
    for x1 in X1vals:
        for x2 in X2vals:
            for x3 in X3vals:
                for x4 in X4vals:
                    Y_pred.append(rappsip([x1, x2, x3, x4]))
                    Y_orig.append(sinc([x1, x2, x3, x4], 4))
    print("#################")
    print("Min max  for n=4 after final iteration")
    print(min(Y_pred), max(Y_pred))
    print(min(Y_orig), max(Y_orig))
    print("#################")
    l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2)
    l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals) * len(X4vals))
    print("#################")
    print("\nMean error after the first approximation = %f\n" % (l22))