Ignore:
Timestamp:
Jun 16, 2015 9:18:59 PM (9 years ago)
Author:
mmh
Message:

remove response surface code

File:
1 edited

Legend:

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

    r5528 r5706  
    1313from puq.jpickle import unpickle
    1414import xml.etree.ElementTree as xml
    15 from itertools import product, combinations
    1615import Rappture
    1716import StringIO
     
    3534        sw.psweep.reinit()
    3635    return sw
    37 
    38 
    39 # variable names to labels
    40 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 varlist
    49 
    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 = name
    58     ydata = h5['/output/data/%s' % name].value
    59     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'] = title
    65     curve['about.group'] = title
    66     curve['xaxis.label'] = varlist[0]
    67     curve['yaxis.label'] = tname
    68     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 surface
    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)
    80 
    81 
    82 def plot_resp2(io, resp, name, varl, h5):
    83     print("plot_resp2", name)
    84     numpoints = 50
    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]
    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 = name
    105     title = tname + ' (Response %s)' % ' vs '.join(varlist)
    106     fname['about.label'] = title
    107     fname['component.mesh'] = m2d.name
    108     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'] = pts
    122 
    123 
    124 def plot_resp3(io, resp, name, varl, h5):
    125     numpoints = 40
    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
    133 
    134     # for now, scale to [0,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
    141 
    142     varlist = subs_names(varl, h5)
    143     try:
    144         tname = h5['/output/data/%s' % name].attrs['label']
    145     except:
    146         tname = name
    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])
    151     title = tname + ' (Response %s)' % ' vs '.join(varlist)
    152     fname['about.label'] = title
    153     fname['component.mesh'] = m3d.name
    154     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 fastest
    161     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'] = pts
    174 
    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             return
    181         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 variables
    186         for v1, v2 in combinations(resp.vars, 2):
    187             plot_resp2(io, resp, name, [v1, v2], h5)
    18836
    18937
     
    338186#  [  34.52960806  163.81660645]
    339187#  [  55.47039194  346.04150004]]
     188
    340189
    341190def write_responses(io, h5):
     
    355204
    356205        rsp = h5['/%s/%s/response' % (uqtype, v)].value
     206
    357207        rout = io['output.response(%s)' % label]
    358208        rout['value'] = rsp
    359209        rout['about.description'] = desc
    360         rout['about.label'] = label
     210        rout['about.label'] = label + " (Response)"
    361211
    362212        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("'", '"')
    363216
    364217        if type(rs) == puq.response.ResponseFunc:
     
    439292uqtype = h5.attrs['UQtype']
    440293for 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)
    442297    pdf = rs.pdf(fit=False)
    443298    odata = h5['/output/data/%s' % v]
     
    514369    dtree.write(f)
    515370
    516 
    517371io = 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)
     372write_responses(io, h5)
    526373write_summary(io, h5)
    527 
    528374io.close()
     375
    529376h5.close()
Note: See TracChangeset for help on using the changeset viewer.