.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/types/plot_remapping_discipline.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_types_plot_remapping_discipline.py: Rename the input and output variables ===================================== The :class:`.RemappingDiscipline` can be used to rename the input and output variables of an original discipline including defining a variable as a part of an original one. .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo.core.discipline import MDODiscipline from gemseo.disciplines.remapping import RemappingDiscipline .. GENERATED FROM PYTHON SOURCE LINES 38-48 Let us consider a discipline that sums up the fruits of the market. The fruits can be classified into three categories: pears, Gala apples and Fuji apples. Then, the input variable of the discipline called ``fruits`` is a triplet containing the numbers of pears, Gala apples and Fuji apples so ordered. Concerning the outputs, ``n_fruits`` is the total number of fruits while ``n_fruits_per_category`` gathers the numbers of pears and apples so ordered. This discipline can be coded as follows: .. GENERATED FROM PYTHON SOURCE LINES 48-65 .. code-block:: Python class FruitCounting(MDODiscipline): def __init__(self) -> None: super().__init__() self.input_grammar.update_from_names(["fruits"]) self.output_grammar.update_from_names(["n_fruits", "n_fruits_per_category"]) self.default_inputs = {"fruits": array([1, 2, 3])} def _run(self) -> None: fruits = self.local_data["fruits"] self.store_local_data( n_fruits=array([fruits.sum()]), n_fruits_per_category=array([fruits[0], fruits[1:3].sum()]), ) .. GENERATED FROM PYTHON SOURCE LINES 66-67 and we can instantiate it: .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python fruit_counting = FruitCounting() .. GENERATED FROM PYTHON SOURCE LINES 70-74 Then, we create a new discipline renaming ``fruits`` as ``pear`` and ``apples`` and ``n_fruits`` and ``n_fruits_per_category`` as ``total`` and ``sub_total`` to improve the naming: .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: Python clearer_fruit_counting = RemappingDiscipline( fruit_counting, {"pear": ("fruits", 0), "apples": ("fruits", [1, 2])}, {"total": "n_fruits", "sub_total": "n_fruits_per_category"}, ) .. GENERATED FROM PYTHON SOURCE LINES 81-96 Note: :class:`.RemappingDiscipline` requires an instance of the original discipline, the input names mapping to the original input names and the outputs names mapping to the original output names. More precisely, an input or output name mapping looks like ``{"new_x": "x", "new_y": ("y", components)}`` where the variable ``"new_x"`` corresponds to the original variable ``"x"`` and the variable ``"new_y"`` corresponds to some ``components`` of the original variable ``"y"``. ``components`` can be an integer ``i`` (the ``i``-th component of ``y``), a sequence of integers ``[i, j, k]`` (the ``i``-th, ``j``-th and ``k``-th components of ``y``) or an iterable of integers ``range(i, j+1)`` (from the ``i``-th to the ``j``-th components of ``y``). .. GENERATED FROM PYTHON SOURCE LINES 98-100 We can execute this discipline with the original default input values, namely 1 pear, 2 Gala apples and 3 Fuji apples: .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: Python clearer_fruit_counting.execute() clearer_fruit_counting.get_input_data(), clearer_fruit_counting.get_output_data() .. rst-class:: sphx-glr-script-out .. code-block:: none ({'apples': array([2, 3]), 'pear': array([1])}, {'total': array([6]), 'sub_total': array([1, 5])}) .. GENERATED FROM PYTHON SOURCE LINES 104-105 or with new input data: .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: Python clearer_fruit_counting.execute({"pear": array([4]), "apples": array([3, 1])}) clearer_fruit_counting.get_input_data(), clearer_fruit_counting.get_output_data() .. rst-class:: sphx-glr-script-out .. code-block:: none ({'pear': array([4]), 'apples': array([3, 1])}, {'total': array([8]), 'sub_total': array([4, 4])}) .. GENERATED FROM PYTHON SOURCE LINES 109-112 To be even more clear, we can split ``apples`` into ``gala`` and ``fuji`` and ``sub_total`` into ``n_pears`` and ``n_apples``: .. GENERATED FROM PYTHON SOURCE LINES 112-122 .. code-block:: Python even_clearer_fruit_counting = RemappingDiscipline( clearer_fruit_counting, {"pear": "pear", "gala": ("apples", 0), "fuji": ("apples", 0)}, { "total": "total", "n_pears": ("sub_total", 0), "n_apples": ("sub_total", 1), }, ) .. GENERATED FROM PYTHON SOURCE LINES 123-124 and count the number of fruits: .. GENERATED FROM PYTHON SOURCE LINES 124-133 .. code-block:: Python even_clearer_fruit_counting.execute({ "pear": array([4]), "gala": array([3]), "fuji": array([1]), }) ( even_clearer_fruit_counting.get_input_data(), even_clearer_fruit_counting.get_output_data(), ) .. rst-class:: sphx-glr-script-out .. code-block:: none ({'pear': array([4]), 'gala': array([3]), 'fuji': array([1])}, {'total': array([8]), 'n_apples': array([4]), 'n_pears': array([4])}) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.015 seconds) .. _sphx_glr_download_examples_disciplines_types_plot_remapping_discipline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_remapping_discipline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_remapping_discipline.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_