tpm

Provides the ExplicitTPM and related classes.

class pyphi.tpm.ProxyMetaclass(type_name, bases, dct)

A metaclass to create wrappers for the TPM array’s special attributes.

The CPython interpreter resolves double-underscore attributes (e.g., the method definitions of mathematical operators) by looking up in the class’ static methods, not in the instance methods. This makes it impossible to intercept calls to them when an instance’s __getattr__() is implicitly invoked, which in turn means there are only two options to wrap the special methods of the array inside our custom objects (in order to perform arithmetic operations with the TPM while also casting the result to our custom class type):

  1. Manually “overload” all the necessary methods.

  2. Use this metaclass to introspect the underlying array and automatically overload methods in our custom TPM class definition.

class pyphi.tpm.Wrapper

Proxy to the array inside PyPhi’s custom TPM class.

class pyphi.tpm.ExplicitTPM(tpm, validate=False)

An explicit network TPM in multidimensional form.

property tpm

Return the underlying tpm object.

validate(check_independence=True)

Validate this TPM.

to_multidimensional_state_by_node()

Return the current TPM re-represented in multidimensional state-by-node form.

See the PyPhi documentation on Transition probability matrix conventions for more information.

Returns:

The TPM in multidimensional state-by-node format.

Return type:

np.ndarray

conditionally_independent()

Validate that the TPM is conditionally independent.

condition_tpm(condition: Mapping[int, int])

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.

Parameters:

condition (dict[int, int]) – A mapping from node indices to the state to condition on for that node.

Returns:

A conditioned TPM with the same number of dimensions, with singleton dimensions for nodes in a fixed state.

Return type:

TPM

marginalize_out(node_indices)

Marginalize out nodes from this TPM.

Parameters:

node_indices (list[int]) – The indices of nodes to be marginalized out.

Returns:

A TPM with the same number of dimensions, with the nodes marginalized out.

Return type:

ExplicitTPM

is_deterministic()

Return whether the TPM is deterministic.

is_state_by_state()

Return True if tpm is in state-by-state form, otherwise False.

subtpm(fixed_nodes, state)

Return the TPM for a subset of nodes, conditioned on other nodes.

Parameters:
  • fixed_nodes (tuple[int]) – The nodes to select.

  • state (tuple[int]) – The state of the fixed nodes.

Returns:

The TPM of just the subsystem of the free nodes.

Return type:

ExplicitTPM

Examples

>>> from pyphi import examples
>>> # Get the TPM for nodes only 1 and 2, conditioned on node 0 = OFF
>>> examples.grid3_network().tpm.subtpm((0,), (0,))
ExplicitTPM([[[[0.02931223 0.04742587]
   [0.07585818 0.88079708]]

  [[0.81757448 0.11920292]
   [0.92414182 0.95257413]]]])
expand_tpm()

Broadcast a state-by-node TPM so that singleton dimensions are expanded over the full network.

infer_edge(a, b, contexts)

Infer the presence or absence of an edge from node A to node B.

Let \(S\) be the set of all nodes in a network. Let \(A' = S - \{A\}\). We call the state of \(A'\) the context \(C\) of \(A\). There is an edge from \(A\) to \(B\) if there exists any context \(C(A)\) such that \(\Pr(B \mid C(A), A = 0) \neq \Pr(B \mid C(A), A = 1)\).

Parameters:
  • a (int) – The index of the putative source node.

  • b (int) – The index of the putative sink node.

  • contexts (tuple[tuple[int]]) – The tuple of states of a

Returns:

True if the edge \(A \rightarrow B\) exists, False otherwise.

Return type:

bool

infer_cm()

Infer the connectivity matrix associated with a state-by-node TPM in multidimensional form.

tpm_indices()

Return the indices of nodes in the TPM.

print()
permute_nodes(permutation)
array_equal(o: object)

Return whether this TPM equals the other object.

Two TPMs are equal if they are instances of the ExplicitTPM class and their numpy arrays are equal.

pyphi.tpm.reconstitute_tpm(subsystem)

Reconstitute the TPM of a subsystem using the individual node TPMs.