コード例 #1
0
ファイル: test_gp2kronSum.py プロジェクト: mattions/limix
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 200
        P = 2
        self.Y = sp.randn(N, P)
        # define fixed effects
        self.F = []
        self.A = []
        self.F.append(1. * (sp.rand(N, 2) < 0.5))
        self.A.append(sp.eye(P))
        # define row caoriance
        f = 10
        X = 1. * (sp.rand(N, f) < 0.2)
        self.R = covar_rescale(sp.dot(X, X.T))
        self.R += 1e-4 * sp.eye(N)
        # define col covariances
        self.Cg = FreeFormCov(P)
        self.Cn = FreeFormCov(P)
        self.Cg.setCovariance(0.5 * sp.cov(self.Y.T))
        self.Cn.setCovariance(0.5 * sp.cov(self.Y.T))
        # define gp
        self.gp = GP2KronSum(Y=self.Y,
                             F=self.F,
                             A=self.A,
                             Cg=self.Cg,
                             Cn=self.Cn,
                             R=self.R)
コード例 #2
0
ファイル: mvSetFull.py プロジェクト: xypan1232/limix
def define_gp(Y, Xr, Sg, Ug, type):
    P = Y.shape[1]
    _A = sp.eye(P)
    if type in 'rank1': _Cr = limix.core.covar.LowRankCov(P, 1)
    elif type == 'block': _Cr = limix.core.covar.FixedCov(sp.ones((P, P)))
    elif type == 'full': _Cr = limix.core.covar.FreeFormCov(P)
    elif type == 'null': pass
    else: print('poppo')
    _Cn = limix.core.covar.FreeFormCov(P)
    _Cg = limix.core.covar.FreeFormCov(P)
    if type == 'null': _gp = GP2KronSum(Y=Y, Cg=_Cg, Cn=_Cn, S_R=Sg, U_R=Ug)
    else: _gp = GP3KronSumLR(Y=Y, G=Xr, Cr=_Cr, Cg=_Cg, Cn=_Cn, S_R=Sg, U_R=Ug)
    return _gp
コード例 #3
0
ファイル: test_gp_more.py プロジェクト: xypan1232/limix
    def setUp(self):
        np.random.seed(1)

        # define phenotype
        N = 10 
        P = 3
        Y = sp.randn(N,P)

        # pheno with missing data
        Ym = Y.copy()
        Im = sp.rand(N, P)<0.2
        Ym[Im] = sp.nan

        # define fixed effects
        F = []; A = []
        F.append(1.*(sp.rand(N,2)<0.5))
        A.append(sp.eye(P))
        mean = MeanKronSum(Y, F=F, A=A)
        mean_m = MeanKronSum(Ym, F=F, A=A)

        # define row caoriance
        f = 10
        X = 1.*(sp.rand(N, f)<0.2)
        R = covar_rescale(sp.dot(X,X.T))
        R+= 1e-4 * sp.eye(N)

        # define col covariances
        Cg = FreeFormCov(P)
        Cn = FreeFormCov(P)
        Cg.setRandomParams()
        Cn.setRandomParams()

        # define covariance matrices
        covar1 = KronCov(Cg, R)
        covar2 = KronCov(Cn, sp.eye(N))
        covar  = SumCov(covar1,covar2)

        # define covariance matrice with missing data
        Iok = (~Im).reshape(N * P, order='F')
        covar1_m = KronCov(copy.copy(Cg), R, Iok=Iok)
        covar2_m = KronCov(copy.copy(Cn), sp.eye(N), Iok=Iok)
        covar_m  = SumCov(covar1_m,covar2_m)

        # define gp
        self._gp = GP(covar=covar, mean=mean)
        self._gpm = GP(covar=covar_m, mean=mean_m)
        self._gp2ks = GP2KronSum(Y=Y, F=F, A=A, Cg=Cg, Cn=Cn, R=R) 
コード例 #4
0
def define_gp(Y, Xr, F, type, Rr):
    P = Y.shape[1]
    _A = sp.eye(P)
    if type in ['null', 'rank1']: _Cr = limix.core.covar.LowRankCov(P, 1)
    elif type == 'block': _Cr = limix.core.covar.FixedCov(sp.ones((P, P)))
    elif type == 'full': _Cr = limix.core.covar.FreeFormCov(P)
    else: print('poppo')
    _Cn = limix.core.covar.FreeFormCov(P)
    if type == 'null':
        _gp = GP2KronSumLR(Y=Y,
                           G=sp.ones((Y.shape[0], 1)),
                           F=F,
                           A=_A,
                           Cr=_Cr,
                           Cn=_Cn)
        _Cr.setParams(1e-9 * sp.ones(P))
        _gp.covar.act_Cr = False
    else:
        if Xr.shape[1] < Xr.shape[0]:
            _gp = GP2KronSumLR(Y=Y, G=Xr, F=F, A=_A, Cr=_Cr, Cn=_Cn)
        else:
            _gp = GP2KronSum(Y=Y, F=F, A=_A, R=Rr, Cg=_Cr, Cn=_Cn)
    return _gp
