{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Using Non-Vectorized Propagation\n\nHere we go over how to use a propagator with the vectorized setting turned off.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\nFirst lets create a RMEMeas object we can interact with and initialize\na propagator.\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\nnom = xr.DataArray(\n\tnp.zeros((4, 2)), dims=('d1', 'd2'), coords={'d1': [0, 1, 2, 3], 'd2': np.arange(2)}\n)\n\nmeas = RMEMeas.from_nom(name='meas', nom=nom)\n\nmeas.add_umech(\n\tname='my_mechanism',\n\tvalue=meas.nom + np.ones(meas.nom.shape) * 0.01,\n\tdof=np.inf,\n\tcategory={'Type': 'B', 'Origin': 'Data Sheet'},\n)\n\nfor i in range(100):\n\tmeas.add_mc_sample(meas.nom + np.random.rand(*meas.nom.shape) * 0.01)\n\nmyprop = RMEProp(sensitivity=True, montecarlo_sims=100, verbose=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Non-Vectorized Propagation\nSometimes it isn't possible to vectorize a function. In that case, similar\nto the non-vectorized version, the inputs and outputs of the function need\nto be a DataArray for the propagator to recognize the object as being\na RMEMeas object. The first dimension of any inputs that were RMEMeas objects will\nbe 'umech_id' and the coordinates of that dimension will be the\nnames of the uncertainty mechanism. For non vectorized inputs, the\npropagator will loop over that first dimension, so the inputs will have a\nlength 1 first dimension called 'umech_id'. The output should\nalso have that dimension and coordinate.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "myprop.settings['montecarlo_sims'] = 0\nmyprop.settings['sensitivity'] = True\nmyprop.settings['vectorize'] = False\nmyprop.settings['verbose'] = False\n\n\n@myprop.propagate\ndef add(x, y):\n\t\"\"\"Add 2 numbers.\"\"\"\n\toutput = x + y\n\tprint(x.umech_id.values, output.umech_id.values)\n\treturn output\n\n\nadded = add(meas, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vectorized vs. Unvectorized\nBy default a RME propagator vectorizes all its functions. This is because\nvectorized computation is much faster and\nwill save a lot of time, especially with large data sets and large numbers\nof uncertainty mechanisms. For comparison, here is the same function run\nonce with the vectorize functionality and once without. Note the orders of\nmagnitude increase in run time for the Monte Carlo propagation.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@myprop.propagate\ndef add2(x):\n\treturn x + 2\n\n\nmyprop.settings['vectorize'] = True\nadd2(meas)\n\nmyprop.settings['vectorize'] = False\nadd2(meas)" ] } ], "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 }