Commit 1d38fda3 authored by ulrich_y's avatar ulrich_y
Browse files

Made strings consitent

parent af1f26ec
......@@ -13,7 +13,7 @@ class PathType(object):
type: file, dir, symlink, None, or a function returning True for
valid paths
None: don't care
dash_ok: whether to allow "-" as stdin/stdout'''
dash_ok: whether to allow '-' as stdin/stdout'''
assert exists in (True, False, None)
assert type in ('file','dir','symlink',None) or \
......@@ -25,7 +25,7 @@ class PathType(object):
def __call__(self, string):
if string == '-':
# the special argument "-" means sys.std{in,out}
# the special argument '-' means sys.std{in,out}
if self._type == 'dir':
raise err('standard input/output (-) not allowed '
'as directory path')
......@@ -38,29 +38,29 @@ class PathType(object):
e = os.path.exists(string)
if self._exists:
if not e:
raise err("path does not exist: '%s'" % string)
raise err('path does not exist: "%s"' % string)
if self._type is None:
pass
elif self._type == 'file':
if not os.path.isfile(string):
raise err("path is not a file: '%s'" % string)
raise err('path is not a file: "%s"' % string)
elif self._type == 'symlink':
if not os.path.symlink(string):
raise err("path is not a symlink: '%s'" % string)
raise err('path is not a symlink: "%s"' % string)
elif self._type == 'dir':
if not os.path.isdir(string):
raise err("path is not a directory: '%s'" % string)
raise err('path is not a directory: "%s"' % string)
elif not self._type(string):
raise err("path not valid: '%s'" % string)
raise err('path not valid: "%s"' % string)
else:
if (not self._exists) and e:
raise err("path exists: '%s'" % string)
raise err('path exists: "%s"' % string)
p = os.path.dirname(os.path.normpath(string)) or '.'
if not os.path.isdir(p):
raise err("parent path is not a directory: '%s'" % p)
raise err('parent path is not a directory: "%s"' % p)
elif not os.path.exists(p):
raise err("parent directory does not exist: '%s'" % p)
raise err('parent directory does not exist: "%s"' % p)
return string
try:
ip = get_ipython()
if ip.__class__.__name__ == 'TerminalInteractiveShell':
ip.magic("pylab")
ip.magic('pylab')
elif ip.__class__.__name__ == 'ZMQInteractiveShell':
ip.magic("pylab notebook")
ip.magic('pylab notebook')
except NameError:
pass
def warn():
import traceback, sys
sys.stderr.write(
"pymule warning: some functionality may not be available\n%s\n" % (
'pymule warning: some functionality may not be available\n%s\n' % (
traceback.format_exc()
)
)
......
......@@ -13,16 +13,16 @@ from matplotlib import rc
# {1}
# ]
schema = [
"#84b3d6", "#f0cd88", "#94c266", "#d58a83", "#9ba7d1"
'#84b3d6', '#f0cd88', '#94c266', '#d58a83', '#9ba7d1'
]
schema = ["#3f88bf", "#e8b145", "#58a010", "#bd483d", "#6376b7"]
schema = ['#3f88bf', '#e8b145', '#58a010', '#bd483d', '#6376b7']
rc('axes', prop_cycle=cycler('color', schema))
orderscheme = {
'lo': 'C2', 'nlo': 'C0', 'nnlo': 'C3', 'np': 'C1'
}
mathcolour = "#00009c"
mathcolour = '#00009c'
defcol = ['C%d' % i for i in range(10)]
......@@ -30,11 +30,11 @@ defcol = ['C%d' % i for i in range(10)]
def alpha_composite(bg, fg, alpha):
import matplotlib.colors
def hex2rgb(h):
h = h.lstrip("#")
h = h.lstrip('#')
return tuple(int(h[i:i+2], 16)/255. for i in (0, 2, 4))
bg = hex2rgb(matplotlib.colors.to_hex(bg))
fg = hex2rgb(matplotlib.colors.to_hex(fg))
return "#%02x%02x%02x" % (
return '#%02x%02x%02x' % (
255.*((1-alpha) * bg[0] + alpha * fg[0]),
255.*((1-alpha) * bg[1] + alpha * fg[1]),
255.*((1-alpha) * bg[2] + alpha * fg[2])
......
......@@ -13,8 +13,8 @@ def _decomp(b):
fp = io.BytesIO(
zlib.decompress(base64.b64decode(b[2:]))
)
if fp.read(4) != b"!boR":
raise KeyError("Header is not matching")
if fp.read(4) != b'!boR':
raise KeyError('Header is not matching')
return fp
......@@ -42,7 +42,7 @@ def _parse(fp):
ans.append(cur[:])
cur = []
else:
raise KeyError("Unknown cmd %s" % cmd)
raise KeyError('Unknown cmd %s' % cmd)
def _flatten(l):
......@@ -53,9 +53,9 @@ def _flatten(l):
return None
if len(l) == 1 and len(l[0]) == 1:
return l[0][0]
if l[0] == "List" and len(l) == 1:
if l[0] == 'List' and len(l) == 1:
return list
if l[0] == "List" and len(l) > 1:
if l[0] == 'List' and len(l) > 1:
return l[1:]
else:
def iflat(ll):
......@@ -85,16 +85,16 @@ def uncompress(b):
def _tests():
tests = [
("1:eJxTTMoPymRkYGAAAAtIAa8=", 1),
("1:eJxTTMoPSmNkYGAoZgESPpnFJZkgHgA3AQQp", [1]),
("1:eJxTTMoPSmNiYGAoZgESPpnFJZmMQEYmSAgATfoElQ==", [1,2]),
('1:eJxTTMoPymRkYGAAAAtIAa8=', 1),
('1:eJxTTMoPSmNkYGAoZgESPpnFJZkgHgA3AQQp', [1]),
('1:eJxTTMoPSmNiYGAoZgESPpnFJZmMQEYmSAgATfoElQ==', [1,2]),
(
"1:eJxTTMoPSmNiYGAoZgESPpnFJai8TEYgIxMkhCbODBIH8QB/Mwpk",
'1:eJxTTMoPSmNiYGAoZgESPpnFJai8TEYgIxMkhCbODBIH8QB/Mwpk',
[[1,2],[3,4]]
),
("1:eJxTTMoPKmIAgw/2ABRsAuY=", 1.),
("1:eJxTTMoPSmNiYGAoZgESPpnFJUUMYPDBHspgcAAAfs4GEw==", [1.,2.]),
("1:eJxTTMoPCmZmYGBITEoGABH9AsE=", "abc")
('1:eJxTTMoPKmIAgw/2ABRsAuY=', 1.),
('1:eJxTTMoPSmNiYGAoZgESPpnFJUUMYPDBHspgcAAAfs4GEw==', [1.,2.]),
('1:eJxTTMoPCmZmYGBITEoGABH9AsE=', 'abc')
]
return all(
......
......@@ -2,7 +2,7 @@ import numpy as np
import os
def read_globaldef(globaldef=""):
def read_globaldef(globaldef=''):
if len(globaldef) == 0:
dir_path = os.path.dirname(os.path.realpath(__file__))
globaldef = os.path.abspath(os.path.join(
......@@ -10,13 +10,13 @@ def read_globaldef(globaldef=""):
))
if not os.path.exists(globaldef):
raise IOError("No such file or directory %s" % globaldef)
raise IOError('No such file or directory %s' % globaldef)
with open(globaldef) as fp:
return [
(n,eval(v))
for n,v in re.findall(
"parameter *:: *([A-Z][A-Z\d]*) = ([\dE\.+/-]+)",
'parameter *:: *([A-Z][A-Z\d]*) = ([\dE\.+/-]+)',
fp.read(),
re.IGNORECASE
)
......
......@@ -15,9 +15,9 @@ except NameError:
def parsestat(s):
return int(s.
replace("k","").
replace("M","000").
replace("G","000000"))
replace('k','').
replace('M','000').
replace('G','000000'))
def getref():
......@@ -27,7 +27,7 @@ def getref():
], stderr=subprocess.PIPE).splitlines()[0]
return rev
except subprocess.CalledProcessError:
return "not on git"
return 'not on git'
def getbranch():
......@@ -37,38 +37,38 @@ def getbranch():
], stderr=subprocess.PIPE).splitlines()[0]
return rev
except subprocess.CalledProcessError:
return "not on git"
return 'not on git'
def rerunconfig(
flavour="mu-e", genprocess="m2enn",
flavour='mu-e', genprocess='m2enn',
seeds=5, xicuts=[.5, .25, .125],
stats={
"0": (10000, 20, 100000, 100),
"V": (10000, 20, 100000, 100),
"F": (10000, 20, 100000, 100),
"C": (10000, 20, 100000, 100),
"R": (20000, 20, 500000, 100)
}, folder="", binary='mcmule'
'0': (10000, 20, 100000, 100),
'V': (10000, 20, 100000, 100),
'F': (10000, 20, 100000, 100),
'C': (10000, 20, 100000, 100),
'R': (20000, 20, 500000, 100)
}, folder='', binary='mcmule'
):
if len(folder) == 0:
folder = genprocess + flavour
cargs = [
"--seeds %s" % ' '.join(str(i) for i in seeds),
"-xi %s" % ' '.join(str(i) for i in xicuts),
"--flavour %s" % flavour,
"--genprocess %s" % genprocess,
"--output-dir %s" % folder,
"--prog %s" % binary
'--seeds %s' % ' '.join(str(i) for i in seeds),
'-xi %s' % ' '.join(str(i) for i in xicuts),
'--flavour %s' % flavour,
'--genprocess %s' % genprocess,
'--output-dir %s' % folder,
'--prog %s' % binary
]
for part, stat in stats.items():
if len(stat) == 5:
cargs.append("--stat %s,%d,%d,%d,%d,%d" % (
cargs.append('--stat %s,%d,%d,%d,%d,%d' % (
part, stat[0], stat[1], stat[2], stat[3], stat[4])
)
else:
cargs.append("--stat %s,%d,%d,%d,%d" % (
cargs.append('--stat %s,%d,%d,%d,%d' % (
part, stat[0], stat[1], stat[2], stat[3])
)
......@@ -76,15 +76,15 @@ def rerunconfig(
def create_menu(
flavour="mu-e", genprocess="m2enn",
flavour='mu-e', genprocess='m2enn',
seeds=5, xicuts=[.5, .25, .125],
stats={
"0": (10000, 20, 100000, 100),
"V": (10000, 20, 100000, 100),
"F": (10000, 20, 100000, 100),
"C": (10000, 20, 100000, 100),
"R": (20000, 20, 500000, 100)
}, folder="", binary='mcmule'
'0': (10000, 20, 100000, 100),
'V': (10000, 20, 100000, 100),
'F': (10000, 20, 100000, 100),
'C': (10000, 20, 100000, 100),
'R': (20000, 20, 500000, 100)
}, folder='', binary='mcmule'
):
totalstat = [0,0,0]
......@@ -96,8 +96,8 @@ def create_menu(
if len(v) == 4:
# number of seeds is missing
if type(seeds) == list:
raise ValueError("Explicit seeds require the number of seeds "
"per collection to be specified!")
raise ValueError('Explicit seeds require the number of seeds '
'per collection to be specified!')
stats[k] = tuple(list(v) + [seeds])
if 'C' in k or 'R' in k or 'F' in k:
......@@ -114,12 +114,12 @@ def create_menu(
seeds.append(r)
elif type(seeds) == list:
if len(seeds) < nseeds:
print("Not enough seeds provided! Will recycle seeds")
print('Not enough seeds provided! Will recycle seeds')
seeds = seeds * (nseeds//len(seeds)+1)
seeds = seeds[:nseeds]
else:
raise TypeError(
"Seeds needs to be either list or int, is type "
'Seeds needs to be either list or int, is type '
+ str(type(seeds))
)
......@@ -130,45 +130,45 @@ def create_menu(
flavour, genprocess, seeds, xicuts,
stats, folder, binary
)
print("Building files. To rerun this, execute")
print("python %s create\\" % sys.argv[0])
print(" \\\n".join(
" " + i for i in rrc
print('Building files. To rerun this, execute')
print('python %s create\\' % sys.argv[0])
print(' \\\n'.join(
' ' + i for i in rrc
))
header = (
"## Generated at %s by %s\n"
"# git version: %s (%s)\n"
"# To re-generate, run python %s create \\\n"
'## Generated at %s by %s\n'
'# git version: %s (%s)\n'
'# To re-generate, run python %s create \\\n'
) % (
datetime.now().strftime("%H:%M on %B %d %Y"), os.environ['USER'],
datetime.now().strftime('%H:%M on %B %d %Y'), os.environ['USER'],
getbranch(), getref(), sys.argv[0]
) + " \\\n".join(
"# " + i for i in rrc
) + ' \\\n'.join(
'# ' + i for i in rrc
)
menu = "\n\nconf %s/%s-%s.conf" % (folder, genprocess, flavour)
config = ("\n\n# specify the program to run relative to `pwd`\n"
"binary=%s\n\n"
"# specify the output folder\n"
"folder=%s/\n\n"
"# Specify the variables nenter_ad, itmx_ad, nenter and itmx\n"
"# for each piece you want to run.\n"
"declare -A STAT=(\n") % (binary, folder)
menu = '\n\nconf %s/%s-%s.conf' % (folder, genprocess, flavour)
config = ('\n\n# specify the program to run relative to `pwd`\n'
'binary=%s\n\n'
'# specify the output folder\n'
'folder=%s/\n\n'
'# Specify the variables nenter_ad, itmx_ad, nenter and itmx\n'
'# for each piece you want to run.\n'
'declare -A STAT=(\n') % (binary, folder)
for part,stat in stats.items():
config += ' ["%s%s"]="%d\\n%d\\n%d\\n%d"\n' % (
config += ' ['%s%s']='%d\\n%d\\n%d\\n%d'\n' % (
genprocess, part, stat[0], stat[1], stat[2], stat[3]
)
if stat[0] < stat[1] or stat[2] < stat[3]:
print("Warning! In part %s, calls and itmx might be swapped!" % (
print('Warning! In part %s, calls and itmx might be swapped!' % (
part
))
xiloop = (
"R" in part or # real corrections
"F" in part or # eikonal subtracted corrections
"C" in part # explicit counter term
'R' in part or # real corrections
'F' in part or # eikonal subtracted corrections
'C' in part # explicit counter term
)
for xi in (xicuts if xiloop else [1.]):
......@@ -177,14 +177,14 @@ def create_menu(
totalstat[1] += 1000*stat[4]*(stat[0]*stat[1] + stat[2]*stat[3])
# runs
totalstat[2] += stat[4]
menu += "\n\n"
menu += '\n\n'
menu += '\n'.join(
"run %d %f %s%s %s 0" % (seeds.pop(), xi, genprocess, part, flavour)
'run %d %f %s%s %s 0' % (seeds.pop(), xi, genprocess, part, flavour)
for i in range(stat[4])
)
menu += "\n\n"
config += ")"
menu += '\n\n'
config += ')'
if totalstat[1] > 999999999999:
c = 'T'
......@@ -198,12 +198,12 @@ def create_menu(
elif totalstat[1] > 999:
c = 'k'
p = float(totalstat[1])/1.e3
print("Expect %d iterations, %f%s calls, and %d runs" % (
print('Expect %d iterations, %f%s calls, and %d runs' % (
totalstat[0], p, c, totalstat[2]
))
return (
[os.path.join(folder, "menu-%s-%s.menu" % (genprocess, flavour)), header + menu],
[os.path.join(folder, "%s-%s.conf" % (genprocess, flavour)), header + config],
[os.path.join(folder, 'menu-%s-%s.menu' % (genprocess, flavour)), header + menu],
[os.path.join(folder, '%s-%s.conf' % (genprocess, flavour)), header + config],
folder
)
......@@ -214,10 +214,10 @@ def interogate(args={}):
def basic_ask(msg, default, parser=(lambda x: x)):
if type(default) == list:
default = ", ".join(str(i) for i in default)
default = ', '.join(str(i) for i in default)
else:
default = str(default)
ans = input("%s? [%s] " % (msg, default))
ans = input('%s? [%s] ' % (msg, default))
if ans == '':
return parser(default)
else:
......@@ -227,28 +227,28 @@ def interogate(args={}):
if key not in args:
args[key] = basic_ask(msg, defaults[key], parser)
ask('genprocess', "What generic process")
ask('flavour', "Which flavour combination")
ask('seeds', "How many / which seeds",
ask('genprocess', 'What generic process')
ask('flavour', 'Which flavour combination')
ask('seeds', 'How many / which seeds',
parser=lambda x: (
int(x) if ',' not in x else [int(i) for i in x.split(',')]
))
ask('xicuts', "Which xi cuts",
ask('xicuts', 'Which xi cuts',
parser=lambda x: [float(i) for i in x.split(',')]
)
defaults['folder'] = args['genprocess'] + args['flavour']
ask('folder', "Where to store data")
ask('binary', "Which binary to run")
ask('folder', 'Where to store data')
ask('binary', 'Which binary to run')
if 'stats' not in args:
pieces = basic_ask(
"Which pieces", ['0', 'F', 'R'],
'Which pieces', ['0', 'F', 'R'],
parser=lambda x: [i.strip() for i in x.split(',')]
)
args['stats'] = {}
for piece in pieces:
args['stats'][piece] = basic_ask(
"How much statistics for " + piece + " (pc, pi, c, i)",
'How much statistics for ' + piece + ' (pc, pi, c, i)',
['10M', '20', '100M', '100'],
parser=lambda x: tuple(parsestat(i) for i in x.split(','))
)
......@@ -271,40 +271,40 @@ def create_parser(subparsers):
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-ns', '--nseeds', type=int,
help="number of random seeds to use [%d]" % defaults['seeds']
help='number of random seeds to use [%d]' % defaults['seeds']
)
group.add_argument('--seeds', nargs='+', type=int, help="list of seeds")
group.add_argument('--seeds', nargs='+', type=int, help='list of seeds')
parser.add_argument(
'-xi', nargs='+', type=float,
help="list of xicut [%s]" % (
help='list of xicut [%s]' % (
','.join(str(i) for i in defaults['xicuts'])
)
)
parser.add_argument(
'-f', '--flavour', type=str,
help="flavour variable [%s]" % defaults['flavour']
help='flavour variable [%s]' % defaults['flavour']
)
parser.add_argument(
'-p', '--genprocess', type=str,
help="the generic process [%s]" % defaults['genprocess']
help='the generic process [%s]' % defaults['genprocess']
)
parser.add_argument(
'-d', '--output-dir', help="output folder, aborts if folder exists"
'-d', '--output-dir', help='output folder, aborts if folder exists'
)
parser.add_argument(
'--force', action='store_true', help="overwrite existing files"
'--force', action='store_true', help='overwrite existing files'
)
parser.add_argument(
'--prog', default='mcmule', help="executable of the MC [mcmule]"
'--prog', default='mcmule', help='executable of the MC [mcmule]'
)
parser.add_argument(
'-s', '--stat', action="append",
help="comma seperated list of piece,pc,pi,c,i[,seeds to use]"
'-s', '--stat', action='append',
help='comma seperated list of piece,pc,pi,c,i[,seeds to use]'
)
parser.set_defaults(func=main)
......@@ -340,7 +340,7 @@ def parseargs(parsed):
if parsed.i:
args = interogate(args)
elif not parsed.output_dir:
parser.error("Check your flags")
parser.error('Check your flags')
return args, parsed.force
......@@ -348,36 +348,36 @@ def parseargs(parsed):
def save(menu, conf, folder, force=False):
toolspath = os.path.dirname(os.path.realpath(__file__))
if os.path.isdir(folder):
print("The folder %s already exists." % folder)
print('The folder %s already exists.' % folder)
if not force:
raise SystemExit("File exists")
raise SystemExit('File exists')
else:
print("Overwritting.")
print('Overwritting.')
else:
os.mkdir(folder)
os.mkdir(os.path.join(folder, "out"))
os.mkdir(os.path.join(folder, 'out'))
if 'afs' in toolspath:
print("You are running on AFS, won't fix permissions")
print('You are running on AFS, won\'t fix permissions')
else:
os.chmod(folder, 0o2775)
os.chmod(os.path.join(folder, "out"), 0o2775)
os.chmod(os.path.join(folder, 'out'), 0o2775)
with open(os.path.join(toolspath, "submit.sh")) as fp:
with open(os.path.join(toolspath, 'submit.sh')) as fp:
submit = fp.read()
submit = re.sub(
"^#SBATCH --output=.*/slurm-%j.out$",
"#SBATCH --output=%s/slurm-%%j.out" % folder,
'^#SBATCH --output=.*/slurm-%j.out$',
'#SBATCH --output=%s/slurm-%%j.out' % folder,
submit, flags=re.M
)
submit = re.sub(
"^#SBATCH --input=.*$",
"#SBATCH --input=%s" % menu[0],
'^#SBATCH --input=.*$',
'#SBATCH --input=%s' % menu[0],
submit, flags=re.M
)
submit = re.sub(
"^this=\./submit\.sh$",
"this=%s/submit.sh" % folder,
'^this=\./submit\.sh$',
'this=%s/submit.sh' % folder,
submit, flags=re.M
)
......@@ -385,14 +385,14 @@ def save(menu, conf, folder, force=False):
fp.write(menu[1])
with open(conf[0], 'w') as fp:
fp.write(conf[1])
with open(os.path.join(folder, "submit.sh"), 'w') as fp:
with open(os.path.join(folder, 'submit.sh'), 'w') as fp:
fp.write(submit)
os.chmod(menu[0], 0o664)
os.chmod(os.path.join(folder, "submit.sh"), 0o775)
os.chmod(os.path.join(folder, 'submit.sh'), 0o775)
print("Created menu, config and submit script in %s." % folder)
print("Please change the ntasks and time options accordingly")
print('Created menu, config and submit script in %s.' % folder)
print('Please change the ntasks and time options accordingly')
def main(parsed):
......
......@@ -270,7 +270,7 @@ def printnumber(x, prec=0):
digits = int(-np.floor(np.log10(x[1]))) + prec
fmt = "%." + str(digits) + "f(%." + str(prec) + "f)"
fmt = '%.' + str(digits) + 'f(%.' + str(prec) + 'f)'
return fmt % (
round(x[0], digits),
10**digits*round