.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\RME_gettingstarted\plot_e05_vectorizing.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_examples_RME_gettingstarted_plot_e05_vectorizing.py: Using Non-Vectorized Propagation ================================ Here we go over how to use a propagator with the vectorized setting turned off. .. GENERATED FROM PYTHON SOURCE LINES 9-13 Setup ----- First lets create a RMEMeas object we can interact with and initialize a propagator. .. GENERATED FROM PYTHON SOURCE LINES 13-41 .. code-block:: Python from rmellipse.uobjects import RMEMeas from rmellipse.propagators import RMEProp import xarray as xr import numpy as np nom = xr.DataArray(np.zeros((4, 2)), dims=('d1', 'd2'), coords={'d1': [0, 1, 2, 3], 'd2': np.arange(2)}) meas = RMEMeas.from_nom(name='meas', nom=nom) meas.add_umech( name='my_mechanism', value=meas.nom + np.ones(meas.nom.shape) * 0.01, dof=np.inf, category={'Type': 'B', 'Origin': 'Data Sheet'} ) for i in range(100): meas.add_mc_sample(meas.nom + np.random.rand(*meas.nom.shape) * 0.01) myprop = RMEProp( sensitivity=True, montecarlo_sims=100, verbose=True) .. GENERATED FROM PYTHON SOURCE LINES 42-53 Non-Vectorized Propagation -------------------------- Sometimes it isn't possible to vectorize a function. In that case, similar to the non-vectorized version, the inputs and outputs of the function need to be a DataArray for the propagator to recognize the object as being a RMEMeas object. The first dimension of any inputs that were RMEMeas objects will be 'umech_id' and the coordinates of that dimension will be the names of the uncertainty mechanism. For non vectorized inputs, the propagator will loop over that first dimension, so the inputs will have a length 1 first dimension called 'umech_id'. The output should also have that dimension and coordinate. .. GENERATED FROM PYTHON SOURCE LINES 53-70 .. code-block:: Python myprop.settings['montecarlo_sims'] = 0 myprop.settings['sensitivity'] = True myprop.settings['vectorize'] = False myprop.settings['verbose'] = False @myprop.propagate def add(x, y): """Add 2 numbers.""" output = x + y print(x.umech_id.values, output.umech_id.values) return output added = add(meas, 2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['nominal'] ['nominal'] 353a3892-1917-4c56-a60d-c80aabebf4cf 353a3892-1917-4c56-a60d-c80aabebf4cf .. GENERATED FROM PYTHON SOURCE LINES 71-79 Vectorized vs. Unvectorized --------------------------- By default a RME propagator vectorizes all its functions. This is because vectorized computation is much faster and will save a lot of time, especially with large data sets and large numbers of uncertainty mechanisms. For comparison, here is the same function run once with the vectorize functionality and once without. Note the orders of magnitude increase in run time for the Monte Carlo propagation. .. GENERATED FROM PYTHON SOURCE LINES 79-91 .. code-block:: Python @myprop.propagate def add2(x): return x + 2 myprop.settings['vectorize'] = True add2(meas) myprop.settings['vectorize'] = False add2(meas) .. rst-class:: sphx-glr-script-out .. code-block:: none add2 with nominal: Size: 64B array([[2., 2.], [2., 2.], [2., 2.], [2., 2.]]) Coordinates: * d1 (d1) int64 32B 0 1 2 3 * d2 (d2) int64 16B 0 1 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.366 seconds) .. _sphx_glr_download_auto_examples_RME_gettingstarted_plot_e05_vectorizing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_e05_vectorizing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_e05_vectorizing.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_e05_vectorizing.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_