energy_fault_detector
The anomaly-detection-iee package
- class Config(config_filename=None, config_dict=None)
Bases:
BaseConfigConfiguration class. Either config_filename or config_dict must be provided. Reads a yaml files with the anomaly detection configuration and sets corresponding settings.
- class FaultDetector(config=None, model_directory='fault_detector_model', model_subdir=None)
Bases:
FaultDetectionModelMain class for fault detection in renewable energy assets and power grids.
- 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 ‘fault_detector_model’.
model_subdir (Optional[Any], optional) – Deprecated. This argument will be removed in future versions. Defaults to None.
- 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.
- fit(sensor_data, normal_index=None, save_models=True, overwrite_models=False, fit_autoencoder_only=False, fit_preprocessor=True, **kwargs)
Fit models on the given sensor_data and save them locally and return the metadata.
- Parameters:
sensor_data (pd.DataFrame) – DataFrame with the sensor data of one asset for a specific time window. The timestamp should be the index and the sensor values as columns.
normal_index (Optional[pd.Series]) – Series indicating normal behavior as boolean with the timestamp as index. Optional; if not provided, assumes all sensor_data represents normal behavior.
save_models (bool, optional) – Whether to save models. Defaults to True.
overwrite_models (bool, optional) – If True, existing model directories can be overwritten. Defaults to False.
fit_autoencoder_only (bool, optional) – If True, only fit the data preprocessor and autoencoder objects. Defaults to False.
fit_preprocessor (bool, optional) – If True, the preprocessor is fitted. Defaults to True.
- Returns:
metadata of the trained model: model_date, model_path, model reconstruction errors of the training and validation data.
- Return type:
- predict(sensor_data, model_path=None, root_cause_analysis=False, track_losses=False, track_bias=False)
Predict with given models for a specific asset
- Parameters:
sensor_data (pd.DataFrame) – DataFrame with the sensor data of one asset for a specific time window. The timestamp should be the index and the sensor values as columns.
model_path (Optional[str], optional) – Path to the models to be applied. If None, assumes the attributes data_preprocessor, autoencoder, anomaly_score, and threshold_selector contain fitted instances.
root_cause_analysis (bool, optional) – Whether to run ARCANA. Defaults to False.
track_losses (bool, optional) – Optional; if True, ARCANA losses will be tracked over the iterations. Defaults to False.
track_bias (bool, optional) – Optional; if True, ARCANA bias will be tracked over the iterations. Defaults to False.
- Returns:
- with the following DataFrames:
predicted_anomalies: DataFrame with a column ‘anomaly’ (bool).
reconstruction: DataFrame with reconstruction of the sensor data with timestamp as index.
deviations: DataFrame with reconstruction errors.
anomaly_score: DataFrame with anomaly scores for each timestamp.
bias_data: DataFrame with ARCANA results with timestamp as index. None if ARCANA was not run.
arcana_losses: DataFrame containing recorded values for all losses in ARCANA. None if ARCANA was not run.
tracked_bias: List of DataFrames. None if ARCANA was not run.
- Return type:
- predict_anomalies(scores, x_prepped=None)
Predict anomalies based on anomaly scores.
- Return type:
Series
- predict_anomaly_score(sensor_data)
Predict the anomaly score.
- Return type:
Series
- preprocess_train_data(sensor_data, normal_index, fit_preprocessor=True)
Preprocesses the training data using the configured data_preprocessor
- Parameters:
sensor_data (pd.DataFrame) – unprocessed training data
normal_index (pd.Series) – unprocessed normal index
fit_preprocessor (bool, optional) – if True the preprocessor is fitted. If False the preprocessor is not fitted and the user has to provide a ready-to-use preprocessor by loading models before calling this function.
- Return type:
Tuple[DataFrame,DataFrame,Series]
- Returns: tuple of (pd.Dataframe, pd.Dataframe, pd.Series)
x_prepped (pd.DataFrame): preprocessed normal training data x: ordered training data (unprocessed) # needed for _fit_threshold y: ordered normal_index (unprocessed) # needed for _fit_threshold
- run_root_cause_analysis(sensor_data, track_losses=False, track_bias=False)
Run ARCANA
- Parameters:
- Return type:
- Returns: Tuple of (pd.DataFrame, pd.DataFrame, List[pd.DataFrame])
df_arcana_bias: pandas dataframe containing the arcana bias. arcana_losses: dictionary containing loss names as keys and lists of loss values as values. tracked_bias: list of pandas dataframe containing the arcana bias recorded over the iterations.
- tune(sensor_data, normal_index=None, pretrained_model_path=None, new_learning_rate=0.0001, tune_epochs=10, tune_method='full', save_models=True, overwrite_models=False, data_preprocessor=None)
- FaultDetector finetuning via the following methods:
‘full’ (all autoencoder weights + threshold and anomaly-score scaling will be adapted), ‘decoder’ (only decoder weights + threshold will be adapted), ‘threshold’ (only the threshold and anomaly-score scaling is adapted)
- Notes: Parameters tune_epochs and new_learning_rate should be chosen carefully while considering
potential overfitting issues depending on the similarity of the tuning data and the training data.
- Parameters:
sensor_data (pd.DataFrame) – DataFrame with the sensor data of one asset for a specific time window. The timestamp should be the index and the sensor values as columns.
normal_index (pd.Series, optional) – Series indicating normal behavior as boolean with the timestamp as index. If not provided, it is assumed all data in sensor_data represents normal behaviour. Defaults to None.
pretrained_model_path (Optional[str], optional) – Path to pretrained model. If None, assumes attributes data_preprocessor, autoencoder, anomaly_score, and threshold_selector contain fitted instances.
tune_epochs (int, optional) – Number of epochs to fine-tune. Defaults to 10.
new_learning_rate (float, optional) – Learning rate to tune the autoencoder with. Defaults to 0.0001.
tune_method (str, optional) – Possible options: ‘full’ (all autoencoder weights + threshold and anomaly-score scaling will be adapted), ‘decoder’ (only decoder weights + threshold will be adapted), ‘threshold’ (only the threshold and anomaly-score scaling is adapted) Defaults to ‘full’.
save_models (bool, optional) – Whether to save models. Defaults to True.
overwrite_models (bool, optional) – If True, existing model directories can be overwritten. Defaults to False.
data_preprocessor (Optional[DataPreprocessor], optional) – Optional prefitted data preprocessor. Useful when using a generic preprocessor for all models.
- Returns:
metadata of the trained model with model_date, model_path, model reconstruction errors of the training and validation data.
- Return type:
- quick_fault_detector(csv_data_path, csv_test_data_path=None, train_test_column_name=None, train_test_mapping=None, time_column_name=None, status_data_column_name=None, status_mapping=None, status_label_confidence_percentage=0.95, features_to_exclude=None, angle_features=None, automatic_optimization=True, enable_debug_plots=False, min_anomaly_length=18, save_dir=None)
Analyzes provided data using an autoencoder based approach for identifying anomalies based on a learned normal behavior. Anomalies are then aggregated to events and further analyzed. Runs the entire fault detection module chain in one function call. Sections of this function call are: 1. Data Loading and verification 2. Config selection and optimization 3. AnomalyDetector training 4. AnomalyDetector prediction 5. Event aggregation 6. ARCANA-Analysis of detected events 7. Visualization of output
- Parameters:
csv_data_path (str) – Path to a csv-file containing tabular data which must contain training data for the autoencoder. This data can also contain test data for evaluation, but in this case train_test_column and optionally train_test_mapping must be provided.
csv_test_data_path (Optional str) – Path to a csv file containing test data for evaluation. If test data is provided in both ways (i.e. via csv_test_data_path and in csv_data_path + train_test_column) then both test data sets will be fused into one. Default is None.
train_test_column_name (Optional str) – Name of the column which specifies which part of the data in csv_data_path is training data and which is test data. If this column does not contain boolean values or values which can be cast into boolean values, then train_test_mapping must be provided. True values are interpreted as training data. Default is None.
train_test_mapping (Optional dict) – Dictionary which defines a mapping of all non-boolean values in the train_test_column to booleans. Keys of the dictionary must be values from train_test_column, and they must have a datatype which can be cast to the datatype of train_test_column. Values of this dictionary must be booleans or at least castable to booleans. Default is None.
time_column_name (Optional str) – Name of the column containing time stamp information.
status_data_column_name (Optional str) – Name of the column which specifies the status of each row in csv_data_path. The status is used to define which rows represent normal behavior (i.e. which rows can be used for the autoencoder training) and which rows contain anomalous behavior. If this column does not contain boolean values, status_mapping must be provided. If status_data_column_name is not provided, all rows in csv_data_path are assumed to be normal and a warning will be logged. Default is None.
status_mapping (Optional dict) – Dictionary which defines a mapping of all non-boolean values in the status_data_column to booleans. Keys of the dictionary must be values from status_data_column, and they must have a datatype which can be cast to the datatype of train_test_column. Values of this dictionary must be booleans or at least castable to booleans. True values are interpreted as normal status. Default is None.
status_label_confidence_percentage (Optional float) – Specifies how sure the user is that the provided status labels and derived normal_indexes are correct. This determines the quantile for quantile threshold method. Default is 0.95.
features_to_exclude (Optional[List[str]]) – List of column names which are present in the csv-files but which should be ignored for this failure detection run. Default is None.
angle_features (Optional[List[str]]) – List of column names which represent angle-features. This enables a specialized preprocessing of angle features, which might otherwise hinder the failure detection process. Default is None.
automatic_optimization (bool) – If True, an automatic hyperparameter optimization is done based on the dimension of the provided dataset. Default is True.
enable_debug_plots (bool) – If True advanced information for debugging is added to the result plots. Default is False.
min_anomaly_length (int) – Minimal number of consecutive anomalies (i.e. data points with an anomaly score above the FaultDetector threshold) to define an anomaly event.
save_dir (Optional[str]) – Directory to save the output plots. If not provided, the plots are not saved. Defaults to None.
- Returns:
FaultDetectionResult object with the following DataFrames:
predicted_anomalies: DataFrame with a column ‘anomaly’ (bool).
reconstruction: DataFrame with reconstruction of the sensor data with timestamp as index.
deviations: DataFrame with reconstruction errors.
anomaly_score: DataFrame with anomaly scores for each timestamp.
bias_data: DataFrame with ARCANA results with timestamp as index. None if ARCANA was not run.
arcana_losses: DataFrame containing recorded values for all losses in ARCANA. None if ARCANA was not run.
tracked_bias: List of DataFrames. None if ARCANA was not run.
and the detected anomaly events as dataframe.
- Return type:
Tuple(FaultDetectionResult, pd.DataFrame)
- energy_fault_detector.anomaly_scores
- energy_fault_detector.autoencoders
- energy_fault_detector.config
- energy_fault_detector.core
- energy_fault_detector.core.anomaly_score
- energy_fault_detector.core.autoencoder
AutoencoderAutoencoder.is_conditionalAutoencoder.modelAutoencoder.encoderAutoencoder.historyAutoencoder.callbacksAutoencoder.__call__()Autoencoder.compile_model()Autoencoder.create_model()Autoencoder.encode()Autoencoder.fit()Autoencoder.get_reconstruction_error()Autoencoder.load()Autoencoder.predict()Autoencoder.save()Autoencoder.tune()Autoencoder.tune_decoder()
split_inputs()
- energy_fault_detector.core.data_transformer
- energy_fault_detector.core.fault_detection_model
FaultDetectionModelFaultDetectionModel.anomaly_scoreFaultDetectionModel.autoencoderFaultDetectionModel.threshold_selectorFaultDetectionModel.data_preprocessorFaultDetectionModel.save_timestampsFaultDetectionModel.fit()FaultDetectionModel.load_models()FaultDetectionModel.predict()FaultDetectionModel.save_models()FaultDetectionModel.train()FaultDetectionModel.train_val_split()
NoTrainingData
- energy_fault_detector.core.fault_detection_result
- energy_fault_detector.core.model_factory
- energy_fault_detector.core.save_load_mixin
- energy_fault_detector.core.threshold_selector
- energy_fault_detector.data_preprocessing
- energy_fault_detector.data_preprocessing.angle_transformer
- energy_fault_detector.data_preprocessing.column_selector
- energy_fault_detector.data_preprocessing.data_clipper
- energy_fault_detector.data_preprocessing.data_preprocessor
- energy_fault_detector.data_preprocessing.duplicate_value_to_nan
- energy_fault_detector.data_preprocessing.low_unique_value_filter
- energy_fault_detector.data_splitting
- energy_fault_detector.evaluation
- energy_fault_detector.evaluation.care2compare
Care2CompareDatasetCare2CompareDataset.get_event_info()Care2CompareDataset.iter_datasets()Care2CompareDataset.format_event_dataset()Care2CompareDataset.iter_formatted_datasets()Care2CompareDataset.load_event_dataset()Care2CompareDataset.load_and_format_event_dataset()Care2CompareDataset.iter_train_datasets_per_asset()Care2CompareDataset.update_c2c_config()Care2CompareDataset.format_event_dataset()Care2CompareDataset.get_event_info()Care2CompareDataset.iter_datasets()Care2CompareDataset.iter_formatted_datasets()Care2CompareDataset.iter_train_datasets_per_asset()Care2CompareDataset.load_and_format_event_dataset()Care2CompareDataset.load_event_dataset()Care2CompareDataset.update_c2c_config()
- energy_fault_detector.evaluation.care_score
CAREScoreCAREScore.calculate_avg_accuracy()CAREScore.calculate_avg_coverage()CAREScore.calculate_avg_earliness()CAREScore.calculate_reliability()CAREScore.create_ground_truth()CAREScore.evaluate_event()CAREScore.evaluated_eventsCAREScore.get_final_score()CAREScore.load_evaluated_events()CAREScore.save_evaluated_events()
- energy_fault_detector.evaluation.care2compare
- energy_fault_detector.quick_fault_detection
- energy_fault_detector.root_cause_analysis
- energy_fault_detector.threshold_selectors
- energy_fault_detector.utils
- energy_fault_detector.fault_detector
FaultDetectorFaultDetector.anomaly_scoreFaultDetector.autoencoderFaultDetector.threshold_selectorFaultDetector.data_preprocessorFaultDetector.save_timestampsFaultDetector.fit()FaultDetector.predict()FaultDetector.predict_anomalies()FaultDetector.predict_anomaly_score()FaultDetector.preprocess_train_data()FaultDetector.run_root_cause_analysis()FaultDetector.tune()
- energy_fault_detector.main
OptionsOptions.angle_featuresOptions.automatic_optimizationOptions.csv_test_data_pathOptions.enable_debug_plotsOptions.features_to_excludeOptions.min_anomaly_lengthOptions.status_data_column_nameOptions.status_label_confidence_percentageOptions.status_mappingOptions.time_column_nameOptions.train_test_column_nameOptions.train_test_mapping
load_options_from_yaml()main()
- energy_fault_detector.registration