metrics_as_scores.tools package
Submodules
metrics_as_scores.tools.funcs module
This module contains few functions that are required in multiple other modules and classes of Metrics As Scores.
- metrics_as_scores.tools.funcs.nonlinspace(start: float, stop: float, num: int, func: ~typing.Callable[[float], float] = <function <lambda>>) NDArray[Any, Any] [source]
Used to create a non-linear space. This is useful to sample with greater detail in some place, and with lesser detail in another.
- start:
float
The start of the space.
- stop:
float
The stop (end) of the space.
- num:
int
The number of samples.
- func:
Callable[[float], float]
A function that, given a linear x, creates a non-linear y. The default is
lambda x: 1. - .9 * square(x)
.
- start:
- metrics_as_scores.tools.funcs.flatten_dict(d: dict[str, Any], parent_key: str = '', sep: str = '_') dict[str, Any] [source]
Recursively flatten a dictionary, creating merged key names. Courtesy of https://stackoverflow.com/a/6027615/1785141
- class metrics_as_scores.tools.funcs.Interpolator(xp: ~nptyping.ndarray.NDArray[~nptyping.base_meta_classes.Shape[*], ~numpy.float64], fp: ~nptyping.ndarray.NDArray[~nptyping.base_meta_classes.Shape[*], ~numpy.float64], left: ~typing.Optional[float] = None, right: ~typing.Optional[float] = None)[source]
Bases:
object
Helper class used for transforming and interpolating a CDF into a PPF.
- metrics_as_scores.tools.funcs.cdf_to_ppf(cdf: ~typing.Callable[[float], float], x: ~nptyping.ndarray.NDArray[~nptyping.base_meta_classes.Shape[*], ~numpy.float64], cdf_samples: int = 5000, y_left: ~typing.Optional[float] = None, y_right: ~typing.Optional[float] = None) Union[Interpolator, Callable[[float], float]] [source]
Adapted implementation from
statsmodels.distributions.empirical_distribution.monotone_fn_inverter()
that handles out-of-bounds values explicitly. Also, we assume thatfn
is vectorized.
- metrics_as_scores.tools.funcs.natsort(s: str) int [source]
Natural string sorting.
Courtesy of https://stackoverflow.com/a/16090640/1785141
- metrics_as_scores.tools.funcs.transform_to_MAS_dataset(df: DataFrame, group_col: str, feature_cols: list[str]) DataFrame [source]
Transforms a “typical” data frame into the format that is used by Metrics As Scores. A typical data frame is one in which there is a column with the group, and a dedicated column for each feature’s observations. That kind of data frame, however, implies we have the same amount of observations per feature. The format used by Metrics As Scores stacks the observations and adds an extra ordinal column for the feature. This way, we allow an arbitrary number of observations per feature.
- df:
pd.DataFrame
The original data frame that has one or more feature columns and one group designated group column.
- group_col:
str
The name of the group column.
- feature_cols:
list[str]
A non-empty list of features’ names to include.
- Raises:
Exception: If the number of given feature columns is zero or if any of the given features or the group is not present as a column in the data frame.
- Returns:
A data frame with the 3 columns
Feature
(ordinal; name of the feature column from the original dataset),Group
(ordinal, thegroup_col
repeatedn
times, wheren
is the length of the given data frame), andValue
(the numeric observation).
- df:
metrics_as_scores.tools.lazy module
This module contains a single class, SelfResetLazy
. It is used to
lazily load pre-generated densities in the web application. Since these files
can get large quickly, the SelfResetLazy
allows to free memory if
its loaded value has expired.
- class metrics_as_scores.tools.lazy.SelfResetLazy(fn_create_val: Callable[[], T], fn_destroy_val: Optional[Callable[[T], Any]] = None, reset_after: Optional[float] = None)[source]
Bases:
Generic
[T
]This class lazily loads and automatically destroys its value after some timeout, so that subsequent requests to it force the factory to produce a new instance. All of its semantics are thread-safe (except for the explicitly named volatile members).
- __init__(fn_create_val: Callable[[], T], fn_destroy_val: Optional[Callable[[T], Any]] = None, reset_after: Optional[float] = None) None [source]
- fn_create_val:
Callable[[], T]
Function that produces the desired value.
- fn_destroy_val:
Callable[[T], Any]
Function that will be given the value before it is de-referenced here.
- reset_after:
float
Amount of time, in seconds, after which the produced value ought to be destroyed.
- fn_create_val:
- property reset_after
Thread-safe getter for the reset-after property.
- unset_value()[source]
Thread-safe method to destroy a previously produced value. If a value is present, it is passed to py:meth:fn_destroy_val() first.
- property has_value_volatile: bool
Checks, in a volatile manner, if a value is present.
- property has_value: bool
Thread-safe getter for checking if a value is present.
- property value_volatile: Union[None, T]
Volatile getter for the may-not-present value.
- property value: T
Thread-safe getter for the value. If no value is present, one will be produced and this getter blocks until then.
- property value_future: Future[T]
Returns an awaitable
Future
that will hold the value once it is available.
Module contents
This package contains tools that are commonly used in Metrics As Scores.