gemseo.core.discipline.base_discipline module#

The base class defining the concept of discipline.

class BaseDiscipline(name='')[source]#

Bases: BaseMonitoredProcess

The base class defining the concept of discipline.

A discipline computes output data from input data using its execute() method. These data are in dictionary form, i.e. {variable_name: variable_value, ...}. The input-output data resulting from an execution can be accessed via local_data or separately via get_input_data() and get_output_data().

For both input and output variables, default values can be provided using the mappings default_input_data and default_output_data. In this case, the discipline will use these default input values at execution when an input value is not provided and these default output values in the case of virtual_execution.

In other aspects, the cache can store zero, one or more discipline evaluations depending on the CacheType. This cache is set at instantiation and can be changed with the set_cache() method.

Lastly, a discipline is equipped with an input_grammar to check the input data and an output_grammar to check the output data. This validation depends on the GrammarType, e.g. name verification, data type verification, etc.

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

Parameters:

name (str) --

The name of the discipline. If empty, use the name of the class.

By default it is set to "".

class CacheType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: StrEnum

The type of cache.

HDF5 = 'HDF5Cache'#

Store all the execution data on the disk.

MEMORY_FULL = 'MemoryFullCache'#

Store all the execution data in memory.

NONE = ''#

Store nothing.

SIMPLE = 'SimpleCache'#

Store the last execution data.

class GrammarType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: StrEnum

The name of the grammar class.

JSON = 'JSONGrammar'#
PYDANTIC = 'PydanticGrammar'#
SIMPLE = 'SimpleGrammar'#
SIMPLER = 'SimplerGrammar'#
add_namespace_to_input(input_name, namespace)[source]#

Rename an input name with a namespace prefix.

The updated input name will be namespace + namespaces_separator + input_name.

Parameters:
  • input_name (str) -- The input name to rename.

  • namespace (str) -- The name of the namespace.

Return type:

None

add_namespace_to_output(output_name, namespace)[source]#

Rename an output name with a namespace prefix.

The updated output name will be namespace + namespaces_separator + output_name.

Parameters:
  • output_name (str) -- The output name to rename.

  • namespace (str) -- The name of the namespace.

Return type:

None

execute(input_data=mappingproxy({}))[source]#

Execute the discipline, i.e. compute output data from input data.

If virtual_execution is True, this method returns the default_output_data. Otherwise, it calls the _run() method performing the true execution and returns the corresponding output data. This _run() method must be implemented in subclasses.

Parameters:

input_data (StrKeyMapping) --

The input data. Complete this dictionary with the default_input_data.

By default it is set to {}.

Returns:

The input and output data.

Return type:

DisciplineData

get_input_data(with_namespaces=True)[source]#

Return the input data of the last execution.

Parameters:

with_namespaces (bool) --

Whether to keep the namespace prefix of the input names, if any.

By default it is set to True.

Returns:

The input data of the last execution.

Return type:

dict[str, Any]

get_output_data(with_namespaces=True)[source]#

Return the output data of the last execution.

Parameters:

with_namespaces (bool) --

Whether to keep the namespace prefix of the output names, if any.

By default it is set to True.

Returns:

The output data of the last execution.

Return type:

dict[str, Any]

set_cache(cache_type, tolerance=0.0, **kwargs)[source]#

Set the type of cache to use and the tolerance level.

This method defines when the output data have to be cached according to the distance between the corresponding input data and the input data already cached for which output data are also cached.

The cache can be either a SimpleCache recording the last execution or a cache storing all executions, e.g. MemoryFullCache and HDF5Cache. Caching data can be either in-memory, e.g. SimpleCache and MemoryFullCache, or on the disk, e.g. HDF5Cache.

Parameters:
  • cache_type (CacheType) -- The type of cache.

  • tolerance (float) --

    The cache tolerance.

    By default it is set to 0.0.

  • **kwargs (Any) -- The other arguments passed to CacheFactory.create()

Return type:

None

GRAMMAR_DIRECTORY: ClassVar[str | Path] = ''#

The directory in which to search for the grammar files if not the class one.

auto_detect_grammar_files: ClassVar[bool] = False#

Whether to find the grammar files automatically.

cache: BaseCache | None#

The execution and linearization data saved according to the cache type.

default_cache_type: ClassVar[CacheType] = 'SimpleCache'#

The default type of cache.

default_grammar_type: ClassVar[_GrammarType] = 'JSONGrammar'#

The default type of grammar.

property default_input_data: Defaults#

The default input data.

property default_output_data: Defaults#

The default output data used when virtual_execution is True.

property input_grammar: BaseGrammar#

The input grammar.

property local_data: DisciplineData#

The current input and output data.

property output_grammar: BaseGrammar#

The output grammar.

validate_input_data: ClassVar[bool] = True#

Whether to validate the input data.

validate_output_data: ClassVar[bool] = True#

Whether to validate the output data.

virtual_execution: ClassVar[bool] = False#

Whether execute() returns the default_output_data.

A virtual execution mocks the input-output process without performing the true execution.

class CacheType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: StrEnum

The types of cache.

HDF5 = 'HDF5Cache'#

Store all the execution data on the disk.

MEMORY_FULL = 'MemoryFullCache'#

Store all the execution data in memory.

NONE = ''#

Store nothing.

SIMPLE = 'SimpleCache'#

Store the last execution data.