Anomaly detection
=================
The goal is to detect anomalies on the basis of given difference of quantity values measured in two different
time points. One time point is current time and the second one is selected from data that has been recently measured.

The configuration parameters in the examples below can be changed
in the configuration file ``config.ini``
that is described in section :ref:`installation.config_file`.

CO2
---
Detection of anomalies is based on difference in carbon dioxide concentration values measured in two various time points.


.. literalinclude:: ../../examples2/0305_anomaly_detection/co2_run.py
   :language: python

It is necessary to set clients that can communicate with a remote server using the function ``setup_clients``.
Current time is stored in the variable ``actual_time``. Latitude and longitude that enable to get weather data
according to a sensor position are stored in the variable ``lat`` and ``lon``. The API key allows to get weather data
from server and it is obtained by the function ``wunderground_api_key``. The sensors used to gather data are stored
in the variable ``devs``. Anomaly detection is performed by the function ``anomaly_diff`` and the result is stored
in the variable ``notification``.

An output of the detection is a notification that contains basic information described in the section
:ref:`Notifications <notification.notification>` and the following information:

* **actual_diff** - current difference in carbon dioxide concentration values,
* **anomaly** - if anomaly is detected,
* **min_anomaly_diff** - minimal difference in carbon dioxide concentration values to detect anomaly,
* **min_anomaly_time** - time interval between two time points,
* **type** - what quantity is used for prediction.

The example of a notification follows.

.. literalinclude:: ../../examples2/0305_anomaly_detection/co2_notification.doc.json
   :language: json

The example of a command that can be used to detect anomalies on the basis of carbon dioxide concentration difference.

.. code-block:: bash

   python examples2/0305_anomaly_detection/co2_run.py

Temperature
-----------
Detection of anomalies is based on difference in temperature values measured in two various time points.


.. literalinclude:: ../../examples2/0305_anomaly_detection/t_run.py
   :language: python

It is necessary to set clients that can communicate with a remote server using the function ``setup_clients``.
Current time is stored in the variable ``actual_time``. Latitude and longitude that enable to get weather data
according to a sensor position are stored in the variable ``lat`` and ``lon``. The API key allows to get weather data
from server and it is obtained by the function ``wunderground_api_key``. The sensors used to gather data are stored
in the variable ``devs``. Anomaly detection is performed by the function ``anomaly_diff`` and the result is stored
in the variable ``notification``.

An output of the detection is a notification that contains basic information described in the section
:ref:`Notifications <notification.notification>` and the following information:

* **actual_diff** - current difference in temperature values,
* **anomaly** - if anomaly is detected,
* **min_anomaly_diff** - minimal difference in temperature values to detect anomaly,
* **min_anomaly_time** - time interval between two time points,
* **type** - what quantity is used for prediction.

The example of a notification follows.

.. literalinclude:: ../../examples2/0305_anomaly_detection/t_notification.doc.json
   :language: json

The example of a command that can be used to detect anomalies on the basis of temperature difference.

.. code-block:: bash

   python examples2/0305_anomaly_detection/t_run.py


Humidity
--------
Detection of anomalies is based on difference in humidity values measured in two various time points.


.. literalinclude:: ../../examples2/0305_anomaly_detection/h_run.py
   :language: python

It is necessary to set clients that can communicate with a remote server using the function ``setup_clients``.
Current time is stored in the variable ``actual_time``. Latitude and longitude that enable to get weather data
according to a sensor position are stored in the variable ``lat`` and ``lon``. The API key allows to get weather data
from server and it is obtained by the function ``wunderground_api_key``. The sensors used to gather data are stored
in the variable ``devs``. Anomaly detection is performed by the function ``anomaly_diff`` and the result is stored
in the variable ``notification``.

An output of the detection is a notification that contains basic information described in the section
:ref:`Notifications <notification.notification>` and the following information:

* **actual_diff** - current difference in humidity values,
* **anomaly** - if anomaly is detected,
* **min_anomaly_diff** - minimal difference in humidity values to detect anomaly,
* **min_anomaly_time** - time interval between two time points,
* **type** - what quantity is used for prediction.

The example of a notification follows.

.. literalinclude:: ../../examples2/0305_anomaly_detection/h_notification.doc.json
   :language: json

The example of a command that can be used to detect anomalies on the basis of humidity difference.

.. code-block:: bash

   python examples2/0305_anomaly_detection/h_run.py
