-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":
+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)")
+'''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:
+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":