Skip to content

Commit

Permalink
Fix readme and example (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
eimrek authored Aug 19, 2024
1 parent 7ece308 commit 9832a59
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 133 deletions.
116 changes: 47 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,61 @@ A Jupyter widget to plot band structures and density of states. The widget is us

<img src="./example/demo.gif" width='1200'>

## Installation & usage
## Installation

```sh
pip install widget-bandsplot
```
## Usage

### 1. Plot both the band structure and the density of states (DOS) side by side

```python
w = BandsPlotWidget(bands=[banddata1, banddata2], dos=dosdata, plot_fermilevel = True, show_legend = True, energy_range = [-10,10])
display(w)
```

In order to plot the band structure and density of states, one needs
to provide bands data and DOS data as JSON-files. The examples of the input
JSON-files are provided in the `examples/data` folder. The JSON-files for the
band structure can be exported with the [AiiDA command line interface (CLI) `verdi`](https://aiida.readthedocs.io/projects/aiida-core/en/latest/reference/command_line.html#reference-command-line) as demonstrated in
the code below:

```bash
verdi data band export <PK> --format=json
```

One can plot several band structure input files together with the widget.
One exampla valid input is:

```python
dos_data = {
"fermi_energy": -7.0,
"dos": [
{
"label": "Total DOS", # required
"x": [0.0, 0.1, 0.2], # required
"y": [1.2, 3.2, 0.0], # required
"borderColor": "#41e2b3", # optional
"backgroundColor": "#51258b", # optional
"backgroundAlpha": "52%", #optional: A string with integer between 0-100 and '%' in end.
"lineStyle": "dash", # optional
},
{
"label": "Co (s↑)",
"x": [0.0, 0.1, 0.2],
"y": [1.2, 3.2, 0.0],
"lineStyle": "solid",
"borderColor": "#43ee8b",
"backgroundColor": "#59595c",
},
{
"label": "Co (s↓)",
"x": [0.0, 0.1, 0.2],
"y": [1.2, 3.2, 0.0],
"lineStyle": "solid",
"borderColor": "#403bae",
"backgroundColor": "#a16c5e",
},
],
}
```

### 2. Plot only the band structure

```python
w = BandsPlotWidget(bands=[banddata1, banddata2], format_settings = {"plotFermil": True, "showLegend": True}, energy_range = [-10,10])
display(w)
```
## Usage

### 3. Plot only the density of states (DOS)
Minimal usage example of the widget is the following:

```python
w = BandsPlotWidget(dos=dosdata, format_settings = {"plotFermil": True, "showLegend": True}, energy_range = [-10, 10])
display(w)
widget = BandsPlotWidget(
bands = [bands_data],
dos = dos_data,
energy_range = [-10.0, 10.0],
format_settings = {
"showFermi": True,
"showLegend": True,
}
)
display(widget)
```

When only plotting the density of states, the plot will be shown in
horizontal format.

where `bands_data` and `dos_data` are contain the band structure and density of states data, respectively. The format for these data objects is the following:

- Band structure data follows the [AiiDA CLI](https://aiida.readthedocs.io/projects/aiida-core/en/latest/reference/command_line.html#reference-command-line) export format that can be generated from an [AiiDA BandsData](https://aiida.readthedocs.io/projects/aiida-core/en/v2.6.2/topics/data_types.html#topics-data-types-materials-bands) node with the following command:
```bash
verdi data band export <PK> --format=json
```
- The density of states data uses a custom format, with a a valid example being:
```python
dos_data = {
"fermi_energy": -7.0,
"dos": [
{
"label": "Total DOS", # required
"x": [0.0, 0.1, 0.2], # required
"y": [1.2, 3.2, 0.0], # required
"lineStyle": "dash", # optional
"borderColor": "#41e2b3", # optional
"backgroundColor": "#51258b", # optional
},
{
"label": "Co",
"x": [0.0, 0.1, 0.2],
"y": [1.2, 3.2, 0.0],
"lineStyle": "solid",
"borderColor": "#43ee8b",
"backgroundColor": "#59595c",
},
],
}
```

For more detailed usage, see `example/example.ipynb` and for more example input files see `example/data`.

## Development

Expand Down Expand Up @@ -126,8 +104,8 @@ being displayed correctly in the test.

[![screenshot comparison](https://github.com/osscar-org/widget-bandsplot/actions/workflows/screenshot-comparison.yml/badge.svg)](https://github.com/osscar-org/widget-bandsplot/actions/workflows/screenshot-comparison.yml)

If the `widget test` passes but the `screenshot comparison` fails, it indicates the appearance of the widget
is different from the previous version. In this case, you'll need to manually download the artifact from
If the `widget test` passes but the `screenshot comparison` fails, it indicates the appearance of the widget
is different from the previous version. In this case, you'll need to manually download the artifact from
the `widget test` and use it to replace the `widget-sample.png` figure in the `test` folder.
## Acknowledgements
Expand Down
89 changes: 25 additions & 64 deletions example/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,35 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<p style=\"text-align: justify;font-size:20px\">\n",
" This is a Jupyter widget, which plots the bandstructure and density of states from given json files.\n",
"</p>\n",
"This is a Jupyter widget, which plots the bandstructure and density of states from given json files.\n",
"\n",
"## **Input json files**\n",
"## Input json files\n",
"\n",
"<p style=\"text-align: justify;font-size:20px\">\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",
"</p>\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",
"```bash\n",
"verdi data bands export --format json <IDENTIFIER>\n",
"```\n",
"\n",
"<p style=\"text-align: justify;font-size:20px\">\n",
" The json format for the DOS can be checked in the github repository.\n",
"</p>\n",
"The json format for the DOS can be checked from the example data files, e.g. `data/Si_dos.json`.\n",
"\n",
"<a href=\"https://raw.githubusercontent.com/osscar-org/widget-bandsplot/develop/example/data/Si_dos.json\">\n",
"https://raw.githubusercontent.com/osscar-org/widget-bandsplot/develop/example/data/Si_dos.json</a>"
"## Note on Fermi energy\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",
"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 showFermi = False. The legend of the DOS can be turned off\n",
"by set showLegend = False."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p style=\"text-align: justify;font-size:20px\">\n",
" Here, one needs to use the json package to load the json file and pass it to the widget.\n",
"</p>\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",
"<p style=\"text-align: justify;font-size:20px\">\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",
"</p>\n",
"\n",
"<p style=\"text-align: justify;font-size:20px\">\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 showFermi = False. The legend of the DOS can be turned off\n",
" by set showLegend = False.\n",
"</p>\n",
"\n",
"## **Usage of the widget**\n",
"\n",
"<p style=\"text-align: justify;font-size:20px\">\n",
" Remeber to pass the bandstructure data as a list of json objects. \"energy_range\" sets the \n",
" energy range for the plots.\n",
"</p>\n",
"\n",
"### **Plot both bandstructure and DOS**\n",
"```python\n",
"w1 = BandsPlotWidget(\n",
" bands = [si_bands],\n",
" dos = si_dos,\n",
" energy_range = [-10.0, 10.0],\n",
" format_settings = {\n",
" \"showFermi\": True,\n",
" \"showLegend\": True,\n",
" }\n",
")\n",
"display(w1)\n",
"```"
"## Import modules and load example data files"
]
},
{
Expand All @@ -94,9 +50,10 @@
"metadata": {},
"outputs": [],
"source": [
"from widget_bandsplot import BandsPlotWidget\n",
"import json\n",
"from copy import deepcopy"
"# This cell is only needed for development, for javascript changes to be reflected in the notebook automatically.\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"%env ANYWIDGET_HMR=1"
]
},
{
Expand All @@ -105,6 +62,10 @@
"metadata": {},
"outputs": [],
"source": [
"from widget_bandsplot import BandsPlotWidget\n",
"\n",
"import json\n",
"\n",
"def load_file(filename):\n",
" with open(filename, 'r') as fhandle:\n",
" return json.load(fhandle)\n",
Expand Down Expand Up @@ -230,7 +191,7 @@
" bands=[si_bands, si_bands_shifted],\n",
" dos=si_dos,\n",
" energy_range = [-10.0, 10.0],\n",
" bands_color=['red', 'yellow']\n",
" bands_color=['red', 'green']\n",
")\n",
"display(w5)"
]
Expand Down Expand Up @@ -259,7 +220,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.10.0"
},
"voila": {
"authors": "Dou Du and Giovanni Pizzi"
Expand Down

0 comments on commit 9832a59

Please sign in to comment.