Пример #1
0
def build_model(Kinship, phenotype, N_cells, X, cell_types, test_set=None, intrinsic =True, environment = True,
                environmental_cell_types=None, affected_cell_type=None, by_effective_type=True):

    if test_set is not None and test_set.dtype == bool:
        X_training = X[~test_set, :]
        X_test = X[test_set, :]
        mean_training = phenotype[~test_set]
        N_cells_training = sum(~test_set)
        N_cells_test = N_cells - N_cells_training
        cell_types_training = cell_types[~test_set]
        cell_types_test = cell_types[test_set]

    else:
        X_training = X
        X_test = None
        mean_training = phenotype
        N_cells_training = N_cells
        N_cells_test = 0
        cell_types_training = cell_types

    rm_diag = True
    cov = None

    # list cell types
    cell_type_list = np.unique(cell_types)

    # local_noise
    local_noise_cov = SQExpCov(X_training, Xstar=X_test)
    local_noise_cov.setPenalty(mu=50., sigma=50.)

    # noise
    noise_covs = [None for i in range(len(cell_type_list))]
    for t in cell_type_list:
        cells_selection = (cell_types_training == t) * np.eye(N_cells_training)
        if N_cells_test == 0:
            Kcross = None
        # TODO: adapt to multiple cell types
        else:
            # Kcross = np.concatenate((np.zeros([N_cells_test, N_cells_training]), np.eye(N_cells_test)), axis=1)
            Kcross = np.zeros([N_cells_test, N_cells_training])
        noise_covs[t] = FixedCov(cells_selection, Kcross)
        if cov is None:
            cov = SumCov(local_noise_cov, noise_covs[t])
        else:
            cov = SumCov(cov, noise_covs[t])

    # environment effect: for each pair of cell types
    # t1 is the receiving type, t2 is the effector
    if environment:
        if by_effective_type:
            # env_covs = np.array([len(cell_type_list), len(cell_type_list)])
            env_covs = [[None for i in range(len(cell_type_list))] for j in range(len(cell_type_list))]
        else:
            env_covs = [None for i in range(len(cell_type_list))]
        # env_covs = [tmp] * len(cell_type_list)
        for t1 in cell_type_list:
            if affected_cell_type is not None and affected_cell_type != t1:
                continue
            if by_effective_type:
                for t2 in cell_type_list:
                    # select only the environmental cell type if not all
                    if environmental_cell_types is not None and environmental_cell_types != t2:
                        continue
                    interaction_matrix = build_interaction_matrix(t1, t2, cell_types)
                    tmp = ZKZCov(X, Kinship, rm_diag, interaction_matrix, test_set)
                    env_covs[t1][t2] = tmp
                    env_covs[t1][t2].setPenalty(mu=200., sigma=50.)
                    cov = SumCov(cov, env_covs[t1][t2])
            else:
                interaction_matrix = build_interaction_matrix(t1, 'all', cell_types)
                tmp = ZKZCov(X, Kinship, rm_diag, interaction_matrix, test_set)
                env_covs[t1] = tmp
                env_covs[t1].setPenalty(mu=200., sigma=50.)
                cov = SumCov(cov, env_covs[t1])
    else:
        env_covs = None

    if intrinsic:
        K = build_cell_type_kinship(cell_types_training)
        if N_cells_test != 0:
            Kcross = build_cell_type_kinship(cell_types_test, cell_types_training)
        intrinsic_cov = FixedCov(K, Kcross)
        cov = SumCov(cov, intrinsic_cov)
    else:
        intrinsic_cov = None

    # mean term
    mean = MeanBase(mean_training)

    # define GP
    gp = limix.core.gp.GP(covar=cov, mean=mean)

    print('GP created ')

    return gp, noise_covs, local_noise_cov, env_covs, intrinsic_cov
