Skip to content

Commit

Permalink
Moves GP kernels to utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
dimtsap committed Mar 6, 2023
1 parent 5564ab8 commit 2917cca
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from UQpy.utilities.Utilities import process_random_state
from UQpy.surrogates.baseclass.Surrogate import Surrogate
from UQpy.utilities.ValidationTypes import RandomStateType
from UQpy.surrogates.gaussian_process.kernels.baseclass.Kernel import Kernel
from UQpy.utilities.kernels.baseclass.Kernel import Kernel
from UQpy.surrogates.gaussian_process.constraints.baseclass.Constraints import ConstraintsGPR
from UQpy.surrogates.gaussian_process.regression_models.baseclass.Regression import Regression


class GaussianProcessRegression(Surrogate):
Expand Down
3 changes: 0 additions & 3 deletions src/UQpy/surrogates/gaussian_process/kernels/__init__.py

This file was deleted.

39 changes: 0 additions & 39 deletions src/UQpy/surrogates/gaussian_process/kernels/baseclass/Kernel.py

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions src/UQpy/utilities/kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from UQpy.utilities.kernels.baseclass import *
from UQpy.utilities.kernels.euclidean_kernels import *

from UQpy.utilities.kernels.BinetCauchyKernel import BinetCauchyKernel
from UQpy.utilities.kernels.GaussianKernel import GaussianKernel
Expand Down
55 changes: 21 additions & 34 deletions src/UQpy/utilities/kernels/baseclass/Kernel.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,39 @@
import itertools
from abc import ABC, abstractmethod
from typing import Union

import numpy as np

from UQpy.utilities import GrassmannPoint
from UQpy.utilities.ValidationTypes import NumpyFloatArray, Numpy2DFloatArray


class Kernel(ABC):
"""
This is the baseclass for all kernels in :py:mod:`UQpy`.
This serves as a blueprint to show the methods for kernels implemented in the :py:mod:`.kernels` module .
Abstract base class of all Kernels. Serves as a template for creating new Gaussian Process covariance
functions.
"""
def __init__(self):
self.kernel_matrix: np.ndarray = None
"""Kernel matrix defining the similarity between the points"""

def calculate_kernel_matrix(self, points: Union[Numpy2DFloatArray, list[GrassmannPoint]]):
"""
Using the kernel-specific :py:meth:`.kernel_entry` method, this function assembles the kernel matrix.
def __init__(self, kernel_parameter: Union[int, float]):
self.__kernel_parameter = kernel_parameter

:param points: Set of data points. Depending on the type of kernel these should be either :class:`numpy.ndarray`
or of type :class:`.GrassmannPoint`.
"""
pass
@property
def kernel_parameter(self):
return self.__kernel_parameter

def optimize_parameters(self, data: Union[Numpy2DFloatArray, list[GrassmannPoint]], **kwargs_optimization):
"""
This serves as a blueprint function in case a kernel provides the ability to optimize its parameters. In that
case, the kernel will override this method, and store the optimized parameters in the kernel's attributes.
@kernel_parameter.setter
def kernel_parameter(self, value):
self.__kernel_parameter = value

:param data: Set of data points.
:param kwargs_optimization: Keyword arguments containing any extra parameters needed to perform the
optimization. These will be unique to the specific kernel.
"""
pass

@abstractmethod
def kernel_entry(self, xi: Union[Numpy2DFloatArray, GrassmannPoint],
xj: Union[Numpy2DFloatArray, GrassmannPoint]):
def calculate_kernel_matrix(self, x, s):
"""
Given two points, this method calculates the respective kernel entry. Each concrete kernel implementation must
override this method and provide its own implementation.
:param xi: First point.
:param xj: Second point.
:return: Float representing the kernel entry.
Abstract method that needs to be implemented by the user when creating a new Covariance function.
"""
pass

@staticmethod
def check_samples_and_return_stack(x, s):
x_, s_ = np.atleast_2d(x), np.atleast_2d(s)
# Create stack matrix, where each block is x_i with all s
stack = np.tile(
np.swapaxes(np.atleast_3d(x_), 1, 2), (1, np.size(s_, 0), 1)
) - np.tile(s_, (np.size(x_, 0), 1, 1))
return stack
2 changes: 1 addition & 1 deletion src/UQpy/utilities/kernels/baseclass/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from UQpy.utilities.kernels.baseclass.Kernel import Kernel
from UQpy.utilities.kernels.baseclass.DRKernel import Kernel
from UQpy.utilities.kernels.baseclass.EuclideanKernel import EuclideanKernel
from UQpy.utilities.kernels.baseclass.GrassmannianKernel import GrassmannianKernel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from UQpy.surrogates.gaussian_process.kernels.baseclass.Kernel import *
from scipy.spatial.distance import pdist, cdist, squareform
from UQpy.utilities.kernels.baseclass.Kernel import *
from scipy.spatial.distance import cdist
from scipy.special import gamma, kv


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Union

from UQpy.surrogates.gaussian_process.kernels.baseclass.Kernel import *
from UQpy.utilities.kernels.baseclass.Kernel import *


class RBF(Kernel):
Expand Down
2 changes: 2 additions & 0 deletions src/UQpy/utilities/kernels/euclidean_kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from UQpy.utilities.kernels.euclidean_kernels.Matern import Matern
from UQpy.utilities.kernels.euclidean_kernels.RBF import RBF

0 comments on commit 2917cca

Please sign in to comment.