-
Notifications
You must be signed in to change notification settings - Fork 5
Steps tendency #176
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
DaanVanVugt
wants to merge
5
commits into
iterorganization:develop
Choose a base branch
from
DaanVanVugt:steps-tendency
base: develop
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
Steps tendency #176
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53e2550
dtype-aware tendency values
DaanVanVugt 0cf91b8
Generalize categorical tendency values, validate mixing, document
DaanVanVugt efc0a3c
docs: clarify integers are interpolated as floats, not categorical
DaanVanVugt fc48600
Evaluate numeric constant values as float on all eval paths
DaanVanVugt 2ff4794
Add steps (zero-order-hold) tendency
DaanVanVugt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import numpy as np | ||
|
|
||
| from waveform_editor.tendencies.steps import StepsTendency | ||
| from waveform_editor.waveform import Waveform | ||
|
|
||
|
|
||
| def test_filled(): | ||
| """Time/value arrays keep their native dtype and are not interpolated.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5]) | ||
| assert np.all(tendency.time == np.array([0, 2, 4])) | ||
| assert np.all(tendency.value == np.array([1, 3, 5])) | ||
| assert not tendency.is_categorical | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_zero_order_hold(): | ||
| """Each value is held from its breakpoint until the next; ends extrapolate.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5]) | ||
| t = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) | ||
| _, values = tendency.get_value(t) | ||
| assert list(values) == [1, 1, 1, 3, 3, 5, 5] | ||
|
|
||
|
|
||
| def test_derivative_is_zero(): | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5]) | ||
| assert np.all(tendency.get_derivative(np.array([0.0, 1.0, 2.0, 3.0])) == 0) | ||
|
|
||
|
|
||
| def test_string_values(): | ||
| """A steps tendency can hold string values.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=["ohmic", "nbi", "ec"]) | ||
| assert tendency.is_categorical | ||
| _, values = tendency.get_value(np.array([0.0, 1.0, 2.0, 3.0, 4.0])) | ||
| assert list(values) == ["ohmic", "ohmic", "nbi", "nbi", "ec"] | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_boolean_values(): | ||
| """A steps tendency can hold boolean values, which are categorical (held).""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[False, True, False]) | ||
| assert tendency.is_categorical | ||
| _, values = tendency.get_value(np.array([0.0, 1.0, 2.0, 3.0, 4.0])) | ||
| assert [bool(v) for v in values] == [False, False, True, True, False] | ||
| assert not tendency.annotations | ||
|
|
||
|
|
||
| def test_non_monotonic_time(): | ||
| tendency = StepsTendency(user_time=[0, 2, 1], user_value=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_mismatched_lengths(): | ||
| tendency = StepsTendency(user_time=[0, 2], user_value=[1, 2, 3]) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_start_and_end_inference(): | ||
| """Start comes from the first breakpoint; end defaults to the last breakpoint.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5]) | ||
| assert tendency.start == 0 | ||
| assert tendency.end == 4 | ||
|
|
||
|
|
||
| def test_explicit_end_holds_final_value(): | ||
| """An explicit end holds the final value for its full duration.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5], user_end=6) | ||
| assert tendency.start == 0 | ||
| assert tendency.end == 6 | ||
| assert not tendency.annotations | ||
| _, values = tendency.get_value(np.array([4.0, 5.0, 6.0])) | ||
| assert list(values) == [5, 5, 5] | ||
|
|
||
|
|
||
| def test_end_before_last_time_is_flagged(): | ||
| """An end that precedes the last breakpoint is an error.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5], user_end=3) | ||
| assert tendency.annotations | ||
|
|
||
|
|
||
| def test_start_not_allowed(): | ||
| """Providing an explicit start is rejected; it is inferred from the time list.""" | ||
| tendency = StepsTendency(user_time=[0, 2, 4], user_value=[1, 3, 5], user_start=1) | ||
| assert tendency.annotations | ||
| assert tendency.start == 0 | ||
|
|
||
|
|
||
| def test_in_waveform(): | ||
| """A steps tendency drives a zero-order-hold waveform (numeric and string).""" | ||
| numeric = Waveform( | ||
| waveform=[ | ||
| {"user_type": "steps", "user_time": [0, 2, 4], "user_value": [1, 3, 5]} | ||
| ] | ||
| ) | ||
| assert not numeric.is_categorical | ||
| _, values = numeric.get_value(np.array([0.0, 1.0, 2.0, 3.0, 4.0])) | ||
| assert list(values) == [1, 1, 3, 3, 5] | ||
|
|
||
| string = Waveform( | ||
| waveform=[ | ||
| { | ||
| "user_type": "steps", | ||
| "user_time": [0, 2, 4], | ||
| "user_value": ["a", "b", "c"], | ||
| } | ||
| ] | ||
| ) | ||
| assert string.is_categorical | ||
| _, values = string.get_value(np.array([1.0, 3.0, 5.0])) | ||
| assert list(values) == ["a", "b", "c"] |
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
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.
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.
When I try to create the waveform above, it will raise a ValueError in the Scipy interpolation. Can we instead catch at an earlier point that categorical tendencies may not be mixed with non-categorical tendencies?