skreducedmodel package

Submodules

skreducedmodel.empiricalinterpolation module

Empirical Interpolation Methods.

class skreducedmodel.empiricalinterpolation.EmpiricalInterpolation(reduced_basis=None)[source]

Bases: object

Empirital interpolation functions and methods.

Implements EIM algorithm:

The Empirical Interpolation Method (EIM) (TiglioAndVillanueva2021) introspects the basis and selects a set of interpolation nodes from the physical domain for building an interpolant matrix using the basis and the selected nodes. The interpolant matrix can be used to approximate a field of functions for which the span of the basis is a good approximant.

Parameters

reduced_basis (instance of ReducedBasis) –

fit() → None[source]

Implement EIM algorithm.

The Empirical Interpolation Method (EIM) introspects the basis and selects a set of interpolation nodes from the physical domain for building an interpolant matrix using the basis and the selected nodes. The interpolant matrix can be used to approximate a field of functions for which the span of the basis is a good approximant.

Container for EIM data. Contains (interpolant, nodes).

property is_trained

Return True only if the instance is trained, False otherwise.

Returns

Return type

Bool

transform(h, q)[source]

Interpolate a function h at EIM nodes.

This method uses the basis and associated EIM nodes

Parameters

h (np.ndarray) – Function or set of functions to be interpolated.

Returns

h_interpolated – Interpolated function at EIM nodes.

Return type

np.ndarray

skreducedmodel.integrals module

Integration schemes module.

class skreducedmodel.integrals.Integration(interval, rule='riemann')[source]

Bases: object

Integration scheme.

This class fixes a frame for performing integrals, inner products and derived operations. An integral is defined by a quadrature rule composed by nodes and weights which are used to construct a discrete approximation to the true integral (or inner product).

For completeness, an “euclidean” rule is available for which inner products reduce to simple discrete dot products.

Parameters
  • interval (numpy.ndarray) – Equispaced set of points as domain for integrals or inner products.

  • rule (str, optional) – Quadrature rule. Default = “riemann”. Available = (“riemann”, “trapezoidal”, “euclidean”)

dot(f, g)[source]

Return the dot product between functions.

Parameters

g (f,) – Real or complex numbers array.

integral(f)[source]

Integrate a function.

Parameters

f (np.ndarray) – Real or complex numbers array.

norm(f)[source]

Return the norm of a function.

Parameters

f (np.ndarray) – Real or complex numbers array.

normalize(f)[source]

Normalize a function.

Parameters

f (np.ndarray) – Real or complex numbers array.

skreducedmodel.reducedbasis module

Reduced Basis module.

class skreducedmodel.reducedbasis.ReducedBasis(index_seed_global_rb=0, lmax=0, nmax=inf, greedy_tol=1e-12, normalize=False, integration_rule='riemann')[source]

Bases: object

Class for building a reduced basis (RB) using the RB greedy algorithm.

The reduced basis is built from the training data set with a user specified tolerance by linear combinations of its elements. The reduced basis can be also constructed with a domain decomposition of the parameter space, building local basis in each subspace.

Parameters
  • index_seed_global_rb (int, optional) – The seed for construct the reduced basis, by default 0.

  • lmax (int, optional) – The maximum number domain partitions performed.

  • nmax (int, optional) – The maximum number of basis functions to be used, by default np.inf.

  • greedy_tol (float, optional) – The greedy tolerance, by default 1e-12.

  • normalize (bool, optional) – Indicates if the training set should be normalized, by default False.

  • integration_rule (str, optional) – The integration rule to be used, by default “riemann”.

tree

The tree data structure for the reduced basis.

Type

Node

fit(training_set, parameters, physical_points) → None[source]

Build a reduced basis from training data.

This function implements the Reduced Basis (RB) greedy algorithm to build an orthonormalized reduced basis out from training data. The basis is built to reproduce the training functions with the user specified tolerance by linear combinations of its elements.

Parameters
  • training_set (numpy.ndarray) – Training set functions.

  • parameters (numpy.ndarray) – Associated parameters to the training set functions.

  • physical_points (numpy.ndarray) – Physical points for quadrature rules.

property is_trained

Return True only if the instance is trained, False otherwise.

Returns

Return type

Bool

partition(parameters, idx_anchor_0, idx_anchor_1)[source]

Partition the parameter space.

This code partitions a list of parameters into two subspaces based on their distances to two anchors. The function calculates the distances and returns two arrays of indices representing the parameters in each subspace. For equal distances the function make a random decision.

Parameters
  • parameters (np.ndarray) – array of parameter from the domain of problem

  • idx_anchor_0 (float, integer) – first greedy parameter of the space to divide

  • idx_anchor_1 (float, integer) – second greedy parameter of the space to divide

Returns

indices of parameter that correspond to each subspace

Return type

np.ndarray

References

