예제 #1
0
    def _initGP(self,colCovarType,rank_r,rank_g,rank_n):
        """
        Initializes genetic and noise LIMIX trait covar
        Args:
            colCovarType_g:     genetic trait covariance type
            colCovarType_n:     noise trait covariance type
            rank_r:     rank of region trait covar
            rank_g:     rank of genetic trait covar
            rank_n:     rank of noise trait covar
        """
        if self.P==1:
            self.rank_r = 1
            self.rank_g = 1
            self.rank_n = 1
            # ideally should be diag
            colCovarType = 'freeform'
        elif colCovarType is None:
            colCovarType='freeform'

        self.rank_r = rank_r
        self.rank_g = rank_g
        self.rank_n = rank_n
        self.colCovarType = colCovarType
        self.Cg = self._buildTraitCovar(colCovarType,rank_g)
        self.Cn = self._buildTraitCovar(colCovarType,rank_n)
        XXnotNone = self.XX is not None
        SUnotNone = self.S_XX is not None and self.U_XX is not None
        # build mean
        if self.bgRE:
            self.gp = gp3kronSum(self.mean,self.Cg,self.Cn,XX=self.XX,S_XX=self.S_XX,U_XX=self.U_XX,rank=self.rank_r)
        else:
            self.gp = gp2kronSumLR(self.Y,self.Cn,F=self.F,rank=self.rank_r)
예제 #2
0
    def fitNull(self,verbose=True,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(self.mean,self.Cg,self.Cn,XX=self.XX,S_XX=self.S_XX,U_XX=self.U_XX)
            else:
                self.gpNull = gp2kronSumLR(self.Y,self.Cn,Xr=SP.ones((self.N,1)),F=self.F)
            for i in range(n_times):
                params0,Ifilter=self._initParams(init_method=init_method)
                conv,info = OPT.opt_hyper(self.gpNull,params0,Ifilter=Ifilter,factr=factr)
                if conv: break
            if not conv:    warnings.warn("not converged")
            LMLgrad = SP.concatenate([self.gpNull.LMLgrad()[key]**2 for key in self.gpNull.LMLgrad().keys()]).mean()
            LML = self.gpNull.LML()
            if 'mean' in params0.keys():
                RV['params_mean'] = self.gpNull.mean.getParams()
            RV['params0_g'] = self.Cg.getParams()
            RV['params0_n'] = self.Cn.getParams()
            RV['Cg'] = self.Cg.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']])
            if self.bgRE:
                RV['h2'] = self.gpNull.h2()
                RV['h2_ste'] = self.gpNull.h2_ste()
                RV['Cg_ste'] = self.gpNull.ste('Cg')
                RV['Cn_ste'] = self.gpNull.ste('Cn')
            self.null = RV
            if cache:
                f = h5py.File(out_file,'w')
                dumpDictHdf5(RV,f)
                f.close()
        return RV
예제 #3
0
    def _initGP(self, colCovarType, rank_r, rank_g, rank_n):
        """
        Initializes genetic and noise LIMIX trait covar
        Args:
            colCovarType_g:     genetic trait covariance type
            colCovarType_n:     noise trait covariance type
            rank_r:     rank of region trait covar
            rank_g:     rank of genetic trait covar
            rank_n:     rank of noise trait covar
        """
        if self.P == 1:
            self.rank_r = 1
            self.rank_g = 1
            self.rank_n = 1
            # ideally should be diag
            colCovarType = 'freeform'
        elif colCovarType is None:
            colCovarType = 'freeform'

        self.rank_r = rank_r
        self.rank_g = rank_g
        self.rank_n = rank_n
        self.colCovarType = colCovarType
        self.Cg = self._buildTraitCovar(colCovarType, rank_g)
        self.Cn = self._buildTraitCovar(colCovarType, rank_n)
        XXnotNone = self.XX is not None
        SUnotNone = self.S_XX is not None and self.U_XX is not None
        # build mean
        if self.bgRE:
            self.gp = gp3kronSum(self.mean,
                                 self.Cg,
                                 self.Cn,
                                 XX=self.XX,
                                 S_XX=self.S_XX,
                                 U_XX=self.U_XX,
                                 rank=self.rank_r)
        else:
            self.gp = gp2kronSumLR(self.Y, self.Cn, F=self.F, rank=self.rank_r)
예제 #4
0
    def fitNull(self,
                verbose=True,
                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(self.mean,
                                         self.Cg,
                                         self.Cn,
                                         XX=self.XX,
                                         S_XX=self.S_XX,
                                         U_XX=self.U_XX)
            else:
                self.gpNull = gp2kronSumLR(self.Y,
                                           self.Cn,
                                           Xr=SP.ones((self.N, 1)),
                                           F=self.F)
            for i in range(n_times):
                params0, Ifilter = self._initParams(init_method=init_method)
                conv, info = OPT.opt_hyper(self.gpNull,
                                           params0,
                                           Ifilter=Ifilter,
                                           factr=factr)
                if conv: break
            if not conv: warnings.warn("not converged")
            LMLgrad = SP.concatenate([
                self.gpNull.LMLgrad()[key]**2
                for key in self.gpNull.LMLgrad().keys()
            ]).mean()
            LML = self.gpNull.LML()
            if 'mean' in params0.keys():
                RV['params_mean'] = self.gpNull.mean.getParams()
            RV['params0_g'] = self.Cg.getParams()
            RV['params0_n'] = self.Cn.getParams()
            RV['Cg'] = self.Cg.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']])
            if self.bgRE:
                RV['h2'] = self.gpNull.h2()
                RV['h2_ste'] = self.gpNull.h2_ste()
                RV['Cg_ste'] = self.gpNull.ste('Cg')
                RV['Cn_ste'] = self.gpNull.ste('Cn')
            self.null = RV
            if cache:
                f = h5py.File(out_file, 'w')
                dumpDictHdf5(RV, f)
                f.close()
        return RV