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.be2le(i, n)

Convert between big-endian and little-endian for indices in range(n).

pyphi.convert.le2be(i, n)

Convert between big-endian and little-endian for indices in range(n).

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.state2be_index(state)

Convert a PyPhi state-tuple to a decimal index according to the big-endian 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.

Returns

A decimal integer corresponding to a network state under the big-endian convention.

Return type

int

Examples

>>> state2be_index((1, 0, 0, 0, 0))
16
>>> state2be_index((1, 1, 1, 0, 0, 0, 0, 0))
224
pyphi.convert.state2le_index(state)

Convert a PyPhi state-tuple to a decimal index according to the little-endian 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.

Returns

A decimal integer corresponding to a network state under the little-endian convention.

Return type

int

Examples

>>> state2le_index((1, 0, 0, 0, 0))
1
>>> state2le_index((1, 1, 1, 0, 0, 0, 0, 0))
7
pyphi.convert.le_index2state(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple with the little-endian convention.

The output is the reverse of be_index2state().

Parameters

i (int) – A decimal integer corresponding to a network state under the little-endian convention.

Returns

A state-tuple where the \(i^{\textrm{th}}\) element of the tuple gives the state of the \(i^{\textrm{th}}\) node.

Return type

tuple[int]

Examples

>>> number_of_nodes = 5
>>> le_index2state(1, number_of_nodes)
(1, 0, 0, 0, 0)
>>> number_of_nodes = 8
>>> le_index2state(7, number_of_nodes)
(1, 1, 1, 0, 0, 0, 0, 0)
pyphi.convert.be_index2state(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple using the big-endian convention that the most-significant bits correspond to low-index nodes.

The output is the reverse of le_index2state().

Parameters

i (int) – A decimal integer corresponding to a network state under the big-endian convention.

Returns

A state-tuple where the \(i^{\textrm{th}}\) element of the tuple gives the state of the \(i^{\textrm{th}}\) node.

Return type

tuple[int]

Examples

>>> number_of_nodes = 5
>>> be_index2state(1, number_of_nodes)
(0, 0, 0, 0, 1)
>>> number_of_nodes = 8
>>> be_index2state(7, number_of_nodes)
(0, 0, 0, 0, 0, 1, 1, 1)
pyphi.convert.be2le_state_by_state(tpm)

Convert a state-by-state TPM from big-endian to little-endian or vice versa.

Parameters

tpm (np.ndarray) – A state-by-state TPM.

Returns

The state-by-state TPM in the other indexing format.

Return type

np.ndarray

Example

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

Convert a state-by-state TPM from big-endian to little-endian or vice versa.

Parameters

tpm (np.ndarray) – A state-by-state TPM.

Returns

The state-by-state TPM in the other indexing format.

Return type

np.ndarray

Example

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

Reshape a state-by-node TPM to the multidimensional form.

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

pyphi.convert.to_2dimensional(tpm)

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

See Transition probability matrix conventions and documentation for the Network object for more information on TPM representations.

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 little-endian convention. The indices of the rows of the resulting state-by-node TPM also follow the little-endian 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 little-endian convention.

Returns

A state-by-node TPM, with row indices following the little-endian convention.

Return type

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 little-endian convention, while the indices of the columns follow the big-endian convention. The indices of the rows and columns of the resulting state-by-state TPM both follow the big-endian 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 little-endian convention and column indices following the big-endian convention.

Returns

A state-by-state TPM, with both row and column indices following the big-endian convention.

Return type

np.ndarray

Examples: >>> 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.]])

>>> tpm = np.array([[0.1, 0.3, 0.7],
...                 [0.3, 0.9, 0.2],
...                 [0.3, 0.9, 0.1],
...                 [0.2, 0.8, 0.5],
...                 [0.1, 0.7, 0.4],
...                 [0.4, 0.3, 0.6],
...                 [0.4, 0.3, 0.1],
...                 [0.5, 0.2, 0.1]])
>>> state_by_node2state_by_state(tpm)
array([[0.189, 0.021, 0.081, 0.009, 0.441, 0.049, 0.189, 0.021],
       [0.056, 0.024, 0.504, 0.216, 0.014, 0.006, 0.126, 0.054],
       [0.063, 0.027, 0.567, 0.243, 0.007, 0.003, 0.063, 0.027],
       [0.08 , 0.02 , 0.32 , 0.08 , 0.08 , 0.02 , 0.32 , 0.08 ],
       [0.162, 0.018, 0.378, 0.042, 0.108, 0.012, 0.252, 0.028],
       [0.168, 0.112, 0.072, 0.048, 0.252, 0.168, 0.108, 0.072],
       [0.378, 0.252, 0.162, 0.108, 0.042, 0.028, 0.018, 0.012],
       [0.36 , 0.36 , 0.09 , 0.09 , 0.04 , 0.04 , 0.01 , 0.01 ]])
