Base Detector

Base class used for anomaly detection.

class ecgan.anomaly_detection.detector.base_detector.AnomalyDetector(module, tracker)[source]

Bases: ecgan.utils.configurable.Configurable, abc.ABC

A baseclass for various (PyTorch based) anomaly detectors.

This class can be used to implement general anomaly detection algorithms and it is not limited to deep learning/ machine learning approaches. Each AnomalyDetector should be able to

  1. Assert labels to a given time series.

  2. Save/load relevant evaluation data to/from a pkl file. This includes at least the labels but can be expanded for arbitrary information such as anomaly scores or data obtained from reconstructions.

The labeling can depend on the type of detection (e.g. one score per series, channel or point). Thus, no specific format will be enforced. In general, we will save the labels per series and if scores are used to determine if some point is anomalous, we use pointwise scoring whenever possible, since channel-/serieswise anomalies can usually be reconstructed based on that data. The actual performance measures are controlled by the AnomalyManager based on the predicted labels.

__init__(module, tracker)[source]
abstract _detect(data)[source]

Detect anomalies based on the desired detection scheme and return the asserted class labels.

Parameters

data (Tensor) -- Tensor (usually of size [batch_size, seq_len, channels]) of data which shall be classified.

Return type

Tensor

Returns

A Tensor with the label predictions for data.

detect(test_x, test_y)[source]

Detect anomalies based on the desired detection scheme and return the asserted class labels.

Data is expected to be shuffled when passed to the detect method. It is then fed into a DataLoader, chunked into batches on which anomalies are detected.

The function calls the abstract ._detect method and logs wall time.

Parameters
  • test_x (Tensor) -- The shuffled test data.

  • test_y (Tensor) -- The labels corresponding to test_y.

Return type

ndarray

Returns

Predicted labels.

abstract _get_data_to_save()[source]

Select list of objects which shall be saved using the tracker.

Return type

Dict

save(run_id)[source]

Save anomaly detection results to tracker.

Return type

None

abstract load(saved_data)[source]

Load AD data from dict.

The provided dict is usually part of an output of a previous AD run. The load method loads the saved data to the instantiated detector and can subsequently used in to e.g. create embeddings without reprocessing all data. The user is tasked to retrieve the saved dict by themself.

Parameters

saved_data (Dict) -- Previously saved data, loaded into variables of the respective detector.

Return type

None