|
13 | 13 | import fsspec
|
14 | 14 | import pandas as pd
|
15 | 15 | import numpy as np
|
| 16 | +from sklearn import linear_model |
16 | 17 |
|
17 | 18 | from ads.opctl import logger
|
18 | 19 |
|
@@ -61,7 +62,12 @@ def generate_report(self):
|
61 | 62 | import matplotlib.pyplot as plt
|
62 | 63 |
|
63 | 64 | start_time = time.time()
|
64 |
| - anomaly_output = self._build_model() |
| 65 | + # fallback using sklearn oneclasssvm when the sub model _build_model fails |
| 66 | + try: |
| 67 | + anomaly_output = self._build_model() |
| 68 | + except Exception as e: |
| 69 | + anomaly_output = self._fallback_build_model() |
| 70 | + |
65 | 71 | elapsed_time = time.time() - start_time
|
66 | 72 |
|
67 | 73 | summary_metrics = None
|
@@ -298,6 +304,42 @@ def _save_report(
|
298 | 304 | f"generated and placed to the: {unique_output_dir}."
|
299 | 305 | )
|
300 | 306 |
|
| 307 | + def _fallback_build_model(self): |
| 308 | + """ |
| 309 | + Fallback method for the sub model _build_model method. |
| 310 | + """ |
| 311 | + logger.warn( |
| 312 | + "The build_model method has failed for the model: {}. " |
| 313 | + "A fallback model will be built.".format(self.spec.model) |
| 314 | + ) |
| 315 | + |
| 316 | + date_column = self.spec.datetime_column.name |
| 317 | + dataset = self.datasets |
| 318 | + |
| 319 | + anomaly_output = AnomalyOutput(date_column=date_column) |
| 320 | + |
| 321 | + # map the output as per anomaly dataset class, 1: outlier, 0: inlier |
| 322 | + self.outlier_map = {1: 0, -1: 1} |
| 323 | + |
| 324 | + # Iterate over the full_data_dict items |
| 325 | + for target, df in self.datasets.full_data_dict.items(): |
| 326 | + est = linear_model.SGDOneClassSVM(random_state=42) |
| 327 | + est.fit(df[target].values.reshape(-1, 1)) |
| 328 | + y_pred = np.vectorize(self.outlier_map.get)(est.predict(df[target].values.reshape(-1, 1))) |
| 329 | + scores = est.score_samples(df[target].values.reshape(-1, 1)) |
| 330 | + |
| 331 | + anomaly = pd.DataFrame({ |
| 332 | + date_column: df[date_column], |
| 333 | + OutputColumns.ANOMALY_COL: y_pred |
| 334 | + }).reset_index(drop=True) |
| 335 | + score = pd.DataFrame({ |
| 336 | + date_column: df[date_column], |
| 337 | + OutputColumns.SCORE_COL: scores |
| 338 | + }).reset_index(drop=True) |
| 339 | + anomaly_output.add_output(target, anomaly, score) |
| 340 | + |
| 341 | + return anomaly_output |
| 342 | + |
301 | 343 | @abstractmethod
|
302 | 344 | def _generate_report(self):
|
303 | 345 | """
|
|
0 commit comments