Skip to content
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

WIP: Add Runner callbacks and option to cancel points #385

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

basnijholt
Copy link
Member

@basnijholt basnijholt commented Nov 23, 2022

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 using pip install pre-commit)
  • pytest passed

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-commenter
Copy link

codecov-commenter commented Nov 23, 2022

Codecov Report

Merging #385 (a1821f9) into main (28d4c35) will decrease coverage by 0.14%.
The diff coverage is 12.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%) ⬇️
adaptive/learner/learner1D.py 87.71% <0.00%> (+0.49%) ⬆️
adaptive/learner/average_learner1D.py 74.75% <0.00%> (+0.66%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@basnijholt basnijholt changed the title Add Runner callbacks and option to cancel points WIP: Add Runner callbacks and option to cancel points Nov 23, 2022
@basnijholt basnijholt changed the title WIP: Add Runner callbacks and option to cancel points Add Runner callbacks and option to cancel points Nov 23, 2022
@basnijholt basnijholt changed the title Add Runner callbacks and option to cancel points WIP: Add Runner callbacks and option to cancel points Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants