discipline_data module¶
Provide a dict-like class for storing disciplines data.
- class gemseo.core.discipline_data.DisciplineData(data, input_to_namespaced=None, output_to_namespaced=None)[source]¶
Bases:
collections.abc.MutableMapping
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 :class:`pandas.DataFrame`s as if they were multiple items bound to :class:`numpy.ndarray`s. Then, an object of this class may be used as if it was a standard dictionary containing :class:`numpy.ndarray`s, which is the assumption made by the clients of the :class:`MDODiscipline`s subclasses.
As compared to a standard dictionary, the methods of this class may hide the values bound to :class:`pandas.DataFrame`s and instead expose the items of those latter as if they belonged to the dictionary.
If a dict-like object is provided when creating a
DiscplineData
object, its contents is shared with this latter, such that any changes performed via aDiscplineData
object is reflected into the passed in dict-like object.If a
DiscplineData
is 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.ndarray
view of thepandas.DataFrame
column is provided.Printing a
DiscplineData
object prints the shared dict-like object.Nested dictionaries are also 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 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
- Parameters
data (MutableMapping[str, Any]) – A dict-like object or a
DisciplineData
object.input_to_namespaced (Optional[MutableMapping[str, Union[str, List[str]]]]) –
The mapping from input data names to their prefixed names.
By default it is set to None.
output_to_namespaced (Optional[MutableMapping[str, Union[str, List[str]]]]) –
The mapping from output data names to their prefixed names.
By default it is set to None.
- Return type
None
- clear() None. Remove all items from D. ¶
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- pop(k[, d]) v, remove specified key and return the corresponding value. ¶
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair ¶
as a 2-tuple; but raise KeyError if D is empty.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D ¶
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values ¶
- SEPARATOR = '~'¶
The character used to separate the shared dict key from the column of a pandas DataFrame.