{ "cells": [ { "cell_type": "markdown", "id": "31e71fa5", "metadata": {}, "source": [ "# Advanced View3D Tutorial\n", "\n", "For this tutorial, it is assumed that the reader is familar with the [Overview of data using View3D](QuickPlottingofData.html) tutorial. This tutorial seeks to introduce more advanced features for the view3D object. As shown below, one can e.g. specify whether or not a grid is to be plotted with the key word arguments grid but a lot of other parameters are possible.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "8285fd58", "metadata": { "execution": { "iopub.execute_input": "2023-05-01T15:09:05.282306Z", "iopub.status.busy": "2023-05-01T15:09:05.282306Z", "iopub.status.idle": "2023-05-01T15:09:07.207783Z", "shell.execute_reply": "2023-05-01T15:09:07.207679Z" } }, "outputs": [], "source": [ "from MJOLNIR.Data import DataSet\n", "from MJOLNIR import _tools # Usefull tools useful across MJOLNIR\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib import animation\n", "\n", "\n", "plt.rcParams['figure.figsize'] = [14, 10]\n", "plt.rcParams['figure.dpi'] = 200" ] }, { "cell_type": "markdown", "id": "af8bb2b5", "metadata": {}, "source": [ "First, an animation function is create" ] }, { "cell_type": "code", "execution_count": 2, "id": "2a55be39", "metadata": { "execution": { "iopub.execute_input": "2023-05-01T15:09:07.225722Z", "iopub.status.busy": "2023-05-01T15:09:07.225722Z", "iopub.status.idle": "2023-05-01T15:09:07.357044Z", "shell.execute_reply": "2023-05-01T15:09:07.356175Z" } }, "outputs": [], "source": [ "plt.ioff()\n", "\n", "fig = plt.figure()\n", "line = fig.gca().plot([], [], lw=2)\n", "\n", "def View3DAnimator(V,name,folder,frames=None,keyFrame=None,fps=3):\n", " \"\"\"Function to facilitate generation of animations for Viewer3D\"\"\"\n", "\n", " if not frames is None:\n", " Frames = frames\n", "\n", " nFrames = len(Frames)\n", "\n", " def animate(i,correct=False):\n", " if not correct:\n", " I = Frames[i]\n", " else:\n", " I = i\n", "\n", " V.Energy_slider.set_val(I)\n", " return line\n", "\n", "\n", " if not frames is None:\n", " fig = V.ax.get_figure()\n", " anim = animation.FuncAnimation(fig, animate,\n", " frames=nFrames, interval=10, blit=True)\n", " anim.save(folder+name+'.gif', fps=fps,dpi=100,writer='imagemagick')\n", "\n", " if not keyFrame is None:\n", " fig = V.ax.get_figure()\n", " animate(keyFrame,correct=True)\n", " fig.savefig('figure0.png',format='png')\n", "\n", "plt.close(fig)" ] }, { "cell_type": "markdown", "id": "0843b837", "metadata": {}, "source": [ "Data is loaded below and a view3D objec is provided to the animation function. Running this code does take quite some time to complete." ] }, { "cell_type": "code", "execution_count": 3, "id": "0c518f9a", "metadata": { "execution": { "iopub.execute_input": "2023-05-01T15:09:07.358313Z", "iopub.status.busy": "2023-05-01T15:09:07.358313Z", "iopub.status.idle": "2023-05-01T15:09:48.270372Z", "shell.execute_reply": "2023-05-01T15:09:48.270372Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "MovieWriter imagemagick unavailable; using Pillow instead.\n" ] } ], "source": [ "numbers = '161-169'\n", "# Load and convert data\n", "fileName = _tools.fileListGenerator(numbers,folder=r'C:\\Users\\lass_j\\Documents\\CAMEA2018',year=2018)\n", "ds = DataSet.DataSet(dataFiles=fileName)\n", "ds.convertDataFile()\n", "\n", "# Plotting data quickly in equi-sized voxels can be done by\n", "Viewer = ds.View3D(0.03,0.03,0.09,grid=9,rlu=True)\n", "\n", "#Generate the viewer with voxel size 0.03 1/AA x 0.03 1/AA x 0.05 meV, using\n", "# the key word grid, one toggles the regular grid but providing the input\n", "# as a number the zorder of the grid is specified (Data is plotted at 10).\n", "# It is also possible to plot data without making use of reciprocal lattice\n", "# units, chosen by rlu = True or False.\n", "\n", "Viewer.caxis=(1e-8,5e-7)\n", "nFrames = Viewer.Z.shape[-1] # Number of energy planes\n", "frames = np.arange(3,nFrames-3)\n", "frames = np.concatenate([frames,np.flip(frames[1:-1])]) #\n", "# Generate animation as mp4 and create key frame as plane 75\n", "View3DAnimator(Viewer,'ViewerAnimationEPlane',folder='',frames=frames,keyFrame=75,fps=7)" ] }, { "cell_type": "markdown", "id": "85b45148", "metadata": {}, "source": [ "The key frame generated corresponding to frame 75:\n", "\n", "![KeyFrame](figure0.png \"KeyFrame\")" ] }, { "cell_type": "markdown", "id": "6e353d34", "metadata": {}, "source": [ "However, the main output of this code is the animation shown below" ] }, { "cell_type": "markdown", "id": "9da91799", "metadata": {}, "source": [ "" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }