gemseo.disciplines.auto_py module#

A discipline interfacing a Python function automatically.

class AutoDiscDataProcessor[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.

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 validate_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 validate_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]

class AutoPyDiscipline(py_func, py_jac=None, name='', use_arrays=False)[source]#

Bases: Discipline

Wrap a Python function into a discipline.

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

  • returns variables, e.g. return x or return x, y, but no expression like return a+b or return a+b, y,

  • must have a default value per argument if the AutoPyDiscipline is used by an MDA (deriving from BaseMDA), as in the case of MDF and BiLevel formulations, in the presence of strong couplings.

The input names of the discipline are the names of the Python function arguments and the output names are the names of the variable listed in the return statement.

By default, the arguments and returned variables are assumed to be either scalars or NumPy arrays of length greater than 1. When use_arrays is True, the scalar arguments are assumed to be NumPy arrays of length equal to 1. When all the arguments and returned variables have type hints, these types are used by the input and output grammars.

The default input values are the default values of the Python function arguments, if any.

This example from the documentation illustrates this feature.

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) --

    The name of the discipline. If empty, use the name of the Python function.

    By default it is set to "".

  • use_arrays (bool) --

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

    By default it is set to False.

Raises:

ValueError -- Either when the function returns an expression or when two return statements use different variables.

default_grammar_type: ClassVar[_GrammarType] = 'JSONGrammar'#

The default type of grammar.

property input_names: list[str]#

The names of the input variables.

property output_names: list[str]#

The names of the output variables.

property py_func: Callable#

The Python function to compute the outputs from the inputs.

property py_jac: Callable#

The Python function to compute the Jacobian from the inputs.