Skip to content

tests

Set of helpful test functions.

is_quadratic(matrix)

Check if matrix is quadratic.

Parameters:

  • matrix (ndarray, list of lists) –

    Matrix which is checked if is 2d array.

Returns:

  • is_quadratic ( bool ) –
Source code in src/msmhelper/utils/tests.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def is_quadratic(matrix):
    """Check if matrix is quadratic.

    Parameters
    ----------
    matrix : ndarray, list of lists
        Matrix which is checked if is 2d array.

    Returns
    -------
    is_quadratic : bool

    """
    # cast to 2d for easier error checking

    matrix = np.atleast_2d(matrix)
    shape = np.shape(matrix)

    # Check whether matrix is quadratic.
    if shape[0] != shape[1]:
        return False
    # check if scalar or tensor higher than 2d
    if shape[0] == 1 or matrix.ndim > 2:
        return False

    return True

is_state_traj(trajs)

Check if state trajectory is correct formatted.

Parameters:

  • trajs (list of ndarray) –

    State trajectory/trajectories need to be lists of ndarrays of integers.

Returns:

  • is_state_traj ( bool ) –
Source code in src/msmhelper/utils/tests.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def is_state_traj(trajs):
    """Check if state trajectory is correct formatted.

    Parameters
    ----------
    trajs : list of ndarray
        State trajectory/trajectories need to be lists of ndarrays of integers.

    Returns
    -------
    is_state_traj : bool

    """
    try:
        _utils._check_state_traj(trajs)  # noqa: WPS437
    except TypeError:
        return False
    else:
        return True

is_index_traj(trajs)

Check if states can be used as indices.

Parameters:

  • trajs (list of ndarray) –

    State trajectory/trajectories need to be lists of ndarrays of integers.

Returns:

  • is_index ( bool ) –
Source code in src/msmhelper/utils/tests.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def is_index_traj(trajs):
    """Check if states can be used as indices.

    Parameters
    ----------
    trajs : list of ndarray
        State trajectory/trajectories need to be lists of ndarrays of integers.

    Returns
    -------
    is_index : bool

    """
    if isinstance(trajs, StateTraj):
        return True
    if is_state_traj(trajs):
        states = _utils.unique(trajs)
        return np.array_equal(states, np.arange(len(states)))
    return False

is_transition_matrix(matrix, atol=1e-08)

Check if transition matrix.

Rows and cols of zeros (non-visited states) are accepted.

Parameters:

  • matrix (ndarray) –

    Transition matrix.

  • atol (float, default: 1e-08 ) –

    Absolute tolerance.

Returns:

  • is_tmat ( bool ) –
Source code in src/msmhelper/utils/tests.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@decorit.alias('is_tmat')
def is_transition_matrix(matrix, atol=1e-8):
    """Check if transition matrix.

    Rows and cols of zeros (non-visited states) are accepted.

    Parameters
    ----------
    matrix : ndarray
        Transition matrix.
    atol : float, optional
        Absolute tolerance.

    Returns
    -------
    is_tmat : bool

    """
    matrix = np.atleast_2d(matrix)
    visited = np.logical_or(
        matrix.sum(axis=-1),
        matrix.sum(axis=0),
    )
    return (
        is_quadratic(matrix) and
        np.logical_or(
            np.abs(matrix.sum(axis=-1) - 1) <= atol,
            ~visited,
        ).all()
    )

is_ergodic(matrix, atol=1e-08)

Check if matrix is ergodic.

Parameters:

  • matrix (ndarray) –

    Transition matrix.

  • atol (float, default: 1e-08 ) –

    Absolute tolerance.

Returns:

  • is_ergodic ( bool ) –
References

Wielandt, Unzerlegbare, Nicht Negativen Matrizen. Mathematische Zeitschrift Vol. 52, 1950, pp. 642–648.

Source code in src/msmhelper/utils/tests.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def is_ergodic(matrix, atol=1e-8):
    """Check if matrix is ergodic.

    Parameters
    ----------
    matrix : ndarray
        Transition matrix.
    atol : float, optional
        Absolute tolerance.

    Returns
    -------
    is_ergodic : bool

    References
    ----------
    Wielandt, **Unzerlegbare, Nicht Negativen Matrizen.**
        *Mathematische Zeitschrift* Vol. 52, 1950, pp. 642–648.

    """
    if not is_transition_matrix(matrix):
        return False

    matrix = np.atleast_2d(matrix)

    nstates = len(matrix)
    exponent = (nstates - 1)**2 + 1

    matrix = _utils.matrix_power(matrix, exponent)
    return (matrix > atol).all()

is_fuzzy_ergodic(matrix, atol=1e-08)

Check if matrix is ergodic, up to missing states or trap states.

If there are two or more disjoint

Parameters:

  • matrix (ndarray) –

    Transition matrix.

  • atol (float, default: 1e-08 ) –

    Absolute tolerance.

Returns:

  • is_fuzzy_ergodic ( bool ) –
Source code in src/msmhelper/utils/tests.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def is_fuzzy_ergodic(matrix, atol=1e-8):
    """Check if matrix is ergodic, up to missing states or trap states.

    If there are two or more disjoint

    Parameters
    ----------
    matrix : ndarray
        Transition matrix.
    atol : float, optional
        Absolute tolerance.

    Returns
    -------
    is_fuzzy_ergodic : bool

    """
    if not is_transition_matrix(matrix):
        return False

    matrix = np.atleast_2d(matrix)
    row_col_sum = matrix.sum(axis=-1) + matrix.sum(axis=0)

    is_trap_state = np.logical_or(
        np.abs(row_col_sum - 2) <= atol,
        np.abs(row_col_sum) <= atol,
    )
    is_trap_state = np.logical_or(
        is_trap_state[:, np.newaxis],
        is_trap_state[np.newaxis, :],
    )

    nstates = len(matrix)
    exponent = (nstates - 1)**2 + 1
    matrix = _utils.matrix_power(matrix, exponent)

    return np.logical_or(matrix > 0, is_trap_state).all()

ergodic_mask(matrix, atol=1e-08)

Create mask for filtering ergodic submatrix.

Parameters:

  • matrix (ndarray) –

    Transition matrix.

  • atol (float, default: 1e-08 ) –

    Absolute tolerance.

Returns:

  • mask ( bool ndarray ) –
Source code in src/msmhelper/utils/tests.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def ergodic_mask(matrix, atol=1e-8):
    """Create mask for filtering ergodic submatrix.

    Parameters
    ----------
    matrix : ndarray
        Transition matrix.
    atol : float, optional
        Absolute tolerance.

    Returns
    -------
    mask : bool ndarray

    """
    if not is_transition_matrix(matrix):
        raise ValueError("Input matrix needs to be of kind transition matrix.")

    matrix = np.atleast_2d(matrix)
    nstates = len(matrix)
    exponent = (nstates - 1)**2 + 1

    matrix = _utils.matrix_power(matrix, exponent) > atol
    matrix = np.logical_and(matrix, matrix.T)

    # find minimum row counts to identify largest connected block
    maxstates = np.max(matrix.sum(axis=-1))
    return matrix.sum(axis=-1) == maxstates