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 |
Any
|
Acceptable data types. |
None
|
ge |
Any
|
If not |
None
|
gt |
Any
|
If not |
None
|
le |
Any
|
If not |
None
|
lt |
Any
|
If not |
None
|
ne |
Any
|
If not |
None
|
in_ |
Any
|
If not |
None
|
Returns:
Type | Description |
---|---|
R
|
Parameter value. |
Source code in src/tea_tasting/utils.py
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
format_num(val, sig=3, *, pct=False, nan='-', inf='∞', fixed_point_limit=0.001, 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_limit |
float
|
Limit, below which number is formatted as exponential. |
0.001
|
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
get_and_format_num(data, key)
#
Get and format dictionary value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict[str, Any]
|
Dictionary. |
required |
key |
str
|
Key. |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted 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
, 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}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
PrettyDictsMixin
#
Bases: ABC
Pretty representation 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
, 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}, {lower_bound}]"
.
to_dicts()
abstractmethod
#
to_pandas()
#
to_pretty(keys=None, formatter=get_and_format_num)
#
Convert the object to a Pandas Dataframe with formatted values.
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, Any], 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 |
---|---|
DataFrame
|
Pandas Dataframe 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
, 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}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
to_string(keys=None, formatter=get_and_format_num)
#
Convert the object to a string.
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, Any], 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. |
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
, 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}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
to_html(keys=None, formatter=get_and_format_num)
#
Convert the object to HTML.
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, Any], 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 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
, 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}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
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:
nan
if numerator is equal to0
,inf
if numerator is greater than0
,-inf
if numerator is less than0
.
Source code in src/tea_tasting/utils.py
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.