Skip to content

Commit 7267190

Browse files
committed
add to test cases for ad
1 parent 6fbd612 commit 7267190

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

ads/opctl/operator/lowcode/anomaly/model/base_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _test_data_evaluate_metrics(self, anomaly_output, test_data, elapsed_time):
173173

174174
for cat in anomaly_output.list_categories():
175175
output = anomaly_output.category_map[cat][0]
176-
date_col = self.spec.datetime_column.name
176+
date_col = self.spec.datetime_column.name if self.spec.datetime_column else "index"
177177

178178
test_data_i = test_data.get_data_for_series(cat)
179179

ads/opctl/operator/lowcode/anomaly/model/isolationforest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def _build_model(self) -> AnomalyOutput:
3535

3636
for target, df in self.datasets.full_data_dict.items():
3737
model = IsolationForest(**model_kwargs)
38-
model.fit(df[self.spec.target_column].values.reshape(-1, 1))
38+
model.fit(df)
3939
y_pred = np.vectorize(self.outlier_map.get)(
40-
model.predict(df[self.spec.target_column].values.reshape(-1, 1))
40+
model.predict(df)
4141
)
4242

4343
scores = model.score_samples(
44-
df[self.spec.target_column].values.reshape(-1, 1)
44+
df
4545
)
4646

4747
index_col = df.columns[0]

ads/opctl/operator/lowcode/anomaly/model/oneclasssvm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def _build_model(self) -> AnomalyOutput:
3535

3636
for target, df in self.datasets.full_data_dict.items():
3737
model = OneClassSVM(**model_kwargs)
38-
model.fit(df[self.spec.target_column].values.reshape(-1, 1))
38+
model.fit(df)
3939
y_pred = np.vectorize(self.outlier_map.get)(
40-
model.predict(df[self.spec.target_column].values.reshape(-1, 1))
40+
model.predict(df)
4141
)
4242

4343
scores = model.score_samples(
44-
df[self.spec.target_column].values.reshape(-1, 1)
44+
df
4545
)
4646

4747
index_col = df.columns[0]

tests/operators/anomaly/test_anomaly_simple.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

6+
from ads.opctl.operator.lowcode.anomaly.const import NonTimeADSupportedModels
67
import yaml
78
import subprocess
89
import pandas as pd
@@ -16,7 +17,7 @@
1617
from ads.opctl.operator.cmd import run
1718

1819

19-
MODELS = ["autots"] # "automlx",
20+
MODELS = ["autots", "oneclasssvm", "isolationforest"] # "automlx",
2021

2122
# Mandatory YAML parameters
2223
TEMPLATE_YAML = {
@@ -71,7 +72,8 @@ def test_artificial_big(model):
7172
df_i = pd.DataFrame(
7273
d1, columns=[TARGET_COLUMN, "extra reg 1", "extra reg 2"]
7374
) # columns=[f"sensor {i}", f"extra reg {i}-1", f"extra reg {i}-2"] # Uncomment for wide format
74-
df_i[DATETIME_COLUMN] = yr_in_30_min
75+
if model not in NonTimeADSupportedModels.values():
76+
df_i[DATETIME_COLUMN] = yr_in_30_min
7577
df_i[TARGET_CATEGORY_COLUMN] = f"GHNI007894032{i}"
7678
all_data.append(df_i)
7779

@@ -90,7 +92,10 @@ def test_artificial_big(model):
9092
yaml_i["spec"]["output_directory"]["url"] = output_dirname
9193
yaml_i["spec"]["target_column"] = TARGET_COLUMN
9294
yaml_i["spec"]["target_category_columns"] = [TARGET_CATEGORY_COLUMN]
93-
yaml_i["spec"]["datetime_column"]["name"] = DATETIME_COLUMN
95+
if model in NonTimeADSupportedModels.values():
96+
del yaml_i["spec"]["datetime_column"]
97+
else:
98+
yaml_i["spec"]["datetime_column"]["name"] = DATETIME_COLUMN
9499

95100
# run(yaml_i, backend="operator.local", debug=False)
96101

@@ -131,6 +136,8 @@ def test_artificial_small(model):
131136
yaml_i["spec"]["input_data"]["url"] = input_data
132137
yaml_i["spec"]["output_directory"]["url"] = output_dirname
133138
yaml_i["spec"]["contamination"] = 0.3
139+
if model in NonTimeADSupportedModels.values():
140+
del yaml_i["spec"]["datetime_column"]
134141

135142
# run(yaml_i, debug=False)
136143

@@ -162,8 +169,9 @@ def test_validation(model):
162169
np.concatenate([np.zeros(100), np.ones(2), np.zeros(100)], axis=0),
163170
columns=["anomaly"],
164171
)
165-
d = d.reset_index().rename({"index": "ds"}, axis=1)
166-
anomaly_col["ds"] = d["ds"]
172+
if model not in NonTimeADSupportedModels.values():
173+
d = d.reset_index().rename({"index": "ds"}, axis=1)
174+
anomaly_col["ds"] = d["ds"]
167175
v = d.copy()
168176
v["anomaly"] = anomaly_col["anomaly"]
169177
with tempfile.TemporaryDirectory() as tmpdirname:
@@ -185,6 +193,8 @@ def test_validation(model):
185193
yaml_i["spec"]["test_data"] = {"url": test_data}
186194
yaml_i["spec"]["output_directory"]["url"] = output_dirname
187195
yaml_i["spec"]["contamination"] = 0.05
196+
if model in NonTimeADSupportedModels.values():
197+
del yaml_i["spec"]["datetime_column"]
188198

189199
# run(yaml_i, backend="operator.local", debug=False)
190200
with open(anomaly_yaml_filename, "w") as f:
@@ -207,7 +217,10 @@ def test_load_datasets(model, data_dict):
207217
yaml_i = deepcopy(TEMPLATE_YAML)
208218
yaml_i["spec"]["model"] = model
209219
yaml_i["spec"]["input_data"]["url"] = data_dict["url"]
210-
yaml_i["spec"]["datetime_column"]["name"] = data_dict["dt_col"]
220+
if model in NonTimeADSupportedModels.values():
221+
del yaml_i["spec"]["datetime_column"]
222+
else:
223+
yaml_i["spec"]["datetime_column"]["name"] = data_dict["dt_col"]
211224
yaml_i["spec"]["output_directory"]["url"] = output_dirname
212225

213226
# run(yaml_i, backend="operator.local", debug=False)

0 commit comments

Comments
 (0)