discipline_data module¶
Provide a dict-like class for storing disciplines data.
- class gemseo.core.discipline_data.DisciplineData(data=None, input_to_namespaced=None, output_to_namespaced=None)[source]
Bases:
MutableMapping[str,Any]A dict-like class for handling disciplines data.
This class replaces a standard dictionary that was previously used for storing discipline data. It allows handling values bound to
pandas.DataFrameas if they were multiple items bound tonumpy.ndarray. Then, an object of this class may be used as if it was a standard dictionary containingnumpy.ndarray, which is the assumption made by the clients of theMDODisciplinesubclasses.As compared to a standard dictionary, the methods of this class may hide the values bound to
pandas.DataFrameand instead expose the items of those later as if they belonged to the dictionary.If a dict-like object is provided when creating a
DisciplineDataobject, its contents is shared with this latter, such that any changes performed via aDisciplineDataobject is reflected into the passed in dict-like object.If a
DisciplineDatais created from another one, their contents are shared.A separator, by default
~, is used to identify the keys of the items that are bound to an array inside apandas.DataFrame. Such a key is composed of the key from the shared dict-like object, the separator and the name of the target column of thepandas.DataFrame. When such a key is queried, anumpy.ndarrayview of thepandas.DataFramecolumn is provided.Printing a
DisciplineDataobject prints the shared dict-like object.Nested dictionaries are also supported.
Warning
Nested DataFrame (i.e. in a nested dictionary) are not supported.
Examples
>>> from gemseo.core.discipline_data import DisciplineData >>> import numpy as np >>> import pandas as pd >>> data = { ... "x": 0, ... "y": pd.DataFrame(data={"a": np.array([0])}), ... } >>> disc_data = DisciplineData(data) >>> disc_data["x"] 0 >>> disc_data["y"] a 0 0 >>> # DataFrame content can be accessed with the ~ separator. >>> disc_data["y~a"] array([0]) >>> # New columns can be inserted into a DataFrame with the ~ separator. >>> disc_data["y~b"] = np.array([1]) >>> data["y"]["b"] 0 1 Name: b, dtype: int64 >>> # New DataFrame can be added. >>> disc_data["z~c"] = np.array([2]) >>> data["z"] c 0 2 >>> type(data["z"]) <class 'pandas.core.frame.DataFrame'> >>> # DataFrame's columns can be deleted. >>> del disc_data["z~c"] >>> data["z"] Empty DataFrame Columns: [] Index: [0] >>> # Iterating is only done over the exposed keys. >>> list(disc_data) ['x', 'y~a', 'y~b'] >>> # The length is consistent with the iterator. >>> len(disc_data) 3
Initialize self. See help(type(self)) for accurate signature.
- Parameters:
data (MutableData | None) – A dict-like object or a
DisciplineDataobject. IfNone, an empty dictionary is used.input_to_namespaced (NamespacesMapping | None) – The mapping from input data names to their prefixed names.
output_to_namespaced (NamespacesMapping | None) – The mapping from output data names to their prefixed names.
- clear() None. Remove all items from D.[source]
- Return type:
None
- copy(keys=(), with_namespace=True)[source]
Create a shallow copy.
- Parameters:
- Returns:
The shallow copy.
- Return type:
- restrict(*keys)[source]
Remove all but the given keys.
- Parameters:
*keys (str) – The keys of the elements to keep.
- Return type:
None
- update(other, exclude=())[source]
Update from another mapping but for some keys.
- SEPARATOR = '~'
The character used to separate the shared dict key from the column of a pandas DataFrame.