.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/design_space/plot_parameter_space.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_design_space_plot_parameter_space.py: Parameter space =============== In this example, we will see the basics of :class:`.ParameterSpace`. .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library from gemseo.algos.parameter_space import ParameterSpace from gemseo.api import configure_logger, create_discipline, create_scenario configure_logger() standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 41-45 Firstly, a :class:`.ParameterSpace` does not require any mandatory argument. Create a parameter space ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: default parameter_space = ParameterSpace() .. GENERATED FROM PYTHON SOURCE LINES 51-55 Then, we can add either deterministic variables from their lower and upper bounds (use :meth:`.DesignSpace.add_variable`) or uncertain variables from their distribution names and parameters (use :meth:`.add_random_variable`) .. GENERATED FROM PYTHON SOURCE LINES 55-60 .. code-block:: default parameter_space.add_variable("x", l_b=-2.0, u_b=2.0) parameter_space.add_random_variable("y", "SPNormalDistribution", mu=0.0, sigma=1.0) print(parameter_space) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Parameter space | +------+-------------+-------+-------------+-------+-------------------------+----------------+-------------+------+--------------------+---------------------------+ | name | lower_bound | value | upper_bound | type | Initial distribution | Transformation | Support | Mean | Standard deviation | Range | +------+-------------+-------+-------------+-------+-------------------------+----------------+-------------+------+--------------------+---------------------------+ | x | -2 | None | 2 | float | | | | | | | | y | -inf | 0 | inf | float | norm(mu=0.0, sigma=1.0) | y | [-inf inf] | 0.0 | 1.0 | [-7.03448383 7.03448691] | +------+-------------+-------+-------------+-------+-------------------------+----------------+-------------+------+--------------------+---------------------------+ .. GENERATED FROM PYTHON SOURCE LINES 61-63 We can check that the deterministic and uncertain variables are implemented as deterministic and deterministic variables respectively: .. GENERATED FROM PYTHON SOURCE LINES 63-68 .. code-block:: default print("x is deterministic: ", parameter_space.is_deterministic("x")) print("y is deterministic: ", parameter_space.is_deterministic("y")) print("x is uncertain: ", parameter_space.is_uncertain("x")) print("y is uncertain: ", parameter_space.is_uncertain("y")) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none x is deterministic: True y is deterministic: False x is uncertain: False y is uncertain: True .. GENERATED FROM PYTHON SOURCE LINES 69-73 Sample from the parameter space ------------------------------- We can sample the uncertain variables from the :class:`.ParameterSpace` and get values either as an array (default value) or as a dictionary: .. GENERATED FROM PYTHON SOURCE LINES 73-78 .. code-block:: default sample = parameter_space.get_sample(n_samples=2, as_dict=True) print(sample) sample = parameter_space.get_sample(n_samples=4) print(sample) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [{'y': array([1.62434536])}, {'y': array([-0.61175641])}] [[-0.52817175] [-1.07296862] [ 0.86540763] [-2.3015387 ]] .. GENERATED FROM PYTHON SOURCE LINES 79-84 Sample a discipline over the parameter space -------------------------------------------- We can also sample a discipline over the parameter space. For simplicity, we instantiate an :class:`.AnalyticDiscipline` from a dictionary of expressions and update the cache policy # so as to cache all data in memory. .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. code-block:: default discipline = create_discipline("AnalyticDiscipline", expressions_dict={"z": "x+y"}) discipline.set_cache_policy(discipline.MEMORY_FULL_CACHE) .. GENERATED FROM PYTHON SOURCE LINES 89-100 From these parameter space and discipline, we build a :class:`.DOEScenario` and execute it with a Latin Hypercube Sampling algorithm and 100 samples. .. warning:: A :class:`.Scenario` deals with all variables available in the :class:`.DesignSpace`. By inheritance, a :class:`.DOEScenario` deals with all variables available in the :class:`.ParameterSpace`. Thus, if we do not filter the uncertain variables, the :class:`.DOEScenario` will consider all variables. In particular, the deterministic variables will be consider as uniformly distributed. .. GENERATED FROM PYTHON SOURCE LINES 100-106 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 100}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'eval_jac': False, 'algo': 'lhs', 'n_samples': 100} .. GENERATED FROM PYTHON SOURCE LINES 107-109 We can visualize the result by encapsulating the disciplinary cache in a :class:`.Dataset`: .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. code-block:: default dataset = discipline.cache.export_to_dataset() .. GENERATED FROM PYTHON SOURCE LINES 112-113 This visualization can be tabular for example: .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: default print(dataset.export_to_dataframe()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none inputs outputs x y z 0 0 0 0 1.299635 -0.989742 0.309892 1 -1.735541 -0.775826 -2.511367 2 -1.970047 -1.937612 -3.907659 3 1.069838 0.119366 1.189204 4 0.941424 1.992783 2.934207 .. ... ... ... 95 -0.209952 -1.634437 -1.844389 96 0.192194 -1.225602 -1.033407 97 0.672494 0.361084 1.033578 98 1.132283 0.913791 2.046074 99 -0.764176 0.263038 -0.501139 [100 rows x 3 columns] .. GENERATED FROM PYTHON SOURCE LINES 116-117 or graphical by means of a scatter plot matrix for example: .. GENERATED FROM PYTHON SOURCE LINES 117-119 .. code-block:: default dataset.plot("ScatterMatrix") .. image:: /examples/design_space/images/sphx_glr_plot_parameter_space_001.png :alt: plot parameter space :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 120-124 Sample a discipline over the uncertain space -------------------------------------------- If we want to sample a discipline over the uncertain space, we need to filter the uncertain variables: .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: default parameter_space.filter(parameter_space.uncertain_variables) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 127-129 Then, we clear the cache, create a new scenario from this parameter space containing only the uncertain variables and execute it. .. GENERATED FROM PYTHON SOURCE LINES 129-135 .. code-block:: default discipline.cache.clear() scenario = create_scenario( [discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 100}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'eval_jac': False, 'algo': 'lhs', 'n_samples': 100} .. GENERATED FROM PYTHON SOURCE LINES 136-140 Finally, we build a dataset from the disciplinary cache and visualize it. We can see that the deterministic variable 'x' is set to its default value for all evaluations, contrary to the previous case where we were considering the whole parameter space. .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: default dataset = discipline.cache.export_to_dataset() print(dataset.export_to_dataframe()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none inputs outputs x y z 0 0 0 0 0.0 0.415113 0.415113 1 0.0 0.055256 0.055256 2 0.0 0.489919 0.489919 3 0.0 -0.523228 -0.523228 4 0.0 1.138853 1.138853 .. ... ... ... 95 0.0 0.607114 0.607114 96 0.0 0.550599 0.550599 97 0.0 0.114359 0.114359 98 0.0 -0.382169 -0.382169 99 0.0 0.011416 0.011416 [100 rows x 3 columns] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.511 seconds) .. _sphx_glr_download_examples_design_space_plot_parameter_space.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_parameter_space.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_parameter_space.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_