pyphi.convert.b2l(i, n)

Convert between big-endian and little-endian for indices in range(n).

pyphi.convert.l2b(i, n)

Convert between big-endian and little-endian for indices in range(n).

pyphi.convert.l2s(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple with the little-endian convention.

The output is the reverse of be_index2state().

Parameters

i (int) – A decimal integer corresponding to a network state under the little-endian convention.

Returns

A state-tuple where the \(i^{\textrm{th}}\) element of the tuple gives the state of the \(i^{\textrm{th}}\) node.

Return type

tuple[int]

Examples

>>> number_of_nodes = 5
>>> le_index2state(1, number_of_nodes)
(1, 0, 0, 0, 0)
>>> number_of_nodes = 8
>>> le_index2state(7, number_of_nodes)
(1, 1, 1, 0, 0, 0, 0, 0)
pyphi.convert.b2s(i, number_of_nodes)

Convert a decimal integer to a PyPhi state tuple using the big-endian convention that the most-significant bits correspond to low-index nodes.

The output is the reverse of le_index2state().

Parameters

i (int) – A decimal integer corresponding to a network state under the big-endian convention.

Returns

A state-tuple where the \(i^{\textrm{th}}\) element of the tuple gives the state of the \(i^{\textrm{th}}\) node.

Return type

tuple[int]

Examples

>>> number_of_nodes = 5
>>> be_index2state(1, number_of_nodes)
(0, 0, 0, 0, 1)
>>> number_of_nodes = 8
>>> be_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 little-endian 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.

Returns

A decimal integer corresponding to a network state under the little-endian convention.

Return type

int

Examples

>>> state2le_index((1, 0, 0, 0, 0))
1
>>> state2le_index((1, 1, 1, 0, 0, 0, 0, 0))
7
pyphi.convert.s2b(state)

Convert a PyPhi state-tuple to a decimal index according to the big-endian 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.

Returns

A decimal integer corresponding to a network state under the big-endian convention.

Return type

int

Examples

>>> state2be_index((1, 0, 0, 0, 0))
16
>>> state2be_index((1, 1, 1, 0, 0, 0, 0, 0))
224
pyphi.convert.b2l_sbs(tpm)

Convert a state-by-state TPM from big-endian to little-endian or vice versa.

Parameters

tpm (np.ndarray) – A state-by-state TPM.

Returns

The state-by-state TPM in the other indexing format.

Return type

np.ndarray

Example

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

Convert a state-by-state TPM from big-endian to little-endian or vice versa.

Parameters

tpm (np.ndarray) – A state-by-state TPM.

Returns

The state-by-state TPM in the other indexing format.

Return type

np.ndarray

Example

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

Reshape a state-by-node TPM to the multidimensional form.

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

pyphi.convert.to_2d(tpm)

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

See Transition probability matrix conventions and documentation for the Network object for more information on TPM representations.

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 little-endian convention, while the indices of the columns follow the big-endian convention. The indices of the rows and columns of the resulting state-by-state TPM both follow the big-endian 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 little-endian convention and column indices following the big-endian convention.

Returns

A state-by-state TPM, with both row and column indices following the big-endian convention.

Return type

np.ndarray

Examples: >>> 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.]])

>>> tpm = np.array([[0.1, 0.3, 0.7],
...                 [0.3, 0.9, 0.2],
...                 [0.3, 0.9, 0.1],
...                 [0.2, 0.8, 0.5],
...                 [0.1, 0.7, 0.4],
...                 [0.4, 0.3, 0.6],
...                 [0.4, 0.3, 0.1],
...                 [0.5, 0.2, 0.1]])
>>> state_by_node2state_by_state(tpm)
array([[0.189, 0.021, 0.081, 0.009, 0.441, 0.049, 0.189, 0.021],
       [0.056, 0.024, 0.504, 0.216, 0.014, 0.006, 0.126, 0.054],
       [0.063, 0.027, 0.567, 0.243, 0.007, 0.003, 0.063, 0.027],
       [0.08 , 0.02 , 0.32 , 0.08 , 0.08 , 0.02 , 0.32 , 0.08 ],
       [0.162, 0.018, 0.378, 0.042, 0.108, 0.012, 0.252, 0.028],
       [0.168, 0.112, 0.072, 0.048, 0.252, 0.168, 0.108, 0.072],
       [0.378, 0.252, 0.162, 0.108, 0.042, 0.028, 0.018, 0.012],
       [0.36 , 0.36 , 0.09 , 0.09 , 0.04 , 0.04 , 0.01 , 0.01 ]])
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 little-endian convention. The indices of the rows of the resulting state-by-node TPM also follow the little-endian 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 little-endian convention.

Returns

A state-by-node TPM, with row indices following the little-endian convention.

Return type

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]]])