.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/basics/plot_data_frame.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_disciplines_basics_plot_data_frame.py: Create a discipline that uses pandas DataFrames =============================================== .. GENERATED FROM PYTHON SOURCE LINES 22-40 .. code-block:: Python from __future__ import annotations from typing import TYPE_CHECKING import pandera as pa from pandas import DataFrame from pandera.typing import DataFrame as DataFrameType from pandera.typing import Series # noqa: TC002 from pydantic import BaseModel from gemseo.core.data_converters.pydantic import PydanticGrammarDataConverter from gemseo.core.discipline import Discipline from gemseo.core.grammars.pydantic_grammar import PydanticGrammar if TYPE_CHECKING: from gemseo.typing import StrKeyMapping .. GENERATED FROM PYTHON SOURCE LINES 41-43 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 46-74 Create a discipline that uses a DataFrame ----------------------------------------- We will create a class for a simple discipline that computes an output variable ``y = 1 - 0.2 * x`` where ``x`` is an input variable. For whatever reason, the business logic of this discipline uses a pandas DataFrame to store the input and output values outside |g|. Although |g| disciplines only handle input and output variables that are NumPy arrays, their local data and default input values can use DataFrame objects. The input and output grammars of the discipline shall use a naming convention to access the names of the columns of a DataFrame. The naming convention is built with the name of the input or output, the character ``~`` (this can be changed) and the name of the DataFrame column. The code executed by the discipline is in the ``_run`` method, where ``self.data``, i.e. the local data, has automatically been initialized with the default inputs and updated with the inputs passed to the discipline. A DataFrame can be retrieved by querying the corresponding key, e.g. ``df``, in the local data and then changes can be made to this DataFrame, e.g. ``discipline.data["df"]["x"] = value``. The default inputs and local data are instances of :class:`.DisciplineData`. .. seealso:: :class:`.DisciplineData` has more information about how DataFrames are handled. .. GENERATED FROM PYTHON SOURCE LINES 74-121 .. code-block:: Python class InputDataFrameModel(pa.DataFrameModel): x: Series[float] = pa.Field(unique=True) class OutputDataFrameModel(pa.DataFrameModel): y: Series[float] = pa.Field(unique=True) class InputGrammarModel(BaseModel): df: DataFrameType[InputDataFrameModel] class OutputGrammarModel(BaseModel): df: DataFrameType[OutputDataFrameModel] class DataConverter(PydanticGrammarDataConverter): """A data converter where some coupling variables are 2D NumPy arrays.""" def convert_value_to_array(self, name, value): if name == "df": return value.to_numpy().flatten() return super().convert_value_to_array(name, value) def convert_array_to_value(self, name, array_): if name == "df": return DataFrame({"x": [array_[0]], "y": [array_[1]]}) return super().convert_array_to_value(name, array_) PydanticGrammar.DATA_CONVERTER_CLASS = DataConverter class DataFrameDiscipline(Discipline): default_grammar_type = Discipline.GrammarType.PYDANTIC def __init__(self) -> None: super().__init__() self.input_grammar = PydanticGrammar("inputs", model=InputGrammarModel) self.output_grammar = PydanticGrammar("outputs", model=OutputGrammarModel) self.default_input_data = {"df": DataFrame(data={"x": [0.0]})} def _run(self, input_data: StrKeyMapping) -> StrKeyMapping | None: df = self.local_data["df"] df["y"] = 1.0 - 0.2 * df["x"] .. GENERATED FROM PYTHON SOURCE LINES 122-124 Instantiate the discipline -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python discipline = DataFrameDiscipline() .. GENERATED FROM PYTHON SOURCE LINES 127-130 Execute the discipline ---------------------- Then, we can execute it easily, either considering default inputs: .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: Python discipline.execute() .. rst-class:: sphx-glr-script-out .. code-block:: none {'df': x y 0 0.0 1.0} .. GENERATED FROM PYTHON SOURCE LINES 133-134 or using new inputs: .. GENERATED FROM PYTHON SOURCE LINES 134-135 .. code-block:: Python discipline.execute({"df": DataFrame(data={"x": [1.0]})}) .. rst-class:: sphx-glr-script-out .. code-block:: none {'df': x y 0 1.0 0.8} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.095 seconds) .. _sphx_glr_download_examples_disciplines_basics_plot_data_frame.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_data_frame.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_data_frame.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_data_frame.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_