Ignore:
Timestamp:
Mar 23, 2015, 10:48:24 AM (9 years ago)
Author:
mmh
Message:

snapshot of uq work

File:
1 edited

Legend:

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

    r5148 r5166  
    6262    xy.text = pts
    6363
     64def dist(x1,y1, x2,y2, x3,y3): # x3,y3 is the point
     65    diffx = x2-x1
     66    diffy = y2-y1
     67    sqdiff = float(diffx**2 + diffy**2)
     68
     69    u = ((x3 - x1) * diffx + (y3 - y1) * diffy) / sqdiff
     70
     71    if u > 1:
     72        u = 1
     73    elif u < 0:
     74        u = 0
     75
     76    x = x1 + u * diffx
     77    y = y1 + u * diffy
     78
     79    dx = x - x3
     80    dy = y - y3
     81
     82    return np.sqrt(dx*dx + dy*dy)
     83
     84# Plots advanced probability curves
     85def plot_pdf_acurve(hf, acurves, vname, percent, desc):
     86    print 'plot_pdf_acurve %s %s %s' % (vname, percent, desc)
     87    # compute upper and lower percentiles
     88    pm = (100 - percent)/200.0
     89    pp = 1 - pm
     90    vlen = len(acurves[vname])
     91
     92    print 'vlen=', vlen
     93    # collect data into an array
     94    xp = np.empty(vlen)
     95    xm = np.empty(vlen)
     96    yp = np.empty(vlen)
     97    ym = np.empty(vlen)
     98
     99    for vindex in sorted(acurves[vname].keys()):
     100        print 'vindex=', vindex
     101        x1 = acurves[vname][vindex][0].ppf(pp)
     102        x2 = acurves[vname][vindex][0].ppf(pm)
     103        y1 = acurves[vname][vindex][1].ppf(pp)
     104        y2 = acurves[vname][vindex][1].ppf(pm)
     105        xd = hf['/output/data/%s.x[%d]' % (vname, int(vindex))].value
     106        yd = hf['/output/data/%s.y[%d]' % (vname, int(vindex))].value
     107        #p1, p2 = get_closest2()
     108        if int(vindex) == 2:
     109            print x1, x2, y1, y2
     110            print 'xd=', repr(xd)
     111            print 'yd=', repr(yd)
     112
     113        #xp[vindex] = x1
     114        #xm[vindex] = x2
     115        #yp[vindex] = y1
     116        #ym[vindex] = y2
     117    return
     118    curve = xml.SubElement(dout, 'curve', {'id': 'curve_pdf-%s-%s' % (vname, percent)})
     119    about = xml.SubElement(curve, 'about')
     120    if percent == 0:
     121        xml.SubElement(about, 'label').text = "mean"
     122    else:
     123        xml.SubElement(about, 'label').text = "middle %s%%" % percent
     124    xml.SubElement(about, 'group').text = '%s (Probability)' % desc
     125    comp = xml.SubElement(curve, 'component')
     126    xy = xml.SubElement(comp, 'xy')
     127    pts = ""
     128    for x, y in zip(xarr, yp):
     129        pts += "%s %s " % (x, y)
     130    if percent == 0:
     131        pts += '\n'
     132    else:
     133        for x, y in reversed(zip(xarr, ym)):
     134            pts += "%s %s " % (x, y)
     135        pts += "%s %s\n" % (xarr[0], yp[0])
     136    xy.text = pts
     137
    64138
    65139def plot_pdf(v, pdf, desc):
     
    91165pcurves = {}
    92166xvals = {}
    93 
    94 regexp = re.compile('([ \da-z]+)\[([ \d]+)\]')
     167acurves = {}
     168
     169reg1 = re.compile('([ \da-zA-Z]+)\[([ \d]+)\]')
     170reg2 = re.compile('([ \da-zA-Z]+)\.([xy])\[([ \d]+)\]')
    95171
    96172uqtype = h5.attrs['UQtype']
     
    102178    # For curves built from pdfs, just put them in a dict for now
    103179    if 'x' in odata.attrs:
    104         matches = regexp.findall(v)
     180        matches = reg1.findall(v)
    105181        vname, vindex = matches[0]
    106182        vindex = int(vindex)
     
    110186        xvals[vname][vindex] = odata.attrs['x']
    111187        pcurves[vname][vindex] = pdf
     188    elif reg2.findall(v) != []:
     189        matches = reg2.findall(v)
     190        vname, xy, vindex = matches[0]
     191        if vname not in acurves:
     192            acurves[vname] = {}
     193        if vindex not in acurves[vname]:
     194            acurves[vname][vindex] = {}
     195        if xy == 'x':
     196            acurves[vname][vindex][0] = pdf
     197        else:
     198            acurves[vname][vindex][1] = pdf
    112199    else:
    113200        desc = h5['/output/data/%s' % v].attrs['description']
     
    121208    plot_pdf_curve(xvals, vname, 95, desc)
    122209
     210#for vname in acurves:
     211#    desc = h5['/output/data/%s.x[0]' % vname].attrs['description']
     212#    plot_pdf_acurve(h5, acurves, vname, 0, desc)
     213#    plot_pdf_acurve(h5, acurves, vname, 50, desc)
     214#    plot_pdf_acurve(h5, acurves, vname, 95, desc)
     215
    123216with open('run_uq.xml', 'w') as f:
    124217    f.write("<?xml version=\"1.0\"?>\n")
Note: See TracChangeset for help on using the changeset viewer.