# Path: features.py
# def featurizer_from_string(X, desc, extract_dim=0, transpose=False):
#     if desc.startswith("poly"):
#         degree = int(desc[4:])
#         Z, f, norm2, alpha = build_ortho_poly_featurizer(X, extract_dim, degree, transpose)
#         return Z, f, {'norm2': norm2, 'alpha': alpha, 'extract_dim': [extract_dim,]}
#     elif desc.startswith("sin"):
#         degree = int(desc[3:])
#         featurizer = build_sin_featurizer(extract_dim, degree, transpose)
#         Z = featurizer(X)
#         return Z, featurizer, {'extract_dim': [extract_dim,]}
#     elif desc.startswith("mlinear"):
#         featurizer, means,scales = build_multilinear(X, extract_dim, transpose)
#         Z = featurizer(X)
#         return Z, featurizer, {'means': means, 'scales': scales, 'extract_dim': extract_dim}
#     else:
#         raise ValueError("unrecognized feature type %s" % desc)
# 
# def recover_featurizer(desc, recovery_info, transpose=False):
#     if desc.startswith("poly"):
#         degree = int(desc[4:])
#         norm2 = recovery_info['norm2']
#         alpha = recovery_info['alpha']
#         extract_dim = recovery_info['extract_dim'][0]
#         if transpose:
#             f = lambda X : ortho_poly_predict(X[:,extract_dim], alpha, norm2, degree=degree).T
#         else:
#             f = lambda X : ortho_poly_predict(X[:,extract_dim], alpha, norm2, degree=degree)
#         return f, {'norm2': norm2, 'alpha': alpha, 'extract_dim': [extract_dim,]}
#     elif desc.startswith("sin"):
#         degree = int(desc[3:])
#         extract_dim = recovery_info['extract_dim'][0]
#         featurizer = build_sin_featurizer(extract_dim, degree, transpose)
#         return featurizer, {'extract_dim': [extract_dim,]}
#     elif desc.startswith("mlinear"):
#         means = recovery_info['means']
#         scales = recovery_info['scales']
#         dims = recovery_info['extract_dim']
#         if transpose:
#             featurizer = lambda X: multilinear_featurizer(X, dims, means, scales).T
#         else:
#             featurizer = lambda X: multilinear_featurizer(X, dims, means, scales)
#         return featurizer, {'means': means, 'scales': scales, 'extract_dim': dims}
# 
#     else:
#         raise ValueError("unrecognized feature type %s" % desc)
# 
# Path: util.py
# def mkdir_p(path):
#     try:
#         os.makedirs(path)
#     except OSError as exc: # Python >2.5
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else: raise
# 
# Path: gpy_linalg.py
# def pdinv(A, *args):
#     """
#     :param A: A DxD pd numpy array
# 
#     :rval Ai: the inverse of A
#     :rtype Ai: np.ndarray
#     :rval L: the Cholesky decomposition of A
#     :rtype L: np.ndarray
#     :rval Li: the Cholesky decomposition of Ai
#     :rtype Li: np.ndarray
#     :rval logdet: the log of the determinant of A
#     :rtype logdet: float64
# 
#     """
#     L = jitchol(A, *args)
#     logdet = 2.*np.sum(np.log(np.diag(L)))
#     Li = dtrtri(L)
#     Ai, _ = dpotri(L, lower=1)
#     # Ai = np.tril(Ai) + np.tril(Ai,-1).T
#     symmetrify(Ai)
# 
#     return Ai, L, Li, logdet
# 
# def dpotrs(A, B, lower=1):
#     """
#     Wrapper for lapack dpotrs function
#     :param A: Matrix A
#     :param B: Matrix B
#     :param lower: is matrix lower (true) or upper (false)
#     :returns:
#     """
#     A = force_F_ordered(A)
#     return lapack.dpotrs(A, B, lower=lower)


import os
import time
import numpy as np
import collections
import scipy
import scipy.sparse
import scipy.sparse.linalg
import scikits.sparse.cholmod
import pyublas
import hashlib
import types
import marshal
import scipy.weave as weave
        import scipy.sparse
                import sys; sys.exit(1)
            import imp
from features import featurizer_from_string, recover_featurizer
from cover_tree import VectorTree, MatrixTree
from util import mkdir_p
from scipy.weave import converters
from gpy_linalg import pdinv, dpotrs
            self.y_obs_variances = npzfile['y_obs_variances'][0]
        else:
            self.y_obs_variances = None
        self.alpha_r = npzfile['alpha_r'].flatten()
        if 'ymean' in npzfile:
            self.ymean = npzfile['ymean']
        else:
            self.ymean = 0.0

        try:
            # npz mode
            self.noise_var = npzfile['noise_var'].item()
            self.n_features = int(npzfile['n_features'])
        except:
            # dict mode
            self.noise_var = npzfile['noise_var'][0]
            self.n_features = npzfile['n_features'][0]

        self.cov_main = unpack_gpcov(npzfile, 'main')
        self.cov_fic = unpack_gpcov(npzfile, 'fic')
        if self.cov_fic is not None:
            self.Luu = npzfile['Luu'][0]


        self.sparse_threshold = npzfile['sparse_threshold'][0]
        self.ll = npzfile['ll'][0]


        if 'basis' in npzfile:
            self.basis = str(npzfile['basis'])

