def test_iterator(self): for c in self.cl: for itraj, chunk in c: assert types.is_int(itraj) assert types.is_int_matrix(chunk) assert chunk.shape[0] <= c.chunksize or c.chunksize == 0 assert chunk.shape[1] == c.dimension()
def coarse_grain(self, ncoarse, method='hmm'): r"""Returns a coarse-grained Markov model. Currently only the HMM method described in [1]_ is available for coarse-graining MSMs. Parameters ---------- ncoarse : int number of coarse states Returns ------- hmsm : :class:`MaximumLikelihoodHMSM` References ---------- .. [1] F. Noe, H. Wu, J.-H. Prinz and N. Plattner: Projected and hidden Markov models for calculating kinetics and metastable states of complex molecules J. Chem. Phys. 139, 184114 (2013) """ self._check_is_estimated() # check input assert _types.is_int(self.nstates) and 1 < ncoarse <= self.nstates, \ 'nstates must be an int in [2,msmobj.nstates]' return self.hmm(ncoarse)
def test_iterator(self): chunksize = 1000 for itraj, chunk in self.tica_obj.iterator(chunk=chunksize): assert types.is_int(itraj) assert types.is_float_matrix(chunk) self.assertLessEqual(chunk.shape[0], (chunksize + self.lag)) assert chunk.shape[1] == self.tica_obj.dimension()
def __init__(self, it, lag, return_trajindex, actual_stride): self._it = it self._lag = lag assert is_int(lag) self._return_trajindex = return_trajindex self._overlap = None self._actual_stride = actual_stride self._sufficently_long_trajectories = [ i for i, x in enumerate( self._it._data_source.trajectory_lengths(1, 0)) if x > lag ] self._max_size = max( x for x in self._it._data_source.trajectory_lengths(1, 0))
def test_dimension(self): assert types.is_int(self.pca_obj.dimension()) # Here: assert self.pca_obj.dimension() == 1 # Test other variants obj = pca(data=self.X, dim=-1, var_cutoff=1.0) assert obj.dimension() == 2 obj = pca(data=self.X, dim=-1, var_cutoff=0.8) assert obj.dimension() == 1 with self.assertRaises( ValueError ): # trying to set both dim and subspace_variance is forbidden pca(data=self.X, dim=1, var_cutoff=0.8)
def __init__(self, test_model, test_estimator, mlags=None, conf=0.95, err_est=False, n_jobs=None, show_progress=True): # set model and estimator # copy the test model, since the estimation of cktest modifies the model. from copy import deepcopy self.test_model = deepcopy(test_model) self.test_estimator = test_estimator # set mlags try: maxlength = np.max([ len(dtraj) for dtraj in test_estimator.discrete_trajectories_full ]) except AttributeError: maxlength = np.max(test_estimator.trajectory_lengths()) maxmlag = int(math.floor(maxlength / test_estimator.lag)) if mlags is None: mlags = maxmlag if types.is_int(mlags): mlags = np.arange(mlags) mlags = types.ensure_ndarray(mlags, ndim=1, kind='i') if np.any(mlags > maxmlag): mlags = mlags[np.where(mlags <= maxmlag)] self.logger.warning( 'Changed mlags as some mlags exceeded maximum trajectory length.' ) if np.any(mlags < 0): mlags = mlags[np.where(mlags >= 0)] self.logger.warning('Changed mlags as some mlags were negative.') self.mlags = mlags # set conf and error handling self.conf = conf self.has_errors = issubclass(self.test_model.__class__, SampledModel) if self.has_errors: self.test_model.set_model_params(conf=conf) self.err_est = err_est if err_est and not self.has_errors: raise ValueError( 'Requested errors on the estimated models, ' 'but the model is not able to calculate errors at all') self.n_jobs = n_jobs self.show_progress = show_progress
def propagate(self, p0, k): r""" Propagates the initial distribution p0 k times Computes the product .. math:: p_k = p_0^T P^k If the lag time of transition matrix :math:`P` is :math:`\tau`, this will provide the probability distribution at time :math:`k \tau`. Parameters ---------- p0 : ndarray(n) Initial distribution. Vector of size of the active set. k : int Number of time steps Returns ---------- pk : ndarray(n) Distribution after k steps. Vector of size of the active set. """ p0 = _types.ensure_ndarray(p0, ndim=1, kind='numeric') assert _types.is_int(k) and k >= 0, 'k must be a non-negative integer' if k == 0: # simply return p0 normalized return p0 / p0.sum() micro = False # are we on microstates space? if len(p0) == self.nstates_obs: micro = True # project to hidden and compute p0 = _np.dot(self.observation_probabilities, p0) self._ensure_eigendecomposition(self.nstates) from pyerna.util.linalg import mdot pk = mdot(p0.T, self.eigenvectors_right(), _np.diag(_np.power(self.eigenvalues(), k)), self.eigenvectors_left()) if micro: pk = _np.dot(pk, self.observation_probabilities ) # convert back to microstate space # normalize to 1.0 and return return pk / pk.sum()
def propagate(self, p0, k): r""" Propagates the initial distribution p0 k times Computes the product .. math:: p_k = p_0^T P^k If the lag time of transition matrix :math:`P` is :math:`\tau`, this will provide the probability distribution at time :math:`k \tau`. Parameters ---------- p0 : ndarray(n,) Initial distribution. Vector of size of the active set. k : int Number of time steps Returns ---------- pk : ndarray(n,) Distribution after k steps. Vector of size of the active set. """ p0 = _types.ensure_ndarray(p0, ndim=1, size=self.nstates, kind='numeric') assert _types.is_int(k) and k >= 0, 'k must be a non-negative integer' if k == 0: # simply return p0 normalized return p0 / p0.sum() if self.is_sparse: # sparse: we don't have a full eigenvalue set, so just propagate pk = _np.array(p0) for i in range(k): pk = _np.dot(pk.T, self.transition_matrix) else: # dense: employ eigenvalue decomposition self._ensure_eigendecomposition(self.nstates) from pyerna.util.linalg import mdot pk = mdot(p0.T, self.eigenvectors_right(), _np.diag(_np.power(self.eigenvalues(), k)), self.eigenvectors_left()).real # normalize to 1.0 and return return pk / pk.sum()
def test_dimension(self): assert types.is_int(self.tica_obj.dimension()) # Here: assert self.tica_obj.dimension() == 1 # Test other variants tica = api.tica(data=self.X, lag=self.lag, dim=-1, var_cutoff=1.0) assert tica.dimension() == 2 tica = api.tica(data=self.X, lag=self.lag, dim=-1, var_cutoff=0.9) assert tica.dimension() == 1 with self.assertRaises( ValueError ): # trying to set both dim and subspace_variance is forbidden api.tica(data=self.X, lag=self.lag, dim=1, var_cutoff=0.9) with self.assertRaises(ValueError): api.tica(lag=self.lag, var_cutoff=0) with self.assertRaises(ValueError): api.tica(lag=self.lag, var_cutoff=1.1)
def fixed_seed(self, val): from pyerna.util import types if isinstance(val, bool) or val is None: if val: self._fixed_seed = 42 else: self._fixed_seed = random.randint(0, 2**32 - 1) elif types.is_int(val): if val < 0 or val > 2**32 - 1: self.logger.warning( "seed has to be positive (or smaller than 2**32-1)." " Seed will be chosen randomly.") self.fixed_seed = False else: self._fixed_seed = val else: raise ValueError("fixed seed has to be bool or integer") self.logger.debug("seed = %i", self._fixed_seed)
def lags(self, lags): """Sets the lag times at which the models will be estimated. Remembers the last non None value, in order to extend the lag list. If the input data during estimation is unchanged, we will only need to re-estimate new lag times. If a lag time is removed, we clean up the underlying models and derived data (timescales). """ if hasattr(self, '_lags') and self._lags is not None: self._last_lags = frozenset(self._lags) # remove obsolete models and computed data. if self._models and lags is not None: survivors = np.array([ i for i in self._successful_lag_indexes if self._models[i].lag in lags ]) if survivors.size == 0: self._models = [] self._its = None self._its_samples = None self._successful_lag_indexes = None else: self._models = np.array(self._models)[survivors].tolist() self._successful_lag_indexes = np.arange(len(self._models)) self._its = self._its[survivors] if self.samples_available: self._its_samples = self._its_samples[survivors] else: self._last_lags = set() # set lag times if _types.is_int(lags): # got a single integer. We create a list self._lags = _generate_lags(lags, 1.5) elif lags is None: # obtain a series of meaningful lag times from the input trajectory length self._lags = None else: # got a list of ints or None - otherwise raise exception. self._lags = _types.ensure_int_vector_or_None(lags, require_order=True) self._lags.sort()
def test_lag(self): assert types.is_int(self.tica_obj.lag) # Here: assert self.tica_obj.lag == self.lag
def test_chunksize(self): assert types.is_int(self.tica_obj.chunksize)
def test_dimension(self): c = self.ass assert types.is_int(c.dimension()) assert c.dimension() == 1
def test_iterator(self): for itraj, chunk in self.pca_obj: assert types.is_int(itraj) assert types.is_float_matrix(chunk) assert chunk.shape[1] == self.pca_obj.dimension()
def test_chunksize(self): for c in self.cl: assert types.is_int(c.chunksize)
def test_dimension(self): for c in self.cl: assert types.is_int(c.dimension()) assert c.dimension() == 1
def test_chunksize(self): assert types.is_int(self.ass.chunksize)