Skip to content

continuous_timeseries.discrete_to_continuous#

Conversion of timeseries from discrete to continuous

This supports the TimeseriesDiscrete and TimeseriesContinuous APIs, but is in more general where possible.

Modules:

Name Description
higher_order

Conversion of discrete to continuous data assuming higher-order interpolation

interpolation_option

Definition of a our interpolation options (represented by InterpolationOption).

linear

Conversion of discrete to continuous data assuming linear interpolation

piecewise_constant_common

Common functions used across our piecewise constant implementations

piecewise_constant_next_left_closed

Conversion of discrete to continuous data using 'next' piecewise constant steps

piecewise_constant_next_left_open

Conversion of discrete to continuous using 'next' piecewise constant steps

piecewise_constant_previous_left_closed

Conversion of discrete to continuous using 'previous' piecewise constant steps

piecewise_constant_previous_left_open

Conversion of discrete to continuous using 'previous' piecewise constant steps

Classes:

Name Description
InterpolationOption

Interpolation options

Functions:

Name Description
discrete_to_continuous

Convert a discrete timeseries to continuous

InterpolationOption #

Bases: IntEnum

Interpolation options

Attributes:

Name Type Description
Cubic

Cubic interpolation

Linear

Linear interpolation

PiecewiseConstantNextLeftClosed

Piecewise constant 'next' interpolation, each interval is closed on the left

PiecewiseConstantNextLeftOpen

Piecewise constant 'next' interpolation, each interval is open on the left

PiecewiseConstantPreviousLeftClosed

Piecewise constant 'previous' interpolation, each interval is closed on the left

PiecewiseConstantPreviousLeftOpen

Piecewise constant 'previous' interpolation, each interval is open on the left

Quadratic

Quadratic interpolation

Quartic

Quartic interpolation

Source code in src/continuous_timeseries/discrete_to_continuous/interpolation_option.py
@unique
class InterpolationOption(IntEnum):
    """
    Interpolation options
    """

    Linear = 1
    """Linear interpolation"""

    Quadratic = 2
    """Quadratic interpolation"""

    Cubic = 3
    """Cubic interpolation"""

    Quartic = 4
    """Quartic interpolation"""

    PiecewiseConstantNextLeftClosed = 10
    """
    Piecewise constant 'next' interpolation, each interval is closed on the left

    In other words,
    between t(i) and t(i + 1), the value is equal to y(i + 1).
    At t(i), the value is equal to y(i + 1).

    If helpful, we have drawn a picture of how this works below.
    Symbols:

    - time: y-value selected for this time-value
    - i: closed (i.e. inclusive) boundary
    - o: open (i.e. exclusive) boundary

    ```
    y(4):                                    ixxxxxxxxxxxxxxxxxxxxxxxxxx
    y(3):                        ixxxxxxxxxxxo
    y(2):            ixxxxxxxxxxxo
    y(1): xxxxxxxxxxxo
          -----------|-----------|-----------|-----------|--------------
                  time(1)     time(2)     time(3)     time(4)
    ```
    """

    PiecewiseConstantNextLeftOpen = 11
    """
    Piecewise constant 'next' interpolation, each interval is open on the left

    In other words,
    between t(i) and t(i + 1), the value is equal to y(i + 1).
    At t(i), the value is equal to y(i).

    If helpful, we have drawn a picture of how this works below.
    Symbols:

    - time: y-value selected for this time-value
    - i: closed (i.e. inclusive) boundary
    - o: open (i.e. exclusive) boundary

    ```
    y(4):                                    oxxxxxxxxxxxxxxxxxxxxxxxxxx
    y(3):                        oxxxxxxxxxxxi
    y(2):            oxxxxxxxxxxxi
    y(1): xxxxxxxxxxxi
          -----------|-----------|-----------|-----------|--------------
                  time(1)     time(2)     time(3)     time(4)
    ```
    """

    PiecewiseConstantPreviousLeftClosed = 12
    """
    Piecewise constant 'previous' interpolation, each interval is closed on the left

    In other words,
    between t(i) and t(i + 1), the value is equal to y(i).
    At t(i + 1), the value is equal to y(i + 1).

    If helpful, we have drawn a picture of how this works below.
    Symbols:

    - time: y-value selected for this time-value
    - i: closed (i.e. inclusive) boundary
    - o: open (i.e. exclusive) boundary

    ```
    y(4):                                                ixxxxxxxxxxxxxx
    y(3):                                    ixxxxxxxxxxxo
    y(2):                        ixxxxxxxxxxxo
    y(1): xxxxxxxxxxxxxxxxxxxxxxxo
          -----------|-----------|-----------|-----------|--------------
                  time(1)     time(2)     time(3)     time(4)
    ```
    """

    PiecewiseConstantPreviousLeftOpen = 13
    """
    Piecewise constant 'previous' interpolation, each interval is open on the left

    In other words,
    between t(i) and t(i + 1), the value is equal to y(i).
    At t(i + 1), the value is equal to y(i).

    If helpful, we have drawn a picture of how this works below.
    Symbols:

    - time: y-value selected for this time-value
    - i: closed (i.e. inclusive) boundary
    - o: open (i.e. exclusive) boundary

    ```
    y(4):                                                oxxxxxxxxxxxxxx
    y(3):                                    oxxxxxxxxxxxi
    y(2):                        oxxxxxxxxxxxi
    y(1): xxxxxxxxxxxxxxxxxxxxxxxi
          -----------|-----------|-----------|-----------|--------------
                  time(1)     time(2)     time(3)     time(4)
    ```
    """

