Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up formatting warnings #1001

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions ads/opctl/operator/lowcode/anomaly/model/anomaly_merlion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import importlib
import logging

import numpy as np
import pandas as pd
import report_creator as rc
from merlion.post_process.threshold import AggregateAlarms
from merlion.utils import TimeSeries

Expand All @@ -21,6 +23,8 @@
from .anomaly_dataset import AnomalyOutput
from .base_model import AnomalyOperatorBaseModel

logging.getLogger("root").setLevel(logging.WARNING)


class AnomalyMerlionOperatorModel(AnomalyOperatorBaseModel):
"""Class representing Merlion Anomaly Detection operator model."""
Expand Down Expand Up @@ -84,7 +88,7 @@ def _build_model(self) -> AnomalyOutput:
for target, df in self.datasets.full_data_dict.items():
data = df.set_index(date_column)
data = TimeSeries.from_pd(data)
for model_name, (model_config, model) in model_config_map.items():
for _, (model_config, model) in model_config_map.items():
if self.spec.model == SupportedModels.BOCPD:
model_config = model_config(**self.spec.model_kwargs)
else:
Expand Down Expand Up @@ -115,7 +119,7 @@ def _build_model(self) -> AnomalyOutput:
y_pred = (y_pred.to_pd().reset_index()["anom_score"] > 0).astype(
int
)
except Exception as e:
except Exception:
y_pred = (
scores["anom_score"]
> np.percentile(
Expand All @@ -135,15 +139,12 @@ def _build_model(self) -> AnomalyOutput:
OutputColumns.SCORE_COL: scores["anom_score"],
}
).reset_index(drop=True)
# model_objects[model_name].append(model)

anomaly_output.add_output(target, anomaly, score)
return anomaly_output

def _generate_report(self):
"""Genreates a report for the model."""
import report_creator as rc

other_sections = [
rc.Heading("Selected Models Overview", level=2),
rc.Text(
Expand Down
20 changes: 12 additions & 8 deletions ads/opctl/operator/lowcode/anomaly/model/automlx.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*--

# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging

import pandas as pd
import report_creator as rc

from ads.common.decorator.runtime_dependency import runtime_dependency
from .anomaly_dataset import AnomalyOutput
from ads.opctl import logger
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns

from .anomaly_dataset import AnomalyOutput
from .base_model import AnomalyOperatorBaseModel
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns

logging.getLogger("root").setLevel(logging.WARNING)


class AutoMLXOperatorModel(AnomalyOperatorBaseModel):
Expand All @@ -25,16 +30,17 @@ class AutoMLXOperatorModel(AnomalyOperatorBaseModel):
),
)
def _build_model(self) -> pd.DataFrame:
from automlx import init
import logging

import automlx

