Example #1
0
def getresults(farr, noisearr, tarr, ts, allsamples):
    import apprentice
    m = 5
    n = 5
    thresholdvalarr = np.array([float(t) for t in tarr])
    thresholdvalarr = np.sort(thresholdvalarr)

    results = {}
    for fnum, fname in enumerate(farr):
        results[fname] = {}
        dim = getdim(fname)
        infile = [
            "results/plots/poledata_corner" + str(dim) + "D.csv",
            "results/plots/poledata_inside" + str(dim) + "D.csv"
        ]

        X_testfc = np.loadtxt(infile[0], delimiter=',')
        X_testin = np.loadtxt(infile[1], delimiter=',')

        # X_testall = np.vstack(X_test,X_testin)

        print(len(X_testfc), len(X_testin))
        minarr, maxarr = getbox(fname)

        s = apprentice.Scaler(np.array(X_testfc, dtype=np.float64),
                              a=minarr,
                              b=maxarr)
        X_testfc = s.scaledPoints

        s = apprentice.Scaler(np.array(X_testin, dtype=np.float64),
                              a=minarr,
                              b=maxarr)
        X_testin = s.scaledPoints

        Y_testfc = np.array(getData(X_testfc, fname, 0))
        maxY_testfc = max(1, abs(np.max(Y_testfc)))

        Y_testin = np.array(getData(X_testin, fname, 0))
        maxY_testin = max(1, abs(np.max(Y_testin)))
        print(fname, maxY_testfc, maxY_testin)

        results[fname]['npoints_face'] = len(Y_testfc)
        results[fname]['npoints_inside'] = len(Y_testin)

        for snum, sample in enumerate(allsamples):
            results[fname][sample] = {}
            for noise in noisearr:
                results[fname][sample][noise] = {}
                noisestr, noisepct = getnoiseinfo(noise)

                resdata = {}
                resdata['papp'] = {}
                resdata['rapp'] = {}
                resdata['rapprd'] = {}
                resdata['rappsip'] = {}

                resdata['papp']['l2all'] = []
                resdata['rapp']['l2all'] = []
                resdata['rapprd']['l2all'] = []
                resdata['rappsip']['l2all'] = []

                for tval in thresholdvalarr:
                    for method in ['papp', 'rapp', 'rapprd', 'rappsip']:
                        resdata[method][str(tval)] = {}
                        resdata[method][str(tval)]['no'] = []
                        resdata[method][str(tval)]['no_face'] = []
                        resdata[method][str(tval)]['no_inside'] = []
                        resdata[method][str(tval)]['l2count'] = []
                        resdata[method][str(tval)]['l2notcount'] = []

                for rnum, run in enumerate(
                    ["exp1", "exp2", "exp3", "exp4", "exp5"]):
                    fndesc = "%s%s_%s_%s" % (fname, noisestr, sample, ts)
                    folder = "results/%s/%s" % (run, fndesc)
                    # print(folder)
                    pq = "p%d_q%d" % (m, n)
                    # print(run, fname,noisestr,sample,m,n)

                    rappsipfile = "%s/outrasip/%s_%s_ts%s.json" % (
                        folder, fndesc, pq, ts)
                    rappfile = "%s/outra/%s_%s_ts%s.json" % (folder, fndesc,
                                                             pq, ts)
                    rapprdfile = "%s/outrard/%s_%s_ts%s.json" % (
                        folder, fndesc, pq, ts)
                    pappfile = "%s/outpa/%s_%s_ts%s.json" % (folder, fndesc,
                                                             pq, ts)

                    if not os.path.exists(rappsipfile):
                        print("rappsipfile %s not found" % (rappsipfile))
                        if (knowmissing(rappsipfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)

                    if not os.path.exists(rappfile):
                        print("rappfile %s not found" % (rappfile))
                        if (knowmissing(rappfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)

                    if not os.path.exists(rapprdfile):
                        print("rappfile %s not found" % (rapprdfile))
                        if (knowmissing(rapprdfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)

                    if not os.path.exists(pappfile):
                        print("rappfile %s not found" % (pappfile))
                        if (knowmissing(pappfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)
                    print(fndesc + " " + run + " Start")

                    papp = PolynomialApproximation(fname=pappfile)
                    try:
                        Y_pred_pappfc = np.array([papp(x) for x in X_testfc])
                        Y_pred_pappin = np.array([papp(x) for x in X_testin])
                    except:
                        Y_pred_pappfc = findpredval(X_testfc, papp)
                        Y_pred_pappin = findpredval(X_testin, papp)

                    rappsip = RationalApproximationSIP(rappsipfile)
                    try:
                        Y_pred_rappsipfc = rappsip.predictOverArray(X_testfc)
                        Y_pred_rappsipin = rappsip.predictOverArray(X_testin)
                    except:
                        Y_pred_rappsipfc = findpredval(X_testfc, rappsip)
                        Y_pred_rappsipin = findpredval(X_testin, rappsip)

                    rapp = RationalApproximationONB(fname=rappfile)
                    try:
                        Y_pred_rappfc = np.array([rapp(x) for x in X_testfc])
                        Y_pred_rappin = np.array([rapp(x) for x in X_testin])
                    except:
                        Y_pred_rappfc = findpredval(X_testfc, rapp)
                        Y_pred_rappin = findpredval(X_testin, rapp)

                    rapprd = RationalApproximationONB(fname=rapprdfile)
                    try:
                        Y_pred_rapprdfc = np.array(
                            [rapprd(x) for x in X_testfc])
                        Y_pred_rapprdin = np.array(
                            [rapprd(x) for x in X_testin])
                    except:
                        Y_pred_rapprdfc = findpredval(X_testfc, rapprd)
                        Y_pred_rapprdin = findpredval(X_testin, rapprd)

                    print(fndesc + " " + run + " Done ")
                    sys.stdout.flush()

                    Y_testall = np.concatenate((Y_testfc, Y_testin), axis=None)

                    Y_pred_pappall = np.concatenate(
                        (Y_pred_pappfc, Y_pred_pappin), axis=None)
                    Y_pred_rappsipall = np.concatenate(
                        (Y_pred_rappsipfc, Y_pred_rappsipin), axis=None)
                    Y_pred_rappall = np.concatenate(
                        (Y_pred_rappfc, Y_pred_rappin), axis=None)
                    Y_pred_rapprdall = np.concatenate(
                        (Y_pred_rapprdfc, Y_pred_rapprdin), axis=None)

                    l2allrapp = np.sum((Y_pred_rappall - Y_testall)**2)
                    l2allrapprd = np.sum((Y_pred_rapprdall - Y_testall)**2)
                    l2allrappsip = np.sum((Y_pred_rappsipall - Y_testall)**2)
                    l2allpapp = np.sum((Y_pred_pappall - Y_testall)**2)
                    # print(l2allrapp,l2allrapprd,l2allrappsip)

                    resdata['rapp']['l2all'].append(np.sqrt(l2allrapp))
                    resdata['rapprd']['l2all'].append(np.sqrt(l2allrapprd))
                    resdata['rappsip']['l2all'].append(np.sqrt(l2allrappsip))
                    resdata['papp']['l2all'].append(np.sqrt(l2allpapp))

                    for tnum, tval in enumerate(thresholdvalarr):
                        for method in ['papp', 'rapp', 'rapprd', 'rappsip']:
                            resdata[method][str(tval)]['no_face'].append(0)
                            resdata[method][str(tval)]['no_inside'].append(0)
                            resdata[method][str(tval)]['no'].append(0)
                            resdata[method][str(tval)]['l2count'].append(0.)
                            resdata[method][str(tval)]['l2notcount'].append(0.)

                    for num, yt in enumerate(Y_testfc):
                        for method, pred in zip(
                            ['papp', 'rapp', 'rapprd', 'rappsip'], [
                                Y_pred_pappfc, Y_pred_rappfc, Y_pred_rapprdfc,
                                Y_pred_rappsipfc
                            ]):
                            yp = pred[num]
                            for tnum, tval in enumerate(thresholdvalarr):
                                if (abs(yp) / maxY_testfc > tval):
                                    resdata[method][str(tval)]['no'][rnum] += 1
                                    resdata[method][str(
                                        tval)]['no_face'][rnum] += 1
                                    resdata[method][str(
                                        tval)]['l2count'][rnum] += (yp - yt)**2

                    for num, yt in enumerate(Y_testin):
                        for method, pred in zip(
                            ['papp', 'rapp', 'rapprd', 'rappsip'], [
                                Y_pred_pappin, Y_pred_rappin, Y_pred_rapprdin,
                                Y_pred_rappsipin
                            ]):
                            yp = pred[num]
                            for tnum, tval in enumerate(thresholdvalarr):
                                if (abs(yp) / maxY_testin > tval):
                                    resdata[method][str(tval)]['no'][rnum] += 1
                                    resdata[method][str(
                                        tval)]['no_inside'][rnum] += 1
                                    resdata[method][str(
                                        tval)]['l2count'][rnum] += (yp - yt)**2

                    for tnum, tval in enumerate(thresholdvalarr):
                        for method, l2all in zip(
                            ['papp', 'rapp', 'rapprd', 'rappsip'],
                            [l2allpapp, l2allrapp, l2allrapprd, l2allrappsip]):
                            l2count = resdata[method][str(
                                tval)]['l2count'][rnum]
                            resdata[method][str(
                                tval)]['l2notcount'][rnum] = np.sqrt(l2all -
                                                                     l2count)
                            resdata[method][str(
                                tval)]['l2count'][rnum] = np.sqrt(l2count)

                    if (sample == "sg"):
                        break
                missingmean = -1
                for method in ['papp', 'rapp', 'rapprd', 'rappsip']:
                    l2allarr = resdata[method]['l2all']
                    results[fname][sample][noise][method] = {}
                    if (len(l2allarr) != 0):
                        results[fname][sample][noise][method]['l2all'] = float(
                            getstats(l2allarr, 'amean'))
                        results[fname][sample][noise][method][
                            'l2allgm'] = float(getstats(l2allarr, 'gmean'))
                        results[fname][sample][noise][method][
                            'l2allmed'] = float(getstats(l2allarr, 'median'))
                        results[fname][sample][noise][method][
                            'l2allra'] = float(getstats(l2allarr, 'range'))
                        results[fname][sample][noise][method][
                            'l2allsd'] = float(np.std(l2allarr))
                    else:
                        results[fname][sample][noise][method][
                            'l2all'] = missingmean
                        results[fname][sample][noise][method][
                            'l2allgm'] = missingmean
                        results[fname][sample][noise][method][
                            'l2allmed'] = missingmean
                        results[fname][sample][noise][method][
                            'l2allra'] = missingmean
                        results[fname][sample][noise][method]['l2allsd'] = 0

                for tval in thresholdvalarr:
                    for method in ['papp', 'rapp', 'rapprd', 'rappsip']:
                        results[fname][sample][noise][method][str(tval)] = {}
                        for key in [
                                'l2notcount', 'l2count', 'no', 'no_face',
                                'no_inside'
                        ]:

                            arr = resdata[method][str(tval)][key]
                            if (len(arr) != 0):
                                results[fname][sample][noise][method][str(
                                    tval)][key] = float(getstats(arr, 'amean'))
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'gm'] = float(
                                        getstats(arr, 'gmean'))
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'med'] = float(
                                        getstats(arr, 'median'))
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'ra'] = float(
                                        getstats(arr, 'range'))
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'sd'] = float(np.std(arr))
                            else:
                                results[fname][sample][noise][method][str(
                                    tval)][key] = missingmean
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'gm'] = missingmean
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'med'] = missingmean
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'ra'] = missingmean
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'sd'] = 0

        print("done with fn: %s" % (fname))

    return results
