def __init__(self, X=None, y=None, kernel="se", kernel_params=(1, 1,), kernel_priors = None, kernel_extra = None, mean="constant"):
        self.kernel_name = kernel
        self.kernel_params = kernel_params
        self.kernel_priors = kernel_priors
        self.kernel = kernels.setup_kernel(kernel, kernel_params, kernel_extra, kernel_priors)
        self.X = X
        self.n = X.shape[0]

        if mean == "zero":
            self.mu = 0
            self.y = y
        if mean == "constant":
            self.mu = np.mean(y)
            self.y = y - self.mu
        if mean == "linear":
            raise RuntimeError("linear mean not yet implemented...")
        self.mean = mean

        self.K = None
        self.L = None
        self.alpha = None
        self.Kinv = None
        self.posdef = True

        self.tree = KDTree(X)

        self.alpha = self._treeCG()
        print "trained CG, found alpha", self.alpha

        self._invert_kernel_matrix()
        print "true alpha is", self.alpha
    def __init__(self, X=None, y=None, kernel="se", kernel_params=(1, 1,), kernel_priors = None, kernel_extra = None, K=None, inv=True, mean="constant", ignore_pos_def_errors=False, fname=None):
        if fname is not None:
            self.load_trained_model(fname)
        else:
            self.kernel_name = kernel
            self.kernel_params = kernel_params
            self.kernel_priors = kernel_priors
            self.kernel = kernels.setup_kernel(kernel, kernel_params, kernel_extra, kernel_priors)
            self.X = X
            self.n = X.shape[0]

            if mean == "zero":
                self.mu = 0
                self.y = y
            if mean == "constant":
                self.mu = np.mean(y)
                self.y = y - self.mu
            if mean == "linear":
                raise RuntimeError("linear mean not yet implemented...")
            self.mean = mean

            # train model
            if K is None:
                self.K = self.kernel(X, X)
            else:
                self.K = K

            self.L = None
            self.alpha = None
            self.Kinv = None
            self.posdef = True
            try:
                self.L = scipy.linalg.cholesky(self.K, lower=True)
                self.alpha = scipy.linalg.cho_solve((self.L, True), self.y)
            except np.linalg.linalg.LinAlgError:

                if ignore_pos_def_errors:
                    self.posdef=False
                    return
                else:
                    # print traceback.format_exc(limit=100)
                    # u,v = np.linalg.eig(self.K)
                    # print self.K, u

                    print "raising"
#                    import pdb
#                    pdb.set_trace()
#                    sys.exit(1)

                    raise

            if inv:
                self._invert_kernel_matrix()
    def load_trained_model(self, filename):
        npzfile = np.load(filename)
        self.X = npzfile['X']
        self.y = npzfile['y']
        self.mu = npzfile['mu'][0]
        self.mean = npzfile['mname'][0]
        self.kernel_name = npzfile['kernel_name'][0]
        self.kernel_params = npzfile['kernel_params']
        self.alpha = npzfile['alpha']
        self.Kinv = npzfile['Kinv']
        self.L = npzfile['L']
        self.K = npzfile['K']

        self.n = self.X.shape[0]
        self.kernel = kernels.setup_kernel(kernel_name, kernel_params)