[Pylib] add at_tsim case for simulation callbacks#1894
Conversation
|
Thanks @tdavidcl for opening this PR! You can do multiple things directly here: Once the workflow completes a message will appear displaying informations related to the run. Also the PR gets automatically reviewed by gemini, you can: |
There was a problem hiding this comment.
Code Review
This pull request introduces an at_tsim parameter to the @callback decorator in SimulationRunner.py, enabling callbacks to trigger at exact simulation times. The review feedback identifies two issues: a regression in CallbackState where tsim_interval callbacks no longer trigger at the start of the simulation (tsim_start) and exact times equal to tsim_start are skipped, and a parsing bug where a single string argument for at_tsim is incorrectly iterated over as individual characters. Suggestions are provided to address both issues.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| candidates = [] | ||
| if info.tsim_interval is not None: | ||
| candidates.append(tsim_start + info.tsim_interval) | ||
| if info.at_tsim is not None: | ||
| future = [t for t in info.at_tsim if t > tsim_start] | ||
| if future: | ||
| candidates.append(min(future)) | ||
| self.next_tsim = min(candidates) if candidates else None |
There was a problem hiding this comment.
This change introduces a regression where tsim_interval callbacks are no longer triggered at the start of the simulation (tsim_start). In the original code, self.next_tsim was initialized to tsim_start, allowing the callback to run at t = 0.0 before any simulation steps. By changing it to tsim_start + info.tsim_interval, the initial callback is skipped.
Additionally, if at_tsim contains tsim_start (e.g., 0.0), it will also be skipped because of the strict inequality t > tsim_start.
To fix both issues and preserve the original behavior, we should initialize candidates with tsim_start for tsim_interval, and use t >= tsim_start for at_tsim.
| candidates = [] | |
| if info.tsim_interval is not None: | |
| candidates.append(tsim_start + info.tsim_interval) | |
| if info.at_tsim is not None: | |
| future = [t for t in info.at_tsim if t > tsim_start] | |
| if future: | |
| candidates.append(min(future)) | |
| self.next_tsim = min(candidates) if candidates else None | |
| candidates = [] | |
| if info.tsim_interval is not None: | |
| candidates.append(tsim_start) | |
| if info.at_tsim is not None: | |
| future = [t for t in info.at_tsim if t >= tsim_start] | |
| if future: | |
| candidates.append(min(future)) | |
| self.next_tsim = min(candidates) if candidates else None |
| if at_tsim is not None: | ||
| if isinstance(at_tsim, (int, float)): | ||
| at_tsim = [float(at_tsim)] | ||
| else: | ||
| at_tsim = sorted(float(t) for t in at_tsim) |
There was a problem hiding this comment.
If at_tsim is passed as a string (e.g., "10"), it is not an instance of (int, float), so the code falls back to the else block and attempts to iterate over it. Since strings are iterable in Python, this will silently parse "10" as ['1', '0'] and convert them to [1.0, 0.0], leading to unexpected behavior.
To prevent this, we should include str in the isinstance check so that single string values are correctly converted to a single float (or raise a ValueError if the string is not a valid number).
| if at_tsim is not None: | |
| if isinstance(at_tsim, (int, float)): | |
| at_tsim = [float(at_tsim)] | |
| else: | |
| at_tsim = sorted(float(t) for t in at_tsim) | |
| if at_tsim is not None: | |
| if isinstance(at_tsim, (int, float, str)): | |
| at_tsim = [float(at_tsim)] | |
| else: | |
| at_tsim = sorted(float(t) for t in at_tsim) |
Workflow reportworkflow report corresponding to commit 4563eb6 Pre-commit check reportPre-commit check: ✅ Test pipeline can run. Clang-tidy diff reportNo relevant changes found. You should now go back to your normal life and enjoy a hopefully sunny day while waiting for the review. Doxygen diff with
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
No description provided.