Skip to content

bioamla.protocols

The interfaces bioamla accepts from callers — progress and cancel hooks, and audio models — as typing.Protocols. Implement them structurally; nothing needs to inherit from them. The module imports only the standard library.

The interfaces bioamla accepts from callers, in one place.

Functions across the domains take callables and objects supplied by the caller: progress and cancel hooks, and audio models. This module names each of those interfaces as a :class:typing.Protocol, so a downstream package (a GUI, a plugin) can see what it has to implement and type-check its implementation. Any object of the right shape satisfies a protocol; nothing needs to inherit from it.

It imports only the standard library, so import bioamla.protocols works on the base install, with no extras.

  • :class:OnProgress: the on_progress(completed, total) hook on batch functions.
  • :class:OnBatchProgress: progress hook of :func:bioamla.common.progress.process_batch.
  • :class:OnTrainProgress and :class:ShouldStop: the progress and cancel hooks of :func:bioamla.ml.train_ast.
  • :class:OnDownloadProgress: the per-file hook of the iNaturalist downloader.
  • :class:AudioModel: what an audio classification model provides. :class:bioamla.ml.base.BaseAudioModel subclasses satisfy it.
  • :class:ExperimentTracker: where :func:bioamla.ml.train_ast reports a run. :mod:bioamla.ml.trackers has adapters for MLflow and TensorBoard.

OnProgress

Bases: Protocol

Called after each item of a batch: completed of total items are done.

Raising from the hook stops the batch, which is how callers cancel one.

OnBatchProgress

Bases: Protocol

Called with a :class:~bioamla.common.progress.BatchProgress after each item.

OnTrainProgress

Bases: Protocol

Called with a :class:~bioamla.ml.TrainProgress as training advances.

ShouldStop

Bases: Protocol

Polled during a long run; returning True cancels it.

OnDownloadProgress

Bases: Protocol

Called after each download: completed of total, with the item's name.

AudioModel

Bases: Protocol

An audio classification model: load weights, then predict on audio or embed it.

:class:bioamla.ml.base.BaseAudioModel subclasses (ASTModel, ...) satisfy this. isinstance(obj, AudioModel) checks that the members exist, not their signatures.

backend property

backend: ModelBackend

Which model family this is.

classes property

classes: list[str]

Class labels, in output order.

num_classes property

num_classes: int

Number of class labels.

load

load(model_path: str, **kwargs: Any) -> AudioModel

Load weights from a path or hub id and return the model.

predict

predict(
    audio: Any, sample_rate: int | None = None
) -> list[PredictionResult]

Predict on a file path, an array or a tensor (sample_rate for raw audio).

predict_file

predict_file(filepath: str) -> list[PredictionResult]

Predict on an audio file.

extract_embeddings

extract_embeddings(
    audio: Any,
    sample_rate: int | None = None,
    layer: str | None = None,
) -> Any

Return embeddings (a numpy array) from layer, or the model's default.

ExperimentTracker

Bases: Protocol

Receives a training run's parameters, metrics, artifacts and outcome.

:func:bioamla.ml.train_ast calls :meth:start_run once before training, :meth:log_metrics each time the trainer logs, :meth:log_artifact for files it writes on success, and :meth:end_run exactly once, with the real outcome, including a cancelled or failed run. A tracker that raises is logged as a warning and doesn't stop training.

start_run

start_run(name: str | None, params: dict[str, Any]) -> None

Begin a run. params are the run's settings; values may be nested.

log_metrics

log_metrics(step: int, metrics: dict[str, float]) -> None

Record metrics (loss, eval_accuracy, ...) at an optimizer step.

log_artifact

log_artifact(path: str) -> None

Record a file the run produced.

end_run

end_run(status: RunStatus) -> None

Close the run as finished, cancelled or failed.