Skip to content

Commit 077ec39

Browse files
committed
intial commit rcf
1 parent 7f8b70d commit 077ec39

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

ads/opctl/operator/lowcode/anomaly/const.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class NonTimeADSupportedModels(str, metaclass=ExtendedEnumMeta):
2121

2222
OneClassSVM = "oneclasssvm"
2323
IsolationForest = "isolationforest"
24+
RandomCutForest = "randomcutforest"
2425
# TODO : Add DBScan
2526
# DBScan = "dbscan"
2627

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

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .base_model import AnomalyOperatorBaseModel
1616
from .isolationforest import IsolationForestOperatorModel
1717
from .oneclasssvm import OneClassSVMOperatorModel
18+
from .randomcutforest import RandomCutForestOperatorModel
1819

1920

2021
class UnSupportedModelError(Exception):
@@ -52,6 +53,7 @@ class AnomalyOperatorModelFactory:
5253
_NonTime_MAP = {
5354
NonTimeADSupportedModels.OneClassSVM: OneClassSVMOperatorModel,
5455
NonTimeADSupportedModels.IsolationForest: IsolationForestOperatorModel,
56+
NonTimeADSupportedModels.RandomCutForest: RandomCutForestOperatorModel,
5557
# TODO: Add DBScan model for non time based anomaly
5658
# NonTimeADSupportedModels.DBScan: DBScanOperatorModel,
5759
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
4+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5+
6+
import numpy as np
7+
import pandas as pd
8+
9+
from ads.common.decorator.runtime_dependency import runtime_dependency
10+
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns
11+
12+
from .anomaly_dataset import AnomalyOutput
13+
from .base_model import AnomalyOperatorBaseModel
14+
15+
16+
class RandomCutForestOperatorModel(AnomalyOperatorBaseModel):
17+
"""
18+
Class representing Random Cut Forest Anomaly Detection operator model.
19+
"""
20+
21+
@runtime_dependency(
22+
module="rrcf",
23+
err_msg=(
24+
"Please run `pip install rrcf` to "
25+
"install the required dependencies for RandomCutForest."
26+
),
27+
)
28+
def _build_model(self) -> AnomalyOutput:
29+
from rrcf import RCTree
30+
31+
model_kwargs = self.spec.model_kwargs
32+
# map the output as per anomaly dataset class, 1: outlier, 0: inlier
33+
self.outlier_map = {1: 0, -1: 1}
34+
35+
anomaly_output = AnomalyOutput(date_column="index")
36+
#TODO: PDB
37+
import pdb
38+
39+
pdb.set_trace()
40+
41+
for target, df in self.datasets.full_data_dict.items():
42+
model = RCTree(**model_kwargs)
43+
model.fit(df)
44+
y_pred = model.predict(df)
45+
y_pred = np.vectorize(self.outlier_map.get)(y_pred)
46+
47+
scores = model.score_samples(df)
48+
49+
index_col = df.columns[0]
50+
51+
anomaly = pd.DataFrame(
52+
{index_col: df[index_col], OutputColumns.ANOMALY_COL: y_pred}
53+
).reset_index(drop=True)
54+
score = pd.DataFrame(
55+
{"index": df[index_col], OutputColumns.SCORE_COL: scores}
56+
).reset_index(drop=True)
57+
58+
anomaly_output.add_output(target, anomaly, score)
59+
60+
return anomaly_output
61+
62+
def _generate_report(self):
63+
"""Generates the report."""
64+
import report_creator as rc
65+
66+
other_sections = [
67+
rc.Heading("Selected Models Overview", level=2),
68+
rc.Text(
69+
"The following tables provide information regarding the chosen model."
70+
),
71+
]
72+
73+
model_description = rc.Text(
74+
"The Random Cut Forest (RCF) is an unsupervised machine learning algorithm that is used for anomaly detection."
75+
" It works by building an ensemble of binary trees (random cut trees) and using them to compute anomaly scores for data points."
76+
)
77+
78+
return (
79+
model_description,
80+
other_sections,
81+
)

ads/opctl/operator/lowcode/anomaly/schema.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ spec:
363363
- auto
364364
- oneclasssvm
365365
- isolationforest
366+
- randomcutforest
366367
meta:
367368
description: "The model to be used for anomaly detection"
368369

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ anomaly = [
175175
"autots",
176176
"oracledb",
177177
"report-creator==1.0.9",
178+
"rrcf==0.4.4",
179+
"scikit-learn"
178180
]
179181
recommender = [
180182
"oracle_ads[opctl]",

test-requirements-operators.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-r test-requirements.txt
22
-e ".[forecast]"
3+
-e ".[anomaly]"
34
-e ".[recommender]"
45
-e ".[feature-store-marketplace]"
56
plotly

0 commit comments

Comments
 (0)