{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Common Grid Handling\n\nA common problem is when multiple inputs into a workflow\nshare a common dimension like frequency, but those frequency grids aren't\naligned properly. This often results in having to manually\ntrim down arrays based on a known frequency list.\n\nThe RME offers an automatic common grid handling feature to do that for you.\nIt acts on any RMEMeas input into a propagating function.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating an Objects\nFirst lets create 2 RMEMeas objects with slightly different\ncoordinates on dimension 'd1'.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from rmellipse.uobjects import RMEMeas\nfrom rmellipse.propagators import RMEProp\nimport xarray as xr\nimport numpy as np\n\n\ndef make_measurement(d1_coords):\n \"\"\"Make a sample measurement with length 4 coordinate set.\"\"\"\n nom = xr.DataArray(np.zeros((4, 2)),\n dims=('d1', 'd2'),\n coords={'d1': d1_coords,\n 'd2': np.arange(2)})\n\n meas = RMEMeas.from_nom(name='meas', nom=nom)\n\n meas.add_umech(\n name='mymechanisms',\n value=meas.nom + np.ones(meas.nom.shape) * 0.01,\n dof=np.inf,\n category={'Type': 'B', 'Origin': 'Data Sheet'},\n add_uid=True\n )\n\n for i in range(100):\n meas.add_mc_sample(meas.nom + np.random.rand(*meas.nom.shape) * 0.01)\n return meas\n\n\nm1 = make_measurement([0, 1, 2, 3])\nm2 = make_measurement([0, 1.1, 2, 2.9])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting up the Propagator\n\nNext, let's create a propagator. We are going to define the common grid\nas 'd1' and tell it to interpolate that grid to a set of common values. We\ntell it what those common values are with the common_coords argument. The\nverbose argument will print some information about the propagation as we go.\nSee the :func:`rmellipse.propagators.RME.handle_common_grid` function for\nwhat methods are available for the propagator.\n\nWhen we wrap our add function in the propagator and print the d1 coordinate,\nwe see that the d1 coordinate of x and y are now the same because the RME\ninterpolated them down to supplied values. Note how the print statement\nhappens twice, because a vectorized\npropagator calls the function on the`RMEMeas.cov`attribute then on the`RMEMeas.mc` attribute.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "myprop = RMEProp(\n sensitivity=True,\n montecarlo_sims=100,\n common_grid='d1',\n handle_common_grid_method='interp_common',\n common_coords={'d1': [0, .5, 1.5, 2.5]},\n vectorize=True,\n verbose=True)\n\n\n@myprop.propagate\ndef add(x, y):\n \"\"\"Add two data sets.\"\"\"\n print(x.d1.values, y.d1.values)\n return x + y\n\n\nm3 = add(m1, m2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 0 }