try:
init(
automlx.init(
engine="ray",
engine_opts={"ray_setup": {"_temp_dir": "/tmp/ray-temp"}},
loglevel=logging.CRITICAL,
)
except Exception as e:
except Exception:
logger.info("Ray already initialized")
date_column = self.spec.datetime_column.name
anomaly_output = AnomalyOutput(date_column=date_column)
Expand Down Expand Up @@ -73,8 +79,6 @@ def _build_model(self) -> pd.DataFrame:
return anomaly_output

def _generate_report(self):
import report_creator as rc

"""The method that needs to be implemented on the particular model level."""
other_sections = [
rc.Heading("Selected Models Overview", level=2),
Expand Down
9 changes: 6 additions & 3 deletions ads/opctl/operator/lowcode/anomaly/model/autots.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*--

# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging

import report_creator as rc

from ads.common.decorator.runtime_dependency import runtime_dependency
from ads.opctl import logger
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns
Expand All @@ -12,6 +15,8 @@
from .anomaly_dataset import AnomalyOutput
from .base_model import AnomalyOperatorBaseModel

logging.getLogger("root").setLevel(logging.WARNING)


class AutoTSOperatorModel(AnomalyOperatorBaseModel):
"""Class representing AutoTS Anomaly Detection operator model."""
Expand Down Expand Up @@ -91,8 +96,6 @@ def _build_model(self) -> AnomalyOutput:
return anomaly_output

def _generate_report(self):
import report_creator as rc

"""The method that needs to be implemented on the particular model level."""
other_sections = [
rc.Heading("Selected Models Overview", level=2),
Expand Down
26 changes: 19 additions & 7 deletions ads/opctl/operator/lowcode/anomaly/model/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging
import os
import tempfile
import time
Expand All @@ -12,6 +13,7 @@
import fsspec
import numpy as np
import pandas as pd
import report_creator as rc
from sklearn import linear_model

from ads.common.object_storage_details import ObjectStorageDetails
Expand All @@ -33,6 +35,8 @@
from ..operator_config import AnomalyOperatorConfig, AnomalyOperatorSpec
from .anomaly_dataset import AnomalyDatasets, AnomalyOutput, TestData

logging.getLogger("root").setLevel(logging.WARNING)


class AnomalyOperatorBaseModel(ABC):
"""The base class for the anomaly detection operator models."""
Expand All @@ -59,8 +63,8 @@ def __init__(self, config: AnomalyOperatorConfig, datasets: AnomalyDatasets):
def generate_report(self):
"""Generates the report."""
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.max_open_warning': 0})
import report_creator as rc

plt.rcParams.update({"figure.max_open_warning": 0})

start_time = time.time()
# fallback using sklearn oneclasssvm when the sub model _build_model fails
Expand All @@ -84,7 +88,13 @@ def generate_report(self):
anomaly_output, test_data, elapsed_time
)
table_blocks = [
rc.DataTable(df.head(SUBSAMPLE_THRESHOLD) if self.spec.subsample_report_data and len(df) > SUBSAMPLE_THRESHOLD else df, label=col, index=True)
rc.DataTable(
df.head(SUBSAMPLE_THRESHOLD)
if self.spec.subsample_report_data and len(df) > SUBSAMPLE_THRESHOLD
else df,
label=col,
index=True,
)
for col, df in self.datasets.full_data_dict.items()
]
data_table = rc.Select(blocks=table_blocks)
Expand Down Expand Up @@ -144,7 +154,9 @@ def generate_report(self):
else:
figure_blocks = None

blocks.append(rc.Group(*figure_blocks, label=target)) if figure_blocks else None
blocks.append(
rc.Group(*figure_blocks, label=target)
) if figure_blocks else None
plots = rc.Select(blocks)

report_sections = []
Expand All @@ -154,7 +166,9 @@ def generate_report(self):
yaml_appendix = rc.Yaml(self.config.to_dict())
summary = rc.Block(
rc.Group(
rc.Text(f"You selected the **`{self.spec.model}`** model.\n{model_description.text}\n"),
rc.Text(
f"You selected the **`{self.spec.model}`** model.\n{model_description.text}\n"
),
rc.Text(
"Based on your dataset, you could have also selected "
f"any of the models: `{'`, `'.join(SupportedModels.keys() if self.spec.datetime_column else NonTimeADSupportedModels.keys())}`."
Expand Down Expand Up @@ -285,8 +299,6 @@ def _save_report(
test_metrics: pd.DataFrame,
):
"""Saves resulting reports to the given folder."""
import report_creator as rc

unique_output_dir = self.spec.output_directory.url

if ObjectStorageDetails.is_oci_path(unique_output_dir):
Expand Down
19 changes: 9 additions & 10 deletions ads/opctl/operator/lowcode/anomaly/model/isolationforest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*--

# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging

import numpy as np
import pandas as pd
import report_creator as rc

from ads.common.decorator.runtime_dependency import runtime_dependency
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns

from .base_model import AnomalyOperatorBaseModel
from .anomaly_dataset import AnomalyOutput
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns
from .base_model import AnomalyOperatorBaseModel

logging.getLogger("root").setLevel(logging.WARNING)


class IsolationForestOperatorModel(AnomalyOperatorBaseModel):
Expand All @@ -36,13 +40,9 @@ def _build_model(self) -> AnomalyOutput:
for target, df in self.datasets.full_data_dict.items():
model = IsolationForest(**model_kwargs)
model.fit(df)
y_pred = np.vectorize(self.outlier_map.get)(
model.predict(df)
)
y_pred = np.vectorize(self.outlier_map.get)(model.predict(df))

scores = model.score_samples(
df
)
scores = model.score_samples(df)

index_col = df.columns[0]

Expand All @@ -59,7 +59,6 @@ def _build_model(self) -> AnomalyOutput:

def _generate_report(self):
"""Generates the report."""
import report_creator as rc

other_sections = [
rc.Heading("Selected Models Overview", level=2),
Expand Down
21 changes: 10 additions & 11 deletions ads/opctl/operator/lowcode/anomaly/model/oneclasssvm.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*--

# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging

import numpy as np
import pandas as pd
import report_creator as rc

from ads.common.decorator.runtime_dependency import runtime_dependency
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns

from .base_model import AnomalyOperatorBaseModel
from .anomaly_dataset import AnomalyOutput
from ads.opctl.operator.lowcode.anomaly.const import OutputColumns
from .base_model import AnomalyOperatorBaseModel

logging.getLogger("root").setLevel(logging.WARNING)


class OneClassSVMOperatorModel(AnomalyOperatorBaseModel):
Expand All @@ -36,13 +40,9 @@ def _build_model(self) -> AnomalyOutput:
for target, df in self.datasets.full_data_dict.items():
model = OneClassSVM(**model_kwargs)
model.fit(df)
y_pred = np.vectorize(self.outlier_map.get)(
model.predict(df)
)
y_pred = np.vectorize(self.outlier_map.get)(model.predict(df))

scores = model.score_samples(
df
)
scores = model.score_samples(df)

index_col = df.columns[0]

Expand All @@ -54,12 +54,11 @@ def _build_model(self) -> AnomalyOutput:
).reset_index(drop=True)

anomaly_output.add_output(target, anomaly, score)

return anomaly_output

def _generate_report(self):
"""Generates the report."""
import report_creator as rc

other_sections = [
rc.Heading("Selected Models Overview", level=2),
Expand Down
8 changes: 6 additions & 2 deletions ads/opctl/operator/lowcode/anomaly/model/randomcutforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging

import numpy as np
import pandas as pd
import report_creator as rc

from ads.common.decorator.runtime_dependency import runtime_dependency
from ads.opctl import logger
Expand All @@ -13,6 +16,8 @@
from .anomaly_dataset import AnomalyOutput
from .base_model import AnomalyOperatorBaseModel

logging.getLogger("root").setLevel(logging.WARNING)


class RandomCutForestOperatorModel(AnomalyOperatorBaseModel):
"""
Expand All @@ -27,7 +32,7 @@ class RandomCutForestOperatorModel(AnomalyOperatorBaseModel):
),
)
def _build_model(self) -> AnomalyOutput:
from rrcf import RCTree
import rrcf

model_kwargs = self.spec.model_kwargs

Expand Down Expand Up @@ -96,7 +101,6 @@ def _build_model(self) -> AnomalyOutput:

def _generate_report(self):
"""Generates the report."""
import report_creator as rc

other_sections = [
rc.Heading("Selected Models Overview", level=2),
Expand Down
Loading
Loading