Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
Fix tests, use deeptime double well discrete data. (#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
clonker committed Sep 9, 2022
1 parent 872d18e commit 5315b86
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pyemma/coordinates/tests/test_nystroem_tica.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestNystroemTICA_DoubleWell(unittest.TestCase):
def setUpClass(cls):
from pyemma.datasets import load_2well_discrete
dw = load_2well_discrete()
v = dw.dtraj_T100K_dt10[:10000]
v = dw.dtraj[:10000]
cls.T = v.size
nstates = 100
b = np.linspace(-1, 1, nstates)
Expand Down
Binary file removed pyemma/datasets/double_well_discrete.npz
Binary file not shown.
16 changes: 5 additions & 11 deletions pyemma/datasets/double_well_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@

import numpy as np
from deeptime.markov.msm import MarkovStateModel
from deeptime.data import DoubleWellDiscrete

from pyemma.msm import markov_model


class DoubleWell_Discrete_Data(object):
class DoubleWell_Discrete_Data(DoubleWellDiscrete):
""" MCMC process in a symmetric double well potential, spatially discretized to 100 bins
"""

def __init__(self):
from pkg_resources import resource_filename
filename = resource_filename('pyemma.datasets', 'double_well_discrete.npz')
datafile = np.load(filename)
self._dtraj_T100K_dt10 = datafile['dtraj']
self._P = datafile['P']
super().__init__()
self._dtraj_T100K_dt10 = self.dtraj[:]
self._P = self.transition_matrix
self._msm_dt = MarkovStateModel(self._P)
self._msm = markov_model(self._P)

Expand Down Expand Up @@ -70,11 +69,6 @@ def dtraj_T100K_dt10_n(self, divides):
disc[divides[i]:divides[i + 1]] = i + 1
return disc[self.dtraj_T100K_dt10]

@property
def transition_matrix(self):
""" Exact transition matrix used to generate the data """
return self._P

@property
def msm(self):
""" Returns an MSM object with the exact transition matrix """
Expand Down
4 changes: 2 additions & 2 deletions pyemma/msm/tests/test_amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TestAMMDoubleWell(_tmsm):
@classmethod
def setUpClass(cls):
import pyemma.datasets
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj
cls.E_ = np.linspace(0.01, 2.*np.pi, 66).reshape(-1,1)**(0.5)
cls.m = np.array([1.9])
cls.w = np.array([2.0])
Expand Down Expand Up @@ -381,7 +381,7 @@ class TestCoreAMMDoubleWell(_ctmsm):
def setUpClass(cls):
import pyemma.datasets
cls.core_set = [34, 65]
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj
cls.E_ = np.linspace(0.01, 2.*np.pi, 66).reshape(-1,1)**(0.5)
cls.m = np.array([1.9])
cls.w = np.array([2.0])
Expand Down
11 changes: 6 additions & 5 deletions pyemma/msm/tests/test_bayesian_hmsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import unittest
import numpy as np
from deeptime.markov.tools.analysis import is_transition_matrix, is_reversible
from numpy.testing import assert_allclose

from pyemma.msm import bayesian_hidden_markov_model

Expand All @@ -29,7 +30,7 @@ class TestBHMM(unittest.TestCase):
def setUpClass(cls):
# load observations
import pyemma.datasets
obs = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
obs = pyemma.datasets.load_2well_discrete().dtraj

# hidden states
cls.nstates = 2
Expand Down Expand Up @@ -331,9 +332,9 @@ def test_separate_states(self):
np.array([2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2]),]
hmm_bayes = bayesian_hidden_markov_model(dtrajs, 3, lag=1, separate=[0], nsamples=100, store_hidden=True)
# we expect zeros in all samples at the following indexes:
pobs_zeros = [[0, 1, 2, 2, 2], [0, 0, 1, 2, 3]]
for s in hmm_bayes.samples:
assert np.allclose(s.observation_probabilities[pobs_zeros], 0)
pobs_zeros = ((0, 1, 2, 2, 2), (0, 0, 1, 2, 3))
for i, s in enumerate(hmm_bayes.samples):
assert_allclose(s.observation_probabilities[pobs_zeros], 0, err_msg=f"{i}")
for strajs in hmm_bayes.sampled_trajs:
assert strajs[0][0] == 2
assert strajs[0][6] == 2
Expand All @@ -342,7 +343,7 @@ def test_initialized_bhmm(self):
import pyemma.datasets as d
import pyemma.msm

