Utils#
Utility modules supporting anomaly detection workflows, path handling, and configuration helpers.
API Reference#
Utility functions and helpers for anomaly detection.
- This module provides various utility functions and helpers for:
File downloading and management
Metric calculation and evaluation
Anomaly map computation and processing
Result visualization and plotting
API deprecation management
The utilities ensure consistent behavior across the library and provide common functionality used by multiple components.
Example
>>> from anomalib.utils.visualization import ImageVisualizer
>>> # Create visualizer
>>> visualizer = ImageVisualizer()
>>> # Generate visualization
>>> vis_result = visualizer.visualize(image=img, pred_mask=mask)
- The module is organized into submodules for different types of utilities:
download: Functions for downloading datasets and modelsmetrics: Implementations of evaluation metricsmap: Tools for generating and processing anomaly mapsvisualization: Classes for visualizing detection resultsdeprecation: Utilities for managing API deprecation
- anomalib.utils.deprecate(__obj=None, *, since=None, remove=None, use=None, reason=None, args=None, warning_category=<class 'DeprecationWarning'>)#
Mark a function, class, or keyword argument as deprecated.
- Overloads:
__obj (_T) → _T
since (str | None), remove (str | None), use (str | None), reason (str | None), args (dict[str, str | None] | None), warning_category (type[Warning]) → Callable[[_T], _T]
This decorator will cause a warning to be emitted when the function is called, class is instantiated, or deprecated keyword arguments are used. If args are passed, no function deprecation will be shown.
- Parameters:
__obj (
Any) – The function or class to be deprecated. If provided, the decorator is used without arguments. If None, the decorator is used with arguments.since (
str|None) – Version when the function/class/arg was deprecated (e.g. “2.1.0”). If not provided, no deprecation version will be shown in the warning message.remove (
str|None) – Version when the function/class/arg will be removed. If not provided, no removal version will be shown in the warning message.use (
str|None) – Name of the replacement function/class/arg. If not provided, no replacement suggestion will be shown in the warning message.reason (
str|None) – Additional reason for the deprecation. If not provided, no reason will be shown in the warning message.args (
dict[str,str|None] |None) – Mapping of deprecated argument names to their replacements. If not provided, no argument deprecation warning will be shown. If provided, function deprecation will not be show.warning_category (
type[Warning]) – Type of warning to emit (default: DeprecationWarning). Can be DeprecationWarning, FutureWarning, PendingDeprecationWarning, etc.
- Returns:
Decorated function/class that emits a deprecation warning when used.
Example
>>> # Basic deprecation without any information >>> @deprecate ... def old_function(): ... pass
>>> # Deprecation with version info and replacement class >>> @deprecate(since="2.1.0", remove="2.5.0", use="NewClass") ... class OldClass: ... pass
>>> # Deprecation with only removal version >>> @deprecate(remove="2.5.0") ... def another_function(): ... pass
>>> # Deprecation with only deprecation version >>> @deprecate(since="2.1.0") ... def yet_another_function(): ... pass
>>> # Deprecation with reason and custom warning category >>> @deprecate(since="2.1.0", reason="Performance improvements available", warning_category=FutureWarning) ... def old_slow_function(): ... pass
>>> # Deprecation of specific argument(s) >>> @deprecate(args={"old_param": "new_param"}, since="2.1.0", remove="3.0.0") ... def my_function(new_param=None, old_param=None): ... # Handle the mapping logic yourself ... if old_param is not None and new_param is None: ... new_param = old_param ... # Rest of function logic ... pass
>>> @deprecate(args={"old_param": "new_param", "two_param": "two_new_param"}, since="2.1.0") >>> def my_args_function(one_param, two_new_param=None, new_param=None, two_param=None, old_param=None): ... # Handle each mapping individually ... if old_param is not None and new_param is None: ... new_param = old_param ... if two_param is not None and two_new_param is None: ... two_new_param = two_param ... # Rest of function logic ... pass >>> @deprecate(args={"old_arg": "new_arg"}, since="2.1.0", remove="3.0.0") >>> class MyClass: >>> def __init__(self, new_arg=None, old_arg=None): >>> if new_arg is None and old_arg is not None: >>> new_arg = old_arg
- anomalib.utils.get_nested_attr(obj, attr_path, default=None)#
Safely retrieves a nested attribute from an object.
This function helps reduce boilerplate code when working with nested attributes, by allowing you to retrieve a nested attribute with a single function call instead of multiple nested calls to getattr.
- Parameters:
obj (
Any) – The object to retrieve the attribute from.attr_path (
str) – A dot-separated string representing the attribute path.default (
Any|None) – The default value to return if any attribute in the path is missing.
- Return type:
Any- Returns:
The value of the nested attribute, or default if any attribute in the path is missing.
Example
>>> class A: ... def __init__(self, b): ... self.b = b >>> >>> class B: ... def __init__(self, c): ... self.c = c >>> >>> class C: ... def __init__(self, d): ... self.d = d >>> >>> d = 42 >>> c = C(d) >>> b = B(c) >>> a = A(b) >>> get_nested_attr(a, "b.c.d") # 42 >>> # this is equivalent to: >>> # getattr(getattr(getattr(a, "b", None), "c", None), "value", None) >>> >>> get_nested_attr(a, "b.c.foo") # None >>> get_nested_attr(a, "b.c.foo", "bar") # "bar" >>> get_nested_attr(a, "b.d.c") # None