Example #2
0
def plot2Dsurface(infile,testfile,folder, desc,bottom_or_all):

    import json
    if infile:
        with open(infile, 'r') as fn:
            datastore = json.load(fn)
    dim = datastore['dim']
    if(dim != 2):
        raise Exception("plot2Dsurface can only handle dim = 2")
    m = datastore['m']
    try:
        n = datastore['n']
    except:
        n=0
    try:
        ts = datastore['trainingscale']
    except:
        ts = ""
    trainingsize = datastore['trainingsize']


    X, Y = readData(testfile)
    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

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

    outfilepng = "%s/plots/P2d_%s_p%d_q%d_ts%s_2Dsurface.png"%(folder,desc,m,n,ts)

    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(12,12))
    ax = fig.add_subplot(2, 2, 1, projection='3d')

    ax.plot3D(X_test[:,0],X_test[:,1], Y[:],"r.")
    ax.set_xlabel('$x1$', fontsize = 12)
    ax.set_ylabel('$x2$', fontsize = 12)
    ax.set_zlabel('$y$', fontsize = 12)
    ax.set_title('Original data', fontsize = 13)
    nnzthreshold = 1e-6
    for i, p in enumerate(datastore['pcoeff']):
        if(abs(p)<nnzthreshold):
            datastore['pcoeff'][i] = 0.
    if('qcoeff' in datastore):
        for i, q in enumerate(datastore['qcoeff']):
            if(abs(q)<nnzthreshold):
                datastore['qcoeff'][i] = 0.

    try:
        rappsip = RationalApproximationSIP(datastore)
        Y_pred = rappsip.predictOverArray(X_test)
    except:
        if(n==0):
            papp = PolynomialApproximation(initDict=datastore)
            Y_pred = np.array([papp(x) for x in X_test])
        else:
            rapp = RationalApproximation(initDict=datastore)
            Y_pred = np.array([rapp(x) for x in X_test])

    ax = fig.add_subplot(2, 2, 2, projection='3d')
    ax.plot3D(X_test[:,0],X_test[:,1], Y_pred[:],"b.")
    ax.set_xlabel('$x1$', fontsize = 12)
    ax.set_ylabel('$x2$', fontsize = 12)
    ax.set_zlabel('$y$', fontsize = 12)
    ax.set_title('Predicted data', fontsize = 13)

    ax = fig.add_subplot(2, 2, 3, projection='3d')
    ax.plot3D(X_test[:,0],X_test[:,1], np.absolute(Y_pred-Y_test),"g.")
    ax.set_xlabel('$x1$', fontsize = 12)
    ax.set_ylabel('$x2$', fontsize = 12)
    ax.set_zlabel('$y$', fontsize = 12)
    ax.set_title('|Predicted - Original|', fontsize = 13)

    l1 = np.sum(np.absolute(Y_pred-Y_test))
    l2 = np.sqrt(np.sum((Y_pred-Y_test)**2))
    linf = np.max(np.absolute(Y_pred-Y_test))
    if(linf>10**3): print("FOUND===>%f"%(linf))
    try:
        nnz = tools.numNonZeroCoeff(rappsip,nnzthreshold)
    except:
        if(n==0):
            nnz = tools.numNonZeroCoeff(papp,nnzthreshold)
        else:
            nnz = tools.numNonZeroCoeff(rapp,nnzthreshold)

    # print(l2)
    # print(nnz)
    # print(l2/nnz)
    # print(np.log10(l2/nnz))
    fig.suptitle("%s. m = %d, n = %d, ts = %d (%s). l1 = %.4f, l2 = %.4f, linf = %.4f, nnz = %d, l2/nnz = %f"%(desc,m,n,trainingsize,ts,l1,l2,linf,nnz,l2/nnz))

    plt.savefig(outfilepng)
    plt.close('all')
