Use a data converter

from __future__ import annotations

from gemseo.core.data_converters.json import JSONGrammarDataConverter
from gemseo.core.grammars.json_grammar import JSONGrammar


By default, the types of the coupling variables can be either numbers or 1D NumPy array. With custom data converters, one can add support to other types. For instance, to support 2D NumPy arrays, the following specialized data converter could be derived.

class DataConverter(JSONGrammarDataConverter):
    """A data converter where some coupling variables are 2D NumPy arrays."""

    # The names of the coupling variables that are 2D NumPy arrays.
    NAMES_WITH_2D_ARRAY = ("a_coupling_variable_name", "another_coupling_variable_name")

    # The shape of the 2D NumPy arrays.
    SHAPE = (2, 2)

    def convert_value_to_array(self, name, value):
        if name in self.NAMES_WITH_2D_ARRAY:
            return value.flatten()
        return super().convert_value_to_array(name, value)

    def convert_array_to_value(self, name, array_):
        if name in self.NAMES_WITH_2D_ARRAY:
            return array_.reshape(self.SHAPE)
        return super().convert_array_to_value(name, array_)

Use the converter

In order to be used, the custom data converter shall be set to the grammar class. The above custom data converter is defined for grammars of type JSONGrammar since it derives from JSONGrammarDataConverter. Declare the custom data converter with


# This shall be done the earliest,
# before any grammar is instantiated.

Total running time of the script: (0 minutes 0.001 seconds)

Gallery generated by Sphinx-Gallery