Skip to content

Commit 55de0b0

Browse files
authored
Rename alignment arguments to align_to and make them default to UNIX_EPOCH (#349)
This is to make it consistent with the resampler. Fixes #343.
2 parents d00d999 + 3c1d196 commit 55de0b0

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
Note that the parameter `sampling_period` has been renamed to `input_sampling_period` to better distinguish it from the sampling period parameter in the `resampler_config`.
5454

55+
* Rename the constructor argument `window_alignment` to `align_to` and change the default to `UNIX_EPOCH`. This is to make it more consistent with the `ResamplerConfig`.
56+
5557
* `Resampler`
5658

5759
* The `ResamplerConfig` now takes the resampling period as a `timedelta`. The configuration was renamed from `resampling_period_s` to `resampling_period` accordingly.

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import logging
1010
import math
1111
from collections.abc import Sequence
12-
from datetime import datetime, timedelta, timezone
12+
from datetime import datetime, timedelta
1313
from typing import SupportsIndex, overload
1414

1515
import numpy as np
@@ -18,6 +18,7 @@
1818

1919
from .._internal.asyncio import cancel_and_await
2020
from . import Sample
21+
from ._base_types import UNIX_EPOCH
2122
from ._resampling import Resampler, ResamplerConfig
2223
from ._ringbuffer import OrderedRingBuffer
2324

@@ -39,9 +40,10 @@ class MovingWindow:
3940
a fixed defined point in time. Since the moving nature of the window, the
4041
date of the first and the last element are constantly changing and therefore
4142
the point in time that defines the alignment can be outside of the time window.
42-
Modulo arithmetic is used to move the `window_alignment` timestamp into the
43-
latest window.
44-
If for example the `window_alignment` parameter is set to
43+
Modulo arithmetic is used to move the `align_to` timestamp into the latest
44+
window.
45+
46+
If for example the `align_to` parameter is set to
4547
`datetime(1, 1, 1, tzinfo=timezone.utc)` and the window size is bigger than
4648
one day then the first element will always be aligned to the midnight.
4749
For further information see also the
@@ -106,7 +108,7 @@ def __init__( # pylint: disable=too-many-arguments
106108
resampled_data_recv: Receiver[Sample],
107109
input_sampling_period: timedelta,
108110
resampler_config: ResamplerConfig | None = None,
109-
window_alignment: datetime = datetime(1, 1, 1, tzinfo=timezone.utc),
111+
align_to: datetime = UNIX_EPOCH,
110112
) -> None:
111113
"""
112114
Initialize the MovingWindow.
@@ -121,10 +123,9 @@ def __init__( # pylint: disable=too-many-arguments
121123
given sampling period.
122124
input_sampling_period: The time interval between consecutive input samples.
123125
resampler_config: The resampler configuration in case resampling is required.
124-
window_alignment: A datetime object that defines a point in time to which
125-
the window is aligned to modulo window size.
126-
(default is 0001-01-01T00:00:00+00:00)
127-
For further information, consult the class level documentation.
126+
align_to: A datetime object that defines a point in time to which
127+
the window is aligned to modulo window size. For further
128+
information, consult the class level documentation.
128129
129130
Raises:
130131
asyncio.CancelledError: when the task gets cancelled.
@@ -156,7 +157,7 @@ def __init__( # pylint: disable=too-many-arguments
156157
self._buffer = OrderedRingBuffer(
157158
np.empty(shape=num_samples, dtype=float),
158159
sampling_period=sampling,
159-
time_index_alignment=window_alignment,
160+
align_to=align_to,
160161
)
161162

162163
if self._resampler:

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import numpy.typing as npt
1616

1717
from .. import Sample
18+
from .._base_types import UNIX_EPOCH
1819

1920
FloatArray = TypeVar("FloatArray", List[float], npt.NDArray[np.float64])
2021

@@ -53,19 +54,19 @@ def __init__(
5354
self,
5455
buffer: FloatArray,
5556
sampling_period: timedelta,
56-
time_index_alignment: datetime = datetime(1, 1, 1, tzinfo=timezone.utc),
57+
align_to: datetime = UNIX_EPOCH,
5758
) -> None:
5859
"""Initialize the time aware ringbuffer.
5960
6061
Args:
6162
buffer: Instance of a buffer container to use internally.
6263
sampling_period: Timedelta of the desired sampling period.
63-
time_index_alignment: Arbitrary point in time used to align
64+
align_to: Arbitrary point in time used to align
6465
timestamped data with the index position in the buffer.
6566
Used to make the data stored in the buffer align with the
6667
beginning and end of the buffer borders.
6768
68-
For example, if the `time_index_alignment` is set to
69+
For example, if the `align_to` is set to
6970
"0001-01-01 12:00:00", and the `sampling_period` is set to
7071
1 hour and the length of the buffer is 24, then the data
7172
stored in the buffer could correspond to the time range from
@@ -76,7 +77,7 @@ def __init__(
7677

7778
self._buffer: FloatArray = buffer
7879
self._sampling_period: timedelta = sampling_period
79-
self._time_index_alignment: datetime = time_index_alignment
80+
self._time_index_alignment: datetime = align_to
8081

8182
self._gaps: list[Gap] = []
8283
self._datetime_newest: datetime = self._DATETIME_MIN
@@ -200,7 +201,7 @@ def window(
200201
* The force_copy parameter was set to True (default False).
201202
202203
The first case can be avoided by using the appropriate
203-
time_index_alignment value in the constructor so that the data lines up
204+
`align_to` value in the constructor so that the data lines up
204205
with the start/end of the buffer.
205206
206207
This means, if the caller needs to modify the data to account for

tests/timeseries/test_ringbuffer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def test_len_ringbuffer_samples_fit_buffer_size() -> None:
266266
buffer = OrderedRingBuffer(
267267
np.empty(shape=len(test_samples), dtype=float),
268268
sampling_period=timedelta(seconds=1),
269-
time_index_alignment=datetime(1, 1, 1, tzinfo=timezone.utc),
269+
align_to=datetime(1, 1, 1, tzinfo=timezone.utc),
270270
)
271271

272272
start_ts: datetime = datetime(2023, 1, 1, tzinfo=timezone.utc)
@@ -294,7 +294,7 @@ def test_len_ringbuffer_samples_overwrite_buffer() -> None:
294294
buffer = OrderedRingBuffer(
295295
np.empty(shape=half_buffer_size, dtype=float),
296296
sampling_period=timedelta(seconds=1),
297-
time_index_alignment=datetime(1, 1, 1, tzinfo=timezone.utc),
297+
align_to=datetime(1, 1, 1, tzinfo=timezone.utc),
298298
)
299299

300300
start_ts: datetime = datetime(2023, 1, 1, tzinfo=timezone.utc)
@@ -316,10 +316,10 @@ def test_ringbuffer_empty_buffer() -> None:
316316
OrderedRingBuffer(
317317
empty_np_buffer,
318318
sampling_period=timedelta(seconds=1),
319-
time_index_alignment=datetime(1, 1, 1),
319+
align_to=datetime(1, 1, 1),
320320
)
321321
OrderedRingBuffer(
322322
empty_list_buffer,
323323
sampling_period=timedelta(seconds=1),
324-
time_index_alignment=datetime(1, 1, 1),
324+
align_to=datetime(1, 1, 1),
325325
)

0 commit comments

Comments
 (0)