energy_fault_detector.core

This module contains class templates for most of the anomaly detection classes, such as anomaly scores, threshold selectors and data classes.

class AnomalyScore(**kwargs)[source]

Bases: BaseEstimator, TransformerMixin, SaveLoadMixin, ABC

Anomaly score template

abstractmethod fit(x, y=None)[source]

Fit the scorer.

Parameters:
  • x (Union[DataFrame, ndarray]) – numpy 2d array with differences between prediction and actual sensor values

  • y (Optional[Series]) – labels indicating whether sample is normal (True) or anomalous (False).

Return type:

AnomalyScore

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') AnomalyScore

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in fit.

Returns

selfobject

The updated object.

set_transform_request(*, x: bool | None | str = '$UNCHANGED$') AnomalyScore

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in transform.

Returns

selfobject

The updated object.

abstractmethod transform(x)[source]

Implement transform method.

Parameters:

x (Union[DataFrame, ndarray]) – input data

Return type:

Series

Returns:

Scores

class DataTransformer(**kwargs)[source]

Bases: BaseEstimator, TransformerMixin, SaveLoadMixin, ABC

DataTransformer template.

abstractmethod fit(x, y=None)[source]

Fit the preprocessor with training data. Should set attributes feature_names_in_ and n_features_in_. If columns are selected/dropped, should also set feature_names_out_ and columns_dropped_.

Parameters:
  • x (DataFrame) – pandas dataframe with input data

  • y (Series) – (optional) labels indicating whether sample is normal (True) or anomalous (False).

Return type:

DataTransformer

abstractmethod get_feature_names_out(input_features=None)[source]

Get output feature names for transformation.

Return type:

List[str]

Returns

feature_names_out : list/np array of str objects.

abstractmethod inverse_transform(x)[source]

Implement inverse transform method

Parameters:

x (DataFrame) – input data.

Return type:

DataFrame

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') DataTransformer

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in fit.

Returns

selfobject

The updated object.

set_inverse_transform_request(*, x: bool | None | str = '$UNCHANGED$') DataTransformer

Request metadata passed to the inverse_transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to inverse_transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to inverse_transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in inverse_transform.

Returns

selfobject

The updated object.

set_transform_request(*, x: bool | None | str = '$UNCHANGED$') DataTransformer

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in transform.

Returns

selfobject

The updated object.

abstractmethod transform(x)[source]

Implement transform method.

Parameters:

x (DataFrame) – input data.

Return type:

DataFrame

class FaultDetectionResult(predicted_anomalies, reconstruction, recon_error, anomaly_score, bias_data=None, arcana_losses=None, tracked_bias=None)[source]

Bases: object

Class to encapsulate results from the fault detection process.

anomaly_score: Series

Series with predicted anomaly scores.

arcana_losses: DataFrame | None = None

DataFrame containing recorded values for all losses in ARCANA. None if ARCANA was not run. Empty if losses were not tracked.

bias_data: DataFrame | None = None

DataFrame with ARCANA results (ARCANA bias). None if ARCANA was not run.

criticality(normal_idx=None, init_criticality=0, max_criticality=1000)[source]

Criticality based on the predicted anomalies.

Parameters:
  • normal_idx (Series | None) – A pandas Series with boolean values indicating normal operation, indexed by timestamp. Ignored if None.

  • init_criticality (int) – The initial criticality value. Defaults to 0.

  • max_criticality (int) – The maximum criticality value. Defaults to 1000.

Return type:

Series

classmethod load(directory, **kwargs)[source]

Loads the results from CSV files in the specified directory.

Restores MultiIndex if the results were saved with one.

Parameters:
  • directory (str | Path) – The directory where the CSV files are stored.

  • kwargs – other keywords args for pd.read_csv (e.g., sep=’,’)

Returns:

The loaded result object.

Return type:

FaultDetectionResult

predicted_anomalies: Series

Series with a predicted anomalies (bool).

recon_error: DataFrame

DataFrame with reconstruction errors.

reconstruction: DataFrame

DataFrame with reconstruction of the input data.

save(directory, **kwargs)[source]

Saves the results to CSV files in the specified directory.

Parameters:
  • directory (str | Path) – The directory where the CSV files will be saved.

  • kwargs – other keywords args for pd.DataFrame.to_csv (i.e. sep=’,’)

Return type:

None

tracked_bias: List[DataFrame] | None = None

List of DataFrames containing the ARCANA bias every 50th iteration. None if ARCANA was not run. Empty if bias was not tracked.

class ModelMetadata(model_date, model_path, train_recon_error, val_recon_error=None)[source]

Bases: object

Class to encapsulate metadata about the FaultDetector model.

model_date: str
model_path: str | Path
train_recon_error: ndarray | DataFrame
val_recon_error: ndarray | DataFrame | None = None
class ThresholdSelector[source]

Bases: BaseEstimator, ClassifierMixin, SaveLoadMixin

Template for threshold selectors.

Finds the threshold (fit method) of the given reconstruction errors (x), to be considered anomalous. The predict method returns an array of boolean, indicating which samples of the input are anomalous.

threshold

scores above the threshold is classified as anomaly, below is classified as normal.

__sklearn_is_fitted__()[source]

Needed for check_is_fitted

fit(x, y=None)[source]

Fit ThresholdSelector object on anomaly score values x and labels y to determine the score threshold. For prediction, every sample with a scores above this threshold is considered an anomaly.

Parameters:
  • x (Union[ndarray, Series]) – array or pandas Series with calculated anomaly scores

  • y (Union[ndarray, Series]) – labels indicating whether sample is normal (True) or anomalous (False) Optional, if not given, we assume all data provided represents normal behaviour

Return type:

ThresholdSelector

Returns:

The threshold selector

predict(x, **kwargs)[source]

Return a boolean array indicating whether sample is anomalous.

Parameters:
  • x (Union[ndarray, Series]) – array with calculated anomaly scores

  • kwargs – other arguments for specific threshold selectors.

Return type:

Union[ndarray, Series, Tuple[Union[ndarray, Series], Union[ndarray, Series]]]

Returns:

Boolean array where true = anomaly, false = normal

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') ThresholdSelector

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in fit.

Returns

selfobject

The updated object.

set_predict_request(*, x: bool | None | str = '$UNCHANGED$') ThresholdSelector

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in predict.

Returns

selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ThresholdSelector

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns

selfobject

The updated object.