Example #3
0
def plotmntesterrperfile(jsonfile,testfile, desc,folder):
	minp = np.inf
	minq = np.inf
	maxp = 0
	maxq = 0
	miny0 = 0

	minl1 = np.inf
	minl2 = np.inf
	minlinf = np.inf
	pp = 0
	qq =0

	outfile1 = folder+"/"+desc+".299445.png"
	# outfile2 = folder+"/"+fno+"_index.299445.png"
	X_test, Y_test = readData(testfile)
	import json
	if jsonfile:
		with open(jsonfile, 'r') as fn:
			datastore = json.load(fn)
	from apprentice import monomial
	import apprentice

	for key in sorted(datastore.keys()):
		pdeg = datastore[key]['m']
		qdeg = datastore[key]['n']
		if(pdeg<minp):
			minp=pdeg
		if(qdeg<minq):
			minq=qdeg
		if pdeg > maxp:
			maxp = pdeg
		if qdeg > maxq:
			maxq = qdeg
	# print(minp,maxp,minq,maxq)
	error = np.zeros(shape = (maxp-minp+1,maxq-minq+1))
	for key in sorted(datastore.keys()):
		pdeg = datastore[key]['m']
		qdeg = datastore[key]['n']
		Y_pred = np.array([],dtype=np.float64)
		if('scaler' in datastore[key]):
			rappsip = RationalApproximationSIP(datastore[key])
			Y_pred = rappsip.predictOverArray(X_test)
			# print(Y_pred)
		else:
			structp = apprentice.monomialStructure(datastore[key]['dim'], pdeg)
			structq = apprentice.monomialStructure(datastore[key]['dim'], qdeg)
			for x in X_test:
				nnn = np.array(monomial.recurrence(x, structp))
				p = np.array(datastore[key]['pcoeff']).dot(nnn)
				ddd = np.array(monomial.recurrence(x, structq))
				q = np.array(datastore[key]['qcoeff']).dot(ddd)
				Y_pred = np.append(Y_pred,(p/q))
		# print(np.c_[Y_pred,Y_test])

		l1 = np.sum(np.absolute(Y_pred-Y_test))
		# print(l1)
		l2 = np.sqrt(np.sum((Y_pred-Y_test)**2))
		linf = np.max(np.absolute(Y_pred-Y_test))
		x000 = np.zeros(datastore[key]['dim'])
		y000 = rappsip.predict(x000)
		print("p%d q%d %f"%(pdeg,qdeg,y000))
		error[pdeg-minp][qdeg-minq] = l2
		if(minl2>l2):
			minl2 = l2
			minl1 = l1
			minlinf = linf
			print(linf)
			pp = pdeg
			qq = qdeg
			miny0 = y000
			# print(miiny0)

	import matplotlib as mpl
	import matplotlib.pyplot as plt

	mpl.rc('text', usetex = True)
	mpl.rc('font', family = 'serif', size=12)
	mpl.style.use("ggplot")
	cmapname   = 'viridis'
	plt.clf()
	print(error)

	markersize = 1000
	vmin = -4
	vmax = 2
	X,Y = np.meshgrid(range(minq,maxq+1),range(minp,maxp+1))
	# plt.scatter(X,Y , marker = 's', s=markersize, c = np.ma.log10(error), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1)
	plt.scatter(X,Y , marker = 's', s=markersize, c = error, cmap = cmapname, alpha = 1)
	plt.xlabel("$n$")
	plt.ylabel("$m$")
	plt.xlim((minq-1,maxq+1))
	plt.ylim((minp-1,maxp+1))
	b=plt.colorbar()
	# b.set_label("$\log_{10}\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2$")
	b.set_label("$\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2$")
	plt.title("l1=%f, l2=%f, linf=%f y0=%f found at (%d,%d)"%(minl1,minl2,minlinf,pp,qq,miny0))
	plt.savefig(outfile1)
