def test_fitMk_invalidpi_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        try:
            discrete.fitMk(tree, chars, pi = "invalid pi")
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("Pi must be one of: 'Equal', 'Fitzjohn', 'Equilibrium'", e.message)
Пример #2
0
    def test_fitMk_invalidQMatrix_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

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

        try:
            discrete.fitMk(tree, chars, Q)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("rows of q must sum to zero", e.message)
    def test_fitMk_invalidQMatrix_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

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

        try:
            discrete.fitMk(tree, chars, Q)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("rows of q must sum to zero", e.message)
Пример #4
0
    def test_fitMk_invalidpi_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        try:
            discrete.fitMk(tree, chars, pi="invalid pi")
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals(
                "Pi must be one of: 'Equal', 'Fitzjohn', 'Equilibrium'",
                e.message)
    def test_fitMk_invalidQstring_raisesValueError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = "This is not a valid Q"

        try:
            discrete.fitMk(tree, chars, Q)
            self.fail("Value error not raised")
        except ValueError, e:
            self.assertEquals("Q str must be one of: 'Equal', 'Sym', 'ARD'",
                              e.message)
Пример #6
0
    def test_fitMk_invalidQstring_raisesValueError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = "This is not a valid Q"

        try:
            discrete.fitMk(tree, chars, Q)
            self.fail("Value error not raised")
        except ValueError, e:
            self.assertEquals("Q str must be one of: 'Equal', 'Sym', 'ARD'",
                              e.message)
Пример #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 = discrete.fitMk(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_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 = discrete.fitMk(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))
Пример #9
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 = discrete.fitMk(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))
Пример #10
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 = discrete.fitMk(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))
    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 = discrete.fitMk(tree, chars, Q)

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

        self.assertTrue(np.array_equal(Q,calcq) & np.isclose(calclik, -34.7215))
Пример #12
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 = discrete.fitMk(tree, chars, Q)

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

        self.assertTrue(
            np.array_equal(Q, calcq) & np.isclose(calclik, -34.7215))
    def test_EqualRatesEqualPiQ2traits_matchesPhytools(self):

        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 = discrete.fitMk(tree, chars, Q="Equal", pi="Equal")

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

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

        self.assertTrue(np.isclose(expectedLogLikelihood,
                                   calculatedLogLikelihood))
    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 = discrete.fitMk(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))
    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 = discrete.fitMk(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))
    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 = discrete.fitMk(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))
    def test_EqualRatesEquilibriumPiQ2_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 = discrete.fitMk(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))
    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 = discrete.fitMk(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))
    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 = discrete.fitMk(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))
Пример #20
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 = discrete.fitMk(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))
Пример #21
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 = discrete.fitMk(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))
Пример #22
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 = discrete.fitMk(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))
Пример #23
0
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]])

mk(tree, chars, Q, pi="Equal", returnPi=True)


q, lik = discrete.fitMk(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 = discrete.mk(tree, chars, Q,
                                         pi = "Fitzjohn")
calculatedLogLikelihood = math.log(calculatedLikelihood)

calculatedLogLikelihood



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,
Пример #24
0
    def test_fitMk_ARDQequilibriumpi_matchesPhytools(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        calculated = discrete.fitMk(tree, chars, Q="ARD", pi="Equilibrium")
    def test_fitMk_ARDQequilibriumpi_matchesPhytools(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        calculated = discrete.fitMk(tree, chars, Q="ARD", pi="Equilibrium")