Пример #2
0
class TestSQExp(unittest.TestCase):
    def setUp(self):
        np.random.seed(1)
        self._X = np.random.randn(10, 5)
        self._cov = SQExpCov(self._X)

    def test_setX_retE(self):
        X1 = self._X
        np.random.seed(2)
        X2 = np.random.randn(10, 5)

        E1 = sp.spatial.distance.pdist(X1, 'euclidean')**2
        E1 = sp.spatial.distance.squareform(E1)

        E2 = sp.spatial.distance.pdist(X2, 'euclidean')**2
        E2 = sp.spatial.distance.squareform(E2)

        np.testing.assert_almost_equal(E1, self._cov.E())

        self._cov.X = X2
        np.testing.assert_almost_equal(E2, self._cov.E())

    def test_param_activation(self):
        self._cov.act_scale = False
        self._cov.act_length = False
        self.assertEqual(len(self._cov.getParams()), 0)

        self._cov.act_scale = False
        self._cov.act_length = True
        self.assertEqual(len(self._cov.getParams()), 1)

        self._cov.act_scale = True
        self._cov.act_length = False
        self.assertEqual(len(self._cov.getParams()), 1)

        self._cov.act_scale = True
        self._cov.act_length = True
        self.assertEqual(len(self._cov.getParams()), 2)

        self._cov.act_scale = False
        self._cov.act_length = False
        self._cov.setParams(np.array([]))
        with self.assertRaises(ValueError):
            self._cov.setParams(np.array([0]))

        with self.assertRaises(ValueError):
            self._cov.K_grad_i(0)

        with self.assertRaises(ValueError):
            self._cov.K_grad_i(1)

    def test_Kgrad(self):
        def func(x, i):
            self._cov.scale = x[i]
            return self._cov.K()

        def grad(x, i):
            self._cov.scale = x[i]
            return self._cov.K_grad_i(0)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0.)

        def func(x, i):
            self._cov.length = x[i]
            return self._cov.K()

        def grad(x, i):
            self._cov.scale = x[i]
            return self._cov.K_grad_i(1)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)

    def test_penalty(self):
        self._cov.setPenalty(10., 2.)

        def func(x, i):
            self._cov.scale = x[i]
            return self._cov.K()

        def grad(x, i):
            self._cov.scale = x[i]
            return self._cov.K_grad_i(0)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0.)

        def func(x, i):
            self._cov.length = x[i]
            return self._cov.K()

        def grad(x, i):
            self._cov.scale = x[i]
            return self._cov.K_grad_i(1)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)

    def test_Kgrad_activation(self):
        self._cov.act_length = False

        def func(x, i):
            self._cov.scale = x[i]
            return self._cov.K()

        def grad(x, i):
            self._cov.scale = x[i]
            return self._cov.K_grad_i(0)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0.)

        self._cov.act_scale = False
        self._cov.act_length = True

        def func(x, i):
            self._cov.length = x[i]
            return self._cov.K()

        def grad(x, i):
            self._cov.length = x[i]
            return self._cov.K_grad_i(0)

        x0 = np.array([self._cov.length])
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0.)

    def test_Khess(self):
        def func(x, i):
            self._cov.scale = x[i]
            return self._cov.K_grad_i(0)

        def grad(x, i):
            self._cov.scale = x[i]
            return self._cov.K_hess_i_j(0, 0)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=5)

        def func(x, i):
            self._cov.length = x[i]
            return self._cov.K_grad_i(0)

        def grad(x, i):
            self._cov.length = x[i]
            return self._cov.K_hess_i_j(0, 1)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=5)

        def func(x, i):
            self._cov.length = x[i]
            return self._cov.K_grad_i(1)

        def grad(x, i):
            self._cov.length = x[i]
            return self._cov.K_hess_i_j(1, 1)

        x0 = np.array([self._cov.scale])
        err = mcheck_grad(func, grad, x0)
        np.testing.assert_almost_equal(err, 0., decimal=5)

    def test_input(self):
        with self.assertRaises(ValueError):
            SQExpCov(np.array([[np.inf]]))

        with self.assertRaises(ValueError):
            SQExpCov(np.array([[np.nan]]))

        with self.assertRaises(NotArrayConvertibleError):
            SQExpCov("Ola meu querido.")