Skip to content

Commit 79bcd37

Browse files
committed
Replace flashing live animation with clean post-run version
1 parent cd178e3 commit 79bcd37

1 file changed

Lines changed: 88 additions & 29 deletions

File tree

examples/tutorials/aposmm/aposmm_tutorial_notebook.ipynb

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -226,28 +226,7 @@
226226
"source": [
227227
"## Run the Ensemble\n",
228228
"\n",
229-
"Optionally run the next cell to set up a live graphic of the optimization progress during execution.\n",
230-
"\n",
231-
"**WARNING**: The graphic may flicker when the ensemble is running."
232-
]
233-
},
234-
{
235-
"cell_type": "code",
236-
"execution_count": null,
237-
"metadata": {},
238-
"outputs": [],
239-
"source": [
240-
"# Configure to view live progress\n",
241-
"from libensemble.tools.live_data.plot2n import Plot2N\n",
242-
"\n",
243-
"libE_specs[\"live_data\"] = Plot2N(plot_type=\"2d\") # Alt: '3d'"
244-
]
245-
},
246-
{
247-
"cell_type": "markdown",
248-
"metadata": {},
249-
"source": [
250-
"Finally, set `persis_info` (to provide random seeds to workers) and run the ensemble:"
229+
"Finally, set persis_info (to provide random seeds to workers) and run the ensemble."
251230
]
252231
},
253232
{
@@ -290,29 +269,109 @@
290269
" \n",
291270
"The first six values correspond to the local minima for the Six-Hump Camel simulation function.\n",
292271
"\n",
293-
"The 7th value is a repeat minimum, as APOSMM will continue to start local optimization runs.\n",
294-
"\n",
295-
"Please see the [API reference](https://libensemble.readthedocs.io/en/main/examples/aposmm.html) for more APOSMM configuration options and other information.\n",
272+
"The 7th value is a repeat minimum, as APOSMM will continue to start local optimization runs."
273+
]
274+
},
275+
{
276+
"cell_type": "markdown",
277+
"metadata": {},
278+
"source": [
279+
"## Viewing Animation\n",
296280
"\n",
281+
"The following cell produces a 3D animation showing the random sampling points, \n",
282+
"and points produced by the optimization runs, under the local Minima. It may take\n",
283+
"a few seconds to produce the animation."
284+
]
285+
},
286+
{
287+
"cell_type": "code",
288+
"execution_count": null,
289+
"metadata": {},
290+
"outputs": [],
291+
"source": [
292+
"import numpy as np\n",
293+
"import matplotlib.pyplot as plt\n",
294+
"import matplotlib.animation as animation\n",
295+
"from IPython.display import HTML\n",
296+
"from matplotlib.lines import Line2D\n",
297+
"\n",
298+
"def animate_aposmm_3d(H, batch_size):\n",
299+
" x_vals = np.linspace(-2, 2, 50)\n",
300+
" y_vals = np.linspace(-1, 1.1, 50)\n",
301+
" X, Y = np.meshgrid(x_vals, y_vals)\n",
302+
" Z = np.array([six_hump_camel_func(np.array([x, y])) for x, y in zip(X.ravel(), Y.ravel())]).reshape(X.shape)\n",
303+
" fig = plt.figure(figsize=(10, 8))\n",
304+
" ax = fig.add_subplot(111, projection=\"3d\")\n",
305+
" ax.plot_surface(X, Y, Z, cmap=\"winter\", edgecolor=\"none\", alpha=0.3)\n",
306+
" sc_normal = ax.scatter3D([], [], [], s=6, color=\"black\", marker=\"o\", label=\"Point\")\n",
307+
" sc_localp = ax.scatter3D([], [], [], s=40, color=\"red\", marker=\"^\", label=\"Optimization point\")\n",
308+
" custom_M_marker = Line2D([0], [0], linestyle='None', marker='$\\\\mathrm{M}$',\n",
309+
" markersize=8, markerfacecolor='black', markeredgecolor='black', color='white')\n",
310+
" ax.legend([sc_normal, sc_localp, custom_M_marker], [\"Point\", \"Optimization point\", \"Local minimum\"],loc=\"upper left\")\n",
311+
" fig.tight_layout()\n",
312+
" annotations = []\n",
313+
"\n",
314+
" def update(frame):\n",
315+
" for ann in annotations:\n",
316+
" ann.remove()\n",
317+
" annotations.clear()\n",
318+
" end = min((frame + 1) * batch_size, len(H))\n",
319+
" H_sub = H[:end]\n",
320+
" masks = [~H_sub[\"local_pt\"] & ~H_sub[\"local_min\"], H_sub[\"local_pt\"], H_sub[\"local_min\"]]\n",
321+
" (x_n, y_n, f_n), (x_lp, y_lp, f_lp), (x_lm, y_lm, f_lm) = [\n",
322+
" (H_sub[\"x\"][m, 0], H_sub[\"x\"][m, 1], H_sub[\"f\"][m]) for m in masks\n",
323+
" ]\n",
324+
" sc_normal._offsets3d = (x_n, y_n, f_n)\n",
325+
" sc_localp._offsets3d = (x_lp, y_lp, f_lp)\n",
326+
" for i in range(len(x_lm)):\n",
327+
" annotations.append(ax.text(x_lm[i], y_lm[i], f_lm[i], \"M\", color=\"white\", fontsize=12,\n",
328+
" bbox=dict(facecolor=\"black\", alpha=0.7, pad=2), zorder=999\n",
329+
" ))\n",
330+
" return sc_normal, sc_localp\n",
331+
" total_frames = (len(H) + batch_size - 1) // batch_size\n",
332+
" ani = animation.FuncAnimation(fig, update, frames=total_frames, interval=500, blit=False, repeat=False)\n",
333+
" plt.close(fig)\n",
334+
" return HTML(ani.to_jshtml())\n",
335+
"\n",
336+
"# Reduce batch_size for more refined steps\n",
337+
"animate_aposmm_3d(H, batch_size=50)"
338+
]
339+
},
340+
{
341+
"cell_type": "markdown",
342+
"metadata": {},
343+
"source": [
297344
"## Applications\n",
298345
"\n",
299346
"APOSMM is not limited to evaluating minima from pure Python simulation functions.\n",
300347
"Many common libEnsemble use-cases involve using libEnsemble's Executor to launch user\n",
301348
"applications with parameters requested by APOSMM, then evaluate their output using\n",
302349
"APOSMM, and repeat until minima are identified. A currently supported example\n",
303-
"can be found in libEnsemble's [WarpX Scaling Test](https://github.com/Libensemble/libensemble/tree/main/libensemble/tests/scaling_tests/warpx)"
350+
"can be found in libEnsemble's [WarpX Scaling Test](https://github.com/Libensemble/libensemble/tree/main/libensemble/tests/scaling_tests/warpx)\n",
351+
"\n",
352+
"Please see the [API reference](https://libensemble.readthedocs.io/en/main/examples/aposmm.html) for more APOSMM configuration options and other information."
304353
]
305354
}
306355
],
307356
"metadata": {
308357
"kernelspec": {
309-
"display_name": "Python 3",
358+
"display_name": "Python 3 (ipykernel)",
359+
"language": "python",
310360
"name": "python3"
311361
},
312362
"language_info": {
313-
"name": "python"
363+
"codemirror_mode": {
364+
"name": "ipython",
365+
"version": 3
366+
},
367+
"file_extension": ".py",
368+
"mimetype": "text/x-python",
369+
"name": "python",
370+
"nbconvert_exporter": "python",
371+
"pygments_lexer": "ipython3",
372+
"version": "3.13.1"
314373
}
315374
},
316375
"nbformat": 4,
317-
"nbformat_minor": 2
376+
"nbformat_minor": 4
318377
}

0 commit comments

Comments
 (0)