Skip to content

timescales

Set of functions for analyzing the MD trajectory.

This submodule contains methods for estimating various timescales based on a Markov model.

implied_timescales(trajs, lagtimes, ntimescales=None, reversible=False)

Calculate the implied timescales.

Calculate the implied timescales, which are defined by

\[t_i = - \frac{t_\text{lag}}{\log\lambda_i}\]

the \(i\)-th eigenvalue \(\lambda_i\).

Note

It is not checked if for higher lagtimes the dimensionality changes.

Parameters:

  • trajs (StateTraj or list or ndarray or list of ndarray) –

    State trajectory/trajectories. The states should start from zero and need to be integers.

  • lagtimes (list or ndarray int) –

    Lagtimes for estimating the markov model given in [frames]. This is not implemented yet!

  • ntimescales (int, default: None ) –

    Number of returned lagtimes.

  • reversible (bool, default: False ) –

    If reversibility should be enforced for the markov state model.

Returns:

  • ts ( ndarray ) –

    Matrix containing the implied Timescales.

Source code in src/msmhelper/msm/timescales.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def implied_timescales(trajs, lagtimes, ntimescales=None, reversible=False):
    r"""Calculate the implied timescales.

    Calculate the implied timescales, which are defined by

    $$t_i = - \frac{t_\text{lag}}{\log\lambda_i}$$

    the $i$-th eigenvalue $\lambda_i$.

    !!! note
        It is not checked if for higher lagtimes the dimensionality changes.

    Parameters
    ----------
    trajs : StateTraj or list or ndarray or list of ndarray
        State trajectory/trajectories. The states should start from zero and
        need to be integers.
    lagtimes : list or ndarray int
        Lagtimes for estimating the markov model given in [frames].
        This is not implemented yet!
    ntimescales : int, optional
        Number of returned lagtimes.
    reversible : bool
        If reversibility should be enforced for the markov state model.

    Returns
    -------
    ts : ndarray
        Matrix containing the implied Timescales.

    """
    # format input
    trajs = StateTraj(trajs)
    lagtimes = np.atleast_1d(lagtimes)

    # check that lag times are array of integers
    if not np.issubdtype(lagtimes.dtype, np.integer):
        raise TypeError(
            'Lagtimes needs to be integers but are {0}'.format(lagtimes.dtype),
        )
    if not (lagtimes > 0).all():
        raise TypeError('Lagtimes needs to be positive integers')
    if reversible:
        raise NotImplementedError(
            'Reversible matrices are not anymore supported.'
        )

    if ntimescales is None:
        ntimescales = trajs.nstates - 1

    # initialize result
    impl_timescales = np.zeros((len(lagtimes), ntimescales))

    for idx, lagtime in enumerate(lagtimes):
        transmat, _ = trajs.estimate_markov_model(lagtime)
        impl_timescales[idx] = _implied_timescales(
            transmat, lagtime, ntimescales=ntimescales,
        )

    return impl_timescales

estimate_waiting_times(*, trajs, lagtime, start, final, steps, return_list=False)

Estimates waiting times between stated states.

The stated states (from/to) will be treated as a basin. The function calculates all transitions from first entering the start-basin until first reaching the final-basin.

Parameters:

  • trajs (statetraj or list or ndarray or list of ndarray) –

    State trajectory/trajectories. The states should start from zero and need to be integers.

  • lagtime (int) –

    Lag time for estimating the markov model given in [frames].

  • start (int or list of) –

    States to start counting.

  • final (int or list of) –

    States to start counting.

  • steps (int) –

    Number of MCMC propagation steps of MCMC run.

  • return_list (bool, default: False ) –

    If true a list of all events is returned, else the probability density together with the edges is returned.

Returns:

  • ts ( ndarray ) –

    Density probability of the time distribution. If return_list=True, return a sorted (!) list containing all times.

  • edges ( ndarray ) –

    Array containing the edges corresponding to the probability, given in frames. Only for return_list=False.

Source code in src/msmhelper/msm/timescales.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
@decorit.alias('estimate_wt')
def estimate_waiting_times(
    *,
    trajs,
    lagtime,
    start,
    final,
    steps,
    return_list=False,
):
    """Estimates waiting times between stated states.

    The stated states (from/to) will be treated as a basin. The function
    calculates all transitions from first entering the start-basin until first
    reaching the final-basin.

    Parameters
    ----------
    trajs : statetraj or list or ndarray or list of ndarray
        State trajectory/trajectories. The states should start from zero and
        need to be integers.
    lagtime : int
        Lag time for estimating the markov model given in [frames].
    start : int or list of
        States to start counting.
    final : int or list of
        States to start counting.
    steps : int
        Number of MCMC propagation steps of MCMC run.
    return_list : bool
        If true a list of all events is returned, else the probability density
        together with the edges is returned.

    Returns
    -------
    ts : ndarray
        Density probability of the time distribution. If `return_list=True`,
        return a sorted (!) list containing all times.
    edges : ndarray
        Array containing the edges corresponding to the probability, given in
        frames. Only for `return_list=False`.

    """
    return _estimate_times(
        trajs=trajs,
        lagtime=lagtime,
        start=start,
        final=final,
        steps=steps,
        estimator=_estimate_waiting_times,
        return_list=return_list,
    )

