def fit(self, **kwargs): """ Do everything """ from apprentice import tools n_required = tools.numCoeffsPoly(self.dim, self.m) if n_required > self._Y.shape[0]: raise Exception( "Not enough inputs: got %i but require %i to do m=%i" % (self._Y.shape[0], n_required, self.m)) self.setStructures() from apprentice import monomial VM = monomial.vandermonde(self._X, self.m) strategy = kwargs["strategy"] if kwargs.get( "strategy") is not None else 1 if kwargs.get("computecov") is not False: self._cov = np.linalg.inv(2 * VM.T @ VM) if strategy == 1: self.coeffSolve(VM) elif strategy == 2: self.coeffSolve2(VM) # NOTE, strat 1 is faster for smaller problems (Npoints < 250) else: raise Exception("fit() strategy %i not implemented" % strategy)
def vandermonde(params, order): """ Construct the Vandermonde matrix. """ import numpy as np try: dim = len(params[0]) except: dim = 1 from apprentice import tools s = monomialStructure(dim, order) if len(params[0]) == 1: V = np.zeros((len(params), tools.numCoeffsPoly(dim, order)), dtype=np.float64) for a, p in enumerate(params): V[a]=recurrence1D(p, s) return V else: V = np.ones((tools.numCoeffsPoly(dim, order), *params.shape), dtype=np.float64) np.power(params, s[:, np.newaxis], out=(V), where=s[:, np.newaxis]>0) return np.prod(V, axis=2).T
def monomialStructure(dim, order): import numpy as np import copy from apprentice import tools ncmax = tools.numCoeffsPoly(dim, order) gen = genStruct(dim, np.zeros(dim)) structure = np.array([ copy.copy(next(gen)) for _ in range(ncmax)], dtype=int) # Dimension one requires some extra treatment when returning ---writing out is fine if dim==1: return structure.ravel() return structure
def createTable2structure(format = 'table'): if(format == 'table'): print("\t\tRational Approx\t\t\t\t\tPolynomial Approx") print("#vars\t\tdeg num\t\tdeg denom\tDoF\t\tdeg\tDoF") fmt = "%d\t\t%d\t\t%d\t\t%d\t\t%d\t%d" elif(format == 'latex'): print("Not implemented... Exiting Now!") exit(1) for dim in range(1,9): for m in range(1,9): for n in range(1,9): M = tools.numCoeffsPoly(dim, m) N = tools.numCoeffsPoly(dim, n) o = 1 O = tools.numCoeffsPoly(dim, o) while(O < M+N): o += 1 O = tools.numCoeffsPoly(dim, o) print(fmt%(dim,m,n,M+N,o,O))
def setStructures(self): self._struct_p = apprentice.monomialStructure(self.dim, self.m) from apprentice import tools self._M = tools.numCoeffsPoly(self.dim, self.m) self._nnz = self._struct_p > 0
def calcualteNonLin(dim, n): if (n == 0): return 0 N = tools.numCoeffsPoly(dim, n) return N - (dim + 1)
def mkFromData(self, kwargs): """ Calculate the Pade approximation """ self._dim = self._X[0].shape[0] self._m = int(kwargs["m"]) if kwargs.get("m") is not None else 1 self._n = int(kwargs["n"]) if kwargs.get("n") is not None else 1 self._M = tools.numCoeffsPoly(self.dim, self.m) self._N = tools.numCoeffsPoly(self.dim, self.n) self._strategy = int( kwargs["strategy"]) if kwargs.get("strategy") is not None else 0 self._roboptstrategy = kwargs["roboptstrategy"] if kwargs.get( "roboptstrategy") is not None else "ms" self._localoptsolver = kwargs["localoptsolver"] if kwargs.get( "localoptsolver") is not None else "scipy" self._fitstrategy = kwargs["fitstrategy"] if kwargs.get( "fitstrategy") is not None else "scipy" self._filterpyomodebug = kwargs["filterpyomodebug"] if kwargs.get( "filterpyomodebug") is not None else 0 if self._filterpyomodebug == 2: self._debugfolder = kwargs["debugfolder"] self._fnname = kwargs["fnname"] self._trainingscale = kwargs["trainingscale"] if kwargs.get( "trainingscale") is not None else "1x" if (self.trainingscale == ".5x" or self.trainingscale == "0.5x"): self._trainingscale = ".5x" self._trainingsize = int(0.5 * (self.M + self.N)) elif (self.trainingscale == "1x"): self._trainingsize = self.M + self.N elif (self.trainingscale == "2x"): self._trainingsize = 2 * (self.M + self.N) elif (self.trainingscale == "Cp"): self._trainingsize = len(self._X) self._penaltyparam = kwargs["penaltyparam"] if kwargs.get( "penaltyparam") is not None else 0.0 if (kwargs.get("ppenaltybin") is not None): self._ppenaltybin = kwargs["ppenaltybin"] elif (self.strategy == 1 or self.strategy == 2): raise Exception( "Binary Penalty for numerator required for strategy 1 and 2") if (kwargs.get("qpenaltybin") is not None): self._qpenaltybin = kwargs["qpenaltybin"] elif (self.strategy == 1 or self.strategy == 2): raise Exception( "Binary Penalty for denomintor equired for strategy 1 and 2") # self._struct_p = apprentice.monomialStructure(self.dim, self.m) # self._struct_q = apprentice.monomialStructure(self.dim, self.n) self._ipo = np.empty((self.trainingsize, 2), "object") # Here we set up the recurrence relations for i in range(self.trainingsize): self._ipo[i][0] = self._ONB._recurrence(self._X[i, :], self.M) self._ipo[i][1] = self._ONB._recurrence(self._X[i, :], self.N) start = timer() self.fit() end = timer() self._fittime = end - start
def plotoptdegreecomparison(farr, ts): import json dimarr = np.array([]) real = np.array([]) optdegnonoise = np.array([]) optdeg10_1noise = np.array([]) optdeg10_3noise = np.array([]) optdeg10_6noise = np.array([]) realstr = np.array([]) optdegnonoisestr = np.array([]) optdeg10_1noisestr = np.array([]) optdeg10_3noisestr = np.array([]) optdeg10_6noisestr = np.array([]) for f in farr: """ nonoise """ folder = "%s_%s"%(f,ts) if not os.path.exists(folder): print("Folder '{}' not found.".format(folder)) sys.exit(1) optdegjson = "%s/plots/Joptdeg_%s_jsdump_opt6.json"%(folder,f) if not os.path.exists(optdegjson): print("Optimal degree file '{}' not found.".format(optdegjson)) sys.exit(1) if optdegjson: with open(optdegjson, 'r') as fn: optdegds = json.load(fn) dim = optdegds['dim'] dimarr = np.append(dimarr,dim) m = optdegds['optdeg']['m'] n = optdegds['optdeg']['n'] degstr = optdegds['optdeg']['str'] ncoeffs = tools.numCoeffsPoly(dim,m) + tools.numCoeffsPoly(dim, n) optdegnonoise = np.append(optdegnonoise,ncoeffs) optdegnonoisestr = np.append(optdegnonoisestr,degstr) """ Real """ deg,m,n = getactualdegree(f) ncoeffs = tools.numCoeffsPoly(dim,m) + tools.numCoeffsPoly(dim, n) real = np.append(real,ncoeffs) realstr = np.append(realstr,deg) """ 10-6 noise """ noisestr = "noisepct10-6" folder = "%s_%s_%s"%(f,noisestr,ts) if not os.path.exists(folder): print("Folder '{}' not found.".format(folder)) sys.exit(1) optdegjson = "%s/plots/Joptdeg_%s_%s_jsdump_opt6.json"%(folder,f,noisestr) if not os.path.exists(optdegjson): print("Optimal degree file '{}' not found.".format(optdegjson)) sys.exit(1) if optdegjson: with open(optdegjson, 'r') as fn: optdegds = json.load(fn) dim = optdegds['dim'] m = optdegds['optdeg']['m'] n = optdegds['optdeg']['n'] degstr = optdegds['optdeg']['str'] ncoeffs = tools.numCoeffsPoly(dim,m) + tools.numCoeffsPoly(dim, n) optdeg10_6noise = np.append(optdeg10_6noise,ncoeffs) optdeg10_6noisestr = np.append(optdeg10_6noisestr,degstr) """ 10-3 noise """ noisestr = "noisepct10-3" folder = "%s_%s_%s"%(f,noisestr,ts) if not os.path.exists(folder): print("Folder '{}' not found.".format(folder)) sys.exit(1) optdegjson = "%s/plots/Joptdeg_%s_%s_jsdump_opt6.json"%(folder,f,noisestr) if not os.path.exists(optdegjson): print("Optimal degree file '{}' not found.".format(optdegjson)) sys.exit(1) if optdegjson: with open(optdegjson, 'r') as fn: optdegds = json.load(fn) dim = optdegds['dim'] m = optdegds['optdeg']['m'] n = optdegds['optdeg']['n'] degstr = optdegds['optdeg']['str'] ncoeffs = tools.numCoeffsPoly(dim,m) + tools.numCoeffsPoly(dim, n) optdeg10_3noise = np.append(optdeg10_3noise,ncoeffs) optdeg10_3noisestr = np.append(optdeg10_3noisestr,degstr) """ 10-1 noise """ noisestr = "noisepct10-1" folder = "%s_%s_%s"%(f,noisestr,ts) if not os.path.exists(folder): print("Folder '{}' not found.".format(folder)) sys.exit(1) optdegjson = "%s/plots/Joptdeg_%s_%s_jsdump_opt6.json"%(folder,f,noisestr) if not os.path.exists(optdegjson): print("Optimal degree file '{}' not found.".format(optdegjson)) sys.exit(1) if optdegjson: with open(optdegjson, 'r') as fn: optdegds = json.load(fn) dim = optdegds['dim'] m = optdegds['optdeg']['m'] n = optdegds['optdeg']['n'] degstr = optdegds['optdeg']['str'] ncoeffs = tools.numCoeffsPoly(dim,m) + tools.numCoeffsPoly(dim, n) optdeg10_1noise = np.append(optdeg10_1noise,ncoeffs) optdeg10_1noisestr = np.append(optdeg10_1noisestr,degstr) # '#E6E9ED'"#900C3F", '#C70039', '#FF5733', '#FFC300' act = np.zeros(len(real)) noise0 = np.zeros(len(real)) noise1 = np.zeros(len(real)) noise3 = np.zeros(len(real)) noise6 = np.zeros(len(real)) index = 0 yaxislabels = [""] # sdimarr = np.argsort(dimarr) # currdim = -1 data = [] strarr = [] indarr = [] for currind,f in enumerate(farr): indarr.append(currind) data.append(real[currind]) data.append(optdegnonoise[currind]) data.append(optdeg10_6noise[currind]) data.append(optdeg10_3noise[currind]) data.append(optdeg10_1noise[currind]) strarr.append(realstr[currind]) strarr.append(optdegnonoisestr[currind]) strarr.append(optdeg10_6noisestr[currind]) strarr.append(optdeg10_3noisestr[currind]) strarr.append(optdeg10_1noisestr[currind]) print(data) sdata = np.argsort(data) currdeg = "" for s in sdata: if(currdeg != strarr[s]): index += 1 currdeg = strarr[s] yaxislabels.append(strarr[s]) data[s] = index i=0 j=0 while i<len(data): act[indarr[j]] = data[i] i+=1 noise0[indarr[j]] = data[i] i+=1 noise6[indarr[j]] = data[i] i+=1 noise3[indarr[j]] = data[i] i+=1 noise1[indarr[j]] = data[i] i+=1 j+=1 print(yaxislabels) print(act) print(noise0) print(noise6) print(noise3) print(noise1) import matplotlib as mpl 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) import matplotlib.pyplot as plt width = 0.15 fig, ax = plt.subplots(figsize=(15,10)) X = np.arange(len(farr)) # p1 = ax.bar(X, np.log10(real), width, color='gray') # p2 = ax.bar(X+width, np.log10(optdegnonoise), width, color='#900C3F') # p3 = ax.bar(X+2*width, np.log10(optdeg10_6noise), width, color='#C70039') # p4 = ax.bar(X+3*width, np.log10(optdeg10_3noise), width, color='#FF5733') # p5 = ax.bar(X+4*width, np.log10(optdeg10_1noise), width, color='#FFC300') p1 = ax.bar(X, act, width, color='gray') p2 = ax.bar(X+width, noise0, width, color='#900C3F') p3 = ax.bar(X+2*width, noise6, width, color='#C70039') p4 = ax.bar(X+3*width, noise3, width, color='#FF5733') p5 = ax.bar(X+4*width, noise1, width, color='#FFC300') # for num,p in enumerate(p1.patches): # h = p.get_height() # x = p.get_x()+p.get_width()/2. # print(h,x) # ax.annotate("%s"%(realstr[num]), xy=(x,h), xytext=(0,4), rotation=90, fontsize=8, # textcoords="offset points", ha="center", va="bottom") # # for num,p in enumerate(p2.patches): # h = p.get_height() # x = p.get_x()+p.get_width()/2. # print(h,x) # ax.annotate("%s"%(optdegnonoisestr[num]), xy=(x,h), xytext=(0,4), rotation=90, fontsize=8, # textcoords="offset points", ha="center", va="bottom") # # for num,p in enumerate(p3.patches): # h = p.get_height() # x = p.get_x()+p.get_width()/2. # print(h,x) # ax.annotate("%s"%(optdeg10_6noisestr[num]), xy=(x,h), xytext=(0,4), rotation=90, fontsize=8, # textcoords="offset points", ha="center", va="bottom") # # for num,p in enumerate(p4.patches): # h = p.get_height() # x = p.get_x()+p.get_width()/2. # print(h,x) # ax.annotate("%s"%(optdeg10_3noisestr[num]), xy=(x,h), xytext=(0,4), rotation=90, fontsize=8, # textcoords="offset points", ha="center", va="bottom") # for num,p in enumerate(p5.patches): # h = p.get_height() # x = p.get_x()+p.get_width()/2. # print(h,x) # ax.annotate("%s"%(optdeg10_1noisestr[num]), xy=(x,h), xytext=(0,4), rotation=90, fontsize=8, # textcoords="offset points", ha="center", va="bottom") ax.legend((p1[0], p2[0],p3[0],p4[0],p5[0]), ('Actual orders','$\\epsilon=0$', '$\\epsilon=10^{-6}$','$\\epsilon=10^{-3}$', '$\\epsilon=10^{-1}$')) # ax.set_title('Comparing optimal degree obtained for different types of training data with \'%s\' points'%(ts)) ax.set_xticks(X + 4*width / 2) ax.set_yticks(range(0,index+1)) xlab = [] for f in farr: xlab.append("\\ref{fn:%s}"%(f)) ax.set_xticklabels(xlab) ax.set_yticklabels(yaxislabels) ax.set_xlabel('Function No.') ax.set_ylabel('Order of numerator and denominator polynomials') # ax.autoscale_view() # plt.show() if not os.path.exists("plots"): os.mkdir('plots') outfilepng = "plots/Popdegcmp_%s_%s_ts%s_optimaldegreecomparison.png"%(farr[0],farr[len(farr)-1],ts) # plt.show() # plt.savefig(outfilepng) plt.savefig("plots/Poptdegcomparebarplots.pgf", bbox_inches="tight")
def tabletotalcputime(farr, noisearr, ts, table_or_latex): print(farr) print(noisearr) # allsamples = ['mc','lhs','so','sg'] # allsamples = ['lhs','splitlhs','sg'] # allsamples = ['sg'] # allsamples = ['splitlhs'] # allsamples = ['lhs','splitlhs'] allsamples = ['sg', 'lhs', 'splitlhs'] allsampleslabels = ['SG', 'LHS', 'd-LHD'] import json from apprentice import tools results = {} dumpr = {} for snum, sample in enumerate(allsamples): results[sample] = {} dumpr[sample] = {} for num, fname in enumerate(farr): results[sample][fname] = {} m = 5 n = 5 for noise in noisearr: results[sample][fname][noise] = {} noisestr, noisepct = getnoiseinfo(noise) timepa = [] timera = [] timerard = [] timerasip = [] iterrasip = [] iterrasipnonlog = [] fittime = [] mstime = [] for run in ["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)): 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("rapprdfile %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) dim = getdim(fname) rdof = tools.numCoeffsRapp(dim, [int(m), int(n)]) rpnnl = tools.numCoeffsPoly(dim, m) - (dim + 1) rqnnl = tools.numCoeffsPoly(dim, n) - (dim + 1) if rappsipfile: with open(rappsipfile, 'r') as fn: datastore = json.load(fn) rappsiptime = datastore['log']['fittime'] rnoiters = len(datastore['iterationinfo']) timerasip.append(rappsiptime) # timerasip.append(rappsiptime) iterrasip.append(rnoiters) iterrasipnonlog.append(rnoiters) ft = 0. mt = 0. for iter in datastore['iterationinfo']: ft += iter['log']['time'] mt += iter['robOptInfo']['info'][0]['log']['time'] fittime.append(ft) mstime.append(mt) # fittime.append(ft) # mstime.append(mt) if rappfile: with open(rappfile, 'r') as fn: datastore = json.load(fn) rapptime = datastore['log']['fittime'] timera.append(rapptime) # timera.append(rapptime) if rapprdfile: with open(rapprdfile, 'r') as fn: datastore = json.load(fn) rapprdtime = datastore['log']['fittime'] timerard.append(rapprdtime) # timerard.append(rapprdtime) if pappfile: with open(pappfile, 'r') as fn: datastore = json.load(fn) papptime = datastore['log']['fittime'] pdof = tools.numCoeffsPoly(datastore['dim'], datastore['m']) timepa.append(papptime) # timepa.append(papptime) if (sample == "sg"): break missingmean = -1 if len(timerard) == 0: rapprd = missingmean rapprdsd = 0 else: rapprd = np.average(timerard) rapprdsd = np.std(timerard) if len(timera) == 0: rapp = missingmean rappsd = 0 else: rapp = np.average(timera) rappsd = np.std(timera) if len(timepa) == 0: papp = missingmean pappsd = 0 else: papp = np.average(timepa) pappsd = np.std(timepa) if len(timerasip) == 0: rappsip = missingmean rappsipsd = 0 else: rappsip = np.average(timerasip) rappsipsd = np.std(timerasip) if len(iterrasip) == 0: rnoiters = missingmean rnoiterssd = 0 else: rnoiters = np.average(iterrasip) rnoiterssd = np.std(iterrasip) if len(fittime) == 0: rfittime = missingmean rfittimesd = 0 else: rfittime = np.average(fittime) rfittimesd = np.std(fittime) if len(mstime) == 0: rmstime = missingmean rmstimesd = 0 else: rmstime = np.average(mstime) rmstimesd = np.std(mstime) results[sample][fname][noise] = { "rapprd": rapprd, "rapprdsd": rapprdsd, "rapp": rapp, "rappsd": rappsd, "rappsip": rappsip, "rappsipsd": rappsipsd, "papp": papp, "pappsd": pappsd, 'rnoiters': rnoiters, 'rnoiterssd': rnoiterssd, 'rfittime': rfittime, 'rfittimesd': rfittimesd, 'rmstime': rmstime, 'rmstimesd': rmstimesd, 'pdof': pdof, 'rdof': rdof, 'rpnnl': rpnnl, 'rqnnl': rqnnl } dumpr[sample][fname] = iterrasipnonlog # from IPython import embed # embed() # print(results) ############################################# #iteration summary latex ############################################# # python tabletotalcputime.py f1,f2,f3,f4,f5,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22 0 2x latex noise = noisearr[0] metricarr = ['amean', 'gmean', 'median', 'range'] metricarrlabel = ['Arithmetic Mean', 'Geometric Mean', 'Median', 'Range'] stats = {} s = "" for mnum, metr in enumerate(metricarr): s += "%s" % (metricarrlabel[mnum]) for snum, sample in enumerate(allsamples): data = [] for fnum, fname in enumerate(farr): data.append(results[sample][fname][noise]['rnoiters']) if (fname == 'f20'): print(results[sample][fname][noise]['rnoiters']) print(np.max(data), np.min(data)) stat = getstats(data, metr) s += "&%.2f" % (stat) s += "\n\\\\\\hline\n" print(s) ############################################# #cputime summary latex ############################################# noise = noisearr[0] metricarr = ['amean', 'gmean', 'median', 'range'] metricarrlabel = ['Arithmetic Mean', 'Geometric Mean', 'Median', 'Range'] methodarr = ['papp', 'rapp', 'rapprd', 'rfittime', 'rmstime'] stats = {} sample = 'splitlhs' s = "" for mnum, metr in enumerate(metricarr): s += "%s" % (metricarrlabel[mnum]) for menum, method in enumerate(methodarr): data = [] for fnum, fname in enumerate(farr): data.append(results[sample][fname][noise][method]) stat = getstats(data, metr) s += "&%.2f" % (stat) s += "\n\\\\\\hline\n" print(s) ############################################# #cputime and iteration electronic suppliment latex ############################################# # python tabletotalcputime.py f1,f2,f3,f4,f5,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22 0 2x latex # python tabletotalcputime.py f1,f2,f3,f4,f5,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22 10-6 2x latex # python tabletotalcputime.py f1,f2,f3,f4,f5,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22 10-2 2x latex noise = noisearr[0] s = "" keyarr = ['rapp', 'rapprd', 'rfittime', 'rmstime', 'rnoiters', 'papp'] for fnum, fname in enumerate(farr): s += "\\multirow{3}{*}{\\ref{fn:%s}}" % (fname) for snum, sample in enumerate(allsamples): s += "&%s" % (allsampleslabels[snum]) for knum, key in enumerate(keyarr): statsarr = [key, key + 'sd'] for stnum, stat in enumerate(statsarr): val = results[sample][fname][noise][stat] if sample == 'sg' and stnum == 1: s += "&-" elif val == int(val): s += "&%d" % (int(val)) elif val < 10**-2 or val > 10**2: s += "&%.2E" % (val) else: s += "&%.2f" % (val) if snum < len(allsamples) - 1: s += "\n\\\\* \\cline{2-14}\n" s += "\n\\\\ \\hline\n" print(s) exit(1) import json with open("results/plots/Jiterations.json", "w") as f: json.dump(dumpr, f, indent=4, sort_keys=True) baseline = 2 totalrow = 3 totalcol = 3 import matplotlib.pyplot as plt ffffff = plt.figure(0, figsize=(45, 20)) axarray = [] width = 0.21 ecolor = 'black' X111 = np.arange(len(farr)) meankeyarr = ['papp', 'rapp', 'rapprd', 'rappsip'] sdkeyarr = ['pappsd', 'rappsd', 'rapprdsd', 'rappsipsd'] legendarr = [ 'Polynomial Approx. ', 'Algorithm \\ref{ALG:MVVandQR} without degree reduction', 'Algorithm \\ref{ALG:MVVandQR}', 'MST Algorithm \\ref{A:Polyak}' ] legend2arr = [None, None, None, 'FT Algorithm \\ref{A:Polyak}'] color = ['#FFC300', '#FF5733', '#C70039', '#900C3F'] colorfittime = ['yellow', 'yellow', 'yellow', 'yellow'] props = dict(boxstyle='square', facecolor='wheat', alpha=0.5) plt.rc('ytick', labelsize=20) plt.rc('xtick', labelsize=20) for nnum, noise in enumerate(noisearr): for snum, sample in enumerate(allsamples): mean = {} fitmean = {} for type in meankeyarr: mean[type] = [] fitmean[type] = [] sd = {} fitsd = {} for type in sdkeyarr: sd[type] = [] fitsd[type] = [] for fname in farr: for type in meankeyarr: mean[type].append(results[sample][fname][noise][type]) # mean[type].append(np.ma.log10(results[sample][fname][noise][type])) for type in sdkeyarr: # print(results[sample][fname][noise][type]) sd[type].append(results[sample][fname][noise][type]) # sd[type].append(np.ma.log10(results[sample][fname][noise][type])) for type in meankeyarr: if (type == "rappsip"): fitmean[type].append( results[sample][fname][noise]['rfittime']) else: fitmean[type].append(-1 * baseline) for type in sdkeyarr: if (type == "rappsip"): fitsd[type].append( results[sample][fname][noise]['rfittimesd']) else: fitsd[type].append(0) if (len(axarray) > 0): ax = plt.subplot2grid((totalrow, totalcol), (nnum, snum), sharex=axarray[0], sharey=axarray[0]) axarray.append(ax) else: ax = plt.subplot2grid((totalrow, totalcol), (nnum, snum)) axarray.append(ax) # print(mean) # print(sd) for typenum, type in enumerate(meankeyarr): sdkey = sdkeyarr[typenum] # print(mean[type]) if (sample == 'sg'): ax.bar(X111 + typenum * width, np.array(mean[type]) + baseline, width, color=color[typenum], capsize=3, label=legendarr[typenum]) else: ax.bar(X111 + typenum * width, np.array(mean[type]) + baseline, width, color=color[typenum], yerr=np.array(sd[sdkey]), align='center', ecolor=ecolor, capsize=3) for typenum, type in enumerate(meankeyarr): sdkey = sdkeyarr[typenum] if (sample == 'sg'): ax.bar(X111 + typenum * width, np.array(fitmean[type]) + baseline, width, color=colorfittime[typenum], capsize=3, label=legend2arr[typenum]) else: ax.bar(X111 + typenum * width, np.array(fitmean[type]) + baseline, width, color=colorfittime[typenum], yerr=np.array(fitsd[sdkey]), align='center', ecolor=ecolor, capsize=3) ax.set_xticks(X111 + (len(meankeyarr) - 1) * width / 2) xlab = [] for f in farr: # print(f) # xlab.append("\\ref{fn:%s}"%(f)) # xlab.append("\\ref{%s}"%(f)) xlab.append("%s" % (f)) ax.set_xticklabels(xlab, fontsize=20) ax.set_xlabel("Test functions", fontsize=22) ax.set_ylabel("$\\log_{10}$ [CPU time (sec)]", fontsize=22) ax.label_outer() if (nnum == 0): l1 = ffffff.legend(loc='upper center', ncol=5, fontsize=25, borderaxespad=0., shadow=False) plt.gca().yaxis.set_major_formatter( mtick.FuncFormatter(lambda x, _: x - baseline)) ffffff.savefig("../../log/cputime.png", bbox_extra_artists=(l1, ), bbox_inches='tight') plt.clf() plt.close('all') # Filtered plot totalrow = 3 totalcol = 1 import matplotlib.pyplot as plt ffffff = plt.figure(0, figsize=(45, 20)) X111 = np.arange(len(farr) * len(noisearr)) axarray = [] width = 0.15 ecolor = 'black' plt.rc('ytick', labelsize=20) plt.rc('xtick', labelsize=20) for snum, sample in enumerate(allsamples): mean = {} fitmean = {} for type in meankeyarr: mean[type] = [] fitmean[type] = [] sd = {} fitsd = {} for type in sdkeyarr: sd[type] = [] fitsd[type] = [] for nnum, noise in enumerate(noisearr): for fname in farr: for type in meankeyarr: mean[type].append(results[sample][fname][noise][type]) # mean[type].append(np.ma.log10(results[sample][fname][noise][type])) for type in sdkeyarr: # print(results[sample][fname][noise][type]) sd[type].append(results[sample][fname][noise][type]) # sd[type].append(np.ma.log10(results[sample][fname][noise][type])) for type in meankeyarr: if (type == "rappsip"): fitmean[type].append( results[sample][fname][noise]['rfittime']) else: fitmean[type].append(-1 * baseline) for type in sdkeyarr: if (type == "rappsip"): fitsd[type].append( results[sample][fname][noise]['rfittimesd']) else: fitsd[type].append(0) if (len(axarray) > 0): ax = plt.subplot2grid((totalrow, totalcol), (snum, 0), sharex=axarray[0], sharey=axarray[0]) axarray.append(ax) else: ax = plt.subplot2grid((totalrow, totalcol), (snum, 0)) axarray.append(ax) ax.set_xlim(-.3, 14.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, 9.7, alpha=0.5, color='lightgrey') plt.axvspan(9.7, 14.7, alpha=0.5, color='cyan') for typenum, type in enumerate(meankeyarr): sdkey = sdkeyarr[typenum] # print(mean[type]) if (sample == 'sg'): ax.bar(X111 + typenum * width, np.array(mean[type]) + baseline, width, color=color[typenum], capsize=3, label=legendarr[typenum]) else: ax.bar(X111 + typenum * width, np.array(mean[type]) + baseline, width, color=color[typenum], yerr=np.array(sd[sdkey]), align='center', ecolor=ecolor, capsize=3, label=legendarr[typenum]) for typenum, type in enumerate(meankeyarr): sdkey = sdkeyarr[typenum] if (sample == 'sg'): ax.bar(X111 + typenum * width, np.array(fitmean[type]) + baseline, width, color=colorfittime[typenum], capsize=3, label=legend2arr[typenum]) else: ax.bar(X111 + typenum * width, np.array(fitmean[type]) + baseline, width, color=colorfittime[typenum], yerr=np.array(fitsd[sdkey]), align='center', ecolor=ecolor, capsize=3, label=legend2arr[typenum]) if (snum == 0): l1 = ffffff.legend(loc='upper center', ncol=5, fontsize=25) noiselegendarr = [ '$\\epsilon=0$', '$\\epsilon=10^{-6}$', '$\\epsilon=10^{-2}$' ] l2 = ffffff.legend(noiselegendarr, loc='upper center', ncol=4, bbox_to_anchor=(0.435, 0.85), fontsize=20, borderaxespad=0., shadow=False) ax.set_xticks(X111 + (len(meankeyarr) - 1) * width / 2) xlab = [] for f in farr: # print(f) # xlab.append("\\ref{fn:%s}"%(f)) # xlab.append("\\ref{%s}"%(f)) xlab.append("%s" % (f)) xlab1 = np.concatenate((xlab, xlab, xlab), axis=None) ax.set_xticklabels(xlab1, fontsize=20) ax.set_xlabel("Test functions", fontsize=22) ax.set_ylabel("$\\log_{10}$ [CPU time (sec)]", fontsize=22) ax.label_outer() plt.gca().yaxis.set_major_formatter( mtick.FuncFormatter(lambda x, _: x - baseline)) ffffff.savefig("../../log/cputime2.png", bbox_extra_artists=(l1, ), bbox_inches='tight') plt.clf() plt.close('all') # CPU time plot for paper import matplotlib as mpl # 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) totalrow = 1 totalcol = 1 meankeyarr1 = ['papp', 'rapp', 'rapprd', 'rappsip', 'rfittime'] sdkeyarr1 = ['pappsd', 'rappsd', 'rapprdsd', 'rappsipsd', 'rmstimesd'] # color1 = ['#900C3F','#C70039','#FF5733','#FFC300','pink'] color1 = ["m", "c", "g", "b"] # legendarr1 = ['Polynomial Approximation ','Algorithm \\ref{ALG:MVVandQR} without degree reduction','Algorithm \\ref{ALG:MVVandQR} with degree reduction' ,'Algorithm \\ref{A:Polyak}: fit time','Algorithm \\ref{A:Polyak}: multistart time'] legendarr1 = [ '$p(x)$', '$r_1(x)$', '$r_2(x)$', '$r_3(x)\\mathrm{:\\ multistart\\ time}$', '$r_3(x)\\mathrm{:\\ fit\\ time}$' ] import matplotlib.pyplot as plt from matplotlib.ticker import ScalarFormatter 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") mpl.rc('font', family='serif') ffffff = plt.figure(0, figsize=(15, 10)) X111 = np.arange(len(farr) * len(noisearr)) axarray = [] width = 0.2 ecolor = 'black' plt.rc('ytick', labelsize=20) plt.rc('xtick', labelsize=20) for snum, sample in enumerate(allsamples): mean = {} for type in meankeyarr1: mean[type] = [] sd = {} for type in sdkeyarr1: sd[type] = [] for nnum, noise in enumerate(noisearr): for fname in farr: for type in meankeyarr1: mean[type].append(results[sample][fname][noise][type]) # mean[type].append(np.ma.log10(results[sample][fname][noise][type])) for type in sdkeyarr1: # print(results[sample][fname][noise][type]) sd[type].append(results[sample][fname][noise][type]) # sd[type].append(np.ma.log10(results[sample][fname][noise][type])) if (len(axarray) > 0): ax = plt.subplot2grid((totalrow, totalcol), (snum, 0), sharex=axarray[0], sharey=axarray[0]) axarray.append(ax) else: ax = plt.subplot2grid((totalrow, totalcol), (snum, 0)) axarray.append(ax) # ax.xaxis.set_major_locator(MaxNLocator(integer=True)) # ax.yaxis.set_major_locator(MaxNLocator(integer=True)) # ax.set_xlim(-.3,14.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, 9.7, alpha=0.5, color='lightgrey') # plt.axvspan(9.7, 14.7, alpha=0.5, color='cyan') alignarr = [0.5, 0.5, 0.5, 0.2] for typenum, type in enumerate(meankeyarr1): sdkey = sdkeyarr1[typenum] # print(mean[type]) if (sample == 'sg'): ax.bar(X111 + typenum * width, np.array(mean[type]), width, color=color1[typenum], capsize=3, label=legendarr1[typenum]) else: ax.bar(X111 + typenum * width, np.array(mean[type]), width, color=color1[typenum], alpha=alignarr[typenum], align='center', ecolor=ecolor, capsize=3, label=legendarr1[typenum]) ax.vlines(X111 + typenum * width, np.array(mean[type]), np.array(mean[type]) + np.array(sd[sdkey])) if (typenum == 3): newtn = typenum + 1 newtype = meankeyarr1[newtn] newsdkey = sdkeyarr1[newtn] ax.bar(X111 + typenum * width, np.array(mean[newtype]), width, color=color1[typenum], alpha=0.5, align='center', ecolor=ecolor, capsize=3, label=legendarr1[newtn]) ax.vlines(X111 + typenum * width, np.array(mean[type]), np.array(mean[type]) + np.array(sd[newsdkey])) break if (snum == 0): l1 = ax.legend(loc='upper left', ncol=1, fontsize=20, framealpha=1, shadow=True, frameon=False) l1.get_frame().set_facecolor('white') noiselegendarr = [ '$\\epsilon=0$', '$\\epsilon=10^{-6}$', '$\\epsilon=10^{-2}$' ] # l2 = ffffff.legend(noiselegendarr,loc='upper center', ncol=4,bbox_to_anchor=(0.435, 0.85), fontsize = 20,borderaxespad=0.,shadow=False) ax.set_xticks(X111 + (len(meankeyarr) - 1) * width / 2) ax.set_yscale("log") xlab = [] for f in farr: # print(f) xlab.append("\\ref{fn:%s}" % (f)) # xlab.append("\\ref{%s}"%(f)) # xlab.append("%s"%(f)) # xlab1 = np.concatenate((xlab,xlab,xlab),axis=None) xlab11 = ['$A.1.4$', '$A.1.7$', '$A.1.15$', '$A.1.16$', '$A.1.17$'] ax.set_xticklabels(xlab11, fontsize=20) plt.rc('ytick', labelsize=20) plt.rc('xtick', labelsize=20) plt.tick_params(labelsize=20) # ax.set_xlabel("Test functions",fontsize=22) ax.set_ylabel("$\\mathrm{CPU\\ time\\ (sec)}$", fontsize=20) # for axis in [ax.xaxis, ax.yaxis]: # axis.set_major_formatter(ScalarFormatter()) ax.label_outer() # plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(lambda x,_: x-baseline)) # ffffff.savefig("../../log/cputimeplot.pgf", bbox_extra_artists=(l1,), bbox_inches='tight') # ffffff.savefig("../../log/cputimeplot.png", bbox_extra_artists=(l1,), bbox_inches='tight') ffffff.savefig("../../log/cputimeplot.pdf", bbox_extra_artists=(l1, ), bbox_inches='tight') plt.clf() plt.close('all') exit(1) # Iteration plot import matplotlib as mpl 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") # 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) color = ['#FFC300', '#FF5733', '#900C3F'] X111 = np.arange(len(farr)) ffffff = plt.figure(0, figsize=(15, 10)) # ffffff = plt.figure(0) plt.rc('ytick', labelsize=20) plt.rc('xtick', labelsize=20) totalrow = 1 totalcol = len(noisearr) baseline = 0 width = 0.4 axarray = [] legendarr = [ '$\\mathrm{Latin\\ Hypercube\\ Sampling\\ (LHS)}$', '$\\mathrm{decoupled\\ Latin\\ Hypercube\\ Design\\ (d-LHD)}$' ] for nnum, noise in enumerate(noisearr): mean = {} sd = {} for snum, sample in enumerate(allsamples): mean[sample] = [] sd[sample] = [] for fname in farr: # mean[sample].append(results[sample][fname][noise]['rnoiters']) # sd[sample].append(results[sample][fname][noise]['rnoiterssd']) mean[sample].append(np.average(dumpr[sample][fname])) sd[sample].append(np.std(dumpr[sample][fname])) if (len(axarray) > 0): ax = plt.subplot2grid((totalrow, totalcol), (0, nnum), sharex=axarray[0], sharey=axarray[0]) axarray.append(ax) else: ax = plt.subplot2grid((totalrow, totalcol), (0, nnum)) axarray.append(ax) for snum, sample in enumerate(allsamples): # print(mean[type]) if (sample == 'sg'): ax.bar(X111 + snum * width, np.array(mean[sample]), width, color=color[snum], capsize=3) else: ax.bar(X111 + snum * width, np.array(mean[sample]), width, color=color[snum], align='center', ecolor=ecolor, capsize=3, label=legendarr[snum]) ax.vlines(X111 + snum * width, np.array(mean[sample]), np.array(mean[sample]) + np.array(sd[sample]), label=None) ax.set_xticks(X111 + (len(allsamples) - 1) * width / 2) xlab = [] for f in farr: # print(f) xlab.append("\\ref{fn:%s}" % (f)) # xlab.append("\\ref{%s}"%(f)) # xlab.append("%s"%(f)) xlab = ['$A.1.4$', '$A.1.7$', '$A.1.15$', '$A.1.16$', '$A.1.17$'] ax.set_xticklabels(xlab, fontsize=24) plt.tick_params(labelsize=24) # ax.set_xlabel("Test functions",fontsize=22) # ax.set_ylabel("$\\log_{10}$ [Number of iterations]",fontsize=40) ax.set_ylabel("$\\mathrm{Number\\ of\\ iterations}$", fontsize=28) ax.label_outer() l1 = ax.legend(loc='upper left', ncol=1, fontsize=24, frameon=False) # plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(lambda x,_: x-baseline)) plt.tight_layout() # plt.savefig("../../log/iterations.png") # ffffff.savefig("../../log/iterations.png", bbox_extra_artists=(l1,), bbox_inches='tight') ffffff.savefig("../../log/iterations.pdf", bbox_extra_artists=(l1, ), bbox_inches='tight') plt.clf() plt.close('all') exit(1)
# else: spl += "\t\t" # if ba<=threshold and so3x>threshold: # spl += "********\t" # else: spl += "\t\t" # if ba<=threshold and so4x>threshold: # spl += "********\t" # else: spl += "\t\t" # print("%d\t%d\t%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n%s"%(pdeg,qdeg,iterno,ss,ms,so1x,so2x,so3x,so4x,ba,spl)) print("%d\t%d\t%d\t%f\t%f\t%f\t%f\n%s"%(pdeg,qdeg,iterno,ss,ms,so1x,ba,spl)) from apprentice import tools # tools.numCoeffsPoly(self.dim, self.n) print(tools.numCoeffsPoly(4, 3)) print(tools.numCoeffsPoly(6, 3)) print(tools.numCoeffsPoly(7, 3)) print("----") print(tools.numCoeffsPoly(4, 4)) print(tools.numCoeffsPoly(6, 4)) print(tools.numCoeffsPoly(7, 4)) # print(tools.numCoeffsPoly(23, 2) + tools.numCoeffsPoly(23, 2)) # createTable1("test",'table') createTable1("test",'latex') # createTable2structure() exit(1)
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)