-
Notifications
You must be signed in to change notification settings - Fork 5
dtype-aware tendency values #175
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
Closed
DaanVanVugt
wants to merge
4
commits into
iterorganization:develop
from
DaanVanVugt:tendency-value-dtype
Closed
Changes from all commits
Commits
Show all changes
4 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 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
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 |
|---|---|---|
|
|
@@ -69,6 +69,12 @@ def _bind_imports(self): | |
| tendency.resolver = resolver | ||
| tendency.default_path = self.name | ||
|
|
||
| @property | ||
|
Collaborator
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. Should we check whether the specific dtype is valid for the specific IDS node e.g. during exporting? |
||
| def is_categorical(self): | ||
| """Whether this waveform produces non-numeric (categorical) values, e.g. | ||
| strings or booleans, which are held as steps rather than interpolated.""" | ||
| return any(t.is_categorical for t in self.tendencies) | ||
|
|
||
| def get_value( | ||
| self, time: np.ndarray | None = None | ||
| ) -> tuple[np.ndarray, np.ndarray]: | ||
|
|
@@ -125,7 +131,11 @@ def _evaluate_tendencies(self, time, eval_derivatives=False): | |
| Returns: | ||
| numpy array containing the computed values. | ||
| """ | ||
| values = np.zeros_like(time, dtype=float) | ||
| is_categorical = self.is_categorical and not eval_derivatives | ||
| if is_categorical: | ||
| values = np.empty(len(time), dtype=object) | ||
| else: | ||
| values = np.zeros_like(time, dtype=float) | ||
|
|
||
| for i, tendency in enumerate(self.tendencies): | ||
| mask = (time >= tendency.start) & (time <= tendency.end) | ||
|
|
@@ -135,17 +145,17 @@ def _evaluate_tendencies(self, time, eval_derivatives=False): | |
| else: | ||
| _, values[mask] = tendency.get_value(time[mask]) | ||
|
|
||
| # Handle gaps between tendencies, we linearly interpolate between the | ||
| # gap values. | ||
| # Handle gaps between tendencies (hold for strings, interpolate otherwise). | ||
| if i and tendency.prev_tendency.end < tendency.start: | ||
| prev_tendency = tendency.prev_tendency | ||
| mask = (time < tendency.start) & (time > prev_tendency.end) | ||
| slope = (tendency.start_value - prev_tendency.end_value) / ( | ||
| tendency.start - prev_tendency.end | ||
| ) | ||
| if np.any(mask): | ||
| if eval_derivatives: | ||
| values[mask] = slope | ||
| values[mask] = ( | ||
| tendency.start_value - prev_tendency.end_value | ||
| ) / (tendency.start - prev_tendency.end) | ||
| elif is_categorical: | ||
| values[mask] = prev_tendency.end_value | ||
| else: | ||
| values[mask] = np.interp( | ||
| time[mask], | ||
|
|
@@ -201,11 +211,29 @@ def _process_waveform(self, waveform): | |
| self.tendencies[i - 1].set_next_tendency(self.tendencies[i]) | ||
| self.tendencies[i].set_previous_tendency(self.tendencies[i - 1]) | ||
|
|
||
| self._validate_value_types() | ||
|
|
||
| self.update_annotations() | ||
|
|
||
| for tendency in self.tendencies: | ||
| tendency.param.watch(self.update_annotations, "annotations") | ||
|
|
||
| def _validate_value_types(self): | ||
| """Categorical (e.g. string or boolean) and numeric tendencies cannot be | ||
| mixed within a single waveform, as the gaps between them cannot be | ||
| interpolated. Flag the first tendency whose type breaks the pattern.""" | ||
| if not self.tendencies: | ||
| return | ||
| first_is_categorical = self.tendencies[0].is_categorical | ||
| for tendency in self.tendencies[1:]: | ||
| if tendency.is_categorical != first_is_categorical: | ||
| error_msg = ( | ||
| "Cannot mix categorical (e.g. string or boolean) and numeric " | ||
| "values within a single waveform.\n" | ||
| ) | ||
| self.annotations.add(tendency.line_number, error_msg) | ||
| break | ||
|
|
||
| def update_annotations(self, event=None): | ||
| """Merges the annotations of the individual tendencies into the annotations | ||
| of this waveform.""" | ||
|
|
||
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.
Screencast.From.2026-07-14.14-53-38.mp4
Reloading a string waveform YAML snippet does not work for the panel editor, could you have a look at that?