alibi_detect.cd.base_online module

class alibi_detect.cd.base_online.BaseMultiDriftOnline(x_ref, ert, window_size, preprocess_fn=None, x_ref_preprocessed=False, n_bootstraps=1000, verbose=True, input_shape=None, data_type=None)[source]

Bases: BaseDetector, StateMixin

__init__(x_ref, ert, window_size, preprocess_fn=None, x_ref_preprocessed=False, n_bootstraps=1000, verbose=True, input_shape=None, data_type=None)[source]

Base class for multivariate online drift detectors.

Parameters:
  • x_ref (Union[ndarray, list]) – Data used as reference distribution.

  • ert (float) – The expected run-time (ERT) in the absence of drift. For the multivariate detectors, the ERT is defined as the expected run-time from t=0.

  • window_size (int) – The size of the sliding test-window used to compute the test-statistic. Smaller windows focus on responding quickly to severe drift, larger windows focus on ability to detect slight drift.

  • preprocess_fn (Optional[Callable]) – Function to preprocess the data before computing the data drift metrics.

  • x_ref_preprocessed (bool) – Whether the given reference data x_ref has been preprocessed yet. If x_ref_preprocessed=True, only the test data x will be preprocessed at prediction time. If x_ref_preprocessed=False, the reference data will also be preprocessed.

  • n_bootstraps (int) – The number of bootstrap simulations used to configure the thresholds. The larger this is the more accurately the desired ERT will be targeted. Should ideally be at least an order of magnitude larger than the ert.

  • verbose (bool) – Whether or not to print progress during configuration.

  • input_shape (Optional[tuple]) – Shape of input data.

  • data_type (Optional[str]) – Optionally specify the data type (tabular, image or time-series). Added to metadata.

backend: Literal['pytorch', 'tensorflow']
get_threshold(t)[source]

Return the threshold for timestep t.

Parameters:

t (int) – The timestep to return a threshold for.

Return type:

float

Returns:

The threshold at timestep t.

online_state_keys: Tuple[str, ...]
predict(x_t, return_test_stat=True)[source]

Predict whether the most recent window of data has drifted from the reference data.

Parameters:
  • x_t (Union[ndarray, Any]) – A single instance to be added to the test-window.

  • return_test_stat (bool) – Whether to return the test statistic and threshold.

Return type:

Dict[Dict[str, str], Dict[str, Union[int, float]]]

Returns:

Dictionary containing 'meta' and 'data' dictionaries. –

  • 'meta' has the model’s metadata.

  • 'data' contains the drift prediction and optionally the test-statistic and threshold.

reset()[source]

Deprecated reset method. This method will be repurposed or removed in the future. To reset the detector to its initial state (t=0) use reset_state().

Return type:

None

reset_state()[source]

Resets the detector to its initial state (t=0). This does not include reconfiguring thresholds.

Return type:

None

t: int = 0
thresholds: ndarray
class alibi_detect.cd.base_online.BaseUniDriftOnline(x_ref, ert, window_sizes, preprocess_fn=None, x_ref_preprocessed=False, n_bootstraps=1000, n_features=None, verbose=True, input_shape=None, data_type=None)[source]

Bases: BaseDetector, StateMixin

__init__(x_ref, ert, window_sizes, preprocess_fn=None, x_ref_preprocessed=False, n_bootstraps=1000, n_features=None, verbose=True, input_shape=None, data_type=None)[source]

Base class for univariate online drift detectors. If n_features > 1, a multivariate correction is used to aggregate p-values during threshold configuration, thus allowing the requested expected run time (ERT) to be targeted. The multivariate correction assumes independence between the features.

Parameters:
  • x_ref (Union[ndarray, list]) – Data used as reference distribution.

  • ert (float) – The expected run-time (ERT) in the absence of drift. For the univariate detectors, the ERT is defined as the expected run-time after the smallest window is full i.e. the run-time from t=min(windows_sizes)-1.

  • window_sizes (List[int]) – The sizes of the sliding test-windows used to compute the test-statistic. Smaller windows focus on responding quickly to severe drift, larger windows focus on ability to detect slight drift.

  • preprocess_fn (Optional[Callable]) – Function to preprocess the data before computing the data drift metrics.

  • x_ref_preprocessed (bool) – Whether the given reference data x_ref has been preprocessed yet. If x_ref_preprocessed=True, only the test data x will be preprocessed at prediction time. If x_ref_preprocessed=False, the reference data will also be preprocessed.

  • n_bootstraps (int) – The number of bootstrap simulations used to configure the thresholds. The larger this is the more accurately the desired ERT will be targeted. Should ideally be at least an order of magnitude larger than the ert.

  • n_features (Optional[int]) – Number of features used in the statistical test. No need to pass it if no preprocessing takes place. In case of a preprocessing step, this can also be inferred automatically but could be more expensive to compute.

  • verbose (bool) – Whether or not to print progress during configuration.

  • input_shape (Optional[tuple]) – Shape of input data.

  • data_type (Optional[str]) – Optionally specify the data type (tabular, image or time-series). Added to metadata.

get_threshold(t)[source]

Return the threshold for timestep t.

Parameters:

t (int) – The timestep to return a threshold for.

Return type:

ndarray

Returns:

The threshold at timestep t.

online_state_keys: Tuple[str, ...]
predict(x_t, return_test_stat=True)[source]

Predict whether the most recent window(s) of data have drifted from the reference data.

Parameters:
  • x_t (Union[ndarray, Any]) – A single instance to be added to the test-window(s).

  • return_test_stat (bool) – Whether to return the test statistic and threshold.

Return type:

Dict[Dict[str, str], Dict[str, Union[int, float]]]

Returns:

Dictionary containing 'meta' and 'data' dictionaries. –

  • 'meta' has the model’s metadata.

  • 'data' contains the drift prediction and optionally the test-statistic and threshold.

reset()[source]

Deprecated reset method. This method will be repurposed or removed in the future. To reset the detector to its initial state (t=0) use reset_state().

Return type:

None

reset_state()[source]

Resets the detector to its initial state (t=0). This does not include reconfiguring thresholds.

Return type:

None

t: int = 0
thresholds: ndarray