mixle.utils.optsutil module

map_to_integers(x, val_map)[source]

Map sequence of type T to integers.

Parameters:
  • x (Sequence[T]) – Sequence of type T to be mapped to integers.

  • val_map (Dict[T, int]) – Dictionary mappings for type T to unique integer values.

Returns:

Returns x mapped to a list of integers.

Return type:

list[int]

get_inv_map(val_map)[source]

Obtain the inverse dictionary mapping of key/value pairs.

Parameters:

val_map (Dict[T1, T]) – Dictionary mapping keys to values.

Returns:

Inverse mapping of val_map (value -> key).

Return type:

dict[T1, T]

text_file(f)[source]

Open a file and split by newline.

Args

f: File to be read-in and parsed.

Returns:

List of strings split on newline character.

Return type:

list[str]

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.

Parameters:

x (Sequence[Tuple[T, T1]]) – A sequence of tuples of key and value pairs.

Returns:

Dictionary mapping keys to list of values for respective keys.

Return type:

dict[T, list[T1]]

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.

Parameters:
  • f (Callable[[T], T1]) – Function mapping type T to its group ‘key’ of type T1.

  • x (Sequence[T]) – Sequence of values to be grouped.

Returns:

Dictionary mapping group id ‘keys’ (type T1) to Lists of values (type T).

Return type:

dict[T1, list[T]]

count_by_value(x)[source]

Count the number of observations of a given value in arg ‘x’.

Parameters:

x (Sequence[T]) – A sequence of type T or numpy array of type T.

Returns:

Dictionary mapping value (type T) to value-count.

Return type:

dict[T, int]

flat_map(f, x)[source]

Map values of x under mapping f().

Parameters:
  • f (Callable[[T], T1]) – Maps values of x to sequence of type T1.

  • x (Sequence[T]) – Seuquence to be mapped under f.

Returns:

List of mapped type T1.

Return type:

list[T1]

least_occurring(x, count=None, percent=None, keep_freq=True)[source]
Parameters: