.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_stochastic_processes/plot_field_manipulation.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_auto_stochastic_processes_plot_field_manipulation.py: Draw a field ============ .. GENERATED FROM PYTHON SOURCE LINES 7-17 In this example, we show the different ways of drawing a :class:`~openturns.Field` that is a realization of a process. We consider the following fields: - **Case 1**: A field defined on a 2-d mesh with 2-d values, - **Case 2**: A field defined on a 2-d mesh with scalar values, - **Case 3**: A field defined on a 1-d mesh with 2-d values, - **Case 4**: A field defined on a 1-d mesh with scalar values. .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: Python import openturns as ot import openturns.viewer as otv # sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 25-32 Case 1: A field defined on a 2-d mesh with 2-d values ------------------------------------------------------ We consider a normal process defined on :math:`\cD = [0,1]^2`, with 2-d output values, defined by a covariance function which is a :class:`~openturns.TensorizedCovarianceModel` built as the tensorization of two :class:`~openturns.MaternModel` covariance functions. The domain :math:`\cD` is discretized by a regular grid, using the class :class:`~openturns.IntervalMesher`. .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python cov_model = ot.TensorizedCovarianceModel([ot.MaternModel([0.1] * 2, [0.01], 2.5)] * 2) mesh = ot.IntervalMesher([30] * 2).build(ot.Interval([0.0] * 2, [1.0] * 2)) normal_proc = ot.GaussianProcess(cov_model, mesh) field = normal_proc.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 38-39 We first draw the field with arrows fixed on each vertex of the mesh. .. GENERATED FROM PYTHON SOURCE LINES 39-44 .. code-block:: Python g_1 = field.draw() g_1.setTitle(r"A field on 2-d mesh and 2-d values") g_1.setXTitle(r"$x_0$") g_1.setYTitle(r"$x_1$") .. GENERATED FROM PYTHON SOURCE LINES 45-47 In order to change the arrows, we can modify the :class:`~openturns.ResourceMap` keys associated to the class :class:`~openturns.Field`. .. GENERATED FROM PYTHON SOURCE LINES 47-55 .. code-block:: Python ot.ResourceMap.SetAsScalar("Field-ArrowRatio", 0.05) ot.ResourceMap.SetAsScalar("Field-ArrowScaling", 0.8) g_2 = field.draw() g_2.setTitle("The same field with other arrows parameters") g_2.setXTitle(r"$x_0$") g_2.setYTitle(r"$x_1$") .. GENERATED FROM PYTHON SOURCE LINES 56-57 We can compare both figures. .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: Python grid = ot.GridLayout(1, 2) grid.setGraph(0, 0, g_1) grid.setGraph(0, 1, g_2) view = otv.View(grid) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_001.svg :alt: , A field on 2-d mesh and 2-d values, The same field with other arrows parameters :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-65 We can also use the output values to deform the initial mesh and draw the deformed mesh. First, we recover the default values of the ResourceMap keys. .. GENERATED FROM PYTHON SOURCE LINES 65-74 .. code-block:: Python ot.ResourceMap.SetAsScalar("Field-ArrowRatio", 0.01) ot.ResourceMap.SetAsScalar("Field-ArrowScaling", 1) deformed_field = field.asDeformedMesh() g_def = deformed_field.draw() g_def.setTitle("The same field as deformed mesh") g_def.setXTitle(r"$x_0$") g_def.setYTitle(r"$x_1$") g_def.setLegendPosition("") .. GENERATED FROM PYTHON SOURCE LINES 75-76 We can compare both figures. .. GENERATED FROM PYTHON SOURCE LINES 76-81 .. code-block:: Python grid = ot.GridLayout(1, 2) grid.setGraph(0, 0, g_1) grid.setGraph(0, 1, g_def) view = otv.View(grid) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_002.svg :alt: , A field on 2-d mesh and 2-d values, The same field as deformed mesh :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-87 Case 2: A field defined on a 2-d mesh with scalar values --------------------------------------------------------- We consider a normal process defined on :math:`\cD = [0,1]^2`, with scalar output values, defined by a covariance function which is a :class:`~openturns.MaternModel`. The domain :math:`\cD` is discretized by a regular grid, using the class :class:`~openturns.IntervalMesher`. .. GENERATED FROM PYTHON SOURCE LINES 87-92 .. code-block:: Python cov_model = ot.MaternModel([0.15] * 2, [0.01], 2.5) mesh = ot.IntervalMesher([35] * 2).build(ot.Interval([0.0] * 2, [1.0] * 2)) normal_proc = ot.GaussianProcess(cov_model, mesh) field = normal_proc.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 93-94 We draw the field by filling each cell of the mesh with a color. .. GENERATED FROM PYTHON SOURCE LINES 94-100 .. code-block:: Python g_1 = field.draw() g_1.setTitle("A field on 2-d mesh and scalar values") g_1.setXTitle(r"$x_0$") g_1.setYTitle(r"$x_1$") .. GENERATED FROM PYTHON SOURCE LINES 101-102 We can also draw iso-value lines of the output values using interpolation. .. GENERATED FROM PYTHON SOURCE LINES 102-108 .. code-block:: Python g_2 = field.drawMarginal(0, True) g_2.setTitle("A field on 2-d mesh and scalar values") g_2.setXTitle(r"$x_0$") g_2.setYTitle(r"$x_1$") g_2.setLegendPosition("") .. GENERATED FROM PYTHON SOURCE LINES 109-110 We can compare both figures. .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. code-block:: Python grid = ot.GridLayout(1, 2) grid.setGraph(0, 0, g_1) grid.setGraph(0, 1, g_2) view = otv.View(grid) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_003.svg :alt: , A field on 2-d mesh and scalar values, A field on 2-d mesh and scalar values :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-122 Case 3: A field defined on a 1-d mesh with 2-d values ------------------------------------------------------- We consider a :class:`~openturns.RandomWalk` process which distribution is the normal distribution of dimension 2 with zero mean and identity covariance matrix. The process is defined on :math:`\cD = [0,1]` which is discretized by a regular grid, using the class :class:`~openturns.RegularGrid`. .. GENERATED FROM PYTHON SOURCE LINES 122-130 .. code-block:: Python dist_rw = ot.Normal(2) origin = dist_rw.getMean() N = 10000 step = 1.0 / N mesh = ot.RegularGrid(0, step, N) rw_process = ot.RandomWalk(origin, dist_rw, mesh) field = rw_process.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 131-132 We draw the field by interpolating the output vectors. .. GENERATED FROM PYTHON SOURCE LINES 132-138 .. code-block:: Python g = field.draw() g.setTitle("A 2-d random walk field") g.setXTitle(r"$x_0$") g.setYTitle(r"$x_1$") view = otv.View(g) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_004.svg :alt: A 2-d random walk field :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-145 Case 4: A field defined on a 1-d mesh with scalar values ---------------------------------------------------------- We consider a normal process defined on :math:`\cD = [0,1]`, with scalar output values, defined by a covariance function which is a :class:`~openturns.MaternModel`. The domain :math:`\cD` is discretized by a regular grid, using :class:`~openturns.IntervalMesher`. This process is stationary. .. GENERATED FROM PYTHON SOURCE LINES 145-150 .. code-block:: Python cov_model = ot.MaternModel([0.05], [0.01], 2.5) mesh = ot.IntervalMesher([1000]).build(ot.Interval(0.0, 1.0)) normal_proc = ot.GaussianProcess(cov_model, mesh) field = normal_proc.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 151-152 We draw its trajectory without interpolating the output values. .. GENERATED FROM PYTHON SOURCE LINES 152-157 .. code-block:: Python g_1 = field.draw() g_1.setTitle("A field on 1-d mesh and scalar values - Stationary process") g_1.setXTitle(r"$x_0$") g_1.setYTitle(r"$x_1$") .. GENERATED FROM PYTHON SOURCE LINES 158-159 We can also interpolate the output values. .. GENERATED FROM PYTHON SOURCE LINES 159-165 .. code-block:: Python g_2 = field.drawMarginal(0, True) g_2.setTitle("The same field with interpolation") g_2.setXTitle(r"$x_0$") g_2.setYTitle(r"$x_1$") g_2.setLegendPosition("") .. GENERATED FROM PYTHON SOURCE LINES 166-167 We can compare both figures. .. GENERATED FROM PYTHON SOURCE LINES 167-172 .. code-block:: Python grid = ot.GridLayout(1, 2) grid.setGraph(0, 0, g_1) grid.setGraph(0, 1, g_2) view = otv.View(grid) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_005.svg :alt: , A field on 1-d mesh and scalar values - Stationary process, The same field with interpolation :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 173-180 We consider now a normal process which is not stationary. We use the :class:`~openturns.FractionalBrownianMotionModel`: - when the Hurst exponent is close to 1, the trajectory is smooth, - when the Hurst exponent is equal to 1/2, the trajectory is Brownian motion (Wiener process), - when the Hurst exponent is close to 0, the process tends to a White noise process. .. GENERATED FROM PYTHON SOURCE LINES 180-192 .. code-block:: Python scale = 0.01 amplitude = 1.5 hurst_exponent = 0.75 cov_model = ot.FractionalBrownianMotionModel(scale, amplitude, hurst_exponent) mesh = ot.IntervalMesher([1000]).build(ot.Interval(0.0, 1.0)) normal_proc = ot.GaussianProcess(cov_model, mesh) field = normal_proc.getRealization() g_3 = field.drawMarginal(0, True) g_3.setTitle("A field on 1-d mesh and scalar values - Non stationary process") g_3.setXTitle(r"$x_0$") g_3.setYTitle(r"$x_1$") .. GENERATED FROM PYTHON SOURCE LINES 193-194 We can compare both processes. .. GENERATED FROM PYTHON SOURCE LINES 194-200 .. code-block:: Python grid = ot.GridLayout(1, 2) g_2.setTitle("A field on 1-d mesh and scalar values - Stationary process") grid.setGraph(0, 0, g_2) grid.setGraph(0, 1, g_3) view = otv.View(grid) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_006.svg :alt: , A field on 1-d mesh and scalar values - Stationary process, A field on 1-d mesh and scalar values - Non stationary process :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 201-202 We draw here a trajectory from a FractionalBrownianMotionModel with different Hurst exponent. .. GENERATED FROM PYTHON SOURCE LINES 202-217 .. code-block:: Python grid = ot.GridLayout(1, 3) hurst_exponent_list = [0.99, 0.50, 0.001] for index, hurst_exponent in enumerate(hurst_exponent_list): cov_model = ot.FractionalBrownianMotionModel(scale, amplitude, hurst_exponent) normal_proc = ot.GaussianProcess(cov_model, mesh) field = normal_proc.getRealization() g = field.drawMarginal(0, True) g.setTitle( r"Fractional Brownian Motion Model, Hurst exponent = " + str(hurst_exponent) ) g.setXTitle(r"$x_0$") g.setYTitle(r"$x_1$") grid.setGraph(0, index, g) view = otv.View(grid) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_007.svg :alt: , Fractional Brownian Motion Model, Hurst exponent = 0.99, Fractional Brownian Motion Model, Hurst exponent = 0.5, Fractional Brownian Motion Model, Hurst exponent = 0.001 :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_007.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 218-219 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 219-220 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_stochastic_processes_plot_field_manipulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_field_manipulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_field_manipulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_field_manipulation.zip `