Example #4
0
def ploterrorbars(fff, baseline=13.5, usejson=0):
    import matplotlib as mpl
    import json
    import apprentice
    if not os.path.exists('results/plots/'):
        os.makedirs('results/plots/', exist_ok=True)

    # mpl.use('pgf')
    # pgf_with_custom_preamble = {
    #     "text.usetex": True,    # use inline math for ticks
    #     "pgf.rcfonts": False,   # don't setup fonts from rc parameters
    #     "pgf.preamble": [
    #         "\\usepackage{amsmath}",         # load additional packages
    #     ]
    # }
    # mpl.rcParams.update(pgf_with_custom_preamble)

    # fff = getfarr()
    # pqqq = ['p4_q3','p2_q3','p3_q3','p3_q7','p2_q7','p3_q6','p2_q3','p3_q3']
    width = 0.15
    # import matplotlib.pyplot as plt
    # fig, ax = plt.subplots(1,2,figsize=(15,10),sharey=True)
    data = {}
    noiselevels = ['0', '10-6', '10-2']
    # noiselevels = ['0']
    # allsamples = ['mc','lhs','so','sg']
    allsamples = ['lhs', 'splitlhs', 'sg']
    # allsamples = ['mc','lhs']
    # allsamples = ['sg']
    if (usejson == 0):
        for snum, sample in enumerate(allsamples):
            data[sample] = {}
            # first = sample
            for nnum, noise in enumerate(noiselevels):
                data[sample][noise] = {}

                second = noise

                noisestr, noisepct = getnoiseinfo(noise)

                for fnum, fname in enumerate(fff):
                    data[sample][noise][fname] = {}

                    # IF USING TESTFILE

                    # testfile = "../benchmarkdata/"+fname+"_test.txt"
                    # # testfile = "../benchmarkdata/"+fname+".txt"
                    # print(testfile)
                    # 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

                    # IF USING POLEDATA FILES
                    dim = getdim(fname)
                    infile = "results/plots/poledata_corner" + str(
                        dim) + "D.csv"
                    X_test_1 = np.loadtxt(infile, delimiter=',')
                    infile = "results/plots/poledata_inside" + str(
                        dim) + "D.csv"
                    X_test_2 = np.loadtxt(infile, delimiter=',')
                    X_test = np.vstack([X_test_1, X_test_2])
                    minarr, maxarr = getbox(fname)
                    s = apprentice.Scaler(np.array(X_test, dtype=np.float64),
                                          a=minarr,
                                          b=maxarr)
                    X_test = s.scaledPoints
                    # print(np.shape(X_test_1),np.shape(X_test_2),np.shape(X_test))
                    Y_test = np.array(getData(X_test, fname, 0))

                    # print(np.shape(np.array(Y_test)))
                    # exit(1)

                    ts = "2x"

                    datapa = []
                    datara = []
                    datarard = []
                    datarasip = []
                    for run in ["exp1", "exp2", "exp3", "exp4", "exp5"]:
                        # for run in ["./"]:
                        fndesc = "%s%s_%s_%s" % (fname, noisestr, sample, ts)
                        folder = "results/%s/%s" % (run, fndesc)
                        m = 5
                        n = 5
                        pq = "p%d_q%d" % (m, n)
                        print(run, fname, noisestr, sample, m, n)

                        rappsipfile = "%s/outrasip/%s_%s_ts%s.json" % (
                            folder, fndesc, pq, ts)
                        rappfile = "%s/outra/%s_%s_ts%s.json" % (
                            folder, fndesc, pq, ts)
                        rapprdfile = "%s/outrard/%s_%s_ts%s.json" % (
                            folder, fndesc, pq, ts)
                        pappfile = "%s/outpa/%s_%s_ts%s.json" % (
                            folder, fndesc, pq, ts)
                        if not os.path.exists(rappsipfile):
                            print("rappsipfile %s not found" % (rappsipfile))
                            if (knowmissing(rappsipfile)):
                                if (sample == "sg"):
                                    break
                                continue
                            exit(1)

                        if not os.path.exists(rappfile):
                            print("rappfile %s not found" % (rappfile))
                            if (knowmissing(rappfile)):
                                if (sample == "sg"):
                                    break
                                continue
                            exit(1)

                        if not os.path.exists(rapprdfile):
                            print("rappfile %s not found" % (rapprdfile))
                            if (knowmissing(rapprdfile)):
                                if (sample == "sg"):
                                    break
                                continue
                            exit(1)

                        if not os.path.exists(pappfile):
                            print("pappfile %s not found" % (pappfile))
                            if (knowmissing(pappfile)):
                                if (sample == "sg"):
                                    break
                                continue
                            exit(1)

                        rappsip = RationalApproximationSIP(rappsipfile)
                        Y_pred_rappsip = rappsip.predictOverArray(X_test)

                        rapp = RationalApproximationONB(fname=rappfile)
                        Y_pred_rapp = np.array([rapp(x) for x in X_test])

                        rapprd = RationalApproximationONB(fname=rapprdfile)
                        Y_pred_rapprd = np.array([rapprd(x) for x in X_test])

                        papp = PolynomialApproximation(fname=pappfile)
                        Y_pred_papp = np.array([papp(x) for x in X_test])

                        datapa.append(
                            np.log10(np.sqrt(np.sum(
                                (Y_pred_papp - Y_test)**2))))
                        datara.append(
                            np.log10(np.sqrt(np.sum(
                                (Y_pred_rapp - Y_test)**2))))
                        datarard.append(
                            np.log10(
                                np.sqrt(np.sum((Y_pred_rapprd - Y_test)**2))))
                        datarasip.append(
                            np.log10(
                                np.sqrt(np.sum((Y_pred_rappsip - Y_test)**2))))

                        if (sample == "sg"):
                            break

                    missingmean = -15
                    if (len(datapa) == 0):
                        data[sample][noise][fname]['pamean'] = missingmean
                        data[sample][noise][fname]['pasd'] = 0
                    else:
                        data[sample][noise][fname]['pamean'] = np.average(
                            datapa)
                        data[sample][noise][fname]['pasd'] = np.std(datapa)

                    if (len(datara) == 0):
                        data[sample][noise][fname]['ramean'] = missingmean
                        data[sample][noise][fname]['rasd'] = 0
                    else:
                        data[sample][noise][fname]['ramean'] = np.average(
                            datara)
                        data[sample][noise][fname]['rasd'] = np.std(datara)
                    if (len(datarard) == 0):
                        data[sample][noise][fname]['rardmean'] = missingmean
                        data[sample][noise][fname]['rardsd'] = 0
                    else:
                        data[sample][noise][fname]['rardmean'] = np.average(
                            datarard)
                        data[sample][noise][fname]['rardsd'] = np.std(datarard)
                    if (len(datarasip) == 0):
                        data[sample][noise][fname]['rasipmean'] = missingmean
                        data[sample][noise][fname]['rasipsd'] = 0
                    else:
                        data[sample][noise][fname]['rasipmean'] = np.average(
                            datarasip)
                        data[sample][noise][fname]['rasipsd'] = np.std(
                            datarasip)

                    if (sample == "sg"):
                        data[sample][noise][fname]['pasd'] = 0
                        data[sample][noise][fname]['rasd'] = 0
                        data[sample][noise][fname]['rardsd'] = 0
                        data[sample][noise][fname]['rasipsd'] = 0

        outfile111 = "results/plots/Jerrors_" + fff[0] + ".json"
        import json
        with open(outfile111, "w") as f:
            json.dump(data, f, indent=4, sort_keys=True)
        exit(0)
    # else:
    # import json
    # outfile111 = "results/plots/Jerrors.json"
    # if outfile111:
    #     with open(outfile111, 'r') as fn:
    #         data = json.load(fn)

    ecolor = 'black'
    # if(plottype == 'persample' or plottype == 'pernoiselevel'):
    # if(plottype == 'persample'):
    # minval = np.Infinity
    methodarr = ['ra', 'rard', 'rasip', 'pa']
    import matplotlib.pyplot as plt
    ffffff = plt.figure(0, figsize=(25, 20))
    # totalrow = 5
    # totalcol = 4
    totalrow = 2
    totalcol = 2
    baseline = baseline
    # color = ['#900C3F','#C70039','#FF5733','#FFC300']
    color = ['#FFC300', '#FF5733', '#900C3F']
    width = 0.2
    ecolor = 'black'
    plt.rc('ytick', labelsize=14)
    plt.rc('xtick', labelsize=14)
    props = dict(boxstyle='square', facecolor='wheat', alpha=0.5)
    X111 = np.arange(len(noiselevels) * len(methodarr))
    # color100 = ['#FFC300','#FF5733','#900C3F']
    # color1k = ['yellow','wheat','r']
    axarray = []

    for fnum, fname in enumerate(fff):
        import json
        outfile111 = "results/plots/Jerrors_" + fname + ".json"
        if outfile111:
            with open(outfile111, 'r') as fn:
                data = json.load(fn)
        plotd = {}
        for snum, sample in enumerate(allsamples):
            plotd[sample] = {}
            plotd[sample]['mean'] = []
            plotd[sample]['sd'] = []
            for nnum, noise in enumerate(noiselevels):
                for method in methodarr:
                    meankey = method + 'mean'
                    sdkey = method + 'sd'

                    plotd[sample]['mean'].append(
                        data[sample][noise][fname][meankey])
                    plotd[sample]['sd'].append(
                        data[sample][noise][fname][sdkey])
        if (len(axarray) > 0):
            ax = plt.subplot2grid((totalrow, totalcol),
                                  (int(fnum / totalcol), int(fnum % totalcol)),
                                  sharex=axarray[0],
                                  sharey=axarray[0])
            axarray.append(ax)
        else:
            ax = plt.subplot2grid((totalrow, totalcol),
                                  (int(fnum / totalcol), int(fnum % totalcol)))
            axarray.append(ax)
        ax.set_xlim(-.3, 11.7)
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        plt.axvspan(-.3, 3.7, alpha=0.5, color='pink')
        plt.axvspan(3.7, 7.7, alpha=0.5, color='lightgrey')
        plt.axvspan(7.7, 11.7, alpha=0.5, color='cyan')
        plt.title(fname)
        # plt.text(1,3.35, "$\\epsilon = 0$", fontsize=10)
        # plt.text(4,3.35, "$\\epsilon = 10^{-6}$", fontsize=10)
        # plt.text(7,3.35, "$\\epsilon = 10^{-2}$", fontsize=10)
        labels = [
            'Latin hypercube sampling', 'Split latin hypercube sampling',
            'Sparse grids'
        ]
        legendarr = [
            '$\\epsilon=0$', '$\\epsilon=10^{-6}$', '$\\epsilon=10^{-2}$'
        ]
        # plt.tight_layout()
        for snum, sample in enumerate(allsamples):
            if (sample == 'sg'):
                ax.bar(X111 + snum * width,
                       np.array(plotd[sample]['mean']) + baseline,
                       width,
                       color=color[snum],
                       label=labels[snum])
            else:
                ax.bar(X111 + snum * width,
                       np.array(plotd[sample]['mean']) + baseline,
                       width,
                       color=color[snum],
                       yerr=np.array(plotd[sample]['sd']),
                       align='center',
                       ecolor=ecolor,
                       capsize=3,
                       label=labels[snum])

        if (fnum == 0):
            l1 = ffffff.legend(loc='upper center', ncol=3, fontsize=20)
        l2 = ffffff.legend(legendarr,
                           loc='upper center',
                           ncol=4,
                           bbox_to_anchor=(0.435, 0.83),
                           fontsize=20,
                           borderaxespad=0.,
                           shadow=False)
        # ax.label_outer()
        # if(fnum==0):
        #     l222 = ffffff.legend(loc='upper center', ncol=4,bbox_to_anchor=(0.5, 0.92), fontsize = 20,borderaxespad=0.,shadow=False)

        ax.set_xticks(X111 + (len(allsamples) - 1) * width / 2)
        xlab = [
            'Algorithm \\ref{ALG:MVVandQR} w/o DR',
            'Algorithm \\ref{ALG:MVVandQR}', 'Algorithm \\ref{A:Polyak}',
            'Poly. Approx.', 'Algorithm \\ref{ALG:MVVandQR} w/o DR',
            'Algorithm \\ref{ALG:MVVandQR}', 'Algorithm \\ref{A:Polyak}',
            'Poly. Approx.', 'Algorithm \\ref{ALG:MVVandQR} w/o DR',
            'Algorithm \\ref{ALG:MVVandQR}', 'Algorithm \\ref{A:Polyak}',
            'Poly. Approx.'
        ]
        methodlabel = ['$r_1$', '$r_2$', '$r_3$', '$r_4$']
        xlab1 = np.concatenate((methodlabel, methodlabel, methodlabel),
                               axis=None)
        ax.set_xticklabels(xlab1, fontsize=22)
        # ax.set_xlabel("Approach",fontsize=22)
        ax.set_ylabel("$\\log_{10}\\left[\\Delta_r\\right]$", fontsize=22)
        # ax.label_outer()
    # ffffff.text(0.08, 0.5, "$\\log_{10}\\left[\\Delta_r\\right]$", fontsize=22,va='center', rotation='vertical')

    # plt.show()
    plt.gca().yaxis.set_major_formatter(
        mtick.FuncFormatter(lambda x, _: x - baseline))
    # plt.tight_layout()
    # plt.savefig("../../log/errors.png", bbox_extra_artists=(l1,l111,), bbox_inches='tight')
    ffffff.savefig('../../log/errors.png',
                   bbox_extra_artists=(
                       l1,
                       l2,
                   ),
                   bbox_inches='tight')
    # plt.savefig("../../log/errors.png")
    plt.clf()
    plt.close('all')

    exit(0)
    for snum, sample in enumerate(allsamples):
        import matplotlib.pyplot as plt
        plt.rc('ytick', labelsize=14)
        fig, axarr = plt.subplots(3, 1, sharey=True, figsize=(21, 20))
        for nnum, noise in enumerate(noiselevels):
            pa = []
            ra = []
            rard = []
            rasip = []
            paerror = []
            raerror = []
            rarderror = []
            rasiperror = []
            for fnum, fname in enumerate(fff):
                pa.append(data[sample][noise][fname]['pamean'])
                paerror.append(data[sample][noise][fname]['pasd'])

                ra.append(data[sample][noise][fname]['ramean'])
                raerror.append(data[sample][noise][fname]['rasd'])

                rard.append(data[sample][noise][fname]['rardmean'])
                rarderror.append(data[sample][noise][fname]['rardsd'])

                rasip.append(data[sample][noise][fname]['rasipmean'])
                rasiperror.append(data[sample][noise][fname]['rasipsd'])

            p1 = axarr[nnum].bar(X111,
                                 np.array(pa) + baseline,
                                 width,
                                 color=color[0],
                                 yerr=np.array(paerror),
                                 align='center',
                                 ecolor=ecolor,
                                 capsize=3)
            p2 = axarr[nnum].bar(X111 + width,
                                 np.array(ra) + baseline,
                                 width,
                                 color=color[1],
                                 yerr=np.array(raerror),
                                 align='center',
                                 ecolor=ecolor,
                                 capsize=3)
            p3 = axarr[nnum].bar(X111 + 2 * width,
                                 np.array(rard) + baseline,
                                 width,
                                 color=color[2],
                                 yerr=np.array(rarderror),
                                 align='center',
                                 ecolor=ecolor,
                                 capsize=3)
            p4 = axarr[nnum].bar(X111 + 3 * width,
                                 np.array(rasip) + baseline,
                                 width,
                                 color=color[3],
                                 yerr=np.array(rasiperror),
                                 align='center',
                                 alpha=0.5,
                                 ecolor=ecolor,
                                 capsize=3)
            axarr[nnum].legend(
                (p1[0], p2[0], p3[0], p4[0]),
                ('Polynomial Approx. ', 'Algorithm \\ref{ALG:MVVandQR}',
                 'Algorithm \\ref{ALG:MVVandQR} w/ DR',
                 'Algorithm \\ref{A:Polyak}'),
                loc='upper right',
                fontsize=15)

        for ax in axarr.flat:
            ax.set_xticks(X111 + 3 * width / 2)
            xlab = []
            for f in fff:
                print(f)
                # xlab.append("\\ref{fn:%s}"%(f))
                xlab.append("%s" % (f))
            ax.set_xticklabels(xlab, fontsize=14)
            ax.set_ylabel('$log_{10}(\\Delta_r)$', fontsize=17)
            ax.label_outer()

        plt.gca().yaxis.set_major_formatter(
            mtick.FuncFormatter(lambda x, _: x - baseline))
        plt.tight_layout()
        print(xlab)
        # plt.show()
        # plt.savefig("plots/Perrorbars.pgf", bbox_inches="tight")
        # outfile111 = "results/plots/Perrorbars_for_%s.pdf"%(sample)
        outfile111 = "results/plots/Perrorbars_for_%s.pgf" % (sample)
        plt.savefig(outfile111, bbox_inches="tight")
        plt.clf()
        plt.close('all')

        # elif(plottype == 'pernoiselevel'):

        # FOR FUTURE
        # approxqqq = ["Polynomial Approximation", 'RA (linear algebra) without degree reduction', 'RA (linear algebra) with degree reduction', 'Pole-free RA']
        # for nnum,noise in enumerate(noiselevels):
        #     import matplotlib.pyplot as plt
        #     plt.rc('ytick',labelsize=14)
        #     fig, axarr = plt.subplots(4, 1, sharey=True,figsize=(21,20))
        #     for anum,approx in enumerate(["pa","ra","rard","rasip"]):
        #         barobj = {}
        #         for snum,sample in enumerate(allsamples):
        #             mean = []
        #             sd = []
        #             for fnum,fname in enumerate(fff):
        #                 mean.append(data[sample][noise][fname][approx+"mean"])
        #                 sd.append(data[sample][noise][fname][approx+"sd"])
        #             barobj[snum] = axarr[anum].bar(X111+snum*width, np.array(mean)+baseline, width,color=color[snum], yerr=np.array(sd),align='center',  ecolor=ecolor, capsize=3)
        #
        #         axarr[anum].legend((barobj[0][0],barobj[1][0],barobj[2][0],barobj[3][0]),('Uniform Random','Latin Hypercube','Sobol Sequence', 'Sparse Grids'),loc = 'upper right',fontsize = 15)
        #         axarr[anum].set_title(" approx = "+approxqqq[anum],fontsize = 15)

        for nnum, noise in enumerate(noiselevels):
            import matplotlib.pyplot as plt
            plt.rc('ytick', labelsize=14)
            fig, axarr = plt.subplots(4, 1, sharey=True, figsize=(21, 20))
            for anum, approx in enumerate(["pa", "ra", "rard", "rasip"]):
                for snum, sample in enumerate(allsamples):
                    barobj = []
                    mean = []
                    sd = []
                    for fnum, fname in enumerate(fff):
                        mean.append(data[sample][noise][fname][approx +
                                                               "mean"])
                        sd.append(data[sample][noise][fname][approx + "sd"])
                    barobj.append(axarr[anum].bar(X111 + snum * width,
                                                  np.array(mean) + baseline,
                                                  width,
                                                  color=color[snum],
                                                  yerr=np.array(sd),
                                                  align='center',
                                                  ecolor=ecolor,
                                                  capsize=3))
                # axarr[anum].legend(barobj,('Polynomial Approx. ', 'Algorithm \\ref{ALG:MVVandQR}','Algorithm \\ref{A:Polyak}'),loc = 'upper right',fontsize = 15)
                axarr[anum].set_title(str(allsamples) + " approx = " + approx)
            for ax in axarr.flat:
                ax.set_xticks(X111 + (len(allsamples) - 1) * width / 2)
                xlab = []
                for f in fff:
                    print(f)
                    # xlab.append("\\ref{fn:%s}"%(f))
                    xlab.append("%s" % (f))
                ax.set_xticklabels(xlab, fontsize=14)
                ax.set_ylabel('$log_{10}(\\Delta_r)$', fontsize=17)
                ax.label_outer()

            plt.gca().yaxis.set_major_formatter(
                mtick.FuncFormatter(lambda x, _: x - baseline))
            plt.tight_layout()
            print(xlab)
            # plt.show()
            # plt.savefig("plots/Perrorbars.pgf", bbox_inches="tight")
            # outfile111 = "results/plots/Perrorbars_for_%s.pdf"%(noise)
            outfile111 = "results/plots/Perrorbars_for_%s.pgf" % (noise)
            plt.savefig(outfile111, bbox_inches="tight")
            plt.clf()
            plt.close('all')
