1
1
---
2
- kernelspec :
3
- name : python3
4
- display_name : python3
5
2
jupytext :
6
3
text_representation :
7
4
extension : .md
8
5
format_name : myst
9
- format_version : ' 0.13'
10
- jupytext_version : 1.13.8
6
+ format_version : 0.13
7
+ jupytext_version : 1.14.1
8
+ kernelspec :
9
+ display_name : Python 3 (ipykernel)
10
+ language : python
11
+ name : python3
11
12
---
12
13
13
14
``` {code-cell} ipython3
14
15
:tags: [remove-input]
15
16
16
17
import os
18
+ import functools
19
+ from pathlib import Path
17
20
18
21
import matplotlib.tri as mtri
19
22
import numpy as np
@@ -26,7 +29,8 @@ from tqdm.auto import tqdm
26
29
import adaptive
27
30
28
31
29
- def add_rounded_corners(size, rad):
32
+ @functools.lru_cache
33
+ def make_cut(size, rad):
30
34
# Make new images
31
35
circle = Image.new("L", (rad * 2, rad * 2), color=1)
32
36
draw = ImageDraw.Draw(circle)
@@ -41,7 +45,12 @@ def add_rounded_corners(size, rad):
41
45
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
42
46
43
47
# To array
44
- cut = np.array(alpha)
48
+ return np.array(alpha)
49
+
50
+
51
+ @functools.lru_cache
52
+ def add_rounded_corners(size=(1000, 1000), rad=300):
53
+ cut = make_cut(size, rad)
45
54
cut = cut.reshape((*cut.shape, 1)).repeat(4, axis=2)
46
55
47
56
# Set the corners to (252, 252, 252, 255) to match the RTD background #FCFCFC
@@ -50,6 +59,16 @@ def add_rounded_corners(size, rad):
50
59
return cut
51
60
52
61
62
+ def remove_rounded_corners(fname):
63
+ im = Image.open(fname)
64
+ ar = np.array(im)
65
+ cut = make_cut(size=ar.shape[:-1], rad=round(ar.shape[0] * 0.3)).astype(bool)
66
+ ar[:, :, -1] = np.where(~cut, ar[:, :, -1], 0)
67
+ im_new = Image.fromarray(ar)
68
+ im_new.save(fname)
69
+ return im_new
70
+
71
+
53
72
def learner_till(till, learner, data):
54
73
new_learner = adaptive.Learner2D(None, bounds=learner.bounds)
55
74
new_learner.data = {k: v for k, v in data[:till]}
@@ -70,10 +89,14 @@ def get_new_artists(npoints, learner, data, rounded_corners, ax):
70
89
line1, line2 = plot_tri(new_learner, ax)
71
90
data = np.rot90(new_learner.interpolated_on_grid()[-1])
72
91
im = ax.imshow(data, extent=(-0.5, 0.5, -0.5, 0.5), cmap="viridis")
73
- im2 = ax.imshow(rounded_corners, extent=(-0.5, 0.5, -0.5, 0.5), zorder=10)
74
- return im, line1, line2, im2
92
+ if rounded_corners is None:
93
+ return im, line1, line2
94
+ else:
95
+ im2 = ax.imshow(rounded_corners, extent=(-0.5, 0.5, -0.5, 0.5), zorder=10)
96
+ return im, line1, line2, im2
75
97
76
98
99
+ @functools.lru_cache
77
100
def create_and_run_learner():
78
101
def ring(xy):
79
102
import numpy as np
@@ -87,11 +110,7 @@ def create_and_run_learner():
87
110
return learner
88
111
89
112
90
- def main(fname="source/_static/logo_docs.mp4"):
91
- learner = create_and_run_learner()
92
-
93
- data = list(learner.data.items())
94
-
113
+ def get_figure():
95
114
fig, ax = plt.subplots(figsize=(5, 5))
96
115
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
97
116
ax.set_xticks([])
@@ -100,29 +119,83 @@ def main(fname="source/_static/logo_docs.mp4"):
100
119
ax.spines["right"].set_visible(False)
101
120
ax.spines["bottom"].set_visible(False)
102
121
ax.spines["left"].set_visible(False)
122
+ return fig, ax
123
+
124
+
125
+ def setup(nseconds=15):
126
+ learner = create_and_run_learner()
127
+
128
+ data = list(learner.data.items())
129
+
130
+ fig, ax = get_figure()
103
131
104
- nseconds = 15
105
132
npoints = (len(data) * np.linspace(0, 1, 24 * nseconds) ** 2).astype(int)
106
133
rounded_corners = add_rounded_corners(size=(1000, 1000), rad=300)
134
+ return npoints, learner, data, rounded_corners, fig, ax
135
+
136
+
137
+ def animate_mp4(fname="source/_static/logo_docs.mp4"):
138
+ npoints, learner, data, rounded_corners, fig, ax = setup()
107
139
artists = [
108
140
get_new_artists(n, learner, data, rounded_corners, ax) for n in tqdm(npoints)
109
141
]
110
-
111
142
ani = animation.ArtistAnimation(fig, artists, blit=True)
112
143
ani.save(fname, writer=FFMpegWriter(fps=24))
113
144
114
145
146
+ def animate_png(folder="source/_static/logo", nseconds=2):
147
+ npoints, learner, data, rounded_corners, fig, ax = setup(nseconds)
148
+ folder = Path(folder)
149
+ folder.mkdir(parents=True, exist_ok=True)
150
+ fnames = []
151
+ for n in tqdm(npoints):
152
+ fname = folder / f"logo_docs_{n:03d}.png"
153
+ fnames.append(fname)
154
+ npoints, learner, data, _, fig, ax = setup(nseconds=2)
155
+ get_new_artists(n, learner, data, None, ax)
156
+ fig.savefig(fname, transparent=True)
157
+ ax.cla()
158
+ remove_rounded_corners(fname)
159
+ return fnames
160
+
161
+
115
162
if __name__ == "__main__":
116
- fname = "_static/logo_docs.mp4"
117
- if not os.path.exists(fname):
118
- main(fname)
163
+ fname = Path("_static/logo_docs.mp4")
164
+ # if not fname.exists():
165
+ # ar = animate_mp4(fname)
166
+ animate_png()
167
+ ```
168
+
169
+ ``` {code-cell} ipython3
170
+ n = 10
171
+ folder = Path("source/_static/logo")
172
+ fname = folder / f"logo_docs_{n:03d}.png"
173
+ npoints, learner, data, rounded_corners, fig, ax = setup(nseconds=2)
174
+ get_new_artists(n, learner, data, None, ax)
175
+ fig.savefig(fname, transparent=True)
119
176
```
120
177
121
178
``` {eval-rst}
122
179
.. raw:: html
123
180
124
- <video autoplay loop muted playsinline webkit-playsinline
125
- style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
126
- <source src="_static/logo_docs.mp4" type="video/mp4">
127
- </video><br>
181
+ <style>
182
+ .dark-video {
183
+ display: none;
184
+ }
185
+
186
+ @media (prefers-color-scheme: dark) {
187
+ .dark-video {
188
+ display: block;
189
+ }
190
+
191
+ .light-video {
192
+ display: none;
193
+ }
194
+ }
195
+ </style>
196
+ <video autoplay loop muted playsinline webkit-playsinline style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
197
+ <source class="dark-video" src="_static/logo_docs.mp4" type="video/mp4">
198
+ <source class="light-video" src="_static/logo_docs1.mp4" type="video/mp4">
199
+ </video>
200
+ <br>
128
201
```
0 commit comments