Skip to content

Commit b730050

Browse files
samet-akcayashwinvaidya17Ashwin Vaidya
authored
Merge main to feature/benchmarking (#1236)
* Configure readthedocs via `.readthedocs.yaml` file (#1229) * 🚚 Refactor Benchmarking Script (#1216) * New printing stuff * Remove dead code + address codacy issues * Refactor try/except + log to comet/wandb during runs * pre-commit error * third-party configuration --------- Co-authored-by: Ashwin Vaidya <[email protected]> * Update CODEOWNERS --------- Co-authored-by: Ashwin Vaidya <[email protected]> Co-authored-by: Ashwin Vaidya <[email protected]>
1 parent cf71338 commit b730050

File tree

9 files changed

+458
-290
lines changed

9 files changed

+458
-290
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/src/anomalib/models/cflow @ashwinvaidya17
3535
/src/anomalib/models/csflow @ashwinvaidya17
3636
/src/anomalib/models/dfkde @djdameln
37-
/src/anomalib/models/dfm @djdameln @nahuja-intel
37+
/src/anomalib/models/dfm @djdameln
3838
/src/anomalib/models/draem @djdameln
3939
/src/anomalib/models/fastflow @samet-akcay
4040
/src/anomalib/models/ganomaly @ashwinvaidya17

.readthedocs.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Required
2+
version: 2
3+
4+
# Set the OS, Python version and other tools you might need
5+
build:
6+
os: ubuntu-22.04
7+
tools:
8+
python: "3.11"
9+
# You can also specify other tool versions:
10+
# nodejs: "20"
11+
# rust: "1.70"
12+
# golang: "1.20"
13+
14+
# Build documentation in the "docs/" directory with Sphinx
15+
sphinx:
16+
configuration: docs/source/conf.py
17+
# You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs
18+
# builder: "dirhtml"
19+
# Fail on all warnings to avoid broken references
20+
# fail_on_warning: true
21+
22+
# Optionally build your docs in additional formats such as PDF and ePub
23+
formats:
24+
- pdf
25+
- epub
26+
27+
# Optional but recommended, declare the Python requirements required
28+
# to build your documentation
29+
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
30+
python:
31+
install:
32+
- requirements: requirements/docs.txt

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ target-version = "py38"
5858
# Allow imports relative to the "src" and "tests" directories.
5959
src = ["src", "tests"]
6060

61+
[tool.ruff.isort]
62+
known-third-party = ["wandb", "comet_ml"]
63+
6164
[tool.ruff.mccabe]
6265
# Unlike Flake8, default to a complexity level of 10.
6366
max-complexity = 10

requirements/loggers.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
comet-ml>=3.31.7
22
gradio>=2.9.4
33
tensorboard
4-
wandb==0.12.17
4+
wandb>=0.13.0
55
GitPython
66
ipykernel

src/anomalib/utils/sweep/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
from .config import flatten_sweep_params, get_run_config, set_in_nested_config
77
from .helpers import get_openvino_throughput, get_sweep_callbacks, get_torch_throughput
8+
from .utils import Status, exception_wrapper, redirect_output
89

910
__all__ = [
11+
"exception_wrapper",
12+
"flatten_sweep_params",
1013
"get_run_config",
11-
"set_in_nested_config",
1214
"get_sweep_callbacks",
1315
"get_openvino_throughput",
1416
"get_torch_throughput",
15-
"flatten_sweep_params",
17+
"set_in_nested_config",
18+
"redirect_output",
19+
"Status",
1620
]

src/anomalib/utils/sweep/utils.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""Additional utils for sweep."""
2+
3+
# Copyright (C) 2023 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
7+
import functools
8+
import io
9+
import logging
10+
import sys
11+
from enum import Enum
12+
from typing import Any, Callable
13+
14+
logger = logging.getLogger(__name__)
15+
16+
17+
def redirect_output(func: Callable) -> Callable[..., dict[str, Any]]:
18+
"""Decorator to redirect output of the function.
19+
20+
Args:
21+
func (function): Hides output of this function.
22+
23+
Raises:
24+
Exception: Incase the execution of function fails, it raises an exception.
25+
26+
Returns:
27+
object of the called function
28+
"""
29+
30+
@functools.wraps(func)
31+
def wrapper(*args, **kwargs) -> dict[str, Any]:
32+
std_out = sys.stdout
33+
sys.stdout = buf = io.StringIO()
34+
try:
35+
value = func(*args, **kwargs)
36+
logger.info(buf.getvalue())
37+
logger.info(value)
38+
except Exception as exp:
39+
logger.exception(
40+
"Error occurred while computing benchmark %s. Buffer: %s." "\n Method %s, args %s, kwargs %s",
41+
exp,
42+
buf.getvalue(),
43+
func,
44+
args,
45+
kwargs,
46+
)
47+
value = {}
48+
sys.stdout = std_out
49+
return value
50+
51+
return wrapper
52+
53+
54+
class Status(str, Enum):
55+
"""Status of the benchmarking run."""
56+
57+
SUCCESS = "success"
58+
FAILED = "failed"
59+
60+
61+
class Result:
62+
def __init__(self, value: Any, status=Status.SUCCESS):
63+
self.value = value
64+
self.status = status
65+
66+
def __bool__(self):
67+
return self.status == Status.SUCCESS
68+
69+
70+
def exception_wrapper(func: Callable) -> Callable[..., Result]:
71+
"""Wrapper method to handle exceptions.
72+
73+
Args:
74+
func (function): Function to be wrapped.
75+
76+
Raises:
77+
Exception: Incase the execution of function fails, it raises an exception.
78+
79+
Example:
80+
>>> @exception_wrapper
81+
... def func():
82+
... raise Exception("Exception occurred")
83+
>>> func()
84+
Exception: Exception occurred
85+
86+
Returns:
87+
object of the called function
88+
"""
89+
90+
@functools.wraps(func)
91+
def wrapper(*args, **kwargs) -> Result:
92+
try:
93+
value = Result(value=func(*args, **kwargs))
94+
except Exception as exp:
95+
logger.exception(
96+
"Error occurred while computing benchmark %s. Method %s, args %s, kwargs %s",
97+
exp,
98+
func,
99+
args,
100+
kwargs,
101+
)
102+
value = Result(False, Status.FAILED)
103+
return value
104+
105+
return wrapper

0 commit comments

Comments
 (0)