예제 #1
0
    def test_fitMk_invalidQMatrix_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = np.ones([3, 3])

        try:
            mk.fit_Mk(tree, chars, Q)
            self.fail("Assertion error not raised")
        except AssertionError as e:
            self.assertEqual("rows of q must sum to zero", str(e))
예제 #2
0
    def test_fitMk_invalidpi_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        try:
            mk.fit_Mk(tree, chars, pi="invalid pi")
            self.fail("Assertion error not raised")
        except AssertionError as e:
            self.assertEqual(
                "Pi must be one of: 'Equal', 'Fitzjohn', 'Equilibrium'",
                str(e))
예제 #3
0
    def test_fitMk_invalidQstring_raisesValueError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = "This is not a valid Q"

        try:
            mk.fit_Mk(tree, chars, Q)
            self.fail("Value error not raised")
        except ValueError as e:
            self.assertEqual("Q str must be one of: 'Equal', 'Sym', 'ARD'",
                             str(e))
예제 #4
0
    def test_SymRatesEquilibriumPiQ3_matchesPhytools(self):
        """
        The same as a flat pi
        """
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        expectedParam = np.array([[-0.220576, 0.129755, 0.090821],
                                  [0.129755, -0.129755, 0.000000],
                                  [0.090821, 0.000000, -0.090821]])
        expectedLogLikelihood = -34.398614

        calculated = mk.fit_Mk(tree, chars, Q="Sym", pi="Equilibrium")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:  # Need high tolerance for this test
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-3)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #5
0
    def test_EqualRatesEquilibriumPiQ3_matchesPhytools(self):
        """
        The same as a flat pi
        """
        tree = self.randTree100
        chars = self.simChars100states3

        expectedParam = np.array([[-0.556216, 0.278108, 0.278108],
                                  [0.278108, -0.556216, 0.278108],
                                  [0.278108, 0.278108, -0.556216]])
        expectedLogLikelihood = -41.508675

        calculated = mk.fit_Mk(tree, chars, Q="Equal", pi="Equilibrium")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:  # Need high tolerance for this test
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-3)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #6
0
    def test_SymRatesEquilibriumPiQ2_matchesPhytools(self):
        """
        The same as a flat pi
        """
        tree = self.randTree100
        chars = self.simChars100states2

        expectedParam = np.array([[-0.4549581, 0.4549581],
                                  [0.4549581, -0.4549581]])
        expectedLogLikelihood = -27.26863

        calculated = mk.fit_Mk(tree, chars, Q="Sym", pi="Equilibrium")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:  # Need high tolerance for this test
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-3)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #7
0
    def test_ARDEqualPiQ3traits_matchesPhytools(self):

        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        expectedParam = np.array([[-0.31973305, 0.136550, 0.183184],
                                  [0.997779, -0.997779, 0.0000],
                                  [3.315930, 0.0000, -3.315930]])

        expectedLogLikelihood = -32.697278

        calculated = mk.fit_Mk(tree, chars, Q="ARD", pi="Equal")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:  # Need high tolerance for this test
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-3)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #8
0
    def test_SymRatesEqualPiQ2traits_matchesPhytools(self):
        """
        Note that this is the same as an equal-rates 2-trait Q matrix
        """

        tree = self.randTree100
        chars = self.simChars100states2  # Generated with a 2x2 Q matrix where alpha=beta=0.5

        expectedParam = np.array([[-0.4549581, 0.4549581],
                                  [0.4549581, -0.4549581]])
        expectedLogLikelihood = -27.26863

        calculated = mk.fit_Mk(tree, chars, Q="Sym", pi="Equal")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-5)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #9
0
    def test_fitMk_fixedQ_returnsQandLogik(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = np.array([[-.2, .1, .1], [.1, -.2, .1], [.1, .1, -.2]],
                     dtype=np.double)

        calculated = mk.fit_Mk(tree, chars, Q)

        calcq = calculated["Q"]
        calclik = calculated["Log-likelihood"]

        self.assertTrue(
            np.array_equal(Q, calcq) & np.isclose(calclik, -34.7215))
예제 #10
0
    def test_EqualRatesEqualPiQ3traits_matchesPhytools(self):

        tree = self.randTree100
        chars = self.simChars100states3

        expectedParam = np.array([[-0.556216, 0.278108, 0.278108],
                                  [0.278108, -0.556216, 0.278108],
                                  [0.278108, 0.278108, -0.556216]])
        expectedLogLikelihood = -41.508675

        calculated = mk.fit_Mk(tree, chars, Q="Equal", pi="Equal")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-5)
        except:
            self.fail("expectedParam != calculatedParam")
        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #11
0
    def test_ARDEqualPiQ2traits_matchesPhytools(self):

        tree = self.randTree100
        chars = self.simChars100states2

        expectedParam = np.array([[-0.261398, 0.261398], [0.978787,
                                                          -0.978787]])

        expectedLogLikelihood = -25.813332

        calculated = mk.fit_Mk(tree, chars, Q="ARD", pi="Equal")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:  # Need high tolerance for this test
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-3)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #12
0
    def test_SymRatesEqualPiQ3traits_matchesPhytools(self):

        tree = self.randTree100
        chars = self.simChars100states3

        expectedParam = np.array([[-.631001, 0.462874, 0.168128],
                                  [0.462874, -0.462874, 0.000000],
                                  [0.168128, 0.000000, -0.168128]])
        expectedLogLikelihood = -39.141458

        calculated = mk.fit_Mk(tree, chars, Q="Sym", pi="Equal")

        calculatedParam = calculated["Q"]
        calculatedLogLikelihood = calculated["Log-likelihood"]

        try:
            np.testing.assert_allclose(expectedParam,
                                       calculatedParam,
                                       atol=1e-5)
        except AssertionError:
            self.fail("expectedParam != calculatedParam")

        self.assertTrue(
            np.isclose(expectedLogLikelihood, calculatedLogLikelihood))
예제 #13
0
import math
import cProfile

tree = ivy.tree.read("support/randtree100tipsscale2.newick")
chars = [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1]

Q = np.array([[-2.09613850e-01, 1.204029e-01, 8.921095e-02],
              [5.654382e-01, -5.65438217e-01, 1.713339e-08],
              [2.415020e-06, 5.958744e-07, -3.01089440e-06]])


out = mk.fit_Mk(tree, chars, Q="ARD", pi="Equal")

cProfile.run('mk.fit_Mk(tree, chars, Q="ARD", pi="Equal")')

Q = np.array([[-2.09613850e-01, 1.204029e-01, 8.921095e-02],
              [5.654382e-01, -5.65438217e-01, 1.713339e-08],
              [2.415020e-06, 5.958744e-07, -3.01089440e-06]])
calculatedLikelihood = mk.mk(tree, chars, Q,
                                         pi = "Fitzjohn")




tree = ivy.tree.read("support/randtree100tips.newick")

chars = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
예제 #14
0
    def test_fitMk_ARDQequilibriumpi_matchesPhytools(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        calculated = mk.fit_Mk(tree, chars, Q="ARD", pi="Equilibrium")