.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/grammars/plot_name_mapping.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_grammars_plot_name_mapping.py: Rename discipline variables =========================== .. GENERATED FROM PYTHON SOURCE LINES 20-28 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo.core.discipline.data_processor import NameMapping from gemseo.disciplines.analytic import AnalyticDiscipline .. GENERATED FROM PYTHON SOURCE LINES 29-35 :class:`.NameMapping` is a :class:`.DataProcessor` to rename one or more variables of a :class:`.Discipline`. In this example, we consider an :class:`.AnalyticDiscipline` computing the sum and the difference of two operands: .. GENERATED FROM PYTHON SOURCE LINES 35-46 .. code-block:: Python discipline = AnalyticDiscipline( { "sum": "first_operand+second_operand", "diff": "first_operand-second_operand", }, name="SumAndDiff", ) discipline.io.input_grammar.defaults = { "first_operand": array([1.0]), "second_operand": array([2.0]), } .. GENERATED FROM PYTHON SOURCE LINES 47-63 We want to use this discipline in a study to sum and subtract the input variables ``"x1"`` and ``"x2"`` and return the output variables ``"y1"`` and ``"y2"``. Unfortunately, its input names ``"first_operand"`` and ``"second_operand"`` and the output names ``"sum"`` and ``"diff"`` do not match the naming of our user study. For this simple example, we could have easily created a new :class:`.AnalyticDiscipline` with an expression dictionary using ``"x1"``, ``"x2"``, ``"y1"`` and ``"y2"``. but this solution is not generic because in practice, the discipline's :meth:`_run` method manipulating the data from the variable names cannot be modified. To fix this problem, we can rename the input and output variables: .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python discipline.io.input_grammar.rename_element("first_operand", "x1") discipline.io.input_grammar.rename_element("second_operand", "x2") discipline.io.output_grammar.rename_element("sum", "y1") discipline.io.output_grammar.rename_element("diff", "y2") .. GENERATED FROM PYTHON SOURCE LINES 68-70 and set its :class:`.DataProcessor` to a :class:`.NameMapping` defined from a dictionary of the form ``{new_variable_name: variable_name}``: .. GENERATED FROM PYTHON SOURCE LINES 70-76 .. code-block:: Python discipline.io.data_processor = NameMapping({ "x1": "first_operand", "x2": "second_operand", "y1": "sum", "y2": "diff", }) .. GENERATED FROM PYTHON SOURCE LINES 77-78 We can verify that the discipline can be executed correctly: .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: Python discipline.execute() discipline.io.get_input_data(), discipline.io.get_output_data() .. rst-class:: sphx-glr-script-out .. code-block:: none ({'x1': array([1.]), 'x2': array([2.])}, {'y1': array([3.]), 'y2': array([-1.])}) .. GENERATED FROM PYTHON SOURCE LINES 82-83 This :class:`.DataProcessor` is compatible with the use of namespaces: .. GENERATED FROM PYTHON SOURCE LINES 83-88 .. code-block:: Python discipline.add_namespace_to_input("x1", "ns_in") discipline.add_namespace_to_output("y1", "ns_out") discipline.execute() discipline.io.get_input_data(), discipline.io.get_output_data() .. rst-class:: sphx-glr-script-out .. code-block:: none ({'x2': array([2.]), 'ns_in:x1': array([1.])}, {'ns_out:y1': array([3.]), 'y2': array([-1.])}) .. GENERATED FROM PYTHON SOURCE LINES 89-100 Finally, we may also be interested in :ref:`this example `, which illustrates the use of :func:`.rename_discipline_variables`. Given a collection of disciplines and a dictionary of translations generated either by hand or by a user-friendly interface, this function automates the process presented in the current example, namely renaming the input variables, renaming the output variables and using a :class:`.NameMapping`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.014 seconds) .. _sphx_glr_download_examples_disciplines_grammars_plot_name_mapping.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_name_mapping.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_name_mapping.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_name_mapping.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_