Skip to content

Commit b71335f

Browse files
authored
Rename datetime_to_index to to_internal_index (#686)
The method is renamed to avoid confusion between indices in a window, which are passed by the user, and the internal indices that point to the actual position in the internal buffer.
2 parents a6f7964 + 69ccf70 commit b71335f

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
## New Features
1212

1313
- A tutorial section and a getting started tutorial
14-
15-
- Remove `__setitem__` method from `OrderedRingBuffer` to enforce usage of the dedicated `update` method only.
14+
- In `OrderedRingBuffer`:
15+
- Rename `datetime_to_index` to `to_internal_index` to avoid confusion between the internal index and the external index.
16+
- Remove `__setitem__` method to enforce usage of dedicated `update` method only.
1617

1718
## Bug Fixes
1819

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def __getitem__(self, key: SupportsIndex | datetime | slice) -> float | ArrayLik
384384
return self._buffer[key]
385385
elif isinstance(key, datetime):
386386
_logger.debug("Returning value at time %s ", key)
387-
return self._buffer[self._buffer.datetime_to_index(key)]
387+
return self._buffer[self._buffer.to_internal_index(key)]
388388
elif isinstance(key, SupportsIndex):
389389
_logger.debug("Returning value at index %s ", key)
390390
return self._buffer[key]

src/frequenz/sdk/timeseries/_periodic_feature_extractor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def _get_buffer_bounds(
350350

351351
# add the offset to the oldest sample in the ringbuffer and wrap around
352352
# to get the start and end positions in the ringbuffer
353-
rb_offset = self._buffer.datetime_to_index(self._buffer.time_bound_oldest)
353+
rb_offset = self._buffer.to_internal_index(self._buffer.time_bound_oldest)
354354
start_pos = self._buffer.wrap(end_pos + self._period + rb_offset)
355355
end_pos = self._buffer.wrap(end_pos + rb_offset)
356356

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def update(self, sample: Sample[QuantityT]) -> None:
168168
value = sample.value.base_value
169169
else:
170170
value = np.nan
171-
self._buffer[self.datetime_to_index(timestamp)] = value
171+
self._buffer[self.to_internal_index(timestamp)] = value
172172

173173
self._update_gaps(timestamp, prev_newest, not self.has_value(sample))
174174

@@ -221,10 +221,12 @@ def newest_timestamp(self) -> datetime | None:
221221

222222
return self.time_bound_newest
223223

224-
def datetime_to_index(
224+
def to_internal_index(
225225
self, timestamp: datetime, allow_outside_range: bool = False
226226
) -> int:
227-
"""Convert the given timestamp to an index.
227+
"""Convert the given timestamp to the position in the buffer.
228+
229+
!!! Note: This method is meant for advanced use cases and should not generally be used.
228230
229231
Args:
230232
timestamp: Timestamp to convert.
@@ -258,10 +260,10 @@ def datetime_to_index(
258260
def window(
259261
self, start: datetime, end: datetime, *, force_copy: bool = True
260262
) -> FloatArray:
261-
"""Request a view on the data between start timestamp and end timestamp.
263+
"""Request a copy or view on the data between start timestamp and end timestamp.
262264
263-
If the data is not used immediately it could be overwritten.
264265
Always request a copy if you keep the data around for longer.
266+
Otherwise, if the data is not used immediately it could be overwritten.
265267
266268
Will return a copy in the following cases:
267269
* The requested time period is crossing the start/end of the buffer.
@@ -294,12 +296,10 @@ def window(
294296
if start == end:
295297
return np.array([]) if isinstance(self._buffer, np.ndarray) else []
296298

297-
start_index = self.datetime_to_index(start)
298-
end_index = self.datetime_to_index(end)
299+
start_pos = self.to_internal_index(start)
300+
end_pos = self.to_internal_index(end)
299301

300-
return self._wrapped_buffer_window(
301-
self._buffer, start_index, end_index, force_copy
302-
)
302+
return self._wrapped_buffer_window(self._buffer, start_pos, end_pos, force_copy)
303303

304304
@staticmethod
305305
def _wrapped_buffer_window(
@@ -562,10 +562,10 @@ def count_valid(self) -> int:
562562
),
563563
)
564564

565-
start_index = self.datetime_to_index(self._datetime_oldest)
566-
end_index = self.datetime_to_index(self._datetime_newest)
565+
start_pos = self.to_internal_index(self._datetime_oldest)
566+
end_pos = self.to_internal_index(self._datetime_newest)
567567

568-
if end_index < start_index:
569-
return len(self._buffer) - start_index + end_index + 1 - sum_missing_entries
568+
if end_pos < start_pos:
569+
return len(self._buffer) - start_pos + end_pos + 1 - sum_missing_entries
570570

571-
return end_index + 1 - start_index - sum_missing_entries
571+
return end_pos + 1 - start_pos - sum_missing_entries

tests/timeseries/test_ringbuffer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def test_timestamp_ringbuffer_missing_parameter(
182182
) == datetime(2, 2, 2, 0, 10, tzinfo=timezone.utc)
183183
buffer.update(Sample(datetime(2, 2, 2, 0, 7, 31, tzinfo=timezone.utc), Quantity(0)))
184184

185-
assert buffer.datetime_to_index(
185+
assert buffer.to_internal_index(
186186
datetime(2, 2, 2, 0, 7, 31, tzinfo=timezone.utc)
187-
) == buffer.datetime_to_index(datetime(2, 2, 2, 0, 10, tzinfo=timezone.utc))
187+
) == buffer.to_internal_index(datetime(2, 2, 2, 0, 10, tzinfo=timezone.utc))
188188
assert len(buffer.gaps) == 2
189189

190190
# import pdb; pdb.set_trace()

0 commit comments

Comments
 (0)