.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/versioning/versioned_function.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_versioning_versioned_function.py: Function versioning =================== InDSL comes with the :py:mod:`indsl.versioning` module, which allows to implement multiple versions of InDSL functions. As a library user, one can then select and execute a specific function version. Example ------- In this example, we implement the `abs_diff` function, which computes the element-wise absolute difference of two time-series. We will first implement a naive version of that function, which we name version 1.0 (versioning in inDSL always starts with 1.0), followed by a more robust version 1.1. .. GENERATED FROM PYTHON SOURCE LINES 20-27 Implementation -------------- Implementation of v1.0 ^^^^^^^^^^^^^^^^^^^^^^ We begin with a simple implementation: .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python import pandas as pd from indsl import versioning @versioning.register(version="1.0", deprecated=True) def abs_diff(a: pd.Series, b: pd.Series) -> pd.Series: return (a - b).abs() .. GENERATED FROM PYTHON SOURCE LINES 39-43 They key in this function definition is the :func:`indsl.versioning.register` decorator. This decorator registers the function as a versioned function with name `abs_diff` and version `1.0`. We also mark the function as deprecated, since we will soon implement a new version of the function. This means that we can retrieve and execute this version, even after newer version of the same functions have been registered. .. GENERATED FROM PYTHON SOURCE LINES 46-48 Our, initial implementation is not very robust and results easily in `nan` outputs. This happens specifically when we apply `abs`diff` to time-series with non-matching indices: .. GENERATED FROM PYTHON SOURCE LINES 48-57 .. code-block:: Python idx = pd.date_range("2022-01-01", periods=5, freq="1h") a = pd.Series([1, 2, 3, 4, 5], index=idx) idx = pd.date_range("2022-01-01", periods=3, freq="2h") b = pd.Series([1, 3, 5], index=idx) abs_diff(a, b) .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-01-01 00:00:00 0.0 2022-01-01 01:00:00 NaN 2022-01-01 02:00:00 0.0 2022-01-01 03:00:00 NaN 2022-01-01 04:00:00 0.0 dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 58-59 Version 1.1 will fix this issue through a more robust implementation. .. GENERATED FROM PYTHON SOURCE LINES 61-66 Implementation of v1.1 ^^^^^^^^^^^^^^^^^^^^^^ Next, we implement the new version of the `abs_diff` and mark it as version 1.1. .. GENERATED FROM PYTHON SOURCE LINES 66-76 .. code-block:: Python from indsl.resample import reindex_v1 # noqa @versioning.register(version="1.1") # type: ignore def abs_diff(a: pd.Series, b: pd.Series) -> pd.Series: a, b = reindex_v1.reindex(a, b) return (a - b).abs() .. GENERATED FROM PYTHON SOURCE LINES 77-78 We rely on the build-in function `reindex` to align the indices of the time-series (using linear-interpolation) before performing the operations. .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: Python abs_diff(a, b) .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-01-01 00:00:00 0.0 2022-01-01 01:00:00 0.0 2022-01-01 02:00:00 0.0 2022-01-01 03:00:00 0.0 2022-01-01 04:00:00 0.0 Freq: h, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 82-87 Getting versioned functions and their versions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ We can get a list of all versioned functions with: .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python versioning.get_registered_functions() .. rst-class:: sphx-glr-script-out .. code-block:: none ['reindex', 'reindex_many', 'interpolate', 'resample', 'resample_to_granularity', 'trapezoidal_integration', 'differentiate', 'butterworth', 'ewma', 'lwma', 'sma', 'negative_running_hours_check', 'value_decrease_check', 'drift', 'flow_through_valve', 'wavelet_filter', 'arma_predictor', 'holt_winters_predictor', 'no_op', 'detect_outliers', 'remove_outliers', 'SG_SMOOTHER', 'ALMA_SMOOTHER', 'ARMA_SMOOTHER', 'BTR_SMOOTHER', 'CHB_SMOOTHER', 'EXP_WMA', 'LINEAR_WMA', 'SIMPLE_MA', 'INTERPOLATE', 'RESAMPLE_EXTENDED', 'RESAMPLE', 'POLY_REGRESSOR', 'SHUTIN_CALC', 'PI_CALC', 'PASSTHROUGH', 'OUTLIER_DETECTOR', 'DRIFT_DETECTOR', 'SS_DETECTOR', 'VARIABLE_MA', 'WAVELET_FILTER', 'STATUS_FLAG_FILTER', 'OUTLIERS_REMOVE', 'ROUND', 'FLOOR', 'CEIL', 'SIGN', 'CLIP', 'MAX', 'MIN', 'BIN_MAP', 'SIN', 'COS', 'TAN', 'ARCSIN', 'ARCCOS', 'ARCTAN', 'ARCTAN2', 'DEG2RAD', 'RAD2DEG', 'SINH', 'COSH', 'TANH', 'ARCSINH', 'ARCCOSH', 'ARCTANH', 'ADD', 'SUB', 'MUL', 'DIV', 'POW', 'INV', 'SQRT', 'NEG', 'ABS', 'MOD', 'INTEGRATE', 'DDX', 'EXP', 'LOG', 'LOG2', 'LOG10', 'LOGN', 'versioning_test_op', 'abs_diff'] .. GENERATED FROM PYTHON SOURCE LINES 91-92 We can retrieve which versions we have of a function with: .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: Python versioning.get_versions("abs_diff") .. rst-class:: sphx-glr-script-out .. code-block:: none ['1.0', '1.1'] .. GENERATED FROM PYTHON SOURCE LINES 96-101 Running versioned functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^ We can access and run specific function version with the `versioning.get` command: .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python abs_diff_v1 = versioning.get("abs_diff", version="1.0") abs_diff_v1(a, b) .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-01-01 00:00:00 0.0 2022-01-01 01:00:00 NaN 2022-01-01 02:00:00 0.0 2022-01-01 03:00:00 NaN 2022-01-01 04:00:00 0.0 dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 105-106 Omitting the version argument will automatically select the latest version .. GENERATED FROM PYTHON SOURCE LINES 106-110 .. code-block:: Python abs_diff_v1_1 = versioning.get("abs_diff") abs_diff_v1_1(a, b) # sphinx_gallery_thumbnail_path = '_static/images/versioning_thumbnail.png' .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-01-01 00:00:00 0.0 2022-01-01 01:00:00 0.0 2022-01-01 02:00:00 0.0 2022-01-01 03:00:00 0.0 2022-01-01 04:00:00 0.0 Freq: h, dtype: float64 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.013 seconds) .. _sphx_glr_download_auto_examples_versioning_versioned_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: versioned_function.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: versioned_function.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: versioned_function.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_