import numpy as np
import nglview as nv
import ase
import ase.visualize
from ase import Atoms
from IPython.display import display
import ipywidgets as widgets
from ipywidgets import interact,interactive
[ドキュメント]def view_with_index(atoms:Atoms, label_color="blue", label_scale=1.0):
"""元素毎のindex番号付きで構造を表示
Parameters:
atoms: Atoms
ase.Atoms
label_color: str
表示するindex番号の色
label_scale: float
ラベルの大きさ. Defaults to 1.0.
Returns:
nglview.widget.NGLWidget: display()で表示できる
"""
v = nv.show_ase(atoms, viewer="ngl")
v.add_label(
color=label_color, labelType="text",
labelText=[atoms[i].symbol + str(i) for i in range(atoms.get_global_number_of_atoms())],
zOffset=1.0, attachment='middle_center', radius=label_scale
)
return v
def _get_standard_pos(atoms: Atoms) -> np.ndarray:
# NGLViewer shows atoms in standard positions.
pos = atoms.positions
if atoms.get_pbc().any():
rcell, rot_t = atoms.cell.standard_form()
standard_pos = pos.dot(rot_t.T)
else:
standard_pos = pos
return standard_pos
[ドキュメント]def view_with_coordinate(atoms: Atoms, radius=0.5):
"""座標付きで構造を表示
Parameters:
atoms: Atoms
ase.Atoms
radius: float
原子のボールの大きさ
Returns:
nglview.widget.NGLWidget: display()で表示できる
"""
darkgrey = [0.6, 0.6, 0.6]
v = nv.show_ase(atoms, viewer="ngl")
pos = atoms.positions
standard_pos = _get_standard_pos(atoms)
for i in range(len(atoms)):
v.shape.add_sphere(standard_pos[i].tolist(), darkgrey, radius, f"x:{pos[i, 0]}, y:{pos[i, 1]}, z:{pos[i, 2]}")
return v
# def view_with_index(atoms , label_color="blue", label_scale=1.0):
# """原子毎のindex番号付きで構造を表示
# Parameters:
# atoms: Atoms
# ase.Atoms
# label_color: str
# 表示するindex番号の色
# label_scale: float
# ラベルの大きさ. Defaults to 1.0.
# Returns:
# nglview.widget.NGLWidget: display()で表示できる
# """
# v = nv.show_ase(atoms, viewer="ngl")
# v.add_label(
# color=label_color, labelType="text",
# labelText=[str(i) for i in range(len(atoms))],
# zOffset=1.0, attachment='middle_center', radius=label_scale
# )
# return v
[ドキュメント]def view_images(imagess):
"""再生ボタン付きで構造を表示
Parameters:
imagess: list of ase.Atoms or 2D list of ase.Atoms
ase.Atomsの1Dまたは2Dリスト
"""
if type(imagess)==list:
if type(imagess[0])== Atoms:
"""再生ボタン付き"""
v = ase.visualize.view(imagess, viewer='ngl')
v.view.add_representation("ball+stick")
return v
elif type(imagess[0])== list:
def disp_structure(i):
if imagess[i]:
v = ase.visualize.view(imagess[i], viewer='ngl')
else:
v = ase.visualize.view(Atoms(), viewer='ngl')
v.view.add_representation("ball+stick")
display(v)
item = widgets.BoundedIntText(
value=0,
min=0,
max=len(imagess)-1,
step=1,
description=' ',
disabled=False
)
slider = widgets.IntSlider(
value=0,
min=0,
max=len(imagess)-1,
step=1,
description=' ',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d'
)
mylink = widgets.link((item, 'value'), (slider, 'value'))
w = interactive(disp_structure, i=slider)
display(item)
display(w)
else:
raise TypeError("引数が間違っています")
[ドキュメント]def view(atoms):
"""構造を表示
Parameters:
imagess: ase.Atoms or list of ase.Atoms
ase.Atomsまたはase.Atomsのリスト
"""
if type(atoms)==list:
"""Atomsリストで与えられた場合"""
def disp_structure(i):
if atoms[i]:
v = ase.visualize.view(atoms[i], viewer='ngl')
else:
v = ase.visualize.view(Atoms(), viewer='ngl')
v.view.add_representation("ball+stick")
display(v)
item = widgets.BoundedIntText(
value=0,
min=0,
max=len(atoms)-1,
step=1,
description=' ',
disabled=False
)
slider = widgets.IntSlider(
value=0,
min=0,
max=len(atoms)-1,
step=1,
description=' ',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d'
)
mylink = widgets.link((item, 'value'), (slider, 'value'))
w = interactive(disp_structure, i=slider)
display(item)
display(w)
elif type(atoms)==Atoms:
"""Atomsで与えられた場合"""
v = ase.visualize.view(atoms, viewer='ngl')
v.view.add_representation("ball+stick")
display(v)
else:
raise TypeError("引数が間違っています")