Skip to content

Conversation

@cwasicki
Copy link
Collaborator

@cwasicki cwasicki commented Sep 13, 2023

To support int indices and slice index behavior in the moving window and the ring buffer, the following changes are made:

  • Support for int indices in addition to datetime indices is added to moving window and ring buffer.
  • Indices behave like usual slices (gracefully accept start >= end or index out of range).
  • A method to count the number of possible elements in the range between oldest and newest timestamp is added to ring buffer and the moving window.
  • get_timestamp is added to convert an int index passed by the user to the corresponding timestamp.

@cwasicki cwasicki self-assigned this Sep 13, 2023
@github-actions github-actions bot added part:tests Affects the unit, integration and performance (benchmarks) tests part:data-pipeline Affects the data pipeline labels Sep 13, 2023
@cwasicki cwasicki added this to the v1.0.0-rc1 milestone Sep 13, 2023
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some early feedback.

@llucax llucax modified the milestones: v1.0.0-rc1, v1.0.0-rc2 Sep 14, 2023
@github-actions github-actions bot added the part:docs Affects the documentation label Sep 20, 2023
@cwasicki cwasicki changed the title WIP: Support int indices and slice index behavior in ring buffer and moving window Support int indices and slice index behavior in ring buffer and moving window Sep 20, 2023
@cwasicki cwasicki marked this pull request as ready for review September 20, 2023 23:27
@cwasicki cwasicki requested a review from a team as a code owner September 20, 2023 23:27
@cwasicki cwasicki requested a review from llucax September 20, 2023 23:27
end = self.index_to_datetime(end)

# Here we should have both as datetime
if not (isinstance(start, datetime) and isinstance(end, datetime)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if end is not a datetime but start is? Does it really works in that case or should this be

Suggested change
if not (isinstance(start, datetime) and isinstance(end, datetime)):
if not (isinstance(start, datetime) or not isinstance(end, datetime)):

Instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current case it only works if both are same type, if not, this one would throw. The idea was that either both are integer or both are datetime. Technically we could also support a mix of both, I was not sure whether we should. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would support the mix, unless there is a very strong case for it. My point above is, unless I'm understanding the code wrongly, the if above will not raise an error (at least not explicitly) if end is not a datetime but start is, right?

You have:

        # If both are indices or None convert to datetime
        if not isinstance(start, datetime) and not isinstance(end, datetime):
            start, end = self._to_covered_indices(start, end)
            start = self.get_timestamp(start)
            end = self.get_timestamp(end)

        # Here we should have both as datetime
        if not (isinstance(start, datetime) and isinstance(end, datetime)):
            raise IndexError(...)

Here you continue operating as if both start and end were datetimes,
So you should probably raise if any if start or end is not a datetime above.

Copy link
Contributor

@llucax llucax Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I see I misread the parenthesis 🤦 (I was reading if not isinstance(start, datetime) and isinstance(end, datetime): (without the grouping of both and conditions to negate the result)

So scratch all the above and take this suggestion just as a code readability improvement 😆

Suggested change
if not (isinstance(start, datetime) and isinstance(end, datetime)):
if not isinstance(start, datetime) or not isinstance(end, datetime):

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated according to your suggestion.

I would support the mix, unless there is a very strong case for it.

Could you clarify on this please, would you support a mix of datetime and int indices (e.g. moving_window[datetime(2023, 9, 25): -5]) or not? The current version does not, and honestly I find it confusing to use. We can still support it later if we find good use-cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was a typo, I meant "I would not support" 😞

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's what's currently implemented so this PR is ready. @llucax

@cwasicki
Copy link
Collaborator Author

@llucax Updated

llucax
llucax previously approved these changes Sep 22, 2023
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So only one conversation left, and I was reading the code wrongly. As proven 😆 , I find the version without the extra parenthesis harder to read, but I guess every brain works differently, so completely optional.

Copy link
Contributor

@matthias-wende-frequenz matthias-wende-frequenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very nice!

@shsms
Copy link
Contributor

shsms commented Sep 22, 2023

Reminder to remove "Still WIP" from description before merging

@cwasicki
Copy link
Collaborator Author

@llucax Any hint why the CI keeps failing?

@llucax
Copy link
Contributor

llucax commented Sep 26, 2023

I got it in other PRs too, I guess it is a change in pylint, but it is pinned, so it is very strange.

@llucax
Copy link
Contributor

llucax commented Sep 26, 2023

This method maps an index passed by the user to its corresponding
timestamp of the ring buffer.

Signed-off-by: cwasicki <[email protected]>
This returns the count of samples that are covered between the oldest and newest
valid samples.

Signed-off-by: cwasicki <[email protected]>
Using integer-based indices allows for selecting positions within the
window without requiring knowledge of the specific timestamps.

Signed-off-by: cwasicki <[email protected]>
The `__getitem__` method in moving window was not working properly. Since its
functionality is already provided by the window method it one can be
called from the getitem magic.

Signed-off-by: cwasicki <[email protected]>
Signed-off-by: cwasicki <[email protected]>
@cwasicki cwasicki added this pull request to the merge queue Sep 27, 2023
Merged via the queue into frequenz-floss:v0.x.x with commit bd7822c Sep 27, 2023
@cwasicki cwasicki deleted the rbuffer-index branch September 27, 2023 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

Development

Successfully merging this pull request may close these issues.

4 participants