The Model module
4. retrieve and interpret the results from the output of the executed program;
5. reformat the results and return them in the format required by UQ[PY]LAB.
2.2.4 Creating a UQ[PY]LAB MODEL plugin
The API for UQ[PY]LAB models is quite simple: a wrapper is a Python function of the form:
def myWrapper(X, Parameters):
where X is a N ×M numpy array containing N samples of the M input stochastic parameters,
Parameters is a free-form dictionary containing the static parameters used by the external
software (e.g.command line options, configuration file names or other configuration options).
The function returns a N ×N
out
numpy array of model responses as calculated by the external
software. The actual implementation of the wrapper is a complete black-box to UQ[PY]LAB.
A simple generic wrapper
Given the great variety of available modelling software, it is impossible to provide a de-
tailed example of a plugin that would be usable on a wide sample of systems. All the wrap-
pers, however, share a common structure that can be summarized in the following pseu-
docode:
import numpy as np
def myWrapper(X, Parameters):
# 1. retrieve the static configuration parameters
config1 = Parameters['Config1']
config2 = Parameters['Config2']
...
# 2. calculate the model response on each sample
Y = []
for x in X:
create_external_input_files(x, config1, config2, ...)
execute_external_code(...)
Y.append(retrieve_results(...))
return np.array(Y, ndmin=2)
The create_external_input_files(x, config1, config2, ...) function trans-
lates the inputs provided by the X matrix and Parameters dictionary into the format required
by the external code, one sample at a time.
The execute_external_code(...) line contains the necessary commands needed by
Python to execute the external code. It may be for example an external program executed
through the Python os.system command.
Finally, the Y.append(retrieve_results(...)) function retrieves the results from the
execution of the external program and appends them to a Python list Y. This function
can be a simple operation that reformats the output of the external software (e.g. if it was
a Python script), or a complex function that reads several files and merges their results
together. Eventually, the response variable Y is reformatted as a 2D numpy array (N × N
out
)
to ensure consistency.
UQ[PY]LAB-V0.9-103 - 9 -