Example #5
0
def getresults(farr, noisearr, tarr, ts, allsamples, usecornerpoints):
    import apprentice
    m = 5
    n = 5
    thresholdvalarr = np.array([float(t) for t in tarr])
    thresholdvalarr = np.sort(thresholdvalarr)

    results = {}
    for fnum, fname in enumerate(farr):
        results[fname] = {}
        dim = getdim(fname)
        if (usecornerpoints == 1):
            infile = "results/plots/poledata_corner" + str(dim) + "D.csv"
        else:
            infile = "results/plots/poledata_inside" + str(dim) + "D.csv"

        X_test = np.loadtxt(infile, delimiter=',')

        print(len(X_test))
        minarr, maxarr = getbox(fname)
        s = apprentice.Scaler(np.array(X_test, dtype=np.float64),
                              a=minarr,
                              b=maxarr)
        X_test = s.scaledPoints

        Y_test = np.array(getData(X_test, fname, 0))
        maxY_test = max(1, abs(np.max(Y_test)))
        print(fname, maxY_test)

        results[fname]['npoints'] = len(Y_test)

        for snum, sample in enumerate(allsamples):
            results[fname][sample] = {}
            for noise in noisearr:
                results[fname][sample][noise] = {}
                noisestr, noisepct = getnoiseinfo(noise)

                resdata = {}
                resdata['rapp'] = {}
                resdata['rapprd'] = {}
                resdata['rappsip'] = {}

                resdata['rapp']['l2all'] = []
                resdata['rapprd']['l2all'] = []
                resdata['rappsip']['l2all'] = []

                for tval in thresholdvalarr:
                    for method in ['rapp', 'rapprd', 'rappsip']:
                        resdata[method][str(tval)] = {}
                        resdata[method][str(tval)]['no'] = []
                        resdata[method][str(tval)]['l2count'] = []
                        resdata[method][str(tval)]['l2notcount'] = []

                for rnum, run in enumerate(
                    ["exp1", "exp2", "exp3", "exp4", "exp5"]):
                    fndesc = "%s%s_%s_%s" % (fname, noisestr, sample, ts)
                    folder = "results/%s/%s" % (run, fndesc)
                    # print(folder)
                    pq = "p%d_q%d" % (m, n)
                    # print(run, fname,noisestr,sample,m,n)

                    rappsipfile = "%s/outrasip/%s_%s_ts%s.json" % (
                        folder, fndesc, pq, ts)
                    rappfile = "%s/outra/%s_%s_ts%s.json" % (folder, fndesc,
                                                             pq, ts)
                    rapprdfile = "%s/outrard/%s_%s_ts%s.json" % (
                        folder, fndesc, pq, ts)

                    if not os.path.exists(rappsipfile):
                        print("rappsipfile %s not found" % (rappsipfile))
                        if (knowmissing(rappsipfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)

                    if not os.path.exists(rappfile):
                        print("rappfile %s not found" % (rappfile))
                        if (knowmissing(rappfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)

                    if not os.path.exists(rapprdfile):
                        print("rappfile %s not found" % (rapprdfile))
                        if (knowmissing(rapprdfile) == 1):
                            if (sample == "sg"):
                                break
                            continue
                        exit(1)
                    print(fndesc + " Start")

                    rappsip = RationalApproximationSIP(rappsipfile)
                    try:
                        Y_pred_rappsip = rappsip.predictOverArray(X_test)
                    except:
                        Y_pred_rappsip = findpredval(X_test, rappsip)

                    rapp = RationalApproximationONB(fname=rappfile)
                    try:
                        Y_pred_rapp = np.array([rapp(x) for x in X_test])
                    except:
                        Y_pred_rapp = findpredval(X_test, rapp)

                    rapprd = RationalApproximationONB(fname=rapprdfile)
                    try:
                        Y_pred_rapprd = np.array([rapprd(x) for x in X_test])
                    except:
                        Y_pred_rapprd = findpredval(X_test, rapprd)

                    print(fndesc + " Done")
                    sys.stdout.flush()

                    l2allrapp = np.sum((Y_pred_rapp - Y_test)**2)
                    l2allrapprd = np.sum((Y_pred_rapprd - Y_test)**2)
                    l2allrappsip = np.sum((Y_pred_rappsip - Y_test)**2)
                    # print(l2allrapp,l2allrapprd,l2allrappsip)

                    resdata['rapp']['l2all'].append(np.sqrt(l2allrapp))
                    resdata['rapprd']['l2all'].append(np.sqrt(l2allrapprd))
                    resdata['rappsip']['l2all'].append(np.sqrt(l2allrappsip))

                    for tnum, tval in enumerate(thresholdvalarr):
                        for method in ['rapp', 'rapprd', 'rappsip']:
                            resdata[method][str(tval)]['no'].append(0)
                            resdata[method][str(tval)]['l2count'].append(0.)
                            resdata[method][str(tval)]['l2notcount'].append(0.)

                    for num, yt in enumerate(Y_test):
                        for method, pred in zip(
                            ['rapp', 'rapprd', 'rappsip'],
                            [Y_pred_rapp, Y_pred_rapprd, Y_pred_rappsip]):
                            yp = pred[num]
                            for tnum, tval in enumerate(thresholdvalarr):
                                if (abs(yp) / maxY_test > tval):
                                    resdata[method][str(tval)]['no'][rnum] += 1
                                    resdata[method][str(
                                        tval)]['l2count'][rnum] += (yp - yt)**2

                    for tnum, tval in enumerate(thresholdvalarr):
                        for method, l2all in zip(
                            ['rapp', 'rapprd', 'rappsip'],
                            [l2allrapp, l2allrapprd, l2allrappsip]):
                            l2count = resdata[method][str(
                                tval)]['l2count'][rnum]
                            resdata[method][str(
                                tval)]['l2notcount'][rnum] = np.sqrt(l2all -
                                                                     l2count)
                            resdata[method][str(
                                tval)]['l2count'][rnum] = np.sqrt(l2count)

                    if (sample == "sg"):
                        break
                missingmean = -1
                for method in ['rapp', 'rapprd', 'rappsip']:
                    l2allarr = resdata[method]['l2all']
                    results[fname][sample][noise][method] = {}
                    if (len(l2allarr) != 0):
                        results[fname][sample][noise][method][
                            'l2all'] = np.average(l2allarr)
                        results[fname][sample][noise][method][
                            'l2allsd'] = np.std(l2allarr)
                    else:
                        results[fname][sample][noise][method][
                            'l2all'] = missingmean
                        results[fname][sample][noise][method]['l2allsd'] = 0

                for tval in thresholdvalarr:
                    for method in ['rapp', 'rapprd', 'rappsip']:
                        results[fname][sample][noise][method][str(tval)] = {}
                        for key in ['l2notcount', 'l2count', 'no']:

                            arr = resdata[method][str(tval)][key]
                            if (len(arr) != 0):
                                results[fname][sample][noise][method][str(
                                    tval)][key] = np.average(arr)
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'sd'] = np.std(arr)
                            else:
                                results[fname][sample][noise][method][str(
                                    tval)][key] = missingmean
                                results[fname][sample][noise][method][str(
                                    tval)][key + 'sd'] = 0

        print("done with fn: %s for usecornerpoints = %d" %
              (fname, usecornerpoints))

    return results
Example #6
0
def plotminimizeranderror(usejson=0):
    def getData(X_train, fn, noisepct):
        """
        TODO use eval or something to make this less noisy
        """
        from apprentice import testData
        if fn == "f17":
            Y_train = [testData.f17(x) for x in X_train]
        else:
            raise Exception("function {} not implemented, exiting".format(fn))

        return Y_train

    import json
    import apprentice
    from apprentice import RationalApproximationSIP
    folder = "results"
    samplearr = ['lhs', 'splitlhs', 'sg']
    noise = "0"
    fname = "f17"
    noisestr, noisepct = getnoiseinfo(noise)
    dim = 3
    infile = [
        "results/plots/poledata_corner" + str(dim) + "D.csv",
        "results/plots/poledata_inside" + str(dim) + "D.csv"
    ]

    if (usejson == 0):
        minarr = [80, 5, 90]
        maxarr = [100, 10, 93]
        X_testfc = np.loadtxt(infile[0], delimiter=',')
        X_testin = np.loadtxt(infile[1], delimiter=',')
        s = apprentice.Scaler(np.array(X_testfc, dtype=np.float64),
                              a=minarr,
                              b=maxarr)
        X_testfc = s.scaledPoints

        s = apprentice.Scaler(np.array(X_testin, dtype=np.float64),
                              a=minarr,
                              b=maxarr)
        X_testin = s.scaledPoints

        Y_testfc = np.array(getData(X_testfc, fname, 0))
        Y_testin = np.array(getData(X_testin, fname, 0))

        Y_testall = np.concatenate((Y_testfc, Y_testin), axis=None)

        maxiterexp = [0, 0, 0]
        for snum, sample in enumerate(samplearr):
            maxiter = 0
            for exp in ['exp1', 'exp2', 'exp3', 'exp4', 'exp5']:
                file = "%s/%s/%s%s_%s_2x/outrasip/%s%s_%s_2x_p5_q5_ts2x.json" % (
                    folder, exp, fname, noisestr, sample, fname, noisestr,
                    sample)
                if not os.path.exists(file):
                    print("rappsipfile %s not found" % (file))
                    exit(1)
                if file:
                    with open(file, 'r') as fn:
                        datastore = json.load(fn)
                if len(datastore['iterationinfo']) > maxiter:
                    maxiter = len(datastore['iterationinfo'])
                    maxiterexp[snum] = exp
                if (sample == 'sg'):
                    break

        data = {}
        for snum, sample in enumerate(samplearr):
            data[sample] = {}
            exp = maxiterexp[snum]

            file = "%s/%s/%s%s_%s_2x/outrasip/%s%s_%s_2x_p5_q5_ts2x.json" % (
                folder, exp, fname, noisestr, sample, fname, noisestr, sample)
            if not os.path.exists(file):
                print("rappsipfile %s not found" % (file))
                exit(1)
            if file:
                with open(file, 'r') as fn:
                    datastore = json.load(fn)

            data[sample]['x'] = [
                i for i in range(len(datastore['iterationinfo']))
            ]
            data[sample]['minimizer'] = []
            data[sample]['error'] = []
            print('starting error calc for %s' % (sample))
            for inum, iter in enumerate(datastore['iterationinfo']):
                print('staring iter %d' % (inum))
                data[sample]['minimizer'].append(
                    iter['robOptInfo']['info'][0]['robustObj'])

                pcoeff = iter['pcoeff']
                qcoeff = iter['qcoeff']

                if file:
                    with open(file, 'r') as fn:
                        tempds = json.load(fn)
                tempds['pcoeff'] = pcoeff
                tempds['qcoeff'] = qcoeff

                rappsip = RationalApproximationSIP(tempds)

                Y_pred_rappsipfc = rappsip.predictOverArray(X_testfc)
                Y_pred_rappsipin = rappsip.predictOverArray(X_testin)

                Y_pred_rappsipall = np.concatenate(
                    (Y_pred_rappsipfc, Y_pred_rappsipin), axis=None)
                l2allrappsip = np.sum((Y_pred_rappsipall - Y_testall)**2)
                data[sample]['error'].append(np.sqrt(l2allrappsip))
                print('ending iter %d' % (inum))
        print(data)
        import json
        with open('results/plots/Jminimizeranderror.json', "w") as f:
            json.dump(data, f, indent=4, sort_keys=True)

    elif (usejson == 1):
        outfilejson = "results/plots/Jminimizeranderror.json"

        import matplotlib as mpl
        import matplotlib.pyplot as plt
        import matplotlib.text as text
        mpl.rc('text', usetex=True)
        mpl.rc('font', family='serif', size=12)
        mpl.rc('font', weight='bold')
        mpl.rcParams['text.latex.preamble'] = [
            r'\usepackage{sfmath} \boldmath'
        ]
        # mpl.style.use("ggplot")

        f, axarr = plt.subplots(2,
                                1,
                                sharex=True,
                                sharey=False,
                                figsize=(15, 8))
        f.subplots_adjust(hspace=0)
        f.subplots_adjust(wspace=0)

        style = ['b--', 'r-.', 'g-']
        linewidth = [1, 1, 2]
        labelarr = ['$LHS$', '$\\mathrm{d-LHD}$', '$SG$']
        marker = ['x', '*', 'o']

        index = 0
        if outfilejson:
            with open(outfilejson, 'r') as fn:
                data = json.load(fn)
        for snum, sample in enumerate(samplearr):
            x = data[sample]['x']
            y = data[sample]['minimizer']
            x.insert(0, -1)
            y.insert(0, -2.5)
            x = np.array(x) + 1
            axarr[index].plot(x,
                              y,
                              style[snum],
                              label=labelarr[snum],
                              lineWidth=linewidth[snum],
                              markevery=(1, 1),
                              marker=marker[snum])
        axarr[index].axhline(0, linestyle=":", linewidth='1', color='k')
        axarr[index].legend(fontsize=18, frameon=False)
        axarr[index].set_ylabel('$min\\quad q(x)$', fontsize=24)
        axarr[index].tick_params(labelsize=20)

        index = 1
        if outfilejson:
            with open(outfilejson, 'r') as fn:
                data = json.load(fn)
        for snum, sample in enumerate(samplearr):
            x = data[sample]['x']
            y = data[sample]['error']
            x.insert(0, -1)
            y.insert(0, 10**2)
            x = np.array(x) + 1
            axarr[index].plot(x,
                              y,
                              style[snum],
                              label=labelarr[snum],
                              lineWidth=linewidth[snum],
                              markevery=(1, 1),
                              marker=marker[snum])
            if (sample == 'splitlhs'):
                min = np.min(y)
        axarr[index].axhline(min, linestyle=":", linewidth='1', color='k')
        axarr[index].set_yscale('log')
        axarr[index].legend(fontsize=18, frameon=False)
        axarr[index].set_xlabel('$\\mathrm{Iteration\\ number}$', fontsize=24)
        axarr[index].set_ylabel('$\\Delta_r$', fontsize=24)
        axarr[index].tick_params(labelsize=20)

        # plt.yscale("log")

        plt.savefig("../../log/minimizererror.pdf", bbox_inches='tight')
def plottopniterationinfo(folder,testfile, desc,topn, bottom_or_all):
    import glob
    import json
    import re
    filelist = np.array(glob.glob(folder+"/out/*.json"))
    filelist = np.sort(filelist)

    try:
        X, Y = readData(testfile)
    except:
        DATA = tools.readH5(testfile, [0])
        X, Y= DATA[0]

    fileArr = np.array([])
    iterationNoArr = np.array([])

    for file in filelist:
        if file:
            with open(file, 'r') as fn:
                datastore = json.load(fn)
        fileArr = np.append(fileArr,file)
        iterationNoArr = np.append(iterationNoArr,len(datastore['iterationinfo']))

    print(np.c_[fileArr,iterationNoArr])

    if(topn > len(fileArr)):
        raise Exception("n (%d) > available runs (%d)"%(topn,len(fileArr)))

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


    topnindex = iterationNoArr.argsort()[-topn:][::-1]

    for i in range(len(topnindex)):
        index = topnindex[i]
        outfile = "%s/plots/Ptii_%s_topiterinfo_%d.pdf"%(folder, desc, i+1)
        file = fileArr[index]
        if file:
            with open(file, 'r') as fn:
                datastore = json.load(fn)
        m = datastore['m']
        n = datastore['n']
        ts = datastore['trainingscale']
        trainingsize =datastore['trainingsize']
        totaltime = datastore['log']['fittime']

        if(bottom_or_all == "bottom"):
            trainingsize = datastore['trainingsize']
            testset = [i for i in range(trainingsize,len(X))]
            X_test = X[testset]
            Y_test = Y[testset]
        elif(bottom_or_all == "all"):
            X_test = X
            Y_test = Y
        else:
            raise Exception("bottom or all? Option ambiguous. Check spelling and/or usage")

        if(len(X_test)<=1): raise Exception("Not enough testing data")

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

        interationinfo = datastore['iterationinfo']
        for iter in 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"])

        Xvals = range(1,int(iterationNoArr[index])+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)

        nnzthreshold = 1e-6
        for i, p in enumerate(datastore['pcoeff']):
            if(abs(p)<nnzthreshold):
                datastore['pcoeff'][i] = 0.
        if('qcoeff' in datastore):
            for i, q in enumerate(datastore['qcoeff']):
                if(abs(q)<nnzthreshold):
                    datastore['qcoeff'][i] = 0.

        rappsip = RationalApproximationSIP(datastore)
        Y_pred = rappsip.predictOverArray(X_test)

        key = "p%d_q%d_ts%s"%(m,n,ts)
        l1 = np.sum(np.absolute(Y_pred-Y_test))
        l2 = np.sqrt(np.sum((Y_pred-Y_test)**2))
        linf = np.max(np.absolute(Y_pred-Y_test))
        nnz = tools.numNonZeroCoeff(rappsip,nnzthreshold)
        l2divnnz = l2/float(nnz)


        # 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)
        plt.savefig(outfile)
        plt.clf()
Example #8
0
def plotmntesterr(folder,testfile, desc,bottom_or_all, measure):
    import glob
    import json
    import re
    filelist = np.array(glob.glob(folder+"/out/*.json"))
    filelist = np.sort(filelist)

    try:
        X, Y = readData(testfile)
    except:
        DATA = tools.readH5(testfile, [0])
        X, Y= DATA[0]
    minp = np.inf
    minq = np.inf
    maxp = 0
    maxq = 0
    dim = 0

    stats = {}

    for file in filelist:
        if file:
			with open(file, 'r') as fn:
				datastore = json.load(fn)
        dim = datastore['dim']
        m = datastore['m']
        n = datastore['n']
        if(m<minp):
            minp=m
        if(n<minq):
            minq=n
        if m > maxp:
            maxp = m
        if n > maxq:
            maxq = n

    if(bottom_or_all == "bottom"):
        trainingsize = tools.numCoeffsPoly(dim,maxp) + tools.numCoeffsPoly(dim,maxq)
        testset = [i for i in range(trainingsize,len(X))]
        X_test = X[testset]
        Y_test = Y[testset]
    elif(bottom_or_all == "all"):
        X_test = X
        Y_test = Y
    else:
        raise Exception("bottom or all? Option ambiguous. Check spelling and/or usage")
    if(len(X_test)<=1): raise Exception("Not enough testing data")

    if not os.path.exists(folder+"/plots"):
        os.mkdir(folder+'/plots')
    outfilepng = "%s/plots/Pmn_%s_%s_from_plotmntesterr.png"%(folder, desc, measure)
    outfilestats = "%s/plots/J%s_stats_from_plotmntesterr.json"%(folder,desc)
    error = np.empty(shape = (maxp-minp+1,maxq-minq+1))
    for i in range(maxp-minp+1):
        for j in range(maxq-minq+1):
            error[i][j] = None

    nnzthreshold = 1e-6
    for file in filelist:
        if file:
			with open(file, 'r') as fn:
				datastore = json.load(fn)
        m = datastore['m']
        n = datastore['n']
        ts = datastore['trainingscale']

        for i, p in enumerate(datastore['pcoeff']):
            if(abs(p)<nnzthreshold):
                datastore['pcoeff'][i] = 0.
        if('qcoeff' in datastore):
            for i, q in enumerate(datastore['qcoeff']):
                if(abs(q)<nnzthreshold):
                    datastore['qcoeff'][i] = 0.

        rappsip = RationalApproximationSIP(datastore)
        Y_pred = rappsip.predictOverArray(X_test)

        key = "p%d_q%d_ts%s"%(m,n,ts)
        l1 = np.sum(np.absolute(Y_pred-Y_test))
        l2 = np.sqrt(np.sum((Y_pred-Y_test)**2))
        linf = np.max(np.absolute(Y_pred-Y_test))
        nnz = tools.numNonZeroCoeff(rappsip,nnzthreshold)
        l2divnnz = l2/float(nnz)
        stats[key] = {}
        stats[key]['nnz'] =nnz
        stats[key]['l2divnnz'] = l2divnnz
        stats[key]['l1'] =l1
        stats[key]['l2'] =l2
        stats[key]['linf'] =linf

        if(measure == 'l1'):
            error[m-minp][n-minq] = l1
        elif(measure == 'l2'):
            error[m-minp][n-minq] = l2
        elif(measure == 'linf'):
            error[m-minp][n-minq] = linf
        elif(measure == 'l2divnnz'):
            error[m-minp][n-minq] = l2divnnz
        else:
            raise Exception("measure not found. Check spelling and/or usage")



    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import re

    mpl.rc('text', usetex = True)
    mpl.rc('font', family = 'serif', size=12)
    mpl.style.use("ggplot")
    cmapname   = 'viridis'
    plt.clf()

    markersize = 200
    vmin = -4
    vmax = 2
    X,Y = np.meshgrid(range(minq,maxq+1),range(minp,maxp+1))
    # plt.scatter(X,Y , marker = 's', s=markersize, c = np.ma.log10(error), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1)
    plt.scatter(X,Y , marker = 's', s=markersize, c = error, cmap = cmapname, alpha = 1)
    plt.xlabel("$n$")
    plt.ylabel("$m$")
    plt.xlim((minq-1,maxq+1))
    plt.ylim((minp-1,maxp+1))
    b=plt.colorbar()
	# b.set_label("$\log_{10}\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2$")
	# b.set_label("$\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2$")
    if(measure == 'l1'):
        b.set_label("L1 error norm")
    elif(measure == 'l2'):
        b.set_label("L2 error norm")
    elif(measure == 'linf'):
        b.set_label("Linf error norm")
    elif(measure == 'l2divnnz'):
        b.set_label("L2/nnz error norm")



    keys = []
    l1arr = np.array([])
    l2arr = np.array([])
    linfarr = np.array([])
    nnzarr = np.array([])
    l2divnnzarr= np.array([])
    for key in stats:
        keys.append(key)
        l1arr = np.append(l1arr,stats[key]['l1'])
        l2arr = np.append(l2arr,stats[key]['l2'])
        linfarr = np.append(linfarr,stats[key]['linf'])
        nnzarr = np.append(nnzarr,stats[key]['nnz'])
        l2divnnzarr = np.append(l2divnnzarr,stats[key]['l2divnnz'])

    minstats = {}
    minstats["l1"] = {}
    minstats["l1"]["val"] = np.min(l1arr)
    minstats["l1"]["loc"] = keys[np.argmin(l1arr)]

    minstats["l2"] = {}
    minstats["l2"]["val"] = np.min(l2arr)
    minstats["l2"]["loc"] = keys[np.argmin(l2arr)]

    minstats["linf"] = {}
    minstats["linf"]["val"] = np.min(linfarr)
    minstats["linf"]["loc"] = keys[np.argmin(linfarr)]

    minstats["nnz"] = {}
    minstats["nnz"]["val"] = np.min(nnzarr)
    minstats["nnz"]["loc"] = keys[np.argmin(nnzarr)]

    minstats["l2divnnz"] = {}
    minstats["l2divnnz"]["val"] = np.min(l2divnnzarr)
    minstats["l2divnnz"]["loc"] = keys[np.argmin(l2divnnzarr)]

    if(measure == 'l1'):
        minkey = keys[np.argmin(l1arr)]
        minval = np.min(l1arr)
    elif(measure == 'l2'):
        minkey = keys[np.argmin(l2arr)]
        minval = np.min(l2arr)
    elif(measure == 'linf'):
        minkey = keys[np.argmin(linfarr)]
        minval = np.min(linfarr)
    elif(measure == 'l2divnnz'):
        minkey = keys[np.argmin(l2divnnzarr)]
        minval = np.min(l2divnnzarr)

    digits = [int(s) for s in re.findall(r'-?\d+\.?\d*', minkey)]
    winner = (digits[0], digits[1])

    stats["minstats"] = minstats

    plt.scatter(winner[1], winner[0], marker = '*', c = "magenta",s=markersize, alpha = 0.9)

    plt.title("%s. Winner is p%d q%d with val = %f"%(desc,winner[0], winner[1],minval))
    plt.savefig(outfilepng)

    import json
    with open(outfilestats, "w") as f:
        json.dump(stats, f,indent=4, sort_keys=True)
Example #9
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))