utils

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

pyphi.utils.substate(nodes: Tuple[int], state: Tuple[int], node_subset: Tuple[int]) Tuple[int]
pyphi.utils.state_of(nodes, network_state)

Return the state-tuple of the given nodes.

pyphi.utils.state_of_subsystem_nodes(node_indices, nodes, subsystem_state)

Return the state of the nodes, given a subsystem state-tuple.

Deals with using the network-relative node indices nodes with a state-tuple for only the subsystem 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.is_positive(x)

Return whether x is positive up to PRECISION.

pyphi.utils.is_nonpositive(x)

Return True if x is a nonpositive value.

pyphi.utils.is_falsy(x)

Return True if x is a falsy value.

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, min_size=0, max_size=None)

Generate the power set of an iterable.

Parameters:

iterable (Iterable) – The iterable of 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.

  • min_size (int | None) – Only generate subsets of this size or larger. Defaults to None, meaning no restriction. Overrides nonempty.

  • max_size (int | None) – Only generate subsets of this size or smaller. Defaults to None, meaning no restriction.

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,)]
>>> ps = powerset(np.arange(3), max_size=2)
>>> list(ps)
[(), (0,), (1,), (2,), (0, 1), (0, 2), (1, 2)]
>>> ps = powerset(np.arange(3), min_size=2)
>>> list(ps)
[(0, 1), (0, 2), (1, 2), (0, 1, 2)]
>>> ps = powerset(np.arange(3), min_size=2, max_size=2)
>>> list(ps)
[(0, 1), (0, 2), (1, 2)]
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.specified_substate(purview, specified_state, subset)
pyphi.utils.extremum_with_short_circuit(seq, value_func=<function <lambda>>, cmp=<built-in function lt>, initial=inf, shortcircuit_value=0, shortcircuit_callback=None)

Return the extreme value, optionally shortcircuiting.

pyphi.utils.expsublog(x, y)

Computes x / y as exp(log(x) - log(y)).

Useful for dividing by extremely large denominators.

See also numpy.logaddexp.

pyphi.utils.expaddlog(x, y)

Computes x * y as exp(log(x) + log(y)).

Useful for dividing by extremely large denominators.

See also numpy.logaddexp.

pyphi.utils.try_len(*iterables)

Return the minimum length of iterables, or None if none have a length.

pyphi.utils.assume_integer(x)

Attempt cast to integer, raising an error if it is not an integer.

pyphi.utils.enforce_integer(i, name='', min=-inf)
pyphi.utils.enforce_integer_or_none(i, **kwargs)
pyphi.utils.all_same(comparison='__no__default__', seq='__no__default__')
pyphi.utils.all_are_equal(seq='__no__default__')
pyphi.utils.all_are_identical(seq='__no__default__')
pyphi.utils.all_extrema(comparison='__no__default__', seq='__no__default__', default=<object object>)

Return the extrema of seq.

Use < as the comparison to obtain the minima; use > as the comparison to obtain the maxima.

Uses only one pass through seq.

Parameters:
  • comparison (callable) – A comparison operator.

  • seq (iterator) – An iterator over a sequence.

Returns:

The maxima/minima in seq.

Return type:

list

pyphi.utils.all_minima(seq='__no__default__', default=<object object>)

Return the extrema of seq.

Use < as the comparison to obtain the minima; use > as the comparison to obtain the maxima.

Uses only one pass through seq.

Parameters:
  • comparison (callable) – A comparison operator.

  • seq (iterator) – An iterator over a sequence.

Returns:

The maxima/minima in seq.

Return type:

list

pyphi.utils.all_maxima(seq='__no__default__', default=<object object>)

Return the extrema of seq.

Use < as the comparison to obtain the minima; use > as the comparison to obtain the maxima.

Uses only one pass through seq.

Parameters:
  • comparison (callable) – A comparison operator.

  • seq (iterator) – An iterator over a sequence.

Returns:

The maxima/minima in seq.

Return type:

list

pyphi.utils.iter_with_default(seq, default)

Iterate over seq, yielding default if seq is empty.