-
Notifications
You must be signed in to change notification settings - Fork 6
Steps tendency #184
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
Merged
Merged
Steps tendency #184
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f7bcc14
create steps tendency
SBlokhuizen 6f6eafa
improve docs
SBlokhuizen bba2e43
cleanup
SBlokhuizen c823004
Merge remote-tracking branch 'origin/develop' into feature/steps-tend…
SBlokhuizen 5f5a3bf
create base class for piecewise/steps tendency
SBlokhuizen 1859904
Merge remote-tracking branch 'origin/develop' into feature/steps-tend…
SBlokhuizen 7696395
remove name
SBlokhuizen 7b33cbb
clarify docs
SBlokhuizen 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Empty file.
2 changes: 1 addition & 1 deletion
2
tests/tendencies/test_piecewise.py → tests/tendencies/points/test_piecewise.py
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,104 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from waveform_editor.tendencies.points.points_base import PointsBaseTendency | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def patch_points_base_tendency(): | ||
| arr = np.array([0.0]) | ||
| with ( | ||
| patch.object(PointsBaseTendency, "get_value", return_value=(arr, arr)), | ||
| patch.object(PointsBaseTendency, "get_derivative", return_value=arr), | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| def test_empty(): | ||
| """Test empty tendency.""" | ||
| tendency = PointsBaseTendency() | ||
| assert tendency.annotations | ||
|
|
||
| tendency = PointsBaseTendency(user_time=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
| tendency = PointsBaseTendency(user_value=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_filled(): | ||
| """Test value of filled tendency.""" | ||
| tendency = PointsBaseTendency(user_time=[1, 2, 3], user_value=[2, 4, 6]) | ||
| assert np.all(tendency.time == np.array([1, 2, 3])) | ||
| assert np.all(tendency.value == np.array([2, 4, 6])) | ||
| assert tendency.value_type is float | ||
| assert tendency.start == 1 | ||
| assert tendency.end == 3 | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_filled_invalid(): | ||
| """Test value of filled tendency with invalid parameters.""" | ||
| # Not monotonically increasing | ||
| tendency = PointsBaseTendency( | ||
| user_time=np.array([3, 2, 1]), user_value=np.array([1, 2, 3]) | ||
| ) | ||
| assert tendency.annotations | ||
|
|
||
| # Mismatched lengths | ||
| tendency = PointsBaseTendency( | ||
| user_time=np.array([1, 2]), user_value=np.array([1, 2, 3]) | ||
| ) | ||
| assert tendency.annotations | ||
|
|
||
| # Not strictly increasing (repeated time point) | ||
| tendency = PointsBaseTendency( | ||
| user_time=np.array([1, 1, 2]), user_value=np.array([1, 2, 3]) | ||
| ) | ||
| assert tendency.annotations | ||
|
|
||
| # Empty arrays | ||
| tendency = PointsBaseTendency(user_time=[], user_value=[]) | ||
| assert tendency.annotations | ||
|
|
||
| # Non-finite value | ||
| tendency = PointsBaseTendency(user_time=[1, 2, 3], user_value=[1, float("inf"), 3]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_start_duration_end_not_allowed(): | ||
| """Test that `start`, `duration`, and `end` may not be provided; they are always | ||
| derived from `time[0]`/`time[-1]`.""" | ||
| tendency = PointsBaseTendency( | ||
| user_time=[1, 2, 3], user_value=[1, 2, 3], user_start=1 | ||
| ) | ||
| assert tendency.annotations | ||
|
|
||
| tendency = PointsBaseTendency( | ||
| user_time=[1, 2, 3], user_value=[1, 2, 3], user_duration=2 | ||
| ) | ||
| assert tendency.annotations | ||
|
|
||
| tendency = PointsBaseTendency(user_time=[1, 2, 3], user_value=[1, 2, 3], user_end=3) | ||
| assert tendency.annotations | ||
|
|
||
| tendency = PointsBaseTendency( | ||
| user_time=[1, 2, 3], | ||
| user_value=[1, 2, 3], | ||
| user_start=1, | ||
| user_duration=2, | ||
| user_end=3, | ||
| ) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_single_point(): | ||
| """Test that a single time/value point is allowed (a zero-duration tendency).""" | ||
| tendency = PointsBaseTendency(user_time=[1.1], user_value=[9.9]) | ||
| assert tendency.time == np.array([1.1]) | ||
| assert tendency.value == np.array([9.9]) | ||
| assert tendency.start == 1.1 | ||
| assert tendency.end == 1.1 | ||
| assert not tendency.annotations |
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,164 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from waveform_editor.tendencies.points.steps import StepsTendency | ||
|
|
||
|
|
||
| def test_empty(): | ||
| """Test empty tendency.""" | ||
| tendency = StepsTendency() | ||
| assert tendency.annotations | ||
|
|
||
| tendency = StepsTendency(user_time=[0, 2, 4]) | ||
| assert tendency.annotations | ||
|
|
||
| tendency = StepsTendency(user_value=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_filled(): | ||
| """Test value of a filled tendency.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 3, 5, 5]) | ||
| assert np.all(tendency.time == np.array([0, 2, 4, 6])) | ||
| assert list(tendency.value) == [1, 3, 5, 5] | ||
| assert tendency.value_type is int | ||
| assert tendency.start == 0 | ||
| assert tendency.end == 6 | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_string_values(): | ||
| """Test a tendency with string values.""" | ||
| tendency = StepsTendency( | ||
| user_time=[0, 10, 20, 30], user_value=["ohmic", "nbi", "ec", "ec"] | ||
| ) | ||
| assert list(tendency.value) == ["ohmic", "nbi", "ec", "ec"] | ||
| assert tendency.value_type is str | ||
| assert not tendency.annotations | ||
|
|
||
| _, values = tendency.get_value(np.array([0, 5, 10, 15, 20, 25, 30])) | ||
| assert list(values) == ["ohmic", "ohmic", "nbi", "nbi", "ec", "ec", "ec"] | ||
|
|
||
|
|
||
| def test_int_float_mixing(): | ||
| """Test that mixing int and float values in a single steps tendency""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 2.5, 3, 3]) | ||
| assert tendency.value_type is float | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_mixed_value_types_not_supported(): | ||
| """Test that mixing string and numerical values is not allowed.""" | ||
| tendency = StepsTendency(user_time=[0, 10], user_value=[1, "ec"]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_unsupported_value_type(): | ||
| """Test that a value which is not an int, float, or str is not supported.""" | ||
| tendency = StepsTendency(user_time=[0, 10], user_value=[1, [2, 3]]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_duration_not_allowed(): | ||
| """Test that `duration` may not be provided; `end` is always `time[-1]`.""" | ||
| tendency = StepsTendency(user_time=[0, 10], user_value=[1, 2], user_duration=20) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_end_not_allowed(): | ||
| """Test that `end` may not be provided; it is always `time[-1]`.""" | ||
| tendency = StepsTendency(user_time=[0, 10], user_value=[1, 2], user_end=20) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_start_not_allowed(): | ||
| """Test that `start` may not be provided; it is always `time[0]`.""" | ||
| tendency = StepsTendency(user_time=[0, 10], user_value=[1, 2], user_start=5) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_mismatched_lengths(): | ||
| """Test that time and value arrays must have the same length.""" | ||
| tendency = StepsTendency(user_time=[0, 10, 20], user_value=[1, 2]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_empty_arrays(): | ||
| """Test that time and value arrays must contain at least one element.""" | ||
| tendency = StepsTendency(user_time=[], user_value=[]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_non_monotonic_time(): | ||
| """Test that the time array must be monotonically increasing.""" | ||
| tendency = StepsTendency(user_time=[0, 10, 5], user_value=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
| tendency = StepsTendency(user_time=[0, 10, 10], user_value=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_start_and_end_values(): | ||
| """Test the start and end values and their derivatives.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 3, 5, 5]) | ||
| assert tendency.start_value == 1 | ||
| assert tendency.end_value == 5 | ||
| assert tendency.start_derivative == 0 | ||
| assert tendency.end_derivative == 0 | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_generate(): | ||
| """Check the generated values.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 3, 5, 5]) | ||
| time, values = tendency.get_value() | ||
| assert np.all(time == [0, 2, 2, 4, 4, 6, 6]) | ||
| assert list(values) == [1, 1, 3, 3, 5, 5, 5] | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_last_value(): | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 3, 5, 7]) | ||
| assert not tendency.annotations | ||
| assert tendency.end_value == 7 | ||
|
|
||
| time, values = tendency.get_value() | ||
| assert np.all(time == [0, 2, 2, 4, 4, 6, 6]) | ||
| assert list(values) == [1, 1, 3, 3, 5, 5, 7] | ||
|
|
||
| _, values = tendency.get_value(np.array([5.999, 6])) | ||
| assert list(values) == [5, 7] | ||
|
|
||
|
|
||
| def test_get_value_at_times(): | ||
| """Check the value assignment at arbitrary time points.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 3, 5, 5]) | ||
| _, values = tendency.get_value(np.array([0, 1, 1.99, 2, 3, 4, 5, 6])) | ||
| assert list(values) == [1, 1, 1, 3, 3, 5, 5, 5] | ||
|
|
||
|
|
||
| def test_get_value_outside_bounds(): | ||
| """Check the generated values outside of the time array""" | ||
| tendency = StepsTendency(user_time=[1, 2, 3, 4], user_value=[2, 4, 8, 8]) | ||
| _, values = tendency.get_value(np.array([-1, 0, 4.5, 5])) | ||
| assert list(values) == [2, 2, 8, 8] | ||
|
|
||
| tendency = StepsTendency(user_time=[1, 2, 3, 4], user_value=[2, 4, 8, 10]) | ||
| _, values = tendency.get_value(np.array([-1, 0, 4.5, 5])) | ||
| assert list(values) == [2, 2, 10, 10] | ||
|
|
||
|
|
||
| def test_get_derivative(): | ||
| """Check that the derivative is always zero.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4, 6], user_value=[1, 3, 5, 5]) | ||
| derivatives = tendency.get_derivative(np.array([0, 1, 2, 3, 4, 5, 6])) | ||
| assert np.all(derivatives == 0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [[1, 2, 3], [1.5, 2.5, 3.5], ["a", "b", "c"]]) | ||
| def test_value_types(value): | ||
| """Test that int, float, and str steps are all supported.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=value) | ||
| assert not tendency.annotations | ||
| assert tendency.value_type is type(value[0]) | ||
| assert list(tendency.value) == value |
Oops, something went wrong.
Oops, something went wrong.
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.