bioamla.client¶
bioamla.client ¶
bioamla.client — work in a bioamla server's workspaces from another machine.
:class:Client talks to a server started with bioamla serve: create and list
workspaces, upload and download files, run operations (waiting for jobs and following
their progress) and manage jobs. Named remotes ([remotes.<name>] in the user's
settings file, with the token in an environment variable) are managed with
:func:add_remote and opened with :func:connect.
It needs only requests, part of the base install, so a laptop without the
server extra or any machine-learning extras can drive a server.
Example
from bioamla.client import add_remote, connect add_remote("lab", "http://lab-server:8765") # token in $BIOAMLA_LAB_TOKEN lab = connect("lab") lab.upload("costa-rica", "rec.wav", "raw/rec.wav") lab.run("costa-rica", "detect.energy", {"file": "raw/rec.wav"})["count"]
Client ¶
Talk to a bioamla server: workspaces, files, operations and jobs.
Every call returns the result of the server's envelope and raises
:class:~bioamla.exceptions.RemoteError (or :class:RemoteNotFound,
:class:RemoteInvalidInput) with the server's message when it refuses or fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The server, e.g. |
required |
token
|
str
|
Its bearer token. |
required |
timeout
|
float | None
|
Seconds to wait for a response ( |
30.0
|
session
|
Any
|
A |
None
|
Example
from bioamla.client import Client server = Client("http://localhost:8765", token) server.run("costa-rica", "detect.energy", {"file": "raw/rec.wav"})
version ¶
version() -> dict[str, Any]
The server's bioamla, API, workspace-format versions and who you are.
operations ¶
operations() -> dict[str, Any]
{"operations": [...], "unavailable": {...}}: what the server can run.
workspaces ¶
workspaces() -> list[dict[str, Any]]
The server's workspaces (workspace is the name to use in calls).
create_workspace ¶
create_workspace(
name: str, title: str | None = None
) -> dict[str, Any]
Create the workspace name on the server.
status ¶
status(ws: str, last_runs: int = 5) -> dict[str, Any]
A workspace's metadata, artifact counts and last runs.
set_setting ¶
set_setting(
ws: str, key: str, value: Any
) -> dict[str, Any]
Set a workspace setting (<operation>.<request field>).
unset_setting ¶
unset_setting(ws: str, key: str) -> bool
Remove a workspace setting; whether it was set.
files ¶
files(
ws: str,
path: str = ".",
recursive: bool = False,
sha256: bool = False,
) -> dict[str, Any]
{"path", "files", "truncated"} for a folder (or one file) in the workspace;
with sha256, each file's SHA-256, computed on the server (None for one it
can't read). Hashing reads every file listed, so the response, once connected,
isn't held to the client's timeout.
upload ¶
upload(
ws: str,
local: str | Path,
remote: str | None = None,
overwrite: bool = False,
) -> dict[str, Any]
Upload the file local to remote (default: its name) in the workspace.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
download ¶
download(
ws: str,
remote: str,
local: str | Path | None = None,
overwrite: bool = False,
) -> Path
Download remote from the workspace. local is the file to write, or a
folder to write it into by its name when it is an existing folder or ends in
/ (default: the current folder). Written whole or not at all.
Returns:
| Type | Description |
|---|---|
Path
|
The file written. |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
If the file exists and |
RemoteError
|
If the server refuses, or the transfer breaks. |
audio_window ¶
audio_window(
ws: str,
path: str,
*,
start: float = 0.0,
duration: float,
sample_rate: int | None = None,
keep_channels: bool = False,
) -> AudioData
A window of the recording path in the workspace, decoded on the server.
Only that part of the file is read and sent, so a few seconds of a long recording
cost a few seconds' worth of bytes. The window is mixed to mono unless
keep_channels, and cut short at the end of the recording.
Returns:
| Name | Type | Description |
|---|---|---|
An |
AudioData
|
class: |
AudioData
|
window's start, and |
|
AudioData
|
say where it came from. |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
If the window is outside the recording or longer than the server allows, or the file isn't audio. |
NotFoundError
|
If the recording isn't in the workspace. |
RemoteError
|
If the server refuses, answers with something other than audio, or the transfer breaks. |
submit ¶
submit(
ws: str,
name: str,
request: dict[str, Any] | None = None,
) -> dict[str, Any]
Post the operation name to the workspace ws.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
For a quick operation, its result; for a job, the job ( |
dict[str, Any]
|
meth: |
run ¶
run(
ws: str,
name: str,
request: dict[str, Any] | None = None,
*,
on_progress: Callable[[int, int], None] | None = None,
on_event: Callable[[dict[str, Any]], None]
| None = None,
typed: bool = False,
workspace_relative: bool = False,
) -> Any
Run the operation name in ws and return its result, waiting for a
job to finish (streaming its events to on_event and its progress to
on_progress).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
typed
|
bool
|
Return the same objects a local run returns (rebuilt from the
server's wire form, see :mod: |
False
|
workspace_relative
|
bool
|
Report paths inside the workspace relative to it (as a local run given relative paths reports them) instead of the server's absolute paths. |
False
|
Raises:
| Type | Description |
|---|---|
UntypedResult
|
With |
JobFailed
|
If the job failed or was cancelled. |
RemoteError
|
If the server refused the request. |
wait ¶
wait(
job_id: str,
*,
on_progress: Callable[[int, int], None] | None = None,
on_event: Callable[[dict[str, Any]], None]
| None = None,
retries: int = 5,
) -> dict[str, Any]
Follow a job's events until it finishes; the finished job. A dropped
stream is resumed after the last event seen (up to retries times in a row).
Raises:
| Type | Description |
|---|---|
JobFailed
|
If it failed or was cancelled. |
RemoteError
|
If the server can't be reached again. |
jobs ¶
jobs(workspace: str | None = None) -> list[dict[str, Any]]
The server's jobs since it started, newest first.
resolve_job_id ¶
resolve_job_id(job_id: str) -> str
The full id of the one job whose id starts with job_id (listings show
them shortened); a full id is returned as is.
Raises:
| Type | Description |
|---|---|
RemoteNotFound
|
If no job, or several, match. |
job ¶
job(job_id: str) -> dict[str, Any]
A job (by id or a unique prefix): status, progress, and its result or error once done.
cancel ¶
cancel(job_id: str) -> dict[str, Any]
Ask a job (by id or a unique prefix) to stop; the job.
events ¶
events(
job_id: str, after: int = 0
) -> Iterator[dict[str, Any]]
A job's events (status, progress, notice, log) as they
happen, from after event after, until it finishes.
Raises:
| Type | Description |
|---|---|
RemoteError
|
If the server can't be reached or the stream breaks. |
JobFailed ¶
Bases: RemoteError
A job finished without a result (failed or cancelled).
Attributes:
| Name | Type | Description |
|---|---|---|
job |
The job as the server reported it. |
RemoteInvalidInput ¶
RemoteNotFound ¶
Bases: RemoteError, NotFoundError
The server has no such workspace, file, job or operation (HTTP 404).
UntypedResult ¶
Bases: RemoteError
A typed run's result couldn't be rebuilt here (e.g. its type needs an extra this
machine doesn't have); :attr:result is its JSON.
Attributes:
| Name | Type | Description |
|---|---|---|
result |
The result as JSON, as an untyped run returns it. |
Remote
dataclass
¶
A server listed in the user's settings.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Its name ( |
url |
str
|
The server's address. |
token_env |
str
|
The environment variable holding its token. |
token ¶
token() -> str
RemoteError ¶
Bases: BioamlaError
A bioamla server couldn't be reached, or refused or failed a request.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
The HTTP status ( |
|
error_type |
The error type the server reported (e.g. |
add_remote ¶
add_remote(
name: str,
url: str,
token_env: str | None = None,
path: str | Path | None = None,
) -> Remote
Add (or replace) the remote name in the user's settings file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Its name: letters, digits, |
required |
url
|
str
|
The server, |
required |
token_env
|
str | None
|
The environment variable that will hold its token (default
|
None
|
path
|
str | Path | None
|
The settings file (default: the user's). |
None
|
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
If the name, URL or variable name isn't usable. |
connect ¶
connect(name: str, **kwargs: Any) -> Any
A :class:~bioamla.client.Client for the remote name.
get_remote ¶
get_remote(
name: str, path: str | Path | None = None
) -> Remote
The remote name.
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If there's no such remote. |
ConfigError
|
If its entry is incomplete. |
remotes ¶
remotes(path: str | Path | None = None) -> list[Remote]
The remotes in the user's settings file (or path), by name. An incomplete
entry is left out (with a logged warning), so it can't hide the others.
Raises:
| Type | Description |
|---|---|
ConfigError
|
If the file can't be read. |
remove_remote ¶
remove_remote(
name: str, path: str | Path | None = None
) -> bool
Remove the remote name; whether it was there.