Skip to content

Commit 6fca822

Browse files
committed
add tutorials
1 parent 77ec068 commit 6fca822

20 files changed

Lines changed: 1362 additions & 8 deletions

adaptive/notebook_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def notebook_extension():
2020
try:
2121
import ipywidgets
2222
import holoviews
23-
holoviews.notebook_extension('bokeh')
23+
holoviews.notebook_extension('bokeh', logo=False)
2424
_plotting_enabled = True
2525
except ModuleNotFoundError:
2626
warnings.warn("holoviews and (or) ipywidgets are not installed; plotting "

adaptive/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class BlockingRunner(BaseRunner):
264264
of cores available in `executor`.
265265
log : bool, default: False
266266
If True, record the method calls made to the learner by this runner.
267-
shutdown_executor : Bool, default: False
267+
shutdown_executor : bool, default: False
268268
If True, shutdown the executor when the runner has completed. If
269269
`executor` is not provided then the executor created internally
270270
by the runner is shut down, regardless of this parameter.
@@ -367,7 +367,7 @@ class AsyncRunner(BaseRunner):
367367
of cores available in `executor`.
368368
log : bool, default: False
369369
If True, record the method calls made to the learner by this runner.
370-
shutdown_executor : Bool, default: False
370+
shutdown_executor : bool, default: False
371371
If True, shutdown the executor when the runner has completed. If
372372
`executor` is not provided then the executor created internally
373373
by the runner is shut down, regardless of this parameter.

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build/*
2+
source/_static/holoviews.*

docs/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ dependencies:
1616
- scikit-optimize
1717
- pip:
1818
- sphinx_rtd_theme
19+
- git+https://github.com/basnijholt/jupyter-sphinx.git@widgets_execute

docs/source/conf.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'sphinx.ext.mathjax',
4848
'sphinx.ext.viewcode',
4949
'sphinx.ext.napoleon',
50+
'jupyter_sphinx.execute',
5051
]
5152

5253
source_parsers = {}
@@ -113,13 +114,52 @@
113114
# Output file base name for HTML help builder.
114115
htmlhelp_basename = 'adaptivedoc'
115116

117+
116118
# -- Extension configuration -------------------------------------------------
117119

118120
default_role = 'autolink'
119121

120-
intersphinx_mapping = {'python': ('https://docs.python.org/3', None),
121-
'distributed': ('https://distributed.readthedocs.io/en/stable/', None),
122-
'holoviews': ('https://holoviews.org/', None),
123-
'ipyparallel': ('https://ipyparallel.readthedocs.io/en/stable/', None),
124-
'scipy': ('https://docs.scipy.org/doc/scipy/reference', None),
122+
intersphinx_mapping = {
123+
'python': ('https://docs.python.org/3', None),
124+
'distributed': ('https://distributed.readthedocs.io/en/stable/', None),
125+
'holoviews': ('https://holoviews.org/', None),
126+
'ipyparallel': ('https://ipyparallel.readthedocs.io/en/stable/', None),
127+
'scipy': ('https://docs.scipy.org/doc/scipy/reference', None),
125128
}
129+
130+
131+
# -- Add Holoviews js and css ------------------------------------------------
132+
133+
def setup(app):
134+
from holoviews.plotting import Renderer
135+
136+
hv_js, hv_css = Renderer.html_assets(
137+
extras=False, backends=[], script=True)
138+
139+
fname_css = 'holoviews.css'
140+
fname_js = 'holoviews.js'
141+
static_dir = 'source/_static'
142+
143+
os.makedirs(static_dir, exist_ok=True)
144+
145+
with open(f'{static_dir}/{fname_css}', 'w') as f:
146+
hv_css = hv_css.split('<style>')[1].replace('</style>', '')
147+
f.write(hv_css)
148+
149+
with open(f'{static_dir}/{fname_js}', 'w') as f:
150+
f.write(hv_js)
151+
152+
app.add_stylesheet(fname_css)
153+
app.add_javascript(fname_js)
154+
155+
dependencies = {**Renderer.core_dependencies, **Renderer.extra_dependencies}
156+
for name, type_url in dependencies.items():
157+
if name in ['bootstrap']:
158+
continue
159+
160+
for url in type_url.get('js', []):
161+
app.add_javascript(url)
162+
for url in type_url.get('css', []):
163+
app.add_stylesheet(url)
164+
165+
app.add_javascript("https://unpkg.com/@jupyter-widgets/html-manager@0.14.4/dist/embed-amd.js")

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
:hidden:
1818

1919
rest_of_readme
20+
tutorial/tutorial
2021
reference/adaptive

docs/source/reference/adaptive.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Runners
2222
adaptive.runner.Runner
2323
adaptive.runner.AsyncRunner
2424
adaptive.runner.BlockingRunner
25+
adaptive.runner.extras
2526

2627
Other
2728
-----
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
adaptive.runner.simple
2+
======================
3+
4+
Simple executor
5+
---------------
6+
7+
.. autofunction:: adaptive.runner.simple
8+
9+
Sequential excecutor
10+
--------------------
11+
12+
.. autofunction:: adaptive.runner.SequentialExecutor
13+
14+
15+
Replay log
16+
----------
17+
18+
.. autofunction:: adaptive.runner.replay_log
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Tutorial `~adaptive.AverageLearner`
2+
-----------------------------------
3+
4+
.. note::
5+
Because this documentation consists of static html, the ``live_plot``
6+
and ``live_info`` widget is not live. Download the notebook
7+
in order to see the real behaviour.
8+
9+
.. seealso::
10+
The complete source code of this tutorial can be found in
11+
:jupyter-download:notebook:`AverageLearner`
12+
13+
.. execute::
14+
:hide-code:
15+
:new-notebook: AverageLearner
16+
17+
import adaptive
18+
adaptive.notebook_extension()
19+
20+
The next type of learner averages a function until the uncertainty in
21+
the average meets some condition.
22+
23+
This is useful for sampling a random variable. The function passed to
24+
the learner must formally take a single parameter, which should be used
25+
like a “seed” for the (pseudo-) random variable (although in the current
26+
implementation the seed parameter can be ignored by the function).
27+
28+
.. execute::
29+
30+
def g(n):
31+
import random
32+
from time import sleep
33+
sleep(random.random() / 1000)
34+
# Properly save and restore the RNG state
35+
state = random.getstate()
36+
random.seed(n)
37+
val = random.gauss(0.5, 1)
38+
random.setstate(state)
39+
return val
40+
41+
.. execute::
42+
43+
learner = adaptive.AverageLearner(g, atol=None, rtol=0.01)
44+
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 2)
45+
46+
.. execute::
47+
:hide-code:
48+
49+
await runner.task # This is not needed in a notebook environment!
50+
51+
.. execute::
52+
53+
runner.live_info()
54+
55+
.. execute::
56+
57+
runner.live_plot(update_interval=0.1)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Tutorial `~adaptive.BalancingLearner`
2+
-------------------------------------
3+
4+
.. note::
5+
Because this documentation consists of static html, the ``live_plot``
6+
and ``live_info`` widget is not live. Download the notebook
7+
in order to see the real behaviour.
8+
9+
.. seealso::
10+
The complete source code of this tutorial can be found in
11+
:jupyter-download:notebook:`BalancingLearner`
12+
13+
.. execute::
14+
:hide-code:
15+
:new-notebook: BalancingLearner
16+
17+
import adaptive
18+
adaptive.notebook_extension()
19+
20+
import holoviews as hv
21+
import numpy as np
22+
from functools import partial
23+
import random
24+
25+
The balancing learner is a “meta-learner” that takes a list of learners.
26+
When you request a point from the balancing learner, it will query all
27+
of its “children” to figure out which one will give the most
28+
improvement.
29+
30+
The balancing learner can for example be used to implement a poor-man’s
31+
2D learner by using the `~adaptive.Learner1D`.
32+
33+
.. execute::
34+
35+
def h(x, offset=0):
36+
a = 0.01
37+
return x + a**2 / (a**2 + (x - offset)**2)
38+
39+
learners = [adaptive.Learner1D(partial(h, offset=random.uniform(-1, 1)),
40+
bounds=(-1, 1)) for i in range(10)]
41+
42+
bal_learner = adaptive.BalancingLearner(learners)
43+
runner = adaptive.Runner(bal_learner, goal=lambda l: l.loss() < 0.01)
44+
45+
.. execute::
46+
:hide-code:
47+
48+
await runner.task # This is not needed in a notebook environment!
49+
50+
.. execute::
51+
52+
runner.live_info()
53+
54+
.. execute::
55+
56+
plotter = lambda learner: hv.Overlay([L.plot() for L in learner.learners])
57+
runner.live_plot(plotter=plotter, update_interval=0.1)
58+
59+
Often one wants to create a set of ``learner``\ s for a cartesian
60+
product of parameters. For that particular case we’ve added a
61+
``classmethod`` called ``~adaptive.BalancingLearner.from_product``.
62+
See how it works below
63+
64+
.. execute::
65+
66+
from scipy.special import eval_jacobi
67+
68+
def jacobi(x, n, alpha, beta): return eval_jacobi(n, alpha, beta, x)
69+
70+
combos = {
71+
'n': [1, 2, 4, 8],
72+
'alpha': np.linspace(0, 2, 3),
73+
'beta': np.linspace(0, 1, 5),
74+
}
75+
76+
learner = adaptive.BalancingLearner.from_product(
77+
jacobi, adaptive.Learner1D, dict(bounds=(0, 1)), combos)
78+
79+
runner = adaptive.BlockingRunner(learner, goal=lambda l: l.loss() < 0.01)
80+
81+
# The `cdims` will automatically be set when using `from_product`, so
82+
# `plot()` will return a HoloMap with correctly labeled sliders.
83+
learner.plot().overlay('beta').grid().select(y=(-1, 3))

0 commit comments

Comments
 (0)