Abstract base class for the SimulationWrapper.
This base class provides basic functions for serialization/deserialization and printing progress.
It cannot be used directly, but a derived class implementing the “simulate” method must be created.
This functions reads previous results from the hard disk (if present).
When reading from the array containing the results, the current
grid-index (i_grid) is considered to maintain the order of the results when the
SimulationModels are executed in parallel. If the function evaluated the results in parallel
internally, i_grid is a range [i_grid_min, i_grid_max].
Parameters:
coords (ndarray of float [n_sims x dim]) – Grid coordinates the simulations are conducted with
Returns:
None – if no serialized results could be found or does not fit to grid
p (dictionary) – Dictionary containing the model parameters
context (dictionary) –
dictionary that contains information about this worker’s context
- lock : reference to the Lock object that all processes share for synchronization
- max_grid : size of the current sub-grid that is processed
- global_task_counter : reference to the Value object that is shared among all processes to keep track
of the overall progress
seq_numbersequence number of the task this object represents; necessary to maintain the correct
sequence of results
fn_results : location of the hdf5 file to serialize the results to
i_grid : current iteration in the sub-grid that is processed
i_iter : current main-iteration
i_subiter : current sub-iteration
coords : parameters of particular simulation in original parameter space
coords_norm : parameters of particular simulation in normalized parameter space
This abstract method must be implemented by the subclass.
It should perform the simulation task depending on the input_values provided to the object on instantiation.
Parameters:
process_id (int) – A unique identifier; no two processes of the pool will run concurrently with the same identifier
matlab_engine (Matlab engine object) – Matlab engine to run Matlab models
This abstract method must be implemented by the subclass.
It should perform the validation task depending on the parameters defined in the problem.
In cases, the model may not run correctly for some parameter combinations, this function changes the definition
of the random parameters and the constants.
This function writes the data to a file on hard disk.
When writing the data the current grid-index (i_grid) is considered.
The data are written to the row corresponding i_grid in order to
maintain the order of the results when the SimulationModels are
executed in parallel.
Parameters:
data_dict (dict of ndarray) – Dictionary, containing the data to write in an .hdf5 file. The keys are the dataset names.
Choose type of error to validate gpc approximation. Use “loocv” (Leave-one-Out cross validation)
to omit any additional calculations and “nrmsd” (normalized root mean square deviation) to compare
against a Problem.ValidationSet.
Type of the calculation scheme to determine the gradient in the grid points
- “FD_fwd”: Finite difference forward approximation of the gradient using n_grid x dim additional sampling
points stored in self.grid.coords_gradient and self.grid.coords_gradient_norm [n_grid x dim x dim].
- “FD_1st”: Finite difference approximation of 1st order accuracy using only the available samples [1]
- “FD_2nd”: Finite difference approximation of 2nd order accuracy using only the available samples [1]
- “FD_1st2nd”: Finite difference approximation of 1st and (where possible) 2nd order accuracy
Ration between the number of model evaluations and the number of basis functions.
If “adaptive_sampling” is activated this factor is only used to
construct the initial grid depending on the initial number of basis functions determined by “order_start”.
(>1 results in an overdetermined system)
Number of validation points used to determine the NRMSD if chosen as “error_type”. Does not create a
validation set if there is already one present in the Problem instance (problem.validation).
Method to extend random grids when adaptive_sampling is turned on:
- “GPR”: Gaussian Process Regression (sample location is optimized according to posterior variance)
- “random”: Samples are added randomly
Check the validity of the results and resample if required.
Updates the gPC object, the containing grid, and the results array.
Parameters:
results (np.ndarray of float [n_samples x n_qoi]) – Model output at sampling points.
grid (Grid object instance) – Grid object instance the results are computed for.
gradient_results (ndarray of float [n_grid x n_out x dim], optional, default: None) – Gradient of model function in grid points.
gradient_results_idx (ndarray of int [n_grid], optional, default: None) – Indices of grid points where the gradient was evaluated.
com (Computation class instance, optional, default: None) – Computation class instance to run the model if resample is True.
resample (bool, optional, default: True) – Resample grid points and rerun model (requires Computational class instance to run model).
If False, the grid points and results are just deleted.
Returns:
results (np.ndarray of float [n_samples x n_qoi]) – Updated (fixed) model output at sampling points.
gpc (SGPC or MEGPC object instance or list of SGPC or MEGPC object instances) – GPC object(s) containing the basis functions and the updated grid.
gradient_results (ndarray of float [n_grid x n_out x dim]) – Updated (fixed) gradients of model function in grid points not containing the points where
the gradients were NaN.
gradient_results_idx (ndarray of int [n_grid], optional, default: None) – Updated (fixed) indices of grid points where the gradient was evaluated not containing the points where
the gradients were NaN.
grid (Grid object instance) – Updated (fixed) grid object instance the results are computed for not containing the grid points where
the results were NaN.
options["order_end"] (int, optional, default=10) – Maximum Gpc expansion order to expand to (algorithm will terminate afterwards)
options["interaction_order"] (int, optional, default=dim) – Define maximum interaction order of parameters (default: all interactions)
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["adaptive_sampling"] (boolean, optional, default: True) – Adds samples adaptively to the expansion until the error is converged and continues by
adding new basis functions.
options["n_samples_discontinuity"] (int, optional, default: 10) – Number of grid points close to discontinuity to refine its location
options["n_grid_init"] (int, optional, default: 10) – Number of initial simulations to explore the parameter space
problem (Problem object) – Object instance of gPC problem to investigate
options["method"] (str) – GPC method to apply [‘Reg’, ‘Quad’]
options["order"] (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
options["order_max"] (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["interaction_order"] (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
options["qoi"] (int or str, optional, default: 0) – Choose for which QOI the projection is determined for. The other QOIs use the same projection.
Alternatively, the projection can be determined for every QOI independently (qoi_index or “all”).
options["classifier"] (str, optional, default: "learning") – Classification algorithm to subdivide parameter domain.
- “learning” … ClassifierLearning algorithm based on Unsupervised and supervised learning
options["classifier_options"] (dict, optional, default: default settings) – Options of classifier
grid (Grid object instance) – Grid object to use for static gPC (Random, SparseGrid, TensorGrid)
Static gPC algorithm using Basis Projection approach
Parameters:
problem (Problem object) – Object instance of gPC problem to investigate
options["order"] (int) – Expansion order, each projected variable eta is expanded to.
Generates individual polynomials also if maximum expansion order in order_max is exceeded
options["order_max"] (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
options["interaction_order"] (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
options["qoi"] (int or str, optional, default: 0) – Choose for which QOI the projection is determined for. The other QOIs use the same projection.
Alternatively, the projection can be determined for every QOI independently (qoi_index or “all”).
options["n_grid_gradient"] (float, optional, default: 10) – Number of initial grid points to determine gradient and projection matrix
options["classifier"] (str, optional, default: "learning") – Classification algorithm to subdivide parameter domain.
- “learning” … ClassifierLearning algorithm based on Unsupervised and supervised learning
options["classifier_options"] (dict, optional, default: default settings) – Options of classifier
Multi-Element Static gPC algorithm using precomputed IO data
Parameters:
parameters (OrderedDict containing the RandomParameter class instances) – Dictionary (ordered) containing the properties of the random parameters
options["order"] (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
options["order_max"] (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["interaction_order"] (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
options["qoi"] (int or str, optional, default: 0) – Choose for which QOI the projection is determined for. The other QOIs use the same projection.
Alternatively, the projection can be determined for every QOI independently (qoi_index or “all”).
options["classifier"] (str, optional, default: "learning") – Classification algorithm to subdivide parameter domain.
- “learning” … ClassifierLearning algorithm based on Unsupervised and supervised learning
options["classifier_options"] (dict, optional, default: default settings) – Options of classifier
grid (Grid object instance) – Grid object to use for static gPC (Random, SparseGrid, TensorGrid) containing the parameter values, where the
output relations were calculated
results (ndarray of float [N_grid x N_qoi]) – Model output at each grid point for each QOI
options["order_end"] (int, optional, default=10) – Maximum Gpc expansion order to expand to (algorithm will terminate afterwards)
options["interaction_order"] (int, optional, default=dim) – Define maximum interaction order of parameters (default: all interactions)
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["adaptive_sampling"] (boolean, optional, default: True) – Adds samples adaptively to the expansion until the error is converged and continues by
adding new basis functions.
options["order_end"] (int, optional, default=10) – Maximum Gpc expansion order to expand to (algorithm will terminate afterwards)
options["interaction_order"] (int, optional, default=dim) – Define maximum interaction order of parameters (default: all interactions)
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["n_grid_gradient"] (float, optional, default: 10) – Number of initial grid points to determine gradient and projection matrix. When the algorithm goes
into the main interations the number will be increased depending on the options “matrix_ratio”
and “adaptive_sampling”.
options["qoi"] (int or str, optional, default: 0) – Choose for which QOI the projection is determined for. The other QOIs use the same projection.
Alternatively, the projection can be determined for every QOI independently (qoi_index or “all”).
options["adaptive_sampling"] (boolean, optional, default: True) – Adds samples adaptively to the expansion until the error is converged and continues by
adding new basis functions.
problem (Problem object) – Object instance of gPC problem to investigate
options["method"] (str) – GPC method to apply [‘Reg’, ‘Quad’]
options["order"] (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
options["order_max"] (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["interaction_order"] (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
grid (Grid object instance) – Grid object to use for static gPC (Random, SparseGrid, TensorGrid)
validation (Validation Set class instance, optional) – Validation set containing reference solutions at precomputed grid points
Static gPC algorithm using Basis Projection approach
Parameters:
problem (Problem object) – Object instance of gPC problem to investigate
options["method"] (str) – GPC method to apply [‘Reg’, ‘Quad’]
options["order"] (int) – Expansion order, each projected variable eta is expanded to.
Generates individual polynomials also if maximum expansion order in order_max is exceeded
options["order_max"] (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["interaction_order"] (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
options["qoi"] (int or str, optional, default: 0) – Choose for which QOI the projection is determined for. The other QOIs use the same projection.
Alternatively, the projection can be determined for every QOI independently (qoi_index or “all”).
options["n_grid"] (float, optional, default: 10) – Number of initial grid points to determine gradient and projection matrix
Static gPC algorithm, which uses precomputed input output relationships to construct the gPC approximation
Parameters:
parameters (OrderedDict containing the RandomParameter class instances) – Dictionary (ordered) containing the properties of the random parameters
options["order"] (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
options["order_max"] (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
options["order_max_norm"] (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
options["interaction_order"] (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
grid (Grid object instance) – Grid object to use for static gPC (Random, SparseGrid, TensorGrid) containing the parameter values, where the
output relations were calculated
results (ndarray of float [N_grid x N_qoi]) – Model output at each grid point for each QOI
validation (Validation Set class instance, optional) – Validation set containing reference solutions at precomputed grid points
Examples
>>> importpygpc>>> # initialize static gPC algorithm using precomputed IO relationships>>> algorithm=pygpc.Static_IO(parameters=parameters,options=options,grid=grid,results=results)>>> # run algorithm>>> gpc,coeffs=algorithm.run()
Parameter wise basis function objects used in gPC.
Multiplying all elements in a row at location xi = (x1, x2, …, x_dim) yields the global basis function.
Type:
list of list of BasisFunction object instances [n_basis][n_dim]
Extends polynomial basis coefficients for fast processing. Converts list of lists of b_added
into np.ndarray that can be processed on multi core systems.
Parameters:
b_added (list of list of BasisFunction instances [n_b_added][dim]) – Individual BasisFunctions to add
Initialize polynomial basis coefficients for fast processing. Converts list of lists of self.b
into np.ndarray that can be processed on multi core systems.
Construct array of scaling factors self.b_norm [n_basis x dim] and self.b_norm_basis [n_basis x 1]
to normalize basis functions <psi^2> = int(psi^2*p)dx
order (list of int [dim]) – Maximum individual expansion order
Generates individual polynomials also if maximum expansion order in order_max is exceeded
order_max (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
order_max_norm (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
interaction_order (int) – Number of random variables, which can interact with each other
interaction_order_current (int, optional, default: interaction_order) – Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with “interaction order”.
b: list of BasisFunction object instances [n_basis x n_dim]
Parameter wise basis function objects used in gPC.
Multiplying all elements in a row at location xi = (x1, x2, …, x_dim) yields the global basis function.
Worker function to initialize a global basis function (called by multiprocessing.pool).
It also initializes polynomial basis coefficients for fast processing. Converts list of lists of basis
into np.ndarray that can be processed on multi core systems.
Parameters:
i_basis (int) – Index of global basis function
problem (Problem class instance) – gPC problem
Returns:
b_ (list [n_dim]) – List containing the individual basis functions of the parameters
b_a_ (ndarray of int) – Concatenated list of polynomial basis coefficients
b_a_grad_ (ndarray of int) – Concatenated list of polynomial basis coefficients for gradient evaluation
Sets up polynomial basis self.b for given order, order_max_norm and interaction order. Adds only the basis
functions, which are not yet included.
Parameters:
order (list of int) – Maximum individual expansion order
Generates individual polynomials also if maximum expansion order in order_max is exceeded
order_max (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
order_max_norm (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more.
sum(a_i^q)^1/q <= p, where p is order_max and q is order_max_norm (for more details see eq (11) in [1]).
interaction_order (int) – Number of random variables, which can interact with each other
All polynomials are ignored, which have an interaction order greater than specified
interaction_order_current (int, optional, default: interaction_order) – Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with interaction_order.
problem (Problem class instance) – GPC Problem to analyze
Abstract class of basis functions.
This base class provides basic properties and methods for the basis functions.
It cannot be used directly, but inherits properties and methods to the specific basis function sub classes.
Parameters:
p (dict) – Parameters of the polynomial (see subclasses for details)
Helper function to initialize the Computation class.
n_cpu = 0 : use this if the model is capable of to evaluate several parameterizations in parallel
n_cpu = 1 : the model is called in serial for every paramerization.
n_cpu > 1 : A multiprocessing.Pool will be opened and n_cpu parameterizations are calculated in parallel
Parameters:
n_cpu (int) – Number of CPU cores to use (parallel model evaluations)
matlab_model (boolean, optional, default: False) – Use a Matlab model
problem (Problem class instance) – GPC Problem under investigation
options (dict) – Options of gPC algorithm
validation (ValidationSet object (optional)) – Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
Maximal possible length of new axis in eta space. Since the projected variables are modelled in
the normalized space between [-1, 1], the transformed coordinates need to be scaled.
b (list of BasisFunction object instances [n_basis][n_dim]) – Parameter wise basis function objects used in gPC (Basis.b)
Multiplying all elements in a row at location xi = (x1, x2, …, x_dim) yields the global basis function.
x (ndarray of float [n_x x n_dim]) – Coordinates of x = (x1, x2, …, x_dim) where the rows of the gPC matrix are evaluated (normalized [-1, 1])
verbose (bool, optional, default: False) – boolean value to determine if to print out the progress into the standard output
Returns:
gpc_matrix – GPC matrix where the columns correspond to the basis functions and the rows the to the sample coordinates.
If gradient_idx!=None, the gradient is returned at the specified sample coordinates point by point.
Creates a ValidationSet instance (calls the model)
Parameters:
n_samples (int) – Number of sampling points contained in the validation set
n_cpu (int) – Number of parallel function evaluations to evaluate validation set (n_cpu=0 assumes that the
model is capable to evaluate all grid points in parallel)
coeffs (ndarray of float [n_basis x n_out]) – GPC coefficients for each output variable
x (ndarray of float [n_x x n_dim]) – Coordinates of x = (x1, x2, …, x_dim) where the rows of the gPC matrix are evaluated (normalized [-1, 1]).
The coordinates will be transformed in case of projected gPC.
output_idx (ndarray of int, optional, default=None [n_out]) – Indices of output quantities to consider (Default: all).
Returns:
pce – GPC approximation at normalized coordinates x.
Perform leave-one-out cross validation of gPC approximation and add error value to self.relative_error_loocv.
The loocv error is calculated analytically after eq. (35) in [1] but omitting the “1 - “ term, i.e. it
corresponds to 1 - Q^2.
coeffs (ndarray of float [n_coeffs x n_out]) – GPC coefficients
n_samples (int) – Number of samples used to estimate output pdfs
output_idx (ndarray, optional, default=None [1 x n_out]) – Index of output quantities to consider (if output_idx=None, all output quantities are considered)
filter (bool, optional, default: True) – Use savgol_filter to smooth probability density
return_samples (bool, optional, default: False) – Additionally returns in and output samples with which the pdfs were computed
Returns:
pdf_x (ndarray of float [100 x n_out]) – x-coordinates of output pdfs of output quantities
pdf_y (ndarray of float [100 x n_out]) – y-coordinates of output pdfs (probability density of output quantity)
samples_in (ndarray of float [n_samples x dim] (optional)) – Input samples (if return_samples=True)
samples_out (ndarray of float [n_samples x n_out] (optional)) – Output samples (if return_samples=True)
Replace distinct sample points from the gPC matrix with new ones.
GPC.replace_gpc_matrix_samples(idx, seed=None)
Parameters:
idx (ndarray of int [n_samples]) – Array of grid indices of grid.coords[idx, :] which are going to be replaced
(rows of gPC matrix will be replaced by new ones)
seed (float, optional, default=None) – Random seeding point
Save gPC matrix and gPC gradient matrix in .hdf5 file <”fn_results” + “.hdf5”> under the key “gpc_matrix”
and “gpc_matrix_gradient”. If matrices are already present, check for equality and save only appended
rows and columns.
Parameters:
hdf5_path_gpc_matrix (str) – Path in .hdf5 file, where the gPC matrix is saved in
hdf5_path_gpc_matrix_gradient (str) – Path in .hdf5 file, where the gPC gradient matrix is saved in
results ([n_grid x n_out] np.ndarray of float) – Results from simulations with N_out output quantities
gradient_results (ndarray of float [n_gradient x n_out x dim], optional, default: None) – Gradient of results in original parameter space in specific grid points
solver (str) – Solver to determine the gPC coefficients
- ‘Moore-Penrose’ … Pseudoinverse of gPC matrix (SGPC.Reg, EGPC)
- ‘OMP’ … Orthogonal Matching Pursuit, sparse recovery approach (SGPC.Reg, EGPC)
- ‘LarsLasso’ … Least-Angle Regression using Lasso model (SGPC.Reg, EGPC)
- ‘NumInt’ … Numerical integration, spectral projection (SGPC.Quad)
matrix (ndarray of float, optional, default: self.gpc_matrix or [self.gpc_matrix, self.gpc_matrix_gradient]) – Matrix to invert. Depending on gradient_enhanced option, this matrix consist of the standard gPC matrix and
their derivatives.
verbose (bool) – boolean value to determine if to print out the progress into the standard output
Update gPC matrix and gPC matrix gradient according to existing self.grid and self.basis.
Call this method when self.gpc_matrix does not fit to self.grid and self.basis objects anymore
The old gPC matrix with their self.gpc_matrix_b_id and self.gpc_matrix_coords_id is compared
to self.basis.b_id and self.grid.coords_id. New rows and columns are computed if differences are found.
Validate gPC approximation using the ValidationSet object contained in the Problem object.
Determines the normalized root mean square deviation between the gpc approximation and the
original model. Skips this step if no validation set is present
Parameters:
coeffs (ndarray of float [n_coeffs x n_out]) – GPC coefficients
results (ndarray of float [n_grid x n_out]) – Results from n_grid simulations with n_out output quantities
gradient_results (ndarray of float [n_grid x n_out x dim], optional, default: None) – Gradient of results in original parameter space (tensor)
qoi_idx (int, optional, default: None) – Index of QOI to validate (if None, all QOI are considered)
Returns:
error – Estimated difference between gPC approximation and original model
parameters_random (OrderedDict of RandomParameter instances) – OrderedDict containing the RandomParameter instances the grids are generated for
n_grid (int) – Number of random samples to generate
seed (float) – Seeding point to replicate random grids
options (dict, optional, default=None) – Grid options:
- ‘n_pool’ : number of random samples to determine next optimal grid point
- ‘seed’ : random seed
coords (ndarray of float [n_grid_add x dim]) – Grid points to add (model space)
coords_norm (ndarray of float [n_grid_add x dim]) – Grid points to add (normalized space)
coords_gradient (ndarray of float [n_grid x dim x dim]) – Denormalized coordinates xi
coords_gradient_norm (ndarray of float [n_grid x dim x dim]) – Normalized coordinates xi
coords_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
coords_gradient_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
Grid options:
- method: “greedy”, “iteration”
- criterion: [“mc”], [“tmc”, “cc”]
- weights: [1], [0.5, 0.5]
- n_pool: size of samples in pool to choose greedy results from
- n_iter: number of iterations
- seed: random seed
Creates new grid points to determine gradient of model function.
Adds or updates self.coords_gradient, self.coords_gradient_norm and self.coords_gradient_id.
Parameters:
delta (float, optional, default: 1e-3) – Shift of grid-points along axes in normalized parameter space
Grid options:
- method: “greedy”, “iteration”
- criterion: [“mc”], [“tmc”, “cc”], [“D”], [“D-coh”]
- weights: [1], [0.5, 0.5], [1]
- n_pool: size of samples in pool to choose greedy results from
- n_iter: number of iterations
- seed: random seed
parameters_random (OrderedDict of RandomParameter instances) – OrderedDict containing the RandomParameter instances the grids are generated for
n_grid (int) – Number of random samples to generate
seed (float) – Seeding point to replicate random grids
options (dict, optional, default=None) – Grid options:
- ‘corr’ : optimizes design points in their spearman correlation coefficients
- ‘maximin’ or ‘m’ : optimizes design points in their maximum minimal distance using the Phi-P criterion
- ‘ese’ : uses an enhanced evolutionary algorithm to optimize the Phi-P criterion
coords (ndarray of float [n_grid_add x dim]) – Grid points to add (model space)
coords_norm (ndarray of float [n_grid_add x dim]) – Grid points to add (normalized space)
coords_gradient (ndarray of float [n_grid x dim x dim]) – Denormalized coordinates xi
coords_gradient_norm (ndarray of float [n_grid x dim x dim]) – Normalized coordinates xi
coords_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
coords_gradient_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
Grid options:
- method: “greedy”, “iteration”
- criterion: [“mc”], [“tmc”, “cc”]
- weights: [1], [0.5, 0.5]
- n_pool: size of samples in pool to choose greedy results from
- n_iter: number of iterations
- seed: random seed
Calculate the L2 discrepancy of the design
The discrepancy is a measure of the difference between the empirical cumulative distribution function
of an experimental design and the uniform cumulative distribution function [1].
Parameters:
array (ndarray of float [m x n]) – Array with n rows of samples and m columns of variables/dimensions
parameters_random (OrderedDict of RandomParameter instances) – OrderedDict containing the RandomParameter instances the grids are generated for
n_grid (int) – Number of random samples to generate
options (dict, optional, default=None) – Grid options:
- ‘corr’ : optimizes design points in their spearman correlation coefficients
- ‘maximin’ or ‘m’ : optimizes design points in their maximum minimal distance using the Phi-P criterion
- ‘ese’ : uses an enhanced evolutionary algorithm to optimize the Phi-P criterion
coords (ndarray of float [n_grid_add x dim]) – Grid points to add (model space)
coords_norm (ndarray of float [n_grid_add x dim]) – Grid points to add (normalized space)
coords_gradient (ndarray of float [n_grid x dim x dim]) – Denormalized coordinates xi
coords_gradient_norm (ndarray of float [n_grid x dim x dim]) – Normalized coordinates xi
coords_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
coords_gradient_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
Grid options:
- method: “greedy”, “iteration”
- criterion: [“mc”], [“tmc”, “cc”]
- weights: [1], [0.5, 0.5]
- n_pool: size of samples in pool to choose greedy results from
- n_iter: number of iterations
- seed: random seed
Add sample points according to input pdfs to grid (old points are kept). Define either the new total number of
grid points with “n_grid_new” or add grid-points manually by providing “coords” and “coords_norm”.
results (np.ndarray of float [n_grid x n_qoi]) – Results computed so far before adding additional grid points
type (str, optional, default: None) – Type of adding new grid points
- “GP”: Gaussian process regression (points are added where the uncertainty of sampling is very high).
Does only work for Random, LHS, and GP grids.
- None: grid points are added according to the grid type.
results (np.ndarray of float [n_grid x n_qoi]) – Results computed so far before adding additional grid points
type (str, optional, default: None) – Type of adding new grid points
- “GP”: Gaussian process regression (points are added where the uncertainty of sampling is very high).
Does only work for Random, LHS, and GP grids.
- None: grid points are added according to the grid type.
parameters_random (OrderedDict of RandomParameter instances) – OrderedDict containing the RandomParameter instances the grids are generated for
options (dict) – Grid options
- parameters[“grid_type”] … list of str [dim]: type of grid (‘jacobi’, ‘hermite’, ‘cc’, ‘fejer2’)
- parameters[“n_dim”] … list of int [dim]: Number of nodes in each dimension
coords (ndarray of float [n_grid_add x dim]) – Grid points to add (model space)
coords_norm (ndarray of float [n_grid_add x dim]) – Grid points to add (normalized space)
coords_gradient (ndarray of float [n_grid x dim x dim]) – Denormalized coordinates xi
coords_gradient_norm (ndarray of float [n_grid x dim x dim]) – Normalized coordinates xi
coords_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
coords_gradient_id (list of UUID objects (version 4) [n_grid]) – Unique IDs of grid points
knots_dim_list (list of float [dim][n_knots]) – Knots of polynomials in each dimension
weights_dim_list (list of float [dim][n_knots]) – Weights of polynomials in each dimension
weights (ndarray of float [n_grid]) – Quadrature weights for each grid point
Grid options
- parameters[“grid_type”] … list of str [dim]: type of grid (‘jacobi’, ‘hermite’, ‘cc’, ‘fejer2’)
- parameters[“n_dim”] … list of int [dim]: Number of nodes in each dimension
problem (Problem class instance) – GPC Problem under investigation
options (dict) – Options of gPC algorithm
validation (ValidationSet object (optional)) – Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
Creates a ValidationSet instance (calls the model)
Parameters:
n_samples (int) – Number of sampling points contained in the validation set
n_cpu (int) – Number of parallel function evaluations to evaluate validation set (n_cpu=0 assumes that the
model is capable to evaluate all grid points in parallel)
gradient (bool, optional, default: False) – Determine gradient of results in each grid points
Determine the local derivative based sensitivity coefficients in the point of interest x
(normalized coordinates [-1, 1]).
local_sens = MEGPC.calc_localsens(coeffs, x)
Parameters:
coeffs (list of ndarray of float [n_gpc][n_basis x n_out]) – GPC coefficients
x (ndarray of float [n_points x dim], optional, default: center of parameter space) – Points in variable space to evaluate local sensitivity in (normalized coordinates [-1, 1])
(original parameter space)
Returns:
local_sens – Local sensitivity of output quantities in point x
coeffs (list of ndarray of float [n_gpc][n_coeffs x n_out]) – GPC coefficients
n_samples (int) – Number of samples used to estimate output pdfs
output_idx (ndarray, optional, default=None [1 x n_out]) – Index of output quantities to consider (if output_idx=None, all output quantities are considered)
Returns:
pdf_x (ndarray of float [100 x n_out]) – x-coordinates of output pdfs of output quantities
pdf_y (ndarray of float [100 x n_out]) – y-coordinates of output pdfs (probability density of output quantity)
coeffs (list of ndarray of float [n_gpc][n_basis x n_out]) – GPC coefficients
n_samples (int, optional, default: 1e4) – Number of samples to determine Sobol indices by sampling. The efficient number of samples
increases to n_samples * (2*dim + 2) in Saltelli’s Sobol sampling sequence.
Returns:
sobol (ndarray of float [n_sobol x n_out]) – Normalized Sobol indices w.r.t. total variance
sobol_idx (list of ndarray of int [n_sobol x (n_sobol_included)]) – Parameter combinations in rows of sobol.
sobol_idx_bool (ndarray of bool [n_sobol x dim]) – Boolean mask which contains unique multi indices.
Perform leave-one-out cross validation of gPC approximation and add error value to self.relative_error_loocv.
The loocv error is calculated analytically after eq. (35) in [1] but omitting the “1 - “ term, i.e. it
corresponds to 1 - Q^2.
Save gPC matrix and gPC gradient matrix in .hdf5 file <”fn_results” + “.hdf5”> under the key “gpc_matrix/dom_x”
and “gpc_matrix_gradient/dom_x”. If matrices are already present, check for equality and save only appended
rows and columns.
results (ndarray of float [n_grid x n_out]) – Results from simulations with n_out output quantities
gradient_results (ndarray of float [n_gradient x n_out x dim], optional, default: None) – Gradient of results in original parameter space in specific grid points
solver (str) – Solver to determine the gPC coefficients
- ‘Moore-Penrose’ … Pseudoinverse of gPC matrix (SGPC.Reg, EGPC)
- ‘OMP’ … Orthogonal Matching Pursuit, sparse recovery approach (SGPC.Reg, EGPC)
- ‘LarsLasso’ … Least-Angle Regression using Lasso model (SGPC.Reg, EGPC)
- ‘NumInt’ … Numerical integration, spectral projection (SGPC.Quad)
Update gPC matrix according to existing self.grid and self.basis.
Call this method when self.gpc_matrix does not fit to self.grid and self.basis objects anymore
The old gPC matrix with their self.gpc_matrix_b_id and self.gpc_matrix_coords_id is compared
to self.basis.b_id and self.grid.coords_id. New rows and columns are computed when differences are found.
Validate gPC approximation using the ValidationSet object contained in the Problem object.
Determines the normalized root mean square deviation between the gpc approximation and the
original model. Skips this step if no validation set is present
Parameters:
coeffs (list of ndarray of float [n_gpc][n_coeffs x n_out]) – GPC coefficients
results (ndarray of float [n_grid x n_out]) – Results from n_grid simulations with n_out output quantities
gradient_results (ndarray of float [n_grid x n_out x dim], optional, default: None) – Gradient of results in original parameter space (tensor)
output_idx (int or list of int) – Index of the QOI the provided coeffs and results are referring to. The correct QOI will be
selected from the validation set in case of nrmsd error.
Returns:
error – Estimated difference between gPC approximation and original model
upper and lower bounds of random variables
beta-dist: [[], … [min, max], …, []]
normal-dist: [[], … [0, 0], …, []] (not used)
Examples
Setup model and specify parameters of gPC problem
>>> importpygpc>>> fromcollectionsimportOrderedDict>>>>>> # Define model>>> model=pygpc.testfunctions.SphereModel>>>>>> # Define Problem>>> parameters=OrderedDict()# we must use an ordered dict form the start, otherwise the order will be mixed>>> parameters["R"]=[80,90,100]# constant parameter>>> parameters["phi_electrode"]=15# ">>> parameters["N_points"]=201# ">>> parameters["sigma_1"]=pygpc.RandomParameter.Beta(pdf_shape=[5,5],pdf_limits=[0.15,0.45])# random variable>>> parameters["sigma_2"]=pygpc.RandomParameter.Beta(pdf_shape=[1,3],pdf_limits=[0.01,0.02])# ">>> parameters["sigma_3"]=pygpc.RandomParameter.Norm(pdf_shape=[2,2])# ">>> problem=pygpc.Problem(model,parameters)
Verifies the problem, by testing if the parameters including the random variables are defined appropriate.
In cases, the model may not run correctly for some parameter combinations, the user may change the definition
of the random parameters or the constants in model.validate.
pdf_shape (list of float [3]) – Shape parameters of gamma distributed random variable [shape, rate, loc] (=[alpha, beta, location])
p_perc (float, optional, default=0.9973) – Probability of percentile, where infinite distributions are cut off
(default value corresponds to 6 sigma from normal distribution)
problem (Problem class instance) – GPC Problem under investigation
order (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
order_max (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
order_max_norm (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
interaction_order (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
interaction_order_current (int, optional, default: interaction_order) – Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with “interaction order”.
options (dict) – Options of gPC
validation (ValidationSet object (optional)) – Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
problem (Problem class instance) – GPC Problem under investigation
order (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
order_max (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
order_max_norm (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
interaction_order (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
interaction_order_current (int, optional, default: interaction_order) – Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with “interaction order”.
options (dict) – Options of gPC
validation (ValidationSet object (optional)) – Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
problem (Problem class instance) – GPC Problem under investigation
order (list of int [dim]) – Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
order_max (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
order_max_norm (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
interaction_order (int) – Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
interaction_order_current (int, optional, default: interaction_order) – Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with “interaction order”.
options (dict) – Options of gPC
validation (ValidationSet object (optional)) – Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
Maximum individual expansion order [order_1, order_2, …, order_dim].
Generates individual polynomials also if maximum expansion order in order_max is exceeded
Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more. This truncation scheme
is also referred to “hyperbolic polynomial chaos expansion” such that sum(a_i^q)^1/q <= p,
where p is order_max and q is order_max_norm (for more details see eq. (27) in [1]).
Number of random variables, which can interact with each other.
All polynomials are ignored, which have an interaction order greater than the specified
Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with “interaction order”.
Object containing a set of validation points and corresponding solutions. Can be used
to validate gpc approximation setting options[“error_type”]=”nrmsd”.
- grid: Grid object containing the validation points (grid.coords, grid.coords_norm)
- results: ndarray [n_grid x n_out] results
coeffs (ndarray of float [n_basis x n_out], optional, default: None) – GPC coefficients
algorithm (str, optional, default: "standard") – Algorithm to determine the Sobol indices
- “standard”: Sobol indices are determined from the gPC coefficients
- “sampling”: Sobol indices are determined from sampling using Saltelli’s Sobol sampling sequence [1, 2, 3]
n_samples (int, optional, default: 1e4) – Number of samples
Returns:
global_sens – Global derivative based sensitivity coefficients
Determine the local derivative based sensitivity coefficients in the point of interest x
(normalized coordinates [-1, 1]).
local_sens = SGPC.calc_localsens(coeffs, x)
Parameters:
coeffs (ndarray of float [n_basis x n_out]) – GPC coefficients
x (ndarray of float [dim], optional, default: center of parameter space) – Point in variable space to evaluate local sensitivity in (normalized coordinates [-1, 1])
Returns:
local_sens – Local sensitivity of output quantities in point x
Calculate the available sobol indices from the gPC coefficients (standard) or by sampling.
In case of sampling, the Sobol indices are calculated up to second order.
coeffs (ndarray of float [n_basis x n_out]) – GPC coefficients
algorithm (str, optional, default: "standard") – Algorithm to determine the Sobol indices
- “standard”: Sobol indices are determined from the gPC coefficients
- “sampling”: Sobol indices are determined from sampling using Saltelli’s Sobol sampling sequence [1, 2, 3]
n_samples (int, optional, default: 1e4) – Number of samples to determine Sobol indices by sampling. The efficient number of samples
increases to n_samples * (2*dim + 2) in Saltelli’s Sobol sampling sequence.
Returns:
sobol (ndarray of float [n_sobol x n_out]) – Normalized Sobol indices w.r.t. total variance
sobol_idx (list of ndarray of int [n_sobol x (n_sobol_included)]) – Parameter combinations in rows of sobol.
sobol_idx_bool (ndarray of bool [n_sobol x dim]) – Boolean mask which contains unique multi indices.
Add a new subplot to the current visualization, so that multiple graphs can be overlaid onto one chart
(e.g. scatterplot over heatmap).
create_new_chart(layout_id=None)
Parameters:
layout_id ((3-digit) int, optional, default=None) – Denoting the position of the graph in figure (xyn : ‘x’=width, ‘y’=height of grid, ‘n’=position within grid)
Blue, white, and red color map.
This function is designed to generate a blue to red colormap. The color of the colorbar is from blue to white and
then to red, corresponding to the data values from negative to zero to positive, respectively.
The color white always corresponds to value zero. The brightness of blue and red will change according to your
setting, so that the brightness of the color corresponded to the color of his opposite number.
Parameters:
cmin_input (float) – Minimum value of data
cmax_input (float) – Maximum value of data
Returns:
newmap – Colormap
Return type:
ndarray of float [N_RGB x 3]
Examples
>>> b2rcw_cmap_1=make_cmap(b2rcw(-3,6))# is from light blue to deep red>>> b2rcw_cmap_2=make_cmap(b2rcw(-3,3))# is from deep blue to deep red
make_cmap takes a list of tuples which contain RGB values. The RGB
values may either be in 8-bit [0 to 255] (in which bit must be set to
True when called) or arithmetic [0 to 1] (default). make_cmap returns
a cmap with equally spaced colors.
Arrange your tuples so that the first color is the lowest value for the
colorbar and the last is the highest.
Parameters:
colors (list of 3-tuples [n_rgb]) – RGB values. The RGB values may either be in 8-bit [0 to 255] (in which bit must be set to True when called)
or arithmetic [0 to 1] (default).
position (ndarray of float [n_rgb], optional, default=None) – Contains values from 0 to 1 to dictate the location of each color.
bit (boolean, optional, default=False) – Defines if colors are in 8-bit [0 to 255] (True) or arithmetic [0 to 1] (False)
sobol_rel_order_mean (ndarray of float [n_sobol]) – Average proportion of the Sobol indices of the different order to the total variance (1st, 2nd, etc..,)
over all output quantities
sobol_rel_1st_order_mean (ndarray of float [dim]) – Average proportion of the random variables of the 1st order Sobol indices to the total variance over all
output quantities
fn_plot (str) – Filename of plot
random_vars ([dim] list of str) – String labels of the random variables
This is a wrapper script to be called by the ‘multiprocessing.map’ function
to calculate the model functions in parallel.
This function will be called upon initialization of the process.
It sets a global variable denoting the ID of this process that can
be read by any function of this process
Parameters:
queue (multiprocessing.Queue) – the queue object that manages the unique IDs of the process pool
If data is list of dict, write_dict_to_hdf5() is called for each dict with adapted hdf5-folder name
Otherwise, data is casted to np.ndarray and dtype of unicode data casted to ‘|S’.
Parameters:
fn_hdf5 (str) – Filename of .hdf5 file
arr_name (str) – Complete path in .hdf5 file with array name
Write average ratios of Sobol indices into logfile.
Parameters:
fname (str) – Path of logfile
random_vars (list of str) – Labels of random variables
sobol_rel_order_mean (np.ndarray) – Average proportion of the Sobol indices of the different order to the total variance (1st, 2nd, etc..,).
(over all output quantities)
sobol_rel_1st_order_mean (np.ndarray) – Average proportion of the random variables of the 1st order Sobol indices to the total variance.
(over all output quantities)
sobol_extracted_idx_1st (list of int [N_sobol_1st]) – Indices of extracted 1st order Sobol indices corresponding to SGPC.random_vars.
Returns:
<File> – Logfile containing information about the average ratios of 1st order Sobol indices w.r.t. the total variance
gradient_results (ndarray of float [n_grid x n_out x dim]) – Gradient of model function in grid points
lambda_eps (float, optional, default: 0.95) – Bound of principal components in %. All eigenvectors are included until lambda_eps of total sum of all
eigenvalues is included in the system.
Returns:
p_matrix (ndarray of float [dim_reduced x dim]) – Reduced projection matrix for QOI.
p_matrix_complete (ndarray of float [dim_reduced x dim]) – Complete projection matrix for QOI.
pygpc.misc.display_fancy_bar(text, i, n_i, more_text=None)[source]#
Display a simple progress bar. Call in each iteration and start with i=1.
Parameters:
text (str) – Text to display in front of actual iteration
i (str or int) – Actual iteration
n_i (int) – Total number of iterations
more_text (str, optional, default=None) – Text that is displayed on an extra line above the bar.
Examples
fancy_bar(‘Run’,7,10):
Run 07 from 10 [================================ ] 70%
fancy_bar(Run,9,10,’Some more text’):
Some more text
Run 09 from 10 [======================================= ] 90%
Compute all k-tuples (e_1, e_2, …, e_k) of combinations of the set of elements of the input array where
e_n+1 > e_n.
combinations = get_all_combinations(array, number_elements)
:param array: Array to perform the combinatorial problem with
:type array: np.ndarray
:param number_elements: Number of elements in tuple
:type number_elements: int
data (ndarray of float) – Data to fit beta distribution on
beta_tolerance (float or list of float, optional, default=0) – Specifies bounds of beta distribution. If float, it calculates the tolerance
from observed data, e.g. 0.2 (+-20% tolerance on observed max and min value).
If list, it takes [min, max] as bounds [a, b].
uni_interval (float or list of float, optional, default=0) – uniform distribution interval. If float, the bounds are defined as a fraction of the beta distribution
interval (e.g. 0.95 (95%)). If list, it takes [min, max] as bounds [a, b].
fn_plot (str) – Filename of plot so save (.pdf and .png)
Returns:
beta_parameters (list of float [4]) – Two shape parameters and lower and upper limit [p, q, a, b]
moments (list of float [4]) – Mean and std of raw data and fitted beta distribution [data_mean, data_std, beta_mean, beta_std]
p_value (float) – p-value of the Kolmogorov Smirnov test
uni_parameters (list of float [2]) – Lower and upper limits of uniform distribution [a, b]
Compares rows from matrix a with rows from matrix b. It is assumed that b contains rows from a.
The function returns the rows of b, which are not included in a.
Parameters:
a (ndarray of float [m1 x n]) – First matrix (usually the smaller one), where rows are part of b
b (ndarray of float [m2 x n]) – Second matrix (usually the larger one), containing rows of a
order (list of int [dim]) – Maximum individual expansion order
Generates individual polynomials also if maximum expansion order in order_max is exceeded
order_max (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
order_max_norm (float) – Norm for which the maximum global expansion order is defined [0, 1]. Values < 1 decrease the total number
of polynomials in the expansion such that interaction terms are penalized more.
sum(a_i^q)^1/q <= p, where p is order_max and q is order_max_norm (for more details see eq (11) in [1]).
interaction_order (int) – Number of random variables, which can interact with each other
interaction_order_current (int, optional, default: interaction_order) – Number of random variables currently interacting with respect to the highest order.
(interaction_order_current <= interaction_order)
The parameters for lower orders are all interacting with interaction_order.
Returns:
multi_indices – Multi-indices for a maximum order gPC assuming a certain order norm.
Extract possible multi-indices from a given set which are potential candidates for anisotropic basis extension.
Two criteria must be met:
(1) The basis function may not be completely enclosed by already existing basis functions. In this case the added
basis would be already included in any case.
The basis function is not a candidate if adding any basis would have no predecessors. The new basis must have
predecessors in all decreasing direction and may not “float”.
Parameters:
multi_indices (ndarray of float [n_basis, dim]) – Array of multi-indices of basis functions
interaction_order (int) – Allowed interaction order between variables (<= dim)
Returns:
multi_indices_non_enclosed (ndarray of float [n_basis_candidates, dim]) – Array of possible multi-indices of basis functions
poly_indices_non_enclosed (list of int) – Indices of selected basis functions in global “multi-indices” array
Calculate the number of gPC coefficients for a specific maximum order in each dimension “order_dim_max”,
global maximum order “order_glob_max” and the interaction order “order_inter_max”.
order_dim_max (ndarray of int or list of int [dim]) – Maximum order in each dimension
order_glob_max (int) – Maximum global order of interacting polynomials
order_inter_max (int) – Interaction order
dim (int) – Number of random variables
order_inter_current (int)
order_glob_max_norm (float) – Norm, which defines how the orders are accumulated to derive the total order (default: 1-norm).
Values smaller than one restrict higher orders and shrink the basis.
Returns:
num_coeffs_sparse – Number of gPC coefficients and polynomials
order_current (int) – Maximum global expansion order.
The maximum expansion order considers the sum of the orders of combined polynomials together with the
chosen norm “order_max_norm”. Typically this norm is 1 such that the maximum order is the sum of all
monomial orders.
interaction_order_current (int) – Current number of random variables, which can interact with each other
All polynomials are ignored, which have an interaction order greater than specified
interaction_order_max (int) – Maximum number of random variables, which can interact with each other
All polynomials are ignored, which have an interaction order greater than specified
incr (int) – Number of sub-iteration increments
Returns:
order (int) – Updated order
interaction_order (int) – Updated interaction order
array_ref (np.ndarray) – reference data [ (x_ref), y0_ref, y1_ref, y2_ref … ]
if array_ref is 1D, all sizes have to match
error_norm (str, optional, default="relative") – Decide if error is determined “relative” or “absolute”
x_axis (boolean, optional, default=False) – If True, the first column of array and array_ref is interpreted as the x-axis, where the data points are
evaluated. If False, the data points are assumed to be at the same location.
Returns:
normalized_rms – Normalized root mean square deviation between the columns of array and array_ref
Algorithm by Gerstner and Griebel to expand polynomial basis [1] according to two criteria:
The basis function may not be completely enclosed by already existing basis functions. In this case the added
basis would be already included in any case.
The basis function is not a candidate if adding any basis would have no predecessors. The new basis must have
predecessors in all decreasing direction and may not “float”.
Parameters:
current_set (ndarray of int [n_basis, dim]) – Array of multi-indices of basis functions
to_expand (ndarray of int [dim]) – Selected basis function (with highest gPC coefficient), which will be expanded in all possible direction
order_max (int) – Maximal accumulated order (sum over all parameters)
interaction_order (int) – Allowed interaction order between variables (<= dim)
Returns:
expand – Array of multi-indices, which will be added to the set of basis functions
parameters_random (OrderedDict containing the RandomParameter class instances) – Dictionary (ordered) containing the properties of the random parameters
fn_out (str) – Filename of output .txt file containing the Sobol coefficient summary
Returns:
sobol (pandas DataFrame) – Pandas DataFrame containing the normalized Sobol indices by significance
gsens (pandas DataFrame) – Pandas DataFrame containing the global derivative based sensitivity coefficients
Post-processes the gPC expansion from the gPC coefficients (standard) or by sampling. Adds mean,
standard deviation, relative standard deviation, variance, Sobol indices, global derivative based
sensitivity coefficients and probability density functions of output quantities to .hdf5 file of gPC.
Parameters:
fn_gpc (str) – Filename of gPC .pkl object and corresponding .hdf5 results file (without file extension)
(e.g. …/foo/gpc)
output_idx (nparray of int) – Indices of output quantities (QOIs) to consider in postprocessing (default: all)
calc_sobol (bool) – Calculate Sobol indices (default: True)
calc_global_sens (bool) – Calculate global derivative based sensitivities (default: False)
calc_pdf (bool) – Calculate probability density functions of output quantities (default: False)
algorithm (str, optional, default: "standard") – Algorithm to determine the Sobol indices
- “standard”: Sobol indices are determined from the gPC coefficients
- “sampling”: Sobol indices are determined from sampling using Saltelli’s Sobol sampling sequence [1, 2, 3]
n_samples (int, optional, default: 1e5) – Number of samples to determine Sobol indices by sampling. The efficient number of samples
increases to n_samples * (2*dim + 2) in Saltelli’s Sobol sampling sequence.
Returns:
<File> – Adds datasets “sens/…” to the gPC .hdf5 file
sens
I—/mean [n_qoi] Mean of QOIs
I—/std [n_qoi] Standard deviation of QOIs
I—/rstd [n_qoi] Relative standard deviation of QOIs
I—/var [n_qoi] Variance of QOIs
I—/sobol [n_sobol x n_qoi] Sobol indices (all available orders)
I—/sobol_idx_bool [n_sobol x n_dim] Corresponding parameter (combinations) of Sobol indices
I—/global_sens [n_dim x n_qoi] Global derivative based sensitivity coefficients
I—/pdf_x [100 x n_qoi] x-axis values of output PDFs
I—/pdf_y [100 x n_qoi] y-axis values of output PDFs
Determine average ratios of Sobol indices over all output quantities:
(i) over all orders and (e.g. 1st: 90%, 2nd: 8%, 3rd: 2%)
(ii) for the 1st order indices w.r.t. each random variable. (1st: x1: 50%, x2: 40%)
Parameters:
sobol (ndarray of float [n_sobol x n_out]) – Unnormalized sobol_indices
sobol_idx_bool (list of ndarray of bool) – Boolean mask which contains unique multi indices.
random_vars (list of str) – Names of random variables in the order as they appear in the OrderedDict from the Problem class
verbose (boolean, optional, default=True) – Print output info
Returns:
sobol_rel_order_mean (ndarray of float [n_out]) – Average proportion of the Sobol indices of the different order to the total variance (1st, 2nd, etc..,),
(over all output quantities)
sobol_rel_order_std (ndarray of float [n_out]) – Standard deviation of the proportion of the Sobol indices of the different order to the total variance
(1st, 2nd, etc..,), (over all output quantities)
sobol_rel_1st_order_mean (ndarray of float [n_out]) – Average proportion of the random variables of the 1st order Sobol indices to the total variance,
(over all output quantities)
sobol_rel_1st_order_std (ndarray of float [n_out]) – Standard deviation of the proportion of the random variables of the 1st order Sobol indices to the total
variance
(over all output quantities)
sobol_rel_2nd_order_mean (ndarray of float [n_out]) – Average proportion of the random variables of the 2nd order Sobol indices to the total variance,
(over all output quantities)
sobol_rel_2nd_order_std (ndarray of float [n_out]) – Standard deviation of the proportion of the random variables of the 2nd order Sobol indices to the total
variance
(over all output quantities)
Compares gPC approximation with original model function. Evaluates both at n_grid (x n_grid) sampling points and
calculate the difference between two solutions at the output quantity with output_idx and saves the plot as
*_QOI_idx_<output_idx>.png/pdf. Also generates one .hdf5 results file with the evaluation results.
Parameters:
session (GPC Session object instance) – GPC session object containing all information i.e., gPC, Problem, Model, Grid, Basis, RandomParameter instances
coeffs (ndarray of float [n_coeffs x n_out] or list of ndarray of float [n_qoi][n_coeffs x n_out]) – GPC coefficients
random_vars (str or list of str [2]) – Names of the random variables, the analysis is performed for one or max. two random variables
n_grid (int or list of int [2], optional) – Number of samples in each dimension to compare the gPC approximation with the original model function.
A cartesian grid is generated based on the limits of the specified random_vars
coords (ndarray of float [n_coords x n_dim]) – Parameter combinations for the random_vars the comparison is conducted with
output_idx (int, str or None, optional, default=0) – Indices of output quantity to consider
results (ndarray of float [n_coords x n_out]) – If available, data of original model function at grid, containing all QOIs
fn_plot (str, optional, default: None) – Filename of plot comparing original vs gPC model (*.png or *.pdf)
camera_pos (list [2], optional, default: None) – Camera position of 3D surface plot (for 2 random variables only) [azimuth, elevation]
zlim (list of float, optional, default: None) – Limits of 3D plot (e.g. pdf) in z direction
plot_pdf_over_output_idx (bool, optional, default: False) – Plots pdf as a surface plot over output index (e.g. a time axis)
qois (numpy ndarray) – Axis of quantities of interest (x-axis, e.g. time)
x_label (str) – Label of x-axis in case of multiple QOI plots
y_label (str) – Label of y-axis in case of multiple QOI plots
Returns:
<file> (.hdf5 file) – Data file containing the grid points and the results of the original and the gpc approximation
<file> (.png and .pdf file) – Plot comparing original vs gPC model
Plot summary of Sobol indices and global derivative based sensitivity coefficients
Parameters:
session (GPC Session object instance) – GPC session object containing all information i.e., gPC, Problem, Model, Grid, Basis, RandomParameter instances
coeffs (ndarray of float [n_coeffs x n_out] or list of ndarray of float [n_qoi][n_coeffs x n_out]) – GPC coefficients
sobol (pandas DataFrame) – Pandas DataFrame containing the normalized Sobol indices from get_sens_summary()
gsens (pandas DataFrame) – Pandas DataFrame containing the global derivative based sensitivity coefficients from get_sens_summary()
sobol_donut (Boolean) – Option to plot the sobol indices as donut (pie) chart instead of bars, default is True
multiple_qoi (Boolean) – Option to plot over a quantity of interest, needs an array of qoi values and results
qois (numpy ndarray) – Axis of quantities of interest (x-axis, e.g. time)
mean (numpy ndarray) – Mean from gpc session (determined with e.g.: pygpc.SGPC.get_mean(coeffs))
std (numpy ndarray) – Std from gpc session (determined with e.g.: pygpc.SGPC.get_std(coeffs))
(can be given and plotted when plot_pdf_over_output_idx=False)
output_idx (int, str or None, optional, default=0) – Indices of output quantity to consider
x_label (str) – Label of x-axis in case of multiple QOI plots
y_label (str) – Label of y-axis in case of multiple QOI plots
zlim (list of float, optional, default: None) – Limits of 3D plot (e.g. pdf) in z direction
plot_pdf_over_output_idx (bool, optional, default: False) – Plots pdf as a surface plot over output index (e.g. a time axis)
fn_plot (str, optional, default: None) – Filename of the plot to save (.png or .pdf)
Returns a dictionary with keys ‘S1’, ‘S1_conf’, ‘ST’, and ‘ST_conf’, where
each entry is a list of size D (the number of parameters) containing the
indices in the same order as the parameter file. If calc_second_order is
True, the dictionary also contains keys ‘S2’ and ‘S2_conf’.
Parameters:
y (numpy.array) – A NumPy array containing the model outputs
Generates model inputs using Saltelli’s extension of the Sobol sequence.
Returns a NumPy matrix containing the model inputs using Saltelli’s sampling
scheme. Saltelli’s scheme extends the Sobol sequence in a way to reduce
the error rates in the resulting sensitivity index calculations. If
calc_second_order is False, the resulting matrix has N * (D + 2)
rows, where D is the number of parameters. If calc_second_order is True,
the resulting matrix has N * (2D + 2) rows. These model inputs are
intended to be used with SALib.analyze.sobol.analyze().
Parameters:
n_samples (int) – The number of samples to generate
Compares gPC approximation with original model function. Evaluates both at “n_samples” sampling points and
evaluates the root mean square deviation. It also computes the pdf at the output quantity with output_idx
and saves the plot as fn_pdf.png and fn_pdf.pdf.
Parameters:
session (GPC Session object instance) – GPC session object containing all information i.e., gPC, Problem, Model, Grid, Basis, RandomParameter instances
coeffs (ndarray of float [n_coeffs x n_out]) – GPC coefficients
coords (ndarray of float [n_coords x n_dim]) – Parameter combinations for the random_vars the comparison is conducted with
data_original (ndarray of float [n_coords x n_out], optional, default: None) – If available, data of original model function at grid, containing all QOIs
n_samples (int) – Number of samples to validate the gPC approximation (ignored if coords and data_original is provided)
output_idx (int or ndarray of int or list of int, optional, default=None [1 x n_out]) – Index of output quantities to consider (if output_idx=None, all output quantities are considered)
n_cpu (int, optional, default=1) – Number of CPU cores to use (parallel function evaluations) to evaluate original model function
smooth_pdf (bool, optional, default: True) – Smooth probability density functions using a Savgol filter [polylength, order]
bins (int, optional, default: 100) – Number of bins to estimate pdf
hist (bool, optional, default: False) – Plot histogram instead of pdf
fn_out (str or None) – Filename of validation results and plot comparing original vs gPC model (w/o file extension)
folder (str, optional, default: "gpc_vs_original_plot") – Folder in .hdf5 file to save data in
plot (boolean, optional, default: True) – Plots the pdfs of the original model function and the gPC approximation
Returns:
nrmsd (ndarray of float [n_out]) – Normalized root mean square deviation for all output quantities between gPC and original model
<file> (.hdf5 file) – Data file containing the sampling points, the results and the pdfs of the original and the gpc approximation
<file> (.pdf file) – Plot showing the pdfs of the original and the gpc approximation
Compares gPC approximation with original model function. Evaluates both at n_grid (x n_grid) sampling points and
calculate the difference between two solutions at the output quantity with output_idx and saves the plot as
*_QOI_idx_<output_idx>.png/pdf. Also generates one .hdf5 results file with the evaluation results.
Parameters:
session (GPC Session object instance) – GPC session object containing all information i.e., gPC, Problem, Model, Grid, Basis, RandomParameter instances
coeffs (ndarray of float [n_coeffs x n_out] or list of ndarray of float [n_qoi][n_coeffs x n_out]) – GPC coefficients
random_vars (str or list of str [2]) – Names of the random variables, the analysis is performed for one or max. two random variables
n_grid (int or list of int [2], optional) – Number of samples in each dimension to compare the gPC approximation with the original model function.
A cartesian grid is generated based on the limits of the specified random_vars
coords (ndarray of float [n_coords x n_dim]) – Parameter combinations for the random_vars the comparison is conducted with
output_idx (int or list of int, optional, default=0) – List of indices of output quantity to consider
data_original (ndarray of float [n_coords x n_out], optional, default: None) – If available, data of original model function at grid, containing all QOIs
fn_out (str, optional, default: None) – Filename of plot comparing original vs gPC model (*_QOI_idx_<output_idx>.png is added)
Filename of .hdf5 file containing the grid and the data from the original model and the gPC
folder (str, optional, default: "gpc_vs_original_plot") – Folder in .hdf5 file to save data in
n_cpu (int, default=1) – Number of CPU cores to use to calculate results of original model on grid.
Returns:
<file> (.hdf5 file) – Data file containing the grid points and the results of the original and the gpc approximation
<file> (.png and .pdf file) – Plot comparing original vs gPC model