Skip to content

gradient_aware_harmonisation.spline#

Spline handling

Classes:

Name Description
Spline

Single spline

SplineScipy

An adapter which wraps various classes from scipy.interpolate

SumOfSplines

Sum of two splines

Functions:

Name Description
add_constant_to_spline

Add a constant value to a spline

Attributes:

Name Type Description
NP_ARRAY_OF_FLOAT_OR_INT TypeAlias

Type alias for an array of numpy float or int (not complex)

NP_FLOAT_OR_INT TypeAlias

Type alias for a numpy float or int (not complex)

NP_ARRAY_OF_FLOAT_OR_INT module-attribute #

NP_ARRAY_OF_FLOAT_OR_INT: TypeAlias = NDArray[
    NP_FLOAT_OR_INT
]

Type alias for an array of numpy float or int (not complex)

NP_FLOAT_OR_INT module-attribute #

NP_FLOAT_OR_INT: TypeAlias = Union[
    floating[Any], integer[Any]
]

Type alias for a numpy float or int (not complex)

Spline #

Bases: Protocol

Single spline

Methods:

Name Description
__call__

Get the value of the spline at a particular x-value

antiderivative

Calculate the anti-derivative/integral of self

derivative

Calculate the derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
class Spline(Protocol):
    """
    Single spline
    """

    # domain: [float, float]
    # """Domain over the spline can be used"""

    @overload
    def __call__(self, x: int | float) -> int | float: ...

    @overload
    def __call__(self, x: NP_FLOAT_OR_INT) -> NP_FLOAT_OR_INT: ...

    @overload
    def __call__(self, x: NP_ARRAY_OF_FLOAT_OR_INT) -> NP_ARRAY_OF_FLOAT_OR_INT: ...

    def __call__(
        self, x: int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT
    ) -> int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT:
        """Get the value of the spline at a particular x-value"""

    def derivative(self) -> Spline:
        """
        Calculate the derivative of self
        """

    def antiderivative(self) -> Spline:
        """
        Calculate the anti-derivative/integral of self
        """

__call__ #

__call__(x: int | float) -> int | float

Get the value of the spline at a particular x-value

Source code in src/gradient_aware_harmonisation/spline.py
def __call__(
    self, x: int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT
) -> int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT:
    """Get the value of the spline at a particular x-value"""

antiderivative #

antiderivative() -> Spline

Calculate the anti-derivative/integral of self

Source code in src/gradient_aware_harmonisation/spline.py
def antiderivative(self) -> Spline:
    """
    Calculate the anti-derivative/integral of self
    """

derivative #

derivative() -> Spline

Calculate the derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
def derivative(self) -> Spline:
    """
    Calculate the derivative of self
    """

SplineScipy #

An adapter which wraps various classes from scipy.interpolate

Methods:

Name Description
__call__

Evaluate the spline at a given x-value

antiderivative

Calculate the anti-derivative/integral of self

derivative

Calculate the derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
@define
class SplineScipy:
    """
    An adapter which wraps various classes from [scipy.interpolate][]
    """

    # domain: ClassVar[list[float, float]] = [
    #     np.finfo(np.float64).tiny,
    #     np.finfo(np.float64).max,
    # ]
    # """domain of spline (reals)"""

    scipy_spline: scipy.interpolate.BSpline | scipy.interpolate.PPoly

    @overload
    def __call__(self, x: int | float) -> int | float: ...

    @overload
    def __call__(self, x: NP_FLOAT_OR_INT) -> NP_FLOAT_OR_INT: ...

    @overload
    def __call__(self, x: NP_ARRAY_OF_FLOAT_OR_INT) -> NP_ARRAY_OF_FLOAT_OR_INT: ...

    def __call__(
        self, x: int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT
    ) -> int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT:
        """
        Evaluate the spline at a given x-value

        Parameters
        ----------
        x
            x-value

        Returns
        -------
        :
            Value of the spline at `x`
        """
        return self.scipy_spline(x)

    def derivative(self) -> SplineScipy:
        """
        Calculate the derivative of self

        Returns
        -------
        :
            Derivative of self
        """
        return SplineScipy(self.scipy_spline.derivative())

    def antiderivative(self) -> SplineScipy:
        """
        Calculate the anti-derivative/integral of self

        Returns
        -------
        :
            Anti-derivative of self
        """
        return SplineScipy(self.scipy_spline.antiderivative())

__call__ #

__call__(x: int | float) -> int | float

Evaluate the spline at a given x-value

Parameters:

Name Type Description Default
x int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT

x-value

required

Returns:

Type Description
int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT

Value of the spline at x

Source code in src/gradient_aware_harmonisation/spline.py
def __call__(
    self, x: int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT
) -> int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT:
    """
    Evaluate the spline at a given x-value

    Parameters
    ----------
    x
        x-value

    Returns
    -------
    :
        Value of the spline at `x`
    """
    return self.scipy_spline(x)

antiderivative #

antiderivative() -> SplineScipy

Calculate the anti-derivative/integral of self

Returns:

Type Description
SplineScipy

Anti-derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
def antiderivative(self) -> SplineScipy:
    """
    Calculate the anti-derivative/integral of self

    Returns
    -------
    :
        Anti-derivative of self
    """
    return SplineScipy(self.scipy_spline.antiderivative())