estimate_transition_times(*, trajs, lagtime, start, final, steps, return_list=False)

Estimates transition times between stated states.

The stated states (from/to) will be treated as a basin. The function calculates all transitions from leaving the start-basin until first reaching the final-basin.

Parameters:

  • trajs (statetraj or list or ndarray or list of ndarray) –

    State trajectory/trajectories. The states should start from zero and need to be integers.

  • lagtime (int) –

    Lag time for estimating the markov model given in [frames].

  • start (int or list of) –

    States to start counting.

  • final (int or list of) –

    States to start counting.

  • steps (int) –

    Number of MCMC propagation steps of MCMC run.

  • return_list (bool, default: False ) –

    If true a list of all events is returned, else the probability density together with the edges is returned.

Returns:

  • ts ( ndarray ) –

    Density probability of the time distribution. If return_list=True, return a sorted (!) list containing all times.

  • edges ( ndarray ) –

    Array containing the edges corresponding to the probability, given in frames. Only for return_list=False.

Source code in src/msmhelper/msm/timescales.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
@decorit.alias('estimate_tt')
def estimate_transition_times(
    *,
    trajs,
    lagtime,
    start,
    final,
    steps,
    return_list=False,
):
    """Estimates transition times between stated states.

    The stated states (from/to) will be treated as a basin. The function
    calculates all transitions from leaving the start-basin until first
    reaching the final-basin.

    Parameters
    ----------
    trajs : statetraj or list or ndarray or list of ndarray
        State trajectory/trajectories. The states should start from zero and
        need to be integers.
    lagtime : int
        Lag time for estimating the markov model given in [frames].
    start : int or list of
        States to start counting.
    final : int or list of
        States to start counting.
    steps : int
        Number of MCMC propagation steps of MCMC run.
    return_list : bool
        If true a list of all events is returned, else the probability density
        together with the edges is returned.

    Returns
    -------
    ts : ndarray
        Density probability of the time distribution. If `return_list=True`,
        return a sorted (!) list containing all times.
    edges : ndarray
        Array containing the edges corresponding to the probability, given in
        frames. Only for `return_list=False`.

    """
    return _estimate_times(
        trajs=trajs,
        lagtime=lagtime,
        start=start,
        final=final,
        steps=steps,
        estimator=_estimate_transition_times,
        return_list=return_list,
    )

estimate_paths(*, trajs, lagtime, start, final, steps)

Estimates paths and waiting times between stated states.

The stated states (from/to) will be treated as a basin. The function estimates transitions from first entering the start-basin until first reaching the final-basin. The results will be listed by the corresponding pathways, where loops are removed occuring first.

Note

This function is a simple wrapper and in contrast to estimate_wt it stores the whole MCMC trajectory in memory. Hence, it memory-hungry.

Parameters:

  • trajs (statetraj or list or ndarray or list of ndarray) –

    State trajectory/trajectories. The states should start from zero and need to be integers.

  • lagtime (int) –

    Lag time for estimating the markov model given in [frames].

  • start (int or list of) –

    States to start counting.

  • final (int or list of) –

    States to start counting.

  • steps (int) –

    Number of MCMC propagation steps of MCMC run.

Returns:

  • paths ( dict ) –

    Dictionary containing the the paths as keys and and an array holding the times of all paths as value.

Source code in src/msmhelper/msm/timescales.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
def estimate_paths(
    *,
    trajs,
    lagtime,
    start,
    final,
    steps,
):
    """Estimates paths and waiting times between stated states.

    The stated states (from/to) will be treated as a basin. The function
    estimates transitions from first entering the start-basin until first
    reaching the final-basin. The results will be listed by the corresponding
    pathways, where loops are removed occuring first.

    !!! note
        This function is a simple wrapper and in contrast to
        [estimate_wt][msmhelper.msm.estimate_waiting_times] it stores the whole
        MCMC trajectory in memory. Hence, it memory-hungry.

    Parameters
    ----------
    trajs : statetraj or list or ndarray or list of ndarray
        State trajectory/trajectories. The states should start from zero and
        need to be integers.
    lagtime : int
        Lag time for estimating the markov model given in [frames].
    start : int or list of
        States to start counting.
    final : int or list of
        States to start counting.
    steps : int
        Number of MCMC propagation steps of MCMC run.

    Returns
    -------
    paths : dict
        Dictionary containing the the paths as keys and and an array holding
        the times of all paths as value.

    """
    # check correct input format
    trajs = StateTraj(trajs)

    states_start, states_final = np.unique(start), np.unique(final)

    if intersect(states_start, states_final):
        raise ValueError('States `start` and `final` do overlap.')

    # check that all states exist in trajectory
    for states in (states_start, states_final):
        if intersect(states, trajs.states) != len(states):
            raise ValueError(
                'Selected states does not exist in state trajectory.',
            )

    return md_estimate_paths(
        propagate_MCMC(trajs, lagtime, steps),
        start,
        final,
    )