obs = d.load_2well_discrete().dtraj_T100K_dt10
obs = d.load_2well_discrete().dtraj

init_hmm = pyemma.msm.estimate_hidden_markov_model(obs, 2, 10)
bay_hmm = pyemma.msm.estimators.BayesianHMSM(nstates=init_hmm.nstates, lag=init_hmm.lag,
Expand Down
2 changes: 1 addition & 1 deletion pyemma/msm/tests/test_bayesian_msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUpClass(cls):
# load observations
import pyemma.datasets
data = pyemma.datasets.load_2well_discrete()
obs_micro = data.dtraj_T100K_dt10
obs_micro = data.dtraj

# stationary distribution
pi_micro = data.msm.stationary_distribution
Expand Down
8 changes: 4 additions & 4 deletions pyemma/msm/tests/test_cktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def tearDown(self):
assert hasattr(restored, 'has_errors')

def test_ck_msm(self):
MLMSM = msm.estimate_markov_model([self.double_well_data.dtraj_T100K_dt10_n6good], 40)
MLMSM = msm.estimate_markov_model([self.double_well_data.dtraj_n6good], 40)
self.ck = MLMSM.cktest(2, mlags=[0, 1, 10], n_jobs=1)
estref = np.array([[[1., 0.],
[0., 1.]],
Expand All @@ -178,7 +178,7 @@ def test_ck_msm(self):
assert self.ck.predictions_conf[1] is None

def test_its_bmsm(self):
BMSM = msm.bayesian_markov_model([self.double_well_data.dtraj_T100K_dt10_n6good], 40, reversible=True)
BMSM = msm.bayesian_markov_model([self.double_well_data.dtraj_n6good], 40, reversible=True)
# also ensure that reversible bit does not flip during cktest
assert BMSM.reversible
self.ck = BMSM.cktest(2, mlags=[0, 1, 10], n_jobs=1)
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_its_bmsm(self):
assert np.allclose(self.ck.predictions[1], predRref, rtol=0.1, atol=10.0)

def test_its_hmsm(self):
MLHMM = msm.estimate_hidden_markov_model([self.double_well_data.dtraj_T100K_dt10_n6good], 2, 10)
MLHMM = msm.estimate_hidden_markov_model([self.double_well_data.dtraj_n6good], 2, 10)
self.ck = MLHMM.cktest(mlags=[0, 1, 10], n_jobs=1)
estref = np.array([[[1., 0.],
[0., 1.]],
Expand All @@ -239,7 +239,7 @@ def test_its_hmsm(self):
assert self.ck.predictions_conf[1] is None

def test_its_bhmm(self):
BHMM = msm.bayesian_hidden_markov_model([self.double_well_data.dtraj_T100K_dt10_n6good], 2, 10)
BHMM = msm.bayesian_hidden_markov_model([self.double_well_data.dtraj_n6good], 2, 10)
self.ck = BHMM.cktest(mlags=[0, 1, 10], n_jobs=1)
estref = np.array([[[1., 0.],
[0., 1.]],
Expand Down
4 changes: 2 additions & 2 deletions pyemma/msm/tests/test_cmsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def setUpClass(cls):
import pyemma.datasets
cls.core_set = [34, 65]

cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj
nu = 1.*np.bincount(cls.dtraj)[cls.core_set]
cls.statdist = nu/nu.sum()

Expand Down Expand Up @@ -698,7 +698,7 @@ class TestCoreMSM(unittest.TestCase):
@classmethod
def setUpClass(cls):
from pyemma import datasets
cls.dtraj = datasets.load_2well_discrete().dtraj_T100K_dt10
cls.dtraj = datasets.load_2well_discrete().dtraj

def test_core(self):
core_set = [15, 16, 17, 45, 46, 47]
Expand Down
4 changes: 2 additions & 2 deletions pyemma/msm/tests/test_hmsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestMLHMM(unittest.TestCase):
def setUpClass(cls):
# load observations
import pyemma.datasets
obs = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
obs = pyemma.datasets.load_2well_discrete().dtraj.copy()
obs -= np.min(obs) # remove empty states

# hidden states
Expand Down Expand Up @@ -465,7 +465,7 @@ def test_separate_states(self):
np.array([2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2]),]
hmm = msm.estimate_hidden_markov_model(dtrajs, 3, lag=1, separate=[0])
# we expect zeros in all samples at the following indexes:
pobs_zeros = [[0, 1, 2, 2, 2], [0, 0, 1, 2, 3]]
pobs_zeros = ((0, 1, 2, 2, 2), (0, 0, 1, 2, 3))
assert np.allclose(hmm.observation_probabilities[pobs_zeros], 0)

if __name__=="__main__":
Expand Down
2 changes: 1 addition & 1 deletion pyemma/msm/tests/test_its.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def test_its_cmsm_defined_core(self):
core_set = [0, 5]
estimator = msm.estimators.MaximumLikelihoodMSM(core_set=core_set)
its = msm.ImpliedTimescales(estimator, lags=[1, 10, 100, 1000], n_jobs=1)
its.estimate([self.double_well_data.dtraj_T100K_dt10_n6good])
its.estimate([self.double_well_data.dtraj_n6good])
assert its.models[0].n_cores == 2
ref = np.array([[339.22244263],
[334.56862305],
Expand Down
2 changes: 1 addition & 1 deletion pyemma/msm/tests/test_msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class TestMSMDoubleWell(unittest.TestCase):
@classmethod
def setUpClass(cls):
import pyemma.datasets
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
cls.dtraj = pyemma.datasets.load_2well_discrete().dtraj
nu = 1.*np.bincount(cls.dtraj)
cls.statdist = nu/nu.sum()

Expand Down
2 changes: 1 addition & 1 deletion pyemma/msm/tests/test_msm_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestMSMSerialization(unittest.TestCase):
@classmethod
def setUpClass(cls):
data = datasets.load_2well_discrete()
cls.obs_micro = data.dtraj_T100K_dt10
cls.obs_micro = data.dtraj

# coarse-grain microstates to two metastable states
cg = np.zeros(100, dtype=int)
Expand Down
4 changes: 2 additions & 2 deletions pyemma/msm/tests/test_oom_msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,11 +1291,11 @@ def _timescales(self, msm):
ts_ref = self.rmsmrev.timescales()
assert (np.all(np.isreal(ts)))
# HERE:
np.testing.assert_almost_equal(ts, self.tau*ts_ref, decimal=2)
np.testing.assert_almost_equal(ts, self.tau*ts_ref, decimal=1)
else:
ts_ref = self.rmsm.timescales()
# HERE:
np.testing.assert_almost_equal(ts, self.tau*ts_ref, decimal=2)
np.testing.assert_almost_equal(ts, self.tau*ts_ref, decimal=1)

def test_timescales(self):
self._timescales(self.msmrev)
Expand Down
4 changes: 2 additions & 2 deletions pyemma/util/tests/test_discrete_trajectories.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_count_3(self):

def test_count_big(self):
import pyemma.datasets
dtraj = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
dtraj = pyemma.datasets.load_2well_discrete().dtraj
dt.number_of_states(dtraj)
dt.count_states(dtraj)

Expand Down Expand Up @@ -159,7 +159,7 @@ def test_twotraj(self):

def test_big(self):
import pyemma.datasets
dtraj = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10
dtraj = pyemma.datasets.load_2well_discrete().dtraj
# just run these to see if there's any exception
dt.index_states(dtraj)

Expand Down

0 comments on commit 5315b86

Please sign in to comment.