def mkBestRA(X, Y, pnames, split=0.7, norm=2, m_max=5, n_max=None, f_plot=None): """ """ _N, _dim = X.shape i_train = sorted( list(np.random.choice(range(_N), int(np.ceil(split * _N))))) i_test = [i for i in range(_N) if not i in i_train] N_train = len(i_train) N_test = len(i_test) orders = apprentice.tools.possibleOrders(N_train, _dim, mirror=True) if n_max is not None: orders = [o for o in orders if o[1] <= n_max and o[0] <= m_max] # d_RA = { o : apprentice.RationalApproximation(X[i_train], Y[i_train], order=o, pnames=pnames) for o in orders } d_RA = {} for o in orders: if o[1] == 0: d_RA[o] = apprentice.PolynomialApproximation(X[i_train], Y[i_train], order=o[0], pnames=pnames) else: d_RA[o] = apprentice.RationalApproximation(X[i_train], Y[i_train], order=o, pnames=pnames) d_norm = {o: raNorm(d_RA[o], X[i_test], Y[i_test]) for o in orders} import operator sorted_norm = sorted(d_norm.items(), key=operator.itemgetter(1)) if f_plot is not None: mkPlotNorm(d_norm, f_plot, norm) winner = sorted_norm[0] print("Winner: m={} n={} with L2={}".format(*winner[0], winner[1])) return apprentice.RationalApproximation(X, Y, order=winner[0], pnames=pnames)
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 readApprentice(fname): """ Read an apprentice JSON file. We abuse try except here to figure out whether it's a rational or polynomial approximation. """ import apprentice import os if not os.path.exists(fname): raise Exception("File {} not found".format(fname)) try: app = apprentice.RationalApproximation(fname=fname) except: app = apprentice.PolynomialApproximation(fname=fname) return app
def readApprox(fname, set_structures=True, usethese=None): import json, apprentice with open(fname) as f: rd = json.load(f) binids = app.tools.sorted_nicely(rd.keys()) binids = [x for x in binids if not x.startswith("__")] if usethese is not None: binids = [x for x in binids if x in usethese] APP = {} for b in binids: if "n" in rd[b]: APP[b] = apprentice.RationalApproximation( initDict=rd[b] ) # FIXME what about set_structures for rationals? else: APP[b] = apprentice.PolynomialApproximation( initDict=rd[b], set_structures=set_structures) return binids, [APP[b] for b in binids]
binids = [s.decode() for s in f.get("index")[idx]] t2 = time.time() print("Data preparation took {} seconds".format(t2 - t1)) ras = [] scl = [] t1 = time.time() for num, (X, Y) in enumerate(DATA): # t11=time.time() # ras.append(mkBestRA(X,Y, pnames, n_max=3))#, f_plot="{}.pdf".format(binids[num].replace("/","_")))) # t22=time.time() # print("Approximation {}/{} took {} seconds".format(num+1,len(binids), t22-t11)) try: ras.append( apprentice.RationalApproximation(X, Y, order=(2, 0), pnames=pnames)) except Exception as e: print("Problem with {}".format(binids[num])) pass t2 = time.time() print("Approximation took {} seconds".format(t2 - t1)) # This reads the unique identifiers of the bins # jsonify # The decode deals with the conversion of byte string atributes to utf-8 JD = {x: y.asDict for x, y in zip(binids, ras)} import json with open(sys.argv[2], "w") as f: json.dump(JD, f, indent=4)
def readApprox(fname): with open(fname) as f: rd = json.load(f) binids = sorted(rd.keys()) RA = [apprentice.RationalApproximation(initDict=rd[b]) for b in binids] return binids, RA
import apprentice if __name__ == "__main__": import sys D = apprentice.tools.readH5(sys.argv[1], []) # This reads all bins # D = apprentice.readH5(sys.argv[1], [0,1]) # This reads only the first 2 bins R = [apprentice.RationalApproximation(*d) for d in D]
def calcApprox(X, Y, order, pnames, mode="sip", onbtol=-1, debug=False, testforPoles=100, ftol=1e-9, itslsqp=200): M, N = order import apprentice as app if N == 0: _app = app.PolynomialApproximation(X, Y, order=M, pnames=pnames) hasPole = False else: if mode == "la": _app = app.RationalApproximation(X, Y, order=(M, N), pnames=pnames, strategy=2) elif mode == "onb": _app = app.RationalApproximationONB(X, Y, order=(M, N), pnames=pnames, tol=onbtol, debug=debug) elif mode == "sip": try: _app = app.RationalApproximationSLSQP(X, Y, order=(M, N), pnames=pnames, debug=debug, ftol=ftol, itslsqp=itslsqp) except Exception as e: print("Exception:", e) return None, True elif mode == "lasip": try: _app = app.RationalApproximation(X, Y, order=(M, N), pnames=pnames, strategy=2, debug=debug) except Exception as e: print("Exception:", e) return None, True has_pole = denomChangesSignMS(_app, 100)[0] if has_pole: try: _app = app.RationalApproximationSLSQP(X, Y, order=(M, N), pnames=pnames, debug=debug, ftol=ftol, itslsqp=itslsqp) except Exception as e: print("Exception:", e) return None, True else: raise Exception( "Specified mode {} does not exist, choose la|onb|sip".format( mode)) hasPole = denomChangesSignMS(_app, testforPoles)[0] return _app, hasPole
t1 = time.time() Y = [] NSAMPLES = int(sys.argv[2]) X = [] for _ in range(NSAMPLES): rans = [np.random.rand() for i in range(0, 3 * NP - 4 + 2)] X.append(rans[0:5]) moms = generate_point(pa, pb, rans) Y.append(ME_PLB(moms)) t2 = time.time() print("Generation of {} configuration took {} seconds".format( NSAMPLES, t2 - t1)) import apprentice t1 = time.time() apprentice.RationalApproximation(X, Y, order=(5, 5), strategy=3) t2 = time.time() print("Approximation took {} seconds".format(t2 - t1)) # from IPython import embed # embed() # import matplotlib.pyplot as plt # plt.style.use("ggplot") # plt.xlabel("$\log_{10}(ME)$") # plt.hist(np.log10(Y), bins=51,histtype='step', label="Exact") # plt.yscale("log") # import apprentice # S=apprentice.Scaler(X) # XX = S.scale(X)
def mkBestRACPL(X, Y, pnames=None, train_fact=2, split=0.6, norm=2, m_max=None, n_max=None, f_plot=None, seed=1234, allow_const=False, debug=0): """ """ import apprentice _N, _dim = X.shape np.random.seed(seed) # Split dataset in training and test sample i_train = sorted( list(np.random.choice(range(_N), int(np.ceil(split * _N))))) i_test = [i for i in range(_N) if not i in i_train] N_train = len(i_train) N_test = len(i_test) orders = sorted(apprentice.tools.possibleOrders(N_train, _dim, mirror=True)) if not allow_const: orders = orders[1:] if n_max is not None: orders = [o for o in orders if o[1] <= n_max] if m_max is not None: orders = [o for o in orders if o[0] <= m_max] # Discard those orders where we do not have enough training points if train_fact>1 if train_fact > 1: _temp = [] for o in orders: if o[1] > 0: if train_fact * apprentice.tools.numCoeffsRapp(_dim, o) <= N_train: _temp.append(o) else: if train_fact * apprentice.tools.numCoeffsPoly( _dim, o[0]) <= N_train: _temp.append(o) orders = sorted(_temp) APP = [] # print("Calculating {} approximations".format(len(orders))) import time t1 = time.time() for o in orders: m, n = o if n == 0: i_train_o = np.random.choice( i_train, int(train_fact * apprentice.tools.numCoeffsPoly(_dim, m))) APP.append( apprentice.PolynomialApproximation(X[i_train_o], Y[i_train_o], order=m, pnames=pnames)) else: i_train_o = np.random.choice( i_train, int(train_fact * apprentice.tools.numCoeffsRapp(_dim, (m, n)))) APP.append( apprentice.RationalApproximation(X[i_train_o], Y[i_train_o], order=(m, n), strategy=1, pnames=pnames)) t2 = time.time() print("Calculating {} approximations took {} seconds".format( len(orders), t2 - t1)) L2 = [np.sqrt(raNorm(app, X, Y, 2)) for app in APP] Linf = [raNormInf(app, X, Y) for app in APP] NNZ = [apprentice.tools.numNonZeroCoeff(app, 1e-6) for app in APP] VAR = [l / m for l, m in zip(L2, NNZ)] ncN, ncM = [], [] NC = [] for m, n in orders: ncM.append(apprentice.tools.numCoeffsPoly(_dim, m)) ncN.append(apprentice.tools.numCoeffsPoly(_dim, n)) if n == 0: NC.append(apprentice.tools.numCoeffsPoly(_dim, m)) else: NC.append(apprentice.tools.numCoeffsRapp(_dim, (m, n))) # currently, this zips the number of coefficients for P and Q, the L2 norm divided by the number of non-zero # coefficients and for convenients the orders of the polynomials D3D = np.array([(m, n, v, o[0], o[1]) for m, n, v, o in zip(ncM, ncN, VAR, orders)]) # D3D = np.array([(o[0],o[1],v) for o,v in zip(orders, VAR)]) mkPlotParetoSquare(D3D, "paretomn.pdf") # import matplotlib as mpl # import matplotlib.pyplot as plt # from mpl_toolkits.mplot3d import Axes3D # plt.clf() # mpl.rc('text', usetex = True) # mpl.rc('font', family = 'serif', size=12) # mpl.style.use("ggplot") # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # p3D = is_pareto_efficient_dumb(D3D) # # # from IPython import embed # # # embed() # for num, (m,n,v) in enumerate(zip(ncM,ncN, VAR)): # if p3D[num]: # ax.scatter(m, n, np.log10(v), c="gold") # else: # ax.scatter(m, n, np.log10(v), c="r") # ax.set_xlabel('nc m') # ax.set_ylabel('nc n') # ax.set_zlabel('log v') # plt.show() # sys.exit(1) NNC = [] for num, n in enumerate(NC): NNC.append(n - NNZ[num]) CMP = [a * b for a, b in zip(NNZ, L2)] if f_plot: # mkPlotCompromise([(a,b) for a, b in zip(NNZ, L2)], f_plot, orders, ly="$L_2^\\mathrm{test}$", lx="$N_\\mathrm{non-zero}$", logx=False) # mkPlotCompromise([(a,b) for a, b in zip(NNZ, VAR)], "VAR_{}".format(f_plot), orders, ly="$\\frac{L_2^\\mathrm{test}}{N_\mathrm{non-zero}}$", lx="$N_\\mathrm{non-zero}$", logy=True, logx=False) # mkPlotCompromise([(a,b) for a, b in zip(NC, VAR)], "NCVAR_{}".format(f_plot), orders, ly="$\\frac{L_2^\\mathrm{test}}{N_\mathrm{non-zero}}$", lx="$N_\\mathrm{coeff}$", logy=True, logx=True, normalize_data=False) mkPlotCompromise2( [(a, b) for a, b in zip(NC, VAR)], "NCVAR_{}".format(f_plot), orders, ly="$\\frac{L_2^\\mathrm{test}}{N_\mathrm{non-zero}}$", lx="$N_\\mathrm{coeff}$", logy=True, logx=True, normalize_data=False) # mkPlotCompromise([(a,b) for a, b in zip(NNC, VAR)], "NNCVAR_{}".format(f_plot), orders, ly="$\\frac{L_2^\\mathrm{test}}{N_\mathrm{non-zero}}$", lx="$N_\\mathrm{coeff}-N_\mathrm{non-zero}$", logy=True, logx=True) # Proactive memory cleanup del APP for l in sorted(CMP)[0:debug]: i = CMP.index(l) oo = orders[i] print("{} -- L2: {:10.4e} | Loo: {:10.4e} | NNZ : {} | VVV : {:10.4e}". format(oo, L2[i], Linf[i], NNZ[i], VAR[i])) for l in sorted(CMP): i = CMP.index(l) oo = orders[i] # print("{} -- L2: {:10.4e} | Loo: {:10.4e}".format(oo, L2[i], Linf[i])) # If it is a polynomial we are done --- return the approximation that uses all data if oo[1] == 0: return apprentice.PolynomialApproximation(X, Y, order=oo[0], pnames=pnames) else: APP_test = apprentice.RationalApproximation(X, Y, order=oo, strategy=1, pnames=pnames) bad = denomChangesSign(APP_test, APP_test._scaler.box, APP_test._scaler.center)[0] if bad: print("Cannot use {} due to pole in denominator".format(oo)) else: return APP_test
def mkBestRASIP(X, Y, pnames=None, train_fact=1, split=0.5, norm=2, m_max=None, n_max=None, f_plot=None, seed=1234, use_all=False): """ """ import apprentice np.random.seed(seed) _N, _dim = X.shape # Split dataset in training and test sample i_train = sorted( list(np.random.choice(range(_N), int(np.ceil(split * _N))))) i_test = [i for i in range(_N) if not i in i_train] N_train = len(i_train) N_test = len(i_test) orders = sorted(apprentice.tools.possibleOrders(N_train, _dim, mirror=True)) if n_max is not None: orders = [o for o in orders if o[1] <= n_max] if m_max is not None: orders = [o for o in orders if o[0] <= m_max] # Discard those orders where we do not have enough training points if train_fact>1 if train_fact > 1: _temp = [] for o in orders: if o[1] > 0: if train_fact * apprentice.tools.numCoeffsRapp(_dim, o) <= N_train: _temp.append(o) else: if train_fact * apprentice.tools.numCoeffsPoly( _dim, o[0]) <= N_train: _temp.append(o) orders = sorted(_temp) APP, APPcpl = [], [] # print("Calculating {} approximations".format(len(orders))) import time t1 = time.time() for o in orders: m, n = o if n == 0: i_train_o = np.random.choice( i_train, int(train_fact * apprentice.tools.numCoeffsPoly(_dim, m))) APP.append( apprentice.PolynomialApproximation(X[i_train_o], Y[i_train_o], order=m)) APPcpl.append( apprentice.PolynomialApproximation(X[i_train], Y[i_train], order=m)) else: i_train_o = np.random.choice( i_train, int(train_fact * apprentice.tools.numCoeffsRapp(_dim, (m, n)))) APP.append( apprentice.RationalApproximation(X[i_train_o], Y[i_train_o], order=(m, n), strategy=1)) APPcpl.append( apprentice.RationalApproximation(X[i_train], Y[i_train], order=(m, n), strategy=1)) # print("I used a training size of {}/{} available training points for order {}".format(len(i_train_o), len(i_train), o)) t2 = time.time() print("Calculating {} approximations took {} seconds".format( len(orders), t2 - t1)) L2 = [np.sqrt(raNorm(app, X[i_test], Y[i_test], 2)) for app in APP] L2cpl = [np.sqrt(raNorm(app, X[i_test], Y[i_test], 2)) for app in APPcpl] Linf = [raNormInf(app, X[i_test], Y[i_test]) for app in APP] # Find the order that gives the best L2 norm winner = L2.index(min(L2)) runnerup = L2.index(sorted(L2)[1]) winnerinf = Linf.index(min(Linf)) o_win = orders[winner] o_rup = orders[runnerup] APP_win = APP[winner] APP_rup = APP[runnerup] # Confirm using all training data temp = apprentice.RationalApproximation(X[i_train], Y[i_train], order=o_win, strategy=1) l2temp = np.sqrt(raNorm(temp, X[i_test], Y[i_test], 2)) # print("Winner: {} with L2 {} and L2 complete = {}".format( o_win, min(L2), l2temp)) print("Winnerinf: {} with Loo {}".format(orders[winnerinf], min(Linf))) for l in sorted(L2)[0:4]: i = L2.index(l) print("{} -- L2: {:10.2e} || {:10.2e} -- Loo: {:10.2e}".format( orders[i], l, L2cpl[i], Linf[i])) # If it is a polynomial we are done if o_win[1] == 0: return APP_win # Let's check for poles in the denominator isbad = denomChangesSign(APP_win, APP_win._scaler.box, APP_win._scaler.center)[0] if isbad: print("Can't use this guy {} (L2: {:10.2e})".format(o_win, min(L2))) _l2 = min(L2) for l in sorted(L2)[1:]: i = L2.index(l) print("Testing {} (L2: {:10.2e})".format(orders[i], l)) if orders[i][1] == 0: print("This is a polynomial,done") return APP[i] else: bad = denomChangesSign(APP[i], APP[i]._scaler.box, APP[i]._scaler.center)[0] if bad: ("Cannot use {} either".format(orders[i])) else: return APP[i] # if rupisbad: # print("This guy works though{}".format(o_rup)) # else: # print("Need to fix also this guy {}".format(o_rup)) # FS = ["filter", "scipy"] # FS = ["scipy"] # RS = ["ms"] # import json # for fs in FS: # for rs in RS: # rrr = apprentice.RationalApproximationSIP(X[i_train], Y[i_train], # m=o_win[0], n=o_win[1], pnames=pnames, fitstrategy=fs, trainingscale="Cp", # roboptstrategy=rs) # print("Test error FS {} RS {}: 1N:{} 2N:{} InfN:{}".format(fs, rs, # raNorm(rrr, X[i_test], Y[i_test],1), # np.sqrt(raNorm(rrr, X[i_test], Y[i_test],2)), # raNormInf(rrr, X[i_test], Y[i_test]))) # print("Total Approximation time {}\n".format(rrr.fittime)) # print("{}".format(denomChangesSign(rrr, rrr._scaler.box, rrr._scaler.center))) # # with open("test2D_{}_{}.json".format(fs,rs), "w") as f: json.dump(rrr.asDict, f, indent=4) # return rrr else: return APP_win