Repository navigation
fix: bound three-channel captures by the physical buffer #308
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
Open
YashAnand69
wants to merge
2
commits into
fossasia:main
Choose a base branch
from
YashAnand69:fix/three-channel-capture-capacity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Test oscilloscope buffer limits without connected hardware.""" | ||
|
|
||
| from unittest.mock import Mock, call | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| import pslab.protocol as CP | ||
| from pslab.connection import ConnectionHandler | ||
| from pslab.instrument.oscilloscope import Oscilloscope | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def scope(): | ||
| """Return an oscilloscope using a mock connection.""" | ||
| device = Mock(spec=ConnectionHandler) | ||
| instrument = Oscilloscope(device) | ||
| device.reset_mock() | ||
| return instrument | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "channels,capacity", [(1, 10000), (2, 5000), (3, 2500), (4, 2500)] | ||
| ) | ||
| @pytest.mark.parametrize("offset", [1, 833]) | ||
| def test_capture_rejects_samples_beyond_buffer(scope, channels, capacity, offset): | ||
| with pytest.raises(ValueError, match=f"Cannot collect more than {capacity}"): | ||
| scope.capture(channels, capacity + offset, timegap=2, block=False) | ||
|
|
||
| assert scope._device.mock_calls == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "channels,capacity", [(1, 10000), (2, 5000), (3, 2500), (4, 2500)] | ||
| ) | ||
| def test_capture_accepts_full_buffer(scope, channels, capacity): | ||
| timestamps = scope.capture(channels, capacity, timegap=2, block=False)[0] | ||
|
|
||
| np.testing.assert_array_equal(timestamps, 2 * np.arange(capacity)) | ||
| assert call(capacity) in scope._device.send_int.call_args_list | ||
| for channel in scope._channels.values(): | ||
| if channel.buffer_idx is not None: | ||
| assert channel.buffer_idx + channel.samples_in_buffer <= CP.MAX_SAMPLES | ||
|
|
||
|
|
||
| def test_three_channel_capture_uses_four_channel_command(scope, monkeypatch): | ||
| voltages = [np.zeros(2500) for _ in range(4)] | ||
| monkeypatch.setattr(scope, "progress", lambda: (True, 2500)) | ||
| monkeypatch.setattr(scope, "fetch_data", lambda: voltages) | ||
| monkeypatch.setattr("pslab.instrument.oscilloscope.time.sleep", lambda _: None) | ||
|
|
||
| result = scope.capture(3, 2500, timegap=2) | ||
|
|
||
| assert len(result) == 4 | ||
| assert all(result[i + 1] is voltages[i] for i in range(3)) | ||
| scope._device.send_byte.assert_any_call(CP.CAPTURE_FOUR) | ||
| assert scope._channels["MIC"].buffer_idx == 7500 | ||
| assert scope._channels["MIC"].samples_in_buffer == 2500 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("trigger", [None, False, 0.5]) | ||
| def test_oversized_capture_preserves_trigger_configuration(scope, trigger): | ||
| scope.configure_trigger(channel="CH2", voltage=1.1) | ||
| scope._device.reset_mock() | ||
| previous = (scope.trigger_enabled, scope.trigger_channel, scope.trigger_voltage) | ||
|
|
||
| with pytest.raises(ValueError, match="Cannot collect more than 2500"): | ||
| scope.capture(3, 2501, timegap=2, trigger=trigger, trigger_channel="CH3") | ||
|
|
||
| assert ( | ||
| scope.trigger_enabled, | ||
| scope.trigger_channel, | ||
| scope.trigger_voltage, | ||
| ) == previous | ||
| assert scope._device.mock_calls == [] | ||
|
|
||
|
|
||
| def test_oversized_named_channel_capture_preserves_channel_map(scope): | ||
| with pytest.raises(ValueError, match="Cannot collect more than 10000"): | ||
| scope.capture("VOL", 10001, timegap=2, block=False) | ||
|
|
||
| assert scope._channel_one_map == "CH1" | ||
| assert scope._device.mock_calls == [] | ||
|
|
||
|
|
||
| def test_valid_capture_can_disable_trigger_for_shorter_timegap(scope): | ||
| scope.configure_trigger(voltage=1.1) | ||
|
|
||
| timestamps = scope.capture(1, 2, timegap=0.5, trigger=False, block=False)[0] | ||
|
|
||
| assert not scope.trigger_enabled | ||
| np.testing.assert_array_equal(timestamps, [0, 0.5]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.