# Path: features.py
# def featurizer_from_string(X, desc, extract_dim=0, transpose=False):
#     if desc.startswith("poly"):
#         degree = int(desc[4:])
#         Z, f, norm2, alpha = build_ortho_poly_featurizer(X, extract_dim, degree, transpose)
#         return Z, f, {'norm2': norm2, 'alpha': alpha, 'extract_dim': [extract_dim,]}
#     elif desc.startswith("sin"):
#         degree = int(desc[3:])
#         featurizer = build_sin_featurizer(extract_dim, degree, transpose)
#         Z = featurizer(X)
#         return Z, featurizer, {'extract_dim': [extract_dim,]}
#     elif desc.startswith("mlinear"):
#         featurizer, means,scales = build_multilinear(X, extract_dim, transpose)
#         Z = featurizer(X)
#         return Z, featurizer, {'means': means, 'scales': scales, 'extract_dim': extract_dim}
#     else:
#         raise ValueError("unrecognized feature type %s" % desc)
# 
# def recover_featurizer(desc, recovery_info, transpose=False):
#     if desc.startswith("poly"):
#         degree = int(desc[4:])
#         norm2 = recovery_info['norm2']
#         alpha = recovery_info['alpha']
#         extract_dim = recovery_info['extract_dim'][0]
#         if transpose:
#             f = lambda X : ortho_poly_predict(X[:,extract_dim], alpha, norm2, degree=degree).T
#         else:
#             f = lambda X : ortho_poly_predict(X[:,extract_dim], alpha, norm2, degree=degree)
#         return f, {'norm2': norm2, 'alpha': alpha, 'extract_dim': [extract_dim,]}
#     elif desc.startswith("sin"):
#         degree = int(desc[3:])
#         extract_dim = recovery_info['extract_dim'][0]
#         featurizer = build_sin_featurizer(extract_dim, degree, transpose)
#         return featurizer, {'extract_dim': [extract_dim,]}
#     elif desc.startswith("mlinear"):
#         means = recovery_info['means']
#         scales = recovery_info['scales']
#         dims = recovery_info['extract_dim']
#         if transpose:
#             featurizer = lambda X: multilinear_featurizer(X, dims, means, scales).T
#         else:
#             featurizer = lambda X: multilinear_featurizer(X, dims, means, scales)
#         return featurizer, {'means': means, 'scales': scales, 'extract_dim': dims}
# 
#     else:
#         raise ValueError("unrecognized feature type %s" % desc)
# 
# Path: util.py
# def mkdir_p(path):
#     try:
#         os.makedirs(path)
#     except OSError as exc: # Python >2.5
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else: raise
# 
# Path: gpy_linalg.py
# def pdinv(A, *args):
#     """
#     :param A: A DxD pd numpy array
# 
#     :rval Ai: the inverse of A
#     :rtype Ai: np.ndarray
#     :rval L: the Cholesky decomposition of A
#     :rtype L: np.ndarray
#     :rval Li: the Cholesky decomposition of Ai
#     :rtype Li: np.ndarray
#     :rval logdet: the log of the determinant of A
#     :rtype logdet: float64
# 
#     """
#     L = jitchol(A, *args)
#     logdet = 2.*np.sum(np.log(np.diag(L)))
#     Li = dtrtri(L)
#     Ai, _ = dpotri(L, lower=1)
#     # Ai = np.tril(Ai) + np.tril(Ai,-1).T
#     symmetrify(Ai)
# 
#     return Ai, L, Li, logdet
# 
# def dpotrs(A, B, lower=1):
#     """
#     Wrapper for lapack dpotrs function
#     :param A: Matrix A
#     :param B: Matrix B
#     :param lower: is matrix lower (true) or upper (false)
#     :returns:
#     """
#     A = force_F_ordered(A)
#     return lapack.dpotrs(A, B, lower=lower)


# Path: gp.py
import os
import time
import numpy as np
import collections
import scipy
import scipy.sparse
import scipy.sparse.linalg
import scikits.sparse.cholmod
import pyublas
import hashlib
import types
import marshal
import scipy.weave as weave
        import scipy.sparse
                import sys; sys.exit(1)
            import imp
from features import featurizer_from_string, recover_featurizer
from cover_tree import VectorTree, MatrixTree
from util import mkdir_p
from scipy.weave import converters
from gpy_linalg import pdinv, dpotrs

            self.y_obs_variances = npzfile['y_obs_variances'][0]
        else:
            self.y_obs_variances = None
        self.alpha_r = npzfile['alpha_r'].flatten()
        if 'ymean' in npzfile:
            self.ymean = npzfile['ymean']
        else:
            self.ymean = 0.0

        try:
            # npz mode
            self.noise_var = npzfile['noise_var'].item()
            self.n_features = int(npzfile['n_features'])
        except:
            # dict mode
            self.noise_var = npzfile['noise_var'][0]
            self.n_features = npzfile['n_features'][0]

        self.cov_main = unpack_gpcov(npzfile, 'main')
        self.cov_fic = unpack_gpcov(npzfile, 'fic')
        if self.cov_fic is not None:
            self.Luu = npzfile['Luu'][0]


        self.sparse_threshold = npzfile['sparse_threshold'][0]
        self.ll = npzfile['ll'][0]


        if 'basis' in npzfile:
            self.basis = str(npzfile['basis'])

            self.featurizer, self.featurizer_recovery = recover_featurizer(self.basis, npzfile, transpose=True)