Cubic class-attribute instance-attribute #

Cubic = 3

Cubic interpolation

Linear class-attribute instance-attribute #

Linear = 1

Linear interpolation

PiecewiseConstantNextLeftClosed class-attribute instance-attribute #

PiecewiseConstantNextLeftClosed = 10

Piecewise constant 'next' interpolation, each interval is closed on the left

In other words, between t(i) and t(i + 1), the value is equal to y(i + 1). At t(i), the value is equal to y(i + 1).

If helpful, we have drawn a picture of how this works below. Symbols:

  • time: y-value selected for this time-value
  • i: closed (i.e. inclusive) boundary
  • o: open (i.e. exclusive) boundary
y(4):                                    ixxxxxxxxxxxxxxxxxxxxxxxxxx
y(3):                        ixxxxxxxxxxxo
y(2):            ixxxxxxxxxxxo
y(1): xxxxxxxxxxxo
      -----------|-----------|-----------|-----------|--------------
              time(1)     time(2)     time(3)     time(4)

PiecewiseConstantNextLeftOpen class-attribute instance-attribute #

PiecewiseConstantNextLeftOpen = 11

Piecewise constant 'next' interpolation, each interval is open on the left

In other words, between t(i) and t(i + 1), the value is equal to y(i + 1). At t(i), the value is equal to y(i).

If helpful, we have drawn a picture of how this works below. Symbols:

  • time: y-value selected for this time-value
  • i: closed (i.e. inclusive) boundary
  • o: open (i.e. exclusive) boundary
y(4):                                    oxxxxxxxxxxxxxxxxxxxxxxxxxx
y(3):                        oxxxxxxxxxxxi
y(2):            oxxxxxxxxxxxi
y(1): xxxxxxxxxxxi
      -----------|-----------|-----------|-----------|--------------
              time(1)     time(2)     time(3)     time(4)

PiecewiseConstantPreviousLeftClosed class-attribute instance-attribute #

PiecewiseConstantPreviousLeftClosed = 12

Piecewise constant 'previous' interpolation, each interval is closed on the left

In other words, between t(i) and t(i + 1), the value is equal to y(i). At t(i + 1), the value is equal to y(i + 1).

If helpful, we have drawn a picture of how this works below. Symbols:

  • time: y-value selected for this time-value
  • i: closed (i.e. inclusive) boundary
  • o: open (i.e. exclusive) boundary
y(4):                                                ixxxxxxxxxxxxxx
y(3):                                    ixxxxxxxxxxxo
y(2):                        ixxxxxxxxxxxo
y(1): xxxxxxxxxxxxxxxxxxxxxxxo
      -----------|-----------|-----------|-----------|--------------
              time(1)     time(2)     time(3)     time(4)

