nevopy.utils package

Submodules

nevopy.utils.deprecation module

Implements a decorator that can be used to mark functions, methods or classes as being deprecated.

nevopy.utils.deprecation.deprecated(decorated_item=None, *, version=None, instructions=None, warn_once=True)

Decorator for marking functions, methods or classes deprecated.

This decorator logs a deprecation warning whenever the decorated item is called. It has the following format:

From {call info}: {function/method/class} (from {module}) is deprecated and will be removed in the future. Instructions for updating: {instructions}

The field {function/method/class} will contain:

  • the function’s name, if the decorated item is a function;

  • the method’s name and the method’s class name, if the decorated item is a method;

  • the class’ name, if the decorated item is a class.

This decorator also edits the docstring of the decorated item. A deprecation notice is added to the start of the docstring.

Parameters
  • decorated_item (Optional[Any]) – The item being decorated. Having this parameter allows the decorator to be used without arguments.

  • instructions (Optional[str]) – Instructions on how to update the code using the deprecated item.

  • version (Optional[str]) – Version in which the item was deprecated.

  • warn_once (bool) – If True, a warning will be printed only the first time the decorated item is called. Otherwise, every call will log a warning.

Return type

Any

Returns

The decorated function, method or class.

nevopy.utils.utils module

This module implements useful utility functions.

class nevopy.utils.utils.Comparable

Bases: object

Indication of a “comparable” type.

class nevopy.utils.utils.MutableWrapper(value)

Bases: Generic[typing._T]

Simple class for wrapping immutable objects so they can be passed by reference to a callable.

nevopy.utils.utils.align_lists(lists, getkey=None, placeholder=None)

Aligns the given lists based on their common values.

Repeated entries within a single list are discarded.

Example

>>> align_lists(([1, 2, 3, 6], [1, 3, 4, 5]))
[[1, 2, 3, None, None, 6], [1, None, 3, 4, 5, None]]
Parameters
  • lists (Iterable[List[_T]]) – Iterable that yields lists containing the objects to be aligned.

  • getkey (Optional[Callable[[_T], Comparable]]) – Optional function to be passed to sorted() to retrieve comparable keys from the objects to be aligned.

  • placeholder (Optional[Any]) – Value to be used as a placeholder when an item doesn’t match with any other (see the example above, where None is the placeholder).

Return type

List[List[~_T]]

Returns

A list containing the aligned lists. The items in the aligning lists will be sorted in ascending order.

nevopy.utils.utils.chance(p)

Randomly returns True or False.

Parameters

p (float) – Float between 0 and 1. Specifies the chance of the function returning True.

Return type

bool

Returns

A randomly chosen boolean value (True or False).

nevopy.utils.utils.clear_output()

Clears the output.

Should work on Windows and Linux terminals and on Jupyter notebooks. On PyCharm, it simply prints a bunch of new lines.

Return type

None

nevopy.utils.utils.is_jupyter_notebook()

Checks whether the program is running on a jupyter notebook.

Warning

This function is not guaranteed to work! It simply checks if IPython.get_ipython() returns None.

Return type

bool

Returns

True if the program is running on a jupyter notebook and False otherwise.

nevopy.utils.utils.make_table_row(name, current, past, abs_format='.2E', inc_format='+0.2E', pc_format='+0.2%', show_inc_pc=True, colors=True, positive_color='green', negative_color='red', neutral_color='white')

Makes a row for a columnar table.

Information in the row: name of the attribute; current value of the attribute; past value of the attribute; how much the attribute increased (absolute and percentage).

Return type

List[str]

nevopy.utils.utils.make_xor_data(num_variables=2)

Builds data using the XOR logic function.

The generated inputs are all the possible combinations of input values with the specified number of variables. Each variable is a bit (0 or 1). The generated outputs are the results (a single bit each) of the XOR function applied to all the inputs.

Example

>>> xor_in, xor_out = make_xor_data(num_variables=2)
>>> for x, y in zip(xor_in, xor_out):
...     print(f"{x} -> {y}")
...
[0 0] -> 0
[0 1] -> 1
[1 0] -> 1
[1 1] -> 0
Parameters

num_variables (int) – Number of input variables for the XOR logic function.

Return type

Tuple[ndarray, ndarray]

Returns

A tuple with two numpy arrays. The first array contains the input values, and the second array contains the output of the XOR function.

nevopy.utils.utils.min_max_norm(values)

Applies min-max normalization to the given values.

Return type

array

nevopy.utils.utils.pickle_load(abs_path)

Loads an object from the given absolute path.

Simple wrapper around the pickle package.

Parameters

abs_path (str) – Absolute path of the saved “.pkl” file. If the given path doesn’t end with the suffix “.pkl”, it will be automatically added.

Return type

Any

Returns

The loaded object.

nevopy.utils.utils.pickle_save(obj, abs_path)

Saves the given object to the given absolute path.

Simple wrapper around the pickle package.

Parameters
  • obj (Any) – Object to be saved.

  • abs_path (str) – Absolute path of the saving file. If the given path doesn’t end with the suffix “.pkl”, it will be automatically added.

Return type

None

nevopy.utils.utils.rank_prob_dist(size, coefficient, min_prob=1e-09)

Calculates a probability distribution that associates a probability to each position of a rank with the given size.

Parameters
  • size (int) – Size of the rank (and of the probability distribution).

  • coefficient (float) – This constant (let’s call it c) can be interpreted as follows: the position p of the rank is assigned a probability that is c times higher than the position p + 1 of the rank. If c = 2, here is an example of a probability distribution generated by this function: [0.5, 0.25, 0.125, 0.0675, …].

  • min_prob (float) – Probabilities with a value lower than the value passed to this parameter will be converted to 0. This prevents the occurrence of an arithmetic underflow.

Return type

ndarray

Returns

A numpy array with the probability distribution. The value in the index i of the array represents the probability of the position i of the rank.

nevopy.utils.utils.round_proportional_distribution(to_distribute, values)

Given an integer A and a sequence S of arbitrary size k, this function divides A into k integers. The proportion that the i-th integer represents of A is approximately equal to the proportion that S[i] represents of sum(S[i]).

Example

>>> round_proportional_distribution(100, [1.22, 2.78, 0.26, 5.74])
[12, 28, 3, 57]
Parameters
  • to_distribute (int) – Integer to be distributed.

  • values (Sequence[float]) – Values that will serve as a reference.

Return type

List[int]

Returns

A list with the same size as values containing integers that sum to to_distribute.

Module contents

Exposes the main utility functions and classes within this package.