gemseo / problems / scalable / data_driven

diagonal module

Scalable diagonal model

This module implements the concept of scalable diagonal model, which is a particular scalable model built from an input-output dataset relying on a diagonal design of experiments (DOE) where inputs vary proportionally from their lower bounds to their upper bounds, following the diagonal of the input space.

So for every output, the dataset catches its evolution with respect to this proportion, which makes it a monodimensional behavior. Then, for a new user-defined problem dimension, the scalable model extrapolates this monodimensional behavior to the different input directions.

The concept of scalable diagonal model is implemented through the ScalableDiagonalModel class which is composed of a ScalableDiagonalApproximation. With regard to the diagonal DOE, GEMSEO proposes the DiagonalDOE class.

Classes:

ScalableDiagonalApproximation(sizes, ...[, seed])

Methodology that captures the trends of a physical problem, and extends it into a problem that has scalable input and outputs dimensions The original and the resulting scalable problem have the same interface:

ScalableDiagonalModel(data[, sizes, ...])

Scalable diagonal model.

Functions:

choice(a[, size, replace, p])

Generates a random sample from a given 1-D array

npseed

seed(self, seed=None)

rand(d0, d1, ..., dn)

Random values in a given shape.

randint(low[, high, size, dtype])

Return random integers from low (inclusive) to high (exclusive).

class gemseo.problems.scalable.data_driven.diagonal.ScalableDiagonalApproximation(sizes, output_dependency, io_dependency, seed=0)[source]

Bases: object

Methodology that captures the trends of a physical problem, and extends it into a problem that has scalable input and outputs dimensions The original and the resulting scalable problem have the same interface:

all inputs and outputs have the same names; only their dimensions vary.

Constructor:

Parameters
  • sizes (dict) – sizes of both input and output variables.

  • output_dependency (dict) – dependency between old and new outputs.

  • io_dependency (dict) – dependency between new inputs and new outputs.

Methods:

build_scalable_function(function_name, ...)

Build interpolation interpolation from a 1D input and output function.

get_scalable_derivative(output_function)

Retrieve the (scalable) gradient of the scalable function generated from the original discipline.

get_scalable_function(output_function)

Retrieve the scalable function generated from the original discipline.

scale_samples(samples)

Scale samples of array into [0, 1]

build_scalable_function(function_name, dataset, input_names, degree=3)[source]

Build interpolation interpolation from a 1D input and output function. Add the model to the local dictionary.

Parameters
  • function_name (str) – name of the output function

  • dataset (Dataset) – the input-output dataset

  • input_names (list(str)) – names of the input variables

  • degree (int) –

    degree of interpolation (Default value = 3)

    By default it is set to 3.

get_scalable_derivative(output_function)[source]

Retrieve the (scalable) gradient of the scalable function generated from the original discipline.

Parameters

output_function (str) – name of the output function

get_scalable_function(output_function)[source]

Retrieve the scalable function generated from the original discipline.

Parameters

output_function (str) – name of the output function

static scale_samples(samples)[source]

Scale samples of array into [0, 1]

Parameters

samples (list(array)) – samples of multivariate array

Returns

samples of multivariate array

Return type

array

class gemseo.problems.scalable.data_driven.diagonal.ScalableDiagonalModel(data, sizes=None, fill_factor=- 1, comp_dep=None, inpt_dep=None, force_input_dependency=False, allow_unused_inputs=True, seed=1, group_dep=None)[source]

Bases: gemseo.problems.scalable.data_driven.model.ScalableModel

Scalable diagonal model.

Constructor.

Parameters
  • data (Dataset) – learning dataset.

  • sizes (dict) –

    sizes of input and output variables. If None, use the original sizes. Default: None.

    By default it is set to None.

  • fill_factor

    degree of sparsity of the dependency matrix. Default: -1.

    By default it is set to -1.

  • comp_dep

    matrix that establishes the selection of a single original component for each scalable component

    By default it is set to None.

  • inpt_dep

    dependency matrix that establishes the dependency of outputs wrt inputs

    By default it is set to None.

  • force_input_dependency (bool) –

    for any output, force dependency with at least on input.

    By default it is set to False.

  • allow_unused_inputs (bool) –

    possibility to have an input with no dependence with any output

    By default it is set to True.

  • seed (int) –

    seed

    By default it is set to 1.

  • group_dep (dict(list(str))) –

    dependency between inputs and outputs

    By default it is set to None.