PiecewiseConstantPreviousLeftOpen class-attribute instance-attribute #

PiecewiseConstantPreviousLeftOpen = 13

Piecewise constant 'previous' interpolation, each interval is open on the left

In other words, between t(i) and t(i + 1), the value is equal to y(i). At t(i + 1), the value is equal to y(i).

If helpful, we have drawn a picture of how this works below. Symbols:

  • time: y-value selected for this time-value
  • i: closed (i.e. inclusive) boundary
  • o: open (i.e. exclusive) boundary
y(4):                                                oxxxxxxxxxxxxxx
y(3):                                    oxxxxxxxxxxxi
y(2):                        oxxxxxxxxxxxi
y(1): xxxxxxxxxxxxxxxxxxxxxxxi
      -----------|-----------|-----------|-----------|--------------
              time(1)     time(2)     time(3)     time(4)

Quadratic class-attribute instance-attribute #

Quadratic = 2

Quadratic interpolation

Quartic class-attribute instance-attribute #

Quartic = 4

Quartic interpolation

discrete_to_continuous #

discrete_to_continuous(
    x: PINT_NUMPY_ARRAY,
    y: PINT_NUMPY_ARRAY,
    interpolation: InterpolationOption,
    name: str,
) -> TimeseriesContinuous

Convert a discrete timeseries to continuous

Parameters:

Name Type Description Default
x PINT_NUMPY_ARRAY

The discrete x-values from which to convert

required
y PINT_NUMPY_ARRAY

The discrete y-values from which to convert

required
interpolation InterpolationOption

Interpolation type to use for converting from discrete to continuous.

required
name str

The value to use to set the result's name attribute

required

Returns:

Type Description
TimeseriesContinuous

Continuous version of discrete based on interpolation.

Source code in src/continuous_timeseries/discrete_to_continuous/__init__.py
def discrete_to_continuous(  # noqa: PLR0911
    x: PINT_NUMPY_ARRAY,
    y: PINT_NUMPY_ARRAY,
    interpolation: InterpolationOption,
    name: str,
) -> TimeseriesContinuous:
    """
    Convert a discrete timeseries to continuous

    Parameters
    ----------
    x
        The discrete x-values from which to convert

    y
        The discrete y-values from which to convert

    interpolation
        Interpolation type to use for converting from discrete to continuous.

    name
        The value to use to set the result's name attribute

    Returns
    -------
    :
        Continuous version of `discrete` based on `interpolation`.
    """
    if interpolation == InterpolationOption.PiecewiseConstantNextLeftClosed:
        return discrete_to_continuous_piecewise_constant_next_left_closed(
            x=x,
            y=y,
            name=name,
        )

    if interpolation == InterpolationOption.PiecewiseConstantNextLeftOpen:
        return discrete_to_continuous_piecewise_constant_next_left_open(
            x=x,
            y=y,
            name=name,
        )

    if interpolation == InterpolationOption.PiecewiseConstantPreviousLeftClosed:
        return discrete_to_continuous_piecewise_constant_previous_left_closed(
            x=x,
            y=y,
            name=name,
        )

    if interpolation == InterpolationOption.PiecewiseConstantPreviousLeftOpen:
        return discrete_to_continuous_piecewise_constant_previous_left_open(
            x=x,
            y=y,
            name=name,
        )

    if interpolation == InterpolationOption.Linear:
        return discrete_to_continuous_linear(
            x=x,
            y=y,
            name=name,
        )

    if interpolation == InterpolationOption.Quadratic:
        return discrete_to_continuous_higher_order(x=x, y=y, name=name, order=2)

    if interpolation == InterpolationOption.Cubic:
        return discrete_to_continuous_higher_order(x=x, y=y, name=name, order=3)

    if interpolation == InterpolationOption.Quartic:
        return discrete_to_continuous_higher_order(x=x, y=y, name=name, order=4)

    raise NotImplementedError(interpolation.name)  # pragma: no cover