Ejemplo n.º 1
0
    def test_random_model(self):
        num_segments = 5
        num_clones = 3
        num_alleles = 2

        model = self.create_random_model(num_segments, num_clones, num_alleles)
        x = np.random.randint(100000, size=(num_segments, 3))

        for m in xrange(num_clones):
            for ell in xrange(num_alleles):
                elbo_1 = model.calculate_elbo(x)
                model.update(x)
                elbo_2 = model.calculate_elbo(x)
                print elbo_2 - elbo_1
                self.assertTrue(elbo_2 - elbo_1 > -1e-10)
Ejemplo n.º 2
0
    def test_update_swap(self):
        num_segments = 4

        for i in xrange(num_replicates):

            cn = np.array([[[1, 1], [0, 1]], [[1, 1], [2, 1]], [[1, 1], [0,
                                                                         1]],
                           [[1, 1], [0, 1]]])

            h = np.array([0.2, 0.3])
            l = np.array([10000., 30000., 20000., 10000.])
            phi = np.array([0.2, 0.2, 0.2, 0.2])
            mu = remixt.likelihood.expected_read_count(l, cn, h, phi)

            x = np.array([np.random.poisson(a) for a in mu])
            x.sort(axis=1)
            x = x.reshape(mu.shape)

            model = remixt.model3.RemixtModel(
                2,
                num_segments,
                1,
                cn_max,
                x,
                np.array([0, 0, 0, 1]),
                np.array([0, 0, -1, -1]),
                np.array([-1, +1, 0, 0]),
                l,
                l,
                1.0,
            )

            print h

            model.prior_variance = 1e5

            model.h = np.array([0.2, 0.3])

            model.init_p_cn()

            print model.get_cn()

            # model.posterior_marginals[0, :, :, :] = (
            #     np.array([[[0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99]],
            #               [[0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99]]]))

            elbo_prev = None
            for i in xrange(20):
                elbo = model.update()
                print 'elbo', elbo
                print 'h', model.h
                if elbo_prev is not None:
                    print 'diff:', elbo - elbo_prev
                    self.assertTrue(elbo - elbo_prev > -1e-5)
                elbo_prev = elbo

            print model.get_cn()

            print np.asarray(model.p_allele)

            for n in range(2):
                for v in range(2):
                    for w in range(2):
                        print n, v, w, model.p_allele[n, v, w]

            print np.asarray(model.p_breakpoint)

            brk_cn = np.argmax(model.p_breakpoint, axis=-1)
            print brk_cn

            self.assertTrue(np.all(brk_cn == np.array([0, 1, 0])))
Ejemplo n.º 3
0
    def test_update(self):
        num_segments = 4

        for i in range(num_replicates):

            cn = np.array([[[1, 1], [1, 1], [1, 1]], [[1, 1], [1, 0], [1, 1]],
                           [[1, 1], [1, 1], [1, 1]], [[1, 1], [0, 0], [0, 0]]])

            h = np.array([0.2, 0.3, 0.1])
            l = np.array([10000., 30000., 20000., 10000.])
            phi = np.array([0.2, 0.2, 0.2, 0.2])
            mu = remixt.likelihood.expected_read_count(l, cn, h, phi)

            x = np.array([np.random.poisson(a) for a in mu])
            x = x.reshape(mu.shape)

            model = remixt.model3.RemixtModel(
                3,
                num_segments,
                1,
                cn_max,
                x,
                np.array([0, 0, 0, 1]),
                np.array([0, 0, -1, -1]),
                np.array([+1, -1, 0, 0]),
                l,
                l,
                1.0,
            )

            print(h)

            model.prior_variance = 1e5

            model.h = np.array([0.2, 0.3, 0.1])

            print(np.asarray(model.cn_states))

            model.init_p_cn()

            print(model.posterior_marginals)
            print(model.get_cn())

            # model.posterior_marginals[0, :, :, :] = (
            #     np.array([[[0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99]],
            #               [[0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99],
            #                [0.01, 0.99]]]))

            elbo_prev = None
            for i in range(20):
                elbo = model.update()
                print('elbo', elbo)
                print('h', np.asarray(model.h))
                if elbo_prev is not None:
                    print('diff:', elbo - elbo_prev)
                    self.assertTrue(elbo - elbo_prev > -1e-5)
                elbo_prev = elbo

            print(model.get_cn())
            print(np.asarray(model.p_garbage))
            print(np.argmax(model.p_garbage, axis=-1))

            brk_cn = np.argmax(model.p_breakpoint, axis=-1)
            self.assertTrue(np.all(brk_cn == np.array([0, 1, 0])))