Shared validators and converters for container attrs classes.
Functions:
| Name | Description |
|---|---|
convert_corr_to_dict | Convert correlation specification to dictionary format. |
is_choice_str | Build a validator for a string constrained to a fixed set of choices. |
is_conf_level | Validate that a value is a normalized confidence level in (0, 1). |
is_ndarray | Validate that a value is a numpy ndarray. |
is_nonnegative_int | Validate that a value is a non-negative integer. |
is_optional_conf_level | Validate that a value is a normalized confidence level or None. |
is_optional_int | Validate that a value is an int or None. |
is_optional_ndarray | Validate that a value is a numpy ndarray or None. |
is_optional_positive_int | Validate that a value is a positive integer or None. |
is_optional_sparse_csc | Validate that a value is a scipy.sparse.csc_matrix or None. |
is_optional_str | Validate that a value is a string or None. |
is_optional_str_key_dict | Validate that a value is a dict with string keys or None. |
is_optional_tuple_of_str | Validate that a value is a tuple of strings or None. |
is_positive_int | Validate that a value is a positive integer. |
is_tuple_of_str | Validate that a value is a tuple of strings. |
normalize_conf_level | Normalize conf_level to a float in (0, 1). |
normalize_optional_conf_level | Normalize an optional confidence level. |
to_factor_dict | Convert factor dict values from lists to tuples. |
to_frozen_dict | Ensure dict values are tuples where applicable. |
to_optional_tuple | Convert an optional list of strings to a tuple. |
to_tuple | Convert list to tuple for immutability. |
to_tuple_of_tuples | Convert dict values to tuples for immutability. |
validate_correlations | Validate correlation values are in [-1, 1]. |
validate_sigma | Validate sigma is non-negative. |
validate_slope_sds | Validate slope SDs are non-negative. |
Functions¶
convert_corr_to_dict¶
convert_corr_to_dict(corr: float | dict[tuple[str, str], float], slope_sds: dict[str, float]) -> dict[tuple[str, str], float]Convert correlation specification to dictionary format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corr | float | dict[tuple[str, str], float] | Correlation as single value or dict. | required |
slope_sds | dict[str, float] | Dictionary of slope SDs (to determine term names). | required |
Returns:
| Type | Description |
|---|---|
dict[tuple[str, str], float] | Dictionary mapping term pairs to correlations. |
is_choice_str¶
is_choice_str(choices: tuple[str, ...])Build a validator for a string constrained to a fixed set of choices.
is_conf_level¶
is_conf_level(instance: object, attribute: object, value: object) -> NoneValidate that a value is a normalized confidence level in (0, 1).
is_ndarray¶
is_ndarray(instance: object, attribute: object, value: object) -> NoneValidate that a value is a numpy ndarray.
is_nonnegative_int¶
is_nonnegative_int(instance: object, attribute: object, value: object) -> NoneValidate that a value is a non-negative integer.
is_optional_conf_level¶
is_optional_conf_level(instance: object, attribute: object, value: object) -> NoneValidate that a value is a normalized confidence level or None.
is_optional_int¶
is_optional_int(instance: object, attribute: object, value: object) -> NoneValidate that a value is an int or None.
is_optional_ndarray¶
is_optional_ndarray(instance: object, attribute: object, value: object) -> NoneValidate that a value is a numpy ndarray or None.
is_optional_positive_int¶
is_optional_positive_int(instance: object, attribute: object, value: object) -> NoneValidate that a value is a positive integer or None.
is_optional_sparse_csc¶
is_optional_sparse_csc(instance: object, attribute: object, value: object) -> NoneValidate that a value is a scipy.sparse.csc_matrix or None.
is_optional_str¶
is_optional_str(instance: object, attribute: object, value: object) -> NoneValidate that a value is a string or None.
is_optional_str_key_dict¶
is_optional_str_key_dict(instance: object, attribute: object, value: object) -> NoneValidate that a value is a dict with string keys or None.
is_optional_tuple_of_str¶
is_optional_tuple_of_str(instance: object, attribute: object, value: object) -> NoneValidate that a value is a tuple of strings or None.
is_positive_int¶
is_positive_int(instance: object, attribute: object, value: object) -> NoneValidate that a value is a positive integer.
is_tuple_of_str¶
is_tuple_of_str(instance: object, attribute: object, value: object) -> NoneValidate that a value is a tuple of strings.
normalize_conf_level¶
normalize_conf_level(conf_level: float | int | str) -> floatNormalize conf_level to a float in (0, 1).
Float in (0, 1): returned as-is (e.g. 0.95)
Float in (0, 1): returned as-is (e.g. 0.95)
Int or float in (1, 100): divided by 100 (e.g. 95 -> 0.95)
String with optional
%suffix: parsed and normalized (e.g."95%"-> 0.95,"0.95"-> 0.95)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conf_level | float | int | str | Confidence level in any supported format. | required |
Returns:
| Type | Description |
|---|---|
float | Confidence level as a float strictly between 0 and 1 (exclusive). |
normalize_optional_conf_level¶
normalize_optional_conf_level(conf_level: float | int | str | None) -> float | NoneNormalize an optional confidence level.
to_factor_dict¶
to_factor_dict(val: Any) -> dict[str, tuple[str, ...]]Convert factor dict values from lists to tuples.
to_frozen_dict¶
to_frozen_dict(val: Any) -> dictEnsure dict values are tuples where applicable.
to_optional_tuple¶
to_optional_tuple(value: tuple[str, ...] | list[str] | None) -> tuple[str, ...] | NoneConvert an optional list of strings to a tuple.
to_tuple¶
to_tuple(value: tuple[str, ...] | list[str]) -> tuple[str, ...]Convert list to tuple for immutability.
to_tuple_of_tuples¶
to_tuple_of_tuples(value: dict[str, tuple[str, ...] | list[str]] | None) -> dict[str, tuple[str, ...]]Convert dict values to tuples for immutability.
validate_correlations¶
validate_correlations(instance: Any, attribute: Any, value: dict[tuple[str, str], float]) -> NoneValidate correlation values are in [-1, 1].
validate_sigma¶
validate_sigma(instance: Any, attribute: Any, value: float) -> NoneValidate sigma is non-negative.
validate_slope_sds¶
validate_slope_sds(instance: Any, attribute: Any, value: dict[str, float]) -> NoneValidate slope SDs are non-negative.