Attributes:

ABBR

inputs_names

Inputs names.

original_sizes

Original sizes of variables.

outputs_names

Outputs names.

Methods:

build_model()

Build model with original sizes for input and output variables.

compute_bounds()

Compute lower and upper bounds of both input and output variables.

generate_random_dependency()

Generates a random dependency structure for use in scalable discipline.

normalize_data()

Normalize dataset from lower and upper bounds.

plot_1d_interpolations([save, show, step, ...])

This methods plots the scaled 1D interpolations, a.k.a.

plot_dependency([add_levels, save, show, ...])

This method plots the dependency matrix of a discipline in the form of a chessboard, where rows represent inputs, columns represent output and gray scale represent the dependency level between inputs and outputs.

scalable_derivatives([input_value])

Evaluate the scalable derivatives.

scalable_function([input_value])

Evaluate the scalable functions.

ABBR = 'sdm'
build_model()[source]

Build model with original sizes for input and output variables.

Returns

scalable approximation.

Return type

ScalableDiagonalApproximation

compute_bounds()

Compute lower and upper bounds of both input and output variables.

Returns

lower bounds, upper bounds.

Return type

dict, dict

generate_random_dependency()[source]

Generates a random dependency structure for use in scalable discipline.

Returns

output component dependency and input-output dependency

Return type

dict(int), dict(dict(array))

property inputs_names

Inputs names.

Returns

names of the inputs.

Return type

list(str)

normalize_data()

Normalize dataset from lower and upper bounds.

property original_sizes

Original sizes of variables.

Returns

original sizes of variables.

Return type

dict

property outputs_names

Outputs names.

Returns

names of the outputs.

Return type

list(str)

plot_1d_interpolations(save=False, show=False, step=0.01, varnames=None, directory='.', png=False)[source]

This methods plots the scaled 1D interpolations, a.k.a. basis functions.

A basis function is a monodimensional function interpolating the samples of a given output component over the input sampling line \(t\in[0,1]\mapsto \\underline{x}+t(\overline{x}-\\underline{x})\).

There are as many basis functions as there are output components from the discipline. Thus, for a discipline with a single output in dimension 1, there is 1 basis function. For a discipline with a single output in dimension 2, there are 2 basis functions. For a discipline with an output in dimension 2 and an output in dimension 13, there are 15 basis functions. And so on. This method allows to plot the basis functions associated with all outputs or only part of them, either on screen (show=True), in a file (save=True) or both. We can also specify the discretization step whose default value is 0.01.

Parameters
  • save (bool) –

    if True, export the plot as a PDF file (Default value = False)

    By default it is set to False.

  • show (bool) –

    if True, display the plot (Default value = False)

    By default it is set to False.

  • step (bool) –

    Step to evaluate the 1d interpolation function (Default value = 0.01)

    By default it is set to 0.01.

  • varnames (list(str)) –

    names of the variable to plot; if None, all variables are plotted (Default value = None)

    By default it is set to None.

  • directory (str) –

    directory path. Default: ‘.’.

    By default it is set to ..

  • png (bool) –

    if True, the file format is PNG. Otherwise, use PDF. Default: False.

    By default it is set to False.

plot_dependency(add_levels=True, save=True, show=False, directory='.', png=False)[source]

This method plots the dependency matrix of a discipline in the form of a chessboard, where rows represent inputs, columns represent output and gray scale represent the dependency level between inputs and outputs.

Parameters
  • add_levels (bool) –

    add values of dependency levels in percentage. Default: True.

    By default it is set to True.

  • save (bool) –

    if True, export the plot into a file. Default: True.

    By default it is set to True.

  • show (bool) –

    if True, display the plot. Default: False.

    By default it is set to False.

  • directory (str) –

    directory path. Default: ‘.’.

    By default it is set to ..

  • png (bool) –

    if True, the file format is PNG. Otherwise, use PDF. Default: False.

    By default it is set to False.

scalable_derivatives(input_value=None)[source]

Evaluate the scalable derivatives.

Parameters

input_value (dict) –

input values. If None, use default inputs.

By default it is set to None.

Returns

evaluation of the scalable derivatives.

Return type

dict

scalable_function(input_value=None)[source]

Evaluate the scalable functions.

Parameters

input_value (dict) –

input values. If None, use default inputs.

By default it is set to None.

Returns

