aimbat.utils
Miscellaneous helpers for AIMBAT.
Covers four areas:
- JSON — render JSON data as Rich tables (
json_to_table). - Sample data — download and delete the bundled sample dataset
(
download_sampledata,delete_sampledata). - Styling — shared Rich/table style helpers (
make_table). - UUIDs — look up model records by short UUID prefix (
get_by_uuid).
Modules:
| Name | Description |
|---|---|
formatters |
|
Functions:
| Name | Description |
|---|---|
delete_sampledata |
Delete sample data. |
download_sampledata |
Download sample data. |
get_title_map |
Creates a mapping from field names to their 'title' metadata. |
mean_and_sem |
Return the mean and standard error of the mean (SEM) for a list of numeric values, ignoring None values. |
mean_and_sem_timedelta |
Return (mean, sem) for a list of pd.Timedelta values. |
rel |
Cast a SQLModel relationship attribute to |
string_to_uuid |
Determine a UUID from a string containing the first few characters. |
uuid_shortener |
Calculates the shortest unique prefix for a UUID, returning with dashes. |
delete_sampledata
download_sampledata
download_sampledata(force: bool = False) -> None
Download sample data.
Source code in src/aimbat/utils/_sampledata.py
get_title_map
cached
Creates a mapping from field names to their 'title' metadata.
Source code in src/aimbat/utils/_pydantic.py
mean_and_sem
Return the mean and standard error of the mean (SEM) for a list of numeric values, ignoring None values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float | None]
|
List of numeric values (float or int) or None. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float | None, float | None]
|
A tuple containing the mean and SEM of the input data, both as floats or None if not computable. |
Source code in src/aimbat/utils/_maths.py
mean_and_sem_timedelta
Return (mean, sem) for a list of pd.Timedelta values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Sequence[Timedelta]
|
List of pd.Timedelta values. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Timedelta | None, Timedelta | None]
|
|
Source code in src/aimbat/utils/_maths.py
rel
Cast a SQLModel relationship attribute to QueryableAttribute for use with selectinload.
SQLModel types relationship fields as their Python collection type (e.g. list[Foo]),
but SQLAlchemy's selectinload expects a QueryableAttribute. This helper performs
the cast to satisfy mypy without requiring per-call # type: ignore comments.
Source code in src/aimbat/utils/_sqlalchemy.py
string_to_uuid
string_to_uuid(
session: Session,
id: str,
aimbat_class: type[AimbatTypes],
custom_error: str | None = None,
) -> UUID
Determine a UUID from a string containing the first few characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Database session. |
required |
id
|
str
|
Input string to find UUID for. |
required |
aimbat_class
|
type[AimbatTypes]
|
Aimbat class to use to find UUID. |
required |
custom_error
|
str | None
|
Overrides the default error message. |
None
|
Returns:
| Type | Description |
|---|---|
UUID
|
The full UUID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the UUID could not be determined. |
Source code in src/aimbat/utils/_uuid.py
uuid_shortener
uuid_shortener(
session: Session,
aimbat_obj: T | type[T],
min_length: int = 2,
str_uuid: str | None = None,
) -> str
Calculates the shortest unique prefix for a UUID, returning with dashes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
An active SQLModel/SQLAlchemy session. |
required |
aimbat_obj
|
T | type[T]
|
Either an instance of a SQLModel or the SQLModel class itself. |
required |
min_length
|
int
|
The starting character length for the shortened ID. |
2
|
str_uuid
|
str | None
|
The full UUID string. Required only if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The shortest unique prefix string, including hyphens where applicable. |
Source code in src/aimbat/utils/_uuid.py
formatters
Functions:
| Name | Description |
|---|---|
fmt_bool |
Format a boolean as |
fmt_depth_km |
Format a depth value in metres as kilometres with one decimal place. |
fmt_flip |
Format a boolean flip flag as |
fmt_float |
Format a float to 3 decimal places, or |
fmt_timedelta |
Format a Timedelta as total seconds to 5 decimal places, or |
fmt_timestamp |
Format a timestamp as |
fmt_bool
fmt_depth_km
Format a depth value in metres as kilometres with one decimal place.
fmt_flip
Format a boolean flip flag as ↕ (True) or empty string (False).
fmt_float
Format a float to 3 decimal places, or — for None/NaN.
Source code in src/aimbat/utils/formatters.py
fmt_timedelta
Format a Timedelta as total seconds to 5 decimal places, or — for None.
Source code in src/aimbat/utils/formatters.py
fmt_timestamp
Format a timestamp as YYYY-MM-DD HH:MM:SS, or — for missing values.