|
1 | 1 | import numpy as np |
2 | 2 |
|
3 | | -from collections.abc import Callable |
4 | 3 | from sklearn.base import BaseEstimator, TransformerMixin, check_is_fitted |
5 | | -from abc import ABC, abstractmethod |
6 | 4 | from sklearn.utils.validation import validate_data |
7 | | -from ml3_drift.callbacks.models import DriftInfo |
8 | 5 | from ml3_drift.enums.monitoring import DataDimension, DataType |
9 | 6 | from ml3_drift.monitoring.base import MonitoringAlgorithm |
10 | 7 |
|
11 | 8 | from copy import deepcopy |
12 | 9 |
|
13 | 10 |
|
14 | | -class BaseDriftDetector(TransformerMixin, BaseEstimator, ABC): |
15 | | - """ |
16 | | - Base class for drift detector. |
17 | | - Base Drift Detectors are neither transformers nor predictors, they |
18 | | - just observe the data and detects drift, executing specified |
19 | | - actions when necessary. |
20 | | - For this reason, they implement both the transform and predict methods. |
21 | | -
|
22 | | - Parameters |
23 | | - ---------- |
24 | | - callbacks: list[Callable[[DriftInfo], None] |
25 | | - list of callbacks function used to act when drift are detected |
26 | | - """ |
27 | | - |
28 | | - def __init__(self, callbacks: list[Callable[[DriftInfo], None]] | None = None): |
29 | | - super().__init__() |
30 | | - self.callbacks = callbacks |
31 | | - |
32 | | - @abstractmethod |
33 | | - def _fit(self, X, y=None): |
34 | | - """ |
35 | | - Fit method that should be implemented in child classes |
36 | | - """ |
37 | | - |
38 | | - @abstractmethod |
39 | | - def _detect(self, X) -> list[DriftInfo]: |
40 | | - """ |
41 | | - Core method for detecting drift that is implemented in child classes. |
42 | | - """ |
43 | | - |
44 | | - def fit(self, X, y=None): |
45 | | - """ |
46 | | - Fit method. Calls _inner_fit and returns self |
47 | | - """ |
48 | | - |
49 | | - X = self._validate_data(X, y, reset=True) |
50 | | - self._fit(X) |
51 | | - self.is_fitted_ = True |
52 | | - return self |
53 | | - |
54 | | - def transform(self, X): |
55 | | - """ |
56 | | - Transform method. Calls _detect method and return X. |
57 | | - This step does not change the data but only performs drift detection. |
58 | | - """ |
59 | | - X = self._validate_data(X, reset=False) |
60 | | - check_is_fitted(self) |
61 | | - if (drift_info_list := self._detect(X)) and (self.callbacks is not None): |
62 | | - for drift_info in drift_info_list: |
63 | | - for callback in self.callbacks: |
64 | | - callback(drift_info) |
65 | | - return X |
66 | | - |
67 | | - def predict(self, X): |
68 | | - """ |
69 | | - Predict method. Calls _detect method and return X. |
70 | | - This step does not change the data but only performs drift detection. |
71 | | - """ |
72 | | - X = self._validate_data(X, reset=False) |
73 | | - check_is_fitted(self) |
74 | | - if (drift_info_list := self._detect(X)) and (self.callbacks is not None): |
75 | | - for drift_info in drift_info_list: |
76 | | - for callback in self._callbacks: |
77 | | - callback(drift_info) |
78 | | - return X |
79 | | - |
80 | | - def _validate_data(self, X, y=None, reset=False): |
81 | | - """ |
82 | | - Validate data method. This calls validate_data sklearn method with |
83 | | - provided parameters and returns the validated X. |
84 | | - Child classes can override with their own validation methods if needed |
85 | | - or just call the base class method with the custom parameters. |
86 | | - """ |
87 | | - |
88 | | - # Workaround since validate data doesn't return y if it is None |
89 | | - if y is None: |
90 | | - X = validate_data(self, X, reset=reset, accept_sparse=False) |
91 | | - else: |
92 | | - X, _ = validate_data(self, X, y, reset=reset, accept_sparse=False) |
93 | | - return X |
94 | | - |
95 | | - def __sklearn_tags__(self): |
96 | | - tags = super().__sklearn_tags__() |
97 | | - # Currently empty, but can be used to add tags to the estimator |
98 | | - return tags |
99 | | - |
100 | | - |
101 | 11 | class SklearnDriftDetector(TransformerMixin, BaseEstimator): |
102 | 12 | """Adapter class for sklearn library. |
103 | 13 |
|
|
0 commit comments