Changeset 5706 for branches/uq/puq/puq_analyze.py
- Timestamp:
- Jun 16, 2015 9:18:59 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/uq/puq/puq_analyze.py
r5528 r5706 13 13 from puq.jpickle import unpickle 14 14 import xml.etree.ElementTree as xml 15 from itertools import product, combinations16 15 import Rappture 17 16 import StringIO … … 35 34 sw.psweep.reinit() 36 35 return sw 37 38 39 # variable names to labels40 def subs_names(varl, h5):41 varlist = []42 for v in varl:43 try:44 lab = h5['/input/params/%s' % v[0]].attrs['label']45 except:46 lab = str(v[0])47 varlist.append(lab)48 return varlist49 50 51 def plot_resp1(io, resp, name, h5):52 print('plot_resp1', name)53 varlist = subs_names(resp.vars, h5)54 try:55 tname = h5['/output/data/%s' % name].attrs['label']56 except:57 tname = name58 ydata = h5['/output/data/%s' % name].value59 xdata = h5['/input/param_array'].value.T[0]60 61 title = '%s (Response)' % (tname)62 63 curve = io['output.curve(response-%s)' % name]64 curve['about.label'] = title65 curve['about.group'] = title66 curve['xaxis.label'] = varlist[0]67 curve['yaxis.label'] = tname68 x = np.linspace(*resp.vars[0][1], num=50)69 y = np.array(resp.eval(x))70 curve['component.xy'] = (x, y)71 72 # scatter plot sampled data on response surface73 curve = io['output.curve(response-%s-scatter)' % name]74 curve['about.label'] = 'Data Points'75 curve['about.group'] = title76 curve['about.type'] = 'scatter'77 curve['xaxis.label'] = varlist[0]78 curve['yaxis.label'] = tname79 curve['component.xy'] = (xdata, ydata)80 81 82 def plot_resp2(io, resp, name, varl, h5):83 print("plot_resp2", name)84 numpoints = 5085 m2d = io['output.mesh(%s%s)' % (varl[0][0], varl[1][0])]86 87 m2d['about.label'] = '2D Mesh'88 m2d['dim'] = 289 m2d['hide'] = 'yes'90 m2d['grid.xaxis.numpoints'] = numpoints91 m2d['grid.yaxis.numpoints'] = numpoints92 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]96 97 varlist = subs_names(varl, h5)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]101 try:102 tname = h5['/output/data/%s' % name].attrs['label']103 except:104 tname = name105 title = tname + ' (Response %s)' % ' vs '.join(varlist)106 fname['about.label'] = title107 fname['component.mesh'] = m2d.name108 fname['about.view'] = 'heightmap'109 x = np.linspace(*varl[0][1], num=numpoints)110 y = np.linspace(*varl[1][1], num=numpoints)111 pts = np.array([(b, a) for a, b in product(y, x)])112 allpts = np.empty((numpoints**2, len(resp.vars)))113 for i, v in enumerate(resp.vars):114 if v[0] == varl[0][0]:115 allpts[:, i] = pts[:, 0]116 elif v[0] == varl[1][0]:117 allpts[:, i] = pts[:, 1]118 else:119 allpts[:, i] = np.mean(v[1])120 pts = np.array(resp.evala(allpts))121 fname['component.values'] = pts122 123 124 def plot_resp3(io, resp, name, varl, h5):125 numpoints = 40126 m3d = io['output.mesh(m3d)']127 m3d['about.label'] = '3D Mesh'128 m3d['dim'] = 3129 m3d['hide'] = 'yes'130 m3d['grid.xaxis.numpoints'] = numpoints131 m3d['grid.yaxis.numpoints'] = numpoints132 m3d['grid.zaxis.numpoints'] = numpoints133 134 # for now, scale to [0,1]135 m3d['grid.xaxis.min'] = 0136 m3d['grid.xaxis.max'] = 1137 m3d['grid.yaxis.min'] = 0138 m3d['grid.yaxis.max'] = 1139 m3d['grid.zaxis.min'] = 0140 m3d['grid.zaxis.max'] = 1141 142 varlist = subs_names(varl, h5)143 try:144 tname = h5['/output/data/%s' % name].attrs['label']145 except:146 tname = name147 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])151 title = tname + ' (Response %s)' % ' vs '.join(varlist)152 fname['about.label'] = title153 fname['component.mesh'] = m3d.name154 fname['about.description'] = '3D Field Description'155 fname['about.view'] = 'vtkvolume'156 x = np.linspace(*varl[0][1], num=numpoints)157 y = np.linspace(*varl[1][1], num=numpoints)158 z = np.linspace(*varl[2][1], num=numpoints)159 160 # generate points in the right order, with x changing fastest161 pts = np.array([(c, b, a) for a, b, c in product(z, y, x)])162 allpts = np.empty((numpoints**3, len(resp.vars)))163 for i, v in enumerate(resp.vars):164 if v[0] == varl[0][0]:165 allpts[:, i] = pts[:, 0]166 elif v[0] == varl[1][0]:167 allpts[:, i] = pts[:, 1]168 elif v[0] == varl[2][0]:169 allpts[:, i] = pts[:, 2]170 else:171 allpts[:, i] = np.mean(v[1])172 pts = np.array(resp.evala(allpts))173 fname['component.values'] = pts174 175 176 def plot_resp(io, resp, name, h5):177 if resp is not None:178 if len(resp.vars) == 1:179 plot_resp1(io, resp, name, h5)180 return181 if len(resp.vars) >= 3:182 for v1, v2, v3 in combinations(resp.vars, 3):183 plot_resp3(io, resp, name, [v1, v2, v3], h5)184 185 # plot all combinations of 2 variables186 for v1, v2 in combinations(resp.vars, 2):187 plot_resp2(io, resp, name, [v1, v2], h5)188 36 189 37 … … 338 186 # [ 34.52960806 163.81660645] 339 187 # [ 55.47039194 346.04150004]] 188 340 189 341 190 def write_responses(io, h5): … … 355 204 356 205 rsp = h5['/%s/%s/response' % (uqtype, v)].value 206 357 207 rout = io['output.response(%s)' % label] 358 208 rout['value'] = rsp 359 209 rout['about.description'] = desc 360 rout['about.label'] = label 210 rout['about.label'] = label + " (Response)" 361 211 362 212 rs = unpickle(rsp) 213 rout['variables'] = ' '.join([str(p.name) for p in rs.params]) 214 labels = ' '.join([repr(str(p.label)) for p in rs.params]) 215 rout['labels'] = labels.replace("'", '"') 363 216 364 217 if type(rs) == puq.response.ResponseFunc: … … 439 292 uqtype = h5.attrs['UQtype'] 440 293 for v in h5[uqtype]: 441 rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value) 294 rsp = h5['/%s/%s/response' % (uqtype, v)].value 295 print("rsp=", rsp) 296 rs = unpickle(rsp) 442 297 pdf = rs.pdf(fit=False) 443 298 odata = h5['/output/data/%s' % v] … … 514 369 dtree.write(f) 515 370 516 517 371 io = Rappture.PyXml('run_uq.xml') 518 for v in h5[uqtype]: 519 if '[' in v: 520 continue 521 rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value) 522 print ('Plotting response', v) 523 plot_resp(io, rs, v, h5) 524 525 # write_responses(io, h5) 372 write_responses(io, h5) 526 373 write_summary(io, h5) 527 528 374 io.close() 375 529 376 h5.close()
Note: See TracChangeset
for help on using the changeset viewer.