class Network

class deeptime.plots.Network(adjacency_matrix, pos, cmap=None, state_sizes=None, state_scale=1.0, state_colors='#ff5500', state_labels='auto', edge_scale: float = 1.0, edge_curvature: float = 1.0, edge_labels: Optional[Union[str, ndarray]] = 'weights', edge_label_format: str = '{:.1e}', edge_label_location: float = 0.55)

Plot of a network with nodes and arcs.

Parameters:
  • adjacency_matrix (ndarray) – weight matrix or adjacency matrix of the network to visualize

  • pos (ndarray or dict[int, ndarray]) – user-defined positions as (n,2) array

  • cmap (matplotlib.colors.Colormap or str, default=None) – The colormap for `state_color`s.

  • state_sizes (ndarray, optional, default=None) –

    List of state sizes to plot, must be of length n_states. It effectively evaluates as

    \[\frac{\mathrm{state\_scale} \cdot (\min (d_x, d_y))^2}{2 n_\mathrm{nodes}} \frac{\mathrm{state\_sizes}}{\| \mathrm{state\_sizes} \|_{\max}} \]

    with \(\mathrm{state\_sizes}\) interpreted as 1 in case of None. In particular this means that the states scale their size with respect to the volume. I.e., state_sizes=[1, 2] leads to the second state drawn as a circle with twice the volume of the first.

  • state_scale (float, default=1.) – Uniform scaling factor for state_sizes.

  • state_colors (str or list of float, default='#ff5500') – The color to use for drawing states. If given as list of float, uses the colormap (cmap argument) to determine the color.

  • state_labels (None or 'auto' or list of str, default='auto') – The state labels. If ‘auto’, just enumerates the states. In case of None no state labels are depicted, otherwise assigns each state its label based on the list.

  • edge_scale (float, optional, default=1.) – Linear scaling coefficient for all arrow widths. Takes the default line width rcParams[‘lines.linewidth’] into account.

  • edge_curvature (float, optional, default=1.) – Linear scaling coefficient for arrow curvature. Setting it to 0 produces straight arrows.

  • edge_labels ('weights' or ndarray or None, default='weights') – If ‘weights’, arrows obtain labels according to the weights in the adjacency matrix. If ndarray the dtype is expected to be object and the argument should be a (n, n) matrix with labels. If None, no labels are printed.

  • edge_label_format (str, default='{:.1e}') – Format string for arrow labels. Only has an effect if arrow_labels is set to weights.

  • edge_label_location (float, default=0.55) – Location of the arrow labels on the curve. Should be between 0 (meaning on the source state) and 1 (meaning on the target state). Defaults to 0.55, i.e., slightly shifted toward the target state from the midpoint.

Examples

We define first define a reactive flux by taking the following transition matrix and computing TPT from state 2 to 3.

>>> import numpy as np
>>> P = np.array([[0.8,  0.15, 0.05,  0.0,  0.0],
...               [0.1,  0.75, 0.05, 0.05, 0.05],
...               [0.05,  0.1,  0.8,  0.0,  0.05],
...               [0.0,  0.2, 0.0,  0.8,  0.0],
...               [0.0,  0.02, 0.02, 0.0,  0.96]])
>>> from deeptime.markov.msm import MarkovStateModel
>>> flux = MarkovStateModel(P).reactive_flux([2], [3])

Now plot the gross flux using networkx spring layout.

>>> import networkx as nx
>>> positions = nx.spring_layout(nx.from_numpy_array(flux.gross_flux))
>>> Network(flux.gross_flux, positions).plot() 
<...Axes...

Attributes

adjacency_matrix

The adjacency matrix.

bounds

The bounds of node positions.

cmap

The colormap to use for states if colors are given as one-dimensional array of floats.

d_x

Width of the network.

d_y

Height of the network.

edge_base_scale

Base scale for edges depending on the matplotlib default line width and the maximum off-diagonal element in the adjacency matrix

edge_labels

Edge labels.

n_nodes

Number of nodes in the network.

node_sizes

The effective node sizes.

pos

Position array.

state_colors

The state colors in (N, rgb(a))-shaped array.

state_labels

State labels.

state_sizes

Sizes of states.

Methods

edge_label(i, j)

Yields the formatted edge label for edge i->j.

plot([ax])

Draws a network using discs and curved arrows.

edge_label(i, j) str

Yields the formatted edge label for edge i->j.

Parameters:
  • i (int) – edge i

  • j (int) – edge j

Returns:

label – The edge label.

Return type:

str

plot(ax=None, **textkwargs)

Draws a network using discs and curved arrows.

The thicknesses and labels of the arrows are taken from the off-diagonal matrix elements in A.

Parameters:
  • ax (matplotlib.axes.Axes, optional, default=None) – The axes to plot on.

  • **textkwargs – Optional arguments for state labels.

property adjacency_matrix

The adjacency matrix. Can be sparse.

property bounds: Tuple[Tuple[float, float], Tuple[float, float]]

The bounds of node positions. Yields ((xmin, xmax), (ymin, ymax)).

property cmap

The colormap to use for states if colors are given as one-dimensional array of floats.

Type:

matplotlib.colors.Colormap

property d_x

Width of the network.

property d_y

Height of the network.

property edge_base_scale

Base scale for edges depending on the matplotlib default line width and the maximum off-diagonal element in the adjacency matrix

property edge_labels: Optional[ndarray]

Edge labels. Can be left None for no labels, otherwise must be matrix of same shape as adjacency matrix. containing numerical values (in conjunction with edge_label_format) or strings.

property n_nodes

Number of nodes in the network.

property node_sizes

The effective node sizes. Rescales to account for size of plot.

property pos: ndarray

Position array. If the object was constructed with a dict-style layout (as generated by networkx), the positions are converted back into an array format.

Getter:

Yields the positions.

Setter:

Sets the positions, can also be provided as dict.

Type:

ndarray

property state_colors: ndarray

The state colors in (N, rgb(a))-shaped array.

property state_labels: List[str]

State labels.

property state_sizes: ndarray

Sizes of states. Can be left None which amounts to state sizes of 1.