# convert¶

Conversion functions.

See the documentation on PyPhi Transition probability matrix conventions for information on the different representations that these functions convert between.

pyphi.convert.reverse_bits(i, n)

Reverse the bits of the n-bit decimal number i.

Examples

>>> reverse_bits(12, 7)
24
>>> reverse_bits(0, 1)
0
>>> reverse_bits(1, 2)
2

pyphi.convert.nodes2indices(nodes)

Convert nodes to a tuple of their indices.

pyphi.convert.nodes2state(nodes)

Convert nodes to a tuple of their states.

pyphi.convert.holi2loli(i, n)

Convert between HOLI and LOLI for indices in range(n).

pyphi.convert.loli2holi(i, n)

Convert between HOLI and LOLI for indices in range(n).

pyphi.convert.state2holi_index(state)

Convert a PyPhi state-tuple to a decimal index according to the HOLI convention.

Parameters: state (tuple[int]) – A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. A decimal integer corresponding to a network state under the HOLI convention. int

Examples

>>> state2holi_index((1, 0, 0, 0, 0))
16
>>> state2holi_index((1, 1, 1, 0, 0, 0, 0, 0))
224

pyphi.convert.state2loli_index(state)

Convert a PyPhi state-tuple to a decimal index according to the LOLI convention.

Parameters: state (tuple[int]) – A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. A decimal integer corresponding to a network state under the LOLI convention. int

Examples

>>> state2loli_index((1, 0, 0, 0, 0))
1
>>> state2loli_index((1, 1, 1, 0, 0, 0, 0, 0))
7