[CerinoAndTiglio2023] An automated parameter domain decomposition approach for gravitational wave surrogates using hp-greedy refinement. Cerino, F. and Tiglio M. arXiv:2212.08554 (2023)

search_leaf(parameters, node)[source]

Search Leaf.

This function finds the leaf node in a tree by recursively evaluating each node using the select_child_node function. If a node is a leaf, the node is returned, otherwise the search continues on the selected child node.

Parameters

parameters (np.ndarray) – Set of parameters to search in the tree.

Returns

node – Set of nodes where the parameters are located in the tree.

Return type

np.ndarray

transform(test_set, parameters, s=(None, ))[source]

Project a function h onto the basis.

This method represents the action of projecting the function h onto the span of the basis.

Parameters
  • h (np.ndarray) – Function or set of functions to be projected.

  • s (tuple, optional) – Slice the basis. If the slice is not provided, the whole basis is considered. Default = (None,)

Returns

projected_function – Projection of h onto the basis.

Return type

np.ndarray

skreducedmodel.reducedbasis.error(h1, h2, domain, rule='riemann')[source]

Error function.

The error is computed in the L2 norm (continuous case) or the 2-norm (discrete case), that is, ||h1 - h2||^2.

Parameters
  • h1 (np.ndarray) –

  • h2 (np.ndarray) –

  • domain (np.ndarray) –

  • rule (integration rule used to compute the error (default="riemann")) –

skreducedmodel.reducedbasis.normalize_set(array, domain, rule='riemann')[source]

Normalize set.

Normalize a set of functions or arrays.

Parameters
  • array (np.ndarray) – Set of functions to normalize.

  • domain (np.ndarray) – Physical domain of the set of functions.

  • rule (str, optional) –

    Integration rule used to calculate the norm (default=”riemann”),

    by default “riemann”

Returns

Normalized set of functions.

Return type

np.ndarray

skreducedmodel.reducedbasis.select_child_node(parameter, node)[source]

Select child node.

The function selects a child node from a binary tree structure, given a parameter. The function calculates the distances between the parameter and two anchors, stored in the node, and returns the child node with the closest anchor. If the distances are equal, a random choice is made.

Parameters
  • parameter (np.ndarray) – parameter to evaluate by the sustitute model in a subspace

  • node (np.ndarray) – node where the parameter is located in the subspace

skreducedmodel.surrogate module

Surrogate module.

class skreducedmodel.surrogate.Surrogate(eim=None, poly_deg=3)[source]

Bases: object

Build reduced order models.

This class comprises a set of tools to build and handle reduced bases, empirical interpolants and predictive models from pre-computed training set of functions. The underlying or ground truth model describing the training set is a real function g(v,x) parameterized by a training parameter v. The physical variable x belongs to a domain for which an inner product can defined. The surrogate model is built bringing together the Reduced Basis (RB) greedy algorithm and the Empirical Interpolation Method (EIM) to work in synergy towards a predictive model for the ground truth model.

Parameters
  • eim (EmpiricalInterpolation, optional) –

    .

  • poly_deg (int, optional) – Degree <= 5 of polynomials used for splines. Default = 3.

eim

Instance of EmpiricalInterpolation.

fit() → None[source]

Construct the model.

Build a surrogate model valid for the entire parameter domain. Regressions at empirical times are trained to build the surrogate model.

property is_trained

Return True only if the instance is trained, False otherwise.

predict(parameter)[source]

Evaluate the surrogate model at a given parameter.

Parameters

param (float or array_like(float)) – Point or set of parameters inside the parameter domain.

Returns

h_surrogate – The evaluated surrogate function for the given parameters.

Return type

numpy.ndarray

skreducedmodel.mksurrogate module

mksurrogate function implementation.

exception skreducedmodel.mksurrogate.InputError[source]

Bases: ValueError

Class used to give input errors of the function mksurrogate.

skreducedmodel.mksurrogate.mksurrogate(instance=None, **kwargs)[source]

Trains and returns a Surrogate model.

Factory function for the classes ReducedBasis, EmpiricalInterpolation and Surrogate. In one way, only hyperparameters and training data can be given. In another way, can be given an instance of ReducedBasis or EmpiricalInterpolation and hyperparameters of the subsequent classes. If the instance will be trained if that did not happen before.

Parameters
  • instance (_type_, optional) – reduced basis or eim instance, by default None

  • kwargs

  • data or hyperparameters for the given instance. (Training) –

Returns

A trained surrogate model, ready to make predictions.

Return type

Surrogate

Raises

InputError – Bad parameters given as input to the function. Cases: - A reduced basis or eim instance is not needed as input if training data is given. If want to specify hyperparameters when using a function or method, they must be passed as kwargs. - ‘input’ must not be given. It is not a parameter to use. - If an instance of ReducedBasis is given, must not be given hyperparameters of it. - There is no training data to build a surrogate model - if ‘instance’ is given, must be an instance of EmpiricalInterpolation or ReducedBasis.