def mkAndStoreRapp(fin, fout, strategy, order, trainingsize): X, Y = app.readData(fin) if trainingsize <= Y.size: X=X[0:trainingsize] Y=Y[0:trainingsize] else: raise Exception("Requested trainigsize {} exceeds available data {}".format(trainingsize, Y.size)) r=app.RationalApproximation(X,Y, order=order, strategy=strategy) r.save(fout)
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 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 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 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 plotPorQResidual(dir_in, dir_out, fno=1, datatype="train", f_test="", norm=1): noiseStr = "" if "noise_0.1" in dir_in: noiseStr = "_noise_0.1" elif "noise_0.5" in dir_in: noiseStr = "_noise_0.5" X_test = [] Y_test = [] testSize = 0 if (datatype == "test"): X_test, Y_test = app.readData(f_test) testSize = len(X_test[:, 0]) # print(fno); # print(noiseStr) # print(dir_in) # print(dir_out) # exit(1) porqOpt = ["ppen", "qpen"] noPointsScale = ["1x", "2x", "1k"] # # Static for now # pOrq0 = porqOpt[0] # pOrqN0 = "qpen" # npoints = "1x" for pOrq0 in porqOpt: error_m_n_1x = np.zeros(shape=(4, 4)) error_m_n_2x = np.zeros(shape=(4, 4)) error_m_n_1k = np.zeros(shape=(4, 4)) if (pOrq0 == "ppen"): pOrqN0 = "qpen" else: pOrqN0 = "ppen" for npoints in noPointsScale: 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, sharex=True, figsize=(20, 20)) dataTypeStr = "" if (datatype == "train"): dataTypeStr = "Training LSQ" elif (datatype == "test"): dataTypeStr = "Testing LSQ" f.suptitle(dataTypeStr + ". FixedPenalty = " + pOrq0 + ". noOfPoints = " + npoints + ". f" + str(fno) + ": " + getFunctionLatex(fno), fontsize=28) # yOrder = [ "1111", # 0 1 "0111", # 1 2 "1011", # 2 3 "1101", # 3 4 "1110", # 4 5.1 "0011", # 5 5.2 "0101", # 6 6 "0110", # 7 7.1 "1001", # 8 7.2 "1010", # 9 8 "1100", # 10 9.1 "0001", # 11 9.2 "0010", # 12 10 "0100", # 13 11 "1000", # 14 12 "0000" ] # 15 13 for pdeg in range(1, 5): for qdeg in range(1, 5): leastSq = {} index = 0 while index < len(yOrder): yKey = yOrder[index] yAct = (yKey)[::-1] penStr = "" if (pOrqN0 == "ppen"): if (int(yAct, 2) >= 2**pdeg): index += 1 continue penStr = pOrqN0 + yKey + "_" + pOrq0 + "0000" else: if (int(yAct, 2) >= 2**qdeg): index += 1 continue penStr = pOrq0 + "0000_" + pOrqN0 + yKey jsonfn = dir_in + "/f" + str( fno ) + noiseStr + "_p" + str(pdeg) + "_q" + str( qdeg ) + "_n" + npoints + "_RA_SIP_LSQSO_Qxge1_Xsample_s10_" + penStr + ".json" # print(jsonfn) if (pOrqN0 == "ppen"): yKey = flipBitsUpto(yKey, pdeg) elif (pOrqN0 == "qpen"): yKey = flipBitsUpto(yKey, qdeg) if (datatype == "train"): import json if jsonfn: with open(jsonfn, 'r') as fn: datastore = json.load(fn) iterationInfo = datastore["iterationInfo"] # print(yOrder[index]) leastSq[yKey] = iterationInfo[len(iterationInfo) - 1]['LeastSqObj'] elif (datatype == "test"): R = app.readApprentice(jsonfn) if norm == 1: res = [ abs(R(x) - Y_test[num]) for num, x in enumerate(X_test) ] if norm == 2: res = [(R(x) - Y_test[num])**2 for num, x in enumerate(X_test)] leastSq[yKey] = sum(res) / testSize index += 1 # print(leastSq) X = [] Y = [] # OPTIONS plotOrder = 2 if (plotOrder == 1): # IN the order of Yorder index = 0 while index < len(yOrder): yKey = yOrder[index] if (pOrqN0 == "ppen"): yKey = flipBitsUpto(yKey, pdeg) elif (pOrqN0 == "qpen"): yKey = flipBitsUpto(yKey, qdeg) if (yKey in leastSq): X.append(leastSq[yKey]) Y.append(yKey + "(" + str(calcNumberOfNonZeroCoeff(yKey)) + ")") index += 1 elif (plotOrder == 2): # IN decreasing order of leastSq for key, value in sorted(leastSq.iteritems(), key=lambda (k, v): (v, k), reverse=True): index = 0 Y.append(key + "(" + str(calcNumberOfNonZeroCoeff(key)) + ")") X.append(value) # print ("=================",pdeg,qdeg,"=============") nonZeroY = np.array([]) keyArr = np.array([]) leastSqX = np.array([]) for key in leastSq: nonZeroY = np.append(nonZeroY, calcNumberOfNonZeroCoeff(key)) keyArr = np.append(keyArr, key) leastSqX = np.append(leastSqX, leastSq[key]) # OPTIONS scaleOption = 1 if (scaleOption == 1): # Y normalized to be between X.min() and X.max() nonZeroYNorm = (leastSqX.max() - leastSqX.min()) * ( (nonZeroY - nonZeroY.min()) / (nonZeroY.max() - nonZeroY.min())) + leastSqX.min( ) leastSqXNorm = leastSqX elif (scaleOption == 2): # X and Y normalized to be between 0 and 1 nonZeroYNorm = (nonZeroY - nonZeroY.min()) / ( nonZeroY.max() - nonZeroY.min()) leastSqXNorm = (leastSqX - leastSqX.min()) / ( leastSqX.max() - leastSqX.min()) distance = [] distance = np.sqrt( np.sum( [np.square(nonZeroYNorm), np.square(leastSqXNorm)], 0)) minKey = keyArr[np.argmin(distance)] minIndex = 0 while minIndex < len(Y): if (Y[minIndex] == minKey + "(" + str(calcNumberOfNonZeroCoeff(minKey)) + ")"): break minIndex += 1 if (npoints == "1x"): error_m_n_1x[pdeg - 1][qdeg - 1] = X[minIndex] if (npoints == "2x"): error_m_n_2x[pdeg - 1][qdeg - 1] = X[minIndex] if (npoints == "1k"): error_m_n_1k[pdeg - 1][qdeg - 1] = X[minIndex] logX = np.ma.log10(X) if (pOrqN0 == "ppen"): axarr[pdeg - 1][qdeg - 1].plot(logX, Y, '-rD', markevery=[minIndex]) axarr[pdeg - 1][qdeg - 1].set_title("p = " + str(pdeg) + "; q = " + str(qdeg)) else: axarr[qdeg - 1][pdeg - 1].plot(logX, Y, '-rD', markevery=[minIndex]) axarr[qdeg - 1][pdeg - 1].set_title("p = " + str(pdeg) + "; q = " + str(qdeg)) for ax in axarr.flat: ax.set(xlim=(-6, 4)) if (ax.is_first_col()): ax.set_ylabel(pOrqN0[0] + ' Non Zeros', fontsize=15) if (ax.is_last_row()): if (datatype == "train"): ax.set_xlabel( "$log_{10}\\left(\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2^2\\right)$", fontsize=15) elif (datatype == "test"): ax.set_xlabel( "$log_{10}\\left(\\frac{\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_%i}{%i}\\right)$" % (norm, testSize), fontsize=15) f_out = "" if (datatype == "train"): f_out = dir_out + "/f" + str( fno ) + noiseStr + "_n" + npoints + "_nz-" + pOrqN0 + "_training.png" elif (datatype == "test"): f_out = dir_out + "/f" + str( fno ) + noiseStr + "_n" + npoints + "_nz-" + pOrqN0 + "_testing.png" plt.savefig(f_out) # print(np.c_[error_m_n_1x ,error_m_n_2x, error_m_n_1k]) 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, 5), range(1, 5)) f, axarr = plt.subplots(3, sharex=True, sharey=True, figsize=(15, 15)) dataTypeStr = "" if (datatype == "train"): dataTypeStr = "Training LSQ" elif (datatype == "test"): dataTypeStr = "Testing LSQ" f.suptitle(dataTypeStr + ". FixedPenalty = " + pOrq0 + ". f" + str(fno) + ": " + getFunctionLatex(fno), fontsize=28) markersize = 1000 vmin = -6 vmax = 4 sc = axarr[0].scatter(X, Y, marker='s', s=markersize, c=np.ma.log10(error_m_n_1x), cmap=cmapname, vmin=vmin, vmax=vmax, alpha=1) axarr[0].set_title('Training size = 1x', fontsize=28) sc = axarr[1].scatter(X, Y, marker='s', s=markersize, c=np.ma.log10(error_m_n_2x), cmap=cmapname, vmin=vmin, vmax=vmax, alpha=1) axarr[1].set_title('Training size = 2x', fontsize=28) sc = axarr[2].scatter(X, Y, marker='s', s=markersize, c=np.ma.log10(error_m_n_1k), cmap=cmapname, vmin=vmin, vmax=vmax, alpha=1) axarr[2].set_title('Training size = 1000', fontsize=28) for ax in axarr.flat: ax.set(xlim=(0, 5), ylim=(0, 5)) ax.tick_params(axis='both', which='major', labelsize=18) ax.tick_params(axis='both', which='minor', labelsize=18) ax.set_xlabel('$m$', fontsize=22) ax.set_ylabel('$n$', fontsize=22) for ax in axarr.flat: ax.label_outer() b = f.colorbar(sc, ax=axarr.ravel().tolist(), shrink=0.95) if (datatype == "train"): b.set_label( "$log_{10}\\left(\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2^2\\right)$", fontsize=28) elif (datatype == "test"): b.set_label( "$log_{10}\\left(\\frac{\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_%i}{%i}\\right)$" % (norm, testSize), fontsize=28) f_out = "" if (datatype == "train"): f_out = dir_out + "/f" + str( fno) + noiseStr + "_nz-" + pOrqN0 + "_training.png" if (datatype == "test"): f_out = dir_out + "/f" + str( fno) + noiseStr + "_nz-" + pOrqN0 + "_testing.png" plt.savefig(f_out)
def plotPandQResidual(dir_in, dir_out, fno=1, datatype="train", f_test="", norm=1): noiseStr = "" if "noise_0.1" in dir_in: noiseStr = "_noise_0.1" elif "noise_0.5" in dir_in: noiseStr = "_noise_0.5" X_test = [] Y_test = [] testSize = 0 if (datatype == "test"): X_test, Y_test = app.readData(f_test) testSize = len(X_test[:, 0]) noPointsScale = ["1x", "2x", "1k"] # Only for (p,q) = (4,4) for now pdeg = 4 qdeg = 4 pyOrder = [ "1111", # 0 1 "1111", # 1 2.1 "0111", # 2 2.2 "0111", # 3 3 "1111", # 4 4.1 "0011", # 5 4.2 "0111", # 6 5.1 "0011", # 7 5.2 "1111", # 8 6.1 "0001", # 9 6.2 "0011", # 10 7 "0111", # 11 8.1 "0001", # 12 8.2 "0011", # 13 9.1 "0001", # 14 9.2 "0000", # 15 9.3 "1111", # 16 9.4 "0000", # 17 10.1 "0111", # 18 10.2 "0001", # 19 11 "0011", # 20 12.1 "0000", # 21 12.2 "0001", # 22 13.1 "0000" ] # 23 13.2 qyOrder = [ "1111", # 0 1 "0111", # 1 2.1 "1111", # 2 2.2 "0111", # 3 3 "0011", # 4 4.1 "1111", # 5 4.2 "0011", # 6 5.1 "0111", # 7 5.2 "0001", # 8 6.1 "1111", # 9 6.2 "0011", # 10 7 "0001", # 11 8.1 "0111", # 12 8.2 "0001", # 13 9.1 "0011", # 14 9.2 "1111", # 15 9.3 "0000", # 16 9.4 "0111", # 17 10.1 "0000", # 18 10.2 "0001", # 19 11 "0000", # 20 12.1 "0011", # 21 12.2 "0000", # 22 13.1 "0001" ] # 23 13.2 # print(np.c_[pyOrder,qyOrder]) 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(3, sharex=True, figsize=(15, 15)) dataTypeStr = "" if (datatype == "train"): dataTypeStr = "Training LSQ" elif (datatype == "test"): dataTypeStr = "Testing LSQ" f.suptitle(dataTypeStr + ". m = 4, n = 4. f" + str(fno) + ": " + getFunctionLatex(fno), fontsize=28) for npoints in noPointsScale: leastSq = {} index = 0 while index < len(pyOrder): pyKey = pyOrder[index] qyKey = qyOrder[index] penStr = "ppen" + pyKey + "_" + "qpen" + qyKey jsonfn = dir_in + "/f" + str(fno) + noiseStr + "_p" + str( pdeg ) + "_q" + str( qdeg ) + "_n" + npoints + "_RA_SIP_LSQSO_Qxge1_Xsample_s10_" + penStr + ".json" pyKey = flipBitsUpto(pyKey, pdeg) qyKey = flipBitsUpto(qyKey, qdeg) if (datatype == "train"): import json if jsonfn: with open(jsonfn, 'r') as fn: datastore = json.load(fn) iterationInfo = datastore["iterationInfo"] # print(yOrder[index]) leastSq[(pyKey, qyKey)] = iterationInfo[len(iterationInfo) - 1]['LeastSqObj'] elif (datatype == "test"): R = app.readApprentice(jsonfn) if norm == 1: res = [ abs(R(x) - Y_test[num]) for num, x in enumerate(X_test) ] if norm == 2: res = [(R(x) - Y_test[num])**2 for num, x in enumerate(X_test)] leastSq[(pyKey, qyKey)] = sum(res) / testSize index += 1 # print(leastSq) X = [] Y = [] # OPTIONS plotOrder = 2 if (plotOrder == 1): # IN the order of Yorder index = 0 while index < len(pyOrder): pyKey = pyOrder[index] qyKey = qyOrder[index] pyKey = flipBitsUpto(pyKey, pdeg) qyKey = flipBitsUpto(qyKey, qdeg) if ((pyKey, qyKey) in leastSq): X.append(leastSq[(pyKey, qyKey)]) no_nz_coeff = (calcNumberOfNonZeroCoeff(pyKey)) \ + (calcNumberOfNonZeroCoeff(qyKey)) Y.append(pyKey + "-" + qyKey + "(" + str(no_nz_coeff) + ")") index += 1 elif (plotOrder == 2): # IN decreasing order of leastSq for key, value in sorted(leastSq.iteritems(), key=lambda (k, v): (v, k), reverse=True): index = 0 no_nz_coeff = (calcNumberOfNonZeroCoeff(key[0])) \ + (calcNumberOfNonZeroCoeff(key[1])) Y.append(key[0] + "-" + key[1] + "(" + str(no_nz_coeff) + ")") X.append(value) # print(np.c_[X,Y]) nonZeroY = np.array([]) pkeyArr = np.array([]) qkeyArr = np.array([]) leastSqX = np.array([]) for key in leastSq: no_nz_coeff = (calcNumberOfNonZeroCoeff(key[0])) \ + (calcNumberOfNonZeroCoeff(key[1])) nonZeroY = np.append(nonZeroY, no_nz_coeff) pkeyArr = np.append(pkeyArr, key[0]) qkeyArr = np.append(qkeyArr, key[1]) leastSqX = np.append(leastSqX, leastSq[key]) # OPTIONS scaleOption = 1 if (scaleOption == 1): # Y normalized to be between X.min() and X.max() nonZeroYNorm = (leastSqX.max() - leastSqX.min()) * ( (nonZeroY - nonZeroY.min()) / (nonZeroY.max() - nonZeroY.min())) + leastSqX.min() leastSqXNorm = leastSqX elif (scaleOption == 2): # X and Y normalized to be between 0 and 1 nonZeroYNorm = (nonZeroY - nonZeroY.min()) / (nonZeroY.max() - nonZeroY.min()) leastSqXNorm = (leastSqX - leastSqX.min()) / (leastSqX.max() - leastSqX.min()) distance = [] distance = np.sqrt( np.sum([np.square(nonZeroYNorm), np.square(leastSqXNorm)], 0)) pminKey = pkeyArr[np.argmin(distance)] qminKey = qkeyArr[np.argmin(distance)] minIndex = 0 while minIndex < len(Y): no_nz_coeff = (calcNumberOfNonZeroCoeff(pminKey)) \ + (calcNumberOfNonZeroCoeff(qminKey)) if (Y[minIndex] == pminKey + "-" + qminKey + "(" + str(no_nz_coeff) + ")"): break minIndex += 1 # print(np.c_[X,Y]) # print(np.c_[pkeyArr, qkeyArr, distance, Y, X]) # print(pminKey,qminKey, minIndex, np.min(distance)) # exit(1) logX = np.ma.log10(X) if (npoints == "1x"): axarr[0].plot(logX, Y, '-rD', markevery=[minIndex]) axarr[0].set_title("no of points = 1x") if (npoints == "2x"): axarr[1].plot(logX, Y, '-rD', markevery=[minIndex]) axarr[1].set_title("no of points = 2x") if (npoints == "1k"): axarr[2].plot(logX, Y, '-rD', markevery=[minIndex]) axarr[2].set_title("no of points = 1000") for ax in axarr.flat: ax.set(xlim=(-6, 4)) if (ax.is_first_col()): ax.set_ylabel('p-q Non Zeros', fontsize=15) if (ax.is_last_row()): if (datatype == "train"): ax.set_xlabel( "$log_{10}\\left(\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2^2\\right)$", fontsize=15) elif (datatype == "test"): ax.set_xlabel( "$log_{10}\\left(\\frac{\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_%i}{%i}\\right)$" % (norm, testSize), fontsize=15) f_out = "" if (datatype == "train"): f_out = dir_out + "/f" + str(fno) + noiseStr + "_p" + str( pdeg) + "_q" + str( qdeg) + "_n" + npoints + "_nz-pandq_training.png" elif (datatype == "test"): f_out = dir_out + "/f" + str(fno) + noiseStr + "_p" + str( pdeg) + "_q" + str(qdeg) + "_n" + npoints + "_nz-pandq_testing.png" plt.savefig(f_out)
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 plotError(f_test, f_out, norm=1, fno=1, *f_rapp): X_test, Y_test = app.readData(f_test) testSize = len(X_test[:,0]) # error that maps average error to m,n on x and y axis respectively import numpy as np # error_m_n_all = np.zeros(shape=(4,4)) error_m_n_1x = np.zeros(shape=(4,4)) error_m_n_2x = np.zeros(shape=(4,4)) error_m_n_1k = np.zeros(shape=(4,4)) for i in range(len(f_rapp[0])): # print(f_rapp[0][i]) R = app.readApprentice(f_rapp[0][i]) if norm == 1: res = [abs(R(x)-Y_test[num]) for num, x in enumerate(X_test)] if norm == 2: res = [(R(x)-Y_test[num])**2 for num, x in enumerate(X_test)] m = R.m n = R.n addTerm = sum(res)/testSize if R.trainingsize == R.M + R.N: error_m_n_1x[m-1][n-1] = error_m_n_1x[m-1][n-1] + addTerm elif R.trainingsize == 2*(R.M + R.N): error_m_n_2x[m-1][n-1] = error_m_n_2x[m-1][n-1] + addTerm elif R.trainingsize == 1000: error_m_n_1k[m-1][n-1] = error_m_n_1k[m-1][n-1] + addTerm else: raise Exception("Something is wrong here. Incorrect training size used") # error_m_n_all[m-1][n-1] = error_m_n_all[m-1][n-1] + addTerm 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,5), range(1,5)) f, axarr = plt.subplots(3, sharex=True, sharey=True, figsize=(15,15)) f.suptitle("f"+str(fno)+": "+getFunctionLatex(fno), fontsize = 28) markersize = 1000 vmin = -4 vmax = 2.5 v = np.linspace(-6, 3, 1, endpoint=True) # sc1 = axarr[0,0].scatter(X,Y, marker = 's', s=markersize, c = np.ma.log10(error_m_n_all), cmap = cmapname, alpha = 1) # axarr[0,0].set_title('All training size') sc = axarr[0].scatter(X,Y, marker = 's', s=markersize, c = np.ma.log10(error_m_n_1x), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1) axarr[0].set_title('Training size = 1x', fontsize = 28) sc = axarr[1].scatter(X,Y, marker = 's', s=markersize, c = np.ma.log10(error_m_n_2x), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1) axarr[1].set_title('Training size = 2x', fontsize = 28) sc = axarr[2].scatter(X,Y, marker = 's', s=markersize, c = np.ma.log10(error_m_n_1k), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1) axarr[2].set_title('Training size = 1000', fontsize = 28) for ax in axarr.flat: ax.set(xlim=(0,5),ylim=(0,5)) ax.tick_params(axis = 'both', which = 'major', labelsize = 18) ax.tick_params(axis = 'both', which = 'minor', labelsize = 18) ax.set_xlabel('$m$', fontsize = 22) ax.set_ylabel('$n$', 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.show() plt.savefig(f_out)
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 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))