1
1
#!/usr/bin/env python
2
- # -*- coding: utf-8 -*--
3
2
4
3
# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
5
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
6
5
7
- import fsspec
8
- import numpy as np
9
6
import os
10
- import pandas as pd
11
7
import tempfile
12
8
import time
13
9
from abc import ABC , abstractmethod
14
- from sklearn import linear_model
15
10
from typing import Tuple
16
11
12
+ import fsspec
13
+ import numpy as np
14
+ import pandas as pd
15
+ from sklearn import linear_model
16
+
17
17
from ads .common .object_storage_details import ObjectStorageDetails
18
18
from ads .opctl import logger
19
19
from ads .opctl .operator .lowcode .anomaly .const import OutputColumns , SupportedMetrics
20
20
from ads .opctl .operator .lowcode .anomaly .utils import _build_metrics_df , default_signer
21
21
from ads .opctl .operator .lowcode .common .utils import (
22
- human_time_friendly ,
23
- enable_print ,
24
22
disable_print ,
23
+ enable_print ,
24
+ human_time_friendly ,
25
25
write_data ,
26
26
)
27
- from . anomaly_dataset import AnomalyDatasets , AnomalyOutput , TestData
27
+
28
28
from ..const import NonTimeADSupportedModels , SupportedModels
29
29
from ..operator_config import AnomalyOperatorConfig , AnomalyOperatorSpec
30
+ from .anomaly_dataset import AnomalyDatasets , AnomalyOutput , TestData
30
31
31
32
32
33
class AnomalyOperatorBaseModel (ABC ):
@@ -53,16 +54,18 @@ def __init__(self, config: AnomalyOperatorConfig, datasets: AnomalyDatasets):
53
54
54
55
def generate_report (self ):
55
56
"""Generates the report."""
56
- import report_creator as rc
57
57
import matplotlib .pyplot as plt
58
+ import report_creator as rc
58
59
59
60
start_time = time .time ()
60
61
# fallback using sklearn oneclasssvm when the sub model _build_model fails
61
62
try :
62
63
anomaly_output = self ._build_model ()
63
64
except Exception as e :
65
+ logger .warn (f"Found exception: { e } " )
64
66
if self .spec .datetime_column :
65
67
anomaly_output = self ._fallback_build_model ()
68
+ raise e
66
69
67
70
elapsed_time = time .time () - start_time
68
71
@@ -98,7 +101,7 @@ def generate_report(self):
98
101
ax .grid ()
99
102
ax .plot (time_col , y , color = "black" )
100
103
for i , index in enumerate (anomaly_col ):
101
- if anomaly_col [ i ] == 1 :
104
+ if index == 1 :
102
105
ax .scatter (time_col [i ], y [i ], color = "red" , marker = "o" )
103
106
plt .xlabel (date_column )
104
107
plt .ylabel (col )
@@ -173,7 +176,9 @@ def _test_data_evaluate_metrics(self, anomaly_output, test_data, elapsed_time):
173
176
174
177
for cat in anomaly_output .list_categories ():
175
178
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
+ )
177
182
178
183
test_data_i = test_data .get_data_for_series (cat )
179
184
@@ -250,7 +255,7 @@ def _save_report(
250
255
if ObjectStorageDetails .is_oci_path (unique_output_dir ):
251
256
storage_options = default_signer ()
252
257
else :
253
- storage_options = dict ()
258
+ storage_options = {}
254
259
255
260
# report-creator html report
256
261
with tempfile .TemporaryDirectory () as temp_dir :
@@ -304,12 +309,11 @@ def _fallback_build_model(self):
304
309
Fallback method for the sub model _build_model method.
305
310
"""
306
311
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."
309
314
)
310
315
311
316
date_column = self .spec .datetime_column .name
312
- dataset = self .datasets
313
317
314
318
anomaly_output = AnomalyOutput (date_column = date_column )
315
319
0 commit comments