@@ -14,10 +14,114 @@ jupytext:
14
14
---
15
15
tags: [remove-input]
16
16
---
17
- :load: ../logo_animated.py
17
+ import os
18
+
19
+ import matplotlib.tri as mtri
20
+ import numpy as np
21
+ from matplotlib import animation
22
+ from matplotlib import pyplot as plt
23
+ from matplotlib.animation import FFMpegWriter
24
+ from PIL import Image, ImageDraw
25
+ from tqdm.auto import tqdm
26
+
27
+ import adaptive
28
+
29
+
30
+ def add_rounded_corners(size, rad):
31
+ # Make new images
32
+ circle = Image.new("L", (rad * 2, rad * 2), color=1)
33
+ draw = ImageDraw.Draw(circle)
34
+ draw.ellipse((0, 0, rad * 2, rad * 2), fill=0)
35
+ alpha = Image.new("L", size, 0)
36
+
37
+ # Crop circles
38
+ w, h = size
39
+ alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
40
+ alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
41
+ alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
42
+ alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
43
+
44
+ # To array
45
+ cut = np.array(alpha)
46
+ cut = cut.reshape((*cut.shape, 1)).repeat(4, axis=2)
47
+
48
+ # Set the corners to (252, 252, 252, 255) to match the RTD background #FCFCFC
49
+ cut[:, :, -1] *= 255
50
+ cut[:, :, :-1] *= 252
51
+ return cut
52
+
53
+
54
+ def learner_till(till, learner, data):
55
+ new_learner = adaptive.Learner2D(None, bounds=learner.bounds)
56
+ new_learner.data = {k: v for k, v in data[:till]}
57
+ for x, y in learner._bounds_points:
58
+ # always include the bounds
59
+ new_learner.tell((x, y), learner.data[x, y])
60
+ return new_learner
61
+
62
+
63
+ def plot_tri(learner, ax):
64
+ tri = learner.ip().tri
65
+ triang = mtri.Triangulation(*tri.points.T, triangles=tri.vertices)
66
+ return ax.triplot(triang, c="k", lw=0.8, alpha=0.8)
67
+
68
+
69
+ def get_new_artists(npoints, learner, data, rounded_corners, ax):
70
+ new_learner = learner_till(npoints, learner, data)
71
+ line1, line2 = plot_tri(new_learner, ax)
72
+ data = np.rot90(new_learner.interpolated_on_grid()[-1])
73
+ im = ax.imshow(data, extent=(-0.5, 0.5, -0.5, 0.5), cmap="viridis")
74
+ im2 = ax.imshow(rounded_corners, extent=(-0.5, 0.5, -0.5, 0.5), zorder=10)
75
+ return im, line1, line2, im2
76
+
77
+
78
+ def create_and_run_learner():
79
+ def ring(xy):
80
+ import numpy as np
81
+
82
+ x, y = xy
83
+ a = 0.2
84
+ return x + np.exp(-((x**2 + y**2 - 0.75**2) ** 2) / a**4)
85
+
86
+ learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)])
87
+ adaptive.runner.simple(learner, goal=lambda l: l.loss() < 0.005)
88
+ return learner
89
+
90
+
91
+ def main(fname="source/_static/logo_docs.mp4"):
92
+ learner = create_and_run_learner()
93
+
94
+ data = list(learner.data.items())
95
+
96
+ fig, ax = plt.subplots(figsize=(5, 5))
97
+ fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
98
+ ax.set_xticks([])
99
+ ax.set_yticks([])
100
+ ax.spines["top"].set_visible(False)
101
+ ax.spines["right"].set_visible(False)
102
+ ax.spines["bottom"].set_visible(False)
103
+ ax.spines["left"].set_visible(False)
104
+
105
+ nseconds = 15
106
+ npoints = (len(data) * np.linspace(0, 1, 24 * nseconds) ** 2).astype(int)
107
+ rounded_corners = add_rounded_corners(size=(1000, 1000), rad=300)
108
+ artists = [
109
+ get_new_artists(n, learner, data, rounded_corners, ax) for n in tqdm(npoints)
110
+ ]
111
+
112
+ ani = animation.ArtistAnimation(fig, artists, blit=True)
113
+ ani.save(fname, writer=FFMpegWriter(fps=24))
114
+
115
+
116
+ if __name__ == "__main__":
117
+ fname = "_static/logo_docs.mp4"
118
+ if not os.path.exists(fname):
119
+ main(fname)
18
120
```
19
121
20
- ``` {raw} html
122
+ ``` {raw-cell}
123
+ :format: text/html
124
+
21
125
<video autoplay loop muted playsinline webkit-playsinline
22
126
style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
23
127
<source src="_static/logo_docs.mp4" type="video/mp4">
0 commit comments