{ "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\t\"\"\"Make a sample measurement with length 4 coordinate set.\"\"\"\n\tnom = xr.DataArray(\n\t\tnp.zeros((4, 2)),\n\t\tdims=('d1', 'd2'),\n\t\tcoords={'d1': d1_coords, 'd2': np.arange(2)},\n\t)\n\n\tmeas = RMEMeas.from_nom(name='meas', nom=nom)\n\n\tmeas.add_umech(\n\t\tname='mymechanisms',\n\t\tvalue=meas.nom + np.ones(meas.nom.shape) * 0.01,\n\t\tdof=np.inf,\n\t\tcategory={'Type': 'B', 'Origin': 'Data Sheet'},\n\t\tadd_uid=True,\n\t)\n\n\tfor i in range(100):\n\t\tmeas.add_mc_sample(meas.nom + np.random.rand(*meas.nom.shape) * 0.01)\n\treturn 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\tsensitivity=True,\n\tmontecarlo_sims=100,\n\tcommon_grid='d1',\n\thandle_common_grid_method='interp_common',\n\tcommon_coords={'d1': [0, 0.5, 1.5, 2.5]},\n\tvectorize=True,\n\tverbose=True,\n)\n\n\n@myprop.propagate\ndef add(x, y):\n\t\"\"\"Add two data sets.\"\"\"\n\tprint(x.d1.values, y.d1.values)\n\treturn 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 }