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.

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

    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 is expected to take arrays as inputs and give outputs as arrays.

    By default it is set to False.

default_grammar_type: ClassVar[_GrammarType] = 'JSONGrammar'#

The default type of grammar.

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.

sizes: dict[str, int]#

The sizes of the input and output variables.