def write_sensitivity(io, h5): # If more than one variable, display sensitivity. # Curves have indexed variables, so skip them. if len(h5['/input/params']) > 1 and ['[' in x for x in h5[uqtype]].count(False): for v in h5[uqtype]: if '[' in v: # curve. skip it. continue desc = h5['/output/data/%s' % v].attrs['label'] sens = unpickle(h5['/%s/%s/sensitivity' % (uqtype, v)].value) hist = io['output.histogram(sens-%s)' % v] hist['about.label'] = desc hist['about.uqtype'] = 'Sensitivity' hist['about.type'] = 'scatter' hist['xaxis.label'] = 'Parameters' hist['yaxis.label'] = 'Sensitivity' pts = '' for name in sens: n = name[0] try: n = h5['/input/params/%s' % n].attrs['label'] except: pass pts += "\"%s\" %s\n" % (n, name[1]['ustar']) hist['component.xy'] = pts
def write_summary(io, h5): outstr = StringIO.StringIO() write_params(h5, outstr) uqtype = h5.attrs['UQtype'] for v in h5[uqtype]: if '[' in v: # It is a curve. Ignore. continue desc = h5['%s/%s' % (uqtype, v)].attrs['description'] print("QoI: %s (%s)" % (v, desc), file=outstr) rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value) if type(rs) == puq.response.ResponseFunc: print("\nv=%s\n" % rs.eqn, file=outstr) print("SURROGATE MODEL ERROR:{:6.3g}%".format(rs.rmse()[1]), file=outstr) sens = puq.unpickle(h5['/%s/%s/sensitivity' % (uqtype, v)].value) max_name_len = max(map(len, [p[0] for p in sens])) print("\nSENSITIVITY:", file=outstr) print("Var%s u* dev" % (' '*(max_name_len)), file=outstr) print('-'*(28+max_name_len), file=outstr) for item in sens: pad = ' '*(max_name_len - len(item[0])) print("{}{} {:10.4g} {:10.4g}".format(pad, item[0], item[1]['ustar'], item[1]['std']), file=outstr) print('-'*(28+max_name_len), file=outstr) print(file=outstr) iostr = io['output.string(UQ Summary)'] iostr['about.label'] = 'UQ Summary' iostr['current'] = outstr.getvalue() outstr.close()
def _calibrate(self, hf): ovar = get_output_names(hf)[0] method = hf.attrs['UQtype'] rs = unpickle(hf["/%s/%s/response" % (method, ovar)].value) # print "Calling calibrate from sweep" self.psweep.params, self.psweep.kde = calibrate(self.psweep.params, self.caldata, self.err, rs.eval)
def load_internal(name): debug(name) try: name = name[0] except: name = '' h5, fname = open_hdf5_file(name) if 'private' in h5: val = h5['private/sweep'].value else: # backwards compatibility val = h5['input/sweep'].value if type(val) != str: val = val.decode('UTF-8') sw = unpickle(val) sw.fname = os.path.splitext(fname)[0] h5.close() sw.psweep._sweep = sw if hasattr(sw.psweep, 'reinit'): sw.psweep.reinit() return sw
def _calibrate(self, hf): ovar = get_output_names(hf)[0] method = hf.attrs['UQtype'] rs = unpickle(hf["/%s/%s/response" % (method, ovar)].value) # print "Calling calibrate from sweep" self.psweep.params, self.psweep.kde = calibrate(self.psweep.params, self.caldata, self.err, rs.eval)
def load_internal(name): debug(name) try: name = name[0] except: name = '' h5, fname = open_hdf5_file(name) if 'private' in h5: val = h5['private/sweep'].value else: # backwards compatibility val = h5['input/sweep'].value if type(val) != str: val = val.decode('UTF-8') sw = unpickle(val) sw.fname = os.path.splitext(fname)[0] h5.close() sw.psweep._sweep = sw if hasattr(sw.psweep, 'reinit'): sw.psweep.reinit() return sw
def write_responses(io, h5): uqtype = h5.attrs['UQtype'] for v in h5[uqtype]: print("write_responses", v) if '[' in v: # It is a curve. Ignore. continue try: desc = h5['%s/%s' % (uqtype, v)].attrs['description'] except: desc = '' try: label = h5['%s/%s' % (uqtype, v)].attrs['label'] except: label = '' rsp = h5['/%s/%s/response' % (uqtype, v)].value rout = io['output.response(%s)' % v] rout['value'] = rsp rout['about.description'] = desc rout['about.label'] = label rout['about.uqtype'] = 'Response' rs = unpickle(rsp) rout['variables'] = ' '.join([str(p.name) for p in rs.params]) labels = ' '.join([repr(str(p.label)) for p in rs.params]) rout['labels'] = labels.replace("'", '"') if type(rs) == puq.response.ResponseFunc: rout['equation'] = rs.eqn rout['rmse'] = "{:6.3g}".format(rs.rmse()[1]) rout['data'] = rs.data
def get_params(hf): """get_params(hf) Returns a list of arrays of input parameter values. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. """ return [unpickle(hf['/input/params'][p].value) for p in hf['/input/params']]
def get_params(hf): """get_params(hf) Returns a list of arrays of input parameter values. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. """ return [unpickle(hf['/input/params'][p].value) for p in hf['/input/params']]
def load_from_hdf5(name): h5 = h5py.File(name, 'r+') sw = unpickle(h5['private/sweep'].value) sw.fname = os.path.splitext(name)[0] h5.close() sw.psweep._sweep = sw if hasattr(sw.psweep, 'reinit'): sw.psweep.reinit() return sw
def plot_resp2(dout, resp, name1, name2, rlabel): print("plot_resp2", name1, name2, rlabel) numpoints = 50 resp = unpickle(resp) for p in resp.params: if p.name == name1: v1 = p elif p.name == name2: v2 = p x = np.linspace(*v1.pdf.range, num=numpoints) y = np.linspace(*v2.pdf.range, num=numpoints) pts = np.array([(b, a) for a, b in product(y, x)]) allpts = np.empty((numpoints**2, len(resp.vars))) for i, v in enumerate(resp.vars): if v[0] == v1.name: allpts[:, i] = pts[:, 0] elif v[0] == v2.name: allpts[:, i] = pts[:, 1] else: allpts[:, i] = np.mean(v[1]) pts = np.array(resp.evala(allpts)) print('plot_resp2 returns array of', pts.shape) # mesh mesh = xml.SubElement(dout, 'mesh', {'id': 'm2d'}) about = xml.SubElement(mesh, 'about') label = xml.SubElement(about, 'label') label.text = '2D Mesh' xml.SubElement(mesh, 'dim').text = '2' xml.SubElement(mesh, 'hide').text = 'yes' grid = xml.SubElement(mesh, 'grid') xaxis = xml.SubElement(grid, 'xaxis') xml.SubElement(xaxis, 'numpoints').text = str(numpoints) xml.SubElement(xaxis, 'min').text = str(v1.pdf.range[0]) xml.SubElement(xaxis, 'max').text = str(v1.pdf.range[1]) yaxis = xml.SubElement(grid, 'yaxis') xml.SubElement(yaxis, 'numpoints').text = str(numpoints) xml.SubElement(yaxis, 'min').text = str(v2.pdf.range[0]) xml.SubElement(yaxis, 'max').text = str(v2.pdf.range[1]) # field field = xml.SubElement(dout, 'field', {'id': 'f2d'}) about = xml.SubElement(field, 'about') xml.SubElement(xml.SubElement(about, 'xaxis'), 'label').text = v1.label xml.SubElement(xml.SubElement(about, 'yaxis'), 'label').text = v2.label xml.SubElement(about, 'label').text = rlabel comp = xml.SubElement(field, 'component') xml.SubElement(comp, 'mesh').text = 'output.mesh(m2d)' xml.SubElement(about, 'view').text = 'heightmap' pts = ' '.join(map(str, pts.ravel('F').tolist())) xml.SubElement(comp, 'values').text = pts
def get_response(hf, var): """get_response(hf, var) Returns the response function for an output variable. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Output variable name. """ psweep = hf.attrs['UQtype'] return unpickle(hf['/%s/%s/response' % (psweep, var)].value)
def get_response(hf, var): """get_response(hf, var) Returns the response function for an output variable. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Output variable name. """ psweep = hf.attrs['UQtype'] return unpickle(hf['/%s/%s/response' % (psweep, var)].value)
def test_add_norm_dist(): sw = run('./vel_add.py') sw.run(fname, overwrite=True) sw.analyze() h5 = h5py.File(fname) tv = unpickle(h5['/smolyak/total_velocity/response'].value) s1, s2 = unpickle(h5['/smolyak/v1/sensitivity'].value) assert (s2[0] == 'v2') assert (s2[1]['ustar'] == 0) s1, s2 = unpickle(h5['/smolyak/v2/sensitivity'].value) assert (s2[0] == 'v1') assert (s2[1]['ustar'] == 0) pdf = tv.pdf() assert (allclose(pdf.mean, 110., atol=.03)) dev = math.sqrt(13) assert (allclose(pdf.dev, dev, atol=.1)) h5.close() os.remove(fname)
def test_add_norm_dist(): sw = run('./vel_add.py') sw.run(fname, overwrite=True) sw.analyze() h5 = h5py.File(fname) tv = unpickle(h5['/smolyak/total_velocity/response'].value) s1, s2 = unpickle(h5['/smolyak/v1/sensitivity'].value) assert(s2[0] == 'v2') assert(s2[1]['ustar'] == 0) s1, s2 = unpickle(h5['/smolyak/v2/sensitivity'].value) assert(s2[0] == 'v1') assert(s2[1]['ustar'] == 0) pdf = tv.pdf() assert(allclose(pdf.mean, 110., atol=.03)) dev = math.sqrt(13) assert(allclose(pdf.dev, dev, atol=.1)) h5.close() os.remove(fname)
def load_internal(name): debug(name) try: name = name[0] except: name = '' h5, fname = open_hdf5_file(name) # in 201, switched to private/sweep if 'private' in h5: sw = unpickle(h5['private/sweep'].value) else: sw = unpickle(h5['input/sweep'].value) sw.fname = os.path.splitext(fname)[0] h5.close() sw.psweep._sweep = sw if hasattr(sw.psweep, 'reinit'): sw.psweep.reinit() return sw
def param_description(hf, var): """param_description(hf, var) Returns the description of an input variable. If the description is empty, returns the variable name. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Input parameter name. """ desc = unpickle(hf['/input/params/%s' % var].value).description if desc: return desc return var
def test_add_unif_dist(): sw = run_uniform(progname) sw.run(fname, overwrite=True) sw.analyze() h5 = h5py.File(fname) tv = unpickle(h5['/smolyak/total_velocity/response'].value) s1, s2 = unpickle(h5['/smolyak/v1/sensitivity'].value) assert(s2[0] == 'v2') assert(s2[1]['ustar'] == 0) s1, s2 = unpickle(h5['/smolyak/v2/sensitivity'].value) assert(s2[0] == 'v1') assert(s2[1]['ustar'] == 0) pdf = tv.pdf() assert(allclose(pdf.mean, 110., atol=.2)) # 5.88784057755 dev = math.sqrt((20*20 + 4*4) / 12.0) assert(allclose(pdf.dev, dev, atol=.2)) h5.close() os.remove(fname)
def param_description(hf, var): """param_description(hf, var) Returns the description of an input variable. If the description is empty, returns the variable name. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Input parameter name. """ desc = unpickle(hf['/input/params/%s' % var].value).description if desc: return desc return var
def get_params(hf): """get_params(hf) Returns a list of arrays of input parameter values. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. """ plist = [] for p in hf['/input/params']: val = hf['/input/params'][p].value if type(val) != str: val = val.decode('UTF-8') plist.append(unpickle(val)) return plist
def get_params(hf): """get_params(hf) Returns a list of arrays of input parameter values. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. """ plist = [] for p in hf['/input/params']: val = hf['/input/params'][p].value if type(val) != str: val = val.decode('UTF-8') plist.append(unpickle(val)) return plist
def param_description(hf, var): """param_description(hf, var) Returns the description of an input variable. If the description is empty, returns the variable name. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Input parameter name. """ val = hf['/input/params/%s' % var].value if type(val) != str: val = val.decode('UTF-8') val = unpickle(val) desc = val.description if desc: return desc return var
def param_description(hf, var): """param_description(hf, var) Returns the description of an input variable. If the description is empty, returns the variable name. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Input parameter name. """ val = hf['/input/params/%s' % var].value if type(val) != str: val = val.decode('UTF-8') val = unpickle(val) desc = val.description if desc: return desc return var
def _dump_hdf5(self, grp, line, job, mjob): debug("Dump %s : %s" % (job, line)) global _vcache, _dcache # old format used single quotes. if line.startswith("{'"): line = line.replace("'", '"') x = unpickle(line) v = x['value'] n = x['name'] if n not in _vcache: if isinstance(v, ndarray): _vcache[n] = np.empty([mjob] + list(v.shape)) else: _vcache[n] = np.empty((mjob)) _vcache[n].fill(np.nan) _dcache[n] = x['desc'] _vcache[n][job] = v
def get_sensitivity(hf, var): """get_sensitivity(hf, var). Returns the sensitivity indices for var in a dictionary with keys equal to parameter names and values consisting of dictionaries with keys equal to sensitivity indices. The keys in the sub dictionaries will vary depending on the type of sensitivity analysis (Smolyak or Morris) but will contain at least 'ustar' and 'std'. Morris also contains 'u' and 'ustar_conf95'. Args: hf: An open HDF5 filehandle or a string containing the HDF5 filename to use. var: Output variable name. """ psweep = hf.attrs['UQtype'] if not '/%s/%s/sensitivity' % (psweep, var) in hf: print("Sensitivity indices weren't compute for output '{}'".format(var)) raise ValueError return unpickle(hf['/%s/%s/sensitivity' % (psweep, var)].value)
def _dump_hdf5(self, grp, line, job, mjob): debug("Dump %s : %s" % (job, line)) global _vcache, _dcache # old format used single quotes. if line.startswith("{'"): line = line.replace("'", '"') x = unpickle(line) v = x['value'] n = x['name'] if not n in _vcache: if isinstance(v, ndarray): _vcache[n] = np.empty([mjob] + list(v.shape)) else: _vcache[n] = np.empty((mjob)) _vcache[n].fill(np.nan) _dcache[n] = x['desc'] _vcache[n][job] = v
def plot_pdf_acurve(io, h5, acurves, vname, percent): """ This function plots the probability curves for parametric PDFs. """ print('plot_pdf_acurve %s %s' % (vname, percent)) label = None prev_pts = None # last set of points poly = [] for vindex in sorted(acurves[vname].keys()): if label is None: label = h5['/output/data/%s[%d]' % (vname, vindex)].attrs['label'] f1 = unpickle(h5['/output/data/%s[%d]' % (vname, vindex)].attrs['curve']) bpts = add_pts(f1, percent) # first data set? Just remember it. if prev_pts is None: prev_pts = bpts continue pts = np.array((prev_pts, bpts)).ravel().reshape(-1, 2) hull = ConvexHull(pts, qhull_options='Pp') p1 = Polygon([hull.points[v] for v in hull.vertices]) poly.append(p1) prev_pts = bpts u = unary_union(poly) curve = io['output.curve(curve_pdf-%s-%s)' % (vname, percent)] if percent == 0: curve['about.label'] = "mean" else: curve['about.label'] = "middle %s%%" % percent curve['about.group'] = label curve['about.uqtype'] = 'Probability' curve['component.xy'] = np.array(u.exterior.xy)
h5 = h5py.File(sys.argv[1], 'r+') io = Rappture.PyXml('run_uq.xml') # curves built from pdfs pcurves = {} xvals = {} acurves = {} reg1 = re.compile('([ \da-zA-Z_]+)\[([ \d]+)\]') uqtype = h5.attrs['UQtype'] for v in h5[uqtype]: print('v=', v) rsp = h5['/%s/%s/response' % (uqtype, v)].value rs = unpickle(rsp) pdf = rs.pdf(fit=False) odata = h5['/output/data/%s' % v] # For curves built from pdfs, just put them in a dict for now if 'x' in odata.attrs: matches = reg1.findall(v) vname, vindex = matches[0] print('CURVE: vname=%s vindex=%s' % (vname, vindex)) vindex = int(vindex) if vname not in pcurves: pcurves[vname] = {} xvals[vname] = {} xvals[vname][vindex] = odata.attrs['x'] pcurves[vname][vindex] = pdf elif 'curve' in odata.attrs:
def _calibrate(self, hf): ovar = get_output_names(hf)[0] rs = unpickle(hf["/smolyak/%s/response" % ovar].value) self.psweep.params = calibrate(self.psweep.params, self.caldata, self.err, rs.eval)
from puq import NormalParameter, UniformParameter, Sweep, RapptureHost, Smolyak import numpy as np from puq.jpickle import unpickle # Redirect stdout and stderr to files for debugging. sys.stdout = open("uq_debug.out", 'w') sys.stderr = open("uq_debug.err", 'w') print(sys.argv) pid, varlist, uq_type, args = sys.argv[1:] dfile = "driver%s.xml" % pid cvsname = "params%s.csv" % pid hname = "puq_%s.hdf5" % pid varlist = unpickle(varlist) print("varlist=", varlist) v = {} units = {} for p in varlist: name, _units, dist = p name = str(name) units[name] = str(_units) if dist[0] == u'gaussian': try: kwargs = dist[3] except: kwargs = {} v[name] = NormalParameter(name, name,
def plot(sweep, h5, opt, params=[]): if opt.v: opt.v = [s.strip() for s in opt.v.split(',')] if not opt.l: opt.k = True method = string.lower(sweep.psweep.__class__.__name__) if opt.r: for vname in h5[method]: if not opt.v or vname in opt.v: print("Plotting Response Surface for %s" % vname) desc = h5[method][vname].attrs['description'] rsv = h5[method][vname]['response'].value rs = unpickle(rsv) p = rs.plot() if desc and desc != vname: plt.title(desc) else: plt.title("Response Function for %s" % vname) plot_customize(opt, p, 'response-%s' % vname) else: if opt.psamples: psamples = get_psamples_from_csv(sweep, h5, opt.samples) else: psamples = None for vname in h5[method]: if not opt.v or vname in opt.v: print("plotting PDF for %s" % vname) desc = h5[method][vname].attrs['description'] if 'samples' in h5[method][vname]: # response surface already sampled. Just calculate pdf. data = h5[method][vname]['samples'].value if opt.k: p = ExperimentalPDF(data, fit=True).plot() if opt.l: p = ExperimentalPDF(data, fit=False).plot() else: rsv = h5[method][vname]['response'].value rs = unpickle(rsv) data = None if opt.k: pdf, data = rs.pdf(fit=True, params=params, psamples=psamples, return_samples=True) p = pdf.plot() if opt.l: if data is not None: p = ExperimentalPDF(data, fit=False).plot() else: pdf, data = rs.pdf(fit=False, params=params, psamples=psamples, return_samples=True) p = pdf.plot() h5[method][vname]['samples'] = data if 'pdf' not in h5[method][vname]: h5[method][vname]['pdf'] = pickle(pdf) plt.xlabel(vname) if desc and desc != vname: plt.title("PDF for %s" % desc) else: plt.title("PDF for %s" % vname) plot_customize(opt, p, 'pdf-%s' % vname)
def plot_resp1(dout, resp, name, rlabel): print('plot_resp1', name, rlabel) numpoints = 100 resp = unpickle(resp) var = None for index, p in enumerate(resp.params): if p.name == name: var = p break if var is None: print("plot_resp1 error: name %s not recognized" % name) return data = resp.data print('data=', repr(data)) for ind, p in enumerate(resp.params): if ind == index: continue m = p.pdf.mean means = np.isclose(m, data[:, ind], rtol=1e-6, atol=1e-12) data = data[means] print("vars=", resp.vars) print("data=", repr(data)) curve = xml.SubElement(dout, 'curve', {'id': 'response'}) about = xml.SubElement(curve, 'about') xml.SubElement(about, 'label').text = rlabel xml.SubElement(about, 'group').text = rlabel xaxis = xml.SubElement(curve, 'xaxis') xml.SubElement(xaxis, 'label').text = var.label yaxis = xml.SubElement(curve, 'yaxis') xml.SubElement(yaxis, 'label').text = rlabel x = np.linspace(*var.pdf.range, num=numpoints) allpts = np.empty((numpoints, len(resp.params))) for i, v in enumerate(resp.params): if v.name == var.name: allpts[:, i] = x else: allpts[:, i] = np.mean(v.pdf.mean) pts = resp.evala(allpts) xy = '\n'.join([' '.join(map(repr, a)) for a in zip(x, pts)]) comp = xml.SubElement(curve, 'component') xml.SubElement(comp, 'xy').text = xy # scatter plot sampled data on response surface curve = xml.SubElement(dout, 'curve', {'id': 'scatter'}) about = xml.SubElement(curve, 'about') xml.SubElement(about, 'label').text = 'Data Points' xml.SubElement(about, 'group').text = rlabel xml.SubElement(about, 'type').text = 'scatter' comp = xml.SubElement(curve, 'component') xy = '\n'.join([' '.join(map(repr, a)) for a in zip(data[:, index], data[:, -1])]) xml.SubElement(comp, 'xy').text = xy
def plot(sweep, h5, opt, params=[]): if opt.v: opt.v = [s.strip() for s in opt.v.split(',')] if not opt.l: opt.k = True method = string.lower(sweep.psweep.__class__.__name__) if opt.r: for vname in h5[method]: if not opt.v or vname in opt.v: print "Plotting Response Surface for %s" % vname desc = h5[method][vname].attrs['description'] rsv = h5[method][vname]['response'].value rs = unpickle(rsv) p = rs.plot() if desc and desc != vname: plt.title(desc) else: plt.title("Response Function for %s" % vname) plot_customize(opt, p, 'response-%s' % vname) else: if opt.psamples: psamples = get_psamples_from_csv(sweep, h5, opt.samples) else: psamples = None for vname in h5[method]: if not opt.v or vname in opt.v: print "plotting PDF for %s" % vname desc = h5[method][vname].attrs['description'] if 'samples' in h5[method][vname]: # response surface already sampled. Just calculate pdf. data = h5[method][vname]['samples'].value if opt.k: p = ExperimentalPDF(data, fit=True).plot() if opt.l: p = ExperimentalPDF(data, fit=False).plot() else: rsv = h5[method][vname]['response'].value rs = unpickle(rsv) data = None if opt.k: pdf, data = rs.pdf(fit=True, params=params, psamples=psamples, return_samples=True) p = pdf.plot() if opt.l: if data is not None: p = ExperimentalPDF(data, fit=False).plot() else: pdf, data = rs.pdf(fit=False, params=params, psamples=psamples, return_samples=True) p = pdf.plot() h5[method][vname]['samples'] = data if not 'pdf' in h5[method][vname]: h5[method][vname]['pdf'] = pickle(pdf) plt.xlabel(vname) if desc and desc != vname: plt.title("PDF for %s" % desc) else: plt.title("PDF for %s" % vname) plot_customize(opt, p, 'pdf-%s' % vname)