Page MenuHomeHEPForge

No OneTemporary

diff --git a/bin/prof2-sobol b/bin/prof2-sobol
deleted file mode 100755
--- a/bin/prof2-sobol
+++ /dev/null
@@ -1,165 +0,0 @@
-#! /usr/bin/env python
-
-"""\
-%prog [-o out1] [-t template1.txt -t ...] PARAM1:low1:high1 PARAM2:low2:high2:'exp(x)'
-or
-%prog [-o out1] [-t template1.txt -t ...] myparamfile
-
-Sample a parameter space, creating a set of parameter files and optionally
-substituting into script templates, with either flat sampling (default) or
-sampling in a transformed space.
-
-Parameter ranges (and bias functions) can either be given inline on the command line,
-with the name, low..high range, and optional bias function separated by colons (:),
-or in a parameter range file with one parameter per line and whitespace separation of
-the name, low, high, and bias terms.
-
-TODO:
- * copy the file mode (esp. executable) from the template to each instantiation
-"""
-
-import optparse
-op = optparse.OptionParser(usage=__doc__)
-op.add_option("-n", dest="NUMPOINTS", metavar="NUM", type=int, default=100, help="number of samples to generate [default=%default]")
-op.add_option("-t", dest="TEMPLATES", metavar="FILE", action="append", default=[], help="specify a template file to be populated for each sample point. Can be given multiple times. Strings in curly braces are instantiated.")
-op.add_option("-o", "--outdir", dest="OUTDIR", metavar="DIR", default="scan", help="specify the output directory name (default: %default)")
-op.add_option("-O", "--outmode", dest="OUTMODE", metavar="MODE", default="hier", help="specify the output structuring mode: either 'hier' (default) or 'flat' to respectively use run subdirs or not or 'table' to create one text file with a table like structure")
-op.add_option("-p", "--pfile", dest="PARAMSFILE", metavar="FILE", default="params.dat", help="specify params file base name to be populated for each sample point")
-op.add_option("-P", "--no-pfile", dest="PARAMSFILE", action="store_const", const=None, help="do not write a params file for each sample point")
-op.add_option("-s", "--seed", dest="SEED", metavar="VAL", default=None, help="specify a random seed for the sampler (default: use system time)")
-op.add_option("--veto", dest="VETOFN", metavar="FILE", default=None, help="specify a file from which to read the definition of a Python sample point-vetoing function, prof_sample_veto(paramsdict). Return True means 'veto point'")
-op.add_option("-v", "--debug", dest="DEBUG", action="store_true", default=False, help="turn on some debug messages")
-op.add_option("-q", "--quiet", dest="QUIET", action="store_true", default=False, help="turn off messages")
-opts, args = op.parse_args()
-assert opts.OUTMODE in ("hier", "flat", "table")
-
-import os
-
-def mkrunstr(num):
- return "{run:04d}".format(run=num)
-
-def mkoutname(fname, run, prefix="", suffix=""):
- if type(run) is int:
- run = mkrunstr(run)
- if opts.OUTMODE == "hier":
- name = os.path.join(run, fname)
- elif opts.OUTMODE == "flat":
- fname = os.path.basename(fname)
- base, ext = os.path.splitext(fname)
- name = prefix + base + "-" + run + suffix + ext
- print "Name:", name
- elif opts.OUTMODE == "table":
- name=fname
- return os.path.join(opts.OUTDIR, name)
-
-def mkdir(path):
- d = os.path.dirname(path) #< if path is to a dir, make sure to terminate with a /
- if not os.path.exists(d):
- os.makedirs(d)
-
-
-## Populate dict of script templates
-TEMPLATES = {}
-for t in opts.TEMPLATES:
- tname = os.path.basename(t)
- with open(tname, "r") as f:
- TEMPLATES[tname] = f.read()
-
-## Initialise random number generator
-import random
-if opts.SEED:
- random.seed(opts.SEED)
-
-## Populate param samplers dictionary
-from professor2 import Sampler
-try:
- from collections import OrderedDict
-except:
- from ordereddict import OrderedDict
-
-
-
-
-
-# ## Load a point veto function if supplied by the user
-if opts.VETOFN:
- execfile(opts.VETOFN)
- assert "prof_sample_veto" in dir()
- opts.VETOFN = prof_sample_veto
-
-# # Head for table like write out
-# if opts.PARAMSFILE and opts.OUTMODE=="table":
- # f_table = open(opts.OUTDIR, "w")
- # head = "#"
- # for name, s in samplers.iteritems():
- # head += " %s"%name
- # head += "\n"
- # f_table.write(head)
-
-
-
-def mkDictPoint(SOB, ranges):
- params = OrderedDict()
- # from IPython import embed
- # embed()
- for num, pname in enumerate(ranges.keys()):
- pmin = ranges[pname][0]
- pmax = ranges[pname][1]
- p = SOB[num]
- params[pname] = pmin + (pmax-pmin)*p
- return params
-
-RANGES = OrderedDict()
-with open(args[0], "r") as prf:
- for line in prf:
- line = line.split("#")[0].strip() #< strip comments and whitespace
- if line:
- parts = line.split()
- name = parts[0]
- RANGES[name] = map(float, [parts[1], parts[2]])
-
-## Do random param sampling and template instantiation
-from pygsl import qrng
-SOBOL=qrng.sobol(len(RANGES.keys()))
-
-for n in xrange(opts.NUMPOINTS):
- npad = mkrunstr(n)
-
- ## Populate params dictionary
- while True:
- ## Sample a point
- P=SOBOL.get()[0]
- params = mkDictPoint(P, RANGES)
- ## Allow a user function to veto the point
- if opts.VETOFN and opts.VETOFN(params):
- continue #< try sampling again
- break #< successful sample: proceed
-
- ## Write params file if not disabled (by specifying a null filename)
- if opts.PARAMSFILE and not opts.OUTMODE=="table":
- pname = mkoutname(opts.PARAMSFILE, npad)
- mkdir(pname)
- with open(pname, "w") as pf:
- for k, v in params.iteritems():
- pf.write("{name} {val:e}\n".format(name=k, val=v))
-
- # Table like writeout
- elif opts.PARAMSFILE and opts.OUTMODE=="table":
- line = ""
- for k, v in params.iteritems():
- line+="%.18f "%v
- line+="\n"
- f_table.write(line)
-
- ## Instantiate template(s)
- params["N"] = npad #< Add the run number *after* writing out the params file
- for tbasename, tmpl in TEMPLATES.iteritems():
- txt = tmpl.format(**params)
- tname = mkoutname(tbasename, npad)
- mkdir(tname)
- with open(tname, "w") as tf:
- tf.write(txt)
-
-# Close file for table like write-out
-if opts.PARAMSFILE and opts.OUTMODE=="table":
- f_table.close()
diff --git a/contrib/prof2-heatmapper b/contrib/prof2-heatmapper
new file mode 100755
--- /dev/null
+++ b/contrib/prof2-heatmapper
@@ -0,0 +1,86 @@
+#! /usr/bin/env python
+
+'''
+Given an output file from mapbuilder, plot a heat map of chi2 values
+'''
+
+import numpy
+import matplotlib.pyplot as pyplot
+from matplotlib import cm
+import matplotlib.colors as colorplot
+import yoda
+
+import optparse, os, sys
+op = optparse.OptionParser(usage=__doc__)
+op.add_option("--params", dest="WFILE", default=None, help="Path to a weight file to specify unequal chi2 weights of each bin in the fit (default: %default)")
+op.add_option("--output", dest="OUTPUT", default="tunes", help="Prefix for outputs (default: %default)")
+
+opts, args = op.parse_args()
+if len(args) < 1:
+ print "Argument missing... exiting\n\n"
+ op.print_usage()
+ sys.exit(1)
+histos=yoda.readYODA(args[0])
+params1 = args[1]
+params2 = args[2]
+print params1, params2
+
+s = histos["/PROFLIKE/{0}{1}".format(params1, params2)]
+
+x_array = numpy.array([])
+y_array = numpy.array([])
+'''
+#coords = {}
+coords = numpy.empty((1, 3))
+for p in s.points:
+ coord = numpy.zeros((1, 3))
+ coord[0, 0] = p.x
+ coord[0, 1] = p.y
+ coord[0, 2] = p.z
+ coords = numpy.append(coords, coord, axis=0)
+coords[:, 2] = coords[:, 2]/numpy.amax(coords[:, 2])
+'''
+coords = {}
+
+for p in s.points:
+ x = p.x
+ y = p.y
+ z = p.z
+ x_array = numpy.append(x_array, x)
+ y_array = numpy.append(y_array, y)
+ coords[(x, y)] = z
+
+x_list = numpy.unique(x_array)
+y_list = numpy.unique(y_array)
+print x_list
+print y_list
+
+z = numpy.zeros((len(y_list), len(x_list)))
+for i in range(len(x_list)):
+ for j in range(len(y_list)):
+ z[j, i] = coords[(x_list[i], y_list[j])]
+ #if z[j, i] >=3000:
+ #z[j, i] = 3000
+
+#z = numpy.log(z)
+#z = z/numpy.amax(z)
+
+from matplotlib.ticker import MaxNLocator
+levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())
+
+pyplot.figure(figsize=(7, 6))
+
+im = pyplot.imshow(z,extent=(numpy.amin(x_list), numpy.amax(x_list), numpy.amin(y_list), numpy.amax(y_list)), aspect='auto',origin = 'lower', cmap=cm.jet)
+pyplot.colorbar(cmap=cm.jet)
+pyplot.xlabel('{0}'.format(params1))
+pyplot.ylabel('{0}'.format(params2))
+
+#cf = pyplot.contour(x_list,extent = (xmin, xmax, ymin, ymax),
+# y_list, z, levels=levels,
+# cmap=cm.jet)
+#ax1.colorbar(cf, ax=ax1)
+#ax1.set_title('contourf with levels')
+pyplot.tight_layout()
+
+pyplot.show()
+
diff --git a/contrib/prof2-mapbuilder b/contrib/prof2-mapbuilder
new file mode 100755
--- /dev/null
+++ b/contrib/prof2-mapbuilder
@@ -0,0 +1,60 @@
+#! /usr/bin/env python
+
+'''Call with args: parameters, directory of limit files (REFDIR>param1_param2_fixed>(limits_index1_index2.txt, index1_index2_chi.txt)), # points. Amalgamates all the chi2 files from prof2-tune-queue into a set of coordinates which can be plotted with heatmapper
+'''
+import optparse, os, sys
+op = optparse.OptionParser(usage=__doc__)
+op.add_option("--params", dest="WFILE", default=None, help="Path to a weight file to specify unequal chi2 weights of each bin in the fit (default: %default)")
+op.add_option("--output", dest="OUTPUT", default="tunes", help="Prefix for outputs (default: %default)")
+
+opts, args = op.parse_args()
+if len(args) < 1:
+ print "Argument missing... exiting\n\n"
+ op.print_usage()
+ sys.exit(1)
+with open(args[0], "r") as f:
+ params = f.read().splitlines()
+REFDIR = args[1]
+N = int(args[2])
+print params
+
+from numpy import zeros
+import yoda
+yodas3d=[]
+for i, p1 in enumerate(params):
+ for j, p2 in enumerate(params):
+ if i<j:
+ print p1, p2
+ scatter = []
+ x = zeros((N, N))
+ y = zeros((N, N))
+ z = zeros((N, N))
+ for n in range(N):
+ for m in range(N):
+ with open("{0}{1}_{2}_fixed/limits_{3}_{4}.txt_chi.txt".format(REFDIR, p1, p2, n, m)) as chi:
+ chi2 = chi.read()
+ z[n][m] = chi2
+ with open("{0}{1}_{2}_fixed/limits_{3}_{4}.txt".format(REFDIR, p1, p2, n, m)) as lim:
+ lims = {}
+ for line in lim:
+ parts = line.split()
+ key = parts[0]
+ val = parts[1]
+ lims[key] = val
+ x[n][m] = lims[p1]
+ y[n][m] = lims[p2]
+
+ s=yoda.Scatter3D(path="PROFLIKE/%s%s"%(p1, p2), title=p1+"vs"+p2)
+ dx = (x[1][1] - x[0][1])*0.5
+ print dx
+ dy = (y[1][1] - y[1][0])*0.5
+ for n in range(N):
+ for m in range(N):
+ i=x[n][m]
+ j=y[n][m]
+ k=z[n][m]
+ s.addPoint(i,j,k, xerrs=dx, yerrs=dy)
+ yodas3d.append(s)
+ yoda.writeYODA(yodas3d, "%s_2dproflike.yoda" % opts.OUTPUT)
+
+
diff --git a/contrib/prof2-sobol b/contrib/prof2-sobol
new file mode 100755
--- /dev/null
+++ b/contrib/prof2-sobol
@@ -0,0 +1,165 @@
+#! /usr/bin/env python
+
+"""\
+%prog [-o out1] [-t template1.txt -t ...] PARAM1:low1:high1 PARAM2:low2:high2:'exp(x)'
+or
+%prog [-o out1] [-t template1.txt -t ...] myparamfile
+
+Sample a parameter space, creating a set of parameter files and optionally
+substituting into script templates, with either flat sampling (default) or
+sampling in a transformed space.
+
+Parameter ranges (and bias functions) can either be given inline on the command line,
+with the name, low..high range, and optional bias function separated by colons (:),
+or in a parameter range file with one parameter per line and whitespace separation of
+the name, low, high, and bias terms.
+
+TODO:
+ * copy the file mode (esp. executable) from the template to each instantiation
+"""
+
+import optparse
+op = optparse.OptionParser(usage=__doc__)
+op.add_option("-n", dest="NUMPOINTS", metavar="NUM", type=int, default=100, help="number of samples to generate [default=%default]")
+op.add_option("-t", dest="TEMPLATES", metavar="FILE", action="append", default=[], help="specify a template file to be populated for each sample point. Can be given multiple times. Strings in curly braces are instantiated.")
+op.add_option("-o", "--outdir", dest="OUTDIR", metavar="DIR", default="scan", help="specify the output directory name (default: %default)")
+op.add_option("-O", "--outmode", dest="OUTMODE", metavar="MODE", default="hier", help="specify the output structuring mode: either 'hier' (default) or 'flat' to respectively use run subdirs or not or 'table' to create one text file with a table like structure")
+op.add_option("-p", "--pfile", dest="PARAMSFILE", metavar="FILE", default="params.dat", help="specify params file base name to be populated for each sample point")
+op.add_option("-P", "--no-pfile", dest="PARAMSFILE", action="store_const", const=None, help="do not write a params file for each sample point")
+op.add_option("-s", "--seed", dest="SEED", metavar="VAL", default=None, help="specify a random seed for the sampler (default: use system time)")
+op.add_option("--veto", dest="VETOFN", metavar="FILE", default=None, help="specify a file from which to read the definition of a Python sample point-vetoing function, prof_sample_veto(paramsdict). Return True means 'veto point'")
+op.add_option("-v", "--debug", dest="DEBUG", action="store_true", default=False, help="turn on some debug messages")
+op.add_option("-q", "--quiet", dest="QUIET", action="store_true", default=False, help="turn off messages")
+opts, args = op.parse_args()
+assert opts.OUTMODE in ("hier", "flat", "table")
+
+import os
+
+def mkrunstr(num):
+ return "{run:04d}".format(run=num)
+
+def mkoutname(fname, run, prefix="", suffix=""):
+ if type(run) is int:
+ run = mkrunstr(run)
+ if opts.OUTMODE == "hier":
+ name = os.path.join(run, fname)
+ elif opts.OUTMODE == "flat":
+ fname = os.path.basename(fname)
+ base, ext = os.path.splitext(fname)
+ name = prefix + base + "-" + run + suffix + ext
+ print "Name:", name
+ elif opts.OUTMODE == "table":
+ name=fname
+ return os.path.join(opts.OUTDIR, name)
+
+def mkdir(path):
+ d = os.path.dirname(path) #< if path is to a dir, make sure to terminate with a /
+ if not os.path.exists(d):
+ os.makedirs(d)
+
+
+## Populate dict of script templates
+TEMPLATES = {}
+for t in opts.TEMPLATES:
+ tname = os.path.basename(t)
+ with open(tname, "r") as f:
+ TEMPLATES[tname] = f.read()
+
+## Initialise random number generator
+import random
+if opts.SEED:
+ random.seed(opts.SEED)
+
+## Populate param samplers dictionary
+from professor2 import Sampler
+try:
+ from collections import OrderedDict
+except:
+ from ordereddict import OrderedDict
+
+
+
+
+
+# ## Load a point veto function if supplied by the user
+if opts.VETOFN:
+ execfile(opts.VETOFN)
+ assert "prof_sample_veto" in dir()
+ opts.VETOFN = prof_sample_veto
+
+# # Head for table like write out
+# if opts.PARAMSFILE and opts.OUTMODE=="table":
+ # f_table = open(opts.OUTDIR, "w")
+ # head = "#"
+ # for name, s in samplers.iteritems():
+ # head += " %s"%name
+ # head += "\n"
+ # f_table.write(head)
+
+
+
+def mkDictPoint(SOB, ranges):
+ params = OrderedDict()
+ # from IPython import embed
+ # embed()
+ for num, pname in enumerate(ranges.keys()):
+ pmin = ranges[pname][0]
+ pmax = ranges[pname][1]
+ p = SOB[num]
+ params[pname] = pmin + (pmax-pmin)*p
+ return params
+
+RANGES = OrderedDict()
+with open(args[0], "r") as prf:
+ for line in prf:
+ line = line.split("#")[0].strip() #< strip comments and whitespace
+ if line:
+ parts = line.split()
+ name = parts[0]
+ RANGES[name] = map(float, [parts[1], parts[2]])
+
+## Do random param sampling and template instantiation
+from pygsl import qrng
+SOBOL=qrng.sobol(len(RANGES.keys()))
+
+for n in xrange(opts.NUMPOINTS):
+ npad = mkrunstr(n)
+
+ ## Populate params dictionary
+ while True:
+ ## Sample a point
+ P=SOBOL.get()[0]
+ params = mkDictPoint(P, RANGES)
+ ## Allow a user function to veto the point
+ if opts.VETOFN and opts.VETOFN(params):
+ continue #< try sampling again
+ break #< successful sample: proceed
+
+ ## Write params file if not disabled (by specifying a null filename)
+ if opts.PARAMSFILE and not opts.OUTMODE=="table":
+ pname = mkoutname(opts.PARAMSFILE, npad)
+ mkdir(pname)
+ with open(pname, "w") as pf:
+ for k, v in params.iteritems():
+ pf.write("{name} {val:e}\n".format(name=k, val=v))
+
+ # Table like writeout
+ elif opts.PARAMSFILE and opts.OUTMODE=="table":
+ line = ""
+ for k, v in params.iteritems():
+ line+="%.18f "%v
+ line+="\n"
+ f_table.write(line)
+
+ ## Instantiate template(s)
+ params["N"] = npad #< Add the run number *after* writing out the params file
+ for tbasename, tmpl in TEMPLATES.iteritems():
+ txt = tmpl.format(**params)
+ tname = mkoutname(tbasename, npad)
+ mkdir(tname)
+ with open(tname, "w") as tf:
+ tf.write(txt)
+
+# Close file for table like write-out
+if opts.PARAMSFILE and opts.OUTMODE=="table":
+ f_table.close()

File Metadata

Mime Type
text/x-diff
Expires
Sat, Dec 21, 3:17 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4023257
Default Alt Text
(17 KB)

Event Timeline