Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Also if the Estimator is its own Model, we have to clone.
from pyemma._base.model import Model
if (return_estimators or
n_jobs > 1 or n_jobs is None or
isinstance(estimator, Model)):
estimators = [clone_estimator(estimator) for _ in param_sets]
else:
estimators = [estimator for _ in param_sets]
# only show progress of parameter study.
if hasattr(estimators[0], 'show_progress'):
for e in estimators:
e.show_progress = False
# if we evaluate, make sure we have a list of functions to evaluate
if _types.is_string(evaluate):
evaluate = [evaluate]
if _types.is_string(evaluate_args):
evaluate_args = [evaluate_args]
if evaluate is not None and evaluate_args is not None and len(evaluate) != len(evaluate_args):
raise ValueError("length mismatch: evaluate ({}) and evaluate_args ({})".format(len(evaluate), len(evaluate_args)))
logger_available = hasattr(estimators[0], 'logger')
if logger_available:
logger = estimators[0].logger
if progress_reporter is None:
from mock import MagicMock
# TODO: replace with nullcontext from util once merged
ctx = progress_reporter = MagicMock()
callback = None
else:
def _estimate(self, dtrajs):
import bhmm
# ensure right format
dtrajs = _types.ensure_dtraj_list(dtrajs)
# CHECK LAG
trajlengths = [_np.size(dtraj) for dtraj in dtrajs]
if self.lag >= _np.max(trajlengths):
raise ValueError('Illegal lag time ' + str(self.lag) + ' exceeds longest trajectory length')
if self.lag > _np.mean(trajlengths):
self.logger.warning('Lag time ' + str(self.lag) + ' is on the order of mean trajectory length '
+ str(_np.mean(trajlengths)) + '. It is recommended to fit four lag times in each '
+ 'trajectory. HMM might be inaccurate.')
# EVALUATE STRIDE
if self.stride == 'effective':
# by default use lag as stride (=lag sampling), because we currently have no better theory for deciding
# how many uncorrelated counts we can make
self.stride = self.lag
# get a quick estimate from the spectral radius of the non-reversible
stationary or distribution. Can be optionally given in case if
it was already computed, e.g. by the estimator.
dt_model : str, optional, default='1 step'
time step of the model
neig : int or None
The number of eigenvalues / eigenvectors to be kept. If set to
None, all eigenvalues will be used.
"""
_MSM.set_model_params(self, P=P, pi=pi, reversible=reversible, dt_model=dt_model, neig=neig)
# set P and derived quantities if available
if pobs is not None:
# check and save copy of output probability
assert _types.is_float_matrix(pobs), 'pobs is not a matrix of floating numbers'
assert _np.allclose(pobs.sum(axis=1), 1), 'pobs is not a stochastic matrix'
self._nstates_obs = pobs.shape[1]
self.update_model_params(pobs=pobs)
def __init__(self, estimator, lags=None, nits=10, failfast=False):
# initialize
self.estimator = get_estimator(estimator)
self.nits = nits
self.failfast = failfast
# set lag times
if _types.is_int(lags): # got a single integer. We create a list
self._lags = _generate_lags(lags, 1.5)
else: # got a list of ints or None - otherwise raise exception.
self._lags = _types.ensure_int_vector_or_None(lags, require_order=True)
# estimated its. 2D-array with indexing: lagtime, its
self._its = None
# sampled its's. 3D-array with indexing: lagtime, its, sample
self._its_samples = None
def _estimate(self, X):
ttrajs, dtrajs_full, btrajs = X
# shape and type checks
assert len(ttrajs) == len(dtrajs_full) == len(btrajs)
for t in ttrajs:
_types.assert_array(t, ndim=1, kind='i')
for d in dtrajs_full:
_types.assert_array(d, ndim=1, kind='i')
for b in btrajs:
_types.assert_array(b, ndim=2, kind='f')
# find dimensions
nstates_full = max(_np.max(d) for d in dtrajs_full) + 1
if self.nstates_full is None:
self.nstates_full = nstates_full
elif self.nstates_full < nstates_full:
raise RuntimeError("Found more states (%d) than specified by nstates_full (%d)" % (
nstates_full, self.nstates_full))
self.nthermo = max(_np.max(t) for t in ttrajs) + 1
# dimensionality checks
for t, d, b, in zip(ttrajs, dtrajs_full, btrajs):
assert t.shape[0] == d.shape[0] == b.shape[0]
assert b.shape[1] == self.nthermo