.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\RME_gettingstarted\plot_e04_common_grid_handling.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_e04_common_grid_handling.py: Common Grid Handling ==================== A common problem is when multiple inputs into a workflow share a common dimension like frequency, but those frequency grids aren't aligned properly. This often results in having to manually trim down arrays based on a known frequency list. The RME offers an automatic common grid handling feature to do that for you. It acts on any RMEMeas input into a propagating function. .. GENERATED FROM PYTHON SOURCE LINES 15-19 Creating an Objects ------------------- First lets create 2 RMEMeas objects with slightly different coordinates on dimension 'd1'. .. GENERATED FROM PYTHON SOURCE LINES 19-51 .. code-block:: Python from rmellipse.uobjects import RMEMeas from rmellipse.propagators import RMEProp import xarray as xr import numpy as np def make_measurement(d1_coords): """Make a sample measurement with length 4 coordinate set.""" nom = xr.DataArray(np.zeros((4, 2)), dims=('d1', 'd2'), coords={'d1': d1_coords, 'd2': np.arange(2)}) meas = RMEMeas.from_nom(name='meas', nom=nom) meas.add_umech( name='mymechanisms', value=meas.nom + np.ones(meas.nom.shape) * 0.01, dof=np.inf, category={'Type': 'B', 'Origin': 'Data Sheet'}, add_uid=True ) for i in range(100): meas.add_mc_sample(meas.nom + np.random.rand(*meas.nom.shape) * 0.01) return meas m1 = make_measurement([0, 1, 2, 3]) m2 = make_measurement([0, 1.1, 2, 2.9]) .. rst-class:: sphx-glr-script-out .. code-block:: none C:\Users\dcg2\AppData\Local\Temp\1\tmps8r3znms\a287edda79ce3f5464ad6ffd462fce95db2ae8aa\docs\examples\RME_gettingstarted\plot_e04_common_grid_handling.py:35: DeprecationWarning: add_uid is deprecated and will be removed in 0.5.0, all umech_ids will be assigned as a uid moving. This is included to avoid breaking existing code. meas.add_umech( C:\Users\dcg2\AppData\Local\Temp\1\tmps8r3znms\a287edda79ce3f5464ad6ffd462fce95db2ae8aa\docs\examples\RME_gettingstarted\plot_e04_common_grid_handling.py:35: DeprecationWarning: add_uid is deprecated and will be removed in 0.5.0, all umech_ids will be assigned as a uid moving. This is included to avoid breaking existing code. meas.add_umech( .. GENERATED FROM PYTHON SOURCE LINES 52-67 Setting up the Propagator ------------------------- Next, let's create a propagator. We are going to define the common grid as 'd1' and tell it to interpolate that grid to a set of common values. We tell it what those common values are with the common_coords argument. The verbose argument will print some information about the propagation as we go. See the :func:`rmellipse.propagators.RME.handle_common_grid` function for what methods are available for the propagator. When we wrap our add function in the propagator and print the d1 coordinate, we see that the d1 coordinate of x and y are now the same because the RME interpolated them down to supplied values. Note how the print statement happens twice, because a vectorized propagator calls the function on the`RMEMeas.cov`attribute then on the`RMEMeas.mc` attribute. .. GENERATED FROM PYTHON SOURCE LINES 67-86 .. code-block:: Python myprop = RMEProp( sensitivity=True, montecarlo_sims=100, common_grid='d1', handle_common_grid_method='interp_common', common_coords={'d1': [0, .5, 1.5, 2.5]}, vectorize=True, verbose=True) @myprop.propagate def add(x, y): """Add two data sets.""" print(x.d1.values, y.d1.values) return x + y m3 = add(m1, m2) .. rst-class:: sphx-glr-script-out .. code-block:: none Propagating: add ---------------- [0. 0.5 1.5 2.5] [0. 0.5 1.5 2.5] [0. 0.5 1.5 2.5] [0. 0.5 1.5 2.5] grid handling runtime:0.0474550724029541 sec linear mechanisms:2 linear runtime:0.009504318237304688 sec montecarlo trials:100 mc runtime:0.0019347667694091797 sec recategorize runtime:0.013343334197998047 sec repack runtime:0.019120216369628906 sec .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.574 seconds) .. _sphx_glr_download_auto_examples_RME_gettingstarted_plot_e04_common_grid_handling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_e04_common_grid_handling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_e04_common_grid_handling.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_e04_common_grid_handling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_