bayesvalidrox.pylink.pylink.PyLinkForwardModel¶
- class bayesvalidrox.pylink.pylink.PyLinkForwardModel(link_type='pylink', name=None, py_file=None, func_args={}, shell_command='', input_file=None, input_template=None, aux_file=None, exe_path='', output_file_names=[], output_names=[], output_parser='', multi_process=True, n_cpus=None, meas_file=None, meas_file_valid=None, mc_ref_file=None, obs_dict={}, obs_dict_valid={}, mc_ref_dict={}, store=True, out_dir='')¶
Bases:
object
A forward model binder
This calss serves as a code wrapper. This wrapper allows the execution of a third-party software/solver within the scope of BayesValidRox.
Attributes¶
- link_typestr
The type of the wrapper. The default is ‘pylink’. This runs the third-party software or an executable using a shell command with given input files. Second option is ‘function’ which assumed that model can be run using a function written separately in a Python script.
- namestr
Name of the model.
- py_filestr
Python file name without .py extension to be run for the ‘function’ wrapper. Note that the name of the python file and that of the function must be simillar. This function must recieve the parameters in an array of shape (n_samples, n_params) and returns a dictionary with the x_values and output arrays for given output names.
- func_argsdict
Additional arguments for the python file. The default is {}.
- shell_commandstr
Shell command to be executed for the ‘pylink’ wrapper.
- input_filestr or list
The input file to be passed to the ‘pylink’ wrapper.
- input_templatestr or list
A template input file to be passed to the ‘pylink’ wrapper. This file must be a copy of input_file with <Xi> place holder for the input parameters defined using inputs class, with i being the number of parameter. The file name ending should include .tpl before the actual extension of the input file, for example, params.tpl.input.
- aux_filestr or list
The list of auxiliary files needed for the ‘pylink’ wrapper.
- exe_pathstr
Execution path if you wish to run the model for the ‘pylink’ wrapper in another directory. The default is None, which corresponds to the currecnt working directory.
- output_file_nameslist of str
List of the name of the model output text files for the ‘pylink’ wrapper.
- output_nameslist of str
List of the model outputs to be used for the analysis.
- output_parserstr
Name of the model parser file (without .py extension) that recieves the output_file_names and returns a 2d-array with the first row being the x_values, e.g. x coordinates or time and the rest of raws pass the simulation output for each model output defined in output_names. Note that again here the name of the file and that of the function must be the same.
- multi_process: bool
Whether the model runs to be executed in parallel for the ‘pylink’ wrapper. The default is True.
- n_cpus: int
The number of cpus to be used for the parallel model execution for the ‘pylink’ wrapper. The default is None, which corresponds to all available cpus.
- meas_filestr
The name of the measurement text-based file. This file must contain x_values as the first column and one column for each model output. The default is None. Only needed for the Bayesian Inference.
- meas_file_validstr
The name of the measurement text-based file for the validation. The default is None. Only needed for the validation with Bayesian Inference.
- mc_ref_filestr
The name of the text file for the Monte-Carlo reference (mean and standard deviation) values. It must contain x_values as the first column, mean as the second column and std as the third. It can be used to compare the estimated moments using meta-model in the post- processing step. This is only available for one output.
- obs_dictdict
A dictionary containing the measurement text-based file. It must contain x_values as the first item and one item for each model output . The default is {}. Only needed for the Bayesian Inference.
- obs_dict_validdict
A dictionary containing the validation measurement text-based file. It must contain x_values as the first item and one item for each model output. The default is {}.
- mc_ref_dictdict
A dictionary containing the Monte-Carlo reference (mean and standard deviation) values. It must contain x_values as the first item and mean as the second item and std as the third. The default is {}. This is only available for one output.
- __init__(link_type='pylink', name=None, py_file=None, func_args={}, shell_command='', input_file=None, input_template=None, aux_file=None, exe_path='', output_file_names=[], output_names=[], output_parser='', multi_process=True, n_cpus=None, meas_file=None, meas_file_valid=None, mc_ref_file=None, obs_dict={}, obs_dict_valid={}, mc_ref_dict={}, store=True, out_dir='')¶
Methods
__init__
([link_type, name, py_file, ...])read_observation
([case])Reads/prepare the observation/measurement data for calibration.
Reads the the parser output file and returns it as an
run_command
(command, output_file_names)Runs the execution command given by the user to run the given model.
run_forwardmodel
(xx)This function creates subdirectory for the current run and copies the necessary files to this directory and renames them.
run_model_parallel
(c_points[, prevRun_No, ...])Runs model simulations. If mp is true (default), then the simulations
uMBridge_model
(params)Function that calls a UMBridge model and transforms its output into the shape expected for the surrogate.
update_input_params
(new_input_file, param_set)Finds this pattern with <X1> in the new_input_file and replace it with
zip_subdirs
(dir_name, key)Zips all the files containing the key(word).
- class OutputData(parser: str = '', names: list = None, file_names: list = None)¶
Bases:
object
- read_observation(case='calib')¶
Reads/prepare the observation/measurement data for calibration.
Parameters¶
- casestr
The type of observation to read in. Can be either ‘calib’, ‘valid’ or ‘mc_ref’
Returns¶
- DataFrame
A dataframe with the calibration data.
- read_output()¶
- Reads the the parser output file and returns it as an
executable function. It is required when the models returns the simulation outputs in csv files.
Returns¶
- Outputfunc
Output parser function.
- run_command(command, output_file_names)¶
Runs the execution command given by the user to run the given model. It checks if the output files have been generated. If yes, the jobe is done and it extracts and returns the requested output(s). Otherwise, it executes the command again.
Parameters¶
- commandstr
The shell command to be executed.
- output_file_nameslist
Name of the output file names.
Returns¶
- simulation_outputsarray of shape (n_obs, n_outputs)
Simulation outputs.
- run_forwardmodel(xx)¶
This function creates subdirectory for the current run and copies the necessary files to this directory and renames them. Next, it executes the given command.
Parameters¶
- xxtuple
A tuple including parameter set, simulation number and key string.
Returns¶
- outputarray of shape (n_outputs+1, n_obs)
An array passed by the output paraser containing the x_values as the first row and the simulations results stored in the the rest of the array.
- run_model_parallel(c_points, prevRun_No=0, key_str='', mp=True, verbose=True)¶
- Runs model simulations. If mp is true (default), then the simulations
are started in parallel.
Parameters¶
- c_pointsarray of shape (n_samples, n_params)
Collocation points (training set).
- prevRun_Noint, optional
Previous run number, in case the sequential design is selected. The default is 0.
- key_strstr, optional
A descriptive string for validation runs. The default is ‘’.
- mpbool, optional
Multiprocessing. The default is True.
- verbose: bool, optional
Verbosity. The default is True.
Returns¶
- all_outputsdict
A dictionary with x values (time step or point id) and all outputs. Each key contains an array of the shape (n_samples, n_obs).
- new_c_pointsarray
Updated collocation points (training set). If a simulation does not executed successfully, the parameter set is removed.
- uMBridge_model(params)¶
Function that calls a UMBridge model and transforms its output into the shape expected for the surrogate.
Parameters¶
- params2d np.array, shape (#samples, #params)
The parameter values for which the model is run.
Returns¶
- dict
The transformed model outputs.
- update_input_params(new_input_file, param_set)¶
- Finds this pattern with <X1> in the new_input_file and replace it with
the new value from the array param_sets.
Parameters¶
- new_input_filelist
List of the input files with the adapted names.
- param_setarray of shape (n_params)
Parameter set.
Returns¶
None.