derivative #

derivative() -> SplineScipy

Calculate the derivative of self

Returns:

Type Description
SplineScipy

Derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
def derivative(self) -> SplineScipy:
    """
    Calculate the derivative of self

    Returns
    -------
    :
        Derivative of self
    """
    return SplineScipy(self.scipy_spline.derivative())

SumOfSplines #

Sum of two splines

Methods:

Name Description
__call__

Evaluate the spline at a given x-value

antiderivative

Calculate the anti-derivative/integral of self

derivative

Calculate the derivative of self

Attributes:

Name Type Description
spline_one Spline

First spline

spline_two Spline

Second spline

Source code in src/gradient_aware_harmonisation/spline.py
@define
class SumOfSplines:
    """
    Sum of two splines
    """

    spline_one: Spline
    """First spline"""

    spline_two: Spline
    """Second spline"""

    # domain: ClassVar[list[float, float]] = [
    #     np.finfo(np.float64).tiny,
    #     np.finfo(np.float64).max,
    # ]
    # """Domain of spline"""

    @overload
    def __call__(self, x: int | float) -> int | float: ...

    @overload
    def __call__(self, x: NP_FLOAT_OR_INT) -> NP_FLOAT_OR_INT: ...

    @overload
    def __call__(self, x: NP_ARRAY_OF_FLOAT_OR_INT) -> NP_ARRAY_OF_FLOAT_OR_INT: ...

    def __call__(
        self, x: int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT
    ) -> int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT:
        """
        Evaluate the spline at a given x-value

        Parameters
        ----------
        x
            x-value

        Returns
        -------
        :
            Value of the spline at `x`
        """
        return self.spline_one(x) + self.spline_two(x)

    def derivative(self) -> SumOfSplines:
        """
        Calculate the derivative of self

        Returns
        -------
        :
            Derivative of self
        """
        return SumOfSplines(self.spline_one.derivative(), self.spline_two.derivative())

    def antiderivative(self) -> SumOfSplines:
        """
        Calculate the anti-derivative/integral of self

        Returns
        -------
        :
            Anti-derivative of self
        """
        return SumOfSplines(
            self.spline_one.antiderivative(), self.spline_two.antiderivative()
        )

spline_one instance-attribute #

spline_one: Spline

First spline

spline_two instance-attribute #

spline_two: Spline

Second spline

__call__ #

__call__(x: int | float) -> int | float

Evaluate the spline at a given x-value

Parameters:

Name Type Description Default
x int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT

x-value

required

Returns:

Type Description
int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT

Value of the spline at x

Source code in src/gradient_aware_harmonisation/spline.py
def __call__(
    self, x: int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT
) -> int | float | NP_FLOAT_OR_INT | NP_ARRAY_OF_FLOAT_OR_INT:
    """
    Evaluate the spline at a given x-value

    Parameters
    ----------
    x
        x-value

    Returns
    -------
    :
        Value of the spline at `x`
    """
    return self.spline_one(x) + self.spline_two(x)

antiderivative #

antiderivative() -> SumOfSplines

Calculate the anti-derivative/integral of self

Returns:

Type Description
SumOfSplines

Anti-derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
def antiderivative(self) -> SumOfSplines:
    """
    Calculate the anti-derivative/integral of self

    Returns
    -------
    :
        Anti-derivative of self
    """
    return SumOfSplines(
        self.spline_one.antiderivative(), self.spline_two.antiderivative()
    )

derivative #

derivative() -> SumOfSplines

Calculate the derivative of self

Returns:

Type Description
SumOfSplines

Derivative of self

Source code in src/gradient_aware_harmonisation/spline.py
def derivative(self) -> SumOfSplines:
    """
    Calculate the derivative of self

    Returns
    -------
    :
        Derivative of self
    """
    return SumOfSplines(self.spline_one.derivative(), self.spline_two.derivative())

add_constant_to_spline #

add_constant_to_spline(
    in_spline: Spline, constant: float | int
) -> Spline

Add a constant value to a spline

Parameters:

Name Type Description Default
in_spline Spline

Input spline

required
constant float | int

Constant to add

required

Returns:

Type Description
Spline

Spline plus the given constant

Source code in src/gradient_aware_harmonisation/spline.py
def add_constant_to_spline(in_spline: Spline, constant: float | int) -> Spline:
    """
    Add a constant value to a spline

    Parameters
    ----------
    in_spline
        Input spline

    constant
        Constant to add

    Returns
    -------
    :
        Spline plus the given constant
    """
    try:
        import scipy.interpolate
    except ImportError as exc:
        raise MissingOptionalDependencyError(
            "add_constant_to_spline", requirement="scipy"
        ) from exc

    return SumOfSplines(
        spline_one=in_spline,
        spline_two=SplineScipy(
            scipy.interpolate.PPoly(
                c=[[constant]],
                # # TODO: Problem: Currently domain is defined for SumOfSplines
                # #  and SplineScipy should be specified only once
                # #  preferably in SplineScipy
                # x=in_spline.domain,
                # TODO: better solution for domain handling
                x=[-1e8, 1e8],
            )
        ),
    )