From 5e1535dc2f5289500584c66f6838013fa49de4ce Mon Sep 17 00:00:00 2001 From: Dou Du Date: Thu, 13 Jun 2024 09:30:12 +0200 Subject: [PATCH] update the example notebook --- example/example.ipynb | 174 +++++++++++++++++++++++++----------------- 1 file changed, 102 insertions(+), 72 deletions(-) diff --git a/example/example.ipynb b/example/example.ipynb index 1aca97e..0192c88 100644 --- a/example/example.ipynb +++ b/example/example.ipynb @@ -1,15 +1,83 @@ { "cells": [ { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# **A Jupyter widget to plot the bandstructure and density of states (DOS)**\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# This cell is only needed for development.\n", - "%load_ext autoreload\n", - "%autoreload 2\n", - "%env ANYWIDGET_HMR=1" + "

\n", + " This is a Jupyter widget, which plots the bandstructure and density of states from given json files.\n", + "

\n", + "\n", + "## **Input json files**\n", + "\n", + "

\n", + " On the left, it plots the bandstructures. One can input several bandstructure json files as a list.\n", + " The figure on the right shows the density of states, which can only show one DOS plot. The json files\n", + " for the bandstructures can be generated from AiiDA with the verdi command:\n", + "

\n", + "\n", + "```bash\n", + "verdi data bands export --format json \n", + "```\n", + "\n", + "

\n", + " The json format for the DOS can be checked in the github repository.\n", + "

\n", + "\n", + "\n", + "https://raw.githubusercontent.com/osscar-org/widget-bandsplot/develop/example/data/Si_dos.json" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "paramsparams

\n", + " Here, one needs to use the json package to load the json file and pass it to the widget.\n", + "

\n", + "\n", + "```python\n", + "with open('./data/Si_bands.json', 'r') as file:\n", + " band_Si = json.load(file)\n", + " \n", + "with open('./data/Si_dos.json', 'r') as file:\n", + " dos_Si = json.load(file)\n", + "```\n", + "\n", + "## **Fermi energy**\n", + "\n", + "

\n", + " The Fermi energy is reading from the bands and DOS json files. And bandstructure and density \n", + " of states plots are aligned to the Fermi energy (shift the Fermi energy to zero).\n", + "

\n", + "\n", + "

\n", + " In the default plot for the DOS, there is a horizontal line to highlight the Fermi level. One \n", + " can turn it off by setting plot_fermilevel = False. The legend of the DOS can be turned off\n", + " by set show_legend = False.\n", + "

\n", + "\n", + "## **Usage of the widget**\n", + "\n", + "

\n", + " Remeber to pass the bandstructure data as a list of json objects. \"energy_range\" sets the \n", + " energy range for the plots.\n", + "

\n", + "\n", + "### **Plot both bandstructure and DOS**\n", + "```python\n", + "w1 = BandsPlotWidget(bands=[band_Si], dos=dos_Si, plot_fermilevel = True, energy_range = {\"ymin\": -10.0, \"ymax\": 10.0})\n", + "display(w1)\n", + "```" ] }, { @@ -18,17 +86,9 @@ "metadata": {}, "outputs": [], "source": [ + "from widget_bandsplot import BandsPlotWidget\n", "import json\n", - "\n", - "def load_file(filename):\n", - " with open(filename, 'r') as fhandle:\n", - " return json.load(fhandle)\n", - "\n", - "si_bands = load_file(\"./data/Si_bands.json\")\n", - "si_dos = load_file(\"./data/Si_dos.json\")\n", - "co_bands = load_file(\"./data/Co_bands.json\")\n", - "co_dos = load_file(\"./data/Co_dos.json\")\n", - "si_bands_shifted = load_file(\"./data/Si_bands_Shifted.json\")" + "from copy import deepcopy" ] }, { @@ -37,16 +97,27 @@ "metadata": {}, "outputs": [], "source": [ - "from widget_bandsplot import BandsPlotWidget\n", - "# Note: the widget might not work in the vscode jupyter visualizer\n", - "# Jupyter notebook and lab seem to work fine." + "with open('./data/Si_bands.json', 'r') as file:\n", + " band_Si = json.load(file)\n", + " \n", + "with open('./data/Si_dos.json', 'r') as file:\n", + " dos_Si = json.load(file)\n", + " \n", + "with open('./data/Co_bands.json', 'r') as file:\n", + " band_Co = json.load(file)\n", + " \n", + "with open('./data/Co_dos.json', 'r') as file:\n", + " dos_Co = json.load(file)\n", + " \n", + "with open('./data/Si_bands_Shifted.json', 'r') as file:\n", + " band_Si_shifted = json.load(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Plot Si bandstructure and density of states" + "## Plot the Si bandstructure and density of states" ] }, { @@ -55,15 +126,7 @@ "metadata": {}, "outputs": [], "source": [ - "w1 = BandsPlotWidget(\n", - " bands = [si_bands],\n", - " dos = si_dos,\n", - " energy_range = [-8.0, 8.0],\n", - " format_settings = {\n", - " \"showFermi\": True,\n", - " \"showLegend\": True,\n", - " }\n", - ")\n", + "w1 = BandsPlotWidget(bands=[band_Si], dos=dos_Si, plot_fermilevel = True, show_legend = True, energy_range = [-10.0, 10.0])\n", "display(w1)" ] }, @@ -71,7 +134,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Plot Co bandstructure and density of states (spin polarized)" + "## Plot the Co bandstructure and density of states (spin polarized)" ] }, { @@ -80,11 +143,7 @@ "metadata": {}, "outputs": [], "source": [ - "w2 = BandsPlotWidget(\n", - " bands=[co_bands],\n", - " dos=co_dos,\n", - " energy_range = [-10.0, 10.0],\n", - " bands_color = [[\"red\", \"blue\"]])\n", + "w2 = BandsPlotWidget(bands=[band_Co], dos=dos_Co, plot_fermilevel = True, show_legend = True, energy_range = [-10.0, 10.0], bands_color = [[\"red\", \"blue\"]])\n", "display(w2)" ] }, @@ -101,10 +160,7 @@ "metadata": {}, "outputs": [], "source": [ - "w3 = BandsPlotWidget(\n", - " dos=co_dos,\n", - " energy_range = [-10.0, 10.0]\n", - ")\n", + "w3 = BandsPlotWidget(dos=dos_Co, plot_fermilevel = True, show_legend = True, energy_range = [-10,10])\n", "display(w3)" ] }, @@ -121,10 +177,7 @@ "metadata": {}, "outputs": [], "source": [ - "w4 = BandsPlotWidget(\n", - " bands=[si_bands],\n", - " energy_range = [-10.0, 10.0]\n", - ")\n", + "w4 = BandsPlotWidget(bands=[band_Si], plot_fermilevel = True, show_legend = True, energy_range = [-10.0, 10.0])\n", "display(w4)" ] }, @@ -132,7 +185,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Plot two bandstructures" + "## Plot two bandstructure data" ] }, { @@ -141,36 +194,10 @@ "metadata": {}, "outputs": [], "source": [ - "w5 = BandsPlotWidget(\n", - " bands=[si_bands, si_bands_shifted],\n", - " dos=si_dos,\n", - " energy_range = [-10.0, 10.0],\n", - " bands_color=['red', 'yellow']\n", - ")\n", + "w5 = BandsPlotWidget(bands=[band_Si, band_Si_shifted], dos=dos_Si, plot_fermilevel = True, show_legend = True, energy_range = [-10.0, 10.0], bands_color=['red', 'yellow'])\n", "display(w5)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null, @@ -196,6 +223,9 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" + }, + "voila": { + "authors": "Dou Du and Giovanni Pizzi" } }, "nbformat": 4,