Ignore:
Timestamp:
May 17, 2015, 6:20:41 PM (9 years ago)
Author:
mmh
Message:

add uq summary output

File:
1 edited

Legend:

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

    r5468 r5528  
    44process the results.
    55"""
     6from __future__ import print_function
    67import sys
    78import os
     
    910import h5py
    1011import re
     12import puq
    1113from puq.jpickle import unpickle
    1214import xml.etree.ElementTree as xml
    1315from itertools import product, combinations
     16import Rappture
     17import StringIO
    1418
    1519# Redirect stdout and stderr to files for debugging.
     
    4650
    4751def plot_resp1(io, resp, name, h5):
    48     print 'plot_resp1', name
     52    print('plot_resp1', name)
    4953    varlist = subs_names(resp.vars, h5)
    5054    try:
    51         tname = h5['/output/data/%s' % name].attrs['description']
     55        tname = h5['/output/data/%s' % name].attrs['label']
    5256    except:
    5357        tname = name
     
    5660
    5761    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
    6368    x = np.linspace(*resp.vars[0][1], num=50)
    6469    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)
    6771
    6872    # 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)
    7780
    7881
    7982def plot_resp2(io, resp, name, varl, h5):
     83    print("plot_resp2", name)
    8084    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]
    9196
    9297    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]
    96101    try:
    97         tname = h5['/output/data/%s' % name].attrs['description']
     102        tname = h5['/output/data/%s' % name].attrs['label']
    98103    except:
    99104        tname = name
    100105    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'
    104109    x = np.linspace(*varl[0][1], num=numpoints)
    105110    y = np.linspace(*varl[1][1], num=numpoints)
     
    114119            allpts[:, i] = np.mean(v[1])
    115120    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
    118122
    119123
    120124def plot_resp3(io, resp, name, varl, h5):
    121125    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
    129133
    130134    # 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
    137141
    138142    varlist = subs_names(varl, h5)
    139143    try:
    140         tname = h5['/output/data/%s' % name].attrs['description']
     144        tname = h5['/output/data/%s' % name].attrs['label']
    141145    except:
    142146        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])
    153151    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'
    158156    x = np.linspace(*varl[0][1], num=numpoints)
    159157    y = np.linspace(*varl[1][1], num=numpoints)
     
    173171            allpts[:, i] = np.mean(v[1])
    174172    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
    177174
    178175
     
    193190# Plots probability curves
    194191def 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))
    196193    # compute upper and lower percentiles
    197194    pm = (100 - percent)/200.0
     
    251248# Plots advanced probability curves
    252249def 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))
    254251    # compute upper and lower percentiles
    255252    pm = (100 - percent)/200.0
     
    257254    vlen = len(acurves[vname])
    258255
    259     print 'vlen=', vlen
     256    print('vlen=', vlen)
    260257    # collect data into an array
    261258    xp = np.empty(vlen)
     
    265262
    266263    for vindex in sorted(acurves[vname].keys()):
    267         print 'vindex=', vindex
     264        print('vindex=', vindex)
    268265        x1 = acurves[vname][vindex][0].ppf(pp)
    269266        x2 = acurves[vname][vindex][0].ppf(pm)
     
    274271        #p1, p2 = get_closest2()
    275272        if int(vindex) == 2:
    276             print x1, x2, y1, y2
    277             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))
    279276
    280277        #xp[vindex] = x1
     
    320317    xy.text = pts
    321318
     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
     341def 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
     371def 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
     393def 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()
    322422
    323423sw = load_from_hdf5(sys.argv[1])
     
    365465            acurves[vname][vindex][1] = pdf
    366466    else:
    367         desc = h5['/output/data/%s' % v].attrs['description']
     467        desc = h5['/output/data/%s' % v].attrs['label']
    368468        plot_pdf(v, pdf, desc)
    369469
     
    388488        if '[' in v:
    389489            continue
    390         desc = h5['/output/data/%s' % v].attrs['description']
     490        desc = h5['/output/data/%s' % v].attrs['label']
    391491        sens = unpickle(h5['/%s/%s/sensitivity' % (uqtype, v)].value)
    392492        elem = xml.SubElement(dout, 'histogram', {'id': 'sens-%s' % v})
     
    415515
    416516
    417 import Rappture
    418 io = Rappture.library('run_uq.xml')
    419 
     517io = Rappture.PyXml('run_uq.xml')
    420518for v in h5[uqtype]:
    421519    if '[' in v:
    422520        continue
    423521    rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value)
    424     print 'Plotting response', v
     522    print ('Plotting response', v)
    425523    plot_resp(io, rs, v, h5)
    426524
    427 Rappture.result(io)
    428 
     525# write_responses(io, h5)
     526write_summary(io, h5)
     527
     528io.close()
    429529h5.close()
Note: See TracChangeset for help on using the changeset viewer.