energy_fault_detector.core.fault_detection_model

Template for anomaly detection models

class FaultDetectionModel(config=None, model_directory='models')

Bases: ABC

Template for fault detection models. Train and predicts should be implemented.

Parameters:
  • config (Optional[Config]) – Config object with fault detection configuration. Defaults to None. If None, the models need to be loaded from a path using the load_models method.

  • model_directory (str, optional) – Directory to save models to. Defaults to ‘output’.

anomaly_score

AnomalyScore object.

autoencoder

Autoencoder object.

threshold_selector

ThresholdSelector object.

data_preprocessor

DataPreprocessorPipeline object.

save_timestamps

a list of string timestamps, indicating when the model was saved.

abstractmethod fit(sensor_data, normal_index=None, asset_id=None, **kwargs)

Fit models on the given sensor_data and save them locally and return the metadata.

Parameters:
  • asset_id (Union[int, str]) – asset ID of the asset for which the model should be trained.

  • sensor_data (DataFrame) – pandas DataFrame with the sensor data to use. The time stamp should be the index and the sensor values as columns.

  • normal_index (Series) – a pandas Series indicating normal behaviour as boolean with the timestamp as index.

Return type:

ModelMetadata

Returns:

ModelMetadata object.

load_models(model_path)

Load saved models given the model path.

Parameters:

model_path (str) – Path to the model files.

Return type:

None

abstractmethod predict(sensor_data, model_path=None, asset_id=None)

Predict with given models for specific asset ID. Return dictionary with results.

Parameters:
  • asset_id (Union[int, str]) – asset ID of the asset for which the model must be applied.

  • model_path (Optional[str]) – path to the models to be applied.

  • sensor_data (DataFrame) – pandas DataFrame with the sensor data to use. The time stamp should be the index and the sensor values as columns.

Return type:

FaultDetectionResult

Returns:

FaultDetectionResult object.

save_models(model_name=None, overwrite=False)

Save the model objects. Model files are saved in self.model_directory/model_name/datetime if overwrite is set to False, otherwise, they are saved in self.model_directory/model_name

Parameters:
  • model_name (optional str) – model name, will be the directory in which the model files are saved. Changes model path to self.model_directory/model_name. Defaults to None.

  • overwrite (optional bool) – If True existing folders can be overwritten. Default: False

Return type:

Tuple[str, str]

Returns:

The full path to the saved models and the timestamp of the function call in string format.

train(sensor_data, normal_index=None, asset_id=None, **kwargs)

Same as the fit-method.

Return type:

ModelMetadata

train_val_split(x)

Split data in train and validation data.

Parameters:

x (Union[DataFrame, ndarray, List]) – dataframe/list/array to split

Returns:

x_train, x_val

Return type:

tuple

exception NoTrainingData

Bases: Exception

Raised when no training data is available.