mixle.utils.optsutil module¶
Small collection and grouping utilities shared by legacy Mixle helpers.
These functions provide deterministic mapping, inverse mapping, grouping, counting, and reduce-by-key operations for examples and older estimator code that work over plain Python sequences.
- map_to_integers(x, val_map)[source]
Map sequence of type T to integers.
- get_inv_map(val_map, *, multi=False)[source]
Obtain an inverse dictionary mapping without silently losing collisions.
- Parameters:
val_map (Dict[T1, T]) – Dictionary mapping keys to values.
multi (bool) – Return every source key in an insertion-ordered list for each value. When false, repeated values make the mapping non-invertible and raise
ValueError.
- Returns:
Inverse mapping of val_map (value -> key), or a value -> keys multi-map when
multi=True.- Return type:
- text_file(f, *, encoding='utf-8')[source]
Open a file and split by newline.
- Args
f: File to be read-in and parsed.
- reduce_by_key(f, x)[source]
Reduce sequence of tuple of key value pairs under grouping function f.
- Parameters:
f (Callable[[T1, T1], T1]) – Function for reducing keys.
x (Sequence[Tuple[T, T1]) – A sequence of key/value pairs.
- Returns:
Dictionary mapping key types T to value types T1.
- Return type:
dict[T, T1]
- sum_by_key(x)[source]
Sum values and return dictionary of items with their respective summed values.
- Parameters:
x (Sequence[Tuple[T, T1]]) – A sequence of tuples of key and value pairs.
- Returns:
Dictionary of keys with summed values.
- Return type:
dict[T, T1]
- group_by_key(x)[source]
Group keys and return dictionary of items with their respective values aggregated as a list.
- group_by(f, x)[source]
Maps values in x to key from mapping f. Dictionary mapping keys to list of grouped values in x is returned.
- count_by_value(x)[source]
Count the number of observations of a given value in arg ‘x’.