gemseo / disciplines

Show inherited members

auto_py module

A discipline interfacing a Python function.

class gemseo.disciplines.auto_py.AutoDiscDataProcessor(out_names)[source]

Bases: DataProcessor

A data processor forcing input data to float and output data to arrays.

Convert all GEMSEO scalar input data to floats, and convert all discipline output data to NumPy arrays.

Initialize self. See help(type(self)) for accurate signature.

Parameters:

out_names (Sequence[str]) – The names of the outputs.

post_process_data(data)[source]

Post-process the output data.

Execute a post-processing of the output data after the _run() method of the discipline is called, and before they are checked by check_output_data().

Parameters:

data (dict[str, float | ndarray]) – The data to be processed.

Returns:

The processed data with NumPy arrays as values.

Return type:

dict[str, ndarray]

pre_process_data(data)[source]

Pre-process the input data.

Execute a pre-processing of input data after they are checked by check_input_data(), and before the _run() method of the discipline is called.

Parameters:

data (dict[str, float | ndarray]) – The data to be processed.

Returns:

The processed data where one-length NumPy arrays have been replaced with floats.

Return type:

dict[str, float | ndarray]

one_output: bool

Whether there is a single output.

out_names: Sequence[str]

The names of the outputs.

class gemseo.disciplines.auto_py.AutoPyDiscipline(py_func, py_jac=None, name=None, use_arrays=False, grammar_type=GrammarType.JSON)[source]

Bases: MDODiscipline

Wrap a Python function into a discipline.

A simplified and straightforward way of integrating a discipline from a Python function.

The Python function can take and return only numbers and NumPy arrays.

The Python function may or may not include default values for input arguments, however, if the resulting AutoPyDiscipline is going to be placed inside an MDF, a BiLevel formulation or an MDA with strong couplings, then the Python function must assign default values for its input arguments.

Examples

>>> from gemseo.disciplines.auto_py import AutoPyDiscipline
>>> from numpy import array
>>> def my_function(x=0., y=0.):
>>>     z1 = x + 2*y
>>>     z2 = x + 2*y + 1
>>>     return z1, z2
>>>
>>> discipline = AutoPyDiscipline(my_function)
>>> discipline.execute()
{'x': array([0.]), 'y': array([0.]), 'z1': array([0.]), 'z2': array([1.])}
>>> discipline.execute({"x": array([1.0]), "y": array([-3.2])})
{'x': array([1.]), 'y': array([-3.2]), 'z1': array([-5.4]), 'z2': array([-4.4])}

Initialize self. See help(type(self)) for accurate signature.

Parameters:
  • py_func (Callable) – The Python function to compute the outputs from the inputs.

  • py_jac (Callable | None) – The Python function to compute the Jacobian from the inputs; its output value must be a 2D NumPy array with rows corresponding to the outputs and columns to the inputs.

  • name (str | None) – The name of the discipline. If None, use the name of the Python function.

  • use_arrays (bool) –

    Whether the function is expected to take arrays as inputs and give outputs as arrays.

    By default it is set to False.

  • grammar_type (MDODiscipline.GrammarType) –

    The type of the input and output grammars.

    By default it is set to “JSONGrammar”.

Raises:

TypeError – When py_func is not callable.

cache: AbstractCache | None

The cache containing one or several executions of the discipline according to the cache policy.

data_processor: AutoDiscDataProcessor

A data processor forcing input data to float and output data to arrays.

exec_for_lin: bool

Whether the last execution was due to a linearization.

input_grammar: BaseGrammar

The input grammar.

input_names: list[str]

The names of the inputs.

jac: MutableMapping[str, MutableMapping[str, ndarray | csr_array | JacobianOperator]]

The Jacobians of the outputs wrt inputs.

The structure is {output: {input: matrix}}.

name: str

The name of the discipline.

output_grammar: BaseGrammar

The output grammar.

output_names: list[str]

The names of the outputs.

py_func: Callable

The Python function to compute the outputs from the inputs.

py_jac: Callable | None

The Python function to compute the Jacobian from the inputs.

re_exec_policy: ReExecutionPolicy

The policy to re-execute the same discipline.

residual_variables: dict[str, str]

The output variables mapping to their inputs, to be considered as residuals; they shall be equal to zero.

run_solves_residuals: bool

Whether the run method shall solve the residuals.

sizes: dict[str, int]

The sizes of the input and output variables.

use_arrays: bool

Whether the function is expected to take arrays as inputs and give outputs as arrays.

gemseo.disciplines.auto_py.to_arrays_dict(data)[source]

Ensure that the values of a dictionary are NumPy arrays.

Parameters:

data (dict[str, float | ndarray]) – The dictionary whose values must be NumPy arrays.

Returns:

The dictionary with NumPy arrays as values.

Return type:

dict[str, ndarray]

Examples using AutoPyDiscipline

GEMSEO in 10 minutes

GEMSEO in 10 minutes

Compute the Jacobian of a discipline with finite differences

Compute the Jacobian of a discipline with finite differences

Create a discipline from a Python function

Create a discipline from a Python function