@@ -48,7 +48,12 @@ def generate_report(self):
48
48
import matplotlib .pyplot as plt
49
49
50
50
start_time = time .time ()
51
- anomaly_output = self ._build_model ()
51
+ # fallback using sklearn oneclasssvm when the sub model _build_model fails
52
+ try :
53
+ anomaly_output = self ._build_model ()
54
+ except Exception as e :
55
+ anomaly_output = self ._fallback_build_model ()
56
+
52
57
elapsed_time = time .time () - start_time
53
58
54
59
summary_metrics = None
@@ -318,6 +323,44 @@ def _save_report(
318
323
f"generated and placed to the: { output_dir } ."
319
324
)
320
325
326
+ def _fallback_build_model (self ):
327
+ """
328
+ Fallback method for the sub model _build_model method.
329
+ """
330
+ logger .warn (
331
+ "The build_model method has failed for the model: {}. "
332
+ "A fallback model will be built." .format (self .spec .model )
333
+ )
334
+ from sklearn import linear_model
335
+ date_column = self .spec .datetime_column .name
336
+ dataset = self .datasets
337
+
338
+ full_data_dict = dataset .full_data_dict
339
+
340
+ anomaly_output = AnomalyOutput (date_column = date_column )
341
+
342
+ # map the output as per anomaly dataset class, 1: outlier, 0: inlier
343
+ outlier_map = {1 : 0 , - 1 : 1 }
344
+
345
+ # Iterate over the full_data_dict items
346
+ for target , df in full_data_dict .items ():
347
+ est = linear_model .SGDOneClassSVM (random_state = 42 )
348
+ est .fit (df [target ].values .reshape (- 1 , 1 ))
349
+ y_pred = np .vectorize (outlier_map .get )(est .predict (df [target ].values .reshape (- 1 , 1 )))
350
+ scores = est .score_samples (df [target ].values .reshape (- 1 , 1 ))
351
+
352
+ anomaly = pd .DataFrame ({
353
+ date_column : df [date_column ],
354
+ OutputColumns .ANOMALY_COL : y_pred
355
+ }).reset_index (drop = True )
356
+ score = pd .DataFrame ({
357
+ date_column : df [date_column ],
358
+ OutputColumns .SCORE_COL : [item for item in scores ]
359
+ }).reset_index (drop = True )
360
+ anomaly_output .add_output (target , anomaly , score )
361
+
362
+ return anomaly_output
363
+
321
364
@abstractmethod
322
365
def _generate_report (self ):
323
366
"""
0 commit comments