gemseo / core / data_converters

Hide inherited members

base module

Base class for converting data values to NumPy arrays and vice versa.

class gemseo.core.data_converters.base.BaseDataConverter(grammar)[source]

Bases: ABC, Generic[T]

Base class for converting data values to NumPy arrays and vice versa.

Typically, data are dictionary-like object that map names to values, such as DisciplineData.

By default, a data converter can handle the conversion of a data value that is a standard number (int, float and complex) or a 1D NumPy array. Other types could be handled in derived classes.

A data converter can also be used to convert a data structure {data_name: data_value, ...} to a NumPy array and vice versa. In this class, a data structure is noted data.

For performance reasons, no checking or error handling is done when calling the methods of this class or of its derivatives.

Note

The data converter uses a grammar, and in particular its mapping from data names to data types, to convert a NumPy array from/to a data value.

Warning

Throughout this class, _NumPy array_ is equivalent to _1D numeric NumPy array_.

Parameters:

grammar (T) – The grammar providing the data types used for the conversions.

compute_names_to_sizes(names, data)[source]

Compute a mapping from data names to data value sizes.

See also

get_value_size().

Parameters:
  • data (Data) – The data structure.

  • names (Iterable[str]) – The data names.

Returns:

The mapping from the data names to the data sizes.

Return type:

dict[str, int]

compute_names_to_slices(names, data, names_to_sizes=mappingproxy({}))[source]

Compute a mapping from data names to data value slices.

The slices are relative to a NumPy array concatenating the data values associated with these data names.

Parameters:
  • data (Data) – The data structure.

  • names (Iterable[str]) – The data names.

  • names_to_sizes (Mapping[str, int]) –

    The mapping from the data names to the data sizes. If empty, it will be computed.

    By default it is set to {}.

Returns:

The mapping from the data names to the data slices of the expected concatenated NumPy array. and the size of this array.

Return type:

tuple[dict[str, slice], int]

convert_array_to_data(array, names_to_slices)[source]

Convert a NumPy array to a data structure.

Parameters:
  • array (NumberArray) – The NumPy array to slice.

  • names_to_slices (Mapping[str, slice]) – The mapping from the data names to the array slices.

Returns:

The mapping from the data names to the array slices.

Return type:

dict[str, ValueType]

convert_array_to_value(name, array)[source]

Convert a NumPy array to a data value.

Parameters:
  • name (str) – The data name.

  • array (NumberArray) – The NumPy array to convert.

Returns:

The data value.

Return type:

ValueType

convert_data_to_array(names, data)[source]

Convert a part of a data structure to a NumPy array.

Parameters:
  • data (Data) – The data structure.

  • names (Iterable[str]) – The data names which values will be concatenated.

Returns:

The concatenated NumPy array.

Return type:

NumberArray

convert_value_to_array(name, value)[source]

Convert a data value to a NumPy array.

Parameters:
  • name (str) – The data name.

  • value (ValueType) – The data value.

Returns:

The NumPy array.

Return type:

NumberArray

classmethod get_value_size(name, value)[source]

Return the size of a data value.

The size is typically what is returned by ndarray.size or len(list). The size of a number is 1.

Parameters:
  • name (str) – The data name.

  • value (ValueType) – The data value to get the size from.

Returns:

The size.

Return type:

int

is_continuous(name)[source]

Check that a data item has a type that can differentiate.

Parameters:

name (str) – The name of the data item.

Returns:

Whether the data item can differentiate.

Return type:

bool

is_numeric(name)[source]

Check that a data item is numeric.

Parameters:

name (str) – The name of the data item.

Returns:

Whether the data item is numeric.

Return type:

bool

Examples using BaseDataConverter

Use a data converter

Use a data converter