adaptive
adaptive copied to clipboard
WIP: Add Runner callbacks and option to cancel points
Description
cc @tlaeven
This allows to add periodic callbacks to the Runner and adds a way to cancel points that are currently being calculated.
import adaptive
adaptive.notebook_extension()
def depletion_voltage(x):
"""Calculates a depletion voltage curve.
Whenever the result is below 0, we know that for all points
to the left of that, the result is also zero.
"""
from time import sleep
from random import random
sleep(random())
if x < -0.3:
# Make some negative points really slow
sleep(10)
return max(0, x)
learner = adaptive.Learner1D(depletion_voltage, bounds=(-1, 1))
runner = adaptive.Runner(learner, npoints_goal=50)
def cancel_depleted_futures(runner):
if not runner.learner.data:
return
zeros = [x for x, y in sorted(learner.data.items()) if y <= 0]
if not zeros:
return
for fut, x in runner.pending_points:
if x < zeros[-1]:
print(f"cancelling {x=} because f(x={zeros[-1]})=0")
runner.cancel_point(fut)
runner.start_periodic_callback(cancel_depleted_futures, 3)
runner.live_info()
Checklist
- [ ] Fixed style issues using
pre-commit run --all(first install usingpip install pre-commit) - [ ]
pytestpassed
Type of change
Check relevant option(s).
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] (Code) style fix or documentation update
- [ ] This change requires a documentation update
Codecov Report
Merging #385 (a1821f9) into main (28d4c35) will decrease coverage by
0.14%. The diff coverage is12.50%.
@@ Coverage Diff @@
## main #385 +/- ##
==========================================
- Coverage 77.88% 77.73% -0.15%
==========================================
Files 37 37
Lines 5508 5525 +17
Branches 989 995 +6
==========================================
+ Hits 4290 4295 +5
- Misses 1066 1078 +12
Partials 152 152
| Impacted Files | Coverage Δ | |
|---|---|---|
| adaptive/runner.py | 65.73% <12.50%> (-3.01%) |
:arrow_down: |
| adaptive/learner/learner1D.py | 87.71% <0.00%> (+0.49%) |
:arrow_up: |
| adaptive/learner/average_learner1D.py | 74.75% <0.00%> (+0.66%) |
:arrow_up: |
:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more