Expose tolerance for milestone checks#1184
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1184 +/- ##
==========================================
- Coverage 94.77% 94.76% -0.02%
==========================================
Files 46 46
Lines 3428 3437 +9
==========================================
+ Hits 3249 3257 +8
- Misses 179 180 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
RemDelaporteMathurin
left a comment
There was a problem hiding this comment.
Thanks @ee-nn and congratulations on your first PR!
A few comments on my end.
- it wuold be nice to add a test in the test suite. You proved your fix is effective in the PR description but ideally we would catch this bug programmatically in the test suite. Let us know if you don't know how to write tests
| milestone_tolerance (float, optional): relative tolerance for how closely | ||
| a time must align with a milestone to be triggered. Defaults to 1e-5. |
There was a problem hiding this comment.
Here the actual argument milestone_tolerance defaults to None. If this is the expected behaviour then we should change line 49. And perhaps removing the setter.
I think we should be more accurate in the description:
| milestone_tolerance (float, optional): relative tolerance for how closely | |
| a time must align with a milestone to be triggered. Defaults to 1e-5. | |
| milestone_tolerance (float, optional): relative tolerance passed to numpy.isclose (rtol) Defaults to 1e-5. |
There was a problem hiding this comment.
@ee-nn keeping the setter discussion in a thread for simplicity:
When you said "perhaps removing the setter" did you envision moving the error check up to the variable assignment?
I initially thought removing the setter/getter methods completely but I see you have a check for rtol < 0 that I think is good to keep.
Co-authored-by: Rémi Delaporte-Mathurin <40028739+RemDelaporteMathurin@users.noreply.github.com>
Co-authored-by: Rémi Delaporte-Mathurin <40028739+RemDelaporteMathurin@users.noreply.github.com>
Co-authored-by: Rémi Delaporte-Mathurin <40028739+RemDelaporteMathurin@users.noreply.github.com>
|
Sorry, I added this comment in the above suggestion chain that I resolved, so re-adding it here...
|
|
When you said "perhaps removing the setter" did you envision moving the error check up to the variable assignment? def __init__(
self,
initial_value,
growth_factor=None,
cutback_factor=None,
target_nb_iterations=None,
max_stepsize=None,
milestones=None,
milestone_tolerance=1e-5,
) -> None:
self.initial_value = initial_value
self.growth_factor = growth_factor
self.cutback_factor = cutback_factor
self.target_nb_iterations = target_nb_iterations
self.max_stepsize = max_stepsize
self.milestones = milestones or []
if milestone_tolerance <=0: # <--- Put the check here
raise ValueError("milestone tolerance should be greater than zero")
self.milestone_tolerance = milestone_tolerance
if milestones and (growth_factor is None or cutback_factor is None):
raise ValueError(
"Milestones are only relevant if the stepsize is adaptive. "
"Please provide growth and cutback factors."
) |
You can always "unresolve" conversations! |
@ee-nn of course. I would suggest adding a test function in this file https://github.com/festim-dev/FESTIM/blob/main/test/test_stepsize.py def test_milestone_tolerance_miss_tolerance():
"""
This tests confirms that with a high rtol the milestone is missed
See issue #number of issue
"""
....
my_model.run()
# check
assert timestep_is_not_in_export
...
# lower tolerance and rerun
# check
assert timestep_is_in_export |
Description
Proposed fix for #933 (and my first attempt at a FESTIM codebase contribution!)
Summary
Adds a
milestone_toleranceargument that allows the user to manually tune the relative tolerance (rtol) in thenp.isclosecheck of whether the time is near a milestone. If none is specified, it passes the default of 1e-5.Motivation and Context
For large times, the default relative tolerance can miss specified milestones. Exposing
milestone_toleranceto the user provides a mitigation strategy.Type of Change
Testing
pytest)Code Quality Checklist
ruff format .)ruff check .)I kept comments to a bare minimum to keep with the rest of the code style, I can add more explanation if needed
Documentation
Screenshots/Examples
Running the "Simple Simulation" example (slightly modified, see below) with
initial_value=1e-3andmilestone_tolerance=1e-2reproduces the bug described in #933 ; thet=1s milestone is missed:Omitting
milestone_tolerancecauses fallback to the default 1e-5, successfully reproducing the graph on the simple simulation page.Additional Notes