
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/detect/plot_unchanged_signal_detection.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_detect_plot_unchanged_signal_detection.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_detect_plot_unchanged_signal_detection.py:


====================================================
Unchanged signal identification of time series data
====================================================

Example of visualizing unchanged signal during a certain time period in a given time series.

.. GENERATED FROM PYTHON SOURCE LINES 9-67



.. image-sg:: /auto_examples/detect/images/sphx_glr_plot_unchanged_signal_detection_001.png
   :alt: Unchanged signal identification for a time series for a duration of 10 minutes
   :srcset: /auto_examples/detect/images/sphx_glr_plot_unchanged_signal_detection_001.png
   :class: sphx-glr-single-img





.. code-block:: Python

    import random

    from datetime import datetime, timedelta

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd

    from indsl.detect import unchanged_signal_detector


    # Generate time series
    start_date = pd.Timestamp("2022/01/01 10:00:00")
    end_date = pd.Timestamp("2022/01/01 11:59:00")

    ts_values = np.random.uniform(1, 80, size=120)
    data = pd.Series(ts_values, index=pd.date_range(start_date, end_date, periods=120), name="value")

    data = data.sort_index()

    duration = pd.Timedelta(minutes=10)

    # Assign a random value to a set of consecutive time series indices
    random_signal_value = np.random.uniform(1, 80)
    nr_consecutive_data_points = 12

    start_position = random.randint(0, int(len(data) - nr_consecutive_data_points))
    data.values[start_position : start_position + nr_consecutive_data_points] = random_signal_value

    unchanged_signal_step_series = unchanged_signal_detector(data, duration=duration, min_nr_data_points=3)

    # Resample and forward fill generated step series
    resampled_step_series = unchanged_signal_step_series.resample("1min")
    unchanged_signal_forward_filled = resampled_step_series.ffill()

    # Plot unchanged signal identification series against actual data
    fig, ax1 = plt.subplots(figsize=(15, 5))
    ax1.plot(data.index, data, label="Time series", marker=".", color="blue")

    values = np.arange(data.index[0], data.index[-1], timedelta(minutes=10)).astype(datetime)

    ax1.set_xticks(values)
    ax1.set_xticklabels([ts.strftime("%d-%m-%Y \n %H:%M:%S") for ts in values], fontsize=8)

    ax2 = ax1.twinx()
    ax2.plot(data.index, unchanged_signal_forward_filled, label="Unchanged signal indicator", marker=".", color="red")

    ax1.legend(loc="upper left")
    ax2.legend(loc="upper right")

    plt.xlabel("Timestamp")
    ax1.set_ylabel("Value")
    ax2.set_ylabel("Unchanged signal")

    fig.suptitle("Unchanged signal identification for a time series for a duration of 10 minutes", fontsize=14)
    fig.tight_layout()

    plt.show()


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

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


.. _sphx_glr_download_auto_examples_detect_plot_unchanged_signal_detection.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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