Skip to content

gradient_aware_harmonisation.utils#

Utility functions

Classes:

Name Description
GetHarmonisedSplineLike

A callable which can generate a final, harmonised spline

Functions:

Name Description
add_constant_to_spline

Add a constant value to a spline

harmonise_splines

Harmonise spline

GetHarmonisedSplineLike #

Bases: Protocol

A callable which can generate a final, harmonised spline

The harmonised spline is generated based on a harmonised spline that doesn't consider convergence and a spline to which the final, harmonised spline should converge.

Methods:

Name Description
__call__

Generate the harmonised spline

Source code in src/gradient_aware_harmonisation/utils.py
class GetHarmonisedSplineLike(Protocol):
    """
    A callable which can generate a final, harmonised spline

    The harmonised spline is generated based on a
    harmonised spline that doesn't consider convergence
    and a spline to which the final, harmonised spline should converge.
    """

    def __call__(
        self,
        harmonisation_time: Union[int, float],
        convergence_time: Union[int, float],
        harmonised_spline_no_convergence: Spline,
        convergence_spline: Spline,
    ) -> Spline:
        """
        Generate the harmonised spline

        Parameters
        ----------
        harmonisation_time
            Harmonisation time

            This is the time at and before which
            the solution should be equal to `harmonised_spline_no_convergence`.

        convergence_time
            Convergence time

            This is the time at and after which
            the solution should be equal to `convergence_spline`.

        harmonised_spline_no_convergence
            Harmonised spline that does not consider convergence

        convergence_spline
            The spline to which the result should converge

        Returns
        -------
        :
            Harmonised spline
        """

__call__ #

__call__(
    harmonisation_time: Union[int, float],
    convergence_time: Union[int, float],
    harmonised_spline_no_convergence: Spline,
    convergence_spline: Spline,
) -> Spline

Generate the harmonised spline

Parameters:

Name Type Description Default
harmonisation_time Union[int, float]

Harmonisation time

This is the time at and before which the solution should be equal to harmonised_spline_no_convergence.

required
convergence_time Union[int, float]

Convergence time

This is the time at and after which the solution should be equal to convergence_spline.

required
harmonised_spline_no_convergence Spline

Harmonised spline that does not consider convergence

required
convergence_spline Spline

The spline to which the result should converge

required

Returns:

Type Description
Spline

Harmonised spline

Source code in src/gradient_aware_harmonisation/utils.py
def __call__(
    self,
    harmonisation_time: Union[int, float],
    convergence_time: Union[int, float],
    harmonised_spline_no_convergence: Spline,
    convergence_spline: Spline,
) -> Spline:
    """
    Generate the harmonised spline

    Parameters
    ----------
    harmonisation_time
        Harmonisation time

        This is the time at and before which
        the solution should be equal to `harmonised_spline_no_convergence`.

    convergence_time
        Convergence time

        This is the time at and after which
        the solution should be equal to `convergence_spline`.

    harmonised_spline_no_convergence
        Harmonised spline that does not consider convergence

    convergence_spline
        The spline to which the result should converge

    Returns
    -------
    :
        Harmonised spline
    """

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/utils.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],
            )
        ),
    )

harmonise_splines #

harmonise_splines(
    harmonisee: Spline,
    target: Spline,
    harmonisation_time: Union[int, float],
    converge_to: Spline,
    convergence_time: Union[int, float],
    get_harmonised_spline: GetHarmonisedSplineLike = get_cosine_decay_harmonised_spline,
) -> Spline

Harmonise spline

Parameters:

Name Type Description Default
harmonisee Spline

Spline that we want to harmonise

required
target Spline

Spline to which we harmonise

required
harmonisation_time Union[int, float]

Time point at which harmonisee should be matched to the target

required
converge_to Spline

The spline to which the result should converge.

If not supplied, we use `harmonisee'. i.e. we converge back to the spline we are harmonising.

required
convergence_time Union[int, float]

Time point at which the harmonised data should converge to the convergence spline

required
get_harmonised_spline GetHarmonisedSplineLike

The method to use to converge back to the convergence spline.

get_cosine_decay_harmonised_spline

Returns:

Type Description
Spline

harmonised spline

Source code in src/gradient_aware_harmonisation/utils.py
def harmonise_splines(  # noqa: PLR0913
    harmonisee: Spline,
    target: Spline,
    harmonisation_time: Union[int, float],
    converge_to: Spline,
    convergence_time: Union[int, float],
    get_harmonised_spline: GetHarmonisedSplineLike = get_cosine_decay_harmonised_spline,
) -> Spline:
    """
    Harmonise spline

    Parameters
    ----------
    harmonisee
        Spline that we want to harmonise

    target
        Spline to which we harmonise

    harmonisation_time
        Time point at which harmonisee should be matched to the target

    converge_to
        The spline to which the result should converge.

        If not supplied, we use `harmonisee'.
        i.e. we converge back to the spline we are harmonising.

    convergence_time
        Time point at which the harmonised data
        should converge to the convergence spline

    get_harmonised_spline
        The method to use to converge back to the convergence spline.

    Returns
    -------
    :
        harmonised spline
    """
    # compute derivatives of splines
    target_dspline = target.derivative()
    harmonisee_dspline = harmonisee.derivative()

    # match first-order derivatives
    diff_dspline = np.subtract(
        target_dspline(harmonisation_time), harmonisee_dspline(harmonisation_time)
    )

    harmonised_first_derivative = add_constant_to_spline(
        in_spline=harmonisee_dspline, constant=diff_dspline
    )

    # integrate to match zero-order derivative
    harmonised_spline_first_derivative_only = (
        harmonised_first_derivative.antiderivative()
    )

    # match zero-order derivatives
    diff_spline = np.subtract(
        target(harmonisation_time),
        harmonised_spline_first_derivative_only(harmonisation_time),
    )

    harmonised_spline_no_convergence = add_constant_to_spline(
        in_spline=harmonised_spline_first_derivative_only, constant=diff_spline
    )

    harmonised_spline = get_harmonised_spline(
        harmonisation_time=harmonisation_time,
        convergence_time=convergence_time,
        harmonised_spline_no_convergence=harmonised_spline_no_convergence,
        convergence_spline=converge_to,
    )

    return harmonised_spline