|
| 1 | +"""library for making animations""" |
| 2 | +"""TODO: push these functions into WrightTools""" |
| 3 | + |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from functools import partial |
| 8 | +from matplotlib.animation import FuncAnimation |
| 9 | + |
| 10 | +# typing |
| 11 | +from ._interact import interact2D_fig |
| 12 | + |
| 13 | +__all__ = ["animate2D", "animate_interact2D"] |
| 14 | + |
| 15 | + |
| 16 | +def animate2D( |
| 17 | + d, |
| 18 | + cmap, norm, |
| 19 | + snake=False, |
| 20 | + back_and_forth=False, |
| 21 | + **ani_kwargs, |
| 22 | +): |
| 23 | + """ |
| 24 | + animate pcolormesh of a nd dataset (ndim >=2), |
| 25 | + mesh plots last two axes of the dataset (use `Data.transform` if needed) |
| 26 | + uses first channel in dataset (use `bring_to_front` if needed) |
| 27 | +
|
| 28 | + Note: snake, back_and_forth are not yet implemented are no-ops |
| 29 | + """ |
| 30 | + |
| 31 | + # initialize canvas |
| 32 | + # need fig, ax, art, and norm to get this working |
| 33 | + fig, ax = plt.subplots(subplot_kw=dict(projection="wright"), dpi=140, layout="constrained") |
| 34 | + art = ax.pcolormesh(d[tuple([0 for i in d.shape[:-2]])], cmap=cmap, norm=norm) |
| 35 | + fig.colorbar(art, ax=ax) |
| 36 | + |
| 37 | + # funcs for updating |
| 38 | + def title(ind): |
| 39 | + parts = [ |
| 40 | + f"{var.natural_name} = {var[:].squeeze()[ind]:.2f} {var.units}" |
| 41 | + for var in map(lambda a: a.variables[0], d.axes[:-2]) |
| 42 | + ] |
| 43 | + return "\n".join(parts) |
| 44 | + |
| 45 | + ax.set_title(title(0)) |
| 46 | + |
| 47 | + def update2D(frame, data, fig, ax, mesh, norm): |
| 48 | + print(frame) |
| 49 | + # for ind, axis in zip(frame, data.axes[:-2]): |
| 50 | + mesh.set_array(data.channels[0][frame]) |
| 51 | + ax.set_title(title(frame)) |
| 52 | + mesh.set_norm(norm) |
| 53 | + fig.canvas.draw_idle() |
| 54 | + return mesh |
| 55 | + |
| 56 | + frames = list(np.ndindex(d.shape[:-2])) |
| 57 | + |
| 58 | + return FuncAnimation( |
| 59 | + fig=fig, |
| 60 | + func=partial(update2D, data=d, mesh=art, fig=fig, ax=ax, norm=norm), |
| 61 | + frames=frames, |
| 62 | + **ani_kwargs, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def animate_interact2D( |
| 67 | + interact2D:interact2D_fig, |
| 68 | + snake=False, |
| 69 | + back_and_forth=False, |
| 70 | + **kwargs |
| 71 | +): |
| 72 | + """ |
| 73 | + Take an interact2D figure and create an animation by moving the sliders. |
| 74 | +
|
| 75 | + Note: snake, back_and_forth are not yet implemented are no-ops |
| 76 | + """ |
| 77 | + |
| 78 | + def update(frame): |
| 79 | + print(frame) |
| 80 | + for ind, slider in zip(frame, interact2D.sliders.values()): |
| 81 | + slider.set_val(ind) |
| 82 | + |
| 83 | + frames = list(np.ndindex(tuple([s.valmax+1 for s in interact2D.sliders.values()]))) |
| 84 | + print(f"beginning animation: {len(frames)} frames to write") |
| 85 | + return FuncAnimation( |
| 86 | + fig=interact2D.fig, |
| 87 | + func=update, |
| 88 | + frames=frames, |
| 89 | + ) |
| 90 | + |
0 commit comments