
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/data_quality/plot_value_decrease_check.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_data_quality_plot_value_decrease_check.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_data_quality_plot_value_decrease_check.py:


================================================
Checking for decreasing values in a timeseries
================================================

Example of algorithm that indicates decreasing values in time series data. This algorithm is applied on Running Hours time series.
It is a specific type of time series that is counting the number of running hours in a pump. Given that we expect
the number of running hours to either stay the same (if the pump is not running) or increase with time (if the pump
is running), the decrease in running hours value indicates bad data quality.

The output of the algorithm is an indicator time series with value 1 where there is a decrease in time series value,
and 0 otherwise. The indicator will be set to 1 until the data gets "back to normal" (that is, until time series reaches
the value it had before the value drop).

The algorithm is applied on the same data twice:

    #. Using default threshold; indicator will be set to 1 if data drop is higher than 0

    #. Using a threshold of 4.1; indicator will be set to 1 if data drop is higher than 4.1

.. GENERATED FROM PYTHON SOURCE LINES 22-55



.. image-sg:: /auto_examples/data_quality/images/sphx_glr_plot_value_decrease_check_001.png
   :alt: Indicator for decrease in time series values, Time series values, Indicator with default threshold 0.0, Indicator with threshold 4.1
   :srcset: /auto_examples/data_quality/images/sphx_glr_plot_value_decrease_check_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import os

    import matplotlib.pyplot as plt
    import pandas as pd

    from indsl.data_quality.value_decrease_indication import value_decrease_check


    # import generated data
    base_path = "" if __name__ == "__main__" else os.path.dirname(__file__)
    data = pd.read_csv(os.path.join(base_path, "../../datasets/data/ts_value_reduction_data.csv"), index_col=0)
    data = data.squeeze()
    data.index = pd.to_datetime(data.index)

    # apply the function for checking decrease in time series values - use default threshold (0.0)
    indicator_default_threshold = value_decrease_check(data)

    # apply the function for checking decrease in time series values - change the threshold (4.1)
    indicator_new_threshold = value_decrease_check(data, 4.1)

    # plot the results
    fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(9, 7))
    ax1.plot(data, "-")
    ax2.plot(indicator_default_threshold, "-")
    ax3.plot(indicator_new_threshold, "-")
    ax1.set_title("Time series values")
    ax2.set_title("Indicator with default threshold 0.0")
    ax3.set_title("Indicator with threshold 4.1")
    fig.suptitle("Indicator for decrease in time series values", fontsize=16)
    fig.tight_layout()

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.292 seconds)


.. _sphx_glr_download_auto_examples_data_quality_plot_value_decrease_check.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_value_decrease_check.ipynb <plot_value_decrease_check.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_value_decrease_check.py <plot_value_decrease_check.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_value_decrease_check.zip <plot_value_decrease_check.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
