<commit_before>__all__ = ["GP"]

import numpy as np
import copy
import matplotlib.pyplot as plt
import scipy.optimize as optim
import warnings

from .ext import gp_c

DTYPE = np.float64
EPS = np.finfo(DTYPE).eps
MIN = np.log(np.exp2(DTYPE(np.finfo(DTYPE).minexp + 4)))


def memoprop(f):
    """
    Memoized property.

    When the property is accessed for the first time, the return value
    is stored and that value is given on subsequent calls. The memoized
    value can be cleared by calling 'del prop', where prop is the name
    of the property.

    """
    fname = f.__name__

    def fget(self):
        if fname not in self._memoized:
            self._memoized[fname] = f(self)
        return self._memoized[fname]

    def fdel(self):
        del self._memoized[fname]

    prop = property(fget=fget, fdel=fdel, doc=f.__doc__)
    return prop


class GP(object):
    r"""
    Gaussian Process object.

    Parameters
    ----------
    K : :class:`kernels.Kernel`
        Kernel object
    x : numpy.ndarray
        :math:`n` array of input locations
    y : numpy.ndarray
        :math:`n` array of input observations
    s : number (default=0)
        Standard deviation of observation noise

    """

    def __init__(self, K, x, y, s=0):
        r"""
        Initialize the GP.

        """
        self._memoized = {}

        #: Kernel for the gaussian process, of type
        #: :class:`kernels.Kernel`
        self.K = K

        self.x = x
        self.y = y
        self.s = s

    @property
    def x(self):
        r"""
        Vector of input locations.

        Returns
        -------
        x : numpy.ndarray
            :math:`n` array, where :math:`n` is the number of
            locations.

        """
        return self._x

    @x.setter
    def x(self, val):
        self._memoized = {}
        self._x = val.astype(DTYPE)
        self.n, = self.x.shape

    @property
    def y(self):
        r"""
        Vector of input observations.

        Returns
        -------
        y : numpy.ndarray
            :math:`n` array, where :math:`n` is the number of
            observations.

        """
        return self._y

    @y.setter
    def y(self, val):
        self._memoized = {}
        self._y = val.astype(DTYPE)
        if self.y.shape != (self.n,):
            raise ValueError("invalid shape for y: %s" % str(self.y.shape))

    @property
    def s(self):
        r"""
        Standard deviation of the observation noise for the gaussian
        process.

        Returns
        -------
        s : numpy.float64

        """
        return self._s

    @s.setter
    def s(self, val):
        self._memoized = {}
        self._s = DTYPE(val)

    @property
    def params(self):
        r"""
        Gaussian process parameters.

        Returns
        -------
        params : numpy.ndarray
           Consists of the kernel's parameters, `self.K.params`, and the
           observation noise parameter, :math:`s`, in that order.

        """
        return np.concatenate([self.K.params, [self._s]])

    @params.setter
    def params(self, val):
        params = self.params
        if (params[:-1] != val[:-1]).any():
            self._memoized = {}
            self.K.params = val[:-1]
        if params[-1] != val[-1]:
            self.s = val[-1]

    def copy(self):
        """
        Create a copy of the gaussian process object.

        Returns
        -------
        gp : :class:`gp.GP`
            New gaussian process object

        """
        new_gp = GP(self.K.copy(), self.x, self.y, s=self.s)
        new_gp._memoized = copy.deepcopy(self._memoized)
        return new_gp

    @memoprop
    def Kxx(self):
        r"""
        Kernel covariance matrix :math:`\mathbf{K}_{xx}`.

        Returns
        -------
        Kxx : numpy.ndarray
            :math:`n\times n` covariance matrix

        Notes
        -----
        The entry at index :math:`(i, j)` is defined as:
<commit_msg>Clean up property setters in gp.py<commit_after>__all__ = ["GP"]

import numpy as np
import copy
import matplotlib.pyplot as plt
import scipy.optimize as optim
import warnings

from .ext import gp_c

DTYPE = np.float64
EPS = np.finfo(DTYPE).eps
MIN = np.log(np.exp2(DTYPE(np.finfo(DTYPE).minexp + 4)))


def memoprop(f):
    """
    Memoized property.

    When the property is accessed for the first time, the return value
    is stored and that value is given on subsequent calls. The memoized
    value can be cleared by calling 'del prop', where prop is the name
    of the property.

    """
    fname = f.__name__

    def fget(self):
        if fname not in self._memoized:
            self._memoized[fname] = f(self)
        return self._memoized[fname]

    def fdel(self):
        del self._memoized[fname]

    prop = property(fget=fget, fdel=fdel, doc=f.__doc__)
    return prop


class GP(object):
    r"""
    Gaussian Process object.

    Parameters
    ----------
    K : :class:`kernels.Kernel`
        Kernel object
    x : numpy.ndarray
        :math:`n` array of input locations
    y : numpy.ndarray
        :math:`n` array of input observations
    s : number (default=0)
        Standard deviation of observation noise

    """

    def __init__(self, K, x, y, s=0):
        r"""
        Initialize the GP.

        """
        self._memoized = {}
        self._x = None
        self._y = None
        self._s = None
        self.n = 0

        #: Kernel for the gaussian process, of type
        #: :class:`kernels.Kernel`
        self.K = K
        self._params = np.empty(K.params.size + 1, dtype=DTYPE)

        self.x = x
        self.y = y
        self.s = s

    @property
    def x(self):
        r"""
        Vector of input locations.

        Returns
        -------
        x : numpy.ndarray
            :math:`n` array, where :math:`n` is the number of
            locations.

        """
        return self._x

    @x.setter
    def x(self, val):
        if np.any(val != self._x):
            self._memoized = {}
            self._x = val.astype(DTYPE)
            self.n, = self.x.shape

    @property
    def y(self):
        r"""
        Vector of input observations.

        Returns
        -------
        y : numpy.ndarray
            :math:`n` array, where :math:`n` is the number of
            observations.

        """
        return self._y

    @y.setter
    def y(self, val):
        if np.any(val != self._y):
            self._memoized = {}
            self._y = val.astype(DTYPE)
            if self.y.shape != (self.n,):
                raise ValueError("invalid shape for y: %s" % str(self.y.shape))

    @property
    def s(self):
        r"""
        Standard deviation of the observation noise for the gaussian
        process.

        Returns
        -------
        s : numpy.float64

        """
        return self._s

    @s.setter
    def s(self, val):
        if val != self._s:
            self._memoized = {}
            self._s = DTYPE(val)

    @property
    def params(self):
        r"""
        Gaussian process parameters.

        Returns
        -------
        params : numpy.ndarray
           Consists of the kernel's parameters, `self.K.params`, and the
           observation noise parameter, :math:`s`, in that order.

        """
        self._params[:-1] = self.K.params
        self._params[-1] = self._s
        return self._params

    @params.setter
    def params(self, val):
        if np.any(self.params != val):
            self._memoized = {}
            self.K.params = val[:-1]
            self.s = val[-1]

    def copy(self):
        """
        Create a copy of the gaussian process object.

        Returns
        -------
        gp : :class:`gp.GP`
            New gaussian process object

        """
        new_gp = GP(self.K.copy(), self.x, self.y, s=self.s)
        new_gp._memoized = copy.deepcopy(self._memoized)
        return new_gp

    @memoprop
    def Kxx(self):
        r"""
        Kernel covariance matrix :math:`\mathbf{K}_{xx}`.

        Returns
        -------
        Kxx : numpy.ndarray
            :math:`n\times n` covariance matrix

        Notes
        -----
        The entry at index :math:`(i, j)` is defined as:

{'fraction_non_alphanumeric': 0.12218296991107322, 'fraction_numerical': 0.0034108904860518942, 'mean_word_length': 1.5817610062893082, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1280827', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2485, 'n_words': 720}