-
Couldn't load subscription status.
- Fork 20
Support int indices and slice index behavior in ring buffer and moving window #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some early feedback.
7c17894 to
03174d3
Compare
03174d3 to
374b101
Compare
| end = self.index_to_datetime(end) | ||
|
|
||
| # Here we should have both as datetime | ||
| if not (isinstance(start, datetime) and isinstance(end, datetime)): |
There was a problem hiding this comment.
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
| if not (isinstance(start, datetime) and isinstance(end, datetime)): | |
| if not (isinstance(start, datetime) or not isinstance(end, datetime)): |
Instead?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😆
| if not (isinstance(start, datetime) and isinstance(end, datetime)): | |
| if not isinstance(start, datetime) or not isinstance(end, datetime): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" 😞
There was a problem hiding this comment.
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
374b101 to
aca714b
Compare
|
@llucax Updated |
There was a problem hiding this 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.
There was a problem hiding this 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!
|
Reminder to remove "Still WIP" from description before merging |
65e0e90
aca714b to
65e0e90
Compare
|
@llucax Any hint why the CI keeps failing? |
|
I got it in other PRs too, I guess it is a change in |
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]>
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]>
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]>
65e0e90 to
7b5f967
Compare
To support int indices and slice index behavior in the moving window and the ring buffer, the following changes are made:
get_timestampis added to convert an int index passed by the user to the corresponding timestamp.