Exemplo n.º 1
0
    def setUpClass(cls):
        # load observations
        import pyerna.datasets
        obs = pyerna.datasets.load_2well_discrete().dtraj_T100K_dt10
        obs -= np.min(obs)  # remove empty states

        # hidden states
        nstates = 2

        # run with lag 1 and 10
        cls.msm_lag1 = msm.estimate_markov_model([obs], 1, reversible=True, connectivity='largest')
        cls.hmsm_lag1 = msm.estimate_hidden_markov_model([obs], nstates, 1, reversible=True, observe_nonempty=True)
        cls.msm_lag10 = msm.estimate_markov_model([obs], 10, reversible=True, connectivity='largest')
        cls.hmsm_lag10 = msm.estimate_hidden_markov_model([obs], nstates, 10, reversible=True, observe_nonempty=True)
Exemplo n.º 2
0
 def test_separate_states(self):
     dtrajs = [np.array([0, 1, 1, 1, 1, 1, 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1]),
               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]]
     assert np.allclose(hmm.observation_probabilities[pobs_zeros], 0)
Exemplo n.º 3
0
    def test_failfast_false(self):
        """ test, that no exception is raised during estimation"""
        from pyerna._base.estimator import _estimate_param_scan_worker
        failfast = False

        @wraps(_estimate_param_scan_worker)
        def worker_wrapper(*args):
            args = list(args)
            args[5] = failfast
            return _estimate_param_scan_worker(*args)

        with mock.patch('pyerna._base.estimator._estimate_param_scan_worker',
                        worker_wrapper):
            hmm = msm.estimate_hidden_markov_model(
                [0, 0, 0, 1, 1, 1, 0, 0],
                2,
                1,
            )
            hmm.cktest()
Exemplo n.º 4
0
    def test_failfast_true(self):
        """ test that exception is thrown for failfast=True"""
        from pyerna._base.estimator import _estimate_param_scan_worker
        failfast = True

        @wraps(_estimate_param_scan_worker)
        def worker_wrapper(*args):
            args = list(args)
            args[5] = failfast
            return _estimate_param_scan_worker(*args)

        with self.assertRaises(NotImplementedError):
            with mock.patch(
                    'pyerna._base.estimator._estimate_param_scan_worker',
                    worker_wrapper):
                hmm = msm.estimate_hidden_markov_model(
                    [0, 0, 0, 1, 1, 1, 0, 0],
                    2,
                    1,
                )
                hmm.cktest()
Exemplo n.º 5
0
 def test_its_hmsm(self):
     MLHMM = msm.estimate_hidden_markov_model([self.double_well_data.dtraj_T100K_dt10_n6good], 2, 10)
     self.ck = MLHMM.cktest(mlags=[0,1,10])
     estref = np.array([[[ 1.,          0.        ],
                         [ 0.,          1.        ]],
                        [[ 0.98515058,  0.01484942],
                         [ 0.01442843,  0.98557157]],
                        [[ 0.88172685,  0.11827315],
                         [ 0.11878823,  0.88121177]]])
     predref = np.array([[[ 1.,          0.        ],
                          [ 0.,          1.        ]],
                         [[ 0.98515058,  0.01484942],
                          [ 0.01442843,  0.98557157]],
                         [[ 0.86961812,  0.13038188],
                          [ 0.12668553,  0.87331447]]])
     # rough agreement with MLE
     assert np.allclose(self.ck.estimates, estref, rtol=0.1, atol=10.0)
     assert self.ck.estimates_conf[0] is None
     assert self.ck.estimates_conf[1] is None
     assert np.allclose(self.ck.predictions, predref, rtol=0.1, atol=10.0)
     assert self.ck.predictions_conf[0] is None
     assert self.ck.predictions_conf[1] is None
Exemplo n.º 6
0
    def test_submodel_simple(self):
        # sanity check for submodel;
        # call should not alter self
        from copy import deepcopy
        # dtrj = np.random.randint(0, 2, size=100)
        # dtrj[np.random.randint(0, dtrj.shape[0], 3)] = 2
        # hard-coded due to stochastic failures
        dtrj = [1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0,
                0, 2, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0,
                1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 1, 2, 0, 1, 1, 1,
                0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]

        h = msm.estimate_hidden_markov_model(dtrj, 3, 2)
        h_original = deepcopy(h)

        hs = h.submodel_largest(mincount_connectivity=5)

        self.assertTrue(h == h_original)

        self.assertEqual(hs.timescales().shape[0], 1)
        self.assertEqual(hs.pi.shape[0], 2)
        self.assertEqual(hs.transition_matrix.shape, (2, 2))