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, holi=False)

Return all binary states for a system.

Parameters:
  • n (int) – The number of elements in the system.
  • holi (bool) – Whether to return the states in HOLI order instead of LOLI order.
Yields:

tuple[int]

The next state of an n-element system, in LOLI order

unless holi is True.

pyphi.utils.np_immutable(a)

Make a NumPy array immutable.

pyphi.utils.np_hash(a)

Return a hash of a NumPy array.

pyphi.utils.phi_eq(x, y)

Compare two phi values up to constants.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:

np.ndarray – An array of combinations.

pyphi.utils.comb_indices(n, k)

\(N\)-\(D\) version of itertools.combinations.

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

np.ndarray – Indices that give the \(k\)-combinations of \(n\) elements.

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)

Generate the power set of an iterable.

Parameters:iterable (Iterable) – The iterable from which to generate the power set.
Returns:generator – An chained generator over the power set.

Example

>>> ps = powerset(np.arange(2))
>>> print(list(ps))
[(), (0,), (1,), (0, 1)]
>>> ps = powerset(np.arange(2), nonempty=True)
>>> print(list(ps))
[(0,), (1,), (0, 1)]
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:list – A list of loaded data, such that list[i] contains the the contents of i.npy.