# utils¶

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

pyphi.utils.cut_mechanism_indices(subsystem, cut)

Returns a tuple of indices of mechanisms that have nodes on both sides of the given cut.

pyphi.utils.mechanism_split_by_cut(mechanism_indices, cut)
pyphi.utils.cut_mice(mice, cut_matrix)
pyphi.utils.condition_tpm(tpm, fixed_nodes, state)

Return a TPM conditioned on the given fixed node indices, whose states are fixed according to the given state-tuple.

The dimensions of the new TPM that correspond to the fixed nodes are collapsed onto their state, making those dimensions singletons suitable for broadcasting. The number of dimensions of the conditioned TPM will be the same as the unconditioned TPM.

pyphi.utils.apply_cut(cut, connectivity_matrix)

Returns a modified connectivity matrix where the connections from one set of nodes to the other are destroyed.

pyphi.utils.apply_boundary_conditions_to_cm(external_indices, connectivity_matrix)

Returns a connectivity matrix with all connections to or from external nodes removed.

pyphi.utils.get_inputs_from_cm(index, connectivity_matrix)

Returns a tuple of node indices that have connections to the node with the given index.

pyphi.utils.get_outputs_from_cm(index, connectivity_matrix)

Returns a tuple of node indices that the node with the given index has connections to.

pyphi.utils.np_hash(a)

Return a hash of a NumPy array.

This is much faster than np.toString for large arrays.

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. combinations – An array of combinations. np.ndarray
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. combination_indices – Indices that give the $$k$$-combinations of $$n$$ elements. 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)

Return the power set of an iterable (see itertools recipes).

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

Example

>>> ps = powerset(np.arange(2))
>>> print(list(ps))
[(), (0,), (1,), (0, 1)]

pyphi.utils.uniform_distribution(number_of_nodes)

Return the uniform distribution for a set of binary nodes, indexed by state (so there is one dimension per node, the size of which is the number of possible states for that node).

Parameters: nodes (np.ndarray) – A set of indices of binary nodes. distribution – The uniform distribution over the set of nodes. np.ndarray
pyphi.utils.marginalize_out(index, tpm, perturb_value=0.5)

Marginalize out a node from a TPM.

Parameters: index (list) – The index of the node to be marginalized out. tpm (np.ndarray) – The TPM to marginalize the node out of. tpm – A TPM with the same number of dimensions, with the node marginalized out. np.ndarray
pyphi.utils.max_entropy_distribution(node_indices, number_of_nodes, perturb_vector=None)

Return the maximum entropy distribution over a set of nodes.

This is different from the network’s uniform distribution because nodes outside the are fixed and treated as if they have only 1 state.

Parameters: nodes (tuple(Nodes) – The set of nodes over which to take the distribution. network (Network) – The network the nodes belong to. distribution – The maximum entropy distribution over the set of nodes. np.ndarray
pyphi.utils.hamming_emd(d1, d2)

Return the Earth Mover’s Distance between two distributions (indexed by state, one dimension per node).

Singleton dimensions are sqeezed out.

pyphi.utils.bipartition(a)

Return a list of bipartitions for a sequence.

Parameters: a (Iterable) – The iterable to partition. bipartition – A list of tuples containing each of the two partitions. list(tuple(tuple

Example

>>> from pyphi.utils import bipartition
>>> bipartition((1,2,3))
[((), (1, 2, 3)), ((1,), (2, 3)), ((2,), (1, 3)), ((1, 2), (3,))]

pyphi.utils.directed_bipartition(a)

Return a list of directed bipartitions for a sequence.

Parameters: a (Iterable) – The iterable to partition. bipartition – A list of tuples containing each of the two partitions. list(tuple(tuple

Example

>>> from pyphi.utils import directed_bipartition
>>> directed_bipartition((1, 2, 3))
[((), (1, 2, 3)), ((1,), (2, 3)), ((2,), (1, 3)), ((1, 2), (3,)), ((3,), (1, 2)), ((1, 3), (2,)), ((2, 3), (1,)), ((1, 2, 3), ())]

pyphi.utils.directed_bipartition_of_one(a)

Return a list of directed bipartitions for a sequence where each bipartitions includes a set of size 1.

Parameters: a (Iterable) – The iterable to partition. bipartition – A list of tuples containing each of the two partitions. list(tuple(tuple

Example

>>> from pyphi.utils import directed_bipartition_of_one
>>> directed_bipartition_of_one((1,2,3))
[((1,), (2, 3)), ((2,), (1, 3)), ((1, 2), (3,)), ((3,), (1, 2)), ((1, 3), (2,)), ((2, 3), (1,))]

pyphi.utils.directed_bipartition_indices(N)

Returns indices for directed bipartitions of a sequence.

Parameters: N (int) – The length of the sequence. bipartition_indices – A list of tuples containing the indices for each of the two partitions. list

Example

>>> from pyphi.utils import directed_bipartition_indices
>>> N = 3
>>> directed_bipartition_indices(N)
[((), (0, 1, 2)), ((0,), (1, 2)), ((1,), (0, 2)), ((0, 1), (2,)), ((2,), (0, 1)), ((0, 2), (1,)), ((1, 2), (0,)), ((0, 1, 2), ())]

pyphi.utils.bipartition_indices(N)

Returns indices for bipartitions of a sequence.

Parameters: N (int) – The length of the sequence. bipartition_indices – A list of tuples containing the indices for each of the two partitions. list

Example

>>> from pyphi.utils import bipartition_indices
>>> N = 3
>>> bipartition_indices(N)
[((), (0, 1, 2)), ((0,), (1, 2)), ((1,), (0, 2)), ((0, 1), (2,))]

pyphi.utils.connectivity_matrix_to_tpm(network)

Generate a TPM from a connectivity matrix and nodes that implement logical functions.

Parameters: network (Network) – The network for which to generate the TPM. tpm – A transition probability matrix. np.ndarray
pyphi.utils.block_cm(cm)

Determining if a given connectivity matrix be rearranged as a block connectivity matrix.

If so, the corresponding mechanism/purview is trivially reducible.

pyphi.utils.not_block_reducible(cm, nodes1, nodes2)

Tests connectivity of one set of nodes to another.

Parameters: cm (np.ndarray) – The network’s connectivity matrix. nodes1 (tuple(Node) – The nodes whose outputs to nodes2 will be tested. nodes2 (tuple(Node) – The nodes whose inputs from nodes1 will be tested.
pyphi.utils.print_repertoire(r)
pyphi.utils.print_repertoire_horiz(r)
pyphi.utils.print_partition(p)