py4sci

Table Of Contents

Previous topic

Using Output PDFs as Inputs

Next topic

Handling Correlated PDFs

This Page

Scripting PUQ

All the examples so far have involved using the ‘puq’ command to start a sweep and again to analyze it. For more complicated jobs, you can script the whole process in Python.

Running Sweeps

Example scripts are in puq/examples/scripting. test1.py runs a level-1 Smolyak sweep, reads the response surface from the HDF5 file, plots the PDF, then extends the level to 2 and repeats.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
"""
Example of scripting a PUQ run, extending it, and plotting PDFs
"""

import puq
import numpy as np
import matplotlib
matplotlib.use('tkagg', warn=False)
import matplotlib.pyplot as plt

# save to this filename
fname = 'script_test.hdf5'

# our input parameters
v1 = puq.Parameter('x', 'velocity1', mean=10, dev=2)
v2 = puq.Parameter('y', 'velocity2', mean=100, dev=3)

# run a level 1 Smolyak
uq = puq.Smolyak([v1, v2], 1)
sw = puq.Sweep(uq, puq.InteractiveHost(), './basic_prog.py')
sw.run(fname)

# get the response surface for output variable 'h'
rs = puq.hdf.get_response(fname, 'h')
# sample the response surface to get a PDF
val, samples = rs.pdf(fit=True, return_samples=True)
# plot it in blue
val.plot(color='b')
print 'Run 1: mean=%s   dev=%s\n' % (np.mean(samples), np.std(samples))

# not extend the run to a level 2
sw.extend()
sw.run()

# plot the PDF like before, but in green
rs = puq.hdf.get_response(fname, 'h')
val, samples = rs.pdf(fit=True, return_samples=True)
val.plot(color='g')
print 'Run 2: mean=%s   dev=%s' % (np.mean(samples), np.std(samples))

plt.show()

To run it, simply do:

./test1.py

or:

python test1.py

Reading Results

The previous example showed how to read a response surface and sample it to produce a pdf. The full list of python functions for reading and writing the HDF5 file is hdf - Functions for Accessing HDF5 Files