diff --git a/benchmarks/__init__.py b/benchmarks/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/benchmarks/calc_signal_benchmark.py b/benchmarks/calc_signal_benchmark.py index a0dcf9df0..baa10cfe8 100644 --- a/benchmarks/calc_signal_benchmark.py +++ b/benchmarks/calc_signal_benchmark.py @@ -42,10 +42,10 @@ # Defining defaults ################### -_LRES = [-3, -3, 0] -_LLOS = [5, 5, 0] -_LT = [3, 3, 0] -_NREP = 2 +_LRES = [-2, -1, 0] +_LLOS = [2, 3, 0] +_LT = [2, 2, 0] +_NREP = 1 _DRES = abs(_LRES[1] - _LRES[0]) _DLOS = abs(_LLOS[1] - _LLOS[0]) @@ -147,23 +147,26 @@ def benchmark(config=None, func=_FUNC, plasma=None, shot=None, ids=None, ('nres',nres), ('nnt',nnt), ('Host',socket.gethostname()), ('USR',getpass.getuser())] name = 'benchmark_LOScalcsignal_' - name += '_'.join(['%s%s'%(nn,vv) + name += '_'.join(['{}{}'.format(nn, vv) for nn,vv in lvar]) if nameappend is not None: name += '_'+nameappend # printing file stdout = False + msg_loc = "\ntofu {} loaded from:\n {}\n".format(tfversion, tforigin) if txtfile is None: txtfile = sys.stdout stdout = True + print(msg_loc) elif type(txtfile) is str: - txtfile = open(os.path.join(path,txtfile), 'w') + txtfile = os.path.join(path, txtfile) + with open(txtfile, 'w') as f: + f.write(msg_loc) elif txtfile is True: - txtfile = open(os.path.join(path,name+'.txt'), 'w') - msg_loc = "\ntofu %s loaded from:\n %s\n"%(tfversion,tforigin) - print(msg_loc, file=txtfile) - + txtfile = os.path.join(path, name+'.txt') + with open(txtfile, 'w') as f: + f.write(msg_loc) # config if config is None: @@ -210,42 +213,62 @@ def benchmark(config=None, func=_FUNC, plasma=None, shot=None, ids=None, #------------ # Start loop - names = np.array([['%s los = %s'%(lalgo[ii],int(nlos[jj])) + names = np.array([['{} los = {}'.format(lalgo[ii], int(nlos[jj])) for jj in range(nnlos)] for ii in range(nalgo)]) lennames = np.max(np.char.str_len(names)) msg = "\n###################################"*2 msg += "\nBenchmark about to be run with:" msg += "\n-------------------------------\n\n" - msg += "lalgo = %s\n"%str(lalgo) - msg += "nlos = %s\n"%str(nlos) - msg += "res = %s\n"%str(res) - msg += "nt = %s\n"%str(nt) - msg += "rep = %s\n\n"%str(nrep) - msg += " algo:".ljust(lennames) + ' times:' - print(msg, file=txtfile) + msg += "lalgo = {}\n".format(lalgo) + msg += "nlos = {}\n".format(nlos) + msg += "res = {}\n".format(res) + msg += "nt = {}\n".format(nt) + msg += "rep = {}\n\n".format(nrep) + msg += " algo:" + msg = msg.ljust(lennames) + msg += ' times:' + if stdout: + print(msg) + else: + with open(txtfile, 'w') as f: + f.write(msg) err0 = None for ii in range(nalgo): - print('', file=txtfile, flush=True) + if stdout: + print('') + sys.stdout.flush() + else: + with open(txtfile, 'w') as f: + f.write(msg) for jj in range(nnlos): cam = tf.geom.utils.create_CamLOS1D(N12=nlos[jj], config=config, - Name=str(names[ii,jj]), Exp='dummy', + Name=str(names[ii, jj]), + Exp='dummy', Diag='Dummy', **_DCAM) - msg = " %s"%(names[ii,jj].ljust(lennames)) - print(msg, file=txtfile, flush=True) - + msg = " {}".format(names[ii, jj].ljust(lennames)) + if stdout: + print(msg) + sys.stdout.flush() + else: + with open(txtfile, 'w') as f: + f.write(msg) for ll in range(nres): - msg = " res %s/%s"%(ll+1, nres) + msg = " res {}/{}".format(ll + 1, nres) for tt in range(nnt): dt = np.zeros((nrep,)) for rr in range(nrep): if stdout: - msgi = "\r" + msg + " nt %s/%s rep %s/%s"%(tt+1,nnt,rr+1,nrep) - print(msgi, end='', file=txtfile, flush=True) - + msgi = "\r" + msg \ + + " nt {}/{} rep {}/{}".format(tt + 1, + nnt, + rr + 1, + nrep) + print(msgi, end='') + sys.stdout.flush() try: if func is None: t0 = dtm.datetime.now() @@ -276,14 +299,23 @@ def benchmark(config=None, func=_FUNC, plasma=None, shot=None, ids=None, t_av[ii,jj,ll,tt] = np.mean(dt) t_std[ii,jj,ll,tt] = np.std(dt) - msgi = msg + ': %s'%str(t_av[ii,jj,ll,:]) + msgi = msg + ': {}'.format(t_av[ii, jj, ll, :]) if stdout: msgi = '\r'+msgi - print(msgi, file=txtfile, flush=True) + print(msgi) + sys.stdout.flush() + else: + with open(txtfile, 'w') as f: + f.write(msgi) + if save: out = {kk:vv for kk,vv in locals().items() if kk in lk} np.savez(pfe, **out) - print(" (saved)", file=txtfile) + if stdout: + print(" (saved)") + else: + with open(txtfile, 'w') as f: + f.write(" (saved)") @@ -295,37 +327,42 @@ def benchmark(config=None, func=_FUNC, plasma=None, shot=None, ids=None, ln = np.max([len(aa) for aa in lalgo]) msg = "\n --------------------\n --- Synthesis ---" msg += "\n\n Speed score:\n " - msg += "\n ".join(["%s : %s"%(lalgo[ii].ljust(ln), + msg += "\n ".join(["{} : {}".format(lalgo[ii].ljust(ln), 100.*np.sum(win==ii)/ncase) +' %' for ii in range(nalgo)]) winname = np.char.rjust(np.asarray(lalgo)[win], ln) - lsblocks = ['nlos = %s'%str(nlos[jj]) + "\n " - + "\n ".join([('res %s/%s '%(ll,nres) - + str(winname[jj,ll,:])) - for ll in range(nres)]) + lsblocks = ['nlos = {}'.format(nlos[jj]) + "\n " + + "\n ".join([('res {}/{} '.format(ll, nres) + + str(winname[jj, ll, :])) + for ll in range(nres)]) for jj in range(nnlos)] msg += "\n" + "\n " + "\n ".join(lsblocks) msg += "\n\n Memory score:\n " - msg += "\n ".join(["%s : %s"%(lalgo[ii].ljust(ln), + msg += "\n ".join(["{} : {}".format(lalgo[ii].ljust(ln), 100.*np.sum(t_av[ii,...]>=0)/ncase) + ' %' for ii in range(nalgo)]) - print(msg, file=txtfile) - - + if stdout: + print(msg) + else: + with open(txtfile, 'w') as f: + f.write(msg) if err0 is not None: raise err0 #------------- # Plot / save - if save: out = {kk:vv for kk,vv in locals().items() if kk in lk} np.savez(pfe, **out) - print('Saved in:\n %s'%pfe, file=txtfile) + if stdout: + print('Saved in:\n {}'.format(pfe)) + else: + with open(txtfile, 'w') as f: + f.write('Saved in:\n {}'.format(pfe)) if plot: try: