def writeIpol(fname, ipolDict, params, runs=[], summary="", runsdir=""): PARAMNAMES = params[0] PARAMSLIST = params[1] import os, tempfile, zlib if fname == "temp": f = tempfile.NamedTemporaryFile(delete=False) else: f = open(fname, "w") import professor2 as prof f.write("Summary: %s\n" % summary) f.write("DataDir: %s\n" % os.path.abspath(runsdir)) f.write("ProfVersion: %s\n" % prof.version()) f.write("Date: %s\n" % prof.mk_timestamp()) f.write( "DataFormat: binned 3\n" ) # This tells the reader how to treat the coefficients that follow # Format and write out parameter names pstring = "ParamNames:" for p in PARAMNAMES: pstring += " %s" % p f.write(pstring + "\n") # Dimension (consistency check) f.write("Dimension: %i\n" % len(PARAMNAMES)) # Interpolation validity (hypercube edges) # TODO a lot of this is obsolete in the format 3 minstring = "MinParamVals:" for v in prof.mk_minvals(PARAMSLIST): minstring += " %f" % v f.write(minstring + "\n") maxstring = "MaxParamVals:" for v in prof.mk_maxvals(PARAMSLIST): maxstring += " %f" % v f.write(maxstring + "\n") f.write("DoParamScaling: 1\n") # Number of inputs per bin f.write("NumInputs: %i\n" % len(PARAMSLIST)) s_runs = "Runs:" for r in runs: s_runs += " %s" % r f.write("%s\n" % s_runs) f.write("---\n") ## Write out numerical data for all interpolations s = "" HNAMES = sorted(list(set([x[0] for x in ipolDict.keys()]))) for hn in sorted(HNAMES): thisbins = sorted(filter(lambda x: x[0] == hn, ipolDict.keys())) for ipolstring in [ipolDict[x] for x in thisbins]: s += zlib.decompress(ipolstring) f.write(s) f.close() if not fname == "temp": print "\nOutput written to %s" % fname else: return f
def mk_ipolbin(rawP, rawV, rawE, xmin, xmax, CFG): # TODO finally learn how to use kwargs order = CFG["ORDER"] errorder = CFG["ERR_ORDER"] errmode = CFG["ERR_MODE"] medfilt = CFG["MEDIAN_FILT"] if medfilt > 0: from numpy import median # TODO figure out what to do with x=0 relErrs = [ rawE[num] / x if x != 0 else 1 for num, x in enumerate(rawV) ] rem = medfilt * median(relErrs) P, V, E = [], [], [] for num, x in enumerate(relErrs): if x < rem: P.append(rawP[num]) V.append(rawV[num]) E.append(rawE[num]) if CFG["DEBUG"]: print "%i/%i survive median filter %f times %f" % ( len(P), len(rawP), medfilt, median(relErrs)) else: P = rawP V = rawV E = rawE import professor2 as prof pmin = prof.mk_minvals(P) pmax = prof.mk_maxvals(P) if order == "auto": valipol = mk_autoipol(P, V, CFG) else: valipol = Ipol(P, V, int(order)) ## Check for NaN coeffs import math if any([math.isnan(x) for x in valipol.coeffs]): raise NanError("NaN coefficient encountered in value ipol") ## Build the error interpolation(s) if not errmode or errmode == "none": erripols = None ## Build the error interpolation(s) elif errmode == "mean": meanerr = sum(E) / float( len(E) ) #histos[run].bins[binnr].err for run in runs) / float(len(runs)) erripols = Ipol(P, [meanerr], 0) #< const 0th order interpolation elif errmode == "median": medianerr = E[len(E) // 2] erripols = Ipol(P, [medianerr], 0) #< const 0th order interpolation elif errmode == "symm": if errorder == "auto": erripols = mk_autoipol(P, E, CFG) else: erripols = Ipol(P, E, int(errorder)) elif errmode == "asymm": raise Exception("Error interpolation mode 'asymm' not yet supported") else: raise Exception("Unknown error interpolation mode '%s'" % errmode) if erripols is not None: if any([math.isnan(x) for x in erripols.coeffs]): raise NanError("NaN coefficient encountered in error ipol") return IpolBin(xmin, xmax, valipol, erripols), pmin, pmax