
# This file is part of MSMTools.
#
# Copyright (c) 2015, 2014 Computational Molecular Biology Group, Freie Universitaet Berlin (GER)
#
# MSMTools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

r"""Unit tests for the mean first passage time module

.. moduleauthor:: B.Trendelkamp-Schroer <benjamin DOT trendelkamp-schroer AT fu-berlin DOT de>
.. moduleauthor:: C.Wehmeyer <christoph DOT wehmeyer AT fu-berlin DOT de>

"""
from __future__ import absolute_import
from __future__ import division
import unittest

import numpy as np
from msmtools.util.numeric import assert_allclose
import scipy.sparse

from .mean_first_passage_time import mfpt, mfpt_between_sets


class TestMfpt(unittest.TestCase):
    def setUp(self):
        p1 = 0.7
        p2 = 0.5
        q2 = 0.4
        q3 = 0.6
        """3x3 birth and death transition matrix"""
        P = np.array([[1.0 - p1, p1, 0.0],
                      [q2, 1.0 - q2 - p2, p2],
                      [0.0, q3, 1.0 - q3]])
        self.P = scipy.sparse.coo_matrix(P)
        """Vector of mean first passage times to target state t=0"""
        self.m0 = np.array([0.0, (p2 + q3) / (q2 * q3), (p2 + q2 + q3) / (q2 * q3)])
        """Vector of mean first passage times to target state t=1"""
        self.m1 = np.array([1.0 / p1, 0.0, 1.0 / q3])
        """Vector of mean first passage times to target state t=2"""
        self.m2 = np.array([(p2 + p1 + q2) / (p1 * p2), (p1 + q2) / (p1 * p2), 0.0])
        """Vector of mean first passage times to target states t=(0,1)"""
        self.m01 = np.array([0.0, 0.0, 1.0 / q3])
        """Vector of mean first passage times to target states t=(1,2)"""
        self.m12 = np.array([1.0 / p1, 0.0, 0.0])
        """Vector of stationary weights"""
        self.mu = np.array([1.0, p1 / q2, p1 * p2 / q2 / q3])
        """Mean first passage times from (0) to (1,2)"""
        self.o0t12 = 1.0 / p1
        """Mean first passage times from (2) to (0,1)"""
        self.o2t01 = 1.0 / q3
        """Mean first passage times from (0,1) to (2)"""
        self.o01t2 = (self.m2[0] * self.mu[0] + self.m2[1] * self.mu[1]) / (self.mu[0] + self.mu[1])
        """Mean first passage times from (1,2) to (0)"""
        self.o12t0 = (self.m0[1] * self.mu[1] + self.m0[2] * self.mu[2]) / (self.mu[1] + self.mu[2])

    def tearDown(self):
        pass

    def test_mfpt(self):
        x = mfpt(self.P, 0)
        assert_allclose(x, self.m0)

        x = mfpt(self.P, 1)
        assert_allclose(x, self.m1)

        x = mfpt(self.P, 2)
        assert_allclose(x, self.m2)

        x = mfpt(self.P, [0, 1])
        assert_allclose(x, self.m01)

        x = mfpt(self.P, [1, 2])
        assert_allclose(x, self.m12)

    def test_mfpt_between_sets(self):
        x = mfpt_between_sets(self.P, [1, 2], 0)
        assert_allclose(x, self.o0t12)

        x = mfpt_between_sets(self.P, [0, 1], 2)
        assert_allclose(x, self.o2t01)

        x = mfpt_between_sets(self.P, 2, [0, 1])
        assert_allclose(x, self.o01t2)

        x = mfpt_between_sets(self.P, 0, [1, 2])
        assert_allclose(x, self.o12t0)


if __name__ == "__main__":
    unittest.main()