Repository navigation
fix: validate waveform tables before device writes #307
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
4
commits into
fossasia:main
Choose a base branch
from
YashAnand69:fix/waveform-table-length
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
4 commits
Select commit
Hold shift + click to select a range
76d9b75
fix: validate waveform tables before device writes
YashAnand69 4fe7dbb
fix: normalize real object waveform values before validation
YashAnand69 9ea2202
Reject boolean waveform tables before changing device state
YashAnand69 face349
Reject boolean elements in object waveform tables
YashAnand69 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Hardware-independent validation of waveform uploads and command framing.""" | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| import pslab.protocol as CP | ||
| from pslab.connection import ConnectionHandler | ||
| from pslab.instrument.waveform_generator import WaveformGenerator | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("loader", ["table", "function"]) | ||
| @pytest.mark.parametrize( | ||
| "points", | ||
| [ | ||
| np.zeros(0), | ||
| np.zeros(1), | ||
| np.zeros(511), | ||
| np.zeros(513), | ||
| np.zeros((512, 1)), | ||
| np.zeros((2, 256)), | ||
| np.full(512, np.nan), | ||
| np.full(512, np.inf), | ||
| np.full(512, -np.inf), | ||
| np.full(512, 1j), | ||
| np.full(512, "not-a-voltage"), | ||
| np.full(512, None, dtype=object), | ||
| np.full(512, {}, dtype=object), | ||
| np.resize(np.array([False, True]), 512), | ||
| np.full(512, True, dtype=object), | ||
| np.array([np.bool_(False)] + [0.5] * 511, dtype=object), | ||
| ], | ||
| ) | ||
| def test_invalid_table_does_not_write_or_change_channel(points, loader): | ||
| device = Mock(spec=ConnectionHandler) | ||
| generator = WaveformGenerator(device) | ||
| channel = generator._channels["SI1"] | ||
| previous_table = channel.waveform_table.copy() | ||
| with pytest.raises(ValueError): | ||
| if loader == "table": | ||
| generator.load_table("SI1", points) | ||
| else: | ||
| generator.load_function("SI1", lambda x: points, [0, 1]) | ||
| assert device.mock_calls == [] | ||
| assert channel.wavetype == "sine" | ||
| assert channel.waveform_table == previous_table | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("channel", ["SI1", "SI2"]) | ||
| @pytest.mark.parametrize("dtype", [np.float64, np.int16, object]) | ||
| def test_valid_table_preserves_upload_framing(channel, dtype): | ||
| device = Mock(spec=ConnectionHandler) | ||
| generator = WaveformGenerator(device) | ||
| points = np.linspace(-5, 5, 512).astype(dtype) | ||
| original = points.copy() | ||
| generator.load_table(channel, points) | ||
| clipped = np.clip(np.asarray(points, dtype=float), -3.3, 3.3) | ||
| expected = np.round((clipped + 3.3) / 6.6 * 511).astype(np.int16).tolist() | ||
| assert [call.args[0] for call in device.send_int.call_args_list] == expected | ||
| commands = [call.args[0] for call in device.send_byte.call_args_list] | ||
| assert commands[:2] == [ | ||
| CP.WAVEGEN, | ||
| CP.LOAD_WAVEFORM1 if channel == "SI1" else CP.LOAD_WAVEFORM2, | ||
| ] | ||
| assert ( | ||
| commands[2:] | ||
| == np.round((clipped[::16] + 3.3) / 6.6 * 63).astype(np.int16).tolist() | ||
| ) | ||
| device.get_ack.assert_called_once_with() | ||
| assert generator._channels[channel].wavetype == "custom" | ||
| np.testing.assert_array_equal(points, original) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("function", ["sine", "tria"]) | ||
| def test_builtin_functions_keep_fixed_upload_sizes(function): | ||
| device = Mock(spec=ConnectionHandler) | ||
| generator = WaveformGenerator(device) | ||
| generator.load_function("SI1", function) | ||
| assert device.send_int.call_count == 512 | ||
| assert device.send_byte.call_count == 34 | ||
| assert generator._channels["SI1"].wavetype == function |
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.