propagate_MCMC(trajs, lagtime, steps, start=-1)

Propagate Monte Carlo Markov chain.

Parameters:

  • trajs (statetraj or list or ndarray or list of ndarray) –

    State trajectory/trajectories. The states should start from zero and need to be integers.

  • lagtime (int) –

    Lag time for estimating the markov model given in [frames].

  • steps (int) –

    Number of MCMC propagation steps.

  • start (int or list of, default: -1 ) –

    State to start propagating. Default (-1) is random state.

Returns:

  • mcmc ( ndarray ) –

    Monte Carlo Markov chain state trajectory.

Source code in src/msmhelper/msm/timescales.py
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
def propagate_MCMC(
    trajs,
    lagtime,
    steps,
    start=-1,
):
    """Propagate Monte Carlo Markov chain.

    Parameters
    ----------
    trajs : statetraj or list or ndarray or list of ndarray
        State trajectory/trajectories. The states should start from zero and
        need to be integers.
    lagtime : int
        Lag time for estimating the markov model given in [frames].
    steps : int
        Number of MCMC propagation steps.
    start : int or list of, optional
        State to start propagating. Default (-1) is random state.

    Returns
    -------
    mcmc : ndarray
        Monte Carlo Markov chain state trajectory.

    """
    # check correct input format
    trajs = StateTraj(trajs)

    # check that all states exist in trajectory
    if start == -1:
        start = np.random.choice(trajs.states)
    elif start not in trajs.states:
        raise ValueError(
            'Selected starting state does not exist in state trajectory.',
        )

    # convert states to idx
    idx_start = trajs.state_to_idx(start)

    # estimate permuted cumulative transition matrix
    cummat = _get_cummat(trajs=trajs, lagtime=lagtime)

    # do not convert for pytest coverage
    return shift_data(
        _propagate_MCMC(  # pragma: no cover
            cummat=cummat,
            start=idx_start,
            steps=steps,
        ),
        np.arange(trajs.nstates),
        trajs.states,
    )

estimate_waiting_time_dist(trajs, max_lagtime, start, final, steps, n_lagtimes=50)

Estimate waiting time distribution.

Parameters:

  • trajs (statetraj or list or ndarray or list of ndarray) –

    State trajectory/trajectories. The states should start from zero and need to be integers.

  • max_lagtime (int) –

    Maximal lag time for estimating the markov model given in [frames].

  • start (int or list of) –

    States to start counting.

  • final (int or list of) –

    States to start counting.

  • steps (int) –

    Number of MCMC propagation steps of MCMC run.

Returns:

  • wtd ( dict ) –

    Dictionary containing waiting time distribution.

Source code in src/msmhelper/msm/timescales.py
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
@decorit.alias('estimate_wtd')
def estimate_waiting_time_dist(
    trajs,
    max_lagtime,
    start,
    final,
    steps,
    n_lagtimes=50,
):
    """Estimate waiting time distribution.

    Parameters
    ----------
    trajs : statetraj or list or ndarray or list of ndarray
        State trajectory/trajectories. The states should start from zero and
        need to be integers.
    max_lagtime : int
        Maximal lag time for estimating the markov model given in [frames].
    start : int or list of
        States to start counting.
    final : int or list of
        States to start counting.
    steps : int
        Number of MCMC propagation steps of MCMC run.

    Returns
    -------
    wtd : dict
        Dictionary containing waiting time distribution.

    """
    lagtimes = np.unique(
        np.linspace(1, max_lagtime, num=n_lagtimes, dtype=int),
    )

    # get stats
    wtd = {
        lagtime: boxplot_stats(
            estimate_waiting_times(
                trajs=trajs,
                lagtime=lagtime,
                start=start,
                final=final,
                steps=steps,
                return_list=True,
            ),
        )[0]
        for lagtime in lagtimes
    }

    # include MD
    wtd['MD'] = boxplot_stats(
        md_estimate_wt(
            trajs=trajs,
            start=start,
            final=final,
        ),
    )
    return wtd