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: theon_progress(completed, total)hook on batch functions. - :class:
OnBatchProgress: progress hook of :func:bioamla.common.progress.process_batch. - :class:
OnTrainProgressand :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.BaseAudioModelsubclasses satisfy it. - :class:
ExperimentTracker: where :func:bioamla.ml.train_astreports a run. :mod:bioamla.ml.trackershas 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.
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).
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.