Changeset 5528 for branches/uq/puq/puq_analyze.py
- Timestamp:
- May 17, 2015, 6:20:41 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/uq/puq/puq_analyze.py
r5468 r5528 4 4 process the results. 5 5 """ 6 from __future__ import print_function 6 7 import sys 7 8 import os … … 9 10 import h5py 10 11 import re 12 import puq 11 13 from puq.jpickle import unpickle 12 14 import xml.etree.ElementTree as xml 13 15 from itertools import product, combinations 16 import Rappture 17 import StringIO 14 18 15 19 # Redirect stdout and stderr to files for debugging. … … 46 50 47 51 def plot_resp1(io, resp, name, h5): 48 print 'plot_resp1', name52 print('plot_resp1', name) 49 53 varlist = subs_names(resp.vars, h5) 50 54 try: 51 tname = h5['/output/data/%s' % name].attrs[' description']55 tname = h5['/output/data/%s' % name].attrs['label'] 52 56 except: 53 57 tname = name … … 56 60 57 61 title = '%s (Response)' % (tname) 58 curve = 'output.curve(response-%s)' % name 59 io.put(curve + '.about.label', title) 60 io.put(curve + '.about.group', title) 61 io.put(curve + '.xaxis.label', varlist[0]) 62 io.put(curve + '.yaxis.label', tname) 62 63 curve = io['output.curve(response-%s)' % name] 64 curve['about.label'] = title 65 curve['about.group'] = title 66 curve['xaxis.label'] = varlist[0] 67 curve['yaxis.label'] = tname 63 68 x = np.linspace(*resp.vars[0][1], num=50) 64 69 y = np.array(resp.eval(x)) 65 for a, b in zip(x, y): 66 io.put(curve + '.component.xy', "%s %s\n" % (a, b), append=1) 70 curve['component.xy'] = (x, y) 67 71 68 72 # scatter plot sampled data on response surface 69 curve = 'output.curve(response-%s-scatter)' % name 70 io.put(curve + '.about.label', 'Data Points') 71 io.put(curve + '.about.group', title) 72 io.put(curve + '.about.type', 'scatter') 73 io.put(curve + '.xaxis.label', varlist[0]) 74 io.put(curve + '.yaxis.label', tname) 75 for a, b in zip(xdata, ydata): 76 io.put(curve + '.component.xy', "%s %s\n" % (a, b), append=1) 73 curve = io['output.curve(response-%s-scatter)' % name] 74 curve['about.label'] = 'Data Points' 75 curve['about.group'] = title 76 curve['about.type'] = 'scatter' 77 curve['xaxis.label'] = varlist[0] 78 curve['yaxis.label'] = tname 79 curve['component.xy'] = (xdata, ydata) 77 80 78 81 79 82 def plot_resp2(io, resp, name, varl, h5): 83 print("plot_resp2", name) 80 84 numpoints = 50 81 m2d = 'output.mesh('+varl[0][0]+varl[1][0]+')' 82 io.put(m2d+'.about.label', '2D Mesh') 83 io.put(m2d+'.dim', '2') 84 io.put(m2d+'.hide', 'yes') 85 io.put(m2d+'.grid.xaxis.numpoints', '%s' % numpoints) 86 io.put(m2d+'.grid.yaxis.numpoints', '%s' % numpoints) 87 io.put(m2d+'.grid.xaxis.min', '%g' % (varl[0][1][0])) 88 io.put(m2d+'.grid.xaxis.max', '%g' % (varl[0][1][1])) 89 io.put(m2d+'.grid.yaxis.min', '%g' % (varl[1][1][0])) 90 io.put(m2d+'.grid.yaxis.max', '%g' % (varl[1][1][1])) 85 m2d = io['output.mesh(%s%s)' % (varl[0][0], varl[1][0])] 86 87 m2d['about.label'] = '2D Mesh' 88 m2d['dim'] = 2 89 m2d['hide'] = 'yes' 90 m2d['grid.xaxis.numpoints'] = numpoints 91 m2d['grid.yaxis.numpoints'] = numpoints 92 m2d['grid.xaxis.min'] = varl[0][1][0] 93 m2d['grid.xaxis.max'] = varl[0][1][1] 94 m2d['grid.yaxis.min'] = varl[1][1][0] 95 m2d['grid.yaxis.max'] = varl[1][1][1] 91 96 92 97 varlist = subs_names(varl, h5) 93 fname = 'output.field(response-'+name+varlist[0]+varlist[1]+')'94 io.put(fname + '.about.xaxis.label', varlist[0])95 io.put(fname + '.about.yaxis.label', varlist[1])98 fname = io['output.field(response-'+name+varlist[0]+varlist[1]+')'] 99 fname['about.xaxis.label'] = varlist[0] 100 fname['about.yaxis.label'] = varlist[1] 96 101 try: 97 tname = h5['/output/data/%s' % name].attrs[' description']102 tname = h5['/output/data/%s' % name].attrs['label'] 98 103 except: 99 104 tname = name 100 105 title = tname + ' (Response %s)' % ' vs '.join(varlist) 101 io.put(fname + '.about.label', title)102 io.put(fname + '.component.mesh', m2d)103 io.put(fname + '.about.view', 'heightmap')106 fname['about.label'] = title 107 fname['component.mesh'] = m2d.name 108 fname['about.view'] = 'heightmap' 104 109 x = np.linspace(*varl[0][1], num=numpoints) 105 110 y = np.linspace(*varl[1][1], num=numpoints) … … 114 119 allpts[:, i] = np.mean(v[1]) 115 120 pts = np.array(resp.evala(allpts)) 116 for z in pts: 117 io.put(fname + '.component.values', '%g\n' % z, append=1) 121 fname['component.values'] = pts 118 122 119 123 120 124 def plot_resp3(io, resp, name, varl, h5): 121 125 numpoints = 40 122 m3d = 'output.mesh(m3d)'123 io.put(m3d+'.about.label', '3D Mesh')124 io.put(m3d+'.dim', '3')125 io.put(m3d+'.hide', 'yes')126 io.put(m3d+'.grid.xaxis.numpoints', '%s' % numpoints)127 io.put(m3d+'.grid.yaxis.numpoints', '%s' % numpoints)128 io.put(m3d+'.grid.zaxis.numpoints', '%s' % numpoints)126 m3d = io['output.mesh(m3d)'] 127 m3d['about.label'] = '3D Mesh' 128 m3d['dim'] = 3 129 m3d['hide'] = 'yes' 130 m3d['grid.xaxis.numpoints'] = numpoints 131 m3d['grid.yaxis.numpoints'] = numpoints 132 m3d['grid.zaxis.numpoints'] = numpoints 129 133 130 134 # for now, scale to [0,1] 131 io.put(m3d+'.grid.xaxis.min', '0')132 io.put(m3d+'.grid.xaxis.max', '1')133 io.put(m3d+'.grid.yaxis.min', '0')134 io.put(m3d+'.grid.yaxis.max', '1')135 io.put(m3d+'.grid.zaxis.min', '0')136 io.put(m3d+'.grid.zaxis.max', '1')135 m3d['grid.xaxis.min'] = 0 136 m3d['grid.xaxis.max'] = 1 137 m3d['grid.yaxis.min'] = 0 138 m3d['grid.yaxis.max'] = 1 139 m3d['grid.zaxis.min'] = 0 140 m3d['grid.zaxis.max'] = 1 137 141 138 142 varlist = subs_names(varl, h5) 139 143 try: 140 tname = h5['/output/data/%s' % name].attrs[' description']144 tname = h5['/output/data/%s' % name].attrs['label'] 141 145 except: 142 146 tname = name 143 fname = 'output.field(response-'+name+varlist[0]+varlist[1]+varlist[2]+')' 144 io.put(fname + '.about.xaxis.label', 145 '{%s [%.3g - %.3g]}' % 146 (varlist[0], varl[0][1][0], varl[0][1][1])) 147 io.put(fname + '.about.yaxis.label', 148 '{%s [%.3g - %.3g]}' % 149 (varlist[1], varl[1][1][0], varl[1][1][1])) 150 io.put(fname + '.about.zaxis.label', 151 '{%s [%.3g - %.3g]}' % 152 (varlist[2], varl[2][1][0], varl[2][1][1])) 147 fname = io['output.field(response-'+name+varlist[0]+varlist[1]+varlist[2]+')'] 148 fname['about.xaxis.label'] = '{%s [%.3g - %.3g]}' % (varlist[0], varl[0][1][0], varl[0][1][1]) 149 fname['about.yaxis.label'] = '{%s [%.3g - %.3g]}' % (varlist[1], varl[1][1][0], varl[1][1][1]) 150 fname['about.zaxis.label'] = '{%s [%.3g - %.3g]}' % (varlist[2], varl[2][1][0], varl[2][1][1]) 153 151 title = tname + ' (Response %s)' % ' vs '.join(varlist) 154 io.put(fname + '.about.label', title)155 io.put(fname + '.component.mesh', m3d)156 io.put(fname + '.about.description', '3D Field Description')157 io.put(fname + '.about.view', 'vtkvolume')152 fname['about.label'] = title 153 fname['component.mesh'] = m3d.name 154 fname['about.description'] = '3D Field Description' 155 fname['about.view'] = 'vtkvolume' 158 156 x = np.linspace(*varl[0][1], num=numpoints) 159 157 y = np.linspace(*varl[1][1], num=numpoints) … … 173 171 allpts[:, i] = np.mean(v[1]) 174 172 pts = np.array(resp.evala(allpts)) 175 for z in pts: 176 io.put(fname + '.component.values', '%g\n' % z, append=1) 173 fname['component.values'] = pts 177 174 178 175 … … 193 190 # Plots probability curves 194 191 def plot_pdf_curve(xvals, vname, percent, desc): 195 print 'plot_pdf_curve %s %s %s' % (vname, percent, desc)192 print('plot_pdf_curve %s %s %s' % (vname, percent, desc)) 196 193 # compute upper and lower percentiles 197 194 pm = (100 - percent)/200.0 … … 251 248 # Plots advanced probability curves 252 249 def plot_pdf_acurve(hf, acurves, vname, percent, desc): 253 print 'plot_pdf_acurve %s %s %s' % (vname, percent, desc)250 print('plot_pdf_acurve %s %s %s' % (vname, percent, desc)) 254 251 # compute upper and lower percentiles 255 252 pm = (100 - percent)/200.0 … … 257 254 vlen = len(acurves[vname]) 258 255 259 print 'vlen=', vlen256 print('vlen=', vlen) 260 257 # collect data into an array 261 258 xp = np.empty(vlen) … … 265 262 266 263 for vindex in sorted(acurves[vname].keys()): 267 print 'vindex=', vindex264 print('vindex=', vindex) 268 265 x1 = acurves[vname][vindex][0].ppf(pp) 269 266 x2 = acurves[vname][vindex][0].ppf(pm) … … 274 271 #p1, p2 = get_closest2() 275 272 if int(vindex) == 2: 276 print x1, x2, y1, y2277 print 'xd=', repr(xd)278 print 'yd=', repr(yd)273 print (x1, x2, y1, y2) 274 print ('xd=', repr(xd)) 275 print ('yd=', repr(yd)) 279 276 280 277 #xp[vindex] = x1 … … 320 317 xy.text = pts 321 318 319 # Plotting response distance 320 # plot_resp1 distance 321 # Plotting response maxheight 322 # plot_resp1 maxheight 323 # desc= The distance the projectile travels. 324 # label= Distance 325 326 # v=-0.610951707827365*angle**2 + 54.985653704463*angle - 217.610577363519 327 328 # SURROGATE MODEL ERROR: 0.442% 329 # desc= The maximum height the projectile reaches. 330 # label= Maximum Height 331 332 # v=3.42068544561648e-12*angle**2 + 8.66308066792632*angle - 134.909576819562 333 334 # SURROGATE MODEL ERROR: 0.586% 335 # [[ 45. 254.92905324] 336 # [ 30.19262971 128.95166845] 337 # [ 59.80737029 380.90643804] 338 # [ 34.52960806 163.81660645] 339 # [ 55.47039194 346.04150004]] 340 341 def write_responses(io, h5): 342 uqtype = h5.attrs['UQtype'] 343 for v in h5[uqtype]: 344 if '[' in v: 345 # It is a curve. Ignore. 346 continue 347 try: 348 desc = h5['%s/%s' % (uqtype, v)].attrs['description'] 349 except: 350 desc = '' 351 try: 352 label = h5['%s/%s' % (uqtype, v)].attrs['label'] 353 except: 354 label = '' 355 356 rsp = h5['/%s/%s/response' % (uqtype, v)].value 357 rout = io['output.response(%s)' % label] 358 rout['value'] = rsp 359 rout['about.description'] = desc 360 rout['about.label'] = label 361 362 rs = unpickle(rsp) 363 364 if type(rs) == puq.response.ResponseFunc: 365 rout['equation'] = rs.eqn 366 rout['rmse'] = "{:6.3g}".format(rs.rmse()[1]) 367 368 rout['data'] = rs.data 369 370 371 def write_params(h5, out): 372 params = map(str, h5['/input/params'].keys()) 373 print('#' * 80, file=out) 374 print('INPUT PARAMETERS', file=out) 375 376 for pname in params: 377 print('-' * 80, file=out) 378 p = puq.unpickle(h5['/input/params/' + pname].value) 379 cname = p.__class__.__name__[:-9] 380 pdf_str = '%s [%s - %s] mean=%s dev=%s mode=%s' % (cname, p.pdf.range[0], p.pdf.range[1], p.pdf.mean, p.pdf.dev, p.pdf.mode) 381 382 print("Name:", p.name, file=out) 383 try: 384 print("Label:", p.label, file=out) 385 except: 386 pass 387 print("Desc:", p.description, file=out) 388 print('Value:', pdf_str, file=out) 389 print('#' * 80, file=out) 390 print(file=out) 391 392 393 def write_summary(io, h5): 394 outstr = StringIO.StringIO() 395 write_params(h5, outstr) 396 uqtype = h5.attrs['UQtype'] 397 for v in h5[uqtype]: 398 if '[' in v: 399 # It is a curve. Ignore. 400 continue 401 desc = h5['%s/%s' % (uqtype, v)].attrs['description'] 402 print("QoI: %s (%s)" % (v, desc), file=outstr) 403 rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value) 404 if type(rs) == puq.response.ResponseFunc: 405 print("\nv=%s\n" % rs.eqn, file=outstr) 406 print("SURROGATE MODEL ERROR:{:6.3g}%".format(rs.rmse()[1]), file=outstr) 407 sens = puq.unpickle(h5['/%s/%s/sensitivity' % (uqtype, v)].value) 408 max_name_len = max(map(len, [p[0] for p in sens])) 409 print("\nSENSITIVITY:", file=outstr) 410 print("Var%s u* dev" % (' '*(max_name_len)), file=outstr) 411 print('-'*(28+max_name_len), file=outstr) 412 for item in sens: 413 pad = ' '*(max_name_len - len(item[0])) 414 print("{}{} {:10.4g} {:10.4g}".format(pad, item[0], 415 item[1]['ustar'], item[1]['std']), file=outstr) 416 print('-'*(28+max_name_len), file=outstr) 417 print(file=outstr) 418 iostr = io['output.string(UQ Summary)'] 419 iostr['about.label'] = 'UQ Summary' 420 iostr['current'] = outstr.getvalue() 421 outstr.close() 322 422 323 423 sw = load_from_hdf5(sys.argv[1]) … … 365 465 acurves[vname][vindex][1] = pdf 366 466 else: 367 desc = h5['/output/data/%s' % v].attrs[' description']467 desc = h5['/output/data/%s' % v].attrs['label'] 368 468 plot_pdf(v, pdf, desc) 369 469 … … 388 488 if '[' in v: 389 489 continue 390 desc = h5['/output/data/%s' % v].attrs[' description']490 desc = h5['/output/data/%s' % v].attrs['label'] 391 491 sens = unpickle(h5['/%s/%s/sensitivity' % (uqtype, v)].value) 392 492 elem = xml.SubElement(dout, 'histogram', {'id': 'sens-%s' % v}) … … 415 515 416 516 417 import Rappture 418 io = Rappture.library('run_uq.xml') 419 517 io = Rappture.PyXml('run_uq.xml') 420 518 for v in h5[uqtype]: 421 519 if '[' in v: 422 520 continue 423 521 rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value) 424 print 'Plotting response', v522 print ('Plotting response', v) 425 523 plot_resp(io, rs, v, h5) 426 524 427 Rappture.result(io) 428 525 # write_responses(io, h5) 526 write_summary(io, h5) 527 528 io.close() 429 529 h5.close()
Note: See TracChangeset
for help on using the changeset viewer.