# Path: pygp/utils/models.py
# def get_params(obj):
#     """
#     Helper function which translates the values returned by _params() into
#     something more meaningful.
#     """
#     offset = 0
#     for param in obj._params():
#         key, size, log = param
#         block = slice(offset, offset+size)
#         offset += size
#         yield key, block, log


import numpy as np
import scipy.optimize as so
from ..utils.models import get_params

# future imports
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

# global imports

# local imports

# exported symbols
__all__ = ['optimize']


def optimize(gp, priors=None):
    """
    Perform type-II maximum likelihood to fit GP hyperparameters.

    If given the priors object should be a dictionary mapping named parameters
    to an object which implements `prior.loglikelihood(hyper, grad)`. If a
    parameter is mapped to the `None` value then this will be assumed fixed.

    Note: nothing is returned by this function. Instead it will modify the
    hyperparameters of the given GP object in place.
    """
    hyper0 = gp.get_hyper()
    active = np.ones(gp.nhyper, dtype=bool)

    # this just manipulates a few lists so that we transform priors into a list
    # of tuples of the form (block, log, prior) for each named prior.

# Path: pygp/utils/models.py
# def get_params(obj):
#     """
#     Helper function which translates the values returned by _params() into
#     something more meaningful.
#     """
#     offset = 0
#     for param in obj._params():
#         key, size, log = param
#         block = slice(offset, offset+size)
#         offset += size
#         yield key, block, log


# Path: pygp/learning/optimization.py
import numpy as np
import scipy.optimize as so
from ..utils.models import get_params


# future imports
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

# global imports

# local imports

# exported symbols
__all__ = ['optimize']


def optimize(gp, priors=None):
    """
    Perform type-II maximum likelihood to fit GP hyperparameters.

    If given the priors object should be a dictionary mapping named parameters
    to an object which implements `prior.loglikelihood(hyper, grad)`. If a
    parameter is mapped to the `None` value then this will be assumed fixed.

    Note: nothing is returned by this function. Instead it will modify the
    hyperparameters of the given GP object in place.
    """
    hyper0 = gp.get_hyper()
    active = np.ones(gp.nhyper, dtype=bool)

    # this just manipulates a few lists so that we transform priors into a list
    # of tuples of the form (block, log, prior) for each named prior.

    params = dict((key, (block, log)) for (key, block, log) in get_params(gp))