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).
Classes:
| Name | Description |
|---|---|
FormatResult |
Container for a formatted value and its display metadata. |
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. |
json_to_table |
Print a JSON dict or list of dicts as a rich table. |
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. |
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. |
FormatResult
Bases: NamedTuple
Container for a formatted value and its display metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
|
None
|
justify
|
str
|
|
'left'
|
style
|
str | None
|
|
None
|
Source code in src/aimbat/utils/_table.py
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
json_to_table
json_to_table(
data: dict[str, Any] | list[dict[str, Any]],
title: str | None = None,
formatters: Mapping[
str, Callable[[Any], str | FormatResult]
]
| None = None,
skip_keys: list[str] | None = None,
column_order: list[str] | None = None,
column_kwargs: Mapping[str, Mapping[str, Any]]
| None = None,
common_column_kwargs: Mapping[str, Any] | None = None,
short: bool = True,
) -> None
Print a JSON dict or list of dicts as a rich table. Headers are kept as-is from keys unless renamed via column_kwargs.
Source code in src/aimbat/utils/_table.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
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
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. |