11#!/usr/bin/env python
2- # -*- coding: utf-8 -*--
32
43# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
54# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
65
7- import fsspec
8- import numpy as np
96import os
10- import pandas as pd
117import tempfile
128import time
139from abc import ABC , abstractmethod
14- from sklearn import linear_model
1510from typing import Tuple
1611
12+ import fsspec
13+ import numpy as np
14+ import pandas as pd
15+ from sklearn import linear_model
16+
1717from ads .common .object_storage_details import ObjectStorageDetails
1818from ads .opctl import logger
1919from ads .opctl .operator .lowcode .anomaly .const import OutputColumns , SupportedMetrics
2020from ads .opctl .operator .lowcode .anomaly .utils import _build_metrics_df , default_signer
2121from ads .opctl .operator .lowcode .common .utils import (
22- human_time_friendly ,
23- enable_print ,
2422 disable_print ,
23+ enable_print ,
24+ human_time_friendly ,
2525 write_data ,
2626)
27- from . anomaly_dataset import AnomalyDatasets , AnomalyOutput , TestData
27+
2828from ..const import NonTimeADSupportedModels , SupportedModels
2929from ..operator_config import AnomalyOperatorConfig , AnomalyOperatorSpec
30+ from .anomaly_dataset import AnomalyDatasets , AnomalyOutput , TestData
3031
3132
3233class AnomalyOperatorBaseModel (ABC ):
@@ -53,16 +54,18 @@ def __init__(self, config: AnomalyOperatorConfig, datasets: AnomalyDatasets):
5354
5455 def generate_report (self ):
5556 """Generates the report."""
56- import report_creator as rc
5757 import matplotlib .pyplot as plt
58+ import report_creator as rc
5859
5960 start_time = time .time ()
6061 # fallback using sklearn oneclasssvm when the sub model _build_model fails
6162 try :
6263 anomaly_output = self ._build_model ()
6364 except Exception as e :
65+ logger .warn (f"Found exception: { e } " )
6466 if self .spec .datetime_column :
6567 anomaly_output = self ._fallback_build_model ()
68+ raise e
6669
6770 elapsed_time = time .time () - start_time
6871
@@ -98,7 +101,7 @@ def generate_report(self):
98101 ax .grid ()
99102 ax .plot (time_col , y , color = "black" )
100103 for i , index in enumerate (anomaly_col ):
101- if anomaly_col [ i ] == 1 :
104+ if index == 1 :
102105 ax .scatter (time_col [i ], y [i ], color = "red" , marker = "o" )
103106 plt .xlabel (date_column )
104107 plt .ylabel (col )
@@ -173,7 +176,9 @@ def _test_data_evaluate_metrics(self, anomaly_output, test_data, elapsed_time):
173176
174177 for cat in anomaly_output .list_categories ():
175178 output = anomaly_output .category_map [cat ][0 ]
176- date_col = self .spec .datetime_column .name if self .spec .datetime_column else "index"
179+ date_col = (
180+ self .spec .datetime_column .name if self .spec .datetime_column else "index"
181+ )
177182
178183 test_data_i = test_data .get_data_for_series (cat )
179184
@@ -250,7 +255,7 @@ def _save_report(
250255 if ObjectStorageDetails .is_oci_path (unique_output_dir ):
251256 storage_options = default_signer ()
252257 else :
253- storage_options = dict ()
258+ storage_options = {}
254259
255260 # report-creator html report
256261 with tempfile .TemporaryDirectory () as temp_dir :
@@ -304,12 +309,11 @@ def _fallback_build_model(self):
304309 Fallback method for the sub model _build_model method.
305310 """
306311 logger .warn (
307- "The build_model method has failed for the model: {}. "
308- "A fallback model will be built." . format ( self . spec . model )
312+ f "The build_model method has failed for the model: { self . spec . model } . "
313+ "A fallback model will be built."
309314 )
310315
311316 date_column = self .spec .datetime_column .name
312- dataset = self .datasets
313317
314318 anomaly_output = AnomalyOutput (date_column = date_column )
315319
0 commit comments