diff --git a/.github/workflows/sync-to-gitlab.yml b/.github/workflows/sync-to-gitlab.yml deleted file mode 100644 index 9264dd68..00000000 --- a/.github/workflows/sync-to-gitlab.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Sync Repository to GitLab - -on: - push: - branches: - - main - - develop - -jobs: - sync: - runs-on: ubuntu-latest - name: Git Repo Sync - steps: - - uses: actions/checkout@v6 - with: - fetch-depth: 0 - - uses: wangchucheng/git-repo-sync@v0.1.0 - with: - target-url: ${{ secrets.GITLAB_URL }} - target-username: ${{ secrets.USER_NAME }} - target-token: ${{ secrets.GITLAB_PAT }} \ No newline at end of file diff --git a/docs/anomaly_scores.rst b/docs/anomaly_scores.rst new file mode 100644 index 00000000..9624590e --- /dev/null +++ b/docs/anomaly_scores.rst @@ -0,0 +1,194 @@ +.. _anomaly_scores: + +Anomaly scores +============== + +The reconstruction errors of an autoencoder are per-sample *and* per-feature: for every input sample there is a +vector of residuals (prediction minus actual value). An :class:`~energy_fault_detector.core.anomaly_score.AnomalyScore` +reduces that vector to a **single scalar score per sample**. Samples with high scores deviate strongly from the learned +normal behaviour and are therefore candidate anomalies. + +The chosen score is configured under ``train.anomaly_score`` in the YAML config. This page explains the available +scores, how they differ and when to use which, and recommends: +:class:`RMSEScore ` as the default. + +.. note:: + An anomaly score only ranks samples; it does **not** classify them. The actual *anomaly decision* + (normal vs. anomalous) is taken afterwards by a :class:`~energy_fault_detector.core.threshold_selector.ThresholdSelector`. + +Available scores +---------------- + +.. list-table:: + :header-rows: 1 + :widths: 35 25 40 + + * - Class + - Typical config names (``train.anomaly_score.name``) + - Description + * - :class:`RMSEScore ` + - ``"rmse"``, ``"RMSE"`` + - Root-mean-square reconstruction error over all features. **Recommended default.** + * - :class:`WeightedRMSEScore ` + - ``"weighted_rmse"``, ``"WeightedRMSE"`` + - RMSE where selected features can be up- or down-weighted. + * - :class:`MahalanobisScore ` + - ``"mahalanobis"``, ``"Mahalanobis"`` + - Correlation-aware Mahalanobis distance. + +RMSE (recommended default) +-------------------------- + +The RMSE score standardises the reconstruction errors (using the mean and standard deviation learned on the training +data) and then computes the per-sample root mean square across all features: + +.. math:: + + s_i = \sqrt{\frac{1}{p} \sum_{j=1}^{p} z_{ij}^{\,2}}, + \qquad z_{ij} = \frac{x_{ij} - \mu_j}{\sigma_j} + +where :math:`x_{ij}` is the reconstruction error of sample :math:`i` on feature :math:`j`, and :math:`\mu_j`, +:math:`\sigma_j` the corresponding training mean and standard deviation. + +.. list-table:: + :widths: 50 50 + :header-rows: 1 + + * - Advantages + - Limitations + * - Simple, fast and numerically robust. + - Treats all features as independent (does not account for correlation). + * - Does not require estimating a covariance and is stable for **any** number of samples. + - A feature shown to be more fault-relevant is not emphasised automatically. + +.. code-block:: yaml + + train: + anomaly_score: + name: rmse # RMSE is the recommended default + params: {} + +Use RMSE as a robust, well-understood default that works well out of the box for most use cases. + +Weighted RMSE +------------- + +:class:`WeightedRMSEScore ` extends RMSE by +allowing **per-feature weights**, so that deviations on sensor/feature values you care most about contribute more to the +final score. The reconstruction errors are first standardised (to remove model bias towards high-magnitude features) and +then multiplied by the feature weight before taking the per-sample root mean square. + +Choosing a weight: + +- ``weight > 1`` increases the influence of a feature (deviations on it are treated as more suspicious), +- ``0 <= weight < 1`` decreases its influence, +- a weight of ``0`` ignores the feature, +- features are *not* required to have an entry; omitted features keep weight ``1``. + +.. code-block:: yaml + + train: + anomaly_score: + name: weighted_rmse + params: + feature_weights: + rotor_speed: 2.0 # emphasise this sensor + temperature_11: 0.5 # de-emphasise this sensor + # any other feature keeps weight 1.0 + +.. note:: + ``WeightedRMSEScore`` requires the input to be a :class:`~pandas.DataFrame` so that the weights can be matched to + the feature names. Negative weights are not allowed. + +Use weighted RMSE when you have **domain knowledge** about which features are most relevant for the faults of interest +and want that prior to influence the anomaly score directly. + +Mahalanobis +----------- + +:class:`MahalanobisScore ` computes the +**Mahalanobis distance**, which unlike RMSE accounts for the *correlation* between features. Instead of summing +independent standardised errors, it measures the distance in units of the covariance of the (mean-centred) errors: + +.. math:: + + s_i = \sqrt{(x_i - \mu)^{\top} \Sigma^{-1} (x_i - \mu)} + +The covariance estimator is chosen via the ``covariance_method`` parameter: + +.. list-table:: + :header-rows: 1 + :widths: 25 75 + + * - ``covariance_method`` + - Behaviour + * - ``"auto"`` (default) + - Use MinCovDet when it is stable, otherwise fall back to a shrinkage estimator (see below). + * - ``"min_cov_det"`` + - Always use Minimum Covariance Determinant (robust, but requires enough samples). + * - ``"shrinkage"`` + - Always use a shrinkage estimator (Ledoit-Wolf / OAS). Robust for high-dimensional data. + +.. list-table:: + :widths: 50 50 + :header-rows: 1 + + * - Advantages + - Limitations + * - Accounts for correlation between features; can separate faults that RMSE cannot. + - More computationally expensive and more complex than RMSE. + * - Robust (MinCovDet) or high-dimensional-friendly (shrinkage) variants available. + - Sensitive to covariance estimation quality. + +Use the Mahalanobis score when features are strongly correlated and you need a **correlation-aware** score, but reserve +it for cases where the added complexity is justified. + +The derived stability threshold +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +MinCovDet needs enough samples per feature: as the sample-to-feature ratio ``n / p`` drops it quickly becomes unstable +(its covariance becomes rank-deficient and ill-conditioned). Concretely: + +- for ``n / p >= min_cov_det_ratio`` (default ``2.0``) the covariance is well conditioned and MinCovDet is usable; +- below that, or when the covariance is singular / ill-conditioned, a **shrinkage** estimator (Ledoit-Wolf / OAS) + should be used instead. + +With ``covariance_method="auto"`` this threshold is applied automatically and the estimator falls back to shrinkage +(Ledoit-Wolf / OAS) whenever MinCovDet would be unstable. + +.. code-block:: yaml + + train: + anomaly_score: + name: mahalanobis + params: + covariance_method: auto # "auto" | "min_cov_det" | "shrinkage" + min_cov_det_ratio: 2.0 # minimum n/p for MinCovDet when "auto" + shrinkage_method: oas # "oas" | "ledoit_wolf" + +.. deprecated:: + The legacy ``pca=True`` path of :class:`MahalanobisScore` is deprecated and will be replaced by the clean Mahalanobis + norm implementation (``pca=False``). Likewise the ``scale`` parameter is deprecated and no longer has any effect. + See :doc:`advanced_usage` for details. + +Which score should I use? +------------------------- + +.. list-table:: + :header-rows: 1 + :widths: 45 55 + + * - Your situation + - Recommended score + * - You want a simple, robust default that works out of the box. + - **RMSE** (``name: rmse``) + * - You have domain knowledge about which features matter most for the fault. + - Weighted RMSE (``name: weighted_rmse``) + * - Features are strongly correlated and a correlation-aware score is important. + - Mahalanobis (``name: mahalanobis``, ``covariance_method: auto``) + * - High-dimensional input where a stable covariance is hard to estimate. + - Mahalanobis with ``covariance_method: shrinkage`` (or RMSE) + +For most projects the RMSE score is a safe, interpretable and efficient default: it needs no covariance estimation, +is stable regardless of the number of samples and features, and pairs well with a good threshold selector. Start with +RMSE and move to a more specialised score only when a concrete need arises. diff --git a/docs/examples/advanced_config.yaml b/docs/examples/advanced_config.yaml index b79496f1..3ec6181c 100644 --- a/docs/examples/advanced_config.yaml +++ b/docs/examples/advanced_config.yaml @@ -109,8 +109,6 @@ train: anomaly_score: name: rmse - params: - scale: false threshold_selector: name: fbeta diff --git a/docs/index.rst b/docs/index.rst index fc1cd0a2..de100a46 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -39,6 +39,7 @@ Installation care2compare_faq models_overview sequence_models + anomaly_scores .. toctree:: :caption: Advanced usage diff --git a/energy_fault_detector/__init__.py b/energy_fault_detector/__init__.py index 41c62847..aa6db98e 100644 --- a/energy_fault_detector/__init__.py +++ b/energy_fault_detector/__init__.py @@ -52,6 +52,9 @@ register(module_path='energy_fault_detector.anomaly_scores.rmse_score.RMSEScore', class_type='anomaly_score', class_names=['RMSEScore', 'RMSE', 'rmse']) +register(module_path='energy_fault_detector.anomaly_scores.weighted_rmse_score.WeightedRMSEScore', + class_type='anomaly_score', + class_names=['WeightedRMSEScore', 'WeightedRMSE', 'weighted_rmse', 'Weighted_RMSE']) # threshold selectors register(module_path='energy_fault_detector.threshold_selectors.fdr_threshold.FDRSelector', diff --git a/energy_fault_detector/anomaly_scores/mahalanobis_score.py b/energy_fault_detector/anomaly_scores/mahalanobis_score.py index 329b9b65..410462e6 100644 --- a/energy_fault_detector/anomaly_scores/mahalanobis_score.py +++ b/energy_fault_detector/anomaly_scores/mahalanobis_score.py @@ -1,27 +1,68 @@ +import warnings from typing import Optional, Union import numpy as np import pandas as pd +from sklearn.covariance import LedoitWolf, MinCovDet, OAS from sklearn.decomposition import PCA -from sklearn.covariance import MinCovDet from sklearn.utils.validation import check_is_fitted from energy_fault_detector.core.anomaly_score import AnomalyScore DataType = Union[pd.DataFrame, np.ndarray] +# Sentinel used to detect whether the deprecated `scale` parameter was passed explicitly. +_DEPRECATED = object() + +# Supported covariance estimation methods for the full dimensional covariance estimation (pca=False) path. +_COVARIANCE_METHODS = ("auto", "min_cov_det", "shrinkage") +_SHRINKAGE_METHODS = ("oas", "ledoit_wolf") + class MahalanobisScore(AnomalyScore): - """Calculate mahalanobis scores using sklearn MinCovDet und optionally a PCA to accelerate calculations. + """Calculate Mahalanobis scores on the reconstruction errors. + + This class implements the mahalanobis norm calculation based on multiple variants of covariance + matrix estimation. The cases are defined by the user-set parameter ``pca`` and the relation of + the number of samples ``n`` and the number of features ``p``: + 1. (``pca=False``) and n>=min_cov_det_ratio * p: Estimates the covariance of the + mean-centred reconstruction errors using the MinCovDet algorithm and returns the + Mahalanobis distance of each sample. + 2. (``pca=False``) and n= min_cov_det_ratio`` and the + resulting covariance is stable (full rank / well conditioned), otherwise fall back + to a shrinkage estimator (``shrinkage_method``). + - ``"min_cov_det"``: always use MinCovDet. May raise or be unstable when ``n`` is + not sufficiently larger than ``p``. + - ``"shrinkage"``: always use a shrinkage estimator (``shrinkage_method``). + min_cov_det_ratio: minimum sample-to-feature ratio ``n / p`` required (in ``"auto"`` mode) + for MinCovDet to be used instead of shrinkage. Default ``2.0``. + shrinkage_method: which shrinkage estimator to use. One of ``"oas"`` (default) or + ``"ledoit_wolf"``. Configuration example: @@ -31,58 +72,153 @@ class MahalanobisScore(AnomalyScore): anomaly_score: name: mahalanobis params: - pca: True - pca_min_var: 0.9 - mcd_support_fraction: 0.9 - scale: False + pca: False + covariance_method: auto + min_cov_det_ratio: 2.0 + shrinkage_method: oas """ - def __init__(self, pca: bool = True, pca_min_var: float = 0.9, mcd_support_fraction: float = 0.9, - scale: bool = False): + def __init__(self, pca: bool = _DEPRECATED, pca_min_var: float = 0.9, mcd_support_fraction: float = 0.9, + scale: bool = _DEPRECATED, covariance_method: str = "auto", + min_cov_det_ratio: float = 2.0, shrinkage_method: str = "oas"): super().__init__() self.pca: bool = pca self.pca_min_var = pca_min_var self.mcd_support_fraction = mcd_support_fraction - self.scale = scale + self.covariance_method = covariance_method + self.min_cov_det_ratio = min_cov_det_ratio + self.shrinkage_method = shrinkage_method + + if pca is _DEPRECATED: + self.pca=True + else: + self.pca=pca + if self.pca: + warnings.warn( + "The PCA-based MahalanobisScore is deprecated and will be replaced by the full-dimensionality Mahalanobis " + "norm implementation. Use pca=False for the direct full-dimensionality Mahalanobis norm.", + DeprecationWarning, + stacklevel=2, + ) + if scale is not _DEPRECATED: + warnings.warn( + "The 'scale' parameter of MahalanobisScore is deprecated and no longer has any effect. " + "It will be removed in a future version.", + DeprecationWarning, + stacklevel=2, + ) + # `scale` is kept as an attribute for backwards compatibility but no longer influences the behaviour. + self.scale = True + + if covariance_method not in _COVARIANCE_METHODS: + raise ValueError( + f"Invalid covariance_method '{covariance_method}'. " + f"Supported values are: {', '.join(_COVARIANCE_METHODS)}." + ) + if shrinkage_method not in _SHRINKAGE_METHODS: + raise ValueError( + f"Invalid shrinkage_method '{shrinkage_method}'. " + f"Supported values are: {', '.join(_SHRINKAGE_METHODS)}." + ) # fitted attributes need trailing underscore self.pca_object: PCA = PCA(n_components=self.pca_min_var) - self.min_cov_det_object: MinCovDet = MinCovDet(support_fraction=self.mcd_support_fraction, assume_centered=True) + self.min_cov_det_object: MinCovDet = MinCovDet(support_fraction=self.mcd_support_fraction, + assume_centered=True) # pylint: disable=attribute-defined-outside-init # noinspection PyAttributeOutsideInit def fit(self, x: DataType, y: Optional[pd.Series] = None) -> 'MahalanobisScore': - """Fit MinCovDet object to determine Mahalanobis distance. + """Fit the covariance estimator to determine Mahalanobis distance. Args: x: numpy 2d array or pandas DataFrame with differences between prediction and actual sensor values. y (optional): not used, labels indicating whether sample is normal (True) or anomalous (False). """ self.mean_x_: np.array = np.mean(x, axis=0) - if self.scale: - self.std_x_: np.array = np.std(x, axis=0) - # standardization of the reconstruction error in X - if np.all(self.std_x_ > 0): - scaled_x = (x - self.mean_x_) / self.std_x_ - else: - scaled_x = x - self.mean_x_ - # replace possible inf values with 0 - scaled_x[np.isinf(scaled_x)] = 0 - else: - scaled_x = x - self.mean_x_ - # Covariance estimation if self.pca: + # Deprecated PCA-based path: standardize and reduce dimensionality before covariance estimation. + self.std_x_: np.array = np.std(x, axis=0) + scaled_x = self.standardize(x) self.pca_object.fit(scaled_x) pca_result = self.pca_object.transform(scaled_x) + self.min_cov_det_object.fit(pca_result) + self.cov_estimator_ = self.min_cov_det_object + self.covariance_method_ = "min_cov_det" else: - pca_result = scaled_x - - self.min_cov_det_object.fit(pca_result) + # Full Mahalanobis norm: full-dimensionality mean-centred errors. + centered = x - self.mean_x_ + self.cov_estimator_, self.covariance_method_ = self._fit_covariance(centered) return self + def _fit_covariance(self, x_centered: np.ndarray): + """Fit the covariance estimator on the mean-centred errors and return (estimator, method). + + In ``auto`` mode MinCovDet is used when the sample-to-feature ratio is sufficient and the + fitted covariance is stable, otherwise a shrinkage estimator is used. + """ + n, p = x_centered.shape + + method = self.covariance_method + if method == "auto": + if n >= self.min_cov_det_ratio * p: + method = "min_cov_det" + else: + method = "shrinkage" + + if method == "min_cov_det": + try: + self.min_cov_det_object.fit(x_centered) + except (ValueError, np.linalg.LinAlgError): + if self.covariance_method == "auto": + method = "shrinkage" + else: + raise + else: + # Even with a sufficient n/p ratio, correlated features can produce a + # singular / ill-conditioned covariance in auto mode -> fall back to shrinkage. + if self.covariance_method == "auto" and self._is_degenerate_np(x_centered.shape): + method = "shrinkage" + else: + return self.min_cov_det_object, method + + # shrinkage path + estimator_cls = OAS if self.shrinkage_method == "oas" else LedoitWolf + estimator = estimator_cls(assume_centered=True) + estimator.fit(x_centered) + return estimator, method + + def _is_degenerate_np(self, shape) -> bool: + """Return True if the fitted MinCovDet covariance is singular or ill-conditioned.""" + cov = getattr(self.min_cov_det_object, "covariance_", None) + p = shape[1] + if cov is None: + return True + try: + rank = np.linalg.matrix_rank(cov) + cond = np.linalg.cond(cov) + except np.linalg.LinAlgError: + return True + return rank < p or not np.isfinite(cond) or cond > self._MAX_CONDITION + + _MAX_CONDITION = 1e8 + + def standardize(self, x: DataType): + """Standardization of the reconstruction error in x (only used in the deprecated PCA path).""" + + check_is_fitted(self) + x_ = x.copy() + if np.all(self.std_x_ > 0): + x_ = (x - self.mean_x_) / self.std_x_ + else: + x_ = x - self.mean_x_ + # replace possible inf values with 0 + x_[np.isinf(x_)] = 0 + return x_ + def transform(self, x: DataType) -> pd.Series: """Calculate Mahalanobis distance from x. @@ -93,24 +229,19 @@ def transform(self, x: DataType) -> pd.Series: Mahalanobis distance for each sample. Output is a pandas Series if input was a pandas DataFrame """ check_is_fitted(self) - check_is_fitted(self.min_cov_det_object) - if self.scale: - # standardization of the reconstruction error in X - if np.all(self.std_x_ > 0): - scaled_x = (x - self.mean_x_) / self.std_x_ - else: - scaled_x = x - self.mean_x_ - # replace possible inf values with 0 - scaled_x[np.isinf(scaled_x)] = 0 - else: - scaled_x = x - self.mean_x_ - if self.pca: + check_is_fitted(self.min_cov_det_object) + scaled_x = self.standardize(x) pca_result = self.pca_object.transform(scaled_x) + # Define fallback option in case MahalanobisScore is loaded from an older version where cov_estimator_ was not present + cov_estimator = getattr(self, 'cov_estimator_', self.min_cov_det_object) + scores = cov_estimator.mahalanobis(pca_result) else: - pca_result = scaled_x + centered = x - self.mean_x_ + # Define fallback option in case MahalanobisScore is loaded from an older version where cov_estimator_ was not present + cov_estimator = getattr(self, 'cov_estimator_', self.min_cov_det_object) + scores = cov_estimator.mahalanobis(centered) - scores = self.min_cov_det_object.mahalanobis(pca_result) if isinstance(x, (pd.DataFrame, pd.Series)): scores = pd.Series(scores, index=x.index) diff --git a/energy_fault_detector/anomaly_scores/rmse_score.py b/energy_fault_detector/anomaly_scores/rmse_score.py index ea3f6850..9cd7d0a1 100644 --- a/energy_fault_detector/anomaly_scores/rmse_score.py +++ b/energy_fault_detector/anomaly_scores/rmse_score.py @@ -1,21 +1,28 @@ +import warnings from typing import Optional, Union +import logging import numpy as np import pandas as pd from sklearn.utils.validation import check_is_fitted from energy_fault_detector.core.anomaly_score import AnomalyScore +logger = logging.getLogger('energy_fault_detector') + DataType = Union[pd.DataFrame, np.ndarray] +# Sentinel used to detect whether the deprecated `scale` parameter was passed explicitly. +_DEPRECATED = object() + class RMSEScore(AnomalyScore): """Calculate the RMSE of given reconstruction errors. - Attributes: - scale: If True, mean and std of the training/fit reconstruction errors will be used to standardize recon errors - during transform. Default: True + .. deprecated:: + The ``scale`` parameter no longer has any effect and is only accepted for backwards compatibility. + It will be removed in a future version. Configuration example: @@ -24,15 +31,21 @@ class RMSEScore(AnomalyScore): train: anomaly_score: name: rmse - params: - scale: false """ - def __init__(self, scale: bool = True, **kwargs): + def __init__(self, scale: bool = _DEPRECATED, **kwargs): super().__init__(**kwargs) - self.scale = scale + if scale is not _DEPRECATED: + warnings.warn( + "The 'scale' parameter of RMSEScore is deprecated and no longer has any effect. " + "It will be removed in a future version.", + DeprecationWarning, + stacklevel=2, + ) + # `scale` is kept as an attribute for backwards compatibility but no longer influences the behaviour. + self.scale = True # pylint: disable=attribute-defined-outside-init # noinspection PyAttributeOutsideInit @@ -43,18 +56,31 @@ def fit(self, x: DataType, y: Optional[pd.Series] = None) -> 'RMSEScore': x: numpy 2d array with differences between prediction and actual sensor values y (optional): not used, labels indicating whether sample is normal (True) or anomalous (False). """ - if not hasattr(self, 'scale'): - # backwards compatibility, add missing attribute and set to True (as that was the standard behaviour) - self.scale = True - - if self.scale: - # fitted attributes need trailing underscore - and are not initialized - self.std_x_: np.array = np.std(x, axis=0) - self.mean_x_: np.array = np.mean(x, axis=0) + # fitted attributes need trailing underscore - and are not initialized + self.std_x_: np.array = np.std(x, axis=0) + self.mean_x_: np.array = np.mean(x, axis=0) self.fitted_ = True # nothing to fit return self + def standardize(self, x: DataType): + """Standardization of the reconstruction error in x""" + + check_is_fitted(self) + x_ = x.copy() + std_mask = self.std_x_ > 0 + if np.all(std_mask): + x_ = (x - self.mean_x_) / self.std_x_ + else: + logger.warning(f'The reconstruction error of the following features has a standard deviation of 0.0 and will not be scaled: {x_.columns[~std_mask]}.' \ + f'Please check these features whether they are useful for anomaly detection or if they should be excluded.') + x_ = x.copy() + scaleable_features = x_.columns[std_mask] + x_[scaleable_features] = x_[scaleable_features] / self.std_x_[std_mask] + # replace possible inf values with 0 + x_[np.isinf(x_)] = 0 + return x_ + def transform(self, x: DataType) -> pd.Series: """Calculate the RMSE based on the deviation matrix. @@ -64,21 +90,7 @@ def transform(self, x: DataType) -> pd.Series: Returns: RMSE for each sample. """ - if not hasattr(self, 'scale'): - # backwards compatibility, add missing attribute and set to True (as that was the standard behaviour) - self.scale = True - - check_is_fitted(self) - - x_ = x - if self.scale: - # standardization of the reconstruction error in X - if np.all(self.std_x_ > 0): - x_ = (x - self.mean_x_) / self.std_x_ - else: - x_ = x - self.mean_x_ - # replace possible inf values with 0 - x_[np.isinf(x_)] = 0 + x_ = self.standardize(x) scores = np.sqrt(np.mean(x_ ** 2, axis=1)) if isinstance(x, (pd.DataFrame, pd.Series)): diff --git a/energy_fault_detector/anomaly_scores/weighted_rmse_score.py b/energy_fault_detector/anomaly_scores/weighted_rmse_score.py new file mode 100644 index 00000000..20a924bc --- /dev/null +++ b/energy_fault_detector/anomaly_scores/weighted_rmse_score.py @@ -0,0 +1,117 @@ + +from typing import Optional, Dict, List + +import logging +import numpy as np +import pandas as pd +from sklearn.utils.validation import check_is_fitted + +from energy_fault_detector.anomaly_scores.rmse_score import RMSEScore + +logger = logging.getLogger('energy_fault_detector') + + +class WeightedRMSEScore(RMSEScore): + """Calculate the RMSE of given reconstruction errors manipulated with specified weights. + + Attributes: + feature_weights (Dict[str, float]): Weight definition for input features. + Weights can be chosen from [0, infinity). Defaults to None. + + Configuration example: + + .. code-block:: yaml + + train: + anomaly_score: + name: weighted_rmse + params: + feature_weights: + feature_1: 2.0 + feature_2: 0.5 + feature_3: 1.0 # If a feature should not be weighted it can also be omitted. + + """ + + def __init__(self, feature_weights: Optional[Dict[str, float]] = None, **kwargs): + super().__init__(**kwargs) + self.feature_weights = {} if feature_weights is None else feature_weights + if self.feature_weights: + if np.min(list(self.feature_weights.values())) < 0: + raise ValueError('WeightedRMSEScore does not accept negative feature weights. ' \ + 'If you want to decrease the importance ' + 'of a feature in the AnomalyScore, choose a weight x with 0<= x < 1.') + + def scale_with_std(self, x: pd.DataFrame) -> pd.DataFrame: + """Scale the input data with the fitted standard deviation of the training data.""" + std_mask = self.std_x_ > 0 + if np.all(std_mask): + x_ = x / self.std_x_ + else: + scaleable_features = x.columns[std_mask] + logger.warning(f'The reconstruction error of the following features has a standard deviation of 0.0 and will not be scaled: {x.columns[~std_mask]}.' \ + f'Please check these features whether they are useful for anomaly detection or if they should be excluded.') + x_ = x.copy() + x_[scaleable_features] = x_[scaleable_features] / self.std_x_[std_mask] + x_[np.isinf(x_)] = 0 + return x_ + + def apply_weights_to_squared_residuals(self, x: pd.DataFrame) -> pd.DataFrame: + """ Applies specified weights to squared std-scaled x, if x is a pandas DataFrame and the features acutally occur in x's columns. + + x (pd.DataFrame): DataFrame of reconstruction errors. + Returns: + pd.DataFrame weighted version of x. + """ + # std-scale reconstruction errors to remove potential model bias towards specific features + x_squared = pd.DataFrame(data=self.scale_with_std(x), columns=x.columns, index=x.index) ** 2 + + # Weight std-scaled reconstruction errors to introduce useful application context bias + for feature in self.feature_weights: + if feature in x.columns: + x_squared[feature] = x_squared[feature] * self.feature_weights[feature] + else: + logger.warning(f'Specified feature {feature} is not part of the list of input ' + f'features. Thus it can not be weighted. Input features: {x.columns}.') + return x_squared + + def fit(self, x: pd.DataFrame, y: Optional[pd.Series] = None) -> 'WeightedRMSEScore': + """Calculate standard deviation and mean on weighted training data + + Args: + x (pd.DataFrame): DataFrame with differences between prediction and actual sensor values + y (Optional[pd.Series]): not used, labels indicating whether sample is normal (True) or anomalous (False). + variable_name_features (Dict[str, List[str]]): Mapping of input feature names that change during preprocessing. + Example: Angle features might be transformed into [feature_name_sin, feature_name_cos. Defaults to an empty dictionary. + """ + if not isinstance(x, pd.DataFrame): + raise ValueError('WeightedRMSEScore requires a DataFrame as input to correctly ' \ + 'apply specified weights.') + + super().fit(x, y) + self.fitted_ = True + return self + + + def transform(self, x: pd.DataFrame): + """Calculate the RMSE based on the weighted deviation matrix. + + Args: + x (pd.DataFrame): Dataframe with differences between prediction and actual sensor values + + Returns: + weighted RMSE for each sample. + """ + if not isinstance(x, pd.DataFrame): + raise ValueError('WeightedRMSEScore requires a DataFrame as input to correctly ' \ + 'apply specified weights.') + + x_squared_weighted = self.apply_weights_to_squared_residuals(x) + + applicable_weights = [self.feature_weights[feature] for feature in self.feature_weights if feature in x.columns] + total_feature_weights = np.sum(applicable_weights) + total_feature_weights += x.shape[1] - len(applicable_weights) # add omitted features with weight 1.0 + scores = np.sqrt(np.sum(x_squared_weighted, axis=1) / total_feature_weights) + if isinstance(x, (pd.DataFrame, pd.Series)): + scores = pd.Series(scores, index=x.index) + return scores diff --git a/tests/anomaly_scores/test_mahalanobis_score.py b/tests/anomaly_scores/test_mahalanobis_score.py index 0761d546..54616e62 100644 --- a/tests/anomaly_scores/test_mahalanobis_score.py +++ b/tests/anomaly_scores/test_mahalanobis_score.py @@ -1,9 +1,11 @@ +import warnings from unittest import TestCase import numpy as np import pandas as pd from numpy.testing import assert_array_equal, assert_array_almost_equal +from sklearn.covariance import LedoitWolf, OAS from sklearn.utils.validation import check_is_fitted from energy_fault_detector.anomaly_scores.mahalanobis_score import MahalanobisScore @@ -11,10 +13,8 @@ class TestMahalanobisScore(TestCase): def setUp(self) -> None: - self.mahalanobis_score = MahalanobisScore(pca=True, scale=False) - self.mahalanobis_score_no_pca = MahalanobisScore(pca=False, scale=False) - self.mahalanobis_score_no_pca_scale = MahalanobisScore(pca=False, scale=True) - self.mahalanobis_score_pca_scale = MahalanobisScore(pca=True, scale=True) + self.mahalanobis_score = MahalanobisScore(pca=True) + self.mahalanobis_score_no_pca = MahalanobisScore(pca=False) self.train_data = pd.DataFrame([ [1, 2, 3, 6], [4, 5, 6, 30], @@ -32,31 +32,149 @@ def test_fit(self) -> None: self.mahalanobis_score.fit(self.train_data) assert_array_equal(np.array([5.5, 6.5, 7.5, 60.]), self.mahalanobis_score.mean_x_) + assert_array_almost_equal(np.array([3.35410197, 3.35410197, 3.35410197, 47.812132]), + self.mahalanobis_score.std_x_) self.assertIsNone(check_is_fitted(self.mahalanobis_score.pca_object)) self.assertIsNone(check_is_fitted(self.mahalanobis_score.min_cov_det_object)) def test_transform(self) -> None: self.mahalanobis_score.fit(self.train_data) score = self.mahalanobis_score.transform(self.test_data) - assert_array_almost_equal(np.array([1.31064256, 0.68390328, 0.68390328]), score) + assert_array_almost_equal(np.array([0.720376, 0.102951, 0.102951]), score) def test_transform_no_pca(self) -> None: + # Explicit MinCovDet on the mean-centred full-dimensionality errors. + self.mahalanobis_score_no_pca.covariance_method = "min_cov_det" self.mahalanobis_score_no_pca.fit(self.train_data) score = self.mahalanobis_score_no_pca.transform(self.test_data) assert_array_almost_equal(np.array([5.49382716, 13.46666667, 13.46666667]), score) - def test_transform_pca_scale(self) -> None: - self.mahalanobis_score_pca_scale.fit(self.train_data) - score = self.mahalanobis_score_pca_scale.transform(self.test_data) - assert_array_almost_equal(np.array([0.720376, 0.102951, 0.102951]), score) - - def test_transform_no_pca_scale(self) -> None: - self.mahalanobis_score_no_pca_scale.fit(self.train_data) - score = self.mahalanobis_score_no_pca_scale.transform(self.test_data) - assert_array_almost_equal(np.array([5.49382716, 13.46666667, 13.46666667]), score) + def test_transform_no_pca_auto_falls_back_to_shrinkage(self) -> None: + # train_data has n/p = 1 (4 samples, 4 features) which is below the MinCovDet + # stability threshold, so the clean path falls back to a shrinkage estimator. + self.mahalanobis_score_no_pca.fit(self.train_data) + self.assertEqual(self.mahalanobis_score_no_pca.covariance_method_, "shrinkage") + self.assertIsInstance(self.mahalanobis_score_no_pca.cov_estimator_, OAS) + score = self.mahalanobis_score_no_pca.transform(self.test_data) + assert_array_almost_equal(np.array([2.22067872, 1.20833788, 1.32463100]), score) def test_transform_not_fitted(self) -> None: with self.assertRaises(ValueError): self.mahalanobis_score.transform(self.test_data) with self.assertRaises(ValueError): self.mahalanobis_score_no_pca.transform(self.test_data) + + def test_pca_is_deprecated(self) -> None: + with self.assertWarns(DeprecationWarning): + MahalanobisScore(pca=True) + + def test_no_pca_is_not_deprecated(self) -> None: + with warnings.catch_warnings(): + warnings.simplefilter("error") + MahalanobisScore(pca=False) + + def test_scale_is_deprecated(self) -> None: + with self.assertWarns(DeprecationWarning): + MahalanobisScore(scale=False) + + def test_scale_has_no_effect(self) -> None: + # the deprecated `scale` parameter no longer has any effect. + with self.assertWarns(DeprecationWarning): + mahalanobis_deprecated = MahalanobisScore(pca=True, scale=False) + + self.mahalanobis_score.fit(self.train_data) + mahalanobis_deprecated.fit(self.train_data) + + score_default = self.mahalanobis_score.transform(self.test_data) + score_deprecated = mahalanobis_deprecated.transform(self.test_data) + assert_array_almost_equal(score_default, score_deprecated) + + def test_invalid_covariance_method(self) -> None: + with self.assertRaises(ValueError): + MahalanobisScore(pca=False, covariance_method="bogus") + + def test_invalid_shrinkage_method(self) -> None: + with self.assertRaises(ValueError): + MahalanobisScore(pca=False, shrinkage_method="bogus") + + def test_auto_uses_min_cov_det_when_ratio_is_sufficient(self) -> None: + # n/p = 20 >= min_cov_det_ratio (2.0) and the covariance is well conditioned. + rng = np.random.RandomState(0) + data = pd.DataFrame(rng.randn(100, 5)) + score = MahalanobisScore(pca=False, covariance_method="auto") + score.fit(data) + self.assertEqual(score.covariance_method_, "min_cov_det") + self.assertIs(score.cov_estimator_, score.min_cov_det_object) + score.transform(data.head(3)) + + def test_auto_uses_shrinkage_when_ratio_is_insufficient(self) -> None: + # n/p = 1 < min_cov_det_ratio (2.0) -> shrinkage fallback. + rng = np.random.RandomState(0) + data = pd.DataFrame(rng.randn(8, 5)) + score = MahalanobisScore(pca=False, covariance_method="auto") + score.fit(data) + self.assertEqual(score.covariance_method_, "shrinkage") + self.assertIsInstance(score.cov_estimator_, OAS) + + def test_auto_uses_min_cov_det_with_custom_ratio(self) -> None: + # With a low enough ratio threshold, MinCovDet is used even at n/p = 1.6. + rng = np.random.RandomState(0) + data = pd.DataFrame(rng.randn(8, 5)) + score = MahalanobisScore(pca=False, covariance_method="auto", min_cov_det_ratio=1.5) + score.fit(data) + self.assertEqual(score.covariance_method_, "min_cov_det") + + def test_auto_falls_back_to_shrinkage_on_degenerate_covariance(self) -> None: + # n/p is sufficient (10 >= 2) but the last feature is a perfect linear combination + # of the first two, so the MinCovDet covariance is singular -> fall back to shrinkage. + rng = np.random.RandomState(0) + mat = np.column_stack([rng.randn(50, 3), rng.randn(50, 1)]) + mat = np.column_stack([mat, mat[:, 0] + mat[:, 1]]) + data = pd.DataFrame(mat) + self.assertEqual(data.shape[0] / data.shape[1], 10.0) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + score = MahalanobisScore(pca=False, covariance_method="auto") + score.fit(data) + self.assertEqual(score.covariance_method_, "shrinkage") + self.assertIsInstance(score.cov_estimator_, OAS) + + def test_explicit_shrinkage_oas(self) -> None: + rng = np.random.RandomState(0) + data = pd.DataFrame(rng.randn(100, 5)) + score = MahalanobisScore(pca=False, covariance_method="shrinkage", shrinkage_method="oas") + score.fit(data) + self.assertEqual(score.covariance_method_, "shrinkage") + self.assertIsInstance(score.cov_estimator_, OAS) + result = score.transform(data.head(3)) + self.assertTrue(np.all(np.isfinite(result))) + + def test_explicit_shrinkage_ledoit_wolf(self) -> None: + rng = np.random.RandomState(0) + data = pd.DataFrame(rng.randn(100, 5)) + score = MahalanobisScore(pca=False, covariance_method="shrinkage", shrinkage_method="ledoit_wolf") + score.fit(data) + self.assertEqual(score.covariance_method_, "shrinkage") + self.assertIsInstance(score.cov_estimator_, LedoitWolf) + result = score.transform(data.head(3)) + self.assertTrue(np.all(np.isfinite(result))) + + def test_explicit_min_cov_det(self) -> None: + rng = np.random.RandomState(0) + data = pd.DataFrame(rng.randn(100, 5)) + score = MahalanobisScore(pca=False, covariance_method="min_cov_det") + score.fit(data) + self.assertEqual(score.covariance_method_, "min_cov_det") + self.assertIs(score.cov_estimator_, score.min_cov_det_object) + result = score.transform(data.head(3)) + self.assertTrue(np.all(np.isfinite(result))) + + def test_shrinkage_handles_high_dimensional_data(self) -> None: + # Shrinkage remains well defined even when p approaches / exceeds n. + rng = np.random.RandomState(1) + data = pd.DataFrame(rng.randn(50, 120)) + score = MahalanobisScore(pca=False, covariance_method="shrinkage", shrinkage_method="oas") + score.fit(data) + self.assertEqual(score.covariance_method_, "shrinkage") + result = score.transform(data.head(5)) + self.assertTrue(np.all(np.isfinite(result))) diff --git a/tests/anomaly_scores/test_rmse_score.py b/tests/anomaly_scores/test_rmse_score.py index 93265483..f5379154 100644 --- a/tests/anomaly_scores/test_rmse_score.py +++ b/tests/anomaly_scores/test_rmse_score.py @@ -2,6 +2,7 @@ from unittest import TestCase import numpy as np +import pandas as pd from numpy.testing import assert_array_equal, assert_array_almost_equal from energy_fault_detector.anomaly_scores.rmse_score import RMSEScore @@ -10,7 +11,6 @@ class TestRMSEScore(TestCase): def setUp(self) -> None: self.rmse_score = RMSEScore() - self.rmse_score_new = RMSEScore(scale=False) self.train_data = np.array([ [1, 2, 3], [4, 5, 6], @@ -20,7 +20,6 @@ def setUp(self) -> None: [1, 5, 6], [4, 8, 6], ]) - self.rmse_score_new.fit(self.train_data) # does nothing def test_fit(self) -> None: @@ -28,17 +27,52 @@ def test_fit(self) -> None: assert_array_equal(np.array([4., 5., 6.]), self.rmse_score.mean_x_) assert_array_almost_equal(np.array([2.44948974, 2.44948974, 2.44948974]), self.rmse_score.std_x_) - self.assertTrue(self.rmse_score_new.fitted_) + self.assertTrue(self.rmse_score.fitted_) def test_transform(self) -> None: self.rmse_score.fit(self.train_data) score = self.rmse_score.transform(self.test_data) assert_array_almost_equal(np.array([0.70710678, 0.70710678]), score) - score_new = self.rmse_score_new.transform(self.test_data) - score_new_expected = np.sqrt(np.mean(self.test_data**2, axis=1)) - assert_array_almost_equal(score_new, score_new_expected) - def test_transform_not_fitted(self) -> None: with self.assertRaises(ValueError): self.rmse_score.transform(self.test_data) + + def test_transform_with_zero_std_feature(self) -> None: + """A feature with std 0 in the training data must not crash a later transform. + + The std-0 feature is left unscaled (dividing by 0 would produce inf/nan), + while the remaining features are still standardized. + """ + train_data = pd.DataFrame({ + "feature_A": [1.0, 2.0, 3.0], + "feature_B": [5.0, 5.0, 5.0], # std == 0 + }) + test_data = pd.DataFrame({ + "feature_A": [1.0, 5.0], + "feature_B": [4.0, 6.0], + }) + self.rmse_score.fit(train_data) + with self.assertLogs('energy_fault_detector', level='WARNING') as captured: + score = self.rmse_score.transform(test_data) + self.assertTrue(np.isfinite(score).all()) + assert_array_almost_equal(score, np.array([2.95803989, 6.06217783])) + self.assertTrue(any( + 'standard deviation of 0.0' in message for message in captured.output + )) + + def test_scale_is_deprecated(self) -> None: + with self.assertWarns(DeprecationWarning): + RMSEScore(scale=False) + + def test_scale_has_no_effect(self) -> None: + # the deprecated `scale` parameter no longer has any effect. + with self.assertWarns(DeprecationWarning): + rmse_deprecated = RMSEScore(scale=False) + + self.rmse_score.fit(self.train_data) + rmse_deprecated.fit(self.train_data) + + score_default = self.rmse_score.transform(self.test_data) + score_deprecated = rmse_deprecated.transform(self.test_data) + assert_array_almost_equal(score_default, score_deprecated) diff --git a/tests/anomaly_scores/test_weighted_rmse_score.py b/tests/anomaly_scores/test_weighted_rmse_score.py new file mode 100644 index 00000000..3169ceb7 --- /dev/null +++ b/tests/anomaly_scores/test_weighted_rmse_score.py @@ -0,0 +1,110 @@ +import os +import pickle +import tempfile +import shutil +from unittest import TestCase + +import numpy as np +import pandas as pd +from numpy.testing import assert_array_equal, assert_array_almost_equal + +from energy_fault_detector.anomaly_scores.weighted_rmse_score import WeightedRMSEScore +from energy_fault_detector.anomaly_scores.rmse_score import RMSEScore + + +class TestWeightedRMSEScore(TestCase): + def setUp(self) -> None: + self.train_data = pd.DataFrame( + {"feature_A": [0, 0, 1], + "feature_B": [0, 1, 0], + "feature_C": [1, 0, 0], + } + ) + self.test_data = pd.DataFrame( + {"feature_A": [1, 0, 0, 1, 0, 1, 1], + "feature_B": [0, 1, 0, 1, 1, 0, 1], + "feature_C": [0, 0, 1, 0, 1, 1, 1], + } + ) + self.feature_weights = {"feature_A": 2.0, "feature_B": 1.0, "feature_C": 0.5} + self.weighted_rmse_score = WeightedRMSEScore(feature_weights=self.feature_weights) + self.rmse_score = RMSEScore() + + def test_fit(self) -> None: + self.rmse_score.fit(self.train_data) + self.weighted_rmse_score.fit(self.train_data) + assert_array_equal(self.rmse_score.mean_x_, self.weighted_rmse_score.mean_x_) + assert_array_equal(self.rmse_score.std_x_, self.weighted_rmse_score.std_x_) + self.assertTrue(self.weighted_rmse_score.fitted_) + + def test_transform(self) -> None: + self.weighted_rmse_score.fit(self.train_data) + weighted_score = self.weighted_rmse_score.transform(self.test_data) + assert_array_almost_equal(weighted_score, np.array([1.603567, 1.133893, 0.801784, 1.963961, 1.38873 , 1.792843, 2.12132])) + + def test_instantiate_without_feature_weights(self) -> None: + """The scorer must be constructible without arguments. + + This is required by the FaultDetector load mechanism, which reconstructs each + sub-model with `model_class()` (no arguments) before restoring the pickled state. + Before a default value was added for `feature_weights`, this raised a TypeError. + """ + score = WeightedRMSEScore() + self.assertEqual(score.feature_weights, {}) + + def test_feature_weights_restored_on_load(self) -> None: + """`feature_weights` must be restored from the pickled state on load.""" + self.weighted_rmse_score.fit(self.train_data) + + loaded_dict = pickle.loads(pickle.dumps(self.weighted_rmse_score.__dict__)) + self.assertEqual(loaded_dict['feature_weights'], self.feature_weights) + + def test_save_load_via_save_load_mixin(self) -> None: + """The SaveLoadMixin save/load path (used by FaultDetector) restores feature_weights.""" + tmp_dir = tempfile.mkdtemp() + try: + self.weighted_rmse_score.fit(self.train_data) + self.weighted_rmse_score.save(tmp_dir) + + loaded_score = WeightedRMSEScore() + loaded_score.load(tmp_dir) + + self.assertIsInstance(loaded_score, WeightedRMSEScore) + self.assertEqual(loaded_score.feature_weights, self.feature_weights) + self.assertTrue(loaded_score.fitted_) + finally: + shutil.rmtree(tmp_dir) + + def test_test_transform_with_incomplete_feature_weights(self) -> None: + """If a feature is missing from the weights, it should be treated as weight=1.0.""" + incomplete_weights = {"feature_A": 2.0, "feature_B": 1.0} # feature_C is missing + weighted_rmse_score = WeightedRMSEScore(feature_weights=incomplete_weights) + weighted_rmse_score.fit(self.train_data) + weighted_score = weighted_rmse_score.transform(self.test_data) + + # The expected score is calculated with feature_C having weight=1.0 + expected_score = np.array([1.5, 1.06066 , 1.06066 , 1.837117, 1.5, 1.837117, 2.12132]) + assert_array_almost_equal(weighted_score, expected_score) + + def test_transform_with_zero_std_feature(self) -> None: + """A feature with std 0 in the training data must not crash a later transform. + + The std-0 feature is left unscaled (dividing by 0 would produce inf/nan), + while the remaining features are still scaled by their standard deviation. + """ + train_data = pd.DataFrame({ + "feature_A": [1.0, 2.0, 3.0], + "feature_B": [5.0, 5.0, 5.0], # std == 0 + }) + test_data = pd.DataFrame({ + "feature_A": [1.0, 5.0], + "feature_B": [4.0, 6.0], + }) + self.weighted_rmse_score.fit(train_data) + with self.assertLogs('energy_fault_detector', level='WARNING') as captured: + score = self.weighted_rmse_score.transform(test_data) + self.assertTrue(np.isfinite(score).all()) + assert_array_almost_equal(score, np.array([2.51661148, 6.08276253])) + self.assertTrue(any( + 'standard deviation of 0.0' in message for message in captured.output + )) diff --git a/tests/test_data/test_config_weighted_rmse.yaml b/tests/test_data/test_config_weighted_rmse.yaml new file mode 100644 index 00000000..4809caeb --- /dev/null +++ b/tests/test_data/test_config_weighted_rmse.yaml @@ -0,0 +1,36 @@ +train: + anomaly_score: + name: 'weighted_rmse' + params: + feature_weights: + a: 2.0 + b: 1.0 + c: 0.5 + data_preprocessor: # no further spec, so defaults are applied + autoencoder: + name: 'MultilayerAutoencoder' + verbose: 0 + params: + layers: [300] + code_size: 50 + learning_rate: 0.001 + decay_rate: 0.001 + decay_steps: 100 + batch_size: 144 + epochs: 10 + loss_name: 'mean_squared_error' + threshold_selector: + name: 'FDR' + params: + target_false_discovery_rate: 0.8 + data_splitter: + train_block_size: 7 + val_block_size: 3 + data_clipping: + lower_percentile: 0.01 + upper_percentile: 0.99 + +root_cause_analysis: + alpha: 0.8 + init_x_bias: recon + num_iter: 200 diff --git a/tests/test_fault_detector.py b/tests/test_fault_detector.py index 7cd8d3b5..10e4c8d5 100644 --- a/tests/test_fault_detector.py +++ b/tests/test_fault_detector.py @@ -96,6 +96,62 @@ def test_save_load_models(self): self.assertEqual(expected_path, model_path) +class TestFaultDetectorWeightedRMSESaveLoad(unittest.TestCase): + """Saving and loading a FaultDetector that uses the weighted_rmse anomaly score. + + Loading a FaultDetector reconstructs each sub-model with `model_class()` (i.e. without + arguments). The WeightedRMSEScore therefore needs a default value for `feature_weights` + so it can be instantiated on load. Previously `feature_weights` was a required argument, + which caused the load to fail for trained detectors that used the weighted_rmse score. + """ + + def setUp(self) -> None: + self.config_path = os.path.join(PROJECT_ROOT, 'tests/test_data/test_config_weighted_rmse.yaml') + self.conf = Config(self.config_path) + self.test_dir = tempfile.mkdtemp() + + np.random.seed(42) + self.sensor_data = pd.DataFrame(data=np.random.random(size=(100, 3)), columns=['a', 'b', 'c']) + self.normal_index = pd.Series(np.random.choice([True, False], size=100)) + + def tearDown(self) -> None: + shutil.rmtree(self.test_dir) + + def test_save_load_roundtrip_with_weighted_rmse(self): + from energy_fault_detector.anomaly_scores.weighted_rmse_score import WeightedRMSEScore + + fault_detector = FaultDetector(config=self.conf, model_directory=self.test_dir) + self.assertIsInstance(fault_detector.anomaly_score, WeightedRMSEScore) + self.assertEqual(fault_detector.anomaly_score.feature_weights, + {'a': 2.0, 'b': 1.0, 'c': 0.5}) + + # Train and save the model + results = fault_detector.fit(sensor_data=self.sensor_data, normal_index=self.normal_index) + + # Load the saved models into a fresh FaultDetector + loaded_fault_detector = FaultDetector.load(model_path=results.model_path) + + # The weighted_rmse score must load back as a WeightedRMSEScore instance ... + self.assertIsInstance(loaded_fault_detector.anomaly_score, WeightedRMSEScore) + # ... and its feature_weights must be restored from the pickled state + self.assertEqual(loaded_fault_detector.anomaly_score.feature_weights, + {'a': 2.0, 'b': 1.0, 'c': 0.5}) + + # The loaded score must still be usable for scoring + recon_error = pd.DataFrame(np.random.random(size=(5, 3)), columns=['a', 'b', 'c']) + scores = loaded_fault_detector.anomaly_score.transform(recon_error) + self.assertEqual(len(scores), 5) + + # Autoencoder weights should round-trip as well + original_weights = fault_detector.autoencoder.model.get_weights() + loaded_weights = loaded_fault_detector.autoencoder.model.get_weights() + for original_weight, loaded_weight in zip(original_weights, loaded_weights): + np.testing.assert_array_almost_equal(original_weight, loaded_weight) + + self.assertDictEqual(fault_detector.config.config_dict, + loaded_fault_detector.config.config_dict) + + @patch("energy_fault_detector.core.autoencoder.Autoencoder", new=mock_autoencoder) @patch("energy_fault_detector.core.model_factory.AnomalyScore", new=mock_score) @patch("energy_fault_detector.core.model_factory.ThresholdSelector", new=mock_threshold)