evaluation of the scalable functions.

Return type

dict

gemseo.problems.scalable.data_driven.diagonal.choice(a, size=None, replace=True, p=None)

Generates a random sample from a given 1-D array

New in version 1.7.0.

Note

New code should use the choice method of a default_rng() instance instead; please see the random-quick-start.

Parameters
  • a (1-D array-like or int) – If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were np.arange(a)

  • size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

  • replace (boolean, optional) – Whether the sample is with or without replacement

  • p (1-D array-like, optional) – The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

Returns

samples – The generated random samples

Return type

single item or ndarray

Raises

ValueError – If a is an int and less than zero, if a or p are not 1-dimensional, if a is an array-like of size 0, if p is not a vector of probabilities, if a and p have different lengths, or if replace=False and the sample size is greater than the population size

See also

randint, shuffle, permutation

Generator.choice

which should be used in new code

Notes

Sampling random rows from a 2-D array is not possible with this function, but is possible with Generator.choice through its axis keyword.

Examples

Generate a uniform random sample from np.arange(5) of size 3:

>>> np.random.choice(5, 3)
array([0, 3, 4]) # random
>>> #This is equivalent to np.random.randint(0,5,3)

Generate a non-uniform random sample from np.arange(5) of size 3:

>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0]) # random

Generate a uniform random sample from np.arange(5) of size 3 without replacement:

>>> np.random.choice(5, 3, replace=False)
array([3,1,0]) # random
>>> #This is equivalent to np.random.permutation(np.arange(5))[:3]

Generate a non-uniform random sample from np.arange(5) of size 3 without replacement:

>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0]) # random

Any of the above can be repeated with an arbitrary array-like instead of just integers. For instance:

>>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random
      dtype='<U11')
gemseo.problems.scalable.data_driven.diagonal.npseed()

seed(self, seed=None)

Reseed a legacy MT19937 BitGenerator

Notes

This is a convenience, legacy function.

The best practice is to not reseed a BitGenerator, rather to recreate a new one. This method is here for legacy reasons. This example demonstrates best practice.

>>> from numpy.random import MT19937
>>> from numpy.random import RandomState, SeedSequence
>>> rs = RandomState(MT19937(SeedSequence(123456789)))
# Later, you want to restart the stream
>>> rs = RandomState(MT19937(SeedSequence(987654321)))
gemseo.problems.scalable.data_driven.diagonal.rand(d0, d1, ..., dn)

Random values in a given shape.

Note

This is a convenience function for users porting code from Matlab, and wraps random_sample. That function takes a tuple to specify the size of the output, which is consistent with other NumPy functions like numpy.zeros and numpy.ones.

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

Parameters
  • d0 (int, optional) – The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.

  • d1 (int, optional) – The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.

  • ... (int, optional) – The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.

  • dn (int, optional) – The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.

Returns

out – Random values.

Return type

ndarray, shape (d0, d1, ..., dn)

See also

random

Examples

>>> np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random
gemseo.problems.scalable.data_driven.diagonal.randint(low, high=None, size=None, dtype=int)

Return random integers from low (inclusive) to high (exclusive).

Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).

Note

New code should use the integers method of a default_rng() instance instead; please see the random-quick-start.

Parameters
  • low (int or array-like of ints) – Lowest (signed) integers to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer).

  • high (int or array-like of ints, optional) – If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None). If array-like, must contain integer values

  • size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

  • dtype (dtype, optional) –

    Desired dtype of the result. Byteorder must be native. The default value is int.

    New in version 1.11.0.

Returns

outsize-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.

Return type

int or ndarray of ints

See also

random_integers

similar to randint, only for the closed interval [low, high], and 1 is the lowest value if high is omitted.

Generator.integers

which should be used for new code.

Examples

>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Generate a 2 x 4 array of ints between 0 and 4, inclusive:

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1], # random
       [3, 2, 2, 0]])

Generate a 1 x 3 array with 3 different upper bounds

>>> np.random.randint(1, [3, 5, 10])
array([2, 2, 9]) # random

Generate a 1 by 3 array with 3 different lower bounds

>>> np.random.randint([1, 5, 7], 10)
array([9, 8, 7]) # random

Generate a 2 by 4 array using broadcasting with dtype of uint8

>>> np.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)
array([[ 8,  6,  9,  7], # random
       [ 1, 16,  9, 12]], dtype=uint8)