tea_tasting.utils
#
Useful functions and classes.
check_scalar(value, name='value', *, typ=None, ge=None, gt=None, le=None, lt=None, ne=None, in_=None)
#
Check if a scalar parameter meets specified type and value constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
R
|
Parameter value. |
required |
name |
str
|
Parameter name. |
'value'
|
typ |
object
|
Acceptable data types. |
None
|
ge |
object
|
If not |
None
|
gt |
object
|
If not |
None
|
le |
object
|
If not |
None
|
lt |
object
|
If not |
None
|
ne |
object
|
If not |
None
|
in_ |
object
|
If not |
None
|
Returns:
Type | Description |
---|---|
R
|
Parameter value. |
Source code in src/tea_tasting/utils.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
auto_check(value, name)
#
Automatically check a parameter's type and value based on its name.
The following parameter names are supported: "alpha"
, "alternative"
,
"confidence_level"
, "correction"
, "equal_var"
, "n_obs"
,
"n_resamples"
, "power"
, "ratio"
, "use_t"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
R
|
Parameter value. |
required |
name |
str
|
Parameter name. |
required |
Returns:
Type | Description |
---|---|
R
|
Parameter value. |
Source code in src/tea_tasting/utils.py
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 |
|
format_num(val, sig=3, *, pct=False, nan='-', inf='∞', fixed_point_range=(0.001, 10000000), thousands_sep=None, decimal_point=None)
#
Format a number according to specified formatting rules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
float | int | None
|
Number to format. |
required |
sig |
int
|
Number of significant digits. |
3
|
pct |
bool
|
If |
False
|
nan |
str
|
Replacement for |
'-'
|
inf |
str
|
Replacement for infinite values. |
'∞'
|
fixed_point_range |
tuple[float | None, float | None]
|
The range within which the number is formatted
as fixed point.
Number outside of the range is formatted as exponential.
|
(0.001, 10000000)
|
thousands_sep |
str | None
|
Thousands separator. If |
None
|
decimal_point |
str | None
|
Decimal point symbol. If |
None
|
Returns:
Type | Description |
---|---|
str
|
Formatted number. |
Source code in src/tea_tasting/utils.py
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 |
|
get_and_format_num(data, key)
#
Get and format dictionary value.
Formatting rules:
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
or is greater than or equal to10_000_000
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {upper_bound}]"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict[str, object]
|
Dictionary. |
required |
key |
str
|
Key. |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted value. |
Source code in src/tea_tasting/utils.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
DictsReprMixin
#
Bases: ABC
Representation and conversion of a sequence of dictionaries.
Default formatting rules:
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
or is greater than or equal to10_000_000
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {upper_bound}]"
.
to_dicts()
abstractmethod
#
Convert the object to a sequence of dictionaries.
Source code in src/tea_tasting/utils.py
294 295 296 |
|
to_arrow()
#
Convert the object to a PyArrow Table.
Source code in src/tea_tasting/utils.py
298 299 300 |
|
to_pandas()
#
Convert the object to a Pandas DataFrame.
Source code in src/tea_tasting/utils.py
302 303 304 305 |
|
to_polars()
#
Convert the object to a Polars DataFrame.
Source code in src/tea_tasting/utils.py
307 308 309 310 |
|
to_pretty_dicts(keys=None, formatter=get_and_format_num)
#
Convert the object to a list of dictionaries with formatted values.
Default formatting rules:
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
or is greater than or equal to10_000_000
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {upper_bound}]"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Sequence[str] | None
|
Keys to convert. If a key is not defined in the dictionary
it's assumed to be |
None
|
formatter |
Callable[[dict[str, object], str], str]
|
Custom formatter function. It should accept a dictionary of metric result attributes and an attribute name, and return a formatted attribute value. |
get_and_format_num
|
Returns:
Type | Description |
---|---|
list[dict[str, str]]
|
List of dictionaries with formatted values. |
Source code in src/tea_tasting/utils.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
to_string(keys=None, formatter=get_and_format_num)
#
Convert the object to a string.
Default formatting rules:
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
or is greater than or equal to10_000_000
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {upper_bound}]"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Sequence[str] | None
|
Keys to convert. If a key is not defined in the dictionary
it's assumed to be |
None
|
formatter |
Callable[[dict[str, object], str], str]
|
Custom formatter function. It should accept a dictionary of metric result attributes and an attribute name, and return a formatted attribute value. |
get_and_format_num
|
Returns:
Type | Description |
---|---|
str
|
A table with results rendered as string. |
Source code in src/tea_tasting/utils.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
|
to_html(keys=None, formatter=get_and_format_num, *, indent=None)
#
Convert the object to HTML.
Default formatting rules:
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
or is greater than or equal to10_000_000
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {upper_bound}]"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Sequence[str] | None
|
Keys to convert. If a key is not defined in the dictionary
it's assumed to be |
None
|
formatter |
Callable[[dict[str, object], str], str]
|
Custom formatter function. It should accept a dictionary of metric result attributes and an attribute name, and return a formatted attribute value. |
get_and_format_num
|
indent |
str | None
|
Whitespace to insert for each indentation level. If |
None
|
Returns:
Type | Description |
---|---|
str
|
A table with results rendered as HTML. |
Source code in src/tea_tasting/utils.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
ReprMixin
#
A mixin class that provides a method for generating a string representation.
Representation string is generated based on parameters values saved in attributes.
div(numer, denom, fill_zero_div='auto')
#
Perform division, providing specified results for cases of division by zero.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numer |
float | int
|
Numerator. |
required |
denom |
float | int
|
Denominator. |
required |
fill_zero_div |
float | int | Literal['auto']
|
Result if denominator is zero. |
'auto'
|
Returns:
Type | Description |
---|---|
float | int
|
Result of the division. |
If fill_zero_div
is equal "auto"
, return:
inf
if numerator is greater than0
,nan
if numerator is equal to or less than0
.
Source code in src/tea_tasting/utils.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
|
Float
#
Bases: _NumericBase
, float
Float that gracefully handles division by zero errors.
Int
#
Bases: _NumericBase
, int
Integer that gracefully handles division by zero errors.
numeric(value, fill_zero_div='auto')
#
Float or integer that gracefully handles division by zero errors.
Source code in src/tea_tasting/utils.py
639 640 641 642 643 644 645 646 647 648 649 650 651 |
|