forked from orest-d/p4vasp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup NGL Viewer class (orest-d#11)
A lot of the functionality previously contained in the Structure class is now moved to the Viewer3d class. The main reason for this change is that the structure class does not need to now about NGL at all. Also the interface is much leaner and more options can be choosen by the user afterwards if desired and don't expand the default interface.
- Loading branch information
1 parent
2fba540
commit db64e22
Showing
9 changed files
with
209 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
name = "py4vasp" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Martin Schlipf <[email protected]>"] | ||
authors = [ | ||
"Martin Schlipf <[email protected]>", | ||
"Orest Dubay <[email protected]>" | ||
] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.7" | ||
|
@@ -31,7 +34,9 @@ numpy = {channel = "anaconda"} | |
pandas = {channel = "anaconda"} | ||
cufflinks = {channel = "conda-forge", name = "cufflinks-py"} | ||
mdtraj = {channel = "conda-forge"} | ||
nglview = {channel = "conda-forge"} | ||
ase = {channel = "conda-forge"} | ||
pymatgen = {channel = "conda-forge"} | ||
sphinx = {channel = "anaconda"} | ||
|
||
[build-system] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import NamedTuple | ||
import nglview | ||
import numpy as np | ||
|
||
|
||
class _Arrow3d(NamedTuple): | ||
tail: np.ndarray | ||
tip: np.ndarray | ||
color: np.ndarray | ||
radius: float = 0.2 | ||
|
||
|
||
_x_axis = _Arrow3d(tail=np.zeros(3), tip=np.array((3, 0, 0)), color=[1, 0, 0]) | ||
_y_axis = _Arrow3d(tail=np.zeros(3), tip=np.array((0, 3, 0)), color=[0, 1, 0]) | ||
_z_axis = _Arrow3d(tail=np.zeros(3), tip=np.array((0, 0, 3)), color=[0, 0, 1]) | ||
|
||
|
||
class Viewer3d: | ||
"""Collection of data and elements to be displayed in a structure viewer""" | ||
|
||
def __init__(self, structure, supercell=None): | ||
self._structure = structure | ||
self._axes = None | ||
self._arrows = [] | ||
self.supercell = supercell | ||
self._ngl = self.show() | ||
|
||
def _ipython_display_(self): | ||
self._ngl._ipython_display_() | ||
|
||
def show_cell(self): | ||
self._ngl.add_unitcell() | ||
|
||
def hide_cell(self): | ||
self._ngl.remove_unitcell() | ||
|
||
def show_axes(self): | ||
if self._axes is not None: | ||
return | ||
self._axes = ( | ||
self._make_arrow(_x_axis), | ||
self._make_arrow(_y_axis), | ||
self._make_arrow(_z_axis), | ||
) | ||
|
||
def hide_axes(self): | ||
if self._axes is None: | ||
return | ||
for axis in self._axes: | ||
self._ngl.remove_component(axis) | ||
self._axes = None | ||
|
||
def show_arrows_at_atoms(self, arrows, color=[0.1, 0.1, 0.8]): | ||
structure = self._structure.to_pymatgen() | ||
for tail, arrow in zip(structure.cart_coords, arrows): | ||
tip = tail + arrow | ||
arrow = _Arrow3d(tail, tip, color) | ||
self._arrows.append(self._make_arrow(arrow)) | ||
|
||
def hide_arrows_at_atoms(self): | ||
for arrow in self._arrows: | ||
self._ngl.remove_component(arrow) | ||
self._arrows = [] | ||
|
||
def _make_arrow(self, arrow): | ||
return self._ngl.shape.add_arrow(*arrow) | ||
|
||
def show(self): | ||
structure = self._structure.to_pymatgen() | ||
if self.supercell is not None: | ||
structure.make_supercell(self.supercell) | ||
view = nglview.show_pymatgen(structure) | ||
return view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.