apache_beam.ml.anomaly.detectors.offline module

class apache_beam.ml.anomaly.detectors.offline.OfflineDetector(*args, **kwargs)[source]

Bases: AnomalyDetector

A offline anomaly detector that uses a provided model handler for scoring.

Parameters:
  • keyed_model_handler – The model handler to use for inference. Requires a KeyModelHandler[Any, Row, PredictionT, Any] instance.

  • run_inference_args – Optional arguments to pass to RunInference

  • **kwargs – Additional keyword arguments to pass to the base AnomalyDetector class.

static score_prediction_adapter(keyed_prediction: tuple[KeyT, PredictionResult]) tuple[KeyT, AnomalyPrediction][source]

Extracts a float score from PredictionResult.inference and wraps it.

Takes a keyed PredictionResult from common ModelHandler output, assumes its inference attribute is a float-convertible score, and returns the key paired with an AnomalyPrediction containing that float score.

Parameters:

keyed_prediction – tuple of (key, PredictionResult). PredictionResult must have an inference attribute supporting float conversion.

Returns:

tuple of (key, AnomalyPrediction) with the extracted score.

Raises:

AssertionError – If PredictionResult.inference doesn’t support float().

static label_prediction_adapter(keyed_prediction: tuple[KeyT, PredictionResult]) tuple[KeyT, AnomalyPrediction][source]

Extracts an integer label from PredictionResult.inference and wraps it.

Takes a keyed PredictionResult, assumes its inference attribute is an integer-convertible label, and returns the key paired with an AnomalyPrediction containing that integer label.

Parameters:

keyed_prediction – tuple of (key, PredictionResult). PredictionResult must have an inference attribute supporting int conversion.

Returns:

tuple of (key, AnomalyPrediction) with the extracted label.

Raises:

AssertionError – If PredictionResult.inference doesn’t support int().

learn_one(x: Row) None[source]

Not implemented since OfflineDetector invokes RunInference directly.

score_one(x: Row) float | None[source]

Not implemented since OfflineDetector invokes RunInference directly.

OfflineDetector__spec_type = 'OfflineDetector'
classmethod from_spec(spec: Spec, _run_init: bool = True) Self | type[Self]

Generate a Specifiable subclass object based on a spec.

Parameters:
  • spec – the specification of a Specifiable subclass object

  • _run_init – whether to call __init__ or not for the initial instantiation

Returns:

the Specifiable subclass object

Return type:

Self

run_original_init() None

Execute the original __init__ method with its saved arguments.

For instances of the Specifiable class, initialization is deferred (lazy initialization). This function forces the execution of the original __init__ method using the arguments captured during the object’s initial instantiation.

classmethod spec_type()
to_spec() Spec

Generate a spec from a Specifiable subclass object.

Returns:

The specification of the instance.

Return type:

Spec

classmethod unspecifiable()