Skip to content

gradient_aware_harmonisation.spline#

Spline handling

Classes:

Name Description
ProductOfSplines

Product of two splines

Spline

Single spline

SplineScipy

An adapter which wraps various classes from scipy.interpolate

SumOfSplines

Sum of two splines

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)

ProductOfSplines #

Product 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 ProductOfSplines:
    """
    Product 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
        """
        # use the product rule in order to get the derivative of the product
        # of two splines
        return SumOfSplines(
            ProductOfSplines(self.spline_one, self.spline_two.derivative()),
            ProductOfSplines(self.spline_one.derivative(), self.spline_two),
        )

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

        Returns
        -------
        :
            Anti-derivative of self
        """
        # computation of the antiderivative of a product of splines is not
        # straightforward
        # However, as we don't need the antiderivative in our current workflow
        # we leave it for the time being as "not implemented"
        raise NotImplementedError

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
    """
    # computation of the antiderivative of a product of splines is not
    # straightforward
    # However, as we don't need the antiderivative in our current workflow
    # we leave it for the time being as "not implemented"
    raise NotImplementedError

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
    """
    # use the product rule in order to get the derivative of the product
    # of two splines
    return SumOfSplines(
        ProductOfSplines(self.spline_one, self.spline_two.derivative()),
        ProductOfSplines(self.spline_one.derivative(), self.spline_two),
    )

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())