utils

Functions used by more than one PyPhi module or class, or that might be of external use.

pyphi.utils.state_of(nodes, network_state)

Return the state-tuple of the given nodes.

pyphi.utils.all_states(n, big_endian=False)

Return all binary states for a system.

Parameters:
  • n (int) – The number of elements in the system.
  • big_endian (bool) – Whether to return the states in big-endian order instead of little-endian order.
Yields:

tuple[int] – The next state of an n-element system, in little-endian order unless big_endian is True.

pyphi.utils.np_immutable(a)

Make a NumPy array immutable.

pyphi.utils.np_hash(a)

Return a hash of a NumPy array.

class pyphi.utils.np_hashable(array)

A hashable wrapper around a NumPy array.

pyphi.utils.eq(x, y)

Compare two values up to PRECISION.

pyphi.utils.combs(a, r)

NumPy implementation of itertools.combinations.

Return successive r-length combinations of elements in the array a.

Parameters:
  • a (np.ndarray) – The array from which to get combinations.
  • r (int) – The length of the combinations.
Returns:

An array of combinations.

Return type:

np.ndarray

pyphi.utils.comb_indices(n, k)

n-dimensional version of itertools.combinations.

Parameters:
  • a (np.ndarray) – The array from which to get combinations.
  • k (int) – The desired length of the combinations.
Returns:

Indices that give the k-combinations of n elements.

Return type:

np.ndarray

Example

>>> n, k = 3, 2
>>> data = np.arange(6).reshape(2, 3)
>>> data[:, comb_indices(n, k)]
array([[[0, 1],
        [0, 2],
        [1, 2]],

       [[3, 4],
        [3, 5],
        [4, 5]]])
pyphi.utils.powerset(iterable, nonempty=False, reverse=False)

Generate the power set of an iterable.

Parameters:

iterable (Iterable) – The iterable from which to generate the power set.

Keyword Arguments:
 
  • nonempty (boolean) – If True, don’t include the empty set.
  • reverse (boolean) – If True, reverse the order of the powerset.
Returns:

An iterator over the power set.

Return type:

Iterable

Example

>>> ps = powerset(np.arange(2))
>>> list(ps)
[(), (0,), (1,), (0, 1)]
>>> ps = powerset(np.arange(2), nonempty=True)
>>> list(ps)
[(0,), (1,), (0, 1)]
>>> ps = powerset(np.arange(2), nonempty=True, reverse=True)
>>> list(ps)
[(1, 0), (1,), (0,)]
pyphi.utils.load_data(directory, num)

Load numpy data from the data directory.

The files should stored in ../data/<dir> and named 0.npy, 1.npy, ... <num - 1>.npy.

Returns:A list of loaded data, such that list[i] contains the the contents of i.npy.
Return type:list
pyphi.utils.time_annotated(func)

Annotate the decorated function or method with the total execution time.

The result is annotated with a time attribute.