コード例 #5
0
    def fitNull(self,
                verbose=False,
                cache=False,
                out_dir='./cache',
                fname=None,
                rewrite=False,
                seed=None,
                n_times=10,
                factr=1e3,
                init_method=None):
        """
        Fit null model
        """
        if seed is not None: sp.random.seed(seed)

        read_from_file = False
        if cache:
            assert fname is not None, 'MultiTraitSetTest:: specify fname'
            if not os.path.exists(out_dir):
                os.makedirs(out_dir)
            out_file = os.path.join(out_dir, fname)
            read_from_file = os.path.exists(out_file) and not rewrite

        RV = {}
        if read_from_file:
            f = h5py.File(out_file, 'r')
            for key in f.keys():
                RV[key] = f[key][:]
            f.close()
            self.setNull(RV)
        else:
            start = TIME.time()
            if self.bgRE:
                self._gpNull = GP2KronSum(Y=self.Y,
                                          F=None,
                                          A=None,
                                          Cg=self.Cg,
                                          Cn=self.Cn,
                                          R=None,
                                          S_R=self.S_R,
                                          U_R=self.U_R)
            else:
                self._gpNull = GP2KronSumLR(self.Y,
                                            self.Cn,
                                            G=sp.ones((self.N, 1)),
                                            F=self.F,
                                            A=self.A)
                # freezes Cg to 0
                n_params = self._gpNull.covar.Cr.getNumberParams()
                self._gpNull.covar.Cr.setParams(1e-9 * sp.ones(n_params))
                self._gpNull.covar.act_Cr = False
            for i in range(n_times):
                params0 = self._initParams(init_method=init_method)
                self._gpNull.setParams(params0)
                conv, info = self._gpNull.optimize(verbose=verbose)
                if conv: break
            if not conv: warnings.warn("not converged")
            LMLgrad = (self._gpNull.LML_grad()['covar']**2).mean()
            LML = self._gpNull.LML()
            if self._gpNull.mean.n_terms == 1:
                RV['B'] = self._gpNull.mean.B[0]
            elif self._gpNull.mean.n_terms > 1:
                warning.warn('generalize to more than 1 fixed effect term')
            if self.bgRE:
                RV['params0_g'] = self.Cg.getParams()
            else:
                RV['params0_g'] = sp.zeros_like(self.Cn.getParams())
            RV['params0_n'] = self.Cn.getParams()
            if self.bgRE:
                RV['Cg'] = self.Cg.K()
            else:
                RV['Cg'] = sp.zeros_like(self.Cn.K())
            RV['Cn'] = self.Cn.K()
            RV['conv'] = sp.array([conv])
            RV['time'] = sp.array([TIME.time() - start])
            RV['NLL0'] = sp.array([LML])
            RV['LMLgrad'] = sp.array([LMLgrad])
            RV['nit'] = sp.array([info['nit']])
            RV['funcalls'] = sp.array([info['funcalls']])
            self.null = RV
            if cache:
                f = h5py.File(out_file, 'w')
                smartDumpDictHdf5(RV, f)
                f.close()
        return RV
コード例 #6
0
    # define row caoriance
    f = 10
    X = 1.*(sp.rand(N, f)<0.2)
    R = covar_rescale(sp.dot(X,X.T))
    R+= 1e-4 * sp.eye(N)
    S_R, U_R = la.eigh(R)

    # define col covariances
    Cg = FreeFormCov(P)
    Cn = FreeFormCov(P)
    Cg.setRandomParams()
    Cn.setRandomParams()

    # define gp
    gp = GP2KronSum(Y=Y, F=F, A=A, Cg=Cg, Cn=Cn, S_R=S_R, U_R=U_R)
    pdb.set_trace()
    print gp.covar.Sr()
    print gp.LML()
    print gp.covar.Sr()
    print Cg.setRandomParams()

    t0 = time.time()
    print 'GP2KronSum.LML():', gp.LML()
    print 'Time elapsed:', time.time() - t0

    # compare with normal gp
    # assess compatibility with this GP
    gp0 = GP(covar = copy.deepcopy(gp.covar), mean = copy.deepcopy(gp.mean))
    t0 = time.time()
    print 'GP.LML():', gp0.LML()