Changeset 5746


Ignore:
Timestamp:
Jul 14, 2015 10:11:27 PM (9 years ago)
Author:
mmh
Message:

support parametric probability curves

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/uq/puq/puq_analyze.py

    r5741 r5746  
    3737
    3838# Plots probability curves
    39 def plot_pdf_curve(xvals, vname, percent, desc):
    40     print('plot_pdf_curve %s %s %s' % (vname, percent, desc))
     39def plot_pdf_curve(xvals, vname, percent):
     40    print('plot_pdf_curve %s %s' % (vname, percent))
    4141    # compute upper and lower percentiles
    4242    pm = (100 - percent)/200.0
    4343    pp = 1 - pm
     44
     45    label = None
    4446
    4547    # collect data into an array
     
    4850    ym = np.empty(len(xvals[vname]))
    4951    for vindex in sorted(xvals[vname].keys()):
     52        if label is None:
     53            label = h5['/output/data/%s[%d]' % (vname, vindex)].attrs['label']
    5054        xarr[vindex] = xvals[vname][vindex]
    5155        yp[vindex] = pcurves[vname][vindex].ppf(pp)
     
    5862    else:
    5963        xml.SubElement(about, 'label').text = "middle %s%%" % percent
    60     xml.SubElement(about, 'group').text = '%s:UQ:6' % desc
     64    xml.SubElement(about, 'group').text = '%s:UQ:6' % label
    6165    comp = xml.SubElement(curve, 'component')
    6266    xy = xml.SubElement(comp, 'xy')
     
    7377
    7478
    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))
     79def 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))
    9985    # compute upper and lower percentiles
    10086    pm = (100 - percent)/200.0
    10187    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 = []
    11192    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
    130101    curve = xml.SubElement(dout, 'curve', {'id': 'curve_pdf-%s-%s' % (vname, percent)})
    131102    about = xml.SubElement(curve, 'about')
     
    134105    else:
    135106        xml.SubElement(about, 'label').text = "middle %s%%" % percent
    136     xml.SubElement(about, 'group').text = '%s:UQ:6' % desc
     107    xml.SubElement(about, 'group').text = '%s:UQ:6' % label
    137108    comp = xml.SubElement(curve, 'component')
    138109    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
    142115    if percent == 0:
    143116        pts += '\n'
    144117    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])
    148120    xy.text = pts
    149121
     
    274246    outstr.close()
    275247
     248
    276249sw = load_from_hdf5(sys.argv[1])
    277250sw.analyze()
     
    288261
    289262reg1 = re.compile('([ \da-zA-Z]+)\[([ \d]+)\]')
    290 reg2 = re.compile('([ \da-zA-Z]+)\.([xy])\[([ \d]+)\]')
    291263
    292264uqtype = h5.attrs['UQtype']
    293265for v in h5[uqtype]:
    294266    rsp = h5['/%s/%s/response' % (uqtype, v)].value
    295     print("rsp=", rsp)
    296267    rs = unpickle(rsp)
    297268    pdf = rs.pdf(fit=False)
     
    308279        xvals[vname][vindex] = odata.attrs['x']
    309280        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))
    313285        if vname not in acurves:
    314286            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
    321288    else:
    322289        desc = h5['/output/data/%s' % v].attrs['label']
    323290        plot_pdf(v, pdf, desc)
    324291
    325 # now do pdf curves
     292# now do probability curves
    326293for 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
     298for 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)
    337302
    338303
Note: See TracChangeset for help on using the changeset viewer.