Changeset 5746
- Timestamp:
- Jul 14, 2015 10:11:27 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/uq/puq/puq_analyze.py
r5741 r5746 37 37 38 38 # Plots probability curves 39 def plot_pdf_curve(xvals, vname, percent , desc):40 print('plot_pdf_curve %s %s %s' % (vname, percent, desc))39 def plot_pdf_curve(xvals, vname, percent): 40 print('plot_pdf_curve %s %s' % (vname, percent)) 41 41 # compute upper and lower percentiles 42 42 pm = (100 - percent)/200.0 43 43 pp = 1 - pm 44 45 label = None 44 46 45 47 # collect data into an array … … 48 50 ym = np.empty(len(xvals[vname])) 49 51 for vindex in sorted(xvals[vname].keys()): 52 if label is None: 53 label = h5['/output/data/%s[%d]' % (vname, vindex)].attrs['label'] 50 54 xarr[vindex] = xvals[vname][vindex] 51 55 yp[vindex] = pcurves[vname][vindex].ppf(pp) … … 58 62 else: 59 63 xml.SubElement(about, 'label').text = "middle %s%%" % percent 60 xml.SubElement(about, 'group').text = '%s:UQ:6' % desc64 xml.SubElement(about, 'group').text = '%s:UQ:6' % label 61 65 comp = xml.SubElement(curve, 'component') 62 66 xy = xml.SubElement(comp, 'xy') … … 73 77 74 78 75 def dist(x1, y1, x2, y2, x3, y3): # x3,y3 is the point 76 diffx = x2-x1 77 diffy = y2-y1 78 sqdiff = float(diffx**2 + diffy**2) 79 80 u = ((x3 - x1) * diffx + (y3 - y1) * diffy) / sqdiff 81 82 if u > 1: 83 u = 1 84 elif u < 0: 85 u = 0 86 87 x = x1 + u * diffx 88 y = y1 + u * diffy 89 90 dx = x - x3 91 dy = y - y3 92 93 return np.sqrt(dx*dx + dy*dy) 94 95 96 # Plots advanced probability curves 97 def plot_pdf_acurve(hf, acurves, vname, percent, desc): 98 print('plot_pdf_acurve %s %s %s' % (vname, percent, desc)) 79 def plot_pdf_acurve(hf, acurves, vname, percent): 80 """ 81 This function plots the probability curves for parametric 82 PDFs. 83 """ 84 print('plot_pdf_acurve %s %s' % (vname, percent)) 99 85 # compute upper and lower percentiles 100 86 pm = (100 - percent)/200.0 101 87 pp = 1 - pm 102 vlen = len(acurves[vname]) 103 104 print('vlen=', vlen) 105 # collect data into an array 106 xp = np.empty(vlen) 107 xm = np.empty(vlen) 108 yp = np.empty(vlen) 109 ym = np.empty(vlen) 110 88 89 label = None 90 maxvals = [] 91 minvals = [] 111 92 for vindex in sorted(acurves[vname].keys()): 112 print('vindex=', vindex) 113 x1 = acurves[vname][vindex][0].ppf(pp) 114 x2 = acurves[vname][vindex][0].ppf(pm) 115 y1 = acurves[vname][vindex][1].ppf(pp) 116 y2 = acurves[vname][vindex][1].ppf(pm) 117 xd = hf['/output/data/%s.x[%d]' % (vname, int(vindex))].value 118 yd = hf['/output/data/%s.y[%d]' % (vname, int(vindex))].value 119 #p1, p2 = get_closest2() 120 if int(vindex) == 2: 121 print (x1, x2, y1, y2) 122 print ('xd=', repr(xd)) 123 print ('yd=', repr(yd)) 124 125 #xp[vindex] = x1 126 #xm[vindex] = x2 127 #yp[vindex] = y1 128 #ym[vindex] = y2 129 return 93 if label is None: 94 label = h5['/output/data/%s[%d]' % (vname, vindex)].attrs['label'] 95 f1 = unpickle(hf['/output/data/%s[%d]' % (vname, vindex)].attrs['curve']) 96 pdf = acurves[vname][vindex] 97 a, b = f1.eval([pdf.ppf(pm), pdf.ppf(pp)]) 98 maxvals.append((a[1], b[1])) 99 minvals.append((a[0], b[0])) 100 130 101 curve = xml.SubElement(dout, 'curve', {'id': 'curve_pdf-%s-%s' % (vname, percent)}) 131 102 about = xml.SubElement(curve, 'about') … … 134 105 else: 135 106 xml.SubElement(about, 'label').text = "middle %s%%" % percent 136 xml.SubElement(about, 'group').text = '%s:UQ:6' % desc107 xml.SubElement(about, 'group').text = '%s:UQ:6' % label 137 108 comp = xml.SubElement(curve, 'component') 138 109 xy = xml.SubElement(comp, 'xy') 139 pts = "" 140 for x, y in zip(xarr, yp): 141 pts += "%s %s " % (x, y) 110 111 pts = '' 112 for v in maxvals: 113 pts += "%s %s " % (v[0], v[1]) 114 142 115 if percent == 0: 143 116 pts += '\n' 144 117 else: 145 for x, y in reversed(zip(xarr, ym)): 146 pts += "%s %s " % (x, y) 147 pts += "%s %s\n" % (xarr[0], yp[0]) 118 for v in reversed(minvals): 119 pts += "%s %s " % (v[0], v[1]) 148 120 xy.text = pts 149 121 … … 274 246 outstr.close() 275 247 248 276 249 sw = load_from_hdf5(sys.argv[1]) 277 250 sw.analyze() … … 288 261 289 262 reg1 = re.compile('([ \da-zA-Z]+)\[([ \d]+)\]') 290 reg2 = re.compile('([ \da-zA-Z]+)\.([xy])\[([ \d]+)\]')291 263 292 264 uqtype = h5.attrs['UQtype'] 293 265 for v in h5[uqtype]: 294 266 rsp = h5['/%s/%s/response' % (uqtype, v)].value 295 print("rsp=", rsp)296 267 rs = unpickle(rsp) 297 268 pdf = rs.pdf(fit=False) … … 308 279 xvals[vname][vindex] = odata.attrs['x'] 309 280 pcurves[vname][vindex] = pdf 310 elif reg2.findall(v) != []: 311 matches = reg2.findall(v) 312 vname, xy, vindex = matches[0] 281 elif 'curve' in odata.attrs: 282 matches = reg1.findall(v) 283 vname, vindex = matches[0] 284 print('ACURVE: %s - %s' % (vname, vindex)) 313 285 if vname not in acurves: 314 286 acurves[vname] = {} 315 if vindex not in acurves[vname]: 316 acurves[vname][vindex] = {} 317 if xy == 'x': 318 acurves[vname][vindex][0] = pdf 319 else: 320 acurves[vname][vindex][1] = pdf 287 acurves[vname][int(vindex)] = pdf 321 288 else: 322 289 desc = h5['/output/data/%s' % v].attrs['label'] 323 290 plot_pdf(v, pdf, desc) 324 291 325 # now do p dfcurves292 # now do probability curves 326 293 for vname in xvals: 327 desc = h5['/output/data/%s[0]' % vname].attrs['description'] 328 plot_pdf_curve(xvals, vname, 0, desc) 329 plot_pdf_curve(xvals, vname, 50, desc) 330 plot_pdf_curve(xvals, vname, 95, desc) 331 332 # for vname in acurves: 333 # desc = h5['/output/data/%s.x[0]' % vname].attrs['description'] 334 # plot_pdf_acurve(h5, acurves, vname, 0, desc) 335 # plot_pdf_acurve(h5, acurves, vname, 50, desc) 336 # plot_pdf_acurve(h5, acurves, vname, 95, desc) 294 plot_pdf_curve(xvals, vname, 0) 295 plot_pdf_curve(xvals, vname, 50) 296 plot_pdf_curve(xvals, vname, 95) 297 298 for vname in acurves: 299 plot_pdf_acurve(h5, acurves, vname, 0) 300 plot_pdf_acurve(h5, acurves, vname, 50) 301 plot_pdf_acurve(h5, acurves, vname, 95) 337 302 338 303
Note: See TracChangeset
for help on using the changeset viewer.