pyphi.convert.loli_index2state(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple with the LOLI convention.

The output is the reverse of holi_index2state().

Parameters: i (int) – A decimal integer corresponding to a network state under the LOLI convention. A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. tuple[int]

Examples

>>> number_of_nodes = 5
>>> loli_index2state(1, number_of_nodes)
(1, 0, 0, 0, 0)
>>> number_of_nodes = 8
>>> loli_index2state(7, number_of_nodes)
(1, 1, 1, 0, 0, 0, 0, 0)

pyphi.convert.holi_index2state(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple using the HOLI convention that high-order bits correspond to low-index nodes.

The output is the reverse of loli_index2state().

Parameters: i (int) – A decimal integer corresponding to a network state under the HOLI convention. A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. tuple[int]

Examples

>>> number_of_nodes = 5
>>> holi_index2state(1, number_of_nodes)
(0, 0, 0, 0, 1)
>>> number_of_nodes = 8
>>> holi_index2state(7, number_of_nodes)
(0, 0, 0, 0, 0, 1, 1, 1)

pyphi.convert.holi2loli_state_by_state(tpm)

Convert a state-by-state TPM from HOLI to LOLI or vice versa.

Parameters: tpm (np.ndarray) – A state-by-state TPM. The state-by-state TPM in the other indexing format. np.ndarray

Example

>>> tpm = np.arange(16).reshape([4, 4])
>>> holi2loli_state_by_state(tpm)
array([[  0.,   1.,   2.,   3.],
[  8.,   9.,  10.,  11.],
[  4.,   5.,   6.,   7.],
[ 12.,  13.,  14.,  15.]])

pyphi.convert.loli2holi_state_by_state(tpm)

Convert a state-by-state TPM from HOLI to LOLI or vice versa.

Parameters: tpm (np.ndarray) – A state-by-state TPM. The state-by-state TPM in the other indexing format. np.ndarray

Example

>>> tpm = np.arange(16).reshape([4, 4])
>>> holi2loli_state_by_state(tpm)
array([[  0.,   1.,   2.,   3.],
[  8.,   9.,  10.,  11.],
[  4.,   5.,   6.,   7.],
[ 12.,  13.,  14.,  15.]])

pyphi.convert.to_n_dimensional(tpm)

Reshape a state-by-node TPM to the n-dimensional form.

See documentation for the Network object for more information on TPM formats.

pyphi.convert.to_2_dimensional(tpm)

Reshape a state-by-node TPM to the 2-dimensional form.

See documentation for the Network object for more information on TPM formats.

pyphi.convert.state_by_state2state_by_node(tpm)

Convert a state-by-state TPM to a state-by-node TPM.

Danger

Many nondeterministic state-by-state TPMs can be represented by a single a state-by-state TPM. However, the mapping can be made to be one-to-one if we assume the state-by-state TPM is conditionally independent, as this function does. If the given TPM is not conditionally independent, the conditional dependencies will be silently lost.

Note

The indices of the rows and columns of the state-by-state TPM are assumed to follow the LOLI convention. The indices of the rows of the resulting state-by-node TPM also follow the LOLI convention. See the documentation on PyPhi the Transition probability matrix conventions more information.

Parameters: tpm (list[list] or np.ndarray) – A square state-by-state TPM with row and column indices following the LOLI convention. A state-by-node TPM, with row indices following the LOLI convention. np.ndarray

Example

>>> tpm = np.array([[0.5, 0.5, 0.0, 0.0],
...                 [0.0, 1.0, 0.0, 0.0],
...                 [0.0, 0.2, 0.0, 0.8],
...                 [0.0, 0.3, 0.7, 0.0]])
>>> state_by_state2state_by_node(tpm)
array([[[ 0.5,  0. ],
[ 1. ,  0.8]],

[[ 1. ,  0. ],
[ 0.3,  0.7]]])

pyphi.convert.state_by_node2state_by_state(tpm)

Convert a state-by-node TPM to a state-by-state TPM.

Important

A nondeterministic state-by-node TPM can have more than one representation as a state-by-state TPM. However, the mapping can be made to be one-to-one if we assume the TPMs to be conditionally independent. Therefore, this function returns the corresponding conditionally independent state-by-state TPM.

Note

The indices of the rows of the state-by-node TPM are assumed to follow the LOLI convention, while the indices of the columns follow the HOLI convention. The indices of the rows and columns of the resulting state-by-state TPM both follow the HOLI convention. See the documentation on PyPhi Transition probability matrix conventions for more info.

Parameters: tpm (list[list] or np.ndarray) – A state-by-node TPM with row indices following the LOLI convention and column indices following the HOLI convention. A state-by-state TPM, with both row and column indices following the HOLI convention. np.ndarray
>>> tpm = np.array([[1, 1, 0],
...                 [0, 0, 1],
...                 [0, 1, 1],
...                 [1, 0, 0],
...                 [0, 0, 1],
...                 [1, 0, 0],
...                 [1, 1, 1],
...                 [1, 0, 1]])
>>> state_by_node2state_by_state(tpm)
array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
[ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
[ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
[ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.]])

pyphi.convert.h2l(i, n)

Convert between HOLI and LOLI for indices in range(n).

pyphi.convert.l2h(i, n)

Convert between HOLI and LOLI for indices in range(n).

pyphi.convert.l2s(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple with the LOLI convention.

The output is the reverse of holi_index2state().

Parameters: i (int) – A decimal integer corresponding to a network state under the LOLI convention. A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. tuple[int]

Examples

>>> number_of_nodes = 5
>>> loli_index2state(1, number_of_nodes)
(1, 0, 0, 0, 0)
>>> number_of_nodes = 8
>>> loli_index2state(7, number_of_nodes)
(1, 1, 1, 0, 0, 0, 0, 0)

pyphi.convert.h2s(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple using the HOLI convention that high-order bits correspond to low-index nodes.

The output is the reverse of loli_index2state().

Parameters: i (int) – A decimal integer corresponding to a network state under the HOLI convention. A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. tuple[int]

Examples

>>> number_of_nodes = 5
>>> holi_index2state(1, number_of_nodes)
(0, 0, 0, 0, 1)
>>> number_of_nodes = 8
>>> holi_index2state(7, number_of_nodes)
(0, 0, 0, 0, 0, 1, 1, 1)

pyphi.convert.s2l(state)

Convert a PyPhi state-tuple to a decimal index according to the LOLI convention.

Parameters: state (tuple[int]) – A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. A decimal integer corresponding to a network state under the LOLI convention. int

Examples

>>> state2loli_index((1, 0, 0, 0, 0))
1
>>> state2loli_index((1, 1, 1, 0, 0, 0, 0, 0))
7

pyphi.convert.s2h(state)

Convert a PyPhi state-tuple to a decimal index according to the HOLI convention.

Parameters: state (tuple[int]) – A state-tuple where the $$i^{\textrm{th}}$$ element of the tuple gives the state of the $$i^{\textrm{th}}$$ node. A decimal integer corresponding to a network state under the HOLI convention. int

Examples

>>> state2holi_index((1, 0, 0, 0, 0))
16
>>> state2holi_index((1, 1, 1, 0, 0, 0, 0, 0))
224

pyphi.convert.h2l_sbs(tpm)

Convert a state-by-state TPM from HOLI to LOLI or vice versa.

Parameters: tpm (np.ndarray) – A state-by-state TPM. The state-by-state TPM in the other indexing format. np.ndarray

Example

>>> tpm = np.arange(16).reshape([4, 4])
>>> holi2loli_state_by_state(tpm)
array([[  0.,   1.,   2.,   3.],
[  8.,   9.,  10.,  11.],
[  4.,   5.,   6.,   7.],
[ 12.,  13.,  14.,  15.]])

pyphi.convert.l2h_sbs(tpm)

Convert a state-by-state TPM from HOLI to LOLI or vice versa.

Parameters: tpm (np.ndarray) – A state-by-state TPM. The state-by-state TPM in the other indexing format. np.ndarray

Example

>>> tpm = np.arange(16).reshape([4, 4])
>>> holi2loli_state_by_state(tpm)
array([[  0.,   1.,   2.,   3.],
[  8.,   9.,  10.,  11.],
[  4.,   5.,   6.,   7.],
[ 12.,  13.,  14.,  15.]])

pyphi.convert.to_n_d(tpm)

Reshape a state-by-node TPM to the n-dimensional form.

See documentation for the Network object for more information on TPM formats.

pyphi.convert.to_2_d(tpm)

Reshape a state-by-node TPM to the 2-dimensional form.

See documentation for the Network object for more information on TPM formats.

pyphi.convert.sbn2sbs(tpm)

Convert a state-by-node TPM to a state-by-state TPM.

Important

A nondeterministic state-by-node TPM can have more than one representation as a state-by-state TPM. However, the mapping can be made to be one-to-one if we assume the TPMs to be conditionally independent. Therefore, this function returns the corresponding conditionally independent state-by-state TPM.

Note

The indices of the rows of the state-by-node TPM are assumed to follow the LOLI convention, while the indices of the columns follow the HOLI convention. The indices of the rows and columns of the resulting state-by-state TPM both follow the HOLI convention. See the documentation on PyPhi Transition probability matrix conventions for more info.

Parameters: tpm (list[list] or np.ndarray) – A state-by-node TPM with row indices following the LOLI convention and column indices following the HOLI convention. A state-by-state TPM, with both row and column indices following the HOLI convention. np.ndarray
>>> tpm = np.array([[1, 1, 0],
...                 [0, 0, 1],
...                 [0, 1, 1],
...                 [1, 0, 0],
...                 [0, 0, 1],
...                 [1, 0, 0],
...                 [1, 1, 1],
...                 [1, 0, 1]])
>>> state_by_node2state_by_state(tpm)
array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
[ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
[ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
[ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.]])

pyphi.convert.sbs2sbn(tpm)

Convert a state-by-state TPM to a state-by-node TPM.

Danger

Many nondeterministic state-by-state TPMs can be represented by a single a state-by-state TPM. However, the mapping can be made to be one-to-one if we assume the state-by-state TPM is conditionally independent, as this function does. If the given TPM is not conditionally independent, the conditional dependencies will be silently lost.

Note

The indices of the rows and columns of the state-by-state TPM are assumed to follow the LOLI convention. The indices of the rows of the resulting state-by-node TPM also follow the LOLI convention. See the documentation on PyPhi the Transition probability matrix conventions more information.

Parameters: tpm (list[list] or np.ndarray) – A square state-by-state TPM with row and column indices following the LOLI convention. A state-by-node TPM, with row indices following the LOLI convention. np.ndarray

Example

>>> tpm = np.array([[0.5, 0.5, 0.0, 0.0],
...                 [0.0, 1.0, 0.0, 0.0],
...                 [0.0, 0.2, 0.0, 0.8],
...                 [0.0, 0.3, 0.7, 0.0]])
>>> state_by_state2state_by_node(tpm)
array([[[ 0.5,  0. ],
[ 1. ,  0.8]],

[[ 1. ,  0. ],
[ 0.3,  0.7]]])