def runRappsipBaseStrategyFromPoints(X, Y,runs, box=np.array([[-1,1],[-1,1]]),trainingScale="1x", roboptstrategy="ms",outfile="out.json",debug=0,debugfile="/tmp/s0_latest.json"): outJSON = {} for r in runs: pdeg=r[0] qdeg=r[1] rappsip = RationalApproximationSIP( X, Y, m=pdeg, n=qdeg, fitstrategy = 'filter', localoptsolver = 'scipy', trainingscale=trainingScale, roboptstrategy=roboptstrategy, scalemin = box[:,0], scalemax = box[:,1], strategy=0 ) outJSON["p%s_q%s"%(str(pdeg),str(qdeg))] = rappsip.asDict if(debug == 1): import json with open("/tmp/s0_latest.json", "w") as f: json.dump(outJSON, f,indent=4, sort_keys=True) import json with open(outfile, "w") as f: json.dump(outJSON, f,indent=4, sort_keys=True)
def runRappsipStrategy2(infile,runs, larr,l1strat="ho_p_q",box=np.array([[-1,1],[-1,1]]),trainingScale="0.5x",outfile="out.json",debug=0): # l1strat="ho_p_q" # l1strat="all_p_q" X, Y = tools.readData(infile) outJSON = {} # runs = [[2,2],[3,?3],[4,4],[5,5],[6,6]] for r in runs: for l in larr: pdeg=r[0] qdeg=r[1] if(l1strat == "ho_p_q"): ppenaltybin = np.ones(pdeg+1) ppenaltybin[pdeg] = 0 qpenaltybin = np.ones(qdeg+1) qpenaltybin[qdeg] = 0 elif(l1strat == "all_p_q"): ppenaltybin = np.zeros(pdeg+1) qpenaltybin = np.zeros(qdeg+1) rappsip = RationalApproximationSIP( X, Y, m=pdeg, n=qdeg, trainingscale=trainingScale, roboptstrategy="baron", box=box, strategy=2, penaltyparam=l, ppenaltybin=ppenaltybin.tolist(), qpenaltybin=qpenaltybin.tolist() ) outJSON["p%s_q%s_%.E"%(str(pdeg),str(qdeg),l)] = rappsip.asDict if(debug == 1): import json with open("/tmp/s2_latest.json", "w") as f: json.dump(outJSON, f,indent=4, sort_keys=True) import json with open(outfile, "w") as f: json.dump(outJSON, f,indent=4, sort_keys=True)
def plotResidualMap(f_rapp, f_test, f_out,norm=1, fno=1, type=None): X_test, Y_test = app.readData(f_test) error = "" if(type == None): R = app.readApprentice(f_rapp) if norm == 1: error = [abs(R(x)-Y_test[num]) for num, x in enumerate(X_test)] if norm == 2: error = [(R(x)-Y_test[num])**2 for num, x in enumerate(X_test)] elif(type == "rappsip"): R = RationalApproximationSIP(f_rapp) Y_pred = R(X_test) error = (abs(Y_pred-Y_test)) if norm == 1: error = np.array(abs(Y_pred-Y_test),dtype=np.float64) if norm == 2: error = np.array((Y_pred-Y_test)**2,) 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() plt.scatter(X_test[:,0], X_test[:,1], marker = '.', c = np.ma.log10(error), cmap = cmapname, alpha = 0.8) plt.vlines(-1, ymin=-1, ymax=1, linestyle="dashed") plt.vlines( 1, ymin=-1, ymax=1, linestyle="dashed") plt.hlines(-1, xmin=-1, xmax=1, linestyle="dashed") plt.hlines( 1, xmin=-1, xmax=1, linestyle="dashed") plt.xlabel("$x$") plt.ylabel("$y$") plt.ylim((-1.5,1.5)) plt.xlim((-1.5,1.5)) b=plt.colorbar() b.set_label("$\log_{10}\left|f - \\frac{p^{(%i)}}{q^{(%i)}}\\right|_%i$"%(R.m, R.n, norm)) plt.title(getFunctionLatex(fno)) plt.savefig(f_out)
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')
def runCrossValidation(infile,box=np.array([[-1,1],[-1,1]]),outfile="out.json",debug=0): trainingScale = "Cp" X, Y = tools.readData(infile) # Some param overrides for debug larr = np.array([10**i for i in range(3,-13,-1)]) # larr = np.array([10**i for i in range(0,-5,-1)]) k=10 # k=2 outJSON = {} for pdeg in range(2,5): # for pdeg in range(3,5): ppenaltybin = np.zeros(pdeg+1) for qdeg in range(2,5): # for qdeg in range(3,5): qpenaltybin = np.zeros(qdeg+1) avgerror = np.zeros(len(larr)) avgerror_k = np.zeros(len(larr)) for index in range(len(larr)): l = larr[index] kfold = KFold(k) error_l = 0 for train, test in kfold.split(X): rappsip = RationalApproximationSIP( X[train], Y[train], m=pdeg, n=qdeg, trainingscale=trainingScale, box=box, strategy=2, penaltyparam=l, ppenaltybin=ppenaltybin.tolist(), qpenaltybin=qpenaltybin.tolist() ) error_l_k = np.sum([(rappsip(X[test])-Y[test])**2]) error_l += error_l_k avgerror[index] = error_l / len(X) avgerror_k[index] = error_l / len(X[test]) stderror = np.std(avgerror_k)/np.sqrt(k) minIndex = np.argmin(avgerror) minv = avgerror[minIndex] minl = larr[minIndex] currIndex = minIndex while currIndex >= 0: if(minv + stderror == avgerror[currIndex]): break elif(minv + stderror > avgerror[currIndex]): currIndex -= 1 else: currIndex += 1 break if(currIndex == -1): currIndex = 0 currl = larr[currIndex] # print(minIndex) # print(currIndex) # # print(avgerror) # print(avgerror_k) # print(stderror) # # print(minl) # print(currl) rappsip_min = RationalApproximationSIP( X, Y, m=pdeg, n=qdeg, trainingscale=trainingScale, box=box, strategy=2, penaltyparam=minl, ppenaltybin=ppenaltybin.tolist(), qpenaltybin=qpenaltybin.tolist() ) rappsip_minpse = rappsip_min if(currl != minl): rappsip_minpse = RationalApproximationSIP( X, Y, m=pdeg, n=qdeg, trainingscale=trainingScale, box=box, strategy=2, penaltyparam=currl, ppenaltybin=ppenaltybin.tolist(), qpenaltybin=qpenaltybin.tolist() ) rappsip = {"min":rappsip_min.asDict, "min plus SE":rappsip_minpse.asDict, "avgerror":avgerror.tolist(), "avgerror_k":avgerror_k.tolist(), "stderror":stderror,"minIndex":minIndex,"minl":minl, "minv":minv, "mpseIndex":currIndex, "mpsel":currl} outJSON["p%s_q%s"%(str(pdeg),str(qdeg))] = rappsip if(debug == 1): import json with open("/tmp/cv_latest.json", "w") as f: json.dump(outJSON, f,indent=4, sort_keys=True) # exit(1) import json with open(outfile, "w") as f: json.dump(outJSON, f,indent=4, sort_keys=True)
def tableS2(jsonfile, testfile, runs, larr): import json # Lcurve if jsonfile: with open(jsonfile, 'r') as fn: datastore = json.load(fn) X_test, Y_test = readData(testfile) # 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' # # f, axarr = plt.subplots(4,4, figsize=(15,15)) # markersize = 1000 # vmin = -4 # vmax = 2.5 mintesterrArr = np.array([]) minaic = np.array([]) minbic = np.array([]) minparam = np.array([]) minnnz = np.array([]) minmn = np.array([]) for r in runs: pdeg=r[0] qdeg=r[1] Y_l1 = np.array([]) X_l2 = np.array([]) Z_testerr = np.array([]) karr = np.array([]) aic = np.array([]) bic = np.array([]) mn = np.array([]) param = np.array([]) for l in larr: key = "p%s_q%s_%.E"%(str(pdeg),str(qdeg),l) iterationInfo = datastore[key]["iterationinfo"] lastii = iterationInfo[len(iterationInfo)-1] regerr = lastii["leastSqSplit"]["l1term"] trainerr = lastii["leastSqSplit"]["l2term"] X_l2 = np.append(X_l2,trainerr) Y_l1 = np.append(Y_l1,regerr) rappsip = RationalApproximationSIP(datastore[key]) Y_pred = rappsip(X_test) testerror = np.sum((Y_pred-Y_test)**2) Z_testerr = np.append(Z_testerr,testerror) k = 2 pcoeff = datastore[key]["pcoeff"] qcoeff = datastore[key]["qcoeff"] maxp = abs(max(pcoeff, key=abs)) maxq = abs(max(qcoeff, key=abs)) # print(np.c_[pcoeff]) # print(np.c_[qcoeff]) # print(maxp,maxq) for pc in pcoeff: if(pc > 10**-2*maxp): k += 1 for qc in qcoeff: if(qc > 10**-2*maxq): k += 1 karr = np.append(karr,k) n = len(X_test) # AIC = 2k - 2log(L) # BIC = klog(n) - 2log(L) # -2log(L) becomes nlog(variance) = nlog(SSE/n) = nlog(testerror/n) a = 2*k + n*np.log(testerror/n) b = k*np.log(n) + n*np.log(testerror/n) aic = np.append(aic,a) bic = np.append(bic,b) param = np.append(param,l) mn = np.append(mn,rappsip.M+rappsip.N) print("p = "+str(pdeg)+"; q = "+str(qdeg)) print("#\tl2 error\tl1 error\ttest err\tnnz\taic\t\tbic") for i in range(len(larr)): print("%d\t%f\t%f\t%f\t%d\t%f\t%f"%(i+1,X_l2[i],Y_l1[i],Z_testerr[i],karr[i], aic[i],bic[i])) print("\nMIN\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n"%(np.argmin(X_l2)+1,np.argmin(Y_l1)+1,np.argmin(Z_testerr)+1,np.argmin(karr)+1,np.argmin(aic)+1,np.argmin(bic)+1)) # axarr[pdeg-2][qdeg-2].plot(X_l2, Y_l1, '-rD') # axarr[pdeg-2][qdeg-2].set_title("p = "+str(pdeg)+"; q = "+str(qdeg)) # if min arg of aic, bic and test err match, then take that and put int min arrays minindexarr = [np.argmin(Z_testerr),np.argmin(aic),np.argmin(bic)] if all(x == minindexarr[0] for x in minindexarr): mintesterrArr = np.append(mintesterrArr,np.min(Z_testerr)) minaic = np.append(minaic,np.min(aic)) minbic = np.append(minbic,np.min(bic)) minparam = np.append(minparam,param[minindexarr[0]]) minnnz = np.append(minnnz,karr[minindexarr[0]]) minmn = np.append(minmn,mn[minindexarr[0]]) # 2 elements match elif len(set(arr)) == 2: # find the 2 mathcing elements and take values from all arrays at that index if minindexarr[0]==minindexarr[1] or minindexarr[0]==minindexarr[2]: mintesterrArr = np.append(mintesterrArr,Z_testerr[minindexarr[0]]) minaic = np.append(minaic,aic[minindexarr[0]]) minbic = np.append(minbic,bic[minindexarr[0]]) minparam = np.append(minparam,param[minindexarr[0]]) minnnz = np.append(minnnz,karr[minindexarr[0]]) minmn = np.append(minmn,mn[minindexarr[0]]) elif minindexarr[1]==minindexarr[2]: mintesterrArr = np.append(mintesterrArr,Z_testerr[minindexarr[1]]) minaic = np.append(minaic,aic[minindexarr[1]]) minbic = np.append(minbic,bic[minindexarr[1]]) minparam = np.append(minparam,param[minindexarr[1]]) minnnz = np.append(minnnz,karr[minindexarr[1]]) minmn = np.append(minmn,mn[minindexarr[1]]) # no elements match. Highly unlikely that we will be here else: #take the case where test arr is minimum mintesterrArr = np.append(mintesterrArr,Z_testerr[minindexarr[0]]) minaic = np.append(minaic,aic[minindexarr[0]]) minbic = np.append(minbic,bic[minindexarr[0]]) minparam = np.append(minparam,param[minindexarr[0]]) minnnz = np.append(minnnz,karr[minindexarr[0]]) minmn = np.append(minmn,mn[minindexarr[0]]) print("#\tpq\ttesterr\t\tM+N\tNNZ\taic\t\tbic\t\tlambda") for i in range(len(runs)): pdeg = runs[i][0] qdeg = runs[i][1] print("%d\tp%dq%d\t%f\t%d\t%d\t%f\t%f\t%.2E"%(i+1,pdeg,qdeg,mintesterrArr[i],minmn[i],minnnz[i],minaic[i],minbic[i],minparam[i])) print("\n") sortedmnindex = np.argsort(minmn) print("#\tpq\ttesterr\t\tM+N\tNNZ\taic\t\tbic\t\tlambda") for i in sortedmnindex: pdeg = runs[i][0] qdeg = runs[i][1] print("%d\tp%dq%d\t%f\t%d\t%d\t%f\t%f\t%.2E"%(i+1,pdeg,qdeg,mintesterrArr[i],minmn[i],minnnz[i],minaic[i],minbic[i],minparam[i])) print("\nMIN\t\t%d\t\t%d\t%d\t%d\t\t%d\n"%(np.argmin(mintesterrArr)+1,np.argmin(minmn)+1,np.argmin(minnnz)+1,np.argmin(minaic)+1,np.argmin(minbic)+1))
def plotmntesterr(jsonfilearr, jsonfiledescrarr, testfile, runs, fno,folder): # LT, RT, LB, RB maxpq = np.amax(runs,axis=0) outfile1 = folder+"/"+fno+".299445.png" outfile2 = folder+"/"+fno+"_index.299445.png" X_test, Y_test = readData(testfile) testerractuals = {} testerrindex = {} for i in range(len(jsonfilearr)): jsonfile = jsonfilearr[i] import json if jsonfile: with open(jsonfile, 'r') as fn: datastore = json.load(fn) testerrarr = np.zeros(shape=(maxpq[0],maxpq[1]),dtype=np.float64) testerrarr2n = np.zeros(shape=(maxpq[0],maxpq[1]),dtype=np.float64) testerrarr1n = np.zeros(shape=(maxpq[0],maxpq[1]),dtype=np.float64) testerrarrinfn = np.zeros(shape=(maxpq[0],maxpq[1]),dtype=np.float64) for r in runs: pdeg=r[0] qdeg=r[1] key = "p%s_q%s"%(str(pdeg),str(qdeg)) print(key) # iterationInfo = datastore[key]["iterationinfo"] # lastii = iterationInfo[len(iterationInfo)-1] rappsip = RationalApproximationSIP(datastore[key]) Y_pred = rappsip(X_test) testerror = np.average((Y_pred-Y_test)**2) testerrarr[pdeg-1][qdeg-1] = testerror testerrarr2n[pdeg-1][qdeg-1] = np.sqrt(np.sum((Y_pred-Y_test)**2)) testerrarr1n[pdeg-1][qdeg-1] = np.sum(np.absolute((Y_pred-Y_test))) testerrarrinfn[pdeg-1][qdeg-1] = np.max(np.absolute((Y_pred-Y_test))) testerractuals[i] = testerrarr sortedindexarr = np.argsort(-testerrarr,axis=None)[::-1].argsort() sortedindexarr = np.reshape(sortedindexarr,(maxpq[0],maxpq[1])) testerrindex[i] = sortedindexarr # print(testerrarr) # print(testerrarr1n) # print(testerrarr2n) # print(testerrarrinfn) # print(sortedindexarr) print(np.argmin(testerrarr), np.min(testerrarr),np.min(testerrarr1n),np.min(testerrarr2n),np.min(testerrarrinfn)) print(np.max(testerrarr)) 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' # X,Y = np.meshgrid(range(1,maxpq[0]+1), range(1,maxpq[1]+1)) X,Y = np.meshgrid(range(1,maxpq[1]+1), range(1,maxpq[0]+1)) f, axarr = plt.subplots(2,2, sharex=True, sharey=True, figsize=(15,15)) f.suptitle(fno + " -- log(average test error)", fontsize = 28) markersize = 1000 vmin = -6 vmax = -1 for i in range(2): for j in range(2): testerrarr = testerractuals[i*2+j] sc = axarr[i][j].scatter(X,Y, marker = 's', s=markersize, c = np.ma.log10(testerrarr), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1) axarr[i][j].set_title(jsonfiledescrarr[i*2+j], fontsize = 28) for ax in axarr.flat: ax.set(xlim=(0,maxpq[1]+1),ylim=(0,maxpq[0]+1)) ax.tick_params(axis = 'both', which = 'major', labelsize = 18) ax.tick_params(axis = 'both', which = 'minor', labelsize = 18) ax.set_xlabel('$n$', fontsize = 22) ax.set_ylabel('$m$', fontsize = 22) for ax in axarr.flat: ax.label_outer() b=f.colorbar(sc,ax=axarr.ravel().tolist(), shrink=0.95) # b.set_label("Error = $log_{10}\\left(\\frac{\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_%i}{%i}\\right)$"%(norm,testSize), fontsize = 28) plt.savefig(outfile1) 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' X,Y = np.meshgrid(range(1,maxpq[1]+1), range(1,maxpq[0]+1)) f, axarr = plt.subplots(2,2, sharex=True, sharey=True, figsize=(15,15)) f.suptitle(fno + " -- ordered enumeration of test error", fontsize = 28) markersize = 1000 vmin = 0 vmax = maxpq[0] * maxpq[1] for i in range(2): for j in range(2): sortedindexarr = testerrindex[i*2+j] sc = axarr[i][j].scatter(X,Y, marker = 's', s=markersize, c = sortedindexarr, cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1) axarr[i][j].set_title(jsonfiledescrarr[i*2+j], fontsize = 28) for ax in axarr.flat: ax.set(xlim=(0,maxpq[1]+1),ylim=(0,maxpq[0]+1)) ax.tick_params(axis = 'both', which = 'major', labelsize = 18) ax.tick_params(axis = 'both', which = 'minor', labelsize = 18) ax.set_xlabel('$n$', fontsize = 22) ax.set_ylabel('$m$', fontsize = 22) for ax in axarr.flat: ax.label_outer() b=f.colorbar(sc,ax=axarr.ravel().tolist(), shrink=0.95) # b.set_label("Error = $log_{10}\\left(\\frac{\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_%i}{%i}\\right)$"%(norm,testSize), fontsize = 28) plt.savefig(outfile2)
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)
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)
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
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)
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
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()
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 findsincroots(): import os, sys def appevaldenom(x, y, z, app, toscale, printnumer): if (toscale == 1): X = app._scaler.scale(np.array([x, y, z])) else: X = np.array([x, y, z]) if (printnumer == 1): print("numer=%f" % (app.numer(X))) return app.denom(X) def appevalnumer(x, y, z, app, toscale, printnumer): if (toscale == 1): X = app._scaler.scale(np.array([x, y, z])) else: X = np.array([x, y, z]) n = app.numer(X) if (printnumer == 1): print("numer=%f" % (n)) return n def appgetonevardenom(x, y, app): import apprentice struct_q = apprentice.monomialStructure(datastore['dim'], datastore['n']) qcoeff = app.qcoeff a = 0 b = 0 c = 0 d = 0 for num, row in enumerate(struct_q): val = qcoeff[num] * (x**row[0] * y**row[1]) if (row[2] == 0): d += val elif (row[2] == 1): c += val elif (row[2] == 2): b += val elif (row[2] == 3): a += val # z = -0.2345 # fthis = a*z**3 + b*z**2 +c*z +d # fnorm = appevaldenom(x,y,z,app,0,0) # print(fthis,fnorm) return a, b, c, d fname = "f20" larr = [10**-6, 10**-3] uarr = [2 * np.pi, 4 * np.pi] lbdesc = {0: "-6", 1: "-3"} ubdesc = {0: "2pi", 1: "4pi"} m = 2 n = 3 ts = "2x" noisestr = "" folder = "%s%s_%s/sincrun" % (fname, noisestr, ts) dim = 3 numlb = 0 numub = 1 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]) l = "_p%d_q%d_ts%s.json" % (m, n, ts) jsonfile = folder + '/' + fndesc + "/out/" + fndesc + l if not os.path.exists(jsonfile): print("%s not found" % (jsonfile)) exit(1) if not os.path.exists(folder + '/' + fndesc + "/plots"): os.mkdir(folder + '/' + fndesc + '/plots') import json if jsonfile: with open(jsonfile, 'r') as fn: datastore = json.load(fn) for iter in range(len(datastore['iterationinfo'])): # print("Doing iter = %d"%(iter+1)) if jsonfile: with open(jsonfile, 'r') as fn: datastore = json.load(fn) datastore['pcoeff'] = datastore['iterationinfo'][iter]['pcoeff'] datastore['qcoeff'] = datastore['iterationinfo'][iter]['qcoeff'] from apprentice import RationalApproximationSIP rappsip = RationalApproximationSIP(datastore) robarg = datastore['iterationinfo'][iter]['robOptInfo']['robustArg'] # print(rappsip._scaler.unscale(np.array(robarg))) print(robarg) continue X = np.linspace(-1, 1, num=1000) Y = np.linspace(-1, 1, num=1000) X0 = np.array([]) Y0 = np.array([]) Z0 = np.array([]) N = np.array([]) for x in X: for y in Y: a, b, c, d = appgetonevardenom(x, y, rappsip) roots, res = solve(a, b, c, d) if (res == "threeequal" or res == "onereal"): r = np.real(roots[0]) if (r >= -1 and r <= 1): xu, yu, ru = rappsip._scaler.unscale([x, y, r]) qnormu = appevaldenom(xu, yu, ru, rappsip, 1, 0) if (qnormu == 0): X0 = np.append(X0, xu) Y0 = np.append(Y0, yu) Z0 = np.append(Z0, ru) N = np.append( N, appevalnumer(xu, yu, ru, rappsip, 1, 0)) elif (res == 'threereal'): for r in roots: r = np.real(r) if (r >= -1 and r <= 1): xu, yu, ru = rappsip._scaler.unscale([x, y, r]) qnormu = appevaldenom(xu, yu, ru, rappsip, 1, 0) if (qnormu == 0): X0 = np.append(X0, xu) Y0 = np.append(Y0, yu) Z0 = np.append(Z0, ru) N = np.append( N, appevalnumer(xu, yu, ru, rappsip, 1, 0)) outcsv = "%s/%s/plots/Croots_iter%d.csv" % (folder, fndesc, iter + 1) np.savetxt(outcsv, np.stack((X0, Y0, Z0, N), axis=1), delimiter=",")
def plot2Dsurface(fname, noise, m, n, ts, fndesc, papp_or_no_papp): 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') if (papp_or_no_papp == "papp"): cols = 4 elif (papp_or_no_papp == "no_papp"): cols = 3 else: raise Exception("papp_or_no_papp ambiguous") 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 = 250 X_test1 = np.linspace(box[0][0], box[0][1], num=npoints) X_test2 = np.linspace(box[1][0], box[1][1], num=npoints) outx1 = "%s/plots/Cfnsurf_X_%s%s_p%s_q%s_ts%s.csv" % (folder, fname, noisestr, m, n, ts) outy = "%s/plots/Cfnsurf_Y_%s%s_p%s_q%s_ts%s.csv" % (folder, fname, noisestr, m, n, ts) rappsipfile = "%s/out/%s%s_%s_p%s_q%s_ts%s.json" % (folder, fname, noisestr, ts, m, n, ts) rappfile = "%s/outra/%s%s_%s_p%s_q%s_ts%s.json" % (folder, fname, noisestr, ts, m, n, ts) pappfile = "%s/outpa/%s%s_%s_p%s_q%s_ts%s.json" % (folder, fname, noisestr, ts, m, n, ts) if not os.path.exists(rappsipfile): print("rappsipfile %s not found" % (rappsipfile)) exit(1) if not os.path.exists(rappfile): print("rappfile %s not found" % (rappfile)) exit(1) if not os.path.exists(pappfile): print("pappfile %s not found" % (pappfile)) exit(1) rappsip = RationalApproximationSIP(rappsipfile) rapp = RationalApproximationONB(fname=rappfile) papp = PolynomialApproximation(fname=pappfile) if (rappsip.dim != 2): print("{} cannot handle dim != 2 Dim found in datastore was {}".format( sys.argv[0], rappsip.dim)) sys.exit(1) from apprentice import testData Y_test = [] for x in X_test1: for y in X_test2: Y_test.append( eval('testData.' + fname + '([' + str(x) + ',' + str(y) + '])')) Y_pred_rappsip = [] for x in X_test1: for y in X_test2: Y_pred_rappsip.append(rappsip.predict([x, y])) Y_pred_rapp = [] for x in X_test1: for y in X_test2: Y_pred_rapp.append(rapp([x, y])) Y_pred_papp = [] if (papp_or_no_papp == "papp"): for x in X_test1: for y in X_test2: Y_pred_papp.append(papp([x, y])) np.savetxt(outx1, np.stack((X_test1, X_test2), axis=1), delimiter=",") def removezeros(YYY): for num, y in enumerate(YYY): if (y == 0): # print(y, num) lower = 0 upper = len(YYY) - 1 index = num index -= 1 while (index >= 0): if (YYY[index] != 0): lower = index break index -= 1 index = num index += 1 while (index < len(YYY)): if (YYY[index] != 0): upper = index break index += 1 YYY[num] = (YYY[lower] + YYY[upper]) / 2 # print("became") # print(YYY[num], num) return YYY Y_pred_papp_diff = np.absolute(np.array(Y_pred_papp) - np.array(Y_test)) Y_pred_papp_diff = removezeros(Y_pred_papp_diff) Y_pred_rapp_diff = np.absolute(np.array(Y_pred_rapp) - np.array(Y_test)) Y_pred_rapp_diff = removezeros(Y_pred_rapp_diff) Y_pred_rappsip_diff = np.absolute( np.array(Y_pred_rappsip) - np.array(Y_test)) Y_pred_rappsip_diff = removezeros(Y_pred_rappsip_diff) np.savetxt( outy, np.stack((np.ma.log10(Y_pred_papp_diff), np.ma.log10(Y_pred_rapp_diff), np.ma.log10(Y_pred_rappsip_diff)), axis=1), delimiter=",") minyval = np.inf maxyval = 0 arr1 = np.array([min(Y_pred_rappsip), min(Y_test), min(Y_pred_rapp)]) if (papp_or_no_papp == "papp"): np.append(arr1, min(Y_pred_papp)) minyval = np.min(arr1) arr1 = np.array([max(Y_pred_rappsip), max(Y_test), max(Y_pred_rapp)]) if (papp_or_no_papp == "papp"): np.append(arr1, max(Y_pred_papp)) maxyval = np.max(arr1) 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' # https://stackoverflow.com/questions/16267143/matplotlib-single-colored-colormap-with-saturation 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 fig = plt.figure(figsize=(20, 5)) ax = fig.add_subplot(1, cols, 1, projection='3d') Y_test = np.reshape(Y_test, [len(X_test1), len(X_test2)]) im1 = ax.contour3D(X_test1, X_test2, Y_test, 100, cmap=cmap, norm=colors.SymLogNorm(linthresh=0.2, linscale=0.5, vmin=minyval, vmax=maxyval), alpha=0.8) ax.set_title('(a)', fontsize=12) ax.set_xlabel('$x_1$', fontsize=12) ax.set_ylabel('$x_2$', fontsize=12) ax = fig.add_subplot(1, cols, 2, projection='3d') Y_pred_rappsip = np.reshape(Y_pred_rappsip, [len(X_test1), len(X_test2)]) im1 = ax.contour3D(X_test1, X_test2, Y_pred_rappsip, 100, cmap=cmap, norm=colors.SymLogNorm(linthresh=0.2, linscale=0.5, vmin=minyval, vmax=maxyval), alpha=0.8) ax.set_title('(b)', fontsize=12) ax.set_xlabel('$x_1$', fontsize=12) ax.set_ylabel('$x_2$', fontsize=12) ax = fig.add_subplot(1, cols, 3, projection='3d') Y_pred_rapp = np.reshape(Y_pred_rapp, [len(X_test1), len(X_test2)]) im1 = ax.contour3D(X_test1, X_test2, Y_pred_rapp, 100, cmap=cmap, norm=colors.SymLogNorm(linthresh=0.2, linscale=0.5, vmin=minyval, vmax=maxyval), alpha=0.8) ax.set_title('(c)', fontsize=12) ax.set_xlabel('$x_1$', fontsize=12) ax.set_ylabel('$x_2$', fontsize=12) if (papp_or_no_papp == "papp"): ax = fig.add_subplot(1, cols, 4, projection='3d') Y_pred_papp = np.reshape(Y_pred_papp, [len(X_test1), len(X_test2)]) im1 = ax.contour3D(X_test1, X_test2, Y_pred_papp, 100, cmap=cmap, norm=colors.SymLogNorm(linthresh=0.2, linscale=0.5, vmin=minyval, vmax=maxyval), alpha=0.8) ax.set_title('(d)', fontsize=12) ax.set_xlabel('$x_1$', fontsize=12) ax.set_ylabel('$x_2$', fontsize=12) mmm = plt.cm.ScalarMappable(cmap=cmap) mmm.set_array(Y_pred_rapp) mmm.set_clim(minyval, maxyval) b2 = fig.colorbar(mmm) b2.set_label("$\\frac{p(x_1,x_2)}{q(x_1,x_2)}$", fontsize=16) outfile = "%s/plots/P2d1f_%s%s_p%s_q%s_ts%s_%s.pdf" % ( folder, fname, noisestr, m, n, ts, papp_or_no_papp) # plt.show() plt.savefig(outfile) print("open %s;" % (outfile)) plt.close('all')
def tableS0(jsonfile, testfile, runs): import json if jsonfile: with open(jsonfile, 'r') as fn: datastore = json.load(fn) X_test, Y_test = readData(testfile) karr = np.array([]) aic = np.array([]) bic = np.array([]) X_l2 = np.array([]) Z_testerr = np.array([]) mn = np.array([]) for r in runs: pdeg=r[0] qdeg=r[1] key = "p%s_q%s"%(str(pdeg),str(qdeg)) iterationInfo = datastore[key]["iterationinfo"] lastii = iterationInfo[len(iterationInfo)-1] trainerr = lastii["leastSqObj"] X_l2 = np.append(X_l2,trainerr) rappsip = RationalApproximationSIP(datastore[key]) Y_pred = rappsip(X_test) testerror = np.sum((Y_pred-Y_test)**2) Z_testerr = np.append(Z_testerr,testerror) k = 2 pcoeff = datastore[key]["pcoeff"] qcoeff = datastore[key]["qcoeff"] maxp = abs(max(pcoeff, key=abs)) maxq = abs(max(qcoeff, key=abs)) # print(np.c_[pcoeff]) # print(np.c_[qcoeff]) # print(maxp,maxq) for pc in pcoeff: if(pc > 10**-2*maxp): k += 1 for qc in qcoeff: if(qc > 10**-2*maxq): k += 1 karr = np.append(karr,k) n = len(X_test) # AIC = 2k - 2log(L) # BIC = klog(n) - 2log(L) # -2log(L) becomes nlog(variance) = nlog(SSE/n) = nlog(testerror/n) a = 2*k + n*np.log(testerror/n) b = k*np.log(n) + n*np.log(testerror/n) aic = np.append(aic,a) bic = np.append(bic,b) mn = np.append(mn,rappsip.M+rappsip.N) sortedmnindex = np.argsort(mn) print("#\tpq\tl2 error\ttest err\tM+N\tnnz\taic\t\tbic") for i in sortedmnindex: r = runs[i] pdeg=r[0] qdeg=r[1] print("%d\tp%dq%d\t%f\t%f\t%d\t%d\t%f\t%f"%(i+1,pdeg,qdeg,X_l2[i],Z_testerr[i],mn[i],karr[i],aic[i],bic[i])) print("\nMIN\t\t%d\t\t%d\t\t%d\t%d\t\t%d\t\t%d\n"%(np.argmin(X_l2)+1,np.argmin(Z_testerr)+1,np.argmin(mn)+1,np.argmin(karr)+1,np.argmin(aic)+1,np.argmin(bic)+1))
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')
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))