Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3dmol format for visualization in jupyter #357

Merged
merged 5 commits into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions dpdata/plugins/3dmol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Tuple
import numpy as np

from dpdata.format import Format
from dpdata.xyz.xyz import coord_to_xyz


@Format.register("3dmol")
class Py3DMolFormat(Format):
"""3DMol format.

To use this format, py3Dmol should be installed in advance.
"""
def to_system(self,
data: dict,
f_idx: int = 0,
size: Tuple[int] = (300,300),
style: dict = {"stick":{}, "sphere":{"radius":0.4}},
**kwargs):
"""Show 3D structure of a frame in jupyter.

Parameters
----------
data : dict
system data
f_idx : int
frame index to show
size : tuple[int]
(width, height) of the widget
style : dict
style of 3DMol. Read 3DMol documentation for details.

Examples
--------
>>> system.to_3dmol()
"""
import py3Dmol
types = np.array(data['atom_names'])[data['atom_types']]
xyz = coord_to_xyz(data['coords'][f_idx], types)
viewer = py3Dmol.view(width=size[0], height=size[1])
viewer.addModel(xyz, 'xyz')
viewer.setStyle(style.copy())
viewer.zoomTo()
return viewer