def setUp(self): np.random.seed(1) self._X1 = np.random.randn(10, 5) self._X2 = np.random.randn(10, 8) self._cov1 = SQExpCov(self._X1) self._cov2 = SQExpCov(self._X2) self._cov = SumCov(self._cov1, self._cov2)
class TestSumCov(unittest.TestCase): def setUp(self): np.random.seed(1) self._X1 = np.random.randn(10, 5) self._X2 = np.random.randn(10, 8) self._cov1 = SQExpCov(self._X1) self._cov2 = SQExpCov(self._X2) self._cov = SumCov(self._cov1, self._cov2) def test_sum_combination(self): K1 = self._cov1.K() + self._cov2.K() K2 = self._cov.K() np.testing.assert_almost_equal(K1, K2) def test_Kgrad(self): cov = self._cov def func(x, i): cov.setParams(x) return cov.K() def grad(x, i): cov.setParams(x) return cov.K_grad_i(i) x0 = cov.getParams() err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) def test_use_to_predict_exception(self): with self.assertRaises(NotImplementedError): self._cov.use_to_predict = 1.
def add_env_to_null(gp_init, cell_types, phenotype, X, env_type_ix, affected_type_ix): cov = gp_init.covar Kinship = build_cell_type_kinship(cell_types) cell_type_list = np.unique(cell_types) rm_diag = True env_covs = [[None for i in range(len(cell_type_list))] for j in range(len(cell_type_list))] for t1 in cell_type_list: if affected_type_ix is not None and affected_type_ix != t1: continue for t2 in cell_type_list: # select only the environmental cell type if not all if env_type_ix is not None and env_type_ix != t2: continue interaction_matrix = build_interaction_matrix(t1, t2, cell_types) env_covs[t1][t2] = ZKZCov(X, Kinship, rm_diag, interaction_matrix) env_covs[t1][t2].setPenalty(mu=200, sigma=50) cov = SumCov(cov, env_covs[t1][t2]) gp_init.covar = cov gp_init.optimize() return gp_init
def rm_cov(self, cov_term): for term in cov_term: if term in self.covar_terms: del self.covar_terms[term] else: print('cov term ', term, ' not found for deletion ') self.covar = SumCov(*self.covar_terms.values()) self.build_gp()
def add_cov(self, cov_term): # import pdb; pdb.set_trace() for term in cov_term: if term in self.covar_terms: continue elif term == 'intrinsic': self.covar_terms['intrinsic'] = self.intrinsic_cov elif term == 'interactions': self.covar_terms['interactions'] = self.interactions_cov elif term == 'environmental': self.covar_terms['environmental'] = self.environmental_cov else: raise Exception('covariance term not recognised ') self.covar = SumCov(*self.covar_terms.values()) self.build_gp()
def reset_from_previous(self): for cov_key in self.covar_terms.keys(): if cov_key in self.init_covs.keys(): self.covar_terms[cov_key].setParams(self.init_covs[cov_key]) else: self.covar_terms[cov_key].scale = 1. k = covar_rescaling_factor_efficient( self.covar_terms[cov_key].K()) self.covar_terms[cov_key].scale = 0.5 * k if self.use_scale_down: for term in self.scale_down: self.covar_terms[term].scale *= 1e-10 self.covar = SumCov(*self.covar_terms.values()) self.build_gp()
def build_cov(self, cov_terms): # import pdb; pdb.set_trace() self.intrinsic_cov = self.build_intrinsic() self.interactions_cov = self.build_interactions() self.environmental_cov = self.build_environmental() self.noise_cov = self.build_noise() self.covar_terms = dict() if 'intrinsic' in cov_terms: self.covar_terms['intrinsic'] = self.intrinsic_cov if 'interactions' in cov_terms: self.covar_terms['interactions'] = self.interactions_cov if 'environmental' in cov_terms: self.covar_terms['environmental'] = self.environmental_cov self.covar_terms['noise'] = self.noise_cov self.covar = SumCov(*self.covar_terms.values()) self.reset_params()
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
N = 400 X = sp.linspace(0,2,N)[:,sp.newaxis] v_noise = 0.01 Y = sp.sin(X) + sp.sqrt(v_noise) * sp.randn(N, 1) # for out-of-sample preditions Xstar = sp.linspace(0,2,1000)[:,sp.newaxis] # define mean term W = 1. * (sp.rand(N, 2) < 0.2) mean = lin_mean(Y, W) # define covariance matrices sqexp = SQExpCov(X, Xstar = Xstar) noise = FixedCov(sp.eye(N)) covar = SumCov(sqexp, noise) # define gp gp = GP(covar=covar,mean=mean) # initialize params sqexp.scale = 1e-4 sqexp.length = 1 noise.scale = Y.var() # optimize gp.optimize(calc_ste=True) # predict out-of-sample Ystar = gp.predict() # print optimized values and standard errors print('weights of fixed effects') print(mean.b[0, 0], '+/-', mean.b_ste[0, 0])