|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# pyre-strict |
| 9 | + |
| 10 | + |
| 11 | +import logging |
| 12 | +from abc import ABC, abstractmethod |
| 13 | + |
| 14 | +_logger: logging.Logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class MetricAnomalyEvaluator(ABC): |
| 18 | + """ |
| 19 | + Abstract base class for metric anomaly evaluators. An evaluator specifies the logic to determine that |
| 20 | + a particular metric value is anomalous. To implement a custom method, create a subclass and implement |
| 21 | + the following methods: |
| 22 | + - :py:meth:`~torchtnt.utils.loggers.metric_anomaly_logger.MetricAnomalyEvaluator.update` should receive |
| 23 | + the metric value and update the internal state. This is specially useful for algorithms that require |
| 24 | + storing some previous values, moving averages, etc. |
| 25 | + - :py:meth:`~torchtnt.utils.loggers.metric_anomaly_logger.MetricAnomalyEvaluator.is_anomaly` determines |
| 26 | + whether the current metric state is anomalous. |
| 27 | +
|
| 28 | + Likely there are some warm-up steps before the metric is stable and can be checked against anomalies, so |
| 29 | + the separation of state update and actual detection provides this flexibility. |
| 30 | + """ |
| 31 | + |
| 32 | + @abstractmethod |
| 33 | + def update(self, value: float) -> None: |
| 34 | + """ |
| 35 | + Update the internal state with the given metric value. This should not determine anomalies itself, but |
| 36 | + only aggregate the current value according to the anomaly detection algorithm. |
| 37 | +
|
| 38 | + Note:: If no aggregation is required, this method can store the value directly, to be used in `is_anomaly`. |
| 39 | +
|
| 40 | + Args: |
| 41 | + value: Metric value |
| 42 | + """ |
| 43 | + pass |
| 44 | + |
| 45 | + @abstractmethod |
| 46 | + def is_anomaly(self) -> bool: |
| 47 | + """ |
| 48 | + Determine whether the current metric state is anomalous. This should be overridden with custom logic related to |
| 49 | + an anomaly detection algorithm. |
| 50 | + """ |
| 51 | + pass |
0 commit comments