- 
                Notifications
    You must be signed in to change notification settings 
- Fork 20
Fixes on copy behavior in ring buffer window method #638
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
Changes from all commits
0ef746f
              54101c8
              a4a59c4
              c584d25
              4d62bca
              61ddef1
              1970178
              4d8de2a
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -66,6 +66,18 @@ def init_moving_window( | |
| return window, lm_tx | ||
|  | ||
|  | ||
| def dt(i: int) -> datetime: # pylint: disable=invalid-name | ||
| """Create datetime objects from indices. | ||
|  | ||
| Args: | ||
| i: Index to create datetime from. | ||
|  | ||
| Returns: | ||
| Datetime object. | ||
| """ | ||
| return datetime.fromtimestamp(i, tz=timezone.utc) | ||
|  | ||
|  | ||
| async def test_access_window_by_index() -> None: | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if that's the correct function to add the tests. This was supposed to access the window by integer index, but the tests are accessing by datetime object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newly added datetime slice tests were added in  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I missed that. All good then ;). | ||
| """Test indexing a window by integer index.""" | ||
| window, sender = init_moving_window(timedelta(seconds=1)) | ||
|  | @@ -92,7 +104,8 @@ async def test_access_window_by_int_slice() -> None: | |
| async with window: | ||
| await push_logical_meter_data(sender, range(0, 5)) | ||
| assert np.array_equal(window[3:5], np.array([3.0, 4.0])) | ||
|  | ||
| with pytest.raises(IndexError): | ||
| window.window(3, 5) # type: ignore | ||
| data = [1, 2, 2.5, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1] | ||
| await push_logical_meter_data(sender, data) | ||
| assert np.array_equal(window[5:14], np.array(data[5:14])) | ||
|  | @@ -106,6 +119,15 @@ async def test_access_window_by_ts_slice() -> None: | |
| time_start = UNIX_EPOCH + timedelta(seconds=3) | ||
| time_end = time_start + timedelta(seconds=2) | ||
| assert np.array_equal(window[time_start:time_end], np.array([3.0, 4.0])) # type: ignore | ||
| assert np.array_equal(window.window(dt(3), dt(5)), np.array([3.0, 4.0])) | ||
| assert np.array_equal(window.window(dt(3), dt(3)), np.array([])) | ||
| # Window only supports slicing with ascending indices within allowed range | ||
| with pytest.raises(IndexError): | ||
| window.window(dt(3), dt(1)) | ||
| with pytest.raises(IndexError): | ||
| window.window(dt(3), dt(6)) | ||
| with pytest.raises(IndexError): | ||
| window.window(dt(-1), dt(5)) | ||
|  | ||
|  | ||
| async def test_access_empty_window() -> None: | ||
|  | ||
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.
Does this have to be a static method? I'd suggest to make it a class method as long it's not used outside the class context.
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.
Why do you think so, or what is the advantage of a classmethod? I made it static since it's an isolated algorithmic piece which does not need any internal state of the class. Only the buffer argument would change here.
The main motivation was testing purpose (actually contained multiple bugs before), i.e. separate wrapping algorithm from index logic (which will become more tricky in follow-ups). But it can also easily be stripped out when needed elsewhere.
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.
One example for outside usage I could imagine is the periodic feature extractor, which I assume is using similar code.
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.
You are right. It doesn't depend